]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
219d1afa 2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 56static bfd_vma opd_entry_value
aef36ac1 57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 58
6d00b590 59#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 60#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 61#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
04c6a44c 67#define ELF_COMMONPAGESIZE 0x10000
ad8e1ba5
AM
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
ad8e1ba5 74#define elf_backend_got_header_size 8
5474d94f 75#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
76#define elf_backend_can_gc_sections 1
77#define elf_backend_can_refcount 1
78#define elf_backend_rela_normal 1
64f52338 79#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 80#define elf_backend_default_execstack 0
ad8e1ba5 81
e717da7e 82#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 83#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 84#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
85#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
86#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
87#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
88#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 89#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 90#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 91#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
92
93#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
94#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
95#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 96#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 97#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 98#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 99#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 100#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 101#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 102#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 103#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 104#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 105#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 106#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 107#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
108#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
109#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 110#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
111#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
112#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 113#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 114#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 115#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
116#define elf_backend_relocate_section ppc64_elf_relocate_section
117#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
118#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
119#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 120#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 121#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 122#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 123#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 124#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 125
5bd4f169
AM
126/* The name of the dynamic interpreter. This is put in the .interp
127 section. */
128#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
129
130/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 131#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
132
133/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 134#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 135
a078d95a
AM
136/* Offsets to some stack save slots. */
137#define STK_LR 16
138#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 139/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
140 CR save slot. Used only by optimised __tls_get_addr call stub,
141 relying on __tls_get_addr_opt not saving CR.. */
142#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
143
5bd4f169 144/* TOC base pointers offset from start of TOC. */
411e1bfb 145#define TOC_BASE_OFF 0x8000
a27e685f
AM
146/* TOC base alignment. */
147#define TOC_BASE_ALIGN 256
411e1bfb
AM
148
149/* Offset of tp and dtp pointers from start of TLS block. */
150#define TP_OFFSET 0x7000
151#define DTP_OFFSET 0x8000
5bd4f169 152
ad8e1ba5
AM
153/* .plt call stub instructions. The normal stub is like this, but
154 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 155 insert an addi to adjust r11. */
a078d95a 156#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
157#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
158#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
159#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
160#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
161#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
162#define BCTR 0x4e800420 /* bctr */
163
1be5d8d3
AM
164#define CRSETEQ 0x4c421242 /* crset 4*%cr0+%eq */
165#define BEQCTRM 0x4dc20420 /* beqctr- */
166#define BEQCTRLM 0x4dc20421 /* beqctrl- */
167
07d6d2b8
AM
168#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
169#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
171
172#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177#define BNECTR 0x4ca20420 /* bnectr+ */
178#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 179
71a39c98 180#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 181#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 182#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 183
07d6d2b8
AM
184#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 187
07d6d2b8 188#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
006589cf
AM
189#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
190#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d 191#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
07d6d2b8 192#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
a345bc8d 193
9e390558
AM
194/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
195#define GLINK_PLTRESOLVE_SIZE(htab) \
1be5d8d3
AM
196 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4) \
197 + (!htab->params->speculate_indirect_jumps ? 2 * 4 : 0))
ee4bf8d2
AM
198 /* 0: */
199 /* .quad plt0-1f */
200 /* __glink: */
201#define MFLR_R12 0x7d8802a6 /* mflr %12 */
202#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
203 /* 1: */
204#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 205 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 206#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
207#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
208 /* ld %12,0(%11) */
209 /* ld %2,8(%11) */
210 /* mtctr %12 */
211 /* ld %11,16(%11) */
ee4bf8d2 212 /* bctr */
b9e5796b
AM
213#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
214#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
215#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
216#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
217#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
218
219/* Pad with this. */
220#define NOP 0x60000000
221
721956f4
AM
222/* Some other nops. */
223#define CROR_151515 0x4def7b82
224#define CROR_313131 0x4ffffb82
225
cedb70c5 226/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
227#define LI_R0_0 0x38000000 /* li %r0,0 */
228#define B_DOT 0x48000000 /* b . */
229
230/* After that, we need two instructions to load the index, followed by
231 a branch. */
232#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 233#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 234
deb0e272
AM
235/* Instructions used by the save and restore reg functions. */
236#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
237#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
238#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
239#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
240#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
241#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
242#define LI_R12_0 0x39800000 /* li %r12,0 */
243#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
244#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
245#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
246#define BLR 0x4e800020 /* blr */
247
41bd81ab
AM
248/* Since .opd is an array of descriptors and each entry will end up
249 with identical R_PPC64_RELATIVE relocs, there is really no need to
250 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 251 relocate .opd without reloc entries. */
41bd81ab
AM
252#ifndef NO_OPD_RELOCS
253#define NO_OPD_RELOCS 0
254#endif
810d4e75 255
a4b6fadd
AM
256#ifndef ARRAY_SIZE
257#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
258#endif
259
810d4e75
AM
260static inline int
261abiversion (bfd *abfd)
262{
263 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
264}
265
266static inline void
267set_abiversion (bfd *abfd, int ver)
268{
269 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
270 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
271}
5bd4f169 272\f
f5e87a1d 273#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 274
5bd4f169 275/* Relocation HOWTO's. */
04c9666a 276static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
277
278static reloc_howto_type ppc64_elf_howto_raw[] = {
279 /* This reloc does nothing. */
280 HOWTO (R_PPC64_NONE, /* type */
281 0, /* rightshift */
6346d5ca
AM
282 3, /* size (0 = byte, 1 = short, 2 = long) */
283 0, /* bitsize */
b34976b6 284 FALSE, /* pc_relative */
5bd4f169 285 0, /* bitpos */
f5e87a1d 286 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
287 bfd_elf_generic_reloc, /* special_function */
288 "R_PPC64_NONE", /* name */
b34976b6 289 FALSE, /* partial_inplace */
d006db6c 290 0, /* src_mask */
5bd4f169 291 0, /* dst_mask */
b34976b6 292 FALSE), /* pcrel_offset */
5bd4f169
AM
293
294 /* A standard 32 bit relocation. */
295 HOWTO (R_PPC64_ADDR32, /* type */
296 0, /* rightshift */
297 2, /* size (0 = byte, 1 = short, 2 = long) */
298 32, /* bitsize */
b34976b6 299 FALSE, /* pc_relative */
5bd4f169
AM
300 0, /* bitpos */
301 complain_overflow_bitfield, /* complain_on_overflow */
302 bfd_elf_generic_reloc, /* special_function */
303 "R_PPC64_ADDR32", /* name */
b34976b6 304 FALSE, /* partial_inplace */
5bd4f169
AM
305 0, /* src_mask */
306 0xffffffff, /* dst_mask */
b34976b6 307 FALSE), /* pcrel_offset */
5bd4f169
AM
308
309 /* An absolute 26 bit branch; the lower two bits must be zero.
310 FIXME: we don't check that, we just clear them. */
311 HOWTO (R_PPC64_ADDR24, /* type */
312 0, /* rightshift */
313 2, /* size (0 = byte, 1 = short, 2 = long) */
314 26, /* bitsize */
b34976b6 315 FALSE, /* pc_relative */
5bd4f169
AM
316 0, /* bitpos */
317 complain_overflow_bitfield, /* complain_on_overflow */
318 bfd_elf_generic_reloc, /* special_function */
319 "R_PPC64_ADDR24", /* name */
b34976b6 320 FALSE, /* partial_inplace */
d006db6c 321 0, /* src_mask */
f5e87a1d 322 0x03fffffc, /* dst_mask */
b34976b6 323 FALSE), /* pcrel_offset */
5bd4f169
AM
324
325 /* A standard 16 bit relocation. */
326 HOWTO (R_PPC64_ADDR16, /* type */
327 0, /* rightshift */
328 1, /* size (0 = byte, 1 = short, 2 = long) */
329 16, /* bitsize */
b34976b6 330 FALSE, /* pc_relative */
5bd4f169
AM
331 0, /* bitpos */
332 complain_overflow_bitfield, /* complain_on_overflow */
333 bfd_elf_generic_reloc, /* special_function */
334 "R_PPC64_ADDR16", /* name */
b34976b6 335 FALSE, /* partial_inplace */
5bd4f169
AM
336 0, /* src_mask */
337 0xffff, /* dst_mask */
b34976b6 338 FALSE), /* pcrel_offset */
5bd4f169
AM
339
340 /* A 16 bit relocation without overflow. */
341 HOWTO (R_PPC64_ADDR16_LO, /* type */
342 0, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 16, /* bitsize */
b34976b6 345 FALSE, /* pc_relative */
5bd4f169
AM
346 0, /* bitpos */
347 complain_overflow_dont,/* complain_on_overflow */
348 bfd_elf_generic_reloc, /* special_function */
349 "R_PPC64_ADDR16_LO", /* name */
b34976b6 350 FALSE, /* partial_inplace */
5bd4f169
AM
351 0, /* src_mask */
352 0xffff, /* dst_mask */
b34976b6 353 FALSE), /* pcrel_offset */
5bd4f169
AM
354
355 /* Bits 16-31 of an address. */
356 HOWTO (R_PPC64_ADDR16_HI, /* type */
357 16, /* rightshift */
358 1, /* size (0 = byte, 1 = short, 2 = long) */
359 16, /* bitsize */
b34976b6 360 FALSE, /* pc_relative */
5bd4f169 361 0, /* bitpos */
f9c6b907 362 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
363 bfd_elf_generic_reloc, /* special_function */
364 "R_PPC64_ADDR16_HI", /* name */
b34976b6 365 FALSE, /* partial_inplace */
5bd4f169
AM
366 0, /* src_mask */
367 0xffff, /* dst_mask */
b34976b6 368 FALSE), /* pcrel_offset */
5bd4f169
AM
369
370 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
371 bits, treated as a signed number, is negative. */
372 HOWTO (R_PPC64_ADDR16_HA, /* type */
373 16, /* rightshift */
374 1, /* size (0 = byte, 1 = short, 2 = long) */
375 16, /* bitsize */
b34976b6 376 FALSE, /* pc_relative */
5bd4f169 377 0, /* bitpos */
f9c6b907 378 complain_overflow_signed, /* complain_on_overflow */
805fc799 379 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 380 "R_PPC64_ADDR16_HA", /* name */
b34976b6 381 FALSE, /* partial_inplace */
5bd4f169
AM
382 0, /* src_mask */
383 0xffff, /* dst_mask */
b34976b6 384 FALSE), /* pcrel_offset */
5bd4f169
AM
385
386 /* An absolute 16 bit branch; the lower two bits must be zero.
387 FIXME: we don't check that, we just clear them. */
388 HOWTO (R_PPC64_ADDR14, /* type */
389 0, /* rightshift */
390 2, /* size (0 = byte, 1 = short, 2 = long) */
391 16, /* bitsize */
b34976b6 392 FALSE, /* pc_relative */
5bd4f169 393 0, /* bitpos */
b80eed39 394 complain_overflow_signed, /* complain_on_overflow */
2441e016 395 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 396 "R_PPC64_ADDR14", /* name */
b34976b6 397 FALSE, /* partial_inplace */
d006db6c 398 0, /* src_mask */
f5e87a1d 399 0x0000fffc, /* dst_mask */
b34976b6 400 FALSE), /* pcrel_offset */
5bd4f169
AM
401
402 /* An absolute 16 bit branch, for which bit 10 should be set to
403 indicate that the branch is expected to be taken. The lower two
404 bits must be zero. */
405 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
406 0, /* rightshift */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
408 16, /* bitsize */
b34976b6 409 FALSE, /* pc_relative */
5bd4f169 410 0, /* bitpos */
b80eed39 411 complain_overflow_signed, /* complain_on_overflow */
805fc799 412 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 413 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 414 FALSE, /* partial_inplace */
d006db6c 415 0, /* src_mask */
f5e87a1d 416 0x0000fffc, /* dst_mask */
b34976b6 417 FALSE), /* pcrel_offset */
5bd4f169
AM
418
419 /* An absolute 16 bit branch, for which bit 10 should be set to
420 indicate that the branch is not expected to be taken. The lower
421 two bits must be zero. */
422 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
423 0, /* rightshift */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
425 16, /* bitsize */
b34976b6 426 FALSE, /* pc_relative */
5bd4f169 427 0, /* bitpos */
b80eed39 428 complain_overflow_signed, /* complain_on_overflow */
805fc799 429 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 430 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 431 FALSE, /* partial_inplace */
d006db6c 432 0, /* src_mask */
f5e87a1d 433 0x0000fffc, /* dst_mask */
b34976b6 434 FALSE), /* pcrel_offset */
5bd4f169
AM
435
436 /* A relative 26 bit branch; the lower two bits must be zero. */
437 HOWTO (R_PPC64_REL24, /* type */
438 0, /* rightshift */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
440 26, /* bitsize */
b34976b6 441 TRUE, /* pc_relative */
5bd4f169
AM
442 0, /* bitpos */
443 complain_overflow_signed, /* complain_on_overflow */
2441e016 444 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 445 "R_PPC64_REL24", /* name */
b34976b6 446 FALSE, /* partial_inplace */
d006db6c 447 0, /* src_mask */
f5e87a1d 448 0x03fffffc, /* dst_mask */
b34976b6 449 TRUE), /* pcrel_offset */
5bd4f169
AM
450
451 /* A relative 16 bit branch; the lower two bits must be zero. */
452 HOWTO (R_PPC64_REL14, /* type */
453 0, /* rightshift */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
455 16, /* bitsize */
b34976b6 456 TRUE, /* pc_relative */
5bd4f169
AM
457 0, /* bitpos */
458 complain_overflow_signed, /* complain_on_overflow */
2441e016 459 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 460 "R_PPC64_REL14", /* name */
b34976b6 461 FALSE, /* partial_inplace */
d006db6c 462 0, /* src_mask */
f5e87a1d 463 0x0000fffc, /* dst_mask */
b34976b6 464 TRUE), /* pcrel_offset */
5bd4f169
AM
465
466 /* A relative 16 bit branch. Bit 10 should be set to indicate that
467 the branch is expected to be taken. The lower two bits must be
468 zero. */
469 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
470 0, /* rightshift */
471 2, /* size (0 = byte, 1 = short, 2 = long) */
472 16, /* bitsize */
b34976b6 473 TRUE, /* pc_relative */
5bd4f169
AM
474 0, /* bitpos */
475 complain_overflow_signed, /* complain_on_overflow */
805fc799 476 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 477 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 478 FALSE, /* partial_inplace */
d006db6c 479 0, /* src_mask */
f5e87a1d 480 0x0000fffc, /* dst_mask */
b34976b6 481 TRUE), /* pcrel_offset */
5bd4f169
AM
482
483 /* A relative 16 bit branch. Bit 10 should be set to indicate that
484 the branch is not expected to be taken. The lower two bits must
485 be zero. */
486 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
487 0, /* rightshift */
488 2, /* size (0 = byte, 1 = short, 2 = long) */
489 16, /* bitsize */
b34976b6 490 TRUE, /* pc_relative */
5bd4f169
AM
491 0, /* bitpos */
492 complain_overflow_signed, /* complain_on_overflow */
805fc799 493 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 494 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 495 FALSE, /* partial_inplace */
d006db6c 496 0, /* src_mask */
f5e87a1d 497 0x0000fffc, /* dst_mask */
b34976b6 498 TRUE), /* pcrel_offset */
5bd4f169
AM
499
500 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
501 symbol. */
502 HOWTO (R_PPC64_GOT16, /* type */
503 0, /* rightshift */
504 1, /* size (0 = byte, 1 = short, 2 = long) */
505 16, /* bitsize */
b34976b6 506 FALSE, /* pc_relative */
5bd4f169
AM
507 0, /* bitpos */
508 complain_overflow_signed, /* complain_on_overflow */
805fc799 509 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 510 "R_PPC64_GOT16", /* name */
b34976b6 511 FALSE, /* partial_inplace */
5bd4f169
AM
512 0, /* src_mask */
513 0xffff, /* dst_mask */
b34976b6 514 FALSE), /* pcrel_offset */
5bd4f169
AM
515
516 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
517 the symbol. */
518 HOWTO (R_PPC64_GOT16_LO, /* type */
519 0, /* rightshift */
520 1, /* size (0 = byte, 1 = short, 2 = long) */
521 16, /* bitsize */
b34976b6 522 FALSE, /* pc_relative */
5bd4f169
AM
523 0, /* bitpos */
524 complain_overflow_dont, /* complain_on_overflow */
805fc799 525 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 526 "R_PPC64_GOT16_LO", /* name */
b34976b6 527 FALSE, /* partial_inplace */
5bd4f169
AM
528 0, /* src_mask */
529 0xffff, /* dst_mask */
b34976b6 530 FALSE), /* pcrel_offset */
5bd4f169
AM
531
532 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
533 the symbol. */
534 HOWTO (R_PPC64_GOT16_HI, /* type */
535 16, /* rightshift */
536 1, /* size (0 = byte, 1 = short, 2 = long) */
537 16, /* bitsize */
b34976b6 538 FALSE, /* pc_relative */
5bd4f169 539 0, /* bitpos */
f9c6b907 540 complain_overflow_signed,/* complain_on_overflow */
805fc799 541 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 542 "R_PPC64_GOT16_HI", /* name */
b34976b6 543 FALSE, /* partial_inplace */
5bd4f169
AM
544 0, /* src_mask */
545 0xffff, /* dst_mask */
b34976b6 546 FALSE), /* pcrel_offset */
5bd4f169
AM
547
548 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
549 the symbol. */
550 HOWTO (R_PPC64_GOT16_HA, /* type */
551 16, /* rightshift */
552 1, /* size (0 = byte, 1 = short, 2 = long) */
553 16, /* bitsize */
b34976b6 554 FALSE, /* pc_relative */
5bd4f169 555 0, /* bitpos */
f9c6b907 556 complain_overflow_signed,/* complain_on_overflow */
805fc799 557 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 558 "R_PPC64_GOT16_HA", /* name */
b34976b6 559 FALSE, /* partial_inplace */
5bd4f169
AM
560 0, /* src_mask */
561 0xffff, /* dst_mask */
b34976b6 562 FALSE), /* pcrel_offset */
5bd4f169
AM
563
564 /* This is used only by the dynamic linker. The symbol should exist
565 both in the object being run and in some shared library. The
566 dynamic linker copies the data addressed by the symbol from the
567 shared library into the object, because the object being
568 run has to have the data at some particular address. */
569 HOWTO (R_PPC64_COPY, /* type */
570 0, /* rightshift */
f5e87a1d
AM
571 0, /* this one is variable size */
572 0, /* bitsize */
b34976b6 573 FALSE, /* pc_relative */
5bd4f169 574 0, /* bitpos */
f5e87a1d
AM
575 complain_overflow_dont, /* complain_on_overflow */
576 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 577 "R_PPC64_COPY", /* name */
b34976b6 578 FALSE, /* partial_inplace */
5bd4f169
AM
579 0, /* src_mask */
580 0, /* dst_mask */
b34976b6 581 FALSE), /* pcrel_offset */
5bd4f169
AM
582
583 /* Like R_PPC64_ADDR64, but used when setting global offset table
584 entries. */
585 HOWTO (R_PPC64_GLOB_DAT, /* type */
586 0, /* rightshift */
587 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
588 64, /* bitsize */
b34976b6 589 FALSE, /* pc_relative */
5bd4f169
AM
590 0, /* bitpos */
591 complain_overflow_dont, /* complain_on_overflow */
805fc799 592 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 593 "R_PPC64_GLOB_DAT", /* name */
b34976b6 594 FALSE, /* partial_inplace */
5bd4f169 595 0, /* src_mask */
f5e87a1d 596 ONES (64), /* dst_mask */
b34976b6 597 FALSE), /* pcrel_offset */
5bd4f169
AM
598
599 /* Created by the link editor. Marks a procedure linkage table
600 entry for a symbol. */
601 HOWTO (R_PPC64_JMP_SLOT, /* type */
602 0, /* rightshift */
603 0, /* size (0 = byte, 1 = short, 2 = long) */
604 0, /* bitsize */
b34976b6 605 FALSE, /* pc_relative */
5bd4f169
AM
606 0, /* bitpos */
607 complain_overflow_dont, /* complain_on_overflow */
805fc799 608 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 609 "R_PPC64_JMP_SLOT", /* name */
b34976b6 610 FALSE, /* partial_inplace */
5bd4f169
AM
611 0, /* src_mask */
612 0, /* dst_mask */
b34976b6 613 FALSE), /* pcrel_offset */
5bd4f169
AM
614
615 /* Used only by the dynamic linker. When the object is run, this
616 doubleword64 is set to the load address of the object, plus the
617 addend. */
618 HOWTO (R_PPC64_RELATIVE, /* type */
619 0, /* rightshift */
620 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
621 64, /* bitsize */
b34976b6 622 FALSE, /* pc_relative */
5bd4f169
AM
623 0, /* bitpos */
624 complain_overflow_dont, /* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
626 "R_PPC64_RELATIVE", /* name */
b34976b6 627 FALSE, /* partial_inplace */
5bd4f169 628 0, /* src_mask */
f5e87a1d 629 ONES (64), /* dst_mask */
b34976b6 630 FALSE), /* pcrel_offset */
5bd4f169
AM
631
632 /* Like R_PPC64_ADDR32, but may be unaligned. */
633 HOWTO (R_PPC64_UADDR32, /* type */
634 0, /* rightshift */
635 2, /* size (0 = byte, 1 = short, 2 = long) */
636 32, /* bitsize */
b34976b6 637 FALSE, /* pc_relative */
5bd4f169
AM
638 0, /* bitpos */
639 complain_overflow_bitfield, /* complain_on_overflow */
640 bfd_elf_generic_reloc, /* special_function */
641 "R_PPC64_UADDR32", /* name */
b34976b6 642 FALSE, /* partial_inplace */
5bd4f169
AM
643 0, /* src_mask */
644 0xffffffff, /* dst_mask */
b34976b6 645 FALSE), /* pcrel_offset */
5bd4f169
AM
646
647 /* Like R_PPC64_ADDR16, but may be unaligned. */
648 HOWTO (R_PPC64_UADDR16, /* type */
649 0, /* rightshift */
650 1, /* size (0 = byte, 1 = short, 2 = long) */
651 16, /* bitsize */
b34976b6 652 FALSE, /* pc_relative */
5bd4f169
AM
653 0, /* bitpos */
654 complain_overflow_bitfield, /* complain_on_overflow */
655 bfd_elf_generic_reloc, /* special_function */
656 "R_PPC64_UADDR16", /* name */
b34976b6 657 FALSE, /* partial_inplace */
5bd4f169
AM
658 0, /* src_mask */
659 0xffff, /* dst_mask */
b34976b6 660 FALSE), /* pcrel_offset */
5bd4f169
AM
661
662 /* 32-bit PC relative. */
663 HOWTO (R_PPC64_REL32, /* type */
664 0, /* rightshift */
665 2, /* size (0 = byte, 1 = short, 2 = long) */
666 32, /* bitsize */
b34976b6 667 TRUE, /* pc_relative */
5bd4f169 668 0, /* bitpos */
5bd4f169
AM
669 complain_overflow_signed, /* complain_on_overflow */
670 bfd_elf_generic_reloc, /* special_function */
671 "R_PPC64_REL32", /* name */
b34976b6 672 FALSE, /* partial_inplace */
5bd4f169
AM
673 0, /* src_mask */
674 0xffffffff, /* dst_mask */
b34976b6 675 TRUE), /* pcrel_offset */
5bd4f169 676
10ed1bba 677 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
678 HOWTO (R_PPC64_PLT32, /* type */
679 0, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 32, /* bitsize */
b34976b6 682 FALSE, /* pc_relative */
5bd4f169
AM
683 0, /* bitpos */
684 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 685 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 686 "R_PPC64_PLT32", /* name */
b34976b6 687 FALSE, /* partial_inplace */
5bd4f169 688 0, /* src_mask */
f5e87a1d 689 0xffffffff, /* dst_mask */
b34976b6 690 FALSE), /* pcrel_offset */
5bd4f169
AM
691
692 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
693 FIXME: R_PPC64_PLTREL32 not supported. */
694 HOWTO (R_PPC64_PLTREL32, /* type */
695 0, /* rightshift */
696 2, /* size (0 = byte, 1 = short, 2 = long) */
697 32, /* bitsize */
b34976b6 698 TRUE, /* pc_relative */
5bd4f169
AM
699 0, /* bitpos */
700 complain_overflow_signed, /* complain_on_overflow */
3ce51288 701 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 702 "R_PPC64_PLTREL32", /* name */
b34976b6 703 FALSE, /* partial_inplace */
5bd4f169 704 0, /* src_mask */
f5e87a1d 705 0xffffffff, /* dst_mask */
b34976b6 706 TRUE), /* pcrel_offset */
5bd4f169
AM
707
708 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
709 the symbol. */
710 HOWTO (R_PPC64_PLT16_LO, /* type */
711 0, /* rightshift */
712 1, /* size (0 = byte, 1 = short, 2 = long) */
713 16, /* bitsize */
b34976b6 714 FALSE, /* pc_relative */
5bd4f169
AM
715 0, /* bitpos */
716 complain_overflow_dont, /* complain_on_overflow */
805fc799 717 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 718 "R_PPC64_PLT16_LO", /* name */
b34976b6 719 FALSE, /* partial_inplace */
5bd4f169
AM
720 0, /* src_mask */
721 0xffff, /* dst_mask */
b34976b6 722 FALSE), /* pcrel_offset */
5bd4f169
AM
723
724 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
725 the symbol. */
726 HOWTO (R_PPC64_PLT16_HI, /* type */
727 16, /* rightshift */
728 1, /* size (0 = byte, 1 = short, 2 = long) */
729 16, /* bitsize */
b34976b6 730 FALSE, /* pc_relative */
5bd4f169 731 0, /* bitpos */
f9c6b907 732 complain_overflow_signed, /* complain_on_overflow */
805fc799 733 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 734 "R_PPC64_PLT16_HI", /* name */
b34976b6 735 FALSE, /* partial_inplace */
5bd4f169
AM
736 0, /* src_mask */
737 0xffff, /* dst_mask */
b34976b6 738 FALSE), /* pcrel_offset */
5bd4f169
AM
739
740 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
741 the symbol. */
742 HOWTO (R_PPC64_PLT16_HA, /* type */
743 16, /* rightshift */
744 1, /* size (0 = byte, 1 = short, 2 = long) */
745 16, /* bitsize */
b34976b6 746 FALSE, /* pc_relative */
5bd4f169 747 0, /* bitpos */
f9c6b907 748 complain_overflow_signed, /* complain_on_overflow */
805fc799 749 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 750 "R_PPC64_PLT16_HA", /* name */
b34976b6 751 FALSE, /* partial_inplace */
5bd4f169
AM
752 0, /* src_mask */
753 0xffff, /* dst_mask */
b34976b6 754 FALSE), /* pcrel_offset */
5bd4f169 755
c061c2d8 756 /* 16-bit section relative relocation. */
5bd4f169
AM
757 HOWTO (R_PPC64_SECTOFF, /* type */
758 0, /* rightshift */
c061c2d8
AM
759 1, /* size (0 = byte, 1 = short, 2 = long) */
760 16, /* bitsize */
b34976b6 761 FALSE, /* pc_relative */
5bd4f169 762 0, /* bitpos */
b80eed39 763 complain_overflow_signed, /* complain_on_overflow */
805fc799 764 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 765 "R_PPC64_SECTOFF", /* name */
b34976b6 766 FALSE, /* partial_inplace */
5bd4f169 767 0, /* src_mask */
c061c2d8 768 0xffff, /* dst_mask */
b34976b6 769 FALSE), /* pcrel_offset */
5bd4f169 770
c061c2d8 771 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
772 HOWTO (R_PPC64_SECTOFF_LO, /* type */
773 0, /* rightshift */
774 1, /* size (0 = byte, 1 = short, 2 = long) */
775 16, /* bitsize */
b34976b6 776 FALSE, /* pc_relative */
5bd4f169
AM
777 0, /* bitpos */
778 complain_overflow_dont, /* complain_on_overflow */
805fc799 779 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 780 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 781 FALSE, /* partial_inplace */
5bd4f169
AM
782 0, /* src_mask */
783 0xffff, /* dst_mask */
b34976b6 784 FALSE), /* pcrel_offset */
5bd4f169
AM
785
786 /* 16-bit upper half section relative relocation. */
787 HOWTO (R_PPC64_SECTOFF_HI, /* type */
788 16, /* rightshift */
789 1, /* size (0 = byte, 1 = short, 2 = long) */
790 16, /* bitsize */
b34976b6 791 FALSE, /* pc_relative */
5bd4f169 792 0, /* bitpos */
f9c6b907 793 complain_overflow_signed, /* complain_on_overflow */
805fc799 794 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 795 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 796 FALSE, /* partial_inplace */
5bd4f169
AM
797 0, /* src_mask */
798 0xffff, /* dst_mask */
b34976b6 799 FALSE), /* pcrel_offset */
5bd4f169
AM
800
801 /* 16-bit upper half adjusted section relative relocation. */
802 HOWTO (R_PPC64_SECTOFF_HA, /* type */
803 16, /* rightshift */
804 1, /* size (0 = byte, 1 = short, 2 = long) */
805 16, /* bitsize */
b34976b6 806 FALSE, /* pc_relative */
5bd4f169 807 0, /* bitpos */
f9c6b907 808 complain_overflow_signed, /* complain_on_overflow */
805fc799 809 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 810 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 811 FALSE, /* partial_inplace */
5bd4f169
AM
812 0, /* src_mask */
813 0xffff, /* dst_mask */
b34976b6 814 FALSE), /* pcrel_offset */
5bd4f169 815
04c9666a
AM
816 /* Like R_PPC64_REL24 without touching the two least significant bits. */
817 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
818 2, /* rightshift */
819 2, /* size (0 = byte, 1 = short, 2 = long) */
820 30, /* bitsize */
b34976b6 821 TRUE, /* pc_relative */
5bd4f169
AM
822 0, /* bitpos */
823 complain_overflow_dont, /* complain_on_overflow */
824 bfd_elf_generic_reloc, /* special_function */
04c9666a 825 "R_PPC64_REL30", /* name */
b34976b6 826 FALSE, /* partial_inplace */
d006db6c 827 0, /* src_mask */
5bd4f169 828 0xfffffffc, /* dst_mask */
b34976b6 829 TRUE), /* pcrel_offset */
5bd4f169
AM
830
831 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
832
833 /* A standard 64-bit relocation. */
834 HOWTO (R_PPC64_ADDR64, /* type */
835 0, /* rightshift */
836 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
837 64, /* bitsize */
b34976b6 838 FALSE, /* pc_relative */
5bd4f169
AM
839 0, /* bitpos */
840 complain_overflow_dont, /* complain_on_overflow */
841 bfd_elf_generic_reloc, /* special_function */
842 "R_PPC64_ADDR64", /* name */
b34976b6 843 FALSE, /* partial_inplace */
5bd4f169 844 0, /* src_mask */
f5e87a1d 845 ONES (64), /* dst_mask */
b34976b6 846 FALSE), /* pcrel_offset */
5bd4f169
AM
847
848 /* The bits 32-47 of an address. */
849 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
850 32, /* rightshift */
851 1, /* size (0 = byte, 1 = short, 2 = long) */
852 16, /* bitsize */
b34976b6 853 FALSE, /* pc_relative */
5bd4f169
AM
854 0, /* bitpos */
855 complain_overflow_dont, /* complain_on_overflow */
856 bfd_elf_generic_reloc, /* special_function */
857 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 858 FALSE, /* partial_inplace */
5bd4f169
AM
859 0, /* src_mask */
860 0xffff, /* dst_mask */
b34976b6 861 FALSE), /* pcrel_offset */
5bd4f169
AM
862
863 /* The bits 32-47 of an address, plus 1 if the contents of the low
864 16 bits, treated as a signed number, is negative. */
865 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
866 32, /* rightshift */
867 1, /* size (0 = byte, 1 = short, 2 = long) */
868 16, /* bitsize */
b34976b6 869 FALSE, /* pc_relative */
5bd4f169
AM
870 0, /* bitpos */
871 complain_overflow_dont, /* complain_on_overflow */
805fc799 872 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 873 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 874 FALSE, /* partial_inplace */
5bd4f169
AM
875 0, /* src_mask */
876 0xffff, /* dst_mask */
b34976b6 877 FALSE), /* pcrel_offset */
5bd4f169
AM
878
879 /* The bits 48-63 of an address. */
880 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
881 48, /* rightshift */
882 1, /* size (0 = byte, 1 = short, 2 = long) */
883 16, /* bitsize */
b34976b6 884 FALSE, /* pc_relative */
5bd4f169
AM
885 0, /* bitpos */
886 complain_overflow_dont, /* complain_on_overflow */
887 bfd_elf_generic_reloc, /* special_function */
888 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 889 FALSE, /* partial_inplace */
5bd4f169
AM
890 0, /* src_mask */
891 0xffff, /* dst_mask */
b34976b6 892 FALSE), /* pcrel_offset */
5bd4f169
AM
893
894 /* The bits 48-63 of an address, plus 1 if the contents of the low
895 16 bits, treated as a signed number, is negative. */
896 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
897 48, /* rightshift */
898 1, /* size (0 = byte, 1 = short, 2 = long) */
899 16, /* bitsize */
b34976b6 900 FALSE, /* pc_relative */
5bd4f169
AM
901 0, /* bitpos */
902 complain_overflow_dont, /* complain_on_overflow */
805fc799 903 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 904 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 905 FALSE, /* partial_inplace */
5bd4f169
AM
906 0, /* src_mask */
907 0xffff, /* dst_mask */
b34976b6 908 FALSE), /* pcrel_offset */
5bd4f169
AM
909
910 /* Like ADDR64, but may be unaligned. */
911 HOWTO (R_PPC64_UADDR64, /* type */
912 0, /* rightshift */
913 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
914 64, /* bitsize */
b34976b6 915 FALSE, /* pc_relative */
5bd4f169
AM
916 0, /* bitpos */
917 complain_overflow_dont, /* complain_on_overflow */
918 bfd_elf_generic_reloc, /* special_function */
919 "R_PPC64_UADDR64", /* name */
b34976b6 920 FALSE, /* partial_inplace */
5bd4f169 921 0, /* src_mask */
f5e87a1d 922 ONES (64), /* dst_mask */
b34976b6 923 FALSE), /* pcrel_offset */
5bd4f169
AM
924
925 /* 64-bit relative relocation. */
926 HOWTO (R_PPC64_REL64, /* type */
927 0, /* rightshift */
928 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
929 64, /* bitsize */
b34976b6 930 TRUE, /* pc_relative */
5bd4f169
AM
931 0, /* bitpos */
932 complain_overflow_dont, /* complain_on_overflow */
933 bfd_elf_generic_reloc, /* special_function */
934 "R_PPC64_REL64", /* name */
b34976b6 935 FALSE, /* partial_inplace */
5bd4f169 936 0, /* src_mask */
f5e87a1d 937 ONES (64), /* dst_mask */
b34976b6 938 TRUE), /* pcrel_offset */
5bd4f169 939
cedb70c5 940 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
941 HOWTO (R_PPC64_PLT64, /* type */
942 0, /* rightshift */
943 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
944 64, /* bitsize */
b34976b6 945 FALSE, /* pc_relative */
5bd4f169
AM
946 0, /* bitpos */
947 complain_overflow_dont, /* complain_on_overflow */
805fc799 948 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 949 "R_PPC64_PLT64", /* name */
b34976b6 950 FALSE, /* partial_inplace */
5bd4f169 951 0, /* src_mask */
f5e87a1d 952 ONES (64), /* dst_mask */
b34976b6 953 FALSE), /* pcrel_offset */
5bd4f169
AM
954
955 /* 64-bit PC relative relocation to the symbol's procedure linkage
956 table. */
957 /* FIXME: R_PPC64_PLTREL64 not supported. */
958 HOWTO (R_PPC64_PLTREL64, /* type */
959 0, /* rightshift */
960 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
961 64, /* bitsize */
b34976b6 962 TRUE, /* pc_relative */
5bd4f169
AM
963 0, /* bitpos */
964 complain_overflow_dont, /* complain_on_overflow */
805fc799 965 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 966 "R_PPC64_PLTREL64", /* name */
b34976b6 967 FALSE, /* partial_inplace */
5bd4f169 968 0, /* src_mask */
f5e87a1d 969 ONES (64), /* dst_mask */
b34976b6 970 TRUE), /* pcrel_offset */
5bd4f169
AM
971
972 /* 16 bit TOC-relative relocation. */
973
974 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
975 HOWTO (R_PPC64_TOC16, /* type */
976 0, /* rightshift */
977 1, /* size (0 = byte, 1 = short, 2 = long) */
978 16, /* bitsize */
b34976b6 979 FALSE, /* pc_relative */
5bd4f169
AM
980 0, /* bitpos */
981 complain_overflow_signed, /* complain_on_overflow */
805fc799 982 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 983 "R_PPC64_TOC16", /* name */
b34976b6 984 FALSE, /* partial_inplace */
5bd4f169
AM
985 0, /* src_mask */
986 0xffff, /* dst_mask */
b34976b6 987 FALSE), /* pcrel_offset */
5bd4f169
AM
988
989 /* 16 bit TOC-relative relocation without overflow. */
990
991 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
992 HOWTO (R_PPC64_TOC16_LO, /* type */
993 0, /* rightshift */
994 1, /* size (0 = byte, 1 = short, 2 = long) */
995 16, /* bitsize */
b34976b6 996 FALSE, /* pc_relative */
5bd4f169
AM
997 0, /* bitpos */
998 complain_overflow_dont, /* complain_on_overflow */
805fc799 999 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1000 "R_PPC64_TOC16_LO", /* name */
b34976b6 1001 FALSE, /* partial_inplace */
5bd4f169
AM
1002 0, /* src_mask */
1003 0xffff, /* dst_mask */
b34976b6 1004 FALSE), /* pcrel_offset */
5bd4f169
AM
1005
1006 /* 16 bit TOC-relative relocation, high 16 bits. */
1007
1008 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1009 HOWTO (R_PPC64_TOC16_HI, /* type */
1010 16, /* rightshift */
1011 1, /* size (0 = byte, 1 = short, 2 = long) */
1012 16, /* bitsize */
b34976b6 1013 FALSE, /* pc_relative */
5bd4f169 1014 0, /* bitpos */
f9c6b907 1015 complain_overflow_signed, /* complain_on_overflow */
805fc799 1016 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1017 "R_PPC64_TOC16_HI", /* name */
b34976b6 1018 FALSE, /* partial_inplace */
5bd4f169
AM
1019 0, /* src_mask */
1020 0xffff, /* dst_mask */
b34976b6 1021 FALSE), /* pcrel_offset */
5bd4f169
AM
1022
1023 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1024 contents of the low 16 bits, treated as a signed number, is
1025 negative. */
1026
1027 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1028 HOWTO (R_PPC64_TOC16_HA, /* type */
1029 16, /* rightshift */
1030 1, /* size (0 = byte, 1 = short, 2 = long) */
1031 16, /* bitsize */
b34976b6 1032 FALSE, /* pc_relative */
5bd4f169 1033 0, /* bitpos */
f9c6b907 1034 complain_overflow_signed, /* complain_on_overflow */
805fc799 1035 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1036 "R_PPC64_TOC16_HA", /* name */
b34976b6 1037 FALSE, /* partial_inplace */
5bd4f169
AM
1038 0, /* src_mask */
1039 0xffff, /* dst_mask */
b34976b6 1040 FALSE), /* pcrel_offset */
5bd4f169
AM
1041
1042 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1043
1044 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1045 HOWTO (R_PPC64_TOC, /* type */
1046 0, /* rightshift */
1047 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1048 64, /* bitsize */
b34976b6 1049 FALSE, /* pc_relative */
5bd4f169 1050 0, /* bitpos */
b80eed39 1051 complain_overflow_dont, /* complain_on_overflow */
805fc799 1052 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1053 "R_PPC64_TOC", /* name */
b34976b6 1054 FALSE, /* partial_inplace */
5bd4f169 1055 0, /* src_mask */
f5e87a1d 1056 ONES (64), /* dst_mask */
b34976b6 1057 FALSE), /* pcrel_offset */
5bd4f169
AM
1058
1059 /* Like R_PPC64_GOT16, but also informs the link editor that the
1060 value to relocate may (!) refer to a PLT entry which the link
1061 editor (a) may replace with the symbol value. If the link editor
1062 is unable to fully resolve the symbol, it may (b) create a PLT
1063 entry and store the address to the new PLT entry in the GOT.
1064 This permits lazy resolution of function symbols at run time.
1065 The link editor may also skip all of this and just (c) emit a
1066 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1067 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1068 HOWTO (R_PPC64_PLTGOT16, /* type */
1069 0, /* rightshift */
1070 1, /* size (0 = byte, 1 = short, 2 = long) */
1071 16, /* bitsize */
b34976b6 1072 FALSE, /* pc_relative */
5bd4f169
AM
1073 0, /* bitpos */
1074 complain_overflow_signed, /* complain_on_overflow */
805fc799 1075 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1076 "R_PPC64_PLTGOT16", /* name */
1077 FALSE, /* partial_inplace */
1078 0, /* src_mask */
1079 0xffff, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1081
1082 /* Like R_PPC64_PLTGOT16, but without overflow. */
1083 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1084 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1085 0, /* rightshift */
1086 1, /* size (0 = byte, 1 = short, 2 = long) */
1087 16, /* bitsize */
1088 FALSE, /* pc_relative */
1089 0, /* bitpos */
1090 complain_overflow_dont, /* complain_on_overflow */
1091 ppc64_elf_unhandled_reloc, /* special_function */
1092 "R_PPC64_PLTGOT16_LO", /* name */
1093 FALSE, /* partial_inplace */
1094 0, /* src_mask */
1095 0xffff, /* dst_mask */
1096 FALSE), /* pcrel_offset */
1097
1098 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1099 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1100 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1101 16, /* rightshift */
1102 1, /* size (0 = byte, 1 = short, 2 = long) */
1103 16, /* bitsize */
1104 FALSE, /* pc_relative */
1105 0, /* bitpos */
f9c6b907 1106 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1107 ppc64_elf_unhandled_reloc, /* special_function */
1108 "R_PPC64_PLTGOT16_HI", /* name */
1109 FALSE, /* partial_inplace */
1110 0, /* src_mask */
1111 0xffff, /* dst_mask */
1112 FALSE), /* pcrel_offset */
1113
1114 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1115 1 if the contents of the low 16 bits, treated as a signed number,
1116 is negative. */
1117 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1118 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1119 16, /* rightshift */
1120 1, /* size (0 = byte, 1 = short, 2 = long) */
1121 16, /* bitsize */
1122 FALSE, /* pc_relative */
1123 0, /* bitpos */
f9c6b907 1124 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1125 ppc64_elf_unhandled_reloc, /* special_function */
1126 "R_PPC64_PLTGOT16_HA", /* name */
1127 FALSE, /* partial_inplace */
1128 0, /* src_mask */
1129 0xffff, /* dst_mask */
1130 FALSE), /* pcrel_offset */
1131
1132 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1133 HOWTO (R_PPC64_ADDR16_DS, /* type */
1134 0, /* rightshift */
1135 1, /* size (0 = byte, 1 = short, 2 = long) */
1136 16, /* bitsize */
1137 FALSE, /* pc_relative */
1138 0, /* bitpos */
b80eed39 1139 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1140 bfd_elf_generic_reloc, /* special_function */
1141 "R_PPC64_ADDR16_DS", /* name */
1142 FALSE, /* partial_inplace */
1143 0, /* src_mask */
1144 0xfffc, /* dst_mask */
1145 FALSE), /* pcrel_offset */
1146
1147 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1148 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1149 0, /* rightshift */
1150 1, /* size (0 = byte, 1 = short, 2 = long) */
1151 16, /* bitsize */
1152 FALSE, /* pc_relative */
1153 0, /* bitpos */
1154 complain_overflow_dont,/* complain_on_overflow */
1155 bfd_elf_generic_reloc, /* special_function */
1156 "R_PPC64_ADDR16_LO_DS",/* name */
1157 FALSE, /* partial_inplace */
1158 0, /* src_mask */
1159 0xfffc, /* dst_mask */
1160 FALSE), /* pcrel_offset */
1161
1162 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1163 HOWTO (R_PPC64_GOT16_DS, /* type */
1164 0, /* rightshift */
1165 1, /* size (0 = byte, 1 = short, 2 = long) */
1166 16, /* bitsize */
1167 FALSE, /* pc_relative */
1168 0, /* bitpos */
1169 complain_overflow_signed, /* complain_on_overflow */
1170 ppc64_elf_unhandled_reloc, /* special_function */
1171 "R_PPC64_GOT16_DS", /* name */
1172 FALSE, /* partial_inplace */
1173 0, /* src_mask */
1174 0xfffc, /* dst_mask */
1175 FALSE), /* pcrel_offset */
1176
1177 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1178 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1179 0, /* rightshift */
1180 1, /* size (0 = byte, 1 = short, 2 = long) */
1181 16, /* bitsize */
1182 FALSE, /* pc_relative */
1183 0, /* bitpos */
1184 complain_overflow_dont, /* complain_on_overflow */
1185 ppc64_elf_unhandled_reloc, /* special_function */
1186 "R_PPC64_GOT16_LO_DS", /* name */
1187 FALSE, /* partial_inplace */
1188 0, /* src_mask */
1189 0xfffc, /* dst_mask */
1190 FALSE), /* pcrel_offset */
1191
1192 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1193 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1194 0, /* rightshift */
1195 1, /* size (0 = byte, 1 = short, 2 = long) */
1196 16, /* bitsize */
1197 FALSE, /* pc_relative */
1198 0, /* bitpos */
1199 complain_overflow_dont, /* complain_on_overflow */
1200 ppc64_elf_unhandled_reloc, /* special_function */
1201 "R_PPC64_PLT16_LO_DS", /* name */
1202 FALSE, /* partial_inplace */
1203 0, /* src_mask */
1204 0xfffc, /* dst_mask */
1205 FALSE), /* pcrel_offset */
1206
1207 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1208 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1209 0, /* rightshift */
1210 1, /* size (0 = byte, 1 = short, 2 = long) */
1211 16, /* bitsize */
1212 FALSE, /* pc_relative */
1213 0, /* bitpos */
b80eed39 1214 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1215 ppc64_elf_sectoff_reloc, /* special_function */
1216 "R_PPC64_SECTOFF_DS", /* name */
1217 FALSE, /* partial_inplace */
1218 0, /* src_mask */
1219 0xfffc, /* dst_mask */
1220 FALSE), /* pcrel_offset */
1221
1222 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1223 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1224 0, /* rightshift */
1225 1, /* size (0 = byte, 1 = short, 2 = long) */
1226 16, /* bitsize */
1227 FALSE, /* pc_relative */
1228 0, /* bitpos */
1229 complain_overflow_dont, /* complain_on_overflow */
1230 ppc64_elf_sectoff_reloc, /* special_function */
1231 "R_PPC64_SECTOFF_LO_DS",/* name */
1232 FALSE, /* partial_inplace */
1233 0, /* src_mask */
1234 0xfffc, /* dst_mask */
1235 FALSE), /* pcrel_offset */
1236
1237 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1238 HOWTO (R_PPC64_TOC16_DS, /* type */
1239 0, /* rightshift */
1240 1, /* size (0 = byte, 1 = short, 2 = long) */
1241 16, /* bitsize */
1242 FALSE, /* pc_relative */
1243 0, /* bitpos */
1244 complain_overflow_signed, /* complain_on_overflow */
1245 ppc64_elf_toc_reloc, /* special_function */
1246 "R_PPC64_TOC16_DS", /* name */
1247 FALSE, /* partial_inplace */
1248 0, /* src_mask */
1249 0xfffc, /* dst_mask */
1250 FALSE), /* pcrel_offset */
1251
1252 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1253 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1254 0, /* rightshift */
1255 1, /* size (0 = byte, 1 = short, 2 = long) */
1256 16, /* bitsize */
1257 FALSE, /* pc_relative */
1258 0, /* bitpos */
1259 complain_overflow_dont, /* complain_on_overflow */
1260 ppc64_elf_toc_reloc, /* special_function */
1261 "R_PPC64_TOC16_LO_DS", /* name */
1262 FALSE, /* partial_inplace */
1263 0, /* src_mask */
1264 0xfffc, /* dst_mask */
1265 FALSE), /* pcrel_offset */
1266
1267 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1268 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1269 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1270 0, /* rightshift */
1271 1, /* size (0 = byte, 1 = short, 2 = long) */
1272 16, /* bitsize */
1273 FALSE, /* pc_relative */
1274 0, /* bitpos */
1275 complain_overflow_signed, /* complain_on_overflow */
1276 ppc64_elf_unhandled_reloc, /* special_function */
1277 "R_PPC64_PLTGOT16_DS", /* name */
1278 FALSE, /* partial_inplace */
1279 0, /* src_mask */
1280 0xfffc, /* dst_mask */
1281 FALSE), /* pcrel_offset */
1282
1283 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1284 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1285 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1286 0, /* rightshift */
1287 1, /* size (0 = byte, 1 = short, 2 = long) */
1288 16, /* bitsize */
1289 FALSE, /* pc_relative */
1290 0, /* bitpos */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 ppc64_elf_unhandled_reloc, /* special_function */
1293 "R_PPC64_PLTGOT16_LO_DS",/* name */
1294 FALSE, /* partial_inplace */
1295 0, /* src_mask */
1296 0xfffc, /* dst_mask */
1297 FALSE), /* pcrel_offset */
1298
727fc41e 1299 /* Marker relocs for TLS. */
411e1bfb
AM
1300 HOWTO (R_PPC64_TLS,
1301 0, /* rightshift */
1302 2, /* size (0 = byte, 1 = short, 2 = long) */
1303 32, /* bitsize */
1304 FALSE, /* pc_relative */
1305 0, /* bitpos */
1306 complain_overflow_dont, /* complain_on_overflow */
1307 bfd_elf_generic_reloc, /* special_function */
1308 "R_PPC64_TLS", /* name */
1309 FALSE, /* partial_inplace */
1310 0, /* src_mask */
1311 0, /* dst_mask */
1312 FALSE), /* pcrel_offset */
1313
727fc41e
AM
1314 HOWTO (R_PPC64_TLSGD,
1315 0, /* rightshift */
1316 2, /* size (0 = byte, 1 = short, 2 = long) */
1317 32, /* bitsize */
1318 FALSE, /* pc_relative */
1319 0, /* bitpos */
1320 complain_overflow_dont, /* complain_on_overflow */
1321 bfd_elf_generic_reloc, /* special_function */
1322 "R_PPC64_TLSGD", /* name */
1323 FALSE, /* partial_inplace */
1324 0, /* src_mask */
1325 0, /* dst_mask */
1326 FALSE), /* pcrel_offset */
1327
1328 HOWTO (R_PPC64_TLSLD,
1329 0, /* rightshift */
1330 2, /* size (0 = byte, 1 = short, 2 = long) */
1331 32, /* bitsize */
1332 FALSE, /* pc_relative */
1333 0, /* bitpos */
1334 complain_overflow_dont, /* complain_on_overflow */
1335 bfd_elf_generic_reloc, /* special_function */
1336 "R_PPC64_TLSLD", /* name */
1337 FALSE, /* partial_inplace */
1338 0, /* src_mask */
1339 0, /* dst_mask */
1340 FALSE), /* pcrel_offset */
1341
3b421ab3
AM
1342 HOWTO (R_PPC64_TOCSAVE,
1343 0, /* rightshift */
1344 2, /* size (0 = byte, 1 = short, 2 = long) */
1345 32, /* bitsize */
1346 FALSE, /* pc_relative */
1347 0, /* bitpos */
1348 complain_overflow_dont, /* complain_on_overflow */
1349 bfd_elf_generic_reloc, /* special_function */
1350 "R_PPC64_TOCSAVE", /* name */
1351 FALSE, /* partial_inplace */
1352 0, /* src_mask */
1353 0, /* dst_mask */
1354 FALSE), /* pcrel_offset */
1355
411e1bfb
AM
1356 /* Computes the load module index of the load module that contains the
1357 definition of its TLS sym. */
1358 HOWTO (R_PPC64_DTPMOD64,
1359 0, /* rightshift */
1360 4, /* size (0 = byte, 1 = short, 2 = long) */
1361 64, /* bitsize */
1362 FALSE, /* pc_relative */
1363 0, /* bitpos */
1364 complain_overflow_dont, /* complain_on_overflow */
1365 ppc64_elf_unhandled_reloc, /* special_function */
1366 "R_PPC64_DTPMOD64", /* name */
1367 FALSE, /* partial_inplace */
1368 0, /* src_mask */
1369 ONES (64), /* dst_mask */
1370 FALSE), /* pcrel_offset */
1371
1372 /* Computes a dtv-relative displacement, the difference between the value
1373 of sym+add and the base address of the thread-local storage block that
1374 contains the definition of sym, minus 0x8000. */
1375 HOWTO (R_PPC64_DTPREL64,
1376 0, /* rightshift */
1377 4, /* size (0 = byte, 1 = short, 2 = long) */
1378 64, /* bitsize */
1379 FALSE, /* pc_relative */
1380 0, /* bitpos */
1381 complain_overflow_dont, /* complain_on_overflow */
1382 ppc64_elf_unhandled_reloc, /* special_function */
1383 "R_PPC64_DTPREL64", /* name */
1384 FALSE, /* partial_inplace */
1385 0, /* src_mask */
1386 ONES (64), /* dst_mask */
1387 FALSE), /* pcrel_offset */
1388
1389 /* A 16 bit dtprel reloc. */
1390 HOWTO (R_PPC64_DTPREL16,
1391 0, /* rightshift */
1392 1, /* size (0 = byte, 1 = short, 2 = long) */
1393 16, /* bitsize */
1394 FALSE, /* pc_relative */
1395 0, /* bitpos */
1396 complain_overflow_signed, /* complain_on_overflow */
1397 ppc64_elf_unhandled_reloc, /* special_function */
1398 "R_PPC64_DTPREL16", /* name */
1399 FALSE, /* partial_inplace */
1400 0, /* src_mask */
1401 0xffff, /* dst_mask */
1402 FALSE), /* pcrel_offset */
1403
1404 /* Like DTPREL16, but no overflow. */
1405 HOWTO (R_PPC64_DTPREL16_LO,
1406 0, /* rightshift */
1407 1, /* size (0 = byte, 1 = short, 2 = long) */
1408 16, /* bitsize */
1409 FALSE, /* pc_relative */
1410 0, /* bitpos */
1411 complain_overflow_dont, /* complain_on_overflow */
1412 ppc64_elf_unhandled_reloc, /* special_function */
1413 "R_PPC64_DTPREL16_LO", /* name */
1414 FALSE, /* partial_inplace */
1415 0, /* src_mask */
1416 0xffff, /* dst_mask */
1417 FALSE), /* pcrel_offset */
1418
1419 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1420 HOWTO (R_PPC64_DTPREL16_HI,
1421 16, /* rightshift */
1422 1, /* size (0 = byte, 1 = short, 2 = long) */
1423 16, /* bitsize */
1424 FALSE, /* pc_relative */
1425 0, /* bitpos */
f9c6b907 1426 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1427 ppc64_elf_unhandled_reloc, /* special_function */
1428 "R_PPC64_DTPREL16_HI", /* name */
1429 FALSE, /* partial_inplace */
1430 0, /* src_mask */
1431 0xffff, /* dst_mask */
1432 FALSE), /* pcrel_offset */
1433
1434 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1435 HOWTO (R_PPC64_DTPREL16_HA,
1436 16, /* rightshift */
1437 1, /* size (0 = byte, 1 = short, 2 = long) */
1438 16, /* bitsize */
1439 FALSE, /* pc_relative */
1440 0, /* bitpos */
f9c6b907 1441 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1442 ppc64_elf_unhandled_reloc, /* special_function */
1443 "R_PPC64_DTPREL16_HA", /* name */
1444 FALSE, /* partial_inplace */
1445 0, /* src_mask */
1446 0xffff, /* dst_mask */
1447 FALSE), /* pcrel_offset */
1448
1449 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1450 HOWTO (R_PPC64_DTPREL16_HIGHER,
1451 32, /* rightshift */
1452 1, /* size (0 = byte, 1 = short, 2 = long) */
1453 16, /* bitsize */
1454 FALSE, /* pc_relative */
1455 0, /* bitpos */
1456 complain_overflow_dont, /* complain_on_overflow */
1457 ppc64_elf_unhandled_reloc, /* special_function */
1458 "R_PPC64_DTPREL16_HIGHER", /* name */
1459 FALSE, /* partial_inplace */
1460 0, /* src_mask */
1461 0xffff, /* dst_mask */
1462 FALSE), /* pcrel_offset */
1463
1464 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1465 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1466 32, /* rightshift */
1467 1, /* size (0 = byte, 1 = short, 2 = long) */
1468 16, /* bitsize */
1469 FALSE, /* pc_relative */
1470 0, /* bitpos */
1471 complain_overflow_dont, /* complain_on_overflow */
1472 ppc64_elf_unhandled_reloc, /* special_function */
1473 "R_PPC64_DTPREL16_HIGHERA", /* name */
1474 FALSE, /* partial_inplace */
1475 0, /* src_mask */
1476 0xffff, /* dst_mask */
1477 FALSE), /* pcrel_offset */
1478
1479 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1480 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1481 48, /* rightshift */
1482 1, /* size (0 = byte, 1 = short, 2 = long) */
1483 16, /* bitsize */
1484 FALSE, /* pc_relative */
1485 0, /* bitpos */
1486 complain_overflow_dont, /* complain_on_overflow */
1487 ppc64_elf_unhandled_reloc, /* special_function */
1488 "R_PPC64_DTPREL16_HIGHEST", /* name */
1489 FALSE, /* partial_inplace */
1490 0, /* src_mask */
1491 0xffff, /* dst_mask */
1492 FALSE), /* pcrel_offset */
1493
1494 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1495 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1496 48, /* rightshift */
1497 1, /* size (0 = byte, 1 = short, 2 = long) */
1498 16, /* bitsize */
1499 FALSE, /* pc_relative */
1500 0, /* bitpos */
1501 complain_overflow_dont, /* complain_on_overflow */
1502 ppc64_elf_unhandled_reloc, /* special_function */
1503 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1504 FALSE, /* partial_inplace */
1505 0, /* src_mask */
1506 0xffff, /* dst_mask */
1507 FALSE), /* pcrel_offset */
1508
1509 /* Like DTPREL16, but for insns with a DS field. */
1510 HOWTO (R_PPC64_DTPREL16_DS,
1511 0, /* rightshift */
1512 1, /* size (0 = byte, 1 = short, 2 = long) */
1513 16, /* bitsize */
1514 FALSE, /* pc_relative */
1515 0, /* bitpos */
1516 complain_overflow_signed, /* complain_on_overflow */
1517 ppc64_elf_unhandled_reloc, /* special_function */
1518 "R_PPC64_DTPREL16_DS", /* name */
1519 FALSE, /* partial_inplace */
1520 0, /* src_mask */
1521 0xfffc, /* dst_mask */
1522 FALSE), /* pcrel_offset */
1523
1524 /* Like DTPREL16_DS, but no overflow. */
1525 HOWTO (R_PPC64_DTPREL16_LO_DS,
1526 0, /* rightshift */
1527 1, /* size (0 = byte, 1 = short, 2 = long) */
1528 16, /* bitsize */
1529 FALSE, /* pc_relative */
1530 0, /* bitpos */
1531 complain_overflow_dont, /* complain_on_overflow */
1532 ppc64_elf_unhandled_reloc, /* special_function */
1533 "R_PPC64_DTPREL16_LO_DS", /* name */
1534 FALSE, /* partial_inplace */
1535 0, /* src_mask */
1536 0xfffc, /* dst_mask */
1537 FALSE), /* pcrel_offset */
1538
1539 /* Computes a tp-relative displacement, the difference between the value of
1540 sym+add and the value of the thread pointer (r13). */
1541 HOWTO (R_PPC64_TPREL64,
1542 0, /* rightshift */
1543 4, /* size (0 = byte, 1 = short, 2 = long) */
1544 64, /* bitsize */
1545 FALSE, /* pc_relative */
1546 0, /* bitpos */
1547 complain_overflow_dont, /* complain_on_overflow */
1548 ppc64_elf_unhandled_reloc, /* special_function */
1549 "R_PPC64_TPREL64", /* name */
1550 FALSE, /* partial_inplace */
1551 0, /* src_mask */
1552 ONES (64), /* dst_mask */
1553 FALSE), /* pcrel_offset */
1554
1555 /* A 16 bit tprel reloc. */
1556 HOWTO (R_PPC64_TPREL16,
1557 0, /* rightshift */
1558 1, /* size (0 = byte, 1 = short, 2 = long) */
1559 16, /* bitsize */
1560 FALSE, /* pc_relative */
1561 0, /* bitpos */
1562 complain_overflow_signed, /* complain_on_overflow */
1563 ppc64_elf_unhandled_reloc, /* special_function */
1564 "R_PPC64_TPREL16", /* name */
1565 FALSE, /* partial_inplace */
1566 0, /* src_mask */
1567 0xffff, /* dst_mask */
1568 FALSE), /* pcrel_offset */
1569
1570 /* Like TPREL16, but no overflow. */
1571 HOWTO (R_PPC64_TPREL16_LO,
1572 0, /* rightshift */
1573 1, /* size (0 = byte, 1 = short, 2 = long) */
1574 16, /* bitsize */
1575 FALSE, /* pc_relative */
1576 0, /* bitpos */
1577 complain_overflow_dont, /* complain_on_overflow */
1578 ppc64_elf_unhandled_reloc, /* special_function */
1579 "R_PPC64_TPREL16_LO", /* name */
1580 FALSE, /* partial_inplace */
1581 0, /* src_mask */
1582 0xffff, /* dst_mask */
1583 FALSE), /* pcrel_offset */
1584
1585 /* Like TPREL16_LO, but next higher group of 16 bits. */
1586 HOWTO (R_PPC64_TPREL16_HI,
1587 16, /* rightshift */
1588 1, /* size (0 = byte, 1 = short, 2 = long) */
1589 16, /* bitsize */
1590 FALSE, /* pc_relative */
1591 0, /* bitpos */
f9c6b907 1592 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1593 ppc64_elf_unhandled_reloc, /* special_function */
1594 "R_PPC64_TPREL16_HI", /* name */
1595 FALSE, /* partial_inplace */
1596 0, /* src_mask */
1597 0xffff, /* dst_mask */
1598 FALSE), /* pcrel_offset */
1599
1600 /* Like TPREL16_HI, but adjust for low 16 bits. */
1601 HOWTO (R_PPC64_TPREL16_HA,
1602 16, /* rightshift */
1603 1, /* size (0 = byte, 1 = short, 2 = long) */
1604 16, /* bitsize */
1605 FALSE, /* pc_relative */
1606 0, /* bitpos */
f9c6b907 1607 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1608 ppc64_elf_unhandled_reloc, /* special_function */
1609 "R_PPC64_TPREL16_HA", /* name */
1610 FALSE, /* partial_inplace */
1611 0, /* src_mask */
1612 0xffff, /* dst_mask */
1613 FALSE), /* pcrel_offset */
1614
1615 /* Like TPREL16_HI, but next higher group of 16 bits. */
1616 HOWTO (R_PPC64_TPREL16_HIGHER,
1617 32, /* rightshift */
1618 1, /* size (0 = byte, 1 = short, 2 = long) */
1619 16, /* bitsize */
1620 FALSE, /* pc_relative */
1621 0, /* bitpos */
1622 complain_overflow_dont, /* complain_on_overflow */
1623 ppc64_elf_unhandled_reloc, /* special_function */
1624 "R_PPC64_TPREL16_HIGHER", /* name */
1625 FALSE, /* partial_inplace */
1626 0, /* src_mask */
1627 0xffff, /* dst_mask */
1628 FALSE), /* pcrel_offset */
1629
1630 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1631 HOWTO (R_PPC64_TPREL16_HIGHERA,
1632 32, /* rightshift */
1633 1, /* size (0 = byte, 1 = short, 2 = long) */
1634 16, /* bitsize */
1635 FALSE, /* pc_relative */
1636 0, /* bitpos */
1637 complain_overflow_dont, /* complain_on_overflow */
1638 ppc64_elf_unhandled_reloc, /* special_function */
1639 "R_PPC64_TPREL16_HIGHERA", /* name */
1640 FALSE, /* partial_inplace */
1641 0, /* src_mask */
1642 0xffff, /* dst_mask */
1643 FALSE), /* pcrel_offset */
1644
1645 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1646 HOWTO (R_PPC64_TPREL16_HIGHEST,
1647 48, /* rightshift */
1648 1, /* size (0 = byte, 1 = short, 2 = long) */
1649 16, /* bitsize */
1650 FALSE, /* pc_relative */
1651 0, /* bitpos */
1652 complain_overflow_dont, /* complain_on_overflow */
1653 ppc64_elf_unhandled_reloc, /* special_function */
1654 "R_PPC64_TPREL16_HIGHEST", /* name */
1655 FALSE, /* partial_inplace */
1656 0, /* src_mask */
1657 0xffff, /* dst_mask */
1658 FALSE), /* pcrel_offset */
1659
1660 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1661 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1662 48, /* rightshift */
1663 1, /* size (0 = byte, 1 = short, 2 = long) */
1664 16, /* bitsize */
1665 FALSE, /* pc_relative */
1666 0, /* bitpos */
1667 complain_overflow_dont, /* complain_on_overflow */
1668 ppc64_elf_unhandled_reloc, /* special_function */
1669 "R_PPC64_TPREL16_HIGHESTA", /* name */
1670 FALSE, /* partial_inplace */
1671 0, /* src_mask */
1672 0xffff, /* dst_mask */
1673 FALSE), /* pcrel_offset */
1674
1675 /* Like TPREL16, but for insns with a DS field. */
1676 HOWTO (R_PPC64_TPREL16_DS,
1677 0, /* rightshift */
1678 1, /* size (0 = byte, 1 = short, 2 = long) */
1679 16, /* bitsize */
1680 FALSE, /* pc_relative */
1681 0, /* bitpos */
1682 complain_overflow_signed, /* complain_on_overflow */
1683 ppc64_elf_unhandled_reloc, /* special_function */
1684 "R_PPC64_TPREL16_DS", /* name */
1685 FALSE, /* partial_inplace */
1686 0, /* src_mask */
1687 0xfffc, /* dst_mask */
1688 FALSE), /* pcrel_offset */
1689
1690 /* Like TPREL16_DS, but no overflow. */
1691 HOWTO (R_PPC64_TPREL16_LO_DS,
1692 0, /* rightshift */
1693 1, /* size (0 = byte, 1 = short, 2 = long) */
1694 16, /* bitsize */
1695 FALSE, /* pc_relative */
1696 0, /* bitpos */
1697 complain_overflow_dont, /* complain_on_overflow */
1698 ppc64_elf_unhandled_reloc, /* special_function */
1699 "R_PPC64_TPREL16_LO_DS", /* name */
1700 FALSE, /* partial_inplace */
1701 0, /* src_mask */
1702 0xfffc, /* dst_mask */
1703 FALSE), /* pcrel_offset */
1704
1705 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1706 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1707 to the first entry relative to the TOC base (r2). */
1708 HOWTO (R_PPC64_GOT_TLSGD16,
1709 0, /* rightshift */
1710 1, /* size (0 = byte, 1 = short, 2 = long) */
1711 16, /* bitsize */
1712 FALSE, /* pc_relative */
1713 0, /* bitpos */
1714 complain_overflow_signed, /* complain_on_overflow */
1715 ppc64_elf_unhandled_reloc, /* special_function */
1716 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1717 FALSE, /* partial_inplace */
5bd4f169
AM
1718 0, /* src_mask */
1719 0xffff, /* dst_mask */
b34976b6 1720 FALSE), /* pcrel_offset */
5bd4f169 1721
411e1bfb
AM
1722 /* Like GOT_TLSGD16, but no overflow. */
1723 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1724 0, /* rightshift */
1725 1, /* size (0 = byte, 1 = short, 2 = long) */
1726 16, /* bitsize */
b34976b6 1727 FALSE, /* pc_relative */
5bd4f169
AM
1728 0, /* bitpos */
1729 complain_overflow_dont, /* complain_on_overflow */
805fc799 1730 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1731 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1732 FALSE, /* partial_inplace */
5bd4f169
AM
1733 0, /* src_mask */
1734 0xffff, /* dst_mask */
b34976b6 1735 FALSE), /* pcrel_offset */
5bd4f169 1736
411e1bfb
AM
1737 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1738 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1739 16, /* rightshift */
1740 1, /* size (0 = byte, 1 = short, 2 = long) */
1741 16, /* bitsize */
b34976b6 1742 FALSE, /* pc_relative */
5bd4f169 1743 0, /* bitpos */
f9c6b907 1744 complain_overflow_signed, /* complain_on_overflow */
805fc799 1745 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1746 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1747 FALSE, /* partial_inplace */
5bd4f169
AM
1748 0, /* src_mask */
1749 0xffff, /* dst_mask */
b34976b6 1750 FALSE), /* pcrel_offset */
5bd4f169 1751
411e1bfb
AM
1752 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1753 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1754 16, /* rightshift */
1755 1, /* size (0 = byte, 1 = short, 2 = long) */
1756 16, /* bitsize */
b34976b6 1757 FALSE, /* pc_relative */
5bd4f169 1758 0, /* bitpos */
f9c6b907 1759 complain_overflow_signed, /* complain_on_overflow */
805fc799 1760 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1761 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1762 FALSE, /* partial_inplace */
5bd4f169
AM
1763 0, /* src_mask */
1764 0xffff, /* dst_mask */
b34976b6 1765 FALSE), /* pcrel_offset */
5bd4f169 1766
411e1bfb
AM
1767 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1768 with values (sym+add)@dtpmod and zero, and computes the offset to the
1769 first entry relative to the TOC base (r2). */
1770 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1771 0, /* rightshift */
1772 1, /* size (0 = byte, 1 = short, 2 = long) */
1773 16, /* bitsize */
b34976b6 1774 FALSE, /* pc_relative */
5bd4f169 1775 0, /* bitpos */
411e1bfb
AM
1776 complain_overflow_signed, /* complain_on_overflow */
1777 ppc64_elf_unhandled_reloc, /* special_function */
1778 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1779 FALSE, /* partial_inplace */
d006db6c 1780 0, /* src_mask */
411e1bfb 1781 0xffff, /* dst_mask */
b34976b6 1782 FALSE), /* pcrel_offset */
5bd4f169 1783
411e1bfb
AM
1784 /* Like GOT_TLSLD16, but no overflow. */
1785 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1786 0, /* rightshift */
1787 1, /* size (0 = byte, 1 = short, 2 = long) */
1788 16, /* bitsize */
b34976b6 1789 FALSE, /* pc_relative */
5bd4f169 1790 0, /* bitpos */
411e1bfb
AM
1791 complain_overflow_dont, /* complain_on_overflow */
1792 ppc64_elf_unhandled_reloc, /* special_function */
1793 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1794 FALSE, /* partial_inplace */
d006db6c 1795 0, /* src_mask */
411e1bfb 1796 0xffff, /* dst_mask */
b34976b6 1797 FALSE), /* pcrel_offset */
5bd4f169 1798
411e1bfb
AM
1799 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1800 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1801 16, /* rightshift */
5bd4f169
AM
1802 1, /* size (0 = byte, 1 = short, 2 = long) */
1803 16, /* bitsize */
b34976b6 1804 FALSE, /* pc_relative */
5bd4f169 1805 0, /* bitpos */
f9c6b907 1806 complain_overflow_signed, /* complain_on_overflow */
805fc799 1807 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1808 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1809 FALSE, /* partial_inplace */
d006db6c 1810 0, /* src_mask */
411e1bfb 1811 0xffff, /* dst_mask */
b34976b6 1812 FALSE), /* pcrel_offset */
5bd4f169 1813
411e1bfb
AM
1814 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1815 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1816 16, /* rightshift */
5bd4f169
AM
1817 1, /* size (0 = byte, 1 = short, 2 = long) */
1818 16, /* bitsize */
b34976b6 1819 FALSE, /* pc_relative */
5bd4f169 1820 0, /* bitpos */
f9c6b907 1821 complain_overflow_signed, /* complain_on_overflow */
805fc799 1822 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1823 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1824 FALSE, /* partial_inplace */
d006db6c 1825 0, /* src_mask */
411e1bfb 1826 0xffff, /* dst_mask */
b34976b6 1827 FALSE), /* pcrel_offset */
5bd4f169 1828
411e1bfb
AM
1829 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1830 the offset to the entry relative to the TOC base (r2). */
1831 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1832 0, /* rightshift */
1833 1, /* size (0 = byte, 1 = short, 2 = long) */
1834 16, /* bitsize */
b34976b6 1835 FALSE, /* pc_relative */
5bd4f169 1836 0, /* bitpos */
411e1bfb 1837 complain_overflow_signed, /* complain_on_overflow */
805fc799 1838 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1839 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1840 FALSE, /* partial_inplace */
d006db6c 1841 0, /* src_mask */
5bd4f169 1842 0xfffc, /* dst_mask */
b34976b6 1843 FALSE), /* pcrel_offset */
5bd4f169 1844
411e1bfb
AM
1845 /* Like GOT_DTPREL16_DS, but no overflow. */
1846 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1847 0, /* rightshift */
c061c2d8
AM
1848 1, /* size (0 = byte, 1 = short, 2 = long) */
1849 16, /* bitsize */
b34976b6 1850 FALSE, /* pc_relative */
5bd4f169 1851 0, /* bitpos */
411e1bfb
AM
1852 complain_overflow_dont, /* complain_on_overflow */
1853 ppc64_elf_unhandled_reloc, /* special_function */
1854 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1855 FALSE, /* partial_inplace */
d006db6c 1856 0, /* src_mask */
c061c2d8 1857 0xfffc, /* dst_mask */
b34976b6 1858 FALSE), /* pcrel_offset */
5bd4f169 1859
411e1bfb
AM
1860 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1861 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1862 16, /* rightshift */
5bd4f169
AM
1863 1, /* size (0 = byte, 1 = short, 2 = long) */
1864 16, /* bitsize */
b34976b6 1865 FALSE, /* pc_relative */
5bd4f169 1866 0, /* bitpos */
f9c6b907 1867 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1868 ppc64_elf_unhandled_reloc, /* special_function */
1869 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1870 FALSE, /* partial_inplace */
d006db6c 1871 0, /* src_mask */
411e1bfb 1872 0xffff, /* dst_mask */
b34976b6 1873 FALSE), /* pcrel_offset */
5bd4f169 1874
411e1bfb
AM
1875 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1876 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1877 16, /* rightshift */
1878 1, /* size (0 = byte, 1 = short, 2 = long) */
1879 16, /* bitsize */
1880 FALSE, /* pc_relative */
1881 0, /* bitpos */
f9c6b907 1882 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1883 ppc64_elf_unhandled_reloc, /* special_function */
1884 "R_PPC64_GOT_DTPREL16_HA", /* name */
1885 FALSE, /* partial_inplace */
1886 0, /* src_mask */
1887 0xffff, /* dst_mask */
1888 FALSE), /* pcrel_offset */
1889
1890 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1891 offset to the entry relative to the TOC base (r2). */
1892 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1893 0, /* rightshift */
1894 1, /* size (0 = byte, 1 = short, 2 = long) */
1895 16, /* bitsize */
b34976b6 1896 FALSE, /* pc_relative */
5bd4f169
AM
1897 0, /* bitpos */
1898 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1899 ppc64_elf_unhandled_reloc, /* special_function */
1900 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1901 FALSE, /* partial_inplace */
d006db6c 1902 0, /* src_mask */
ad8e1ba5 1903 0xfffc, /* dst_mask */
b34976b6 1904 FALSE), /* pcrel_offset */
5bd4f169 1905
411e1bfb
AM
1906 /* Like GOT_TPREL16_DS, but no overflow. */
1907 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1908 0, /* rightshift */
1909 1, /* size (0 = byte, 1 = short, 2 = long) */
1910 16, /* bitsize */
b34976b6 1911 FALSE, /* pc_relative */
5bd4f169
AM
1912 0, /* bitpos */
1913 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1914 ppc64_elf_unhandled_reloc, /* special_function */
1915 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1916 FALSE, /* partial_inplace */
d006db6c 1917 0, /* src_mask */
ad8e1ba5 1918 0xfffc, /* dst_mask */
b34976b6 1919 FALSE), /* pcrel_offset */
5bd4f169 1920
411e1bfb
AM
1921 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1922 HOWTO (R_PPC64_GOT_TPREL16_HI,
1923 16, /* rightshift */
5bd4f169
AM
1924 1, /* size (0 = byte, 1 = short, 2 = long) */
1925 16, /* bitsize */
b34976b6 1926 FALSE, /* pc_relative */
5bd4f169 1927 0, /* bitpos */
f9c6b907 1928 complain_overflow_signed, /* complain_on_overflow */
805fc799 1929 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1930 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1931 FALSE, /* partial_inplace */
d006db6c 1932 0, /* src_mask */
411e1bfb 1933 0xffff, /* dst_mask */
b34976b6 1934 FALSE), /* pcrel_offset */
5bd4f169 1935
411e1bfb
AM
1936 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1937 HOWTO (R_PPC64_GOT_TPREL16_HA,
1938 16, /* rightshift */
5bd4f169
AM
1939 1, /* size (0 = byte, 1 = short, 2 = long) */
1940 16, /* bitsize */
b34976b6 1941 FALSE, /* pc_relative */
5bd4f169 1942 0, /* bitpos */
f9c6b907 1943 complain_overflow_signed, /* complain_on_overflow */
805fc799 1944 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1945 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1946 FALSE, /* partial_inplace */
d006db6c 1947 0, /* src_mask */
411e1bfb 1948 0xffff, /* dst_mask */
b34976b6 1949 FALSE), /* pcrel_offset */
5bd4f169 1950
25f23106
AM
1951 HOWTO (R_PPC64_JMP_IREL, /* type */
1952 0, /* rightshift */
1953 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1954 0, /* bitsize */
1955 FALSE, /* pc_relative */
1956 0, /* bitpos */
1957 complain_overflow_dont, /* complain_on_overflow */
1958 ppc64_elf_unhandled_reloc, /* special_function */
1959 "R_PPC64_JMP_IREL", /* name */
1960 FALSE, /* partial_inplace */
1961 0, /* src_mask */
1962 0, /* dst_mask */
1963 FALSE), /* pcrel_offset */
1964
e054468f
AM
1965 HOWTO (R_PPC64_IRELATIVE, /* type */
1966 0, /* rightshift */
1967 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1968 64, /* bitsize */
1969 FALSE, /* pc_relative */
1970 0, /* bitpos */
1971 complain_overflow_dont, /* complain_on_overflow */
1972 bfd_elf_generic_reloc, /* special_function */
1973 "R_PPC64_IRELATIVE", /* name */
1974 FALSE, /* partial_inplace */
1975 0, /* src_mask */
1976 ONES (64), /* dst_mask */
1977 FALSE), /* pcrel_offset */
1978
25f23106
AM
1979 /* A 16 bit relative relocation. */
1980 HOWTO (R_PPC64_REL16, /* type */
1981 0, /* rightshift */
1982 1, /* size (0 = byte, 1 = short, 2 = long) */
1983 16, /* bitsize */
1984 TRUE, /* pc_relative */
1985 0, /* bitpos */
b80eed39 1986 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1987 bfd_elf_generic_reloc, /* special_function */
1988 "R_PPC64_REL16", /* name */
1989 FALSE, /* partial_inplace */
1990 0, /* src_mask */
1991 0xffff, /* dst_mask */
1992 TRUE), /* pcrel_offset */
1993
1994 /* A 16 bit relative relocation without overflow. */
1995 HOWTO (R_PPC64_REL16_LO, /* type */
1996 0, /* rightshift */
1997 1, /* size (0 = byte, 1 = short, 2 = long) */
1998 16, /* bitsize */
1999 TRUE, /* pc_relative */
2000 0, /* bitpos */
2001 complain_overflow_dont,/* complain_on_overflow */
2002 bfd_elf_generic_reloc, /* special_function */
2003 "R_PPC64_REL16_LO", /* name */
2004 FALSE, /* partial_inplace */
2005 0, /* src_mask */
2006 0xffff, /* dst_mask */
2007 TRUE), /* pcrel_offset */
2008
2009 /* The high order 16 bits of a relative address. */
2010 HOWTO (R_PPC64_REL16_HI, /* type */
2011 16, /* rightshift */
2012 1, /* size (0 = byte, 1 = short, 2 = long) */
2013 16, /* bitsize */
2014 TRUE, /* pc_relative */
2015 0, /* bitpos */
f9c6b907 2016 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2017 bfd_elf_generic_reloc, /* special_function */
2018 "R_PPC64_REL16_HI", /* name */
2019 FALSE, /* partial_inplace */
2020 0, /* src_mask */
2021 0xffff, /* dst_mask */
2022 TRUE), /* pcrel_offset */
2023
2024 /* The high order 16 bits of a relative address, plus 1 if the contents of
2025 the low 16 bits, treated as a signed number, is negative. */
2026 HOWTO (R_PPC64_REL16_HA, /* type */
2027 16, /* rightshift */
2028 1, /* size (0 = byte, 1 = short, 2 = long) */
2029 16, /* bitsize */
2030 TRUE, /* pc_relative */
2031 0, /* bitpos */
f9c6b907 2032 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2033 ppc64_elf_ha_reloc, /* special_function */
2034 "R_PPC64_REL16_HA", /* name */
2035 FALSE, /* partial_inplace */
2036 0, /* src_mask */
2037 0xffff, /* dst_mask */
2038 TRUE), /* pcrel_offset */
2039
a680de9a
PB
2040 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2041 HOWTO (R_PPC64_REL16DX_HA, /* type */
2042 16, /* rightshift */
2043 2, /* size (0 = byte, 1 = short, 2 = long) */
2044 16, /* bitsize */
2045 TRUE, /* pc_relative */
2046 0, /* bitpos */
2047 complain_overflow_signed, /* complain_on_overflow */
2048 ppc64_elf_ha_reloc, /* special_function */
2049 "R_PPC64_REL16DX_HA", /* name */
2050 FALSE, /* partial_inplace */
2051 0, /* src_mask */
2052 0x1fffc1, /* dst_mask */
2053 TRUE), /* pcrel_offset */
2054
7ba71655
AM
2055 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2056 HOWTO (R_PPC64_16DX_HA, /* type */
2057 16, /* rightshift */
2058 2, /* size (0 = byte, 1 = short, 2 = long) */
2059 16, /* bitsize */
2060 FALSE, /* pc_relative */
2061 0, /* bitpos */
2062 complain_overflow_signed, /* complain_on_overflow */
2063 ppc64_elf_ha_reloc, /* special_function */
2064 "R_PPC64_16DX_HA", /* name */
2065 FALSE, /* partial_inplace */
2066 0, /* src_mask */
2067 0x1fffc1, /* dst_mask */
2068 FALSE), /* pcrel_offset */
2069
f9c6b907
AM
2070 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2071 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2072 16, /* rightshift */
2073 1, /* size (0 = byte, 1 = short, 2 = long) */
2074 16, /* bitsize */
2075 FALSE, /* pc_relative */
2076 0, /* bitpos */
2077 complain_overflow_dont, /* complain_on_overflow */
2078 bfd_elf_generic_reloc, /* special_function */
2079 "R_PPC64_ADDR16_HIGH", /* name */
2080 FALSE, /* partial_inplace */
2081 0, /* src_mask */
2082 0xffff, /* dst_mask */
2083 FALSE), /* pcrel_offset */
2084
2085 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2086 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2087 16, /* rightshift */
2088 1, /* size (0 = byte, 1 = short, 2 = long) */
2089 16, /* bitsize */
2090 FALSE, /* pc_relative */
2091 0, /* bitpos */
2092 complain_overflow_dont, /* complain_on_overflow */
2093 ppc64_elf_ha_reloc, /* special_function */
2094 "R_PPC64_ADDR16_HIGHA", /* name */
2095 FALSE, /* partial_inplace */
2096 0, /* src_mask */
2097 0xffff, /* dst_mask */
2098 FALSE), /* pcrel_offset */
2099
2100 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2101 HOWTO (R_PPC64_DTPREL16_HIGH,
2102 16, /* rightshift */
2103 1, /* size (0 = byte, 1 = short, 2 = long) */
2104 16, /* bitsize */
2105 FALSE, /* pc_relative */
2106 0, /* bitpos */
2107 complain_overflow_dont, /* complain_on_overflow */
2108 ppc64_elf_unhandled_reloc, /* special_function */
2109 "R_PPC64_DTPREL16_HIGH", /* name */
2110 FALSE, /* partial_inplace */
2111 0, /* src_mask */
2112 0xffff, /* dst_mask */
2113 FALSE), /* pcrel_offset */
2114
2115 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2116 HOWTO (R_PPC64_DTPREL16_HIGHA,
2117 16, /* rightshift */
2118 1, /* size (0 = byte, 1 = short, 2 = long) */
2119 16, /* bitsize */
2120 FALSE, /* pc_relative */
2121 0, /* bitpos */
2122 complain_overflow_dont, /* complain_on_overflow */
2123 ppc64_elf_unhandled_reloc, /* special_function */
2124 "R_PPC64_DTPREL16_HIGHA", /* name */
2125 FALSE, /* partial_inplace */
2126 0, /* src_mask */
2127 0xffff, /* dst_mask */
2128 FALSE), /* pcrel_offset */
2129
2130 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2131 HOWTO (R_PPC64_TPREL16_HIGH,
2132 16, /* rightshift */
2133 1, /* size (0 = byte, 1 = short, 2 = long) */
2134 16, /* bitsize */
2135 FALSE, /* pc_relative */
2136 0, /* bitpos */
2137 complain_overflow_dont, /* complain_on_overflow */
2138 ppc64_elf_unhandled_reloc, /* special_function */
2139 "R_PPC64_TPREL16_HIGH", /* name */
2140 FALSE, /* partial_inplace */
2141 0, /* src_mask */
2142 0xffff, /* dst_mask */
2143 FALSE), /* pcrel_offset */
2144
2145 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2146 HOWTO (R_PPC64_TPREL16_HIGHA,
2147 16, /* rightshift */
2148 1, /* size (0 = byte, 1 = short, 2 = long) */
2149 16, /* bitsize */
2150 FALSE, /* pc_relative */
2151 0, /* bitpos */
2152 complain_overflow_dont, /* complain_on_overflow */
2153 ppc64_elf_unhandled_reloc, /* special_function */
2154 "R_PPC64_TPREL16_HIGHA", /* name */
2155 FALSE, /* partial_inplace */
2156 0, /* src_mask */
2157 0xffff, /* dst_mask */
2158 FALSE), /* pcrel_offset */
2159
006589cf
AM
2160 /* Marker reloc on ELFv2 large-model function entry. */
2161 HOWTO (R_PPC64_ENTRY,
2162 0, /* rightshift */
2163 2, /* size (0 = byte, 1 = short, 2 = long) */
2164 32, /* bitsize */
2165 FALSE, /* pc_relative */
2166 0, /* bitpos */
2167 complain_overflow_dont, /* complain_on_overflow */
2168 bfd_elf_generic_reloc, /* special_function */
2169 "R_PPC64_ENTRY", /* name */
2170 FALSE, /* partial_inplace */
2171 0, /* src_mask */
2172 0, /* dst_mask */
2173 FALSE), /* pcrel_offset */
2174
45965137
AM
2175 /* Like ADDR64, but use local entry point of function. */
2176 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2177 0, /* rightshift */
2178 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2179 64, /* bitsize */
2180 FALSE, /* pc_relative */
2181 0, /* bitpos */
2182 complain_overflow_dont, /* complain_on_overflow */
2183 bfd_elf_generic_reloc, /* special_function */
2184 "R_PPC64_ADDR64_LOCAL", /* name */
2185 FALSE, /* partial_inplace */
2186 0, /* src_mask */
2187 ONES (64), /* dst_mask */
2188 FALSE), /* pcrel_offset */
2189
5bd4f169
AM
2190 /* GNU extension to record C++ vtable hierarchy. */
2191 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2192 0, /* rightshift */
2193 0, /* size (0 = byte, 1 = short, 2 = long) */
2194 0, /* bitsize */
b34976b6 2195 FALSE, /* pc_relative */
5bd4f169
AM
2196 0, /* bitpos */
2197 complain_overflow_dont, /* complain_on_overflow */
2198 NULL, /* special_function */
2199 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2200 FALSE, /* partial_inplace */
5bd4f169
AM
2201 0, /* src_mask */
2202 0, /* dst_mask */
b34976b6 2203 FALSE), /* pcrel_offset */
5bd4f169
AM
2204
2205 /* GNU extension to record C++ vtable member usage. */
2206 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2207 0, /* rightshift */
2208 0, /* size (0 = byte, 1 = short, 2 = long) */
2209 0, /* bitsize */
b34976b6 2210 FALSE, /* pc_relative */
5bd4f169
AM
2211 0, /* bitpos */
2212 complain_overflow_dont, /* complain_on_overflow */
2213 NULL, /* special_function */
2214 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2215 FALSE, /* partial_inplace */
5bd4f169
AM
2216 0, /* src_mask */
2217 0, /* dst_mask */
b34976b6 2218 FALSE), /* pcrel_offset */
5bd4f169
AM
2219};
2220
2221\f
2222/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2223 be done. */
2224
2225static void
4ce794b7 2226ppc_howto_init (void)
5bd4f169
AM
2227{
2228 unsigned int i, type;
2229
a4b6fadd 2230 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2231 {
2232 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2233 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2234 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2235 }
2236}
2237
2238static reloc_howto_type *
4ce794b7
AM
2239ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2240 bfd_reloc_code_real_type code)
5bd4f169 2241{
411e1bfb 2242 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2243
2244 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2245 /* Initialize howto table if needed. */
2246 ppc_howto_init ();
2247
4ce794b7 2248 switch (code)
5bd4f169
AM
2249 {
2250 default:
4ce794b7 2251 return NULL;
5bd4f169 2252
411e1bfb
AM
2253 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2254 break;
2255 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2256 break;
2257 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2258 break;
2259 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2260 break;
2261 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2262 break;
2263 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2264 break;
f9c6b907
AM
2265 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2266 break;
411e1bfb 2267 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2268 break;
f9c6b907
AM
2269 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2270 break;
411e1bfb 2271 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2272 break;
411e1bfb 2273 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2274 break;
411e1bfb 2275 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2276 break;
411e1bfb 2277 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2278 break;
411e1bfb 2279 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2280 break;
411e1bfb 2281 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2282 break;
411e1bfb 2283 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2284 break;
411e1bfb 2285 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2286 break;
411e1bfb 2287 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2288 break;
411e1bfb 2289 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2290 break;
411e1bfb 2291 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2292 break;
411e1bfb 2293 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2294 break;
411e1bfb 2295 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2296 break;
411e1bfb 2297 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2298 break;
411e1bfb 2299 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2300 break;
411e1bfb 2301 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2302 break;
411e1bfb 2303 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2304 break;
411e1bfb 2305 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2306 break;
411e1bfb 2307 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2308 break;
411e1bfb 2309 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2310 break;
411e1bfb 2311 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2312 break;
411e1bfb 2313 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2314 break;
411e1bfb 2315 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2316 break;
411e1bfb 2317 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2318 break;
411e1bfb 2319 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2320 break;
411e1bfb 2321 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2322 break;
411e1bfb 2323 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2324 break;
411e1bfb 2325 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2326 break;
411e1bfb 2327 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2328 break;
411e1bfb 2329 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2330 break;
411e1bfb 2331 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2332 break;
411e1bfb 2333 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2334 break;
411e1bfb 2335 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2336 break;
411e1bfb 2337 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2338 break;
411e1bfb 2339 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2340 break;
411e1bfb 2341 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2342 break;
411e1bfb 2343 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2344 break;
411e1bfb 2345 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2346 break;
411e1bfb 2347 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2348 break;
411e1bfb 2349 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2350 break;
411e1bfb 2351 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2352 break;
411e1bfb 2353 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2354 break;
411e1bfb 2355 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2356 break;
411e1bfb 2357 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2358 break;
411e1bfb 2359 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2360 break;
411e1bfb 2361 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2362 break;
411e1bfb 2363 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2364 break;
411e1bfb 2365 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2366 break;
411e1bfb 2367 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2368 break;
411e1bfb 2369 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2370 break;
411e1bfb 2371 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2372 break;
411e1bfb 2373 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2374 break;
411e1bfb 2375 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2376 break;
727fc41e
AM
2377 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2378 break;
2379 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2380 break;
411e1bfb 2381 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2382 break;
411e1bfb 2383 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2384 break;
411e1bfb 2385 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2386 break;
411e1bfb 2387 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2388 break;
f9c6b907
AM
2389 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2390 break;
411e1bfb 2391 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2392 break;
f9c6b907
AM
2393 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2394 break;
411e1bfb 2395 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2396 break;
411e1bfb
AM
2397 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2398 break;
2399 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2400 break;
2401 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2402 break;
f9c6b907
AM
2403 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2404 break;
411e1bfb
AM
2405 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2406 break;
f9c6b907
AM
2407 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2408 break;
411e1bfb
AM
2409 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2410 break;
2411 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2412 break;
2413 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2414 break;
2415 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2416 break;
2417 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2418 break;
2419 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2420 break;
2421 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2422 break;
2423 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2424 break;
2425 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2426 break;
2427 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2428 break;
2429 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2430 break;
2431 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2432 break;
2433 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2434 break;
2435 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2436 break;
2437 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2438 break;
2439 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2440 break;
2441 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2442 break;
2443 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2444 break;
2445 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2446 break;
2447 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2448 break;
2449 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2450 break;
2451 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2452 break;
2453 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2454 break;
2455 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2456 break;
2457 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2458 break;
2459 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2460 break;
2461 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2462 break;
2463 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2464 break;
2465 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2466 break;
25f23106
AM
2467 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2468 break;
2469 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2470 break;
2471 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2472 break;
2473 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2474 break;
7ba71655
AM
2475 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2476 break;
a680de9a
PB
2477 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2478 break;
006589cf
AM
2479 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2480 break;
45965137
AM
2481 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2482 break;
411e1bfb
AM
2483 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2484 break;
2485 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2486 break;
2487 }
2488
4ce794b7 2489 return ppc64_elf_howto_table[r];
5bd4f169
AM
2490};
2491
157090f7
AM
2492static reloc_howto_type *
2493ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2494 const char *r_name)
2495{
2496 unsigned int i;
2497
a4b6fadd 2498 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2499 if (ppc64_elf_howto_raw[i].name != NULL
2500 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2501 return &ppc64_elf_howto_raw[i];
2502
2503 return NULL;
2504}
2505
5bd4f169
AM
2506/* Set the howto pointer for a PowerPC ELF reloc. */
2507
2508static void
4aef7643 2509ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 2510 Elf_Internal_Rela *dst)
5bd4f169 2511{
65f38f15
AM
2512 unsigned int type;
2513
ef60b7ff 2514 /* Initialize howto table if needed. */
5bd4f169 2515 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2516 ppc_howto_init ();
2517
65f38f15 2518 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2519 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2520 {
695344c0 2521 /* xgettext:c-format */
4eca0228
AM
2522 _bfd_error_handler (_("%B: invalid relocation type %d"),
2523 abfd, (int) type);
3ec2b351 2524 type = R_PPC64_NONE;
d0fb9a8d 2525 }
65f38f15 2526 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2527}
2528
04c9666a 2529/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2530
2531static bfd_reloc_status_type
4ce794b7
AM
2532ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2533 void *data, asection *input_section,
2534 bfd *output_bfd, char **error_message)
5bd4f169 2535{
a680de9a
PB
2536 enum elf_ppc64_reloc_type r_type;
2537 long insn;
2538 bfd_size_type octets;
3de43e7b 2539 bfd_vma value;
a680de9a 2540
805fc799
AM
2541 /* If this is a relocatable link (output_bfd test tells us), just
2542 call the generic function. Any adjustment will be done at final
2543 link time. */
2544 if (output_bfd != NULL)
cedb70c5 2545 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2546 input_section, output_bfd, error_message);
2547
2548 /* Adjust the addend for sign extension of the low 16 bits.
2549 We won't actually be using the low 16 bits, so trashing them
2550 doesn't matter. */
2551 reloc_entry->addend += 0x8000;
a680de9a
PB
2552 r_type = reloc_entry->howto->type;
2553 if (r_type != R_PPC64_REL16DX_HA)
2554 return bfd_reloc_continue;
2555
2556 value = 0;
2557 if (!bfd_is_com_section (symbol->section))
2558 value = symbol->value;
2559 value += (reloc_entry->addend
2560 + symbol->section->output_offset
2561 + symbol->section->output_section->vma);
2562 value -= (reloc_entry->address
2563 + input_section->output_offset
2564 + input_section->output_section->vma);
3de43e7b 2565 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
2566
2567 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2568 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2569 insn &= ~0x1fffc1;
3de43e7b 2570 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 2571 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 2572 if (value + 0x8000 > 0xffff)
a680de9a
PB
2573 return bfd_reloc_overflow;
2574 return bfd_reloc_ok;
805fc799 2575}
5bd4f169 2576
2441e016
AM
2577static bfd_reloc_status_type
2578ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2579 void *data, asection *input_section,
2580 bfd *output_bfd, char **error_message)
2581{
2582 if (output_bfd != NULL)
2583 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2584 input_section, output_bfd, error_message);
2585
699733f6
AM
2586 if (strcmp (symbol->section->name, ".opd") == 0
2587 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2588 {
2589 bfd_vma dest = opd_entry_value (symbol->section,
2590 symbol->value + reloc_entry->addend,
aef36ac1 2591 NULL, NULL, FALSE);
2441e016
AM
2592 if (dest != (bfd_vma) -1)
2593 reloc_entry->addend = dest - (symbol->value
2594 + symbol->section->output_section->vma
2595 + symbol->section->output_offset);
2596 }
810d4e75
AM
2597 else
2598 {
2599 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2600
2601 if (symbol->section->owner != abfd
9f284bf9 2602 && symbol->section->owner != NULL
810d4e75
AM
2603 && abiversion (symbol->section->owner) >= 2)
2604 {
2605 unsigned int i;
2606
2607 for (i = 0; i < symbol->section->owner->symcount; ++i)
2608 {
2609 asymbol *symdef = symbol->section->owner->outsymbols[i];
2610
2611 if (strcmp (symdef->name, symbol->name) == 0)
2612 {
2613 elfsym = (elf_symbol_type *) symdef;
2614 break;
2615 }
2616 }
2617 }
2618 reloc_entry->addend
2619 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2620 }
2441e016
AM
2621 return bfd_reloc_continue;
2622}
2623
805fc799 2624static bfd_reloc_status_type
4ce794b7
AM
2625ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2626 void *data, asection *input_section,
2627 bfd *output_bfd, char **error_message)
805fc799
AM
2628{
2629 long insn;
04c9666a 2630 enum elf_ppc64_reloc_type r_type;
805fc799 2631 bfd_size_type octets;
794e51c0
AM
2632 /* Assume 'at' branch hints. */
2633 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2634
2635 /* If this is a relocatable link (output_bfd test tells us), just
2636 call the generic function. Any adjustment will be done at final
2637 link time. */
5bd4f169 2638 if (output_bfd != NULL)
cedb70c5 2639 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2640 input_section, output_bfd, error_message);
2641
2642 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2643 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2644 insn &= ~(0x01 << 21);
4ce794b7 2645 r_type = reloc_entry->howto->type;
805fc799
AM
2646 if (r_type == R_PPC64_ADDR14_BRTAKEN
2647 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2648 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2649
794e51c0 2650 if (is_isa_v2)
5bd4f169 2651 {
805fc799
AM
2652 /* Set 'a' bit. This is 0b00010 in BO field for branch
2653 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2654 for branch on CTR insns (BO == 1a00t or 1a01t). */
2655 if ((insn & (0x14 << 21)) == (0x04 << 21))
2656 insn |= 0x02 << 21;
2657 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2658 insn |= 0x08 << 21;
2659 else
2441e016 2660 goto out;
5bd4f169 2661 }
805fc799
AM
2662 else
2663 {
2664 bfd_vma target = 0;
2665 bfd_vma from;
5bd4f169 2666
805fc799
AM
2667 if (!bfd_is_com_section (symbol->section))
2668 target = symbol->value;
2669 target += symbol->section->output_section->vma;
2670 target += symbol->section->output_offset;
2671 target += reloc_entry->addend;
5bd4f169 2672
805fc799
AM
2673 from = (reloc_entry->address
2674 + input_section->output_offset
2675 + input_section->output_section->vma);
5bd4f169 2676
805fc799
AM
2677 /* Invert 'y' bit if not the default. */
2678 if ((bfd_signed_vma) (target - from) < 0)
2679 insn ^= 0x01 << 21;
2680 }
4ce794b7 2681 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2682 out:
2683 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2684 input_section, output_bfd, error_message);
805fc799 2685}
5bd4f169 2686
805fc799 2687static bfd_reloc_status_type
4ce794b7
AM
2688ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2689 void *data, asection *input_section,
2690 bfd *output_bfd, char **error_message)
805fc799
AM
2691{
2692 /* If this is a relocatable link (output_bfd test tells us), just
2693 call the generic function. Any adjustment will be done at final
2694 link time. */
2695 if (output_bfd != NULL)
cedb70c5 2696 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2697 input_section, output_bfd, error_message);
5bd4f169 2698
805fc799
AM
2699 /* Subtract the symbol section base address. */
2700 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2701 return bfd_reloc_continue;
2702}
2703
805fc799 2704static bfd_reloc_status_type
4ce794b7
AM
2705ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2706 void *data, asection *input_section,
2707 bfd *output_bfd, char **error_message)
805fc799
AM
2708{
2709 /* If this is a relocatable link (output_bfd test tells us), just
2710 call the generic function. Any adjustment will be done at final
2711 link time. */
2712 if (output_bfd != NULL)
cedb70c5 2713 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2714 input_section, output_bfd, error_message);
2715
2716 /* Subtract the symbol section base address. */
2717 reloc_entry->addend -= symbol->section->output_section->vma;
2718
2719 /* Adjust the addend for sign extension of the low 16 bits. */
2720 reloc_entry->addend += 0x8000;
2721 return bfd_reloc_continue;
2722}
2723
2724static bfd_reloc_status_type
4ce794b7
AM
2725ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2726 void *data, asection *input_section,
2727 bfd *output_bfd, char **error_message)
805fc799
AM
2728{
2729 bfd_vma TOCstart;
2730
2731 /* If this is a relocatable link (output_bfd test tells us), just
2732 call the generic function. Any adjustment will be done at final
2733 link time. */
2734 if (output_bfd != NULL)
cedb70c5 2735 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2736 input_section, output_bfd, error_message);
2737
2738 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2739 if (TOCstart == 0)
1c865ab2 2740 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2741
2742 /* Subtract the TOC base address. */
2743 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2744 return bfd_reloc_continue;
2745}
2746
2747static bfd_reloc_status_type
4ce794b7
AM
2748ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2749 void *data, asection *input_section,
2750 bfd *output_bfd, char **error_message)
805fc799
AM
2751{
2752 bfd_vma TOCstart;
2753
2754 /* If this is a relocatable link (output_bfd test tells us), just
2755 call the generic function. Any adjustment will be done at final
2756 link time. */
2757 if (output_bfd != NULL)
cedb70c5 2758 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2759 input_section, output_bfd, error_message);
2760
2761 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2762 if (TOCstart == 0)
1c865ab2 2763 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2764
2765 /* Subtract the TOC base address. */
2766 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2767
2768 /* Adjust the addend for sign extension of the low 16 bits. */
2769 reloc_entry->addend += 0x8000;
2770 return bfd_reloc_continue;
2771}
2772
2773static bfd_reloc_status_type
4ce794b7
AM
2774ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2775 void *data, asection *input_section,
2776 bfd *output_bfd, char **error_message)
805fc799
AM
2777{
2778 bfd_vma TOCstart;
2779 bfd_size_type octets;
2780
2781 /* If this is a relocatable link (output_bfd test tells us), just
2782 call the generic function. Any adjustment will be done at final
2783 link time. */
2784 if (output_bfd != NULL)
cedb70c5 2785 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2786 input_section, output_bfd, error_message);
2787
2788 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2789 if (TOCstart == 0)
1c865ab2 2790 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2791
2792 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2793 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2794 return bfd_reloc_ok;
2795}
2796
2797static bfd_reloc_status_type
4ce794b7
AM
2798ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2799 void *data, asection *input_section,
2800 bfd *output_bfd, char **error_message)
805fc799
AM
2801{
2802 /* If this is a relocatable link (output_bfd test tells us), just
2803 call the generic function. Any adjustment will be done at final
2804 link time. */
2805 if (output_bfd != NULL)
cedb70c5 2806 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2807 input_section, output_bfd, error_message);
2808
2809 if (error_message != NULL)
2810 {
2811 static char buf[60];
2812 sprintf (buf, "generic linker can't handle %s",
2813 reloc_entry->howto->name);
2814 *error_message = buf;
2815 }
2816 return bfd_reloc_dangerous;
2817}
2818
927be08e
AM
2819/* Track GOT entries needed for a given symbol. We might need more
2820 than one got entry per symbol. */
2821struct got_entry
2822{
2823 struct got_entry *next;
2824
2825 /* The symbol addend that we'll be placing in the GOT. */
2826 bfd_vma addend;
2827
2828 /* Unlike other ELF targets, we use separate GOT entries for the same
2829 symbol referenced from different input files. This is to support
2830 automatic multiple TOC/GOT sections, where the TOC base can vary
2831 from one input file to another. After partitioning into TOC groups
2832 we merge entries within the group.
2833
2834 Point to the BFD owning this GOT entry. */
2835 bfd *owner;
2836
2837 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2838 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2839 unsigned char tls_type;
927be08e
AM
2840
2841 /* Non-zero if got.ent points to real entry. */
f961d9dd 2842 unsigned char is_indirect;
927be08e
AM
2843
2844 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2845 union
2846 {
2847 bfd_signed_vma refcount;
2848 bfd_vma offset;
2849 struct got_entry *ent;
2850 } got;
2851};
2852
2853/* The same for PLT. */
2854struct plt_entry
2855{
2856 struct plt_entry *next;
2857
2858 bfd_vma addend;
2859
2860 union
2861 {
2862 bfd_signed_vma refcount;
2863 bfd_vma offset;
2864 } plt;
2865};
2866
e717da7e
AM
2867struct ppc64_elf_obj_tdata
2868{
2869 struct elf_obj_tdata elf;
2870
2871 /* Shortcuts to dynamic linker sections. */
2872 asection *got;
2873 asection *relgot;
2874
b3fac117
AM
2875 /* Used during garbage collection. We attach global symbols defined
2876 on removed .opd entries to this section so that the sym is removed. */
2877 asection *deleted_section;
81688140 2878
927be08e 2879 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2880 sections means we potentially need one of these for each input bfd. */
927be08e 2881 struct got_entry tlsld_got;
8860955f 2882
729eabd5
AM
2883 union {
2884 /* A copy of relocs before they are modified for --emit-relocs. */
2885 Elf_Internal_Rela *relocs;
2886
2887 /* Section contents. */
2888 bfd_byte *contents;
2889 } opd;
d77c8a4b
AM
2890
2891 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2892 the reloc to be in the range -32768 to 32767. */
98528052
AM
2893 unsigned int has_small_toc_reloc : 1;
2894
560c8763
AM
2895 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2896 instruction not one we handle. */
2897 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2898};
2899
2900#define ppc64_elf_tdata(bfd) \
2901 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2902
2903#define ppc64_tlsld_got(bfd) \
2904 (&ppc64_elf_tdata (bfd)->tlsld_got)
2905
0c8d6e5c
AM
2906#define is_ppc64_elf(bfd) \
2907 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2908 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2909
e717da7e
AM
2910/* Override the generic function because we store some extras. */
2911
2912static bfd_boolean
2913ppc64_elf_mkobject (bfd *abfd)
2914{
0ffa91dd 2915 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2916 PPC64_ELF_DATA);
e717da7e
AM
2917}
2918
feee612b 2919/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2920 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2921
b34976b6 2922static bfd_boolean
4ce794b7 2923ppc64_elf_object_p (bfd *abfd)
feee612b 2924{
14b57c7c
AM
2925 if (!abfd->arch_info->the_default)
2926 return TRUE;
2927
2928 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2929 {
2930 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2931
2932 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2933 {
2934 /* Relies on arch after 32 bit default being 64 bit default. */
2935 abfd->arch_info = abfd->arch_info->next;
2936 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2937 }
2938 }
14b57c7c 2939 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2940}
2941
d37c89e5
AM
2942/* Support for core dump NOTE sections. */
2943
2944static bfd_boolean
2945ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2946{
eea6121a 2947 size_t offset, size;
d37c89e5
AM
2948
2949 if (note->descsz != 504)
2950 return FALSE;
2951
2952 /* pr_cursig */
228e534f 2953 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2954
2955 /* pr_pid */
228e534f 2956 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2957
2958 /* pr_reg */
2959 offset = 112;
eea6121a 2960 size = 384;
d37c89e5
AM
2961
2962 /* Make a ".reg/999" section. */
2963 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2964 size, note->descpos + offset);
d37c89e5
AM
2965}
2966
2967static bfd_boolean
2968ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2969{
2970 if (note->descsz != 136)
2971 return FALSE;
2972
228e534f 2973 elf_tdata (abfd)->core->pid
bc989cdc 2974 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2975 elf_tdata (abfd)->core->program
d37c89e5 2976 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2977 elf_tdata (abfd)->core->command
d37c89e5
AM
2978 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2979
2980 return TRUE;
2981}
2982
183e98be
AM
2983static char *
2984ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2985 ...)
2986{
2987 switch (note_type)
2988 {
2989 default:
2990 return NULL;
2991
2992 case NT_PRPSINFO:
2993 {
2994 char data[136];
2995 va_list ap;
2996
2997 va_start (ap, note_type);
75cd47ed 2998 memset (data, 0, sizeof (data));
183e98be
AM
2999 strncpy (data + 40, va_arg (ap, const char *), 16);
3000 strncpy (data + 56, va_arg (ap, const char *), 80);
3001 va_end (ap);
3002 return elfcore_write_note (abfd, buf, bufsiz,
3003 "CORE", note_type, data, sizeof (data));
3004 }
3005
3006 case NT_PRSTATUS:
3007 {
3008 char data[504];
3009 va_list ap;
3010 long pid;
3011 int cursig;
3012 const void *greg;
3013
3014 va_start (ap, note_type);
3015 memset (data, 0, 112);
3016 pid = va_arg (ap, long);
3017 bfd_put_32 (abfd, pid, data + 32);
3018 cursig = va_arg (ap, int);
3019 bfd_put_16 (abfd, cursig, data + 12);
3020 greg = va_arg (ap, const void *);
3021 memcpy (data + 112, greg, 384);
3022 memset (data + 496, 0, 8);
3023 va_end (ap);
3024 return elfcore_write_note (abfd, buf, bufsiz,
3025 "CORE", note_type, data, sizeof (data));
3026 }
3027 }
3028}
3029
5d35169e
AM
3030/* Add extra PPC sections. */
3031
b35d266b 3032static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3033{
07d6d2b8
AM
3034 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3035 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3036 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
3037 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3038 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3039 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 3040 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3041};
3042
7c8fe5c4
AM
3043enum _ppc64_sec_type {
3044 sec_normal = 0,
3045 sec_opd = 1,
3046 sec_toc = 2
3047};
3048
f0abc2a1
AM
3049struct _ppc64_elf_section_data
3050{
3051 struct bfd_elf_section_data elf;
411e1bfb 3052
f0abc2a1
AM
3053 union
3054 {
51aecdc5
AM
3055 /* An array with one entry for each opd function descriptor,
3056 and some spares since opd entries may be either 16 or 24 bytes. */
3057#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3058 struct _opd_sec_data
3059 {
3060 /* Points to the function code section for local opd entries. */
3061 asection **func_sec;
3062
3063 /* After editing .opd, adjust references to opd local syms. */
3064 long *adjust;
3065 } opd;
7c8fe5c4 3066
3a71aa26
AM
3067 /* An array for toc sections, indexed by offset/8. */
3068 struct _toc_sec_data
3069 {
3070 /* Specifies the relocation symbol index used at a given toc offset. */
3071 unsigned *symndx;
3072
3073 /* And the relocation addend. */
3074 bfd_vma *add;
3075 } toc;
7c8fe5c4
AM
3076 } u;
3077
3078 enum _ppc64_sec_type sec_type:2;
411e1bfb 3079
7c8fe5c4
AM
3080 /* Flag set when small branches are detected. Used to
3081 select suitable defaults for the stub group size. */
3082 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3083};
3084
3085#define ppc64_elf_section_data(sec) \
411e1bfb 3086 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3087
3088static bfd_boolean
4ce794b7 3089ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3090{
f592407e
AM
3091 if (!sec->used_by_bfd)
3092 {
3093 struct _ppc64_elf_section_data *sdata;
3094 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3095
f592407e
AM
3096 sdata = bfd_zalloc (abfd, amt);
3097 if (sdata == NULL)
3098 return FALSE;
3099 sec->used_by_bfd = sdata;
3100 }
f0abc2a1
AM
3101
3102 return _bfd_elf_new_section_hook (abfd, sec);
3103}
4025353c 3104
74f0fb50 3105static struct _opd_sec_data *
4025353c
AM
3106get_opd_info (asection * sec)
3107{
3108 if (sec != NULL
3109 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3110 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3111 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3112 return NULL;
3113}
90e3cdf2
JJ
3114\f
3115/* Parameters for the qsort hook. */
90e3cdf2 3116static bfd_boolean synthetic_relocatable;
cd285db5 3117static asection *synthetic_opd;
90e3cdf2 3118
699733f6 3119/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3120
3121static int
3122compare_symbols (const void *ap, const void *bp)
3123{
3124 const asymbol *a = * (const asymbol **) ap;
3125 const asymbol *b = * (const asymbol **) bp;
3126
699733f6
AM
3127 /* Section symbols first. */
3128 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3129 return -1;
699733f6 3130 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3131 return 1;
3132
699733f6 3133 /* then .opd symbols. */
cd285db5
AM
3134 if (synthetic_opd != NULL)
3135 {
3136 if (strcmp (a->section->name, ".opd") == 0
3137 && strcmp (b->section->name, ".opd") != 0)
3138 return -1;
3139 if (strcmp (a->section->name, ".opd") != 0
3140 && strcmp (b->section->name, ".opd") == 0)
3141 return 1;
3142 }
90e3cdf2 3143
699733f6 3144 /* then other code symbols. */
90e3cdf2
JJ
3145 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3146 == (SEC_CODE | SEC_ALLOC)
3147 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3148 != (SEC_CODE | SEC_ALLOC))
3149 return -1;
3150
3151 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3152 != (SEC_CODE | SEC_ALLOC)
3153 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3154 == (SEC_CODE | SEC_ALLOC))
3155 return 1;
3156
3157 if (synthetic_relocatable)
3158 {
3159 if (a->section->id < b->section->id)
3160 return -1;
3161
3162 if (a->section->id > b->section->id)
3163 return 1;
3164 }
3165
3166 if (a->value + a->section->vma < b->value + b->section->vma)
3167 return -1;
3168
3169 if (a->value + a->section->vma > b->value + b->section->vma)
3170 return 1;
3171
4d35a0aa
AM
3172 /* For syms with the same value, prefer strong dynamic global function
3173 syms over other syms. */
3174 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3175 return -1;
3176
3177 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3178 return 1;
3179
3180 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3181 return -1;
3182
3183 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3184 return 1;
3185
3186 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3187 return -1;
3188
3189 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3190 return 1;
3191
3192 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3193 return -1;
3194
3195 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3196 return 1;
3197
aaed6f5b 3198 return a > b;
90e3cdf2
JJ
3199}
3200
699733f6 3201/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3202
699733f6 3203static asymbol *
7292b3ac 3204sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3205{
699733f6 3206 long mid;
90e3cdf2 3207
7292b3ac 3208 if (id == (unsigned) -1)
699733f6
AM
3209 {
3210 while (lo < hi)
3211 {
3212 mid = (lo + hi) >> 1;
3213 if (syms[mid]->value + syms[mid]->section->vma < value)
3214 lo = mid + 1;
3215 else if (syms[mid]->value + syms[mid]->section->vma > value)
3216 hi = mid;
3217 else
3218 return syms[mid];
3219 }
3220 }
3221 else
3222 {
3223 while (lo < hi)
3224 {
3225 mid = (lo + hi) >> 1;
3226 if (syms[mid]->section->id < id)
3227 lo = mid + 1;
3228 else if (syms[mid]->section->id > id)
3229 hi = mid;
3230 else if (syms[mid]->value < value)
3231 lo = mid + 1;
3232 else if (syms[mid]->value > value)
3233 hi = mid;
3234 else
3235 return syms[mid];
3236 }
3237 }
3238 return NULL;
90e3cdf2
JJ
3239}
3240
468392fb
AM
3241static bfd_boolean
3242section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3243{
3244 bfd_vma vma = *(bfd_vma *) ptr;
3245 return ((section->flags & SEC_ALLOC) != 0
3246 && section->vma <= vma
3247 && vma < section->vma + section->size);
3248}
3249
699733f6 3250/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3251 entry syms. Also generate @plt symbols for the glink branch table.
3252 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3253
3254static long
a7535cf3
AM
3255ppc64_elf_get_synthetic_symtab (bfd *abfd,
3256 long static_count, asymbol **static_syms,
3257 long dyn_count, asymbol **dyn_syms,
c9727e01 3258 asymbol **ret)
90e3cdf2
JJ
3259{
3260 asymbol *s;
699733f6
AM
3261 long i;
3262 long count;
90e3cdf2 3263 char *names;
a7535cf3 3264 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3265 asection *opd = NULL;
90e3cdf2 3266 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3267 asymbol **syms;
ee67d69a 3268 int abi = abiversion (abfd);
90e3cdf2
JJ
3269
3270 *ret = NULL;
3271
ee67d69a
AM
3272 if (abi < 2)
3273 {
3274 opd = bfd_get_section_by_name (abfd, ".opd");
3275 if (opd == NULL && abi == 1)
3276 return 0;
3277 }
90e3cdf2 3278
a5259595
AM
3279 syms = NULL;
3280 codesecsym = 0;
3281 codesecsymend = 0;
3282 secsymend = 0;
3283 opdsymend = 0;
3284 symcount = 0;
3285 if (opd != NULL)
c9727e01 3286 {
a5259595
AM
3287 symcount = static_count;
3288 if (!relocatable)
3289 symcount += dyn_count;
3290 if (symcount == 0)
3291 return 0;
c9727e01 3292
a5259595
AM
3293 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3294 if (syms == NULL)
3295 return -1;
90e3cdf2 3296
a5259595
AM
3297 if (!relocatable && static_count != 0 && dyn_count != 0)
3298 {
3299 /* Use both symbol tables. */
3300 memcpy (syms, static_syms, static_count * sizeof (*syms));
3301 memcpy (syms + static_count, dyn_syms,
3302 (dyn_count + 1) * sizeof (*syms));
3303 }
3304 else if (!relocatable && static_count == 0)
3305 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3306 else
3307 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3308
a5259595
AM
3309 synthetic_relocatable = relocatable;
3310 synthetic_opd = opd;
3311 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3312
a5259595
AM
3313 if (!relocatable && symcount > 1)
3314 {
3315 long j;
3316 /* Trim duplicate syms, since we may have merged the normal and
3317 dynamic symbols. Actually, we only care about syms that have
3318 different values, so trim any with the same value. */
3319 for (i = 1, j = 1; i < symcount; ++i)
3320 if (syms[i - 1]->value + syms[i - 1]->section->vma
3321 != syms[i]->value + syms[i]->section->vma)
3322 syms[j++] = syms[i];
3323 symcount = j;
3324 }
699733f6 3325
a5259595
AM
3326 i = 0;
3327 /* Note that here and in compare_symbols we can't compare opd and
3328 sym->section directly. With separate debug info files, the
3329 symbols will be extracted from the debug file while abfd passed
3330 to this function is the real binary. */
3331 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3332 ++i;
3333 codesecsym = i;
3334
3335 for (; i < symcount; ++i)
3336 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3337 | SEC_THREAD_LOCAL))
3338 != (SEC_CODE | SEC_ALLOC))
3339 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3340 break;
3341 codesecsymend = i;
3342
3343 for (; i < symcount; ++i)
3344 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3345 break;
3346 secsymend = i;
3347
3348 for (; i < symcount; ++i)
3349 if (strcmp (syms[i]->section->name, ".opd") != 0)
3350 break;
3351 opdsymend = i;
3352
3353 for (; i < symcount; ++i)
3354 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3355 != (SEC_CODE | SEC_ALLOC))
3356 break;
3357 symcount = i;
3358 }
c9727e01 3359 count = 0;
90e3cdf2 3360
699733f6 3361 if (relocatable)
90e3cdf2 3362 {
699733f6
AM
3363 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3364 arelent *r;
3365 size_t size;
3366 long relcount;
90e3cdf2 3367
468392fb
AM
3368 if (opdsymend == secsymend)
3369 goto done;
3370
699733f6 3371 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3372 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3373 if (relcount == 0)
c9727e01 3374 goto done;
90e3cdf2 3375
7356fed5
AM
3376 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3377 {
3378 count = -1;
3379 goto done;
3380 }
3381
699733f6 3382 size = 0;
595da8c5 3383 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3384 {
3385 asymbol *sym;
90e3cdf2 3386
595da8c5 3387 while (r < opd->relocation + relcount
699733f6
AM
3388 && r->address < syms[i]->value + opd->vma)
3389 ++r;
90e3cdf2 3390
595da8c5 3391 if (r == opd->relocation + relcount)
699733f6 3392 break;
90e3cdf2 3393
699733f6
AM
3394 if (r->address != syms[i]->value + opd->vma)
3395 continue;
90e3cdf2 3396
699733f6
AM
3397 if (r->howto->type != R_PPC64_ADDR64)
3398 continue;
90e3cdf2 3399
699733f6
AM
3400 sym = *r->sym_ptr_ptr;
3401 if (!sym_exists_at (syms, opdsymend, symcount,
3402 sym->section->id, sym->value + r->addend))
3403 {
3404 ++count;
3405 size += sizeof (asymbol);
3406 size += strlen (syms[i]->name) + 2;
3407 }
3408 }
90e3cdf2 3409
c4b0b099
AM
3410 if (size == 0)
3411 goto done;
699733f6
AM
3412 s = *ret = bfd_malloc (size);
3413 if (s == NULL)
3414 {
7356fed5 3415 count = -1;
c9727e01 3416 goto done;
699733f6 3417 }
90e3cdf2 3418
699733f6 3419 names = (char *) (s + count);
90e3cdf2 3420
595da8c5 3421 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3422 {
699733f6 3423 asymbol *sym;
90e3cdf2 3424
595da8c5 3425 while (r < opd->relocation + relcount
699733f6
AM
3426 && r->address < syms[i]->value + opd->vma)
3427 ++r;
90e3cdf2 3428
595da8c5 3429 if (r == opd->relocation + relcount)
699733f6
AM
3430 break;
3431
3432 if (r->address != syms[i]->value + opd->vma)
3433 continue;
3434
3435 if (r->howto->type != R_PPC64_ADDR64)
3436 continue;
90e3cdf2 3437
699733f6
AM
3438 sym = *r->sym_ptr_ptr;
3439 if (!sym_exists_at (syms, opdsymend, symcount,
3440 sym->section->id, sym->value + r->addend))
3441 {
3442 size_t len;
3443
3444 *s = *syms[i];
6ba2a415 3445 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3446 s->section = sym->section;
3447 s->value = sym->value + r->addend;
3448 s->name = names;
3449 *names++ = '.';
3450 len = strlen (syms[i]->name);
3451 memcpy (names, syms[i]->name, len + 1);
3452 names += len + 1;
6f610d07
UW
3453 /* Have udata.p point back to the original symbol this
3454 synthetic symbol was derived from. */
3455 s->udata.p = syms[i];
699733f6
AM
3456 s++;
3457 }
3458 }
3459 }
3460 else
90e3cdf2 3461 {
468392fb 3462 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3463 bfd_byte *contents = NULL;
699733f6 3464 size_t size;
468392fb
AM
3465 long plt_count = 0;
3466 bfd_vma glink_vma = 0, resolv_vma = 0;
3467 asection *dynamic, *glink = NULL, *relplt = NULL;
3468 arelent *p;
90e3cdf2 3469
ee67d69a 3470 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3471 {
c4b0b099
AM
3472 free_contents_and_exit_err:
3473 count = -1;
ee67d69a 3474 free_contents_and_exit:
699733f6 3475 if (contents)
ee67d69a 3476 free (contents);
c9727e01 3477 goto done;
699733f6 3478 }
90e3cdf2 3479
699733f6
AM
3480 size = 0;
3481 for (i = secsymend; i < opdsymend; ++i)
3482 {
3483 bfd_vma ent;
90e3cdf2 3484
5ef11c02
AM
3485 /* Ignore bogus symbols. */
3486 if (syms[i]->value > opd->size - 8)
3487 continue;
3488
699733f6
AM
3489 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3490 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3491 {
3492 ++count;
3493 size += sizeof (asymbol);
3494 size += strlen (syms[i]->name) + 2;
3495 }
3496 }
90e3cdf2 3497
468392fb 3498 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3499 if (dyn_count != 0
3500 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3501 {
3502 bfd_byte *dynbuf, *extdyn, *extdynend;
3503 size_t extdynsize;
3504 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3505
3506 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3507 goto free_contents_and_exit_err;
468392fb
AM
3508
3509 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3510 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3511
3512 extdyn = dynbuf;
3513 extdynend = extdyn + dynamic->size;
3514 for (; extdyn < extdynend; extdyn += extdynsize)
3515 {
3516 Elf_Internal_Dyn dyn;
3517 (*swap_dyn_in) (abfd, extdyn, &dyn);
3518
3519 if (dyn.d_tag == DT_NULL)
3520 break;
3521
3522 if (dyn.d_tag == DT_PPC64_GLINK)
3523 {
9e390558
AM
3524 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3525 See comment in ppc64_elf_finish_dynamic_sections. */
3526 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3527 /* The .glink section usually does not survive the final
3528 link; search for the section (usually .text) where the
3529 glink stubs now reside. */
3530 glink = bfd_sections_find_if (abfd, section_covers_vma,
3531 &glink_vma);
3532 break;
3533 }
3534 }
3535
3536 free (dynbuf);
3537 }
3538
3539 if (glink != NULL)
3540 {
3541 /* Determine __glink trampoline by reading the relative branch
3542 from the first glink stub. */
3543 bfd_byte buf[4];
b9e5796b
AM
3544 unsigned int off = 0;
3545
3546 while (bfd_get_section_contents (abfd, glink, buf,
3547 glink_vma + off - glink->vma, 4))
468392fb
AM
3548 {
3549 unsigned int insn = bfd_get_32 (abfd, buf);
3550 insn ^= B_DOT;
3551 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3552 {
3553 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3554 break;
3555 }
3556 off += 4;
3557 if (off > 4)
3558 break;
468392fb
AM
3559 }
3560
3561 if (resolv_vma)
3562 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3563
066ee829
AM
3564 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3565 if (relplt != NULL)
3566 {
3567 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3568 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3569 goto free_contents_and_exit_err;
68ffbac6 3570
066ee829
AM
3571 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3572 size += plt_count * sizeof (asymbol);
468392fb 3573
066ee829
AM
3574 p = relplt->relocation;
3575 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3576 {
3577 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3578 if (p->addend != 0)
3579 size += sizeof ("+0x") - 1 + 16;
3580 }
066ee829 3581 }
468392fb
AM
3582 }
3583
c4b0b099
AM
3584 if (size == 0)
3585 goto free_contents_and_exit;
699733f6
AM
3586 s = *ret = bfd_malloc (size);
3587 if (s == NULL)
c4b0b099 3588 goto free_contents_and_exit_err;
90e3cdf2 3589
468392fb 3590 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3591
699733f6 3592 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3593 {
699733f6 3594 bfd_vma ent;
90e3cdf2 3595
5ef11c02
AM
3596 if (syms[i]->value > opd->size - 8)
3597 continue;
3598
699733f6
AM
3599 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3600 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3601 {
c9727e01 3602 long lo, hi;
699733f6 3603 size_t len;
c9727e01 3604 asection *sec = abfd->sections;
90e3cdf2 3605
699733f6
AM
3606 *s = *syms[i];
3607 lo = codesecsym;
3608 hi = codesecsymend;
3609 while (lo < hi)
3610 {
c9727e01 3611 long mid = (lo + hi) >> 1;
699733f6
AM
3612 if (syms[mid]->section->vma < ent)
3613 lo = mid + 1;
3614 else if (syms[mid]->section->vma > ent)
3615 hi = mid;
3616 else
c9727e01
AM
3617 {
3618 sec = syms[mid]->section;
3619 break;
3620 }
699733f6
AM
3621 }
3622
c9727e01 3623 if (lo >= hi && lo > codesecsym)
699733f6 3624 sec = syms[lo - 1]->section;
699733f6
AM
3625
3626 for (; sec != NULL; sec = sec->next)
3627 {
3628 if (sec->vma > ent)
3629 break;
63524580
JK
3630 /* SEC_LOAD may not be set if SEC is from a separate debug
3631 info file. */
3632 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3633 break;
3634 if ((sec->flags & SEC_CODE) != 0)
3635 s->section = sec;
3636 }
6ba2a415 3637 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3638 s->value = ent - s->section->vma;
3639 s->name = names;
3640 *names++ = '.';
3641 len = strlen (syms[i]->name);
3642 memcpy (names, syms[i]->name, len + 1);
3643 names += len + 1;
6f610d07
UW
3644 /* Have udata.p point back to the original symbol this
3645 synthetic symbol was derived from. */
3646 s->udata.p = syms[i];
699733f6 3647 s++;
90e3cdf2 3648 }
90e3cdf2 3649 }
699733f6 3650 free (contents);
468392fb
AM
3651
3652 if (glink != NULL && relplt != NULL)
3653 {
3654 if (resolv_vma)
3655 {
3656 /* Add a symbol for the main glink trampoline. */
86a4952b 3657 memset (s, 0, sizeof *s);
468392fb 3658 s->the_bfd = abfd;
6ba2a415 3659 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3660 s->section = glink;
3661 s->value = resolv_vma - glink->vma;
3662 s->name = names;
3663 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3664 names += sizeof ("__glink_PLTresolve");
3665 s++;
3666 count++;
3667 }
3668
3669 /* FIXME: It would be very much nicer to put sym@plt on the
3670 stub rather than on the glink branch table entry. The
3671 objdump disassembler would then use a sensible symbol
3672 name on plt calls. The difficulty in doing so is
3673 a) finding the stubs, and,
3674 b) matching stubs against plt entries, and,
3675 c) there can be multiple stubs for a given plt entry.
3676
3677 Solving (a) could be done by code scanning, but older
3678 ppc64 binaries used different stubs to current code.
3679 (b) is the tricky one since you need to known the toc
3680 pointer for at least one function that uses a pic stub to
3681 be able to calculate the plt address referenced.
3682 (c) means gdb would need to set multiple breakpoints (or
3683 find the glink branch itself) when setting breakpoints
3684 for pending shared library loads. */
3685 p = relplt->relocation;
3686 for (i = 0; i < plt_count; i++, p++)
3687 {
3688 size_t len;
3689
3690 *s = **p->sym_ptr_ptr;
3691 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3692 we are defining a symbol, ensure one of them is set. */
3693 if ((s->flags & BSF_LOCAL) == 0)
3694 s->flags |= BSF_GLOBAL;
6ba2a415 3695 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3696 s->section = glink;
3697 s->value = glink_vma - glink->vma;
3698 s->name = names;
3699 s->udata.p = NULL;
3700 len = strlen ((*p->sym_ptr_ptr)->name);
3701 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3702 names += len;
e054468f
AM
3703 if (p->addend != 0)
3704 {
3705 memcpy (names, "+0x", sizeof ("+0x") - 1);
3706 names += sizeof ("+0x") - 1;
3707 bfd_sprintf_vma (abfd, names, p->addend);
3708 names += strlen (names);
3709 }
468392fb
AM
3710 memcpy (names, "@plt", sizeof ("@plt"));
3711 names += sizeof ("@plt");
3712 s++;
b9e5796b
AM
3713 if (abi < 2)
3714 {
3715 glink_vma += 8;
3716 if (i >= 0x8000)
3717 glink_vma += 4;
3718 }
3719 else
468392fb
AM
3720 glink_vma += 4;
3721 }
3722 count += plt_count;
3723 }
90e3cdf2
JJ
3724 }
3725
c9727e01 3726 done:
a7535cf3 3727 free (syms);
90e3cdf2
JJ
3728 return count;
3729}
5bd4f169 3730\f
65f38f15
AM
3731/* The following functions are specific to the ELF linker, while
3732 functions above are used generally. Those named ppc64_elf_* are
3733 called by the main ELF linker code. They appear in this file more
3734 or less in the order in which they are called. eg.
3735 ppc64_elf_check_relocs is called early in the link process,
3736 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3737 called.
3738
3739 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3740 functions have both a function code symbol and a function descriptor
3741 symbol. A call to foo in a relocatable object file looks like:
3742
3743 . .text
3744 . x:
3745 . bl .foo
3746 . nop
3747
3748 The function definition in another object file might be:
3749
3750 . .section .opd
3751 . foo: .quad .foo
3752 . .quad .TOC.@tocbase
3753 . .quad 0
3754 .
3755 . .text
3756 . .foo: blr
3757
3758 When the linker resolves the call during a static link, the branch
3759 unsurprisingly just goes to .foo and the .opd information is unused.
3760 If the function definition is in a shared library, things are a little
3761 different: The call goes via a plt call stub, the opd information gets
3762 copied to the plt, and the linker patches the nop.
3763
3764 . x:
3765 . bl .foo_stub
3766 . ld 2,40(1)
3767 .
3768 .
3769 . .foo_stub:
71a39c98
AM
3770 . std 2,40(1) # in practice, the call stub
3771 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3772 . addi 11,11,Lfoo@toc@l # this is the general idea
3773 . ld 12,0(11)
3774 . ld 2,8(11)
3775 . mtctr 12
3776 . ld 11,16(11)
e86ce104
AM
3777 . bctr
3778 .
3779 . .section .plt
3780 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3781
3782 The "reloc ()" notation is supposed to indicate that the linker emits
3783 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3784 copying.
3785
3786 What are the difficulties here? Well, firstly, the relocations
3787 examined by the linker in check_relocs are against the function code
3788 sym .foo, while the dynamic relocation in the plt is emitted against
3789 the function descriptor symbol, foo. Somewhere along the line, we need
3790 to carefully copy dynamic link information from one symbol to the other.
3791 Secondly, the generic part of the elf linker will make .foo a dynamic
3792 symbol as is normal for most other backends. We need foo dynamic
3793 instead, at least for an application final link. However, when
3794 creating a shared library containing foo, we need to have both symbols
3795 dynamic so that references to .foo are satisfied during the early
3796 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3797 definition from some other object, eg. a static library.
3798
3799 Update: As of August 2004, we support a new convention. Function
3800 calls may use the function descriptor symbol, ie. "bl foo". This
3801 behaves exactly as "bl .foo". */
65f38f15 3802
7c8bbca5
AM
3803/* Of those relocs that might be copied as dynamic relocs, this
3804 function selects those that must be copied when linking a shared
3805 library or PIE, even when the symbol is local. */
65f38f15 3806
1d483afe
AM
3807static int
3808must_be_dyn_reloc (struct bfd_link_info *info,
3809 enum elf_ppc64_reloc_type r_type)
3810{
3811 switch (r_type)
3812 {
3813 default:
7c8bbca5
AM
3814 /* Only relative relocs can be resolved when the object load
3815 address isn't fixed. DTPREL64 is excluded because the
3816 dynamic linker needs to differentiate global dynamic from
3817 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3818 return 1;
3819
3820 case R_PPC64_REL32:
3821 case R_PPC64_REL64:
3822 case R_PPC64_REL30:
3823 return 0;
3824
3825 case R_PPC64_TPREL16:
3826 case R_PPC64_TPREL16_LO:
3827 case R_PPC64_TPREL16_HI:
3828 case R_PPC64_TPREL16_HA:
3829 case R_PPC64_TPREL16_DS:
3830 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3831 case R_PPC64_TPREL16_HIGH:
3832 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3833 case R_PPC64_TPREL16_HIGHER:
3834 case R_PPC64_TPREL16_HIGHERA:
3835 case R_PPC64_TPREL16_HIGHEST:
3836 case R_PPC64_TPREL16_HIGHESTA:
3837 case R_PPC64_TPREL64:
7c8bbca5
AM
3838 /* These relocations are relative but in a shared library the
3839 linker doesn't know the thread pointer base. */
3840 return bfd_link_dll (info);
1d483afe
AM
3841 }
3842}
65f38f15 3843
f4656909
AM
3844/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3845 copying dynamic variables from a shared lib into an app's dynbss
3846 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3847 shared lib. With code that gcc generates, it's vital that this be
3848 enabled; In the PowerPC64 ABI, the address of a function is actually
3849 the address of a function descriptor, which resides in the .opd
3850 section. gcc uses the descriptor directly rather than going via the
3851 GOT as some other ABI's do, which means that initialized function
3852 pointers must reference the descriptor. Thus, a function pointer
3853 initialized to the address of a function in a shared library will
3854 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3855 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3856 presents a problem as a plt entry for that function is also
3857 initialized from the function descriptor symbol and the copy reloc
3858 may not be initialized first. */
a23b6845 3859#define ELIMINATE_COPY_RELOCS 1
f4656909 3860
721956f4 3861/* Section name for stubs is the associated section name plus this
29942be8
NC
3862 string. */
3863#define STUB_SUFFIX ".stub"
721956f4
AM
3864
3865/* Linker stubs.
3866 ppc_stub_long_branch:
3867 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3868 destination, but a 24 bit branch in a stub section will reach.
3869 . b dest
3870
3871 ppc_stub_plt_branch:
3872 Similar to the above, but a 24 bit branch in the stub section won't
3873 reach its destination.
71a39c98
AM
3874 . addis %r11,%r2,xxx@toc@ha
3875 . ld %r12,xxx@toc@l(%r11)
3876 . mtctr %r12
721956f4
AM
3877 . bctr
3878
3879 ppc_stub_plt_call:
2c66dc6c
AM
3880 Used to call a function in a shared library. If it so happens that
3881 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3882 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3883 . std %r2,40(%r1)
71a39c98
AM
3884 . addis %r11,%r2,xxx@toc@ha
3885 . ld %r12,xxx+0@toc@l(%r11)
3886 . mtctr %r12
3887 . ld %r2,xxx+8@toc@l(%r11)
3888 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3889 . bctr
ad8e1ba5
AM
3890
3891 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3892 code to adjust the value and save r2 to support multiple toc sections.
3893 A ppc_stub_long_branch with an r2 offset looks like:
3894 . std %r2,40(%r1)
3895 . addis %r2,%r2,off@ha
3896 . addi %r2,%r2,off@l
3897 . b dest
3898
3899 A ppc_stub_plt_branch with an r2 offset looks like:
3900 . std %r2,40(%r1)
71a39c98
AM
3901 . addis %r11,%r2,xxx@toc@ha
3902 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3903 . addis %r2,%r2,off@ha
3904 . addi %r2,%r2,off@l
71a39c98 3905 . mtctr %r12
ad8e1ba5 3906 . bctr
ac2df442
AM
3907
3908 In cases where the "addis" instruction would add zero, the "addis" is
3909 omitted and following instructions modified slightly in some cases.
721956f4
AM
3910*/
3911
3912enum ppc_stub_type {
3913 ppc_stub_none,
3914 ppc_stub_long_branch,
ad8e1ba5 3915 ppc_stub_long_branch_r2off,
721956f4 3916 ppc_stub_plt_branch,
ad8e1ba5 3917 ppc_stub_plt_branch_r2off,
794e51c0 3918 ppc_stub_plt_call,
7341d5e2 3919 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3920 ppc_stub_global_entry,
3921 ppc_stub_save_res
721956f4
AM
3922};
3923
6f20ed8a
AM
3924/* Information on stub grouping. */
3925struct map_stub
3926{
3927 /* The stub section. */
3928 asection *stub_sec;
3929 /* This is the section to which stubs in the group will be attached. */
3930 asection *link_sec;
a4b6fadd
AM
3931 /* Next group. */
3932 struct map_stub *next;
3933 /* Whether to emit a copy of register save/restore functions in this
3934 group. */
3935 int needs_save_res;
d4aaa2a0
AM
3936 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3937 or -1u if no such stub with bctrl exists. */
3938 unsigned int tls_get_addr_opt_bctrl;
6f20ed8a
AM
3939};
3940
721956f4
AM
3941struct ppc_stub_hash_entry {
3942
3943 /* Base hash table entry structure. */
3944 struct bfd_hash_entry root;
3945
ad8e1ba5
AM
3946 enum ppc_stub_type stub_type;
3947
6f20ed8a
AM
3948 /* Group information. */
3949 struct map_stub *group;
721956f4
AM
3950
3951 /* Offset within stub_sec of the beginning of this stub. */
3952 bfd_vma stub_offset;
3953
3954 /* Given the symbol's value and its section we can determine its final
3955 value when building the stubs (so the stub knows where to jump. */
3956 bfd_vma target_value;
3957 asection *target_section;
3958
721956f4
AM
3959 /* The symbol table entry, if any, that this was derived from. */
3960 struct ppc_link_hash_entry *h;
e054468f 3961 struct plt_entry *plt_ent;
721956f4 3962
6911b7dc
AM
3963 /* Symbol st_other. */
3964 unsigned char other;
721956f4
AM
3965};
3966
3967struct ppc_branch_hash_entry {
3968
3969 /* Base hash table entry structure. */
3970 struct bfd_hash_entry root;
3971
c456f082 3972 /* Offset within branch lookup table. */
721956f4
AM
3973 unsigned int offset;
3974
3975 /* Generation marker. */
3976 unsigned int iter;
3977};
65f38f15 3978
19e08130
AM
3979/* Used to track dynamic relocations for local symbols. */
3980struct ppc_dyn_relocs
3981{
3982 struct ppc_dyn_relocs *next;
3983
3984 /* The input section of the reloc. */
3985 asection *sec;
3986
3987 /* Total number of relocs copied for the input section. */
3988 unsigned int count : 31;
3989
3990 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3991 unsigned int ifunc : 1;
3992};
3993
65f38f15
AM
3994struct ppc_link_hash_entry
3995{
3996 struct elf_link_hash_entry elf;
3997
b3fac117
AM
3998 union {
3999 /* A pointer to the most recently used stub hash entry against this
4000 symbol. */
4001 struct ppc_stub_hash_entry *stub_cache;
4002
4003 /* A pointer to the next symbol starting with a '.' */
4004 struct ppc_link_hash_entry *next_dot_sym;
4005 } u;
721956f4 4006
65f38f15 4007 /* Track dynamic relocs copied for this symbol. */
6061a67d 4008 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4009
721956f4 4010 /* Link between function code and descriptor symbols. */
34814b9f 4011 struct ppc_link_hash_entry *oh;
721956f4 4012
e86ce104
AM
4013 /* Flag function code and descriptor symbols. */
4014 unsigned int is_func:1;
4015 unsigned int is_func_descriptor:1;
908b32fc 4016 unsigned int fake:1;
411e1bfb 4017
c5614fa4
AM
4018 /* Whether global opd/toc sym has been adjusted or not.
4019 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4020 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4021 unsigned int adjust_done:1;
4022
a4b6fadd
AM
4023 /* Set if this is an out-of-line register save/restore function,
4024 with non-standard calling convention. */
4025 unsigned int save_res:1;
4026
8b5f1ed8
AM
4027 /* Set if a duplicate symbol with non-zero localentry is detected,
4028 even when the duplicate symbol does not provide a definition. */
4029 unsigned int non_zero_localentry:1;
4030
411e1bfb 4031 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
4032 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4033 corresponding relocs are encountered during check_relocs.
4034 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4035 indicate the corresponding GOT entry type is not needed.
4036 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4037 a TPREL one. We use a separate flag rather than setting TPREL
4038 just for convenience in distinguishing the two cases. */
4039#define TLS_GD 1 /* GD reloc. */
4040#define TLS_LD 2 /* LD reloc. */
4041#define TLS_TPREL 4 /* TPREL reloc, => IE. */
4042#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4043#define TLS_TLS 16 /* Any TLS reloc. */
4044#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4045#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4046#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4047 unsigned char tls_mask;
65f38f15
AM
4048};
4049
4050/* ppc64 ELF linker hash table. */
4051
4052struct ppc_link_hash_table
4053{
4054 struct elf_link_hash_table elf;
4055
721956f4
AM
4056 /* The stub hash table. */
4057 struct bfd_hash_table stub_hash_table;
4058
4059 /* Another hash table for plt_branch stubs. */
4060 struct bfd_hash_table branch_hash_table;
4061
3b421ab3
AM
4062 /* Hash table for function prologue tocsave. */
4063 htab_t tocsave_htab;
4064
e7d1c40c
AM
4065 /* Various options and other info passed from the linker. */
4066 struct ppc64_elf_params *params;
721956f4 4067
6f20ed8a
AM
4068 /* The size of sec_info below. */
4069 unsigned int sec_info_arr_size;
4070
4071 /* Per-section array of extra section info. Done this way rather
4072 than as part of ppc64_elf_section_data so we have the info for
4073 non-ppc64 sections. */
4074 struct
4075 {
4076 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4077 bfd_vma toc_off;
6f20ed8a
AM
4078
4079 union
4080 {
4081 /* The section group that this section belongs to. */
4082 struct map_stub *group;
4083 /* A temp section list pointer. */
4084 asection *list;
4085 } u;
4086 } *sec_info;
721956f4 4087
a4b6fadd
AM
4088 /* Linked list of groups. */
4089 struct map_stub *group;
4090
ad8e1ba5
AM
4091 /* Temp used when calculating TOC pointers. */
4092 bfd_vma toc_curr;
bf102f86
AM
4093 bfd *toc_bfd;
4094 asection *toc_first_sec;
ad8e1ba5 4095
b3fac117
AM
4096 /* Used when adding symbols. */
4097 struct ppc_link_hash_entry *dot_syms;
4098
33e44f2e 4099 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4100 asection *glink;
9e390558 4101 asection *global_entry;
82bd7b59 4102 asection *sfpr;
4ce794b7
AM
4103 asection *brlt;
4104 asection *relbrlt;
58d180e8 4105 asection *glink_eh_frame;
ec338859 4106
8387904d
AM
4107 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4108 struct ppc_link_hash_entry *tls_get_addr;
4109 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4110
927be08e
AM
4111 /* The size of reliplt used by got entry relocs. */
4112 bfd_size_type got_reli_size;
4113
9b5ecbd0 4114 /* Statistics. */
7341d5e2 4115 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4116
ee75fd95
AM
4117 /* Number of stubs against global syms. */
4118 unsigned long stub_globals;
4119
ee67d69a
AM
4120 /* Set if we're linking code with function descriptors. */
4121 unsigned int opd_abi:1;
4122
4c52953f 4123 /* Support for multiple toc sections. */
33c0ec9d 4124 unsigned int do_multi_toc:1;
4c52953f 4125 unsigned int multi_toc_needed:1;
927be08e 4126 unsigned int second_toc_pass:1;
67f0cbdb 4127 unsigned int do_toc_opt:1;
4c52953f 4128
9a23f96e
AM
4129 /* Set if tls optimization is enabled. */
4130 unsigned int do_tls_opt:1;
4131
5d1634d7 4132 /* Set on error. */
99877b66 4133 unsigned int stub_error:1;
721956f4 4134
8c5b4e52
AM
4135 /* Whether func_desc_adjust needs to be run over symbols. */
4136 unsigned int need_func_desc_adj:1;
721956f4 4137
82e66161
AM
4138 /* Whether there exist local gnu indirect function resolvers,
4139 referenced by dynamic relocations. */
4140 unsigned int local_ifunc_resolver:1;
4141 unsigned int maybe_local_ifunc_resolver:1;
4142
f378ab09
AM
4143 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4144 unsigned int has_plt_localentry0:1;
4145
721956f4
AM
4146 /* Incremented every time we size stubs. */
4147 unsigned int stub_iteration;
5d1634d7 4148
87d72d41
AM
4149 /* Small local sym cache. */
4150 struct sym_cache sym_cache;
65f38f15
AM
4151};
4152
4c52953f
AM
4153/* Rename some of the generic section flags to better document how they
4154 are used here. */
b0dddeec
AM
4155
4156/* Nonzero if this section has TLS related relocations. */
4157#define has_tls_reloc sec_flg0
4158
4159/* Nonzero if this section has a call to __tls_get_addr. */
4160#define has_tls_get_addr_call sec_flg1
4161
4162/* Nonzero if this section has any toc or got relocs. */
4163#define has_toc_reloc sec_flg2
4164
4165/* Nonzero if this section has a call to another section that uses
4166 the toc or got. */
d77c8a4b 4167#define makes_toc_func_call sec_flg3
b0dddeec
AM
4168
4169/* Recursion protection when determining above flag. */
d77c8a4b 4170#define call_check_in_progress sec_flg4
70cc837d 4171#define call_check_done sec_flg5
4c52953f 4172
65f38f15
AM
4173/* Get the ppc64 ELF linker hash table from a link_info structure. */
4174
4175#define ppc_hash_table(p) \
4dfe6ac6
NC
4176 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4177 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4178
721956f4
AM
4179#define ppc_stub_hash_lookup(table, string, create, copy) \
4180 ((struct ppc_stub_hash_entry *) \
4181 bfd_hash_lookup ((table), (string), (create), (copy)))
4182
4183#define ppc_branch_hash_lookup(table, string, create, copy) \
4184 ((struct ppc_branch_hash_entry *) \
4185 bfd_hash_lookup ((table), (string), (create), (copy)))
4186
4187/* Create an entry in the stub hash table. */
4188
4189static struct bfd_hash_entry *
4ce794b7
AM
4190stub_hash_newfunc (struct bfd_hash_entry *entry,
4191 struct bfd_hash_table *table,
4192 const char *string)
721956f4
AM
4193{
4194 /* Allocate the structure if it has not already been allocated by a
4195 subclass. */
4196 if (entry == NULL)
4197 {
4198 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4199 if (entry == NULL)
4200 return entry;
4201 }
4202
4203 /* Call the allocation method of the superclass. */
4204 entry = bfd_hash_newfunc (entry, table, string);
4205 if (entry != NULL)
4206 {
4207 struct ppc_stub_hash_entry *eh;
4208
4209 /* Initialize the local fields. */
4210 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4211 eh->stub_type = ppc_stub_none;
6f20ed8a 4212 eh->group = NULL;
721956f4
AM
4213 eh->stub_offset = 0;
4214 eh->target_value = 0;
4215 eh->target_section = NULL;
721956f4 4216 eh->h = NULL;
6911b7dc 4217 eh->plt_ent = NULL;
6911b7dc 4218 eh->other = 0;
721956f4
AM
4219 }
4220
4221 return entry;
4222}
4223
4224/* Create an entry in the branch hash table. */
4225
4226static struct bfd_hash_entry *
4ce794b7
AM
4227branch_hash_newfunc (struct bfd_hash_entry *entry,
4228 struct bfd_hash_table *table,
4229 const char *string)
721956f4
AM
4230{
4231 /* Allocate the structure if it has not already been allocated by a
4232 subclass. */
4233 if (entry == NULL)
4234 {
4235 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4236 if (entry == NULL)
4237 return entry;
4238 }
4239
4240 /* Call the allocation method of the superclass. */
4241 entry = bfd_hash_newfunc (entry, table, string);
4242 if (entry != NULL)
4243 {
4244 struct ppc_branch_hash_entry *eh;
4245
4246 /* Initialize the local fields. */
4247 eh = (struct ppc_branch_hash_entry *) entry;
4248 eh->offset = 0;
4249 eh->iter = 0;
4250 }
4251
4252 return entry;
4253}
4254
65f38f15
AM
4255/* Create an entry in a ppc64 ELF linker hash table. */
4256
4257static struct bfd_hash_entry *
4ce794b7
AM
4258link_hash_newfunc (struct bfd_hash_entry *entry,
4259 struct bfd_hash_table *table,
4260 const char *string)
65f38f15
AM
4261{
4262 /* Allocate the structure if it has not already been allocated by a
4263 subclass. */
4264 if (entry == NULL)
4265 {
4266 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4267 if (entry == NULL)
4268 return entry;
4269 }
4270
4271 /* Call the allocation method of the superclass. */
4272 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4273 if (entry != NULL)
4274 {
4275 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4276
b3fac117 4277 memset (&eh->u.stub_cache, 0,
908b32fc 4278 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4279 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4280
4281 /* When making function calls, old ABI code references function entry
4282 points (dot symbols), while new ABI code references the function
4283 descriptor symbol. We need to make any combination of reference and
4284 definition work together, without breaking archive linking.
4285
4286 For a defined function "foo" and an undefined call to "bar":
4287 An old object defines "foo" and ".foo", references ".bar" (possibly
4288 "bar" too).
4289 A new object defines "foo" and references "bar".
4290
4291 A new object thus has no problem with its undefined symbols being
4292 satisfied by definitions in an old object. On the other hand, the
4293 old object won't have ".bar" satisfied by a new object.
4294
4295 Keep a list of newly added dot-symbols. */
4296
4297 if (string[0] == '.')
4298 {
4299 struct ppc_link_hash_table *htab;
4300
4301 htab = (struct ppc_link_hash_table *) table;
4302 eh->u.next_dot_sym = htab->dot_syms;
4303 htab->dot_syms = eh;
4304 }
65f38f15
AM
4305 }
4306
4307 return entry;
4308}
4309
3b421ab3
AM
4310struct tocsave_entry {
4311 asection *sec;
4312 bfd_vma offset;
4313};
4314
4315static hashval_t
4316tocsave_htab_hash (const void *p)
4317{
4318 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4319 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4320}
4321
4322static int
4323tocsave_htab_eq (const void *p1, const void *p2)
4324{
4325 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4326 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4327 return e1->sec == e2->sec && e1->offset == e2->offset;
4328}
4329
68faa637
AM
4330/* Destroy a ppc64 ELF linker hash table. */
4331
4332static void
d495ab0d 4333ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4334{
d495ab0d 4335 struct ppc_link_hash_table *htab;
68faa637 4336
d495ab0d 4337 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4338 if (htab->tocsave_htab)
4339 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4340 bfd_hash_table_free (&htab->branch_hash_table);
4341 bfd_hash_table_free (&htab->stub_hash_table);
4342 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4343}
4344
65f38f15
AM
4345/* Create a ppc64 ELF linker hash table. */
4346
4347static struct bfd_link_hash_table *
4ce794b7 4348ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4349{
4350 struct ppc_link_hash_table *htab;
4351 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4352
4ce794b7 4353 htab = bfd_zmalloc (amt);
65f38f15
AM
4354 if (htab == NULL)
4355 return NULL;
4356
66eb6687 4357 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4358 sizeof (struct ppc_link_hash_entry),
4359 PPC64_ELF_DATA))
65f38f15 4360 {
e2d34d7d 4361 free (htab);
65f38f15
AM
4362 return NULL;
4363 }
4364
721956f4 4365 /* Init the stub hash table too. */
66eb6687
AM
4366 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4367 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4368 {
d495ab0d 4369 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4370 return NULL;
4371 }
721956f4
AM
4372
4373 /* And the branch hash table. */
66eb6687
AM
4374 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4375 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4376 {
4377 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4378 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4379 return NULL;
4380 }
721956f4 4381
3b421ab3
AM
4382 htab->tocsave_htab = htab_try_create (1024,
4383 tocsave_htab_hash,
4384 tocsave_htab_eq,
4385 NULL);
4386 if (htab->tocsave_htab == NULL)
2915c55b 4387 {
d495ab0d 4388 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4389 return NULL;
4390 }
d495ab0d 4391 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4392
3254fd24
AM
4393 /* Initializing two fields of the union is just cosmetic. We really
4394 only care about glist, but when compiled on a 32-bit host the
4395 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4396 debugger inspection of these fields look nicer. */
a6aa5195
AM
4397 htab->elf.init_got_refcount.refcount = 0;
4398 htab->elf.init_got_refcount.glist = NULL;
4399 htab->elf.init_plt_refcount.refcount = 0;
4400 htab->elf.init_plt_refcount.glist = NULL;
4401 htab->elf.init_got_offset.offset = 0;
4402 htab->elf.init_got_offset.glist = NULL;
4403 htab->elf.init_plt_offset.offset = 0;
4404 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4405
65f38f15
AM
4406 return &htab->elf.root;
4407}
4408
bfeb4a28
AM
4409/* Create sections for linker generated code. */
4410
4411static bfd_boolean
4412create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4413{
4414 struct ppc_link_hash_table *htab;
4415 flagword flags;
4416
4417 htab = ppc_hash_table (info);
4418
bfeb4a28
AM
4419 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4420 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4421 if (htab->params->save_restore_funcs)
4422 {
4423 /* Create .sfpr for code to save and restore fp regs. */
4424 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4425 flags);
4426 if (htab->sfpr == NULL
4427 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4428 return FALSE;
4429 }
4430
4431 if (bfd_link_relocatable (info))
4432 return TRUE;
bfeb4a28
AM
4433
4434 /* Create .glink for lazy dynamic linking support. */
4435 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4436 flags);
4437 if (htab->glink == NULL
4438 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4439 return FALSE;
4440
9e390558
AM
4441 /* The part of .glink used by global entry stubs, separate so that
4442 it can be aligned appropriately without affecting htab->glink. */
4443 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4444 flags);
4445 if (htab->global_entry == NULL
4446 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4447 return FALSE;
4448
bfeb4a28
AM
4449 if (!info->no_ld_generated_unwind_info)
4450 {
4451 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4452 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4453 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4454 ".eh_frame",
4455 flags);
4456 if (htab->glink_eh_frame == NULL
4457 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4458 return FALSE;
4459 }
4460
4461 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4462 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4463 if (htab->elf.iplt == NULL
4464 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4465 return FALSE;
4466
4467 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4468 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4469 htab->elf.irelplt
4470 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4471 if (htab->elf.irelplt == NULL
4472 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4473 return FALSE;
4474
4475 /* Create branch lookup table for plt_branch stubs. */
4476 flags = (SEC_ALLOC | SEC_LOAD
4477 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4478 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4479 flags);
4480 if (htab->brlt == NULL
4481 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4482 return FALSE;
4483
0e1862bb 4484 if (!bfd_link_pic (info))
bfeb4a28
AM
4485 return TRUE;
4486
4487 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4488 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4489 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4490 ".rela.branch_lt",
4491 flags);
4492 if (htab->relbrlt == NULL
4493 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4494 return FALSE;
4495
4496 return TRUE;
4497}
4498
e717da7e
AM
4499/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4500
bfeb4a28 4501bfd_boolean
e7d1c40c
AM
4502ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4503 struct ppc64_elf_params *params)
e717da7e
AM
4504{
4505 struct ppc_link_hash_table *htab;
4506
e7d1c40c 4507 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4508
4509/* Always hook our dynamic sections into the first bfd, which is the
4510 linker created stub bfd. This ensures that the GOT header is at
4511 the start of the output TOC section. */
4512 htab = ppc_hash_table (info);
e7d1c40c
AM
4513 htab->elf.dynobj = params->stub_bfd;
4514 htab->params = params;
bfeb4a28 4515
bfeb4a28 4516 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4517}
4518
721956f4
AM
4519/* Build a name for an entry in the stub hash table. */
4520
4521static char *
4ce794b7
AM
4522ppc_stub_name (const asection *input_section,
4523 const asection *sym_sec,
4524 const struct ppc_link_hash_entry *h,
4525 const Elf_Internal_Rela *rel)
721956f4
AM
4526{
4527 char *stub_name;
bcaa2f82 4528 ssize_t len;
721956f4
AM
4529
4530 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4531 offsets from a sym as a branch target? In fact, we could
4532 probably assume the addend is always zero. */
4533 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4534
4535 if (h)
4536 {
4537 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4538 stub_name = bfd_malloc (len);
46de2a7c
AM
4539 if (stub_name == NULL)
4540 return stub_name;
4541
bcaa2f82
AM
4542 len = sprintf (stub_name, "%08x.%s+%x",
4543 input_section->id & 0xffffffff,
4544 h->elf.root.root.string,
4545 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4546 }
4547 else
4548 {
ad8e1ba5 4549 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4550 stub_name = bfd_malloc (len);
46de2a7c
AM
4551 if (stub_name == NULL)
4552 return stub_name;
4553
bcaa2f82
AM
4554 len = sprintf (stub_name, "%08x.%x:%x+%x",
4555 input_section->id & 0xffffffff,
4556 sym_sec->id & 0xffffffff,
4557 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4558 (int) rel->r_addend & 0xffffffff);
721956f4 4559 }
bcaa2f82 4560 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4561 stub_name[len - 2] = 0;
721956f4
AM
4562 return stub_name;
4563}
4564
4565/* Look up an entry in the stub hash. Stub entries are cached because
4566 creating the stub name takes a bit of time. */
4567
4568static struct ppc_stub_hash_entry *
4ce794b7
AM
4569ppc_get_stub_entry (const asection *input_section,
4570 const asection *sym_sec,
039b3fef 4571 struct ppc_link_hash_entry *h,
4ce794b7
AM
4572 const Elf_Internal_Rela *rel,
4573 struct ppc_link_hash_table *htab)
721956f4
AM
4574{
4575 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4576 struct map_stub *group;
721956f4
AM
4577
4578 /* If this input section is part of a group of sections sharing one
4579 stub section, then use the id of the first section in the group.
4580 Stub names need to include a section id, as there may well be
4581 more than one stub used to reach say, printf, and we need to
4582 distinguish between them. */
6f20ed8a 4583 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4584 if (group == NULL)
4585 return NULL;
721956f4 4586
b3fac117
AM
4587 if (h != NULL && h->u.stub_cache != NULL
4588 && h->u.stub_cache->h == h
6f20ed8a 4589 && h->u.stub_cache->group == group)
721956f4 4590 {
b3fac117 4591 stub_entry = h->u.stub_cache;
721956f4
AM
4592 }
4593 else
4594 {
4595 char *stub_name;
4596
6f20ed8a 4597 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4598 if (stub_name == NULL)
4599 return NULL;
4600
4601 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4602 stub_name, FALSE, FALSE);
721956f4 4603 if (h != NULL)
b3fac117 4604 h->u.stub_cache = stub_entry;
721956f4
AM
4605
4606 free (stub_name);
4607 }
4608
4609 return stub_entry;
4610}
4611
4612/* Add a new stub entry to the stub hash. Not all fields of the new
4613 stub entry are initialised. */
4614
4615static struct ppc_stub_hash_entry *
4ce794b7
AM
4616ppc_add_stub (const char *stub_name,
4617 asection *section,
25f53a85 4618 struct bfd_link_info *info)
721956f4 4619{
25f53a85 4620 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4621 struct map_stub *group;
721956f4
AM
4622 asection *link_sec;
4623 asection *stub_sec;
4624 struct ppc_stub_hash_entry *stub_entry;
4625
6f20ed8a
AM
4626 group = htab->sec_info[section->id].u.group;
4627 link_sec = group->link_sec;
4628 stub_sec = group->stub_sec;
721956f4
AM
4629 if (stub_sec == NULL)
4630 {
6f20ed8a
AM
4631 size_t namelen;
4632 bfd_size_type len;
4633 char *s_name;
721956f4 4634
6f20ed8a
AM
4635 namelen = strlen (link_sec->name);
4636 len = namelen + sizeof (STUB_SUFFIX);
4637 s_name = bfd_alloc (htab->params->stub_bfd, len);
4638 if (s_name == NULL)
4639 return NULL;
721956f4 4640
6f20ed8a
AM
4641 memcpy (s_name, link_sec->name, namelen);
4642 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4643 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4644 if (stub_sec == NULL)
4645 return NULL;
4646 group->stub_sec = stub_sec;
721956f4
AM
4647 }
4648
4649 /* Enter this entry into the linker stub hash table. */
4650 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4651 TRUE, FALSE);
721956f4
AM
4652 if (stub_entry == NULL)
4653 {
695344c0 4654 /* xgettext:c-format */
8de848d8 4655 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4656 section->owner, stub_name);
721956f4
AM
4657 return NULL;
4658 }
4659
6f20ed8a 4660 stub_entry->group = group;
721956f4 4661 stub_entry->stub_offset = 0;
721956f4
AM
4662 return stub_entry;
4663}
4664
e717da7e
AM
4665/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4666 not already done. */
65f38f15 4667
b34976b6 4668static bfd_boolean
e717da7e 4669create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4670{
e717da7e
AM
4671 asection *got, *relgot;
4672 flagword flags;
4673 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4674
0c8d6e5c 4675 if (!is_ppc64_elf (abfd))
0ffa91dd 4676 return FALSE;
4dfe6ac6
NC
4677 if (htab == NULL)
4678 return FALSE;
0ffa91dd 4679
33e44f2e
AM
4680 if (!htab->elf.sgot
4681 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4682 return FALSE;
e717da7e
AM
4683
4684 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4685 | SEC_LINKER_CREATED);
4686
c456f082 4687 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4688 if (!got
e717da7e
AM
4689 || !bfd_set_section_alignment (abfd, got, 3))
4690 return FALSE;
65f38f15 4691
c456f082
AM
4692 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4693 flags | SEC_READONLY);
e717da7e 4694 if (!relgot
e717da7e 4695 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4696 return FALSE;
e717da7e
AM
4697
4698 ppc64_elf_tdata (abfd)->got = got;
4699 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4700 return TRUE;
65f38f15 4701}
5bd4f169 4702
b31867b6
AM
4703/* Follow indirect and warning symbol links. */
4704
4705static inline struct bfd_link_hash_entry *
4706follow_link (struct bfd_link_hash_entry *h)
4707{
4708 while (h->type == bfd_link_hash_indirect
4709 || h->type == bfd_link_hash_warning)
4710 h = h->u.i.link;
4711 return h;
4712}
4713
4714static inline struct elf_link_hash_entry *
4715elf_follow_link (struct elf_link_hash_entry *h)
4716{
4717 return (struct elf_link_hash_entry *) follow_link (&h->root);
4718}
4719
4720static inline struct ppc_link_hash_entry *
4721ppc_follow_link (struct ppc_link_hash_entry *h)
4722{
4723 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4724}
4725
40d16e0b
AM
4726/* Merge PLT info on FROM with that on TO. */
4727
4728static void
4729move_plt_plist (struct ppc_link_hash_entry *from,
4730 struct ppc_link_hash_entry *to)
4731{
4732 if (from->elf.plt.plist != NULL)
4733 {
4734 if (to->elf.plt.plist != NULL)
4735 {
4736 struct plt_entry **entp;
4737 struct plt_entry *ent;
4738
4739 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4740 {
4741 struct plt_entry *dent;
4742
4743 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4744 if (dent->addend == ent->addend)
4745 {
4746 dent->plt.refcount += ent->plt.refcount;
4747 *entp = ent->next;
4748 break;
4749 }
4750 if (dent == NULL)
4751 entp = &ent->next;
4752 }
4753 *entp = to->elf.plt.plist;
4754 }
4755
4756 to->elf.plt.plist = from->elf.plt.plist;
4757 from->elf.plt.plist = NULL;
4758 }
4759}
4760
65f38f15
AM
4761/* Copy the extra info we tack onto an elf_link_hash_entry. */
4762
4763static void
fcfa13d2
AM
4764ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4765 struct elf_link_hash_entry *dir,
4766 struct elf_link_hash_entry *ind)
65f38f15
AM
4767{
4768 struct ppc_link_hash_entry *edir, *eind;
4769
4770 edir = (struct ppc_link_hash_entry *) dir;
4771 eind = (struct ppc_link_hash_entry *) ind;
4772
c79d6685
AM
4773 edir->is_func |= eind->is_func;
4774 edir->is_func_descriptor |= eind->is_func_descriptor;
4775 edir->tls_mask |= eind->tls_mask;
4776 if (eind->oh != NULL)
4777 edir->oh = ppc_follow_link (eind->oh);
4778
474436e6 4779 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4780 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4781 edir->elf.ref_regular |= eind->elf.ref_regular;
4782 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4783 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4784 edir->elf.needs_plt |= eind->elf.needs_plt;
4785 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4786
d311bc8b
AM
4787 /* If we were called to copy over info for a weak sym, don't copy
4788 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4789 in order to simplify readonly_dynrelocs and save a field in the
4790 symbol hash entry, but that means dyn_relocs can't be used in any
4791 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4792 are then tested. */
d311bc8b 4793 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4794 return;
d311bc8b 4795
411e1bfb 4796 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4797 if (eind->dyn_relocs != NULL)
65f38f15 4798 {
bbd7ec4a
AM
4799 if (edir->dyn_relocs != NULL)
4800 {
6061a67d
AM
4801 struct elf_dyn_relocs **pp;
4802 struct elf_dyn_relocs *p;
bbd7ec4a 4803
fcfa13d2 4804 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4805 list. Merge any entries against the same section. */
4806 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4807 {
6061a67d 4808 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4809
4810 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4811 if (q->sec == p->sec)
4812 {
4813 q->pc_count += p->pc_count;
4814 q->count += p->count;
4815 *pp = p->next;
4816 break;
4817 }
4818 if (q == NULL)
4819 pp = &p->next;
4820 }
4821 *pp = edir->dyn_relocs;
4822 }
4823
65f38f15
AM
4824 edir->dyn_relocs = eind->dyn_relocs;
4825 eind->dyn_relocs = NULL;
4826 }
65f38f15 4827
81848ca0
AM
4828 /* Copy over got entries that we may have already seen to the
4829 symbol which just became indirect. */
411e1bfb
AM
4830 if (eind->elf.got.glist != NULL)
4831 {
4832 if (edir->elf.got.glist != NULL)
4833 {
4834 struct got_entry **entp;
4835 struct got_entry *ent;
4836
4837 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4838 {
4839 struct got_entry *dent;
4840
4841 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4842 if (dent->addend == ent->addend
e717da7e 4843 && dent->owner == ent->owner
411e1bfb
AM
4844 && dent->tls_type == ent->tls_type)
4845 {
4846 dent->got.refcount += ent->got.refcount;
4847 *entp = ent->next;
4848 break;
4849 }
4850 if (dent == NULL)
4851 entp = &ent->next;
4852 }
4853 *entp = edir->elf.got.glist;
4854 }
4855
4856 edir->elf.got.glist = eind->elf.got.glist;
4857 eind->elf.got.glist = NULL;
4858 }
4859
4860 /* And plt entries. */
40d16e0b 4861 move_plt_plist (eind, edir);
411e1bfb 4862
fcfa13d2 4863 if (eind->elf.dynindx != -1)
411e1bfb 4864 {
fcfa13d2
AM
4865 if (edir->elf.dynindx != -1)
4866 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4867 edir->elf.dynstr_index);
411e1bfb
AM
4868 edir->elf.dynindx = eind->elf.dynindx;
4869 edir->elf.dynstr_index = eind->elf.dynstr_index;
4870 eind->elf.dynindx = -1;
4871 eind->elf.dynstr_index = 0;
4872 }
411e1bfb
AM
4873}
4874
8387904d
AM
4875/* Find the function descriptor hash entry from the given function code
4876 hash entry FH. Link the entries via their OH fields. */
4877
4878static struct ppc_link_hash_entry *
b31867b6 4879lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4880{
4881 struct ppc_link_hash_entry *fdh = fh->oh;
4882
4883 if (fdh == NULL)
4884 {
4885 const char *fd_name = fh->elf.root.root.string + 1;
4886
4887 fdh = (struct ppc_link_hash_entry *)
4888 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4889 if (fdh == NULL)
4890 return fdh;
4891
4892 fdh->is_func_descriptor = 1;
4893 fdh->oh = fh;
4894 fh->is_func = 1;
4895 fh->oh = fdh;
8387904d
AM
4896 }
4897
8c5b4e52
AM
4898 fdh = ppc_follow_link (fdh);
4899 fdh->is_func_descriptor = 1;
4900 fdh->oh = fh;
4901 return fdh;
8387904d
AM
4902}
4903
8c5b4e52 4904/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4905
4906static struct ppc_link_hash_entry *
4907make_fdh (struct bfd_link_info *info,
908b32fc 4908 struct ppc_link_hash_entry *fh)
bb700d78 4909{
8c5b4e52
AM
4910 bfd *abfd = fh->elf.root.u.undef.abfd;
4911 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4912 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4913 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4914 ? BSF_WEAK
4915 : BSF_GLOBAL);
4916
4917 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4918 fh->elf.root.root.string + 1,
4919 flags, bfd_und_section_ptr, 0,
4920 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4921 return NULL;
4922
4923 fdh = (struct ppc_link_hash_entry *) bh;
4924 fdh->elf.non_elf = 0;
908b32fc
AM
4925 fdh->fake = 1;
4926 fdh->is_func_descriptor = 1;
4927 fdh->oh = fh;
4928 fh->is_func = 1;
4929 fh->oh = fdh;
bb700d78
AM
4930 return fdh;
4931}
4932
8387904d
AM
4933/* Fix function descriptor symbols defined in .opd sections to be
4934 function type. */
555cd476
AM
4935
4936static bfd_boolean
c16153ae 4937ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4938 struct bfd_link_info *info,
555cd476 4939 Elf_Internal_Sym *isym,
6911b7dc 4940 const char **name,
555cd476
AM
4941 flagword *flags ATTRIBUTE_UNUSED,
4942 asection **sec,
b53dfeb2 4943 bfd_vma *value)
555cd476 4944{
a43942db 4945 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4946 && (ibfd->flags & DYNAMIC) == 0
4947 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4948 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4949
b53dfeb2 4950 if (*sec != NULL
f1885d1e 4951 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4952 {
4953 asection *code_sec;
4954
4955 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4956 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4957 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4958
4959 /* If the symbol is a function defined in .opd, and the function
4960 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4961 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4962 && (*sec)->reloc_count != 0
4963 && opd_entry_value (*sec, *value, &code_sec, NULL,
4964 FALSE) != (bfd_vma) -1
4965 && discarded_section (code_sec))
4966 {
4967 *sec = bfd_und_section_ptr;
4968 isym->st_shndx = SHN_UNDEF;
4969 }
4970 }
dbd1e97e
AM
4971 else if (*sec != NULL
4972 && strcmp ((*sec)->name, ".toc") == 0
4973 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4974 {
4975 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4976 if (htab != NULL)
4977 htab->params->object_in_toc = 1;
4978 }
433817dd 4979
6911b7dc
AM
4980 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4981 {
4982 if (abiversion (ibfd) == 0)
4983 set_abiversion (ibfd, 2);
4984 else if (abiversion (ibfd) == 1)
4985 {
4986 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4987 " for ABI version 1\n"), name);
4988 bfd_set_error (bfd_error_bad_value);
4989 return FALSE;
4990 }
4991 }
4992
555cd476
AM
4993 return TRUE;
4994}
4995
6911b7dc
AM
4996/* Merge non-visibility st_other attributes: local entry point. */
4997
4998static void
4999ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5000 const Elf_Internal_Sym *isym,
5001 bfd_boolean definition,
5002 bfd_boolean dynamic)
5003{
f378ab09 5004 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5005 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5006 | ELF_ST_VISIBILITY (h->other));
5007}
5008
8c5b4e52
AM
5009/* Hook called on merging a symbol. We use this to clear "fake" since
5010 we now have a real symbol. */
5011
5012static bfd_boolean
5013ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5014 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5015 asection **psec ATTRIBUTE_UNUSED,
5016 bfd_boolean newdef ATTRIBUTE_UNUSED,
5017 bfd_boolean olddef ATTRIBUTE_UNUSED,
5018 bfd *oldbfd ATTRIBUTE_UNUSED,
5019 const asection *oldsec ATTRIBUTE_UNUSED)
5020{
5021 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5022 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5023 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5024 return TRUE;
5025}
5026
8387904d 5027/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5028 inclusion of a new ABI object archive that defines "bar".
5029 NAME is a symbol defined in an archive. Return a symbol in the hash
5030 table that might be satisfied by the archive symbols. */
8387904d
AM
5031
5032static struct elf_link_hash_entry *
5033ppc64_elf_archive_symbol_lookup (bfd *abfd,
5034 struct bfd_link_info *info,
5035 const char *name)
5036{
5037 struct elf_link_hash_entry *h;
5038 char *dot_name;
5039 size_t len;
5040
5041 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5042 if (h != NULL
5043 /* Don't return this sym if it is a fake function descriptor
5044 created by add_symbol_adjust. */
8c5b4e52 5045 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5046 return h;
5047
5048 if (name[0] == '.')
5049 return h;
5050
5051 len = strlen (name);
5052 dot_name = bfd_alloc (abfd, len + 2);
5053 if (dot_name == NULL)
5054 return (struct elf_link_hash_entry *) 0 - 1;
5055 dot_name[0] = '.';
5056 memcpy (dot_name + 1, name, len + 1);
5057 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5058 bfd_release (abfd, dot_name);
5059 return h;
5060}
5061
5062/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5063 new ABI object defines "bar". Well, at least, undefined dot symbols
5064 are made weak. This stops later archive searches from including an
5065 object if we already have a function descriptor definition. It also
35b0ce59
AM
5066 prevents the linker complaining about undefined symbols.
5067 We also check and correct mismatched symbol visibility here. The
5068 most restrictive visibility of the function descriptor and the
5069 function entry symbol is used. */
8387904d
AM
5070
5071static bfd_boolean
b3fac117 5072add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5073{
8387904d
AM
5074 struct ppc_link_hash_table *htab;
5075 struct ppc_link_hash_entry *fdh;
5076
b3fac117
AM
5077 if (eh->elf.root.type == bfd_link_hash_warning)
5078 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5079
8400d40d
AM
5080 if (eh->elf.root.type == bfd_link_hash_indirect)
5081 return TRUE;
5082
b3fac117
AM
5083 if (eh->elf.root.root.string[0] != '.')
5084 abort ();
8387904d 5085
b3fac117 5086 htab = ppc_hash_table (info);
4dfe6ac6
NC
5087 if (htab == NULL)
5088 return FALSE;
5089
b31867b6 5090 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5091 if (fdh == NULL
5092 && !bfd_link_relocatable (info)
5093 && (eh->elf.root.type == bfd_link_hash_undefined
5094 || eh->elf.root.type == bfd_link_hash_undefweak)
5095 && eh->elf.ref_regular)
5096 {
5097 /* Make an undefined function descriptor sym, in order to
5098 pull in an --as-needed shared lib. Archives are handled
5099 elsewhere. */
5100 fdh = make_fdh (info, eh);
5101 if (fdh == NULL)
5102 return FALSE;
bb700d78 5103 }
8c5b4e52
AM
5104
5105 if (fdh != NULL)
8387904d 5106 {
35b0ce59
AM
5107 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5108 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5109
5110 /* Make both descriptor and entry symbol have the most
5111 constraining visibility of either symbol. */
35b0ce59
AM
5112 if (entry_vis < descr_vis)
5113 fdh->elf.other += entry_vis - descr_vis;
5114 else if (entry_vis > descr_vis)
5115 eh->elf.other += descr_vis - entry_vis;
5116
8c5b4e52
AM
5117 /* Propagate reference flags from entry symbol to function
5118 descriptor symbol. */
bc4e12de 5119 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5120 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5121 fdh->elf.ref_regular |= eh->elf.ref_regular;
5122 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5123
5124 if (!fdh->elf.forced_local
5125 && fdh->elf.dynindx == -1
5126 && fdh->elf.versioned != versioned_hidden
5127 && (bfd_link_dll (info)
5128 || fdh->elf.def_dynamic
5129 || fdh->elf.ref_dynamic)
5130 && (eh->elf.ref_regular
5131 || eh->elf.def_regular))
5132 {
5133 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5134 return FALSE;
35b0ce59 5135 }
8387904d 5136 }
99877b66 5137
8387904d
AM
5138 return TRUE;
5139}
5140
f6c7c3e8
AM
5141/* Set up opd section info and abiversion for IBFD, and process list
5142 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5143
8387904d 5144static bfd_boolean
f6c7c3e8 5145ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5146{
99877b66 5147 struct ppc_link_hash_table *htab;
b3fac117 5148 struct ppc_link_hash_entry **p, *eh;
459609d6 5149 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5150
459609d6 5151 if (opd != NULL && opd->size != 0)
b3fac117 5152 {
b9399fcf
AM
5153 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5154 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5155
459609d6
AM
5156 if (abiversion (ibfd) == 0)
5157 set_abiversion (ibfd, 1);
8a2058b5 5158 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5159 {
695344c0 5160 /* xgettext:c-format */
459609d6
AM
5161 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5162 " version %d\n"),
5163 ibfd, abiversion (ibfd));
5164 bfd_set_error (bfd_error_bad_value);
5165 return FALSE;
f6c7c3e8 5166 }
b9399fcf 5167 }
f6c7c3e8 5168
b9399fcf
AM
5169 if (is_ppc64_elf (info->output_bfd))
5170 {
5171 /* For input files without an explicit abiversion in e_flags
5172 we should have flagged any with symbol st_other bits set
5173 as ELFv1 and above flagged those with .opd as ELFv2.
5174 Set the output abiversion if not yet set, and for any input
5175 still ambiguous, take its abiversion from the output.
5176 Differences in ABI are reported later. */
5177 if (abiversion (info->output_bfd) == 0)
5178 set_abiversion (info->output_bfd, abiversion (ibfd));
5179 else if (abiversion (ibfd) == 0)
5180 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5181 }
5182
459609d6
AM
5183 htab = ppc_hash_table (info);
5184 if (htab == NULL)
b9399fcf 5185 return TRUE;
459609d6 5186
b9399fcf
AM
5187 if (opd != NULL && opd->size != 0
5188 && (ibfd->flags & DYNAMIC) == 0
5189 && (opd->flags & SEC_RELOC) != 0
5190 && opd->reloc_count != 0
5191 && !bfd_is_abs_section (opd->output_section)
5192 && info->gc_sections)
5193 {
5194 /* Garbage collection needs some extra help with .opd sections.
5195 We don't want to necessarily keep everything referenced by
5196 relocs in .opd, as that would keep all functions. Instead,
5197 if we reference an .opd symbol (a function descriptor), we
5198 want to keep the function code symbol's section. This is
5199 easy for global symbols, but for local syms we need to keep
5200 information about the associated function section. */
5201 bfd_size_type amt;
5202 asection **opd_sym_map;
5203 Elf_Internal_Shdr *symtab_hdr;
5204 Elf_Internal_Rela *relocs, *rel_end, *rel;
5205
5206 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5207 opd_sym_map = bfd_zalloc (ibfd, amt);
5208 if (opd_sym_map == NULL)
5209 return FALSE;
5210 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5211 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5212 info->keep_memory);
5213 if (relocs == NULL)
5214 return FALSE;
5215 symtab_hdr = &elf_symtab_hdr (ibfd);
5216 rel_end = relocs + opd->reloc_count - 1;
5217 for (rel = relocs; rel < rel_end; rel++)
5218 {
5219 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5220 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5221
5222 if (r_type == R_PPC64_ADDR64
5223 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5224 && r_symndx < symtab_hdr->sh_info)
5225 {
5226 Elf_Internal_Sym *isym;
5227 asection *s;
5228
5229 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5230 if (isym == NULL)
5231 {
5232 if (elf_section_data (opd)->relocs != relocs)
5233 free (relocs);
5234 return FALSE;
5235 }
5236
5237 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5238 if (s != NULL && s != opd)
5239 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5240 }
5241 }
5242 if (elf_section_data (opd)->relocs != relocs)
5243 free (relocs);
5244 }
459609d6
AM
5245
5246 p = &htab->dot_syms;
5247 while ((eh = *p) != NULL)
5248 {
5249 *p = NULL;
5250 if (&eh->elf == htab->elf.hgot)
5251 ;
5252 else if (htab->elf.hgot == NULL
5253 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5254 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5255 else if (abiversion (ibfd) <= 1)
5256 {
5257 htab->need_func_desc_adj = 1;
5258 if (!add_symbol_adjust (eh, info))
5259 return FALSE;
5260 }
459609d6
AM
5261 p = &eh->u.next_dot_sym;
5262 }
b3fac117 5263 return TRUE;
8387904d
AM
5264}
5265
97fed1c9
JJ
5266/* Undo hash table changes when an --as-needed input file is determined
5267 not to be needed. */
5268
5269static bfd_boolean
e5034e59
AM
5270ppc64_elf_notice_as_needed (bfd *ibfd,
5271 struct bfd_link_info *info,
5272 enum notice_asneeded_action act)
97fed1c9 5273{
e5034e59
AM
5274 if (act == notice_not_needed)
5275 {
5276 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5277
e5034e59
AM
5278 if (htab == NULL)
5279 return FALSE;
4dfe6ac6 5280
e5034e59
AM
5281 htab->dot_syms = NULL;
5282 }
5283 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5284}
5285
aa374f67
AM
5286/* If --just-symbols against a final linked binary, then assume we need
5287 toc adjusting stubs when calling functions defined there. */
5288
5289static void
5290ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5291{
5292 if ((sec->flags & SEC_CODE) != 0
5293 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5294 && is_ppc64_elf (sec->owner))
5295 {
2c3f079f
AM
5296 if (abiversion (sec->owner) >= 2
5297 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5298 sec->has_toc_reloc = 1;
5299 }
5300 _bfd_elf_link_just_syms (sec, info);
5301}
5302
e054468f 5303static struct plt_entry **
4ce794b7
AM
5304update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5305 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5306{
5307 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5308 struct plt_entry **local_plt;
f961d9dd 5309 unsigned char *local_got_tls_masks;
411e1bfb
AM
5310
5311 if (local_got_ents == NULL)
5312 {
5313 bfd_size_type size = symtab_hdr->sh_info;
5314
e054468f
AM
5315 size *= (sizeof (*local_got_ents)
5316 + sizeof (*local_plt)
5317 + sizeof (*local_got_tls_masks));
4ce794b7 5318 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5319 if (local_got_ents == NULL)
e054468f 5320 return NULL;
411e1bfb
AM
5321 elf_local_got_ents (abfd) = local_got_ents;
5322 }
5323
e054468f 5324 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5325 {
5326 struct got_entry *ent;
5327
5328 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5329 if (ent->addend == r_addend
5330 && ent->owner == abfd
5331 && ent->tls_type == tls_type)
411e1bfb
AM
5332 break;
5333 if (ent == NULL)
5334 {
5335 bfd_size_type amt = sizeof (*ent);
4ce794b7 5336 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5337 if (ent == NULL)
5338 return FALSE;
5339 ent->next = local_got_ents[r_symndx];
5340 ent->addend = r_addend;
e717da7e 5341 ent->owner = abfd;
411e1bfb 5342 ent->tls_type = tls_type;
927be08e 5343 ent->is_indirect = FALSE;
411e1bfb
AM
5344 ent->got.refcount = 0;
5345 local_got_ents[r_symndx] = ent;
5346 }
5347 ent->got.refcount += 1;
5348 }
5349
e054468f 5350 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5351 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5352 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5353
5354 return local_plt + r_symndx;
65f38f15
AM
5355}
5356
411e1bfb 5357static bfd_boolean
e054468f 5358update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5359{
411e1bfb 5360 struct plt_entry *ent;
1e2f5b6e 5361
e054468f 5362 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5363 if (ent->addend == addend)
5364 break;
5365 if (ent == NULL)
1e2f5b6e 5366 {
411e1bfb 5367 bfd_size_type amt = sizeof (*ent);
4ce794b7 5368 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5369 if (ent == NULL)
5370 return FALSE;
e054468f 5371 ent->next = *plist;
411e1bfb
AM
5372 ent->addend = addend;
5373 ent->plt.refcount = 0;
e054468f 5374 *plist = ent;
1e2f5b6e 5375 }
411e1bfb 5376 ent->plt.refcount += 1;
b34976b6 5377 return TRUE;
1e2f5b6e
AM
5378}
5379
e054468f
AM
5380static bfd_boolean
5381is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5382{
5383 return (r_type == R_PPC64_REL24
5384 || r_type == R_PPC64_REL14
5385 || r_type == R_PPC64_REL14_BRTAKEN
5386 || r_type == R_PPC64_REL14_BRNTAKEN
5387 || r_type == R_PPC64_ADDR24
5388 || r_type == R_PPC64_ADDR14
5389 || r_type == R_PPC64_ADDR14_BRTAKEN
5390 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5391}
5392
5bd4f169 5393/* Look through the relocs for a section during the first phase, and
65f38f15 5394 calculate needed space in the global offset table, procedure
5d1634d7 5395 linkage table, and dynamic reloc sections. */
5bd4f169 5396
b34976b6 5397static bfd_boolean
4ce794b7
AM
5398ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5399 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5400{
65f38f15 5401 struct ppc_link_hash_table *htab;
5bd4f169 5402 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5403 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5404 const Elf_Internal_Rela *rel;
5405 const Elf_Internal_Rela *rel_end;
5bd4f169 5406 asection *sreloc;
3a71aa26 5407 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5408 bfd_boolean is_opd;
5bd4f169 5409
0e1862bb 5410 if (bfd_link_relocatable (info))
b34976b6 5411 return TRUE;
5bd4f169 5412
680a3378
AM
5413 /* Don't do anything special with non-loaded, non-alloced sections.
5414 In particular, any relocs in such sections should not affect GOT
5415 and PLT reference counting (ie. we don't allow them to create GOT
5416 or PLT entries), there's no possibility or desire to optimize TLS
5417 relocs, and there's not much point in propagating relocs to shared
5418 libs that the dynamic linker won't relocate. */
5419 if ((sec->flags & SEC_ALLOC) == 0)
5420 return TRUE;
5421
0c8d6e5c 5422 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5423
65f38f15 5424 htab = ppc_hash_table (info);
4dfe6ac6
NC
5425 if (htab == NULL)
5426 return FALSE;
5427
3a71aa26
AM
5428 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5429 FALSE, FALSE, TRUE);
5430 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5431 FALSE, FALSE, TRUE);
0ffa91dd 5432 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5433 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5434 sreloc = NULL;
b9399fcf 5435 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5436 rel_end = relocs + sec->reloc_count;
5437 for (rel = relocs; rel < rel_end; rel++)
5438 {
5439 unsigned long r_symndx;
5440 struct elf_link_hash_entry *h;
04c9666a 5441 enum elf_ppc64_reloc_type r_type;
727fc41e 5442 int tls_type;
7c8fe5c4 5443 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5444 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5445
5446 r_symndx = ELF64_R_SYM (rel->r_info);
5447 if (r_symndx < symtab_hdr->sh_info)
5448 h = NULL;
5449 else
973a3492
L
5450 {
5451 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5452 h = elf_follow_link (h);
1c865ab2
AM
5453
5454 if (h == htab->elf.hgot)
5455 sec->has_toc_reloc = 1;
973a3492 5456 }
5bd4f169 5457
727fc41e 5458 tls_type = 0;
e054468f 5459 ifunc = NULL;
25f23106
AM
5460 if (h != NULL)
5461 {
5462 if (h->type == STT_GNU_IFUNC)
5463 {
5464 h->needs_plt = 1;
5465 ifunc = &h->plt.plist;
5466 }
5467 }
5468 else
5469 {
5470 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5471 abfd, r_symndx);
5472 if (isym == NULL)
5473 return FALSE;
5474
5475 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5476 {
5477 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5478 rel->r_addend, PLT_IFUNC);
5479 if (ifunc == NULL)
5480 return FALSE;
5481 }
5482 }
727fc41e 5483
cbf95972 5484 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5485 switch (r_type)
5bd4f169 5486 {
727fc41e
AM
5487 case R_PPC64_TLSGD:
5488 case R_PPC64_TLSLD:
5489 /* These special tls relocs tie a call to __tls_get_addr with
5490 its parameter symbol. */
5491 break;
5492
411e1bfb
AM
5493 case R_PPC64_GOT_TLSLD16:
5494 case R_PPC64_GOT_TLSLD16_LO:
5495 case R_PPC64_GOT_TLSLD16_HI:
5496 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5497 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5498 goto dogottls;
5499
5500 case R_PPC64_GOT_TLSGD16:
5501 case R_PPC64_GOT_TLSGD16_LO:
5502 case R_PPC64_GOT_TLSGD16_HI:
5503 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5504 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5505 goto dogottls;
5506
5507 case R_PPC64_GOT_TPREL16_DS:
5508 case R_PPC64_GOT_TPREL16_LO_DS:
5509 case R_PPC64_GOT_TPREL16_HI:
5510 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5511 if (bfd_link_dll (info))
411e1bfb
AM
5512 info->flags |= DF_STATIC_TLS;
5513 tls_type = TLS_TLS | TLS_TPREL;
5514 goto dogottls;
5515
5516 case R_PPC64_GOT_DTPREL16_DS:
5517 case R_PPC64_GOT_DTPREL16_LO_DS:
5518 case R_PPC64_GOT_DTPREL16_HI:
5519 case R_PPC64_GOT_DTPREL16_HA:
5520 tls_type = TLS_TLS | TLS_DTPREL;
5521 dogottls:
5522 sec->has_tls_reloc = 1;
1a0670f3 5523 /* Fall through */
411e1bfb 5524
5bd4f169 5525 case R_PPC64_GOT16:
5bd4f169 5526 case R_PPC64_GOT16_DS:
65f38f15
AM
5527 case R_PPC64_GOT16_HA:
5528 case R_PPC64_GOT16_HI:
5529 case R_PPC64_GOT16_LO:
5bd4f169 5530 case R_PPC64_GOT16_LO_DS:
65f38f15 5531 /* This symbol requires a global offset table entry. */
4c52953f 5532 sec->has_toc_reloc = 1;
33c0ec9d
AM
5533 if (r_type == R_PPC64_GOT_TLSLD16
5534 || r_type == R_PPC64_GOT_TLSGD16
5535 || r_type == R_PPC64_GOT_TPREL16_DS
5536 || r_type == R_PPC64_GOT_DTPREL16_DS
5537 || r_type == R_PPC64_GOT16
5538 || r_type == R_PPC64_GOT16_DS)
5539 {
5540 htab->do_multi_toc = 1;
d77c8a4b 5541 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5542 }
5543
e717da7e
AM
5544 if (ppc64_elf_tdata (abfd)->got == NULL
5545 && !create_got_section (abfd, info))
b34976b6 5546 return FALSE;
5bd4f169
AM
5547
5548 if (h != NULL)
5549 {
411e1bfb
AM
5550 struct ppc_link_hash_entry *eh;
5551 struct got_entry *ent;
65f38f15 5552
411e1bfb
AM
5553 eh = (struct ppc_link_hash_entry *) h;
5554 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5555 if (ent->addend == rel->r_addend
e717da7e 5556 && ent->owner == abfd
411e1bfb
AM
5557 && ent->tls_type == tls_type)
5558 break;
5559 if (ent == NULL)
5bd4f169 5560 {
411e1bfb 5561 bfd_size_type amt = sizeof (*ent);
4ce794b7 5562 ent = bfd_alloc (abfd, amt);
411e1bfb 5563 if (ent == NULL)
b34976b6 5564 return FALSE;
411e1bfb
AM
5565 ent->next = eh->elf.got.glist;
5566 ent->addend = rel->r_addend;
e717da7e 5567 ent->owner = abfd;
411e1bfb 5568 ent->tls_type = tls_type;
927be08e 5569 ent->is_indirect = FALSE;
411e1bfb
AM
5570 ent->got.refcount = 0;
5571 eh->elf.got.glist = ent;
5bd4f169 5572 }
411e1bfb 5573 ent->got.refcount += 1;
e7b938ca 5574 eh->tls_mask |= tls_type;
5bd4f169 5575 }
411e1bfb
AM
5576 else
5577 /* This is a global offset table entry for a local symbol. */
5578 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5579 rel->r_addend, tls_type))
5580 return FALSE;
a345bc8d
AM
5581
5582 /* We may also need a plt entry if the symbol turns out to be
5583 an ifunc. */
0e1862bb 5584 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5585 {
5586 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5587 return FALSE;
5588 }
5bd4f169
AM
5589 break;
5590
5bd4f169 5591 case R_PPC64_PLT16_HA:
65f38f15
AM
5592 case R_PPC64_PLT16_HI:
5593 case R_PPC64_PLT16_LO:
5594 case R_PPC64_PLT32:
5595 case R_PPC64_PLT64:
cbf95972
AM
5596 /* This symbol requires a procedure linkage table entry. */
5597 plt_list = ifunc;
5598 if (h != NULL)
e054468f 5599 {
e054468f
AM
5600 h->needs_plt = 1;
5601 if (h->root.root.string[0] == '.'
5602 && h->root.root.string[1] != '\0')
5603 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5604 plt_list = &h->plt.plist;
5605 }
5606 if (plt_list == NULL)
5607 {
5608 /* It does not make sense to have a procedure linkage
5609 table entry for a non-ifunc local symbol. */
5610 info->callbacks->einfo
695344c0 5611 /* xgettext:c-format */
174d0a74 5612 (_("%H: %s reloc against local symbol\n"),
cbf95972
AM
5613 abfd, sec, rel->r_offset,
5614 ppc64_elf_howto_table[r_type]->name);
5615 bfd_set_error (bfd_error_bad_value);
5616 return FALSE;
e054468f 5617 }
cbf95972
AM
5618 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5619 return FALSE;
5bd4f169
AM
5620 break;
5621
5622 /* The following relocations don't need to propagate the
5623 relocation if linking a shared object since they are
5624 section relative. */
5625 case R_PPC64_SECTOFF:
5626 case R_PPC64_SECTOFF_LO:
5627 case R_PPC64_SECTOFF_HI:
5628 case R_PPC64_SECTOFF_HA:
5629 case R_PPC64_SECTOFF_DS:
5630 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5631 case R_PPC64_DTPREL16:
5632 case R_PPC64_DTPREL16_LO:
5633 case R_PPC64_DTPREL16_HI:
5634 case R_PPC64_DTPREL16_HA:
5635 case R_PPC64_DTPREL16_DS:
5636 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5637 case R_PPC64_DTPREL16_HIGH:
5638 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5639 case R_PPC64_DTPREL16_HIGHER:
5640 case R_PPC64_DTPREL16_HIGHERA:
5641 case R_PPC64_DTPREL16_HIGHEST:
5642 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5643 break;
5644
ad8e1ba5 5645 /* Nor do these. */
25f23106
AM
5646 case R_PPC64_REL16:
5647 case R_PPC64_REL16_LO:
5648 case R_PPC64_REL16_HI:
5649 case R_PPC64_REL16_HA:
a680de9a 5650 case R_PPC64_REL16DX_HA:
25f23106
AM
5651 break;
5652
45965137
AM
5653 /* Not supported as a dynamic relocation. */
5654 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5655 if (bfd_link_pic (info))
45965137
AM
5656 {
5657 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5658 ppc_howto_init ();
695344c0 5659 /* xgettext:c-format */
174d0a74 5660 info->callbacks->einfo (_("%H: %s reloc unsupported "
45965137
AM
5661 "in shared libraries and PIEs.\n"),
5662 abfd, sec, rel->r_offset,
5663 ppc64_elf_howto_table[r_type]->name);
5664 bfd_set_error (bfd_error_bad_value);
5665 return FALSE;
5666 }
5667 break;
5668
ad8e1ba5 5669 case R_PPC64_TOC16:
33c0ec9d
AM
5670 case R_PPC64_TOC16_DS:
5671 htab->do_multi_toc = 1;
d77c8a4b 5672 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5673 /* Fall through. */
ad8e1ba5
AM
5674 case R_PPC64_TOC16_LO:
5675 case R_PPC64_TOC16_HI:
5676 case R_PPC64_TOC16_HA:
ad8e1ba5 5677 case R_PPC64_TOC16_LO_DS:
4c52953f 5678 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5679 break;
5680
006589cf
AM
5681 /* Marker reloc. */
5682 case R_PPC64_ENTRY:
5683 break;
5684
5bd4f169
AM
5685 /* This relocation describes the C++ object vtable hierarchy.
5686 Reconstruct it for later use during GC. */
5687 case R_PPC64_GNU_VTINHERIT:
c152c796 5688 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5689 return FALSE;
5bd4f169
AM
5690 break;
5691
5692 /* This relocation describes which C++ vtable entries are actually
5693 used. Record for later use during GC. */
5694 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5695 BFD_ASSERT (h != NULL);
5696 if (h != NULL
5697 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5698 return FALSE;
5bd4f169
AM
5699 break;
5700
721956f4
AM
5701 case R_PPC64_REL14:
5702 case R_PPC64_REL14_BRTAKEN:
5703 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5704 {
5705 asection *dest = NULL;
5706
5707 /* Heuristic: If jumping outside our section, chances are
5708 we are going to need a stub. */
5709 if (h != NULL)
5710 {
5711 /* If the sym is weak it may be overridden later, so
5712 don't assume we know where a weak sym lives. */
5713 if (h->root.type == bfd_link_hash_defined)
5714 dest = h->root.u.def.section;
5715 }
5716 else
87d72d41
AM
5717 {
5718 Elf_Internal_Sym *isym;
5719
5720 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5721 abfd, r_symndx);
5722 if (isym == NULL)
5723 return FALSE;
5724
5725 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5726 }
5727
220c76dd 5728 if (dest != sec)
7c8fe5c4 5729 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5730 }
721956f4
AM
5731 /* Fall through. */
5732
5d1634d7 5733 case R_PPC64_REL24:
cbf95972
AM
5734 plt_list = ifunc;
5735 if (h != NULL)
5d1634d7 5736 {
e054468f
AM
5737 h->needs_plt = 1;
5738 if (h->root.root.string[0] == '.'
5739 && h->root.root.string[1] != '\0')
5740 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5741
3a71aa26 5742 if (h == tga || h == dottga)
cbf95972
AM
5743 {
5744 sec->has_tls_reloc = 1;
5745 if (rel != relocs
5746 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5747 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5748 /* We have a new-style __tls_get_addr call with
5749 a marker reloc. */
5750 ;
5751 else
5752 /* Mark this section as having an old-style call. */
5753 sec->has_tls_get_addr_call = 1;
5754 }
5755 plt_list = &h->plt.plist;
411e1bfb 5756 }
cbf95972
AM
5757
5758 /* We may need a .plt entry if the function this reloc
5759 refers to is in a shared lib. */
5760 if (plt_list
5761 && !update_plt_info (abfd, plt_list, rel->r_addend))
5762 return FALSE;
411e1bfb
AM
5763 break;
5764
cbf95972
AM
5765 case R_PPC64_ADDR14:
5766 case R_PPC64_ADDR14_BRNTAKEN:
5767 case R_PPC64_ADDR14_BRTAKEN:
5768 case R_PPC64_ADDR24:
5769 goto dodyn;
5770
411e1bfb
AM
5771 case R_PPC64_TPREL64:
5772 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5773 if (bfd_link_dll (info))
411e1bfb
AM
5774 info->flags |= DF_STATIC_TLS;
5775 goto dotlstoc;
5776
5777 case R_PPC64_DTPMOD64:
5778 if (rel + 1 < rel_end
5779 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5780 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5781 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5782 else
951fd09b 5783 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5784 goto dotlstoc;
5785
5786 case R_PPC64_DTPREL64:
5787 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5788 if (rel != relocs
5789 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5790 && rel[-1].r_offset == rel->r_offset - 8)
5791 /* This is the second reloc of a dtpmod, dtprel pair.
5792 Don't mark with TLS_DTPREL. */
5793 goto dodyn;
5794
5795 dotlstoc:
5796 sec->has_tls_reloc = 1;
5797 if (h != NULL)
5798 {
5799 struct ppc_link_hash_entry *eh;
5800 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5801 eh->tls_mask |= tls_type;
411e1bfb
AM
5802 }
5803 else
5804 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5805 rel->r_addend, tls_type))
5806 return FALSE;
5807
7c8fe5c4
AM
5808 ppc64_sec = ppc64_elf_section_data (sec);
5809 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5810 {
3a71aa26
AM
5811 bfd_size_type amt;
5812
e7b938ca 5813 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5814 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5815 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5816 if (ppc64_sec->u.toc.symndx == NULL)
5817 return FALSE;
5818 amt = sec->size * sizeof (bfd_vma) / 8;
5819 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5820 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5821 return FALSE;
7c8fe5c4
AM
5822 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5823 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5824 }
5825 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5826 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5827 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5828
5829 /* Mark the second slot of a GD or LD entry.
5830 -1 to indicate GD and -2 to indicate LD. */
5831 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5832 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5833 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5834 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5835 goto dodyn;
5836
5837 case R_PPC64_TPREL16:
5838 case R_PPC64_TPREL16_LO:
5839 case R_PPC64_TPREL16_HI:
5840 case R_PPC64_TPREL16_HA:
5841 case R_PPC64_TPREL16_DS:
5842 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5843 case R_PPC64_TPREL16_HIGH:
5844 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5845 case R_PPC64_TPREL16_HIGHER:
5846 case R_PPC64_TPREL16_HIGHERA:
5847 case R_PPC64_TPREL16_HIGHEST:
5848 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
5849 if (bfd_link_dll (info))
5850 info->flags |= DF_STATIC_TLS;
5851 goto dodyn;
5d1634d7 5852
e86ce104 5853 case R_PPC64_ADDR64:
b9399fcf 5854 if (is_opd
1e2f5b6e 5855 && rel + 1 < rel_end
4ce794b7 5856 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5857 {
8387904d 5858 if (h != NULL)
8c5b4e52 5859 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5860 }
e86ce104
AM
5861 /* Fall through. */
5862
65f38f15
AM
5863 case R_PPC64_ADDR16:
5864 case R_PPC64_ADDR16_DS:
5865 case R_PPC64_ADDR16_HA:
5866 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5867 case R_PPC64_ADDR16_HIGH:
5868 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5869 case R_PPC64_ADDR16_HIGHER:
5870 case R_PPC64_ADDR16_HIGHERA:
5871 case R_PPC64_ADDR16_HIGHEST:
5872 case R_PPC64_ADDR16_HIGHESTA:
5873 case R_PPC64_ADDR16_LO:
5874 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5875 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5876 && rel->r_addend == 0)
5877 {
5878 /* We may need a .plt entry if this reloc refers to a
5879 function in a shared lib. */
5880 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5881 return FALSE;
5882 h->pointer_equality_needed = 1;
5883 }
5884 /* Fall through. */
5885
5886 case R_PPC64_REL30:
5887 case R_PPC64_REL32:
5888 case R_PPC64_REL64:
65f38f15 5889 case R_PPC64_ADDR32:
65f38f15
AM
5890 case R_PPC64_UADDR16:
5891 case R_PPC64_UADDR32:
5892 case R_PPC64_UADDR64:
5bd4f169 5893 case R_PPC64_TOC:
0e1862bb 5894 if (h != NULL && !bfd_link_pic (info))
81848ca0 5895 /* We may need a copy reloc. */
f5385ebf 5896 h->non_got_ref = 1;
81848ca0 5897
41bd81ab 5898 /* Don't propagate .opd relocs. */
b9399fcf 5899 if (NO_OPD_RELOCS && is_opd)
e86ce104 5900 break;
e86ce104 5901
65f38f15
AM
5902 /* If we are creating a shared library, and this is a reloc
5903 against a global symbol, or a non PC relative reloc
5904 against a local symbol, then we need to copy the reloc
5905 into the shared library. However, if we are linking with
5906 -Bsymbolic, we do not need to copy a reloc against a
5907 global symbol which is defined in an object we are
5908 including in the link (i.e., DEF_REGULAR is set). At
5909 this point we have not seen all the input files, so it is
5910 possible that DEF_REGULAR is not set now but will be set
5911 later (it is never cleared). In case of a weak definition,
5912 DEF_REGULAR may be cleared later by a strong definition in
5913 a shared library. We account for that possibility below by
f4656909 5914 storing information in the dyn_relocs field of the hash
65f38f15
AM
5915 table entry. A similar situation occurs when creating
5916 shared libraries and symbol visibility changes render the
5917 symbol local.
5918
5919 If on the other hand, we are creating an executable, we
5920 may need to keep relocations for symbols satisfied by a
5921 dynamic library if we manage to avoid copy relocs for the
5922 symbol. */
411e1bfb 5923 dodyn:
0e1862bb 5924 if ((bfd_link_pic (info)
1d483afe 5925 && (must_be_dyn_reloc (info, r_type)
65f38f15 5926 || (h != NULL
198f1157 5927 && (!SYMBOLIC_BIND (info, h)
65f38f15 5928 || h->root.type == bfd_link_hash_defweak
f5385ebf 5929 || !h->def_regular))))
f4656909 5930 || (ELIMINATE_COPY_RELOCS
0e1862bb 5931 && !bfd_link_pic (info)
65f38f15
AM
5932 && h != NULL
5933 && (h->root.type == bfd_link_hash_defweak
25f23106 5934 || !h->def_regular))
0e1862bb 5935 || (!bfd_link_pic (info)
25f23106 5936 && ifunc != NULL))
5bd4f169 5937 {
65f38f15
AM
5938 /* We must copy these reloc types into the output file.
5939 Create a reloc section in dynobj and make room for
5940 this reloc. */
5bd4f169
AM
5941 if (sreloc == NULL)
5942 {
83bac4b0
NC
5943 sreloc = _bfd_elf_make_dynamic_reloc_section
5944 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5945
5bd4f169 5946 if (sreloc == NULL)
83bac4b0 5947 return FALSE;
5bd4f169
AM
5948 }
5949
65f38f15
AM
5950 /* If this is a global symbol, we count the number of
5951 relocations we need for this symbol. */
5952 if (h != NULL)
5953 {
19e08130
AM
5954 struct elf_dyn_relocs *p;
5955 struct elf_dyn_relocs **head;
5956
ec338859 5957 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5958 p = *head;
5959 if (p == NULL || p->sec != sec)
5960 {
5961 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5962 if (p == NULL)
5963 return FALSE;
5964 p->next = *head;
5965 *head = p;
5966 p->sec = sec;
5967 p->count = 0;
5968 p->pc_count = 0;
5969 }
5970 p->count += 1;
5971 if (!must_be_dyn_reloc (info, r_type))
5972 p->pc_count += 1;
65f38f15
AM
5973 }
5974 else
5975 {
ec338859
AM
5976 /* Track dynamic relocs needed for local syms too.
5977 We really need local syms available to do this
5978 easily. Oh well. */
19e08130
AM
5979 struct ppc_dyn_relocs *p;
5980 struct ppc_dyn_relocs **head;
5981 bfd_boolean is_ifunc;
ec338859 5982 asection *s;
6edfbbad 5983 void *vpp;
87d72d41 5984 Elf_Internal_Sym *isym;
6edfbbad 5985
87d72d41
AM
5986 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5987 abfd, r_symndx);
5988 if (isym == NULL)
b34976b6 5989 return FALSE;
ec338859 5990
87d72d41
AM
5991 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5992 if (s == NULL)
5993 s = sec;
5994
6edfbbad 5995 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5996 head = (struct ppc_dyn_relocs **) vpp;
5997 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5998 p = *head;
5999 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6000 p = p->next;
6001 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6002 {
6003 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6004 if (p == NULL)
6005 return FALSE;
6006 p->next = *head;
6007 *head = p;
6008 p->sec = sec;
6009 p->ifunc = is_ifunc;
6010 p->count = 0;
6011 }
6012 p->count += 1;
ec338859 6013 }
65f38f15 6014 }
5bd4f169 6015 break;
65f38f15
AM
6016
6017 default:
96e0dda4 6018 break;
5bd4f169
AM
6019 }
6020 }
6021
b34976b6 6022 return TRUE;
5bd4f169
AM
6023}
6024
ee67d69a
AM
6025/* Merge backend specific data from an object file to the output
6026 object file when linking. */
6027
6028static bfd_boolean
50e03d47 6029ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6030{
50e03d47 6031 bfd *obfd = info->output_bfd;
ee67d69a
AM
6032 unsigned long iflags, oflags;
6033
6034 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6035 return TRUE;
6036
6037 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6038 return TRUE;
6039
50e03d47 6040 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6041 return FALSE;
6042
6043 iflags = elf_elfheader (ibfd)->e_flags;
6044 oflags = elf_elfheader (obfd)->e_flags;
6045
f6c7c3e8 6046 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6047 {
4eca0228 6048 _bfd_error_handler
695344c0 6049 /* xgettext:c-format */
ee67d69a
AM
6050 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6051 bfd_set_error (bfd_error_bad_value);
6052 return FALSE;
6053 }
f6c7c3e8 6054 else if (iflags != oflags && iflags != 0)
ee67d69a 6055 {
4eca0228 6056 _bfd_error_handler
695344c0 6057 /* xgettext:c-format */
ee67d69a
AM
6058 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6059 ibfd, iflags, oflags);
6060 bfd_set_error (bfd_error_bad_value);
6061 return FALSE;
6062 }
6063
50e03d47 6064 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6065
ee67d69a 6066 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6067 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6068
6069 return TRUE;
6070}
6071
6072static bfd_boolean
6073ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6074{
6075 /* Print normal ELF private data. */
6076 _bfd_elf_print_private_bfd_data (abfd, ptr);
6077
6078 if (elf_elfheader (abfd)->e_flags != 0)
6079 {
6080 FILE *file = ptr;
6081
ee67d69a
AM
6082 fprintf (file, _("private flags = 0x%lx:"),
6083 elf_elfheader (abfd)->e_flags);
6084
6085 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6086 fprintf (file, _(" [abiv%ld]"),
6087 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6088 fputc ('\n', file);
6089 }
6090
6091 return TRUE;
6092}
6093
8387904d 6094/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6095 of the code entry point, and its section, which must be in the same
6096 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6097
6098static bfd_vma
6099opd_entry_value (asection *opd_sec,
6100 bfd_vma offset,
6101 asection **code_sec,
aef36ac1
AM
6102 bfd_vma *code_off,
6103 bfd_boolean in_code_sec)
8387904d
AM
6104{
6105 bfd *opd_bfd = opd_sec->owner;
8860955f 6106 Elf_Internal_Rela *relocs;
8387904d 6107 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6108 bfd_vma val;
8387904d 6109
9f296da3
AM
6110 /* No relocs implies we are linking a --just-symbols object, or looking
6111 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6112 if (opd_sec->reloc_count == 0)
6113 {
729eabd5 6114 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6115
729eabd5
AM
6116 if (contents == NULL)
6117 {
6118 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6119 return (bfd_vma) -1;
6120 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6121 }
ee1e4ede 6122
dbb3fbbb 6123 /* PR 17512: file: 64b9dfbb. */
451dfd38 6124 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6125 return (bfd_vma) -1;
6126
729eabd5 6127 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6128 if (code_sec != NULL)
6129 {
6130 asection *sec, *likely = NULL;
ee1e4ede 6131
aef36ac1 6132 if (in_code_sec)
4b85d634 6133 {
aef36ac1
AM
6134 sec = *code_sec;
6135 if (sec->vma <= val
6136 && val < sec->vma + sec->size)
6137 likely = sec;
6138 else
6139 val = -1;
6140 }
6141 else
6142 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6143 if (sec->vma <= val
6144 && (sec->flags & SEC_LOAD) != 0
6145 && (sec->flags & SEC_ALLOC) != 0)
6146 likely = sec;
6147 if (likely != NULL)
6148 {
6149 *code_sec = likely;
6150 if (code_off != NULL)
6151 *code_off = val - likely->vma;
4b85d634
AM
6152 }
6153 }
aef36ac1 6154 return val;
4b85d634
AM
6155 }
6156
0c8d6e5c 6157 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6158
729eabd5 6159 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6160 if (relocs == NULL)
6161 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6162 /* PR 17512: file: df8e1fd6. */
6163 if (relocs == NULL)
6164 return (bfd_vma) -1;
645ea6a9 6165
8387904d 6166 /* Go find the opd reloc at the sym address. */
8860955f 6167 lo = relocs;
8387904d 6168 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6169 val = (bfd_vma) -1;
8387904d
AM
6170 while (lo < hi)
6171 {
6172 look = lo + (hi - lo) / 2;
6173 if (look->r_offset < offset)
6174 lo = look + 1;
6175 else if (look->r_offset > offset)
6176 hi = look;
6177 else
6178 {
0ffa91dd
NC
6179 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6180
8387904d
AM
6181 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6182 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6183 {
6184 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6185 asection *sec = NULL;
8387904d 6186
b53dfeb2
AM
6187 if (symndx >= symtab_hdr->sh_info
6188 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6189 {
6190 struct elf_link_hash_entry **sym_hashes;
6191 struct elf_link_hash_entry *rh;
6192
6193 sym_hashes = elf_sym_hashes (opd_bfd);
6194 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6195 if (rh != NULL)
6196 {
6197 rh = elf_follow_link (rh);
bb854a36
AM
6198 if (rh->root.type != bfd_link_hash_defined
6199 && rh->root.type != bfd_link_hash_defweak)
6200 break;
6201 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6202 {
bb854a36
AM
6203 val = rh->root.u.def.value;
6204 sec = rh->root.u.def.section;
b53dfeb2
AM
6205 }
6206 }
6207 }
6208
6209 if (sec == NULL)
6210 {
6211 Elf_Internal_Sym *sym;
6212
6213 if (symndx < symtab_hdr->sh_info)
6214 {
6215 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6216 if (sym == NULL)
6217 {
6218 size_t symcnt = symtab_hdr->sh_info;
6219 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6220 symcnt, 0,
6221 NULL, NULL, NULL);
6222 if (sym == NULL)
6223 break;
6224 symtab_hdr->contents = (bfd_byte *) sym;
6225 }
6226 sym += symndx;
128205bb
AM
6227 }
6228 else
6229 {
b53dfeb2
AM
6230 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6231 1, symndx,
6232 NULL, NULL, NULL);
128205bb
AM
6233 if (sym == NULL)
6234 break;
128205bb 6235 }
b53dfeb2
AM
6236 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6237 if (sec == NULL)
6238 break;
6239 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6240 val = sym->st_value;
8387904d 6241 }
b53dfeb2 6242
8387904d
AM
6243 val += look->r_addend;
6244 if (code_off != NULL)
6245 *code_off = val;
6246 if (code_sec != NULL)
aef36ac1
AM
6247 {
6248 if (in_code_sec && *code_sec != sec)
6249 return -1;
6250 else
6251 *code_sec = sec;
6252 }
b53dfeb2 6253 if (sec->output_section != NULL)
8387904d 6254 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6255 }
6256 break;
6257 }
6258 }
645ea6a9 6259
645ea6a9 6260 return val;
8387904d
AM
6261}
6262
aef36ac1
AM
6263/* If the ELF symbol SYM might be a function in SEC, return the
6264 function size and set *CODE_OFF to the function's entry point,
6265 otherwise return zero. */
9f296da3 6266
aef36ac1
AM
6267static bfd_size_type
6268ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6269 bfd_vma *code_off)
9f296da3 6270{
aef36ac1
AM
6271 bfd_size_type size;
6272
6273 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6274 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6275 return 0;
6276
6277 size = 0;
6278 if (!(sym->flags & BSF_SYNTHETIC))
6279 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6280
6281 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6282 {
b07bca4e
AM
6283 struct _opd_sec_data *opd = get_opd_info (sym->section);
6284 bfd_vma symval = sym->value;
6285
6286 if (opd != NULL
6287 && opd->adjust != NULL
6288 && elf_section_data (sym->section)->relocs != NULL)
6289 {
6290 /* opd_entry_value will use cached relocs that have been
6291 adjusted, but with raw symbols. That means both local
6292 and global symbols need adjusting. */
6293 long adjust = opd->adjust[OPD_NDX (symval)];
6294 if (adjust == -1)
6295 return 0;
6296 symval += adjust;
6297 }
6298
6299 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6300 &sec, code_off, TRUE) == (bfd_vma) -1)
6301 return 0;
6302 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6303 symbol. This size has nothing to do with the code size of the
6304 function, which is what we're supposed to return, but the
6305 code size isn't available without looking up the dot-sym.
6306 However, doing that would be a waste of time particularly
6307 since elf_find_function will look at the dot-sym anyway.
6308 Now, elf_find_function will keep the largest size of any
6309 function sym found at the code address of interest, so return
6310 1 here to avoid it incorrectly caching a larger function size
6311 for a small function. This does mean we return the wrong
6312 size for a new-ABI function of size 24, but all that does is
6313 disable caching for such functions. */
6314 if (size == 24)
6315 size = 1;
9f296da3 6316 }
aef36ac1
AM
6317 else
6318 {
6319 if (sym->section != sec)
6320 return 0;
6321 *code_off = sym->value;
6322 }
6323 if (size == 0)
6324 size = 1;
6325 return size;
9f296da3
AM
6326}
6327
f378ab09
AM
6328/* Return true if symbol is a strong function defined in an ELFv2
6329 object with st_other localentry bits of zero, ie. its local entry
6330 point coincides with its global entry point. */
6331
6332static bfd_boolean
6333is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6334{
6335 return (h != NULL
6336 && h->type == STT_FUNC
6337 && h->root.type == bfd_link_hash_defined
6338 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6339 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6340 && is_ppc64_elf (h->root.u.def.section->owner)
6341 && abiversion (h->root.u.def.section->owner) >= 2);
6342}
6343
854b41e7
AM
6344/* Return true if symbol is defined in a regular object file. */
6345
6346static bfd_boolean
6347is_static_defined (struct elf_link_hash_entry *h)
6348{
6349 return ((h->root.type == bfd_link_hash_defined
6350 || h->root.type == bfd_link_hash_defweak)
6351 && h->root.u.def.section != NULL
6352 && h->root.u.def.section->output_section != NULL);
6353}
6354
b31867b6
AM
6355/* If FDH is a function descriptor symbol, return the associated code
6356 entry symbol if it is defined. Return NULL otherwise. */
6357
6358static struct ppc_link_hash_entry *
6359defined_code_entry (struct ppc_link_hash_entry *fdh)
6360{
6361 if (fdh->is_func_descriptor)
6362 {
6363 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6364 if (fh->elf.root.type == bfd_link_hash_defined
6365 || fh->elf.root.type == bfd_link_hash_defweak)
6366 return fh;
6367 }
6368 return NULL;
6369}
6370
6371/* If FH is a function code entry symbol, return the associated
6372 function descriptor symbol if it is defined. Return NULL otherwise. */
6373
6374static struct ppc_link_hash_entry *
6375defined_func_desc (struct ppc_link_hash_entry *fh)
6376{
6377 if (fh->oh != NULL
6378 && fh->oh->is_func_descriptor)
6379 {
6380 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6381 if (fdh->elf.root.type == bfd_link_hash_defined
6382 || fdh->elf.root.type == bfd_link_hash_defweak)
6383 return fdh;
6384 }
6385 return NULL;
6386}
6387
8c5b4e52
AM
6388static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6389
6390/* Garbage collect sections, after first dealing with dot-symbols. */
6391
6392static bfd_boolean
6393ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6394{
6395 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6396
6397 if (htab != NULL && htab->need_func_desc_adj)
6398 {
6399 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6400 htab->need_func_desc_adj = 0;
6401 }
6402 return bfd_elf_gc_sections (abfd, info);
6403}
6404
74f0fb50
AM
6405/* Mark all our entry sym sections, both opd and code section. */
6406
6407static void
6408ppc64_elf_gc_keep (struct bfd_link_info *info)
6409{
6410 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6411 struct bfd_sym_chain *sym;
6412
4dfe6ac6
NC
6413 if (htab == NULL)
6414 return;
6415
74f0fb50
AM
6416 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6417 {
b31867b6 6418 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6419 asection *sec;
6420
6421 eh = (struct ppc_link_hash_entry *)
b31867b6 6422 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6423 if (eh == NULL)
6424 continue;
6425 if (eh->elf.root.type != bfd_link_hash_defined
6426 && eh->elf.root.type != bfd_link_hash_defweak)
6427 continue;
6428
b31867b6
AM
6429 fh = defined_code_entry (eh);
6430 if (fh != NULL)
74f0fb50 6431 {
b31867b6 6432 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6433 sec->flags |= SEC_KEEP;
6434 }
6435 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6436 && opd_entry_value (eh->elf.root.u.def.section,
6437 eh->elf.root.u.def.value,
aef36ac1 6438 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6439 sec->flags |= SEC_KEEP;
6440
6441 sec = eh->elf.root.u.def.section;
6442 sec->flags |= SEC_KEEP;
6443 }
6444}
6445
64d03ab5
AM
6446/* Mark sections containing dynamically referenced symbols. When
6447 building shared libraries, we must assume that any visible symbol is
6448 referenced. */
6449
6450static bfd_boolean
6451ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6452{
6453 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6454 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6455 struct ppc_link_hash_entry *fdh;
b407645f 6456 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6457
64d03ab5 6458 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6459 fdh = defined_func_desc (eh);
6460 if (fdh != NULL)
6461 eh = fdh;
64d03ab5
AM
6462
6463 if ((eh->elf.root.type == bfd_link_hash_defined
6464 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6465 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6466 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6467 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6468 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6469 && (!bfd_link_executable (info)
e278ae05 6470 || info->gc_keep_exported
b407645f
AM
6471 || info->export_dynamic
6472 || (eh->elf.dynamic
6473 && d != NULL
6474 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6475 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6476 || !bfd_hide_sym_by_version (info->version_info,
6477 eh->elf.root.root.string)))))
64d03ab5
AM
6478 {
6479 asection *code_sec;
b31867b6 6480 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6481
6482 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6483
6484 /* Function descriptor syms cause the associated
6485 function code sym section to be marked. */
b31867b6
AM
6486 fh = defined_code_entry (eh);
6487 if (fh != NULL)
6488 {
6489 code_sec = fh->elf.root.u.def.section;
6490 code_sec->flags |= SEC_KEEP;
6491 }
64d03ab5
AM
6492 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6493 && opd_entry_value (eh->elf.root.u.def.section,
6494 eh->elf.root.u.def.value,
aef36ac1 6495 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6496 code_sec->flags |= SEC_KEEP;
6497 }
6498
6499 return TRUE;
6500}
6501
5bd4f169
AM
6502/* Return the section that should be marked against GC for a given
6503 relocation. */
6504
6505static asection *
4ce794b7 6506ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6507 struct bfd_link_info *info,
4ce794b7
AM
6508 Elf_Internal_Rela *rel,
6509 struct elf_link_hash_entry *h,
6510 Elf_Internal_Sym *sym)
5bd4f169 6511{
ccfa59ea
AM
6512 asection *rsec;
6513
ccfa59ea
AM
6514 /* Syms return NULL if we're marking .opd, so we avoid marking all
6515 function sections, as all functions are referenced in .opd. */
6516 rsec = NULL;
6517 if (get_opd_info (sec) != NULL)
6518 return rsec;
1e2f5b6e 6519
5bd4f169
AM
6520 if (h != NULL)
6521 {
04c9666a 6522 enum elf_ppc64_reloc_type r_type;
b31867b6 6523 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6524
4ce794b7 6525 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6526 switch (r_type)
5bd4f169
AM
6527 {
6528 case R_PPC64_GNU_VTINHERIT:
6529 case R_PPC64_GNU_VTENTRY:
6530 break;
6531
6532 default:
6533 switch (h->root.type)
6534 {
6535 case bfd_link_hash_defined:
6536 case bfd_link_hash_defweak:
ccfa59ea 6537 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6538 fdh = defined_func_desc (eh);
6539 if (fdh != NULL)
8c5b4e52
AM
6540 {
6541 /* -mcall-aixdesc code references the dot-symbol on
6542 a call reloc. Mark the function descriptor too
6543 against garbage collection. */
6544 fdh->elf.mark = 1;
60d67dc8
AM
6545 if (fdh->elf.is_weakalias)
6546 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6547 eh = fdh;
6548 }
1e2f5b6e
AM
6549
6550 /* Function descriptor syms cause the associated
6551 function code sym section to be marked. */
b31867b6
AM
6552 fh = defined_code_entry (eh);
6553 if (fh != NULL)
ccfa59ea
AM
6554 {
6555 /* They also mark their opd section. */
74f0fb50 6556 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6557
b31867b6 6558 rsec = fh->elf.root.u.def.section;
ccfa59ea 6559 }
8387904d
AM
6560 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6561 && opd_entry_value (eh->elf.root.u.def.section,
6562 eh->elf.root.u.def.value,
aef36ac1 6563 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6564 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6565 else
1e2f5b6e
AM
6566 rsec = h->root.u.def.section;
6567 break;
5bd4f169
AM
6568
6569 case bfd_link_hash_common:
1e2f5b6e
AM
6570 rsec = h->root.u.c.p->section;
6571 break;
5bd4f169
AM
6572
6573 default:
fb34365b 6574 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6575 }
6576 }
6577 }
6578 else
6579 {
74f0fb50 6580 struct _opd_sec_data *opd;
1e2f5b6e
AM
6581
6582 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6583 opd = get_opd_info (rsec);
6584 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6585 {
74f0fb50 6586 rsec->gc_mark = 1;
ccfa59ea 6587
51aecdc5 6588 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6589 }
5bd4f169
AM
6590 }
6591
1e2f5b6e 6592 return rsec;
5bd4f169
AM
6593}
6594
deb0e272
AM
6595/* The maximum size of .sfpr. */
6596#define SFPR_MAX (218*4)
6597
6598struct sfpr_def_parms
6599{
699733f6
AM
6600 const char name[12];
6601 unsigned char lo, hi;
deb0e272
AM
6602 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6603 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6604};
6605
a4b6fadd
AM
6606/* Auto-generate _save*, _rest* functions in .sfpr.
6607 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6608 instead. */
deb0e272 6609
4dfe6ac6 6610static bfd_boolean
a4b6fadd
AM
6611sfpr_define (struct bfd_link_info *info,
6612 const struct sfpr_def_parms *parm,
6613 asection *stub_sec)
deb0e272
AM
6614{
6615 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6616 unsigned int i;
6617 size_t len = strlen (parm->name);
6618 bfd_boolean writing = FALSE;
699733f6 6619 char sym[16];
deb0e272 6620
4dfe6ac6
NC
6621 if (htab == NULL)
6622 return FALSE;
6623
deb0e272
AM
6624 memcpy (sym, parm->name, len);
6625 sym[len + 2] = 0;
6626
6627 for (i = parm->lo; i <= parm->hi; i++)
6628 {
a4b6fadd 6629 struct ppc_link_hash_entry *h;
deb0e272
AM
6630
6631 sym[len + 0] = i / 10 + '0';
6632 sym[len + 1] = i % 10 + '0';
a4b6fadd 6633 h = (struct ppc_link_hash_entry *)
b32547cd 6634 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6635 if (stub_sec != NULL)
deb0e272 6636 {
a4b6fadd
AM
6637 if (h != NULL
6638 && h->elf.root.type == bfd_link_hash_defined
6639 && h->elf.root.u.def.section == htab->sfpr)
6640 {
6641 struct elf_link_hash_entry *s;
6642 char buf[32];
6643 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6644 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6645 if (s == NULL)
6646 return FALSE;
6647 if (s->root.type == bfd_link_hash_new
6648 || (s->root.type = bfd_link_hash_defined
6649 && s->root.u.def.section == stub_sec))
6650 {
6651 s->root.type = bfd_link_hash_defined;
6652 s->root.u.def.section = stub_sec;
6653 s->root.u.def.value = (stub_sec->size
6654 + h->elf.root.u.def.value);
6655 s->ref_regular = 1;
6656 s->def_regular = 1;
6657 s->ref_regular_nonweak = 1;
6658 s->forced_local = 1;
6659 s->non_elf = 0;
6660 s->root.linker_def = 1;
6661 }
6662 }
6663 continue;
6664 }
6665 if (h != NULL)
6666 {
6667 h->save_res = 1;
6668 if (!h->elf.def_regular)
deb0e272 6669 {
a4b6fadd
AM
6670 h->elf.root.type = bfd_link_hash_defined;
6671 h->elf.root.u.def.section = htab->sfpr;
6672 h->elf.root.u.def.value = htab->sfpr->size;
6673 h->elf.type = STT_FUNC;
6674 h->elf.def_regular = 1;
b32547cd 6675 h->elf.non_elf = 0;
a4b6fadd
AM
6676 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6677 writing = TRUE;
deb0e272 6678 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6679 {
6680 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6681 if (htab->sfpr->contents == NULL)
6682 return FALSE;
6683 }
deb0e272
AM
6684 }
6685 }
6686 if (writing)
6687 {
6688 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6689 if (i != parm->hi)
6690 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6691 else
6692 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6693 htab->sfpr->size = p - htab->sfpr->contents;
6694 }
6695 }
6696
6697 return TRUE;
6698}
6699
6700static bfd_byte *
6701savegpr0 (bfd *abfd, bfd_byte *p, int r)
6702{
6703 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6704 return p + 4;
6705}
6706
6707static bfd_byte *
6708savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6709{
6710 p = savegpr0 (abfd, p, r);
a078d95a 6711 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6712 p = p + 4;
6713 bfd_put_32 (abfd, BLR, p);
6714 return p + 4;
6715}
6716
6717static bfd_byte *
6718restgpr0 (bfd *abfd, bfd_byte *p, int r)
6719{
6720 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6721 return p + 4;
6722}
6723
6724static bfd_byte *
6725restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6726{
a078d95a 6727 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6728 p = p + 4;
6729 p = restgpr0 (abfd, p, r);
6730 bfd_put_32 (abfd, MTLR_R0, p);
6731 p = p + 4;
6732 if (r == 29)
6733 {
6734 p = restgpr0 (abfd, p, 30);
6735 p = restgpr0 (abfd, p, 31);
6736 }
6737 bfd_put_32 (abfd, BLR, p);
6738 return p + 4;
6739}
6740
6741static bfd_byte *
6742savegpr1 (bfd *abfd, bfd_byte *p, int r)
6743{
6744 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6745 return p + 4;
6746}
6747
6748static bfd_byte *
6749savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6750{
6751 p = savegpr1 (abfd, p, r);
6752 bfd_put_32 (abfd, BLR, p);
6753 return p + 4;
6754}
6755
6756static bfd_byte *
6757restgpr1 (bfd *abfd, bfd_byte *p, int r)
6758{
6759 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6760 return p + 4;
6761}
6762
6763static bfd_byte *
6764restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6765{
6766 p = restgpr1 (abfd, p, r);
6767 bfd_put_32 (abfd, BLR, p);
6768 return p + 4;
6769}
6770
6771static bfd_byte *
6772savefpr (bfd *abfd, bfd_byte *p, int r)
6773{
6774 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6775 return p + 4;
6776}
6777
6778static bfd_byte *
6779savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6780{
6781 p = savefpr (abfd, p, r);
a078d95a 6782 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6783 p = p + 4;
6784 bfd_put_32 (abfd, BLR, p);
6785 return p + 4;
6786}
6787
6788static bfd_byte *
6789restfpr (bfd *abfd, bfd_byte *p, int r)
6790{
6791 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6792 return p + 4;
6793}
6794
6795static bfd_byte *
6796restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6797{
a078d95a 6798 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6799 p = p + 4;
6800 p = restfpr (abfd, p, r);
6801 bfd_put_32 (abfd, MTLR_R0, p);
6802 p = p + 4;
6803 if (r == 29)
6804 {
6805 p = restfpr (abfd, p, 30);
6806 p = restfpr (abfd, p, 31);
6807 }
6808 bfd_put_32 (abfd, BLR, p);
6809 return p + 4;
6810}
6811
6812static bfd_byte *
6813savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6814{
6815 p = savefpr (abfd, p, r);
6816 bfd_put_32 (abfd, BLR, p);
6817 return p + 4;
6818}
6819
6820static bfd_byte *
6821restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6822{
6823 p = restfpr (abfd, p, r);
6824 bfd_put_32 (abfd, BLR, p);
6825 return p + 4;
6826}
6827
6828static bfd_byte *
6829savevr (bfd *abfd, bfd_byte *p, int r)
6830{
6831 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6832 p = p + 4;
6833 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6834 return p + 4;
6835}
6836
6837static bfd_byte *
6838savevr_tail (bfd *abfd, bfd_byte *p, int r)
6839{
6840 p = savevr (abfd, p, r);
6841 bfd_put_32 (abfd, BLR, p);
6842 return p + 4;
6843}
6844
6845static bfd_byte *
6846restvr (bfd *abfd, bfd_byte *p, int r)
6847{
6848 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6849 p = p + 4;
6850 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6851 return p + 4;
6852}
6853
6854static bfd_byte *
6855restvr_tail (bfd *abfd, bfd_byte *p, int r)
6856{
6857 p = restvr (abfd, p, r);
6858 bfd_put_32 (abfd, BLR, p);
6859 return p + 4;
6860}
6861
e86ce104
AM
6862/* Called via elf_link_hash_traverse to transfer dynamic linking
6863 information on function code symbol entries to their corresponding
6864 function descriptor symbol entries. */
deb0e272 6865
b34976b6 6866static bfd_boolean
4ce794b7 6867func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6868{
e86ce104 6869 struct bfd_link_info *info;
65f38f15 6870 struct ppc_link_hash_table *htab;
50bc7936
AM
6871 struct ppc_link_hash_entry *fh;
6872 struct ppc_link_hash_entry *fdh;
6873 bfd_boolean force_local;
5bd4f169 6874
50bc7936
AM
6875 fh = (struct ppc_link_hash_entry *) h;
6876 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6877 return TRUE;
e86ce104 6878
8c5b4e52
AM
6879 if (!fh->is_func)
6880 return TRUE;
6881
6882 if (fh->elf.root.root.string[0] != '.'
6883 || fh->elf.root.root.string[1] == '\0')
6884 return TRUE;
6885
4ce794b7 6886 info = inf;
65f38f15 6887 htab = ppc_hash_table (info);
4dfe6ac6
NC
6888 if (htab == NULL)
6889 return FALSE;
5bd4f169 6890
8c5b4e52
AM
6891 /* Find the corresponding function descriptor symbol. */
6892 fdh = lookup_fdh (fh, htab);
6893
c09bdfe5
AM
6894 /* Resolve undefined references to dot-symbols as the value
6895 in the function descriptor, if we have one in a regular object.
6896 This is to satisfy cases like ".quad .foo". Calls to functions
6897 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6898 if ((fh->elf.root.type == bfd_link_hash_undefined
6899 || fh->elf.root.type == bfd_link_hash_undefweak)
6900 && (fdh->elf.root.type == bfd_link_hash_defined
6901 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6902 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6903 && opd_entry_value (fdh->elf.root.u.def.section,
6904 fdh->elf.root.u.def.value,
c09bdfe5 6905 &fh->elf.root.u.def.section,
aef36ac1 6906 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6907 {
b31867b6 6908 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6909 fh->elf.forced_local = 1;
b31867b6
AM
6910 fh->elf.def_regular = fdh->elf.def_regular;
6911 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6912 }
6913
8c5b4e52
AM
6914 if (!fh->elf.dynamic)
6915 {
6916 struct plt_entry *ent;
5bd4f169 6917
8c5b4e52
AM
6918 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6919 if (ent->plt.refcount > 0)
6920 break;
6921 if (ent == NULL)
6922 return TRUE;
6923 }
5bd4f169 6924
8c5b4e52 6925 /* Create a descriptor as undefined if necessary. */
50bc7936 6926 if (fdh == NULL
0e1862bb 6927 && !bfd_link_executable (info)
50bc7936
AM
6928 && (fh->elf.root.type == bfd_link_hash_undefined
6929 || fh->elf.root.type == bfd_link_hash_undefweak))
6930 {
908b32fc 6931 fdh = make_fdh (info, fh);
bb700d78
AM
6932 if (fdh == NULL)
6933 return FALSE;
50bc7936 6934 }
648cca2c 6935
8c5b4e52 6936 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6937 if (fdh != NULL
6938 && fdh->fake
8c5b4e52
AM
6939 && (fh->elf.root.type == bfd_link_hash_defined
6940 || fh->elf.root.type == bfd_link_hash_defweak))
6941 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6942
8c5b4e52
AM
6943 /* Transfer dynamic linking information to the function descriptor. */
6944 if (fdh != NULL)
6945 {
f5385ebf
AM
6946 fdh->elf.ref_regular |= fh->elf.ref_regular;
6947 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6948 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6949 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6950 fdh->elf.dynamic |= fh->elf.dynamic;
6951 fdh->elf.needs_plt |= (fh->elf.needs_plt
6952 || fh->elf.type == STT_FUNC
6953 || fh->elf.type == STT_GNU_IFUNC);
6954 move_plt_plist (fh, fdh);
6955
6956 if (!fdh->elf.forced_local
6957 && fh->elf.dynindx != -1)
6958 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6959 return FALSE;
e86ce104
AM
6960 }
6961
50bc7936
AM
6962 /* Now that the info is on the function descriptor, clear the
6963 function code sym info. Any function code syms for which we
6964 don't have a definition in a regular file, we force local.
6965 This prevents a shared library from exporting syms that have
6966 been imported from another library. Function code syms that
6967 are really in the library we must leave global to prevent the
6968 linker dragging in a definition from a static library. */
93f3fa99
AM
6969 force_local = (!fh->elf.def_regular
6970 || fdh == NULL
6971 || !fdh->elf.def_regular
6972 || fdh->elf.forced_local);
50bc7936
AM
6973 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6974
b34976b6 6975 return TRUE;
e86ce104 6976}
40b8271b 6977
a4b6fadd
AM
6978static const struct sfpr_def_parms save_res_funcs[] =
6979 {
6980 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6981 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6982 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6983 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6984 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6985 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6986 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6987 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6988 { "._savef", 14, 31, savefpr, savefpr1_tail },
6989 { "._restf", 14, 31, restfpr, restfpr1_tail },
6990 { "_savevr_", 20, 31, savevr, savevr_tail },
6991 { "_restvr_", 20, 31, restvr, restvr_tail }
6992 };
6993
e86ce104 6994/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6995 this hook to a) provide some gcc support functions, and b) transfer
6996 dynamic linking information gathered so far on function code symbol
6997 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6998
b34976b6 6999static bfd_boolean
4ce794b7
AM
7000ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7001 struct bfd_link_info *info)
e86ce104
AM
7002{
7003 struct ppc_link_hash_table *htab;
7004
7005 htab = ppc_hash_table (info);
4dfe6ac6
NC
7006 if (htab == NULL)
7007 return FALSE;
7008
b32547cd
AM
7009 /* Provide any missing _save* and _rest* functions. */
7010 if (htab->sfpr != NULL)
7011 {
7012 unsigned int i;
7013
7014 htab->sfpr->size = 0;
7015 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7016 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7017 return FALSE;
7018 if (htab->sfpr->size == 0)
7019 htab->sfpr->flags |= SEC_EXCLUDE;
7020 }
7021
7022 if (bfd_link_relocatable (info))
7023 return TRUE;
7024
7025 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7026 {
7027 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7028 /* Make .TOC. defined so as to prevent it being made dynamic.
7029 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7030 if (!htab->elf.hgot->def_regular
7031 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7032 {
7033 htab->elf.hgot->root.type = bfd_link_hash_defined;
7034 htab->elf.hgot->root.u.def.value = 0;
7035 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7036 htab->elf.hgot->def_regular = 1;
7037 htab->elf.hgot->root.linker_def = 1;
7038 }
dba6fa9b 7039 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7040 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7041 | STV_HIDDEN);
7042 }
c66bb0ee 7043
8c5b4e52
AM
7044 if (htab->need_func_desc_adj)
7045 {
7046 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7047 htab->need_func_desc_adj = 0;
7048 }
805fc799 7049
b34976b6 7050 return TRUE;
e86ce104
AM
7051}
7052
98bbb1b8 7053/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7054
98bbb1b8 7055static asection *
a345bc8d
AM
7056readonly_dynrelocs (struct elf_link_hash_entry *h)
7057{
7058 struct ppc_link_hash_entry *eh;
7059 struct elf_dyn_relocs *p;
7060
7061 eh = (struct ppc_link_hash_entry *) h;
7062 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7063 {
7064 asection *s = p->sec->output_section;
7065
7066 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7067 return p->sec;
a345bc8d 7068 }
98bbb1b8 7069 return NULL;
a345bc8d
AM
7070}
7071
d311bc8b 7072/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7073 aliases, that apply to read-only sections. Cannot be used after
7074 size_dynamic_sections. */
d311bc8b
AM
7075
7076static bfd_boolean
7077alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7078{
7079 struct ppc_link_hash_entry *eh;
7080
7081 eh = (struct ppc_link_hash_entry *) h;
7082 do
7083 {
7084 if (readonly_dynrelocs (&eh->elf))
7085 return TRUE;
ab2477e1 7086 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
d311bc8b
AM
7087 } while (eh != NULL && &eh->elf != h);
7088
7089 return FALSE;
7090}
8a2058b5 7091
8a9e8e72
AM
7092/* Return whether EH has pc-relative dynamic relocs. */
7093
7094static bfd_boolean
7095pc_dynrelocs (struct ppc_link_hash_entry *eh)
7096{
7097 struct elf_dyn_relocs *p;
7098
7099 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7100 if (p->pc_count != 0)
7101 return TRUE;
7102 return FALSE;
7103}
7104
8a2058b5
AM
7105/* Return true if a global entry stub will be created for H. Valid
7106 for ELFv2 before plt entries have been allocated. */
7107
7108static bfd_boolean
7109global_entry_stub (struct elf_link_hash_entry *h)
7110{
7111 struct plt_entry *pent;
7112
7113 if (!h->pointer_equality_needed
7114 || h->def_regular)
7115 return FALSE;
7116
7117 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7118 if (pent->plt.refcount > 0
7119 && pent->addend == 0)
7120 return TRUE;
7121
7122 return FALSE;
7123}
7124
e86ce104
AM
7125/* Adjust a symbol defined by a dynamic object and referenced by a
7126 regular object. The current definition is in some section of the
7127 dynamic object, but we're not including those sections. We have to
7128 change the definition to something the rest of the link can
7129 understand. */
7130
b34976b6 7131static bfd_boolean
4ce794b7
AM
7132ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7133 struct elf_link_hash_entry *h)
e86ce104
AM
7134{
7135 struct ppc_link_hash_table *htab;
5474d94f 7136 asection *s, *srel;
e86ce104
AM
7137
7138 htab = ppc_hash_table (info);
4dfe6ac6
NC
7139 if (htab == NULL)
7140 return FALSE;
e86ce104
AM
7141
7142 /* Deal with function syms. */
7143 if (h->type == STT_FUNC
e054468f 7144 || h->type == STT_GNU_IFUNC
f5385ebf 7145 || h->needs_plt)
e86ce104 7146 {
529fe20e
AM
7147 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7148 || SYMBOL_CALLS_LOCAL (info, h)
7149 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7150 /* Discard dyn_relocs when non-pic if we've decided that a
7151 function symbol is local and not an ifunc. We keep dynamic
7152 relocs for ifuncs when local rather than always emitting a
7153 plt call stub for them and defining the symbol on the call
7154 stub. We can't do that for ELFv1 anyway (a function symbol
7155 is defined on a descriptor, not code) and it can be faster at
7156 run-time due to not needing to bounce through a stub. The
7157 dyn_relocs for ifuncs will be applied even in a static
7158 executable. */
7159 if (!bfd_link_pic (info)
7160 && h->type != STT_GNU_IFUNC
7161 && local)
7162 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7163
e86ce104
AM
7164 /* Clear procedure linkage table information for any symbol that
7165 won't need a .plt entry. */
411e1bfb
AM
7166 struct plt_entry *ent;
7167 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7168 if (ent->plt.refcount > 0)
7169 break;
8387904d 7170 if (ent == NULL
529fe20e 7171 || (h->type != STT_GNU_IFUNC && local))
40b8271b 7172 {
411e1bfb 7173 h->plt.plist = NULL;
f5385ebf 7174 h->needs_plt = 0;
d1eca1e4 7175 h->pointer_equality_needed = 0;
40b8271b 7176 }
8a2058b5 7177 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7178 {
d1eca1e4
AM
7179 /* Taking a function's address in a read/write section
7180 doesn't require us to define the function symbol in the
7181 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7182 be used instead. The reason we prefer a few more dynamic
7183 relocs is that calling via a global entry stub costs a
7184 few more instructions, and pointer_equality_needed causes
7185 extra work in ld.so when resolving these symbols. */
529fe20e 7186 if (global_entry_stub (h))
d1eca1e4 7187 {
ab2477e1 7188 if (!readonly_dynrelocs (h))
529fe20e
AM
7189 {
7190 h->pointer_equality_needed = 0;
7191 /* If we haven't seen a branch reloc then we don't need
7192 a plt entry. */
7193 if (!h->needs_plt)
7194 h->plt.plist = NULL;
7195 }
7196 else if (!bfd_link_pic (info))
7197 /* We are going to be defining the function symbol on the
7198 plt stub, so no dyn_relocs needed when non-pic. */
7199 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7200 }
7201
3988aed5
AM
7202 /* ELFv2 function symbols can't have copy relocs. */
7203 return TRUE;
7204 }
7205 else if (!h->needs_plt
ab2477e1 7206 && !readonly_dynrelocs (h))
3988aed5
AM
7207 {
7208 /* If we haven't seen a branch reloc then we don't need a
7209 plt entry. */
7210 h->plt.plist = NULL;
7211 h->pointer_equality_needed = 0;
a345bc8d
AM
7212 return TRUE;
7213 }
5bd4f169 7214 }
bbd7ec4a 7215 else
411e1bfb 7216 h->plt.plist = NULL;
5bd4f169
AM
7217
7218 /* If this is a weak symbol, and there is a real definition, the
7219 processor independent code will have arranged for us to see the
7220 real definition first, and we can just use the same value. */
60d67dc8 7221 if (h->is_weakalias)
5bd4f169 7222 {
60d67dc8
AM
7223 struct elf_link_hash_entry *def = weakdef (h);
7224 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7225 h->root.u.def.section = def->root.u.def.section;
7226 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7227 if (def->root.u.def.section == htab->elf.sdynbss
7228 || def->root.u.def.section == htab->elf.sdynrelro)
7229 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7230 return TRUE;
5bd4f169
AM
7231 }
7232
5bd4f169
AM
7233 /* If we are creating a shared library, we must presume that the
7234 only references to the symbol are via the global offset table.
7235 For such cases we need not do anything here; the relocations will
7236 be handled correctly by relocate_section. */
0e1862bb 7237 if (bfd_link_pic (info))
b34976b6 7238 return TRUE;
5bd4f169 7239
65f38f15
AM
7240 /* If there are no references to this symbol that do not use the
7241 GOT, we don't need to generate a copy reloc. */
f5385ebf 7242 if (!h->non_got_ref)
b34976b6 7243 return TRUE;
65f38f15 7244
b186458a 7245 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7246 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7247
d93d1c80
AM
7248 /* If -z nocopyreloc was given, don't generate them either. */
7249 || info->nocopyreloc
a127494f 7250
dce2246a 7251 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7252 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7253 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7254
d93d1c80
AM
7255 /* Protected variables do not work with .dynbss. The copy in
7256 .dynbss won't be used by the shared library with the protected
7257 definition for the variable. Text relocations are preferable
7258 to an incorrect program. */
7259 || h->protected_def)
529fe20e 7260 return TRUE;
a127494f 7261
5d35169e 7262 if (h->plt.plist != NULL)
97b639ba
AM
7263 {
7264 /* We should never get here, but unfortunately there are versions
7265 of gcc out there that improperly (for this ABI) put initialized
7266 function pointers, vtable refs and suchlike in read-only
7267 sections. Allow them to proceed, but warn that this might
7268 break at runtime. */
25f53a85 7269 info->callbacks->einfo
bc30df16 7270 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7271 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7272 h->root.root.string);
7273 }
5d35169e
AM
7274
7275 /* This is a reference to a symbol defined by a dynamic object which
7276 is not a function. */
7277
5bd4f169
AM
7278 /* We must allocate the symbol in our .dynbss section, which will
7279 become part of the .bss section of the executable. There will be
7280 an entry for this symbol in the .dynsym section. The dynamic
7281 object will contain position independent code, so all references
7282 from the dynamic object to this symbol will go through the global
7283 offset table. The dynamic linker will use the .dynsym entry to
7284 determine the address it must put in the global offset table, so
7285 both the dynamic object and the regular object will refer to the
7286 same memory location for the variable. */
5474d94f
AM
7287 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7288 {
7289 s = htab->elf.sdynrelro;
7290 srel = htab->elf.sreldynrelro;
7291 }
7292 else
7293 {
7294 s = htab->elf.sdynbss;
7295 srel = htab->elf.srelbss;
7296 }
1d7e9d18 7297 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7298 {
4a7e5234
AM
7299 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7300 linker to copy the initial value out of the dynamic object
7301 and into the runtime process image. */
5474d94f 7302 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7303 h->needs_copy = 1;
5bd4f169
AM
7304 }
7305
529fe20e
AM
7306 /* We no longer want dyn_relocs. */
7307 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7308 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7309}
7310
e86ce104
AM
7311/* If given a function descriptor symbol, hide both the function code
7312 sym and the descriptor. */
7313static void
4ce794b7
AM
7314ppc64_elf_hide_symbol (struct bfd_link_info *info,
7315 struct elf_link_hash_entry *h,
7316 bfd_boolean force_local)
e86ce104 7317{
34814b9f 7318 struct ppc_link_hash_entry *eh;
e86ce104
AM
7319 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7320
34814b9f
AM
7321 eh = (struct ppc_link_hash_entry *) h;
7322 if (eh->is_func_descriptor)
e86ce104 7323 {
34814b9f 7324 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7325
721956f4 7326 if (fh == NULL)
d1329ca3
AM
7327 {
7328 const char *p, *q;
b8ac2841 7329 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7330 char save;
7331
7332 /* We aren't supposed to use alloca in BFD because on
7333 systems which do not have alloca the version in libiberty
7334 calls xmalloc, which might cause the program to crash
7335 when it runs out of memory. This function doesn't have a
7336 return status, so there's no way to gracefully return an
7337 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7338 accessed; It's either a string in an ELF string table,
7339 or allocated in an objalloc structure. */
d1329ca3 7340
34814b9f 7341 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7342 save = *p;
7343 *(char *) p = '.';
34814b9f 7344 fh = (struct ppc_link_hash_entry *)
b8ac2841 7345 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7346 *(char *) p = save;
7347
7348 /* Unfortunately, if it so happens that the string we were
7349 looking for was allocated immediately before this string,
7350 then we overwrote the string terminator. That's the only
7351 reason the lookup should fail. */
7352 if (fh == NULL)
7353 {
34814b9f
AM
7354 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7355 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7356 --q, --p;
34814b9f
AM
7357 if (q < eh->elf.root.root.string && *p == '.')
7358 fh = (struct ppc_link_hash_entry *)
b8ac2841 7359 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7360 }
7361 if (fh != NULL)
7362 {
34814b9f
AM
7363 eh->oh = fh;
7364 fh->oh = eh;
d1329ca3
AM
7365 }
7366 }
e86ce104 7367 if (fh != NULL)
34814b9f 7368 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7369 }
7370}
7371
411e1bfb 7372static bfd_boolean
8843416a
AM
7373get_sym_h (struct elf_link_hash_entry **hp,
7374 Elf_Internal_Sym **symp,
7375 asection **symsecp,
f961d9dd 7376 unsigned char **tls_maskp,
8843416a
AM
7377 Elf_Internal_Sym **locsymsp,
7378 unsigned long r_symndx,
7379 bfd *ibfd)
411e1bfb 7380{
0ffa91dd 7381 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7382
7383 if (r_symndx >= symtab_hdr->sh_info)
7384 {
7385 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7386 struct elf_link_hash_entry *h;
7387
7388 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7389 h = elf_follow_link (h);
411e1bfb
AM
7390
7391 if (hp != NULL)
7392 *hp = h;
7393
7394 if (symp != NULL)
7395 *symp = NULL;
7396
7397 if (symsecp != NULL)
7398 {
7399 asection *symsec = NULL;
7400 if (h->root.type == bfd_link_hash_defined
7401 || h->root.type == bfd_link_hash_defweak)
7402 symsec = h->root.u.def.section;
7403 *symsecp = symsec;
7404 }
7405
e7b938ca 7406 if (tls_maskp != NULL)
411e1bfb
AM
7407 {
7408 struct ppc_link_hash_entry *eh;
7409
7410 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7411 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7412 }
7413 }
7414 else
7415 {
7416 Elf_Internal_Sym *sym;
7417 Elf_Internal_Sym *locsyms = *locsymsp;
7418
7419 if (locsyms == NULL)
7420 {
7421 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7422 if (locsyms == NULL)
7423 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7424 symtab_hdr->sh_info,
7425 0, NULL, NULL, NULL);
7426 if (locsyms == NULL)
7427 return FALSE;
7428 *locsymsp = locsyms;
7429 }
7430 sym = locsyms + r_symndx;
7431
7432 if (hp != NULL)
7433 *hp = NULL;
7434
7435 if (symp != NULL)
7436 *symp = sym;
7437
7438 if (symsecp != NULL)
cb33740c 7439 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7440
e7b938ca 7441 if (tls_maskp != NULL)
411e1bfb
AM
7442 {
7443 struct got_entry **lgot_ents;
f961d9dd 7444 unsigned char *tls_mask;
411e1bfb 7445
e7b938ca 7446 tls_mask = NULL;
411e1bfb
AM
7447 lgot_ents = elf_local_got_ents (ibfd);
7448 if (lgot_ents != NULL)
7449 {
e054468f
AM
7450 struct plt_entry **local_plt = (struct plt_entry **)
7451 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7452 unsigned char *lgot_masks = (unsigned char *)
e054468f 7453 (local_plt + symtab_hdr->sh_info);
e7b938ca 7454 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7455 }
e7b938ca 7456 *tls_maskp = tls_mask;
411e1bfb
AM
7457 }
7458 }
7459 return TRUE;
7460}
7461
e7b938ca 7462/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7463 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7464 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7465
7466static int
f961d9dd 7467get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7468 unsigned long *toc_symndx,
7469 bfd_vma *toc_addend,
0d4792f7 7470 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7471 const Elf_Internal_Rela *rel,
7472 bfd *ibfd)
411e1bfb
AM
7473{
7474 unsigned long r_symndx;
0d4792f7 7475 int next_r;
411e1bfb
AM
7476 struct elf_link_hash_entry *h;
7477 Elf_Internal_Sym *sym;
7478 asection *sec;
7479 bfd_vma off;
7480
7481 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7482 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7483 return 0;
411e1bfb 7484
e7b938ca 7485 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7486 || sec == NULL
6bee8834 7487 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7488 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7489 return 1;
411e1bfb
AM
7490
7491 /* Look inside a TOC section too. */
7492 if (h != NULL)
7493 {
7494 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7495 off = h->root.u.def.value;
7496 }
7497 else
7498 off = sym->st_value;
7499 off += rel->r_addend;
7500 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7501 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7502 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7503 if (toc_symndx != NULL)
7504 *toc_symndx = r_symndx;
3a71aa26
AM
7505 if (toc_addend != NULL)
7506 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7507 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7508 return 0;
854b41e7 7509 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7510 && (next_r == -1 || next_r == -2))
7511 return 1 - next_r;
951fd09b 7512 return 1;
411e1bfb
AM
7513}
7514
3b421ab3
AM
7515/* Find (or create) an entry in the tocsave hash table. */
7516
7517static struct tocsave_entry *
7518tocsave_find (struct ppc_link_hash_table *htab,
7519 enum insert_option insert,
7520 Elf_Internal_Sym **local_syms,
7521 const Elf_Internal_Rela *irela,
7522 bfd *ibfd)
7523{
7524 unsigned long r_indx;
7525 struct elf_link_hash_entry *h;
7526 Elf_Internal_Sym *sym;
7527 struct tocsave_entry ent, *p;
7528 hashval_t hash;
7529 struct tocsave_entry **slot;
7530
7531 r_indx = ELF64_R_SYM (irela->r_info);
7532 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7533 return NULL;
7534 if (ent.sec == NULL || ent.sec->output_section == NULL)
7535 {
4eca0228 7536 _bfd_error_handler
10463f39 7537 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7538 return NULL;
7539 }
7540
7541 if (h != NULL)
7542 ent.offset = h->root.u.def.value;
7543 else
7544 ent.offset = sym->st_value;
7545 ent.offset += irela->r_addend;
7546
7547 hash = tocsave_htab_hash (&ent);
7548 slot = ((struct tocsave_entry **)
7549 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7550 if (slot == NULL)
7551 return NULL;
7552
7553 if (*slot == NULL)
7554 {
7555 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7556 if (p == NULL)
7557 return NULL;
7558 *p = ent;
7559 *slot = p;
7560 }
7561 return *slot;
7562}
7563
754021d0 7564/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7565 code for the old ABI, these will already have been done. */
754021d0
AM
7566
7567static bfd_boolean
7568adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7569{
7570 struct ppc_link_hash_entry *eh;
7571 asection *sym_sec;
74f0fb50 7572 struct _opd_sec_data *opd;
754021d0
AM
7573
7574 if (h->root.type == bfd_link_hash_indirect)
7575 return TRUE;
7576
754021d0
AM
7577 if (h->root.type != bfd_link_hash_defined
7578 && h->root.type != bfd_link_hash_defweak)
7579 return TRUE;
7580
7581 eh = (struct ppc_link_hash_entry *) h;
7582 if (eh->adjust_done)
7583 return TRUE;
7584
7585 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7586 opd = get_opd_info (sym_sec);
7587 if (opd != NULL && opd->adjust != NULL)
754021d0 7588 {
51aecdc5 7589 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7590 if (adjust == -1)
7591 {
7592 /* This entry has been deleted. */
b3fac117 7593 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7594 if (dsec == NULL)
7595 {
7596 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7597 if (discarded_section (dsec))
81688140 7598 {
b3fac117 7599 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7600 break;
7601 }
7602 }
4025353c 7603 eh->elf.root.u.def.value = 0;
81688140 7604 eh->elf.root.u.def.section = dsec;
4025353c
AM
7605 }
7606 else
7607 eh->elf.root.u.def.value += adjust;
754021d0
AM
7608 eh->adjust_done = 1;
7609 }
7610 return TRUE;
7611}
7612
8c1d1bb8 7613/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7614 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7615 have already been determined. */
7616
7617static bfd_boolean
7618dec_dynrel_count (bfd_vma r_info,
7619 asection *sec,
7620 struct bfd_link_info *info,
7621 Elf_Internal_Sym **local_syms,
7622 struct elf_link_hash_entry *h,
19e08130 7623 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7624{
7625 enum elf_ppc64_reloc_type r_type;
19e08130 7626 asection *sym_sec = NULL;
8c1d1bb8
AM
7627
7628 /* Can this reloc be dynamic? This switch, and later tests here
7629 should be kept in sync with the code in check_relocs. */
7630 r_type = ELF64_R_TYPE (r_info);
7631 switch (r_type)
7632 {
7633 default:
7634 return TRUE;
7635
7636 case R_PPC64_TPREL16:
7637 case R_PPC64_TPREL16_LO:
7638 case R_PPC64_TPREL16_HI:
7639 case R_PPC64_TPREL16_HA:
7640 case R_PPC64_TPREL16_DS:
7641 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7642 case R_PPC64_TPREL16_HIGH:
7643 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7644 case R_PPC64_TPREL16_HIGHER:
7645 case R_PPC64_TPREL16_HIGHERA:
7646 case R_PPC64_TPREL16_HIGHEST:
7647 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7648 case R_PPC64_TPREL64:
7649 case R_PPC64_DTPMOD64:
7650 case R_PPC64_DTPREL64:
7651 case R_PPC64_ADDR64:
7652 case R_PPC64_REL30:
7653 case R_PPC64_REL32:
7654 case R_PPC64_REL64:
7655 case R_PPC64_ADDR14:
7656 case R_PPC64_ADDR14_BRNTAKEN:
7657 case R_PPC64_ADDR14_BRTAKEN:
7658 case R_PPC64_ADDR16:
7659 case R_PPC64_ADDR16_DS:
7660 case R_PPC64_ADDR16_HA:
7661 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7662 case R_PPC64_ADDR16_HIGH:
7663 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7664 case R_PPC64_ADDR16_HIGHER:
7665 case R_PPC64_ADDR16_HIGHERA:
7666 case R_PPC64_ADDR16_HIGHEST:
7667 case R_PPC64_ADDR16_HIGHESTA:
7668 case R_PPC64_ADDR16_LO:
7669 case R_PPC64_ADDR16_LO_DS:
7670 case R_PPC64_ADDR24:
7671 case R_PPC64_ADDR32:
7672 case R_PPC64_UADDR16:
7673 case R_PPC64_UADDR32:
7674 case R_PPC64_UADDR64:
7675 case R_PPC64_TOC:
7676 break;
7677 }
7678
7679 if (local_syms != NULL)
7680 {
7681 unsigned long r_symndx;
8c1d1bb8
AM
7682 bfd *ibfd = sec->owner;
7683
7684 r_symndx = ELF64_R_SYM (r_info);
7685 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7686 return FALSE;
7687 }
7688
0e1862bb 7689 if ((bfd_link_pic (info)
1d483afe 7690 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7691 || (h != NULL
198f1157 7692 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7693 || h->root.type == bfd_link_hash_defweak
7694 || !h->def_regular))))
7695 || (ELIMINATE_COPY_RELOCS
0e1862bb 7696 && !bfd_link_pic (info)
8c1d1bb8
AM
7697 && h != NULL
7698 && (h->root.type == bfd_link_hash_defweak
7699 || !h->def_regular)))
7700 ;
7701 else
7702 return TRUE;
7703
7704 if (h != NULL)
6edfbbad 7705 {
19e08130
AM
7706 struct elf_dyn_relocs *p;
7707 struct elf_dyn_relocs **pp;
7708 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7709
7710 /* elf_gc_sweep may have already removed all dyn relocs associated
7711 with local syms for a given section. Also, symbol flags are
7712 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7713 report a dynreloc miscount. */
7714 if (*pp == NULL && info->gc_sections)
7715 return TRUE;
7716
7717 while ((p = *pp) != NULL)
60124e18 7718 {
19e08130
AM
7719 if (p->sec == sec)
7720 {
7721 if (!must_be_dyn_reloc (info, r_type))
7722 p->pc_count -= 1;
7723 p->count -= 1;
7724 if (p->count == 0)
7725 *pp = p->next;
7726 return TRUE;
7727 }
7728 pp = &p->next;
60124e18 7729 }
6edfbbad 7730 }
19e08130
AM
7731 else
7732 {
7733 struct ppc_dyn_relocs *p;
7734 struct ppc_dyn_relocs **pp;
7735 void *vpp;
7736 bfd_boolean is_ifunc;
8c1d1bb8 7737
19e08130
AM
7738 if (local_syms == NULL)
7739 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7740 if (sym_sec == NULL)
7741 sym_sec = sec;
c57da1a7 7742
19e08130
AM
7743 vpp = &elf_section_data (sym_sec)->local_dynrel;
7744 pp = (struct ppc_dyn_relocs **) vpp;
7745
7746 if (*pp == NULL && info->gc_sections)
7747 return TRUE;
7748
7749 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7750 while ((p = *pp) != NULL)
8c1d1bb8 7751 {
19e08130
AM
7752 if (p->sec == sec && p->ifunc == is_ifunc)
7753 {
7754 p->count -= 1;
7755 if (p->count == 0)
7756 *pp = p->next;
7757 return TRUE;
7758 }
7759 pp = &p->next;
8c1d1bb8 7760 }
8c1d1bb8
AM
7761 }
7762
695344c0 7763 /* xgettext:c-format */
8de848d8 7764 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7765 sec->owner, sec);
8c1d1bb8
AM
7766 bfd_set_error (bfd_error_bad_value);
7767 return FALSE;
7768}
7769
754021d0
AM
7770/* Remove unused Official Procedure Descriptor entries. Currently we
7771 only remove those associated with functions in discarded link-once
7772 sections, or weakly defined functions that have been overridden. It
7773 would be possible to remove many more entries for statically linked
7774 applications. */
7775
b34976b6 7776bfd_boolean
e7d1c40c 7777ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7778{
7779 bfd *ibfd;
754021d0 7780 bfd_boolean some_edited = FALSE;
3f764659 7781 asection *need_pad = NULL;
e7d1c40c
AM
7782 struct ppc_link_hash_table *htab;
7783
7784 htab = ppc_hash_table (info);
7785 if (htab == NULL)
7786 return FALSE;
1e2f5b6e 7787
c72f2fb2 7788 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7789 {
7790 asection *sec;
7791 Elf_Internal_Rela *relstart, *rel, *relend;
7792 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7793 Elf_Internal_Sym *local_syms;
74f0fb50 7794 struct _opd_sec_data *opd;
51aecdc5 7795 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7796 bfd_size_type cnt_16b = 0;
1e2f5b6e 7797
854b41e7
AM
7798 if (!is_ppc64_elf (ibfd))
7799 continue;
7800
1e2f5b6e 7801 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7802 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7803 continue;
7804
dbaa2011 7805 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7806 continue;
7807
1e2f5b6e
AM
7808 if (sec->output_section == bfd_abs_section_ptr)
7809 continue;
7810
7811 /* Look through the section relocs. */
7812 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7813 continue;
7814
6cdc0ccc 7815 local_syms = NULL;
0ffa91dd 7816 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7817
7818 /* Read the relocations. */
4ce794b7 7819 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7820 info->keep_memory);
1e2f5b6e 7821 if (relstart == NULL)
b34976b6 7822 return FALSE;
1e2f5b6e
AM
7823
7824 /* First run through the relocs to check they are sane, and to
7825 determine whether we need to edit this opd section. */
b34976b6 7826 need_edit = FALSE;
51aecdc5 7827 broken = FALSE;
3f764659 7828 need_pad = sec;
1e2f5b6e 7829 relend = relstart + sec->reloc_count;
50bc7936 7830 for (rel = relstart; rel < relend; )
1e2f5b6e 7831 {
04c9666a 7832 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7833 unsigned long r_symndx;
7834 asection *sym_sec;
7835 struct elf_link_hash_entry *h;
7836 Elf_Internal_Sym *sym;
51aecdc5 7837 bfd_vma offset;
1e2f5b6e 7838
51aecdc5 7839 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7840 only interested in the reloc pointing to a function entry
7841 point. */
51aecdc5
AM
7842 offset = rel->r_offset;
7843 if (rel + 1 == relend
7844 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7845 {
7846 /* If someone messes with .opd alignment then after a
7847 "ld -r" we might have padding in the middle of .opd.
7848 Also, there's nothing to prevent someone putting
7849 something silly in .opd with the assembler. No .opd
b34976b6 7850 optimization for them! */
3f764659 7851 broken_opd:
4eca0228 7852 _bfd_error_handler
d003868e 7853 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7854 broken = TRUE;
1e2f5b6e
AM
7855 break;
7856 }
7857
50bc7936
AM
7858 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7859 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7860 {
4eca0228 7861 _bfd_error_handler
695344c0 7862 /* xgettext:c-format */
d003868e
AM
7863 (_("%B: unexpected reloc type %u in .opd section"),
7864 ibfd, r_type);
51aecdc5 7865 broken = TRUE;
50bc7936
AM
7866 break;
7867 }
7868
1e2f5b6e 7869 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7870 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7871 r_symndx, ibfd))
50bc7936 7872 goto error_ret;
1e2f5b6e
AM
7873
7874 if (sym_sec == NULL || sym_sec->owner == NULL)
7875 {
411e1bfb
AM
7876 const char *sym_name;
7877 if (h != NULL)
7878 sym_name = h->root.root.string;
7879 else
26c61ae5
L
7880 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7881 sym_sec);
411e1bfb 7882
4eca0228 7883 _bfd_error_handler
695344c0 7884 /* xgettext:c-format */
d003868e
AM
7885 (_("%B: undefined sym `%s' in .opd section"),
7886 ibfd, sym_name);
51aecdc5 7887 broken = TRUE;
1e2f5b6e
AM
7888 break;
7889 }
7890
51020317
AM
7891 /* opd entries are always for functions defined in the
7892 current input bfd. If the symbol isn't defined in the
7893 input bfd, then we won't be using the function in this
7894 bfd; It must be defined in a linkonce section in another
7895 bfd, or is weak. It's also possible that we are
7896 discarding the function due to a linker script /DISCARD/,
7897 which we test for via the output_section. */
7898 if (sym_sec->owner != ibfd
7899 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7900 need_edit = TRUE;
1e2f5b6e 7901
50bc7936 7902 rel += 2;
51aecdc5
AM
7903 if (rel + 1 == relend
7904 || (rel + 2 < relend
7905 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7906 ++rel;
7907
7908 if (rel == relend)
3f764659
JJ
7909 {
7910 if (sec->size == offset + 24)
7911 {
7912 need_pad = NULL;
7913 break;
7914 }
51aecdc5 7915 if (sec->size == offset + 16)
3f764659
JJ
7916 {
7917 cnt_16b++;
7918 break;
7919 }
7920 goto broken_opd;
7921 }
3f764659
JJ
7922 else if (rel + 1 < relend
7923 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7924 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7925 {
51aecdc5
AM
7926 if (rel[0].r_offset == offset + 16)
7927 cnt_16b++;
7928 else if (rel[0].r_offset != offset + 24)
7929 goto broken_opd;
3f764659
JJ
7930 }
7931 else
7932 goto broken_opd;
1e2f5b6e
AM
7933 }
7934
e7d1c40c 7935 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7936
51aecdc5 7937 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7938 {
7939 Elf_Internal_Rela *write_rel;
d4730f92 7940 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7941 bfd_byte *rptr, *wptr;
983bddc8 7942 bfd_byte *new_contents;
74f0fb50
AM
7943 bfd_size_type amt;
7944
983bddc8 7945 new_contents = NULL;
51aecdc5 7946 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7947 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7948 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7949 if (opd->adjust == NULL)
7950 return FALSE;
1e2f5b6e
AM
7951
7952 /* This seems a waste of time as input .opd sections are all
7953 zeros as generated by gcc, but I suppose there's no reason
7954 this will always be so. We might start putting something in
7955 the third word of .opd entries. */
7956 if ((sec->flags & SEC_IN_MEMORY) == 0)
7957 {
eea6121a
AM
7958 bfd_byte *loc;
7959 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7960 {
eea6121a
AM
7961 if (loc != NULL)
7962 free (loc);
50bc7936 7963 error_ret:
6cdc0ccc
AM
7964 if (local_syms != NULL
7965 && symtab_hdr->contents != (unsigned char *) local_syms)
7966 free (local_syms);
6cdc0ccc
AM
7967 if (elf_section_data (sec)->relocs != relstart)
7968 free (relstart);
b34976b6 7969 return FALSE;
6cdc0ccc 7970 }
1e2f5b6e
AM
7971 sec->contents = loc;
7972 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7973 }
7974
7975 elf_section_data (sec)->relocs = relstart;
7976
3f764659 7977 new_contents = sec->contents;
3f764659
JJ
7978 if (add_aux_fields)
7979 {
7980 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7981 if (new_contents == NULL)
7982 return FALSE;
51aecdc5 7983 need_pad = NULL;
3f764659 7984 }
b4f4e59f
AM
7985 wptr = new_contents;
7986 rptr = sec->contents;
1e2f5b6e 7987 write_rel = relstart;
51aecdc5 7988 for (rel = relstart; rel < relend; )
1e2f5b6e 7989 {
50bc7936
AM
7990 unsigned long r_symndx;
7991 asection *sym_sec;
7992 struct elf_link_hash_entry *h;
51aecdc5 7993 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7994 Elf_Internal_Sym *sym;
51aecdc5
AM
7995 long opd_ent_size;
7996 Elf_Internal_Rela *next_rel;
7997 bfd_boolean skip;
50bc7936
AM
7998
7999 r_symndx = ELF64_R_SYM (rel->r_info);
8000 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8001 r_symndx, ibfd))
50bc7936
AM
8002 goto error_ret;
8003
51aecdc5
AM
8004 next_rel = rel + 2;
8005 if (next_rel + 1 == relend
8006 || (next_rel + 2 < relend
8007 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8008 ++next_rel;
8009
8010 /* See if the .opd entry is full 24 byte or
8011 16 byte (with fd_aux entry overlapped with next
8012 fd_func). */
8013 opd_ent_size = 24;
8014 if (next_rel == relend)
1e2f5b6e 8015 {
51aecdc5 8016 if (sec->size == rel->r_offset + 16)
3f764659 8017 opd_ent_size = 16;
51aecdc5
AM
8018 }
8019 else if (next_rel->r_offset == rel->r_offset + 16)
8020 opd_ent_size = 16;
3f764659 8021
51aecdc5
AM
8022 if (h != NULL
8023 && h->root.root.string[0] == '.')
8024 {
8c5b4e52
AM
8025 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8026 if (fdh != NULL)
8027 {
8028 fdh = ppc_follow_link (fdh);
8029 if (fdh->elf.root.type != bfd_link_hash_defined
8030 && fdh->elf.root.type != bfd_link_hash_defweak)
8031 fdh = NULL;
8032 }
51aecdc5 8033 }
1e2f5b6e 8034
51aecdc5
AM
8035 skip = (sym_sec->owner != ibfd
8036 || sym_sec->output_section == bfd_abs_section_ptr);
8037 if (skip)
8038 {
8039 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8040 {
51aecdc5
AM
8041 /* Arrange for the function descriptor sym
8042 to be dropped. */
8043 fdh->elf.root.u.def.value = 0;
8044 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8045 }
51aecdc5 8046 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8047
0e1862bb 8048 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8049 rel = next_rel;
8050 else
8051 while (1)
8052 {
8053 if (!dec_dynrel_count (rel->r_info, sec, info,
8054 NULL, h, sym))
8055 goto error_ret;
754021d0 8056
51aecdc5
AM
8057 if (++rel == next_rel)
8058 break;
1e2f5b6e 8059
51aecdc5
AM
8060 r_symndx = ELF64_R_SYM (rel->r_info);
8061 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8062 r_symndx, ibfd))
8063 goto error_ret;
8064 }
50bc7936
AM
8065 }
8066 else
1e2f5b6e 8067 {
51aecdc5
AM
8068 /* We'll be keeping this opd entry. */
8069 long adjust;
8070
8071 if (fdh != NULL)
8072 {
8073 /* Redefine the function descriptor symbol to
8074 this location in the opd section. It is
8075 necessary to update the value here rather
8076 than using an array of adjustments as we do
8077 for local symbols, because various places
8078 in the generic ELF code use the value
8079 stored in u.def.value. */
8080 fdh->elf.root.u.def.value = wptr - new_contents;
8081 fdh->adjust_done = 1;
8082 }
8083
8084 /* Local syms are a bit tricky. We could
8085 tweak them as they can be cached, but
8086 we'd need to look through the local syms
8087 for the function descriptor sym which we
8088 don't have at the moment. So keep an
8089 array of adjustments. */
8090 adjust = (wptr - new_contents) - (rptr - sec->contents);
8091 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8092
8093 if (wptr != rptr)
8094 memcpy (wptr, rptr, opd_ent_size);
8095 wptr += opd_ent_size;
8096 if (add_aux_fields && opd_ent_size == 16)
8097 {
8098 memset (wptr, '\0', 8);
8099 wptr += 8;
8100 }
8101
50bc7936 8102 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8103 new opd entries. */
8104 for ( ; rel != next_rel; ++rel)
8105 {
8106 rel->r_offset += adjust;
8107 if (write_rel != rel)
8108 memcpy (write_rel, rel, sizeof (*rel));
8109 ++write_rel;
8110 }
1e2f5b6e 8111 }
51aecdc5
AM
8112
8113 rptr += opd_ent_size;
1e2f5b6e
AM
8114 }
8115
3f764659 8116 sec->size = wptr - new_contents;
1e2f5b6e 8117 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8118 if (add_aux_fields)
8119 {
8120 free (sec->contents);
8121 sec->contents = new_contents;
8122 }
8123
05bf9422 8124 /* Fudge the header size too, as this is used later in
cdcf6e38 8125 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8126 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8127 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8128 some_edited = TRUE;
1e2f5b6e 8129 }
6cdc0ccc 8130 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8131 free (relstart);
6cdc0ccc 8132
411e1bfb
AM
8133 if (local_syms != NULL
8134 && symtab_hdr->contents != (unsigned char *) local_syms)
8135 {
8136 if (!info->keep_memory)
8137 free (local_syms);
8138 else
8139 symtab_hdr->contents = (unsigned char *) local_syms;
8140 }
8141 }
8142
754021d0
AM
8143 if (some_edited)
8144 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8145
3f764659
JJ
8146 /* If we are doing a final link and the last .opd entry is just 16 byte
8147 long, add a 8 byte padding after it. */
0e1862bb 8148 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8149 {
8150 bfd_byte *p;
8151
8152 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8153 {
8154 BFD_ASSERT (need_pad->size > 0);
8155
8156 p = bfd_malloc (need_pad->size + 8);
8157 if (p == NULL)
8158 return FALSE;
699733f6 8159
3f764659
JJ
8160 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8161 p, 0, need_pad->size))
8162 return FALSE;
8163
8164 need_pad->contents = p;
8165 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8166 }
8167 else
8168 {
8169 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8170 if (p == NULL)
8171 return FALSE;
8172
8173 need_pad->contents = p;
8174 }
8175
8176 memset (need_pad->contents + need_pad->size, 0, 8);
8177 need_pad->size += 8;
8178 }
8179
411e1bfb
AM
8180 return TRUE;
8181}
8182
e1918d23 8183/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8184
e1918d23 8185asection *
e7d1c40c 8186ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8187{
411e1bfb
AM
8188 struct ppc_link_hash_table *htab;
8189
411e1bfb 8190 htab = ppc_hash_table (info);
4dfe6ac6
NC
8191 if (htab == NULL)
8192 return NULL;
8193
ee67d69a
AM
8194 if (abiversion (info->output_bfd) == 1)
8195 htab->opd_abi = 1;
8196
e7d1c40c 8197 if (htab->params->no_multi_toc)
33c0ec9d
AM
8198 htab->do_multi_toc = 0;
8199 else if (!htab->do_multi_toc)
e7d1c40c 8200 htab->params->no_multi_toc = 1;
33c0ec9d 8201
8b5f1ed8
AM
8202 /* Default to --no-plt-localentry, as this option can cause problems
8203 with symbol interposition. For example, glibc libpthread.so and
8204 libc.so duplicate many pthread symbols, with a fallback
8205 implementation in libc.so. In some cases the fallback does more
8206 work than the pthread implementation. __pthread_condattr_destroy
8207 is one such symbol: the libpthread.so implementation is
8208 localentry:0 while the libc.so implementation is localentry:8.
8209 An app that "cleverly" uses dlopen to only load necessary
8210 libraries at runtime may omit loading libpthread.so when not
8211 running multi-threaded, which then results in the libc.so
8212 fallback symbols being used and ld.so complaining. Now there
8213 are workarounds in ld (see non_zero_localentry) to detect the
8214 pthread situation, but that may not be the only case where
8215 --plt-localentry can cause trouble. */
f378ab09 8216 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8217 htab->params->plt_localentry0 = 0;
d44c746a
AM
8218 if (htab->params->plt_localentry0
8219 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8220 FALSE, FALSE, FALSE) == NULL)
8221 info->callbacks->einfo
8222 (_("%P: warning: --plt-localentry is especially dangerous without "
8223 "ld.so support to detect ABI violations.\n"));
f378ab09 8224
3a71aa26
AM
8225 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8226 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8227 FALSE, FALSE, TRUE));
a7f2871e
AM
8228 /* Move dynamic linking info to the function descriptor sym. */
8229 if (htab->tls_get_addr != NULL)
8230 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8231 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8232 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8233 FALSE, FALSE, TRUE));
7c9cf415 8234 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8235 {
8236 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8237
8238 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8239 FALSE, FALSE, TRUE);
8240 if (opt != NULL)
8241 func_desc_adjust (opt, info);
8242 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8243 FALSE, FALSE, TRUE);
8244 if (opt_fd != NULL
8245 && (opt_fd->root.type == bfd_link_hash_defined
8246 || opt_fd->root.type == bfd_link_hash_defweak))
8247 {
8248 /* If glibc supports an optimized __tls_get_addr call stub,
8249 signalled by the presence of __tls_get_addr_opt, and we'll
8250 be calling __tls_get_addr via a plt call stub, then
8251 make __tls_get_addr point to __tls_get_addr_opt. */
8252 tga_fd = &htab->tls_get_addr_fd->elf;
8253 if (htab->elf.dynamic_sections_created
8254 && tga_fd != NULL
8255 && (tga_fd->type == STT_FUNC
8256 || tga_fd->needs_plt)
8257 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8258 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8259 {
8260 struct plt_entry *ent;
8261
8262 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8263 if (ent->plt.refcount > 0)
8264 break;
8265 if (ent != NULL)
8266 {
8267 tga_fd->root.type = bfd_link_hash_indirect;
8268 tga_fd->root.u.i.link = &opt_fd->root;
8269 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8270 opt_fd->mark = 1;
a7f2871e
AM
8271 if (opt_fd->dynindx != -1)
8272 {
8273 /* Use __tls_get_addr_opt in dynamic relocations. */
8274 opt_fd->dynindx = -1;
8275 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8276 opt_fd->dynstr_index);
8277 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8278 return NULL;
a7f2871e
AM
8279 }
8280 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8281 tga = &htab->tls_get_addr->elf;
8282 if (opt != NULL && tga != NULL)
8283 {
8284 tga->root.type = bfd_link_hash_indirect;
8285 tga->root.u.i.link = &opt->root;
8286 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8287 opt->mark = 1;
a7f2871e
AM
8288 _bfd_elf_link_hash_hide_symbol (info, opt,
8289 tga->forced_local);
8290 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8291 }
8292 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8293 htab->tls_get_addr_fd->is_func_descriptor = 1;
8294 if (htab->tls_get_addr != NULL)
8295 {
8296 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8297 htab->tls_get_addr->is_func = 1;
8298 }
8299 }
8300 }
8301 }
7c9cf415
AM
8302 else if (htab->params->tls_get_addr_opt < 0)
8303 htab->params->tls_get_addr_opt = 0;
a7f2871e 8304 }
33c0ec9d 8305 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8306}
8387904d 8307
3a71aa26
AM
8308/* Return TRUE iff REL is a branch reloc with a global symbol matching
8309 HASH1 or HASH2. */
8387904d 8310
3a71aa26
AM
8311static bfd_boolean
8312branch_reloc_hash_match (const bfd *ibfd,
8313 const Elf_Internal_Rela *rel,
8314 const struct ppc_link_hash_entry *hash1,
8315 const struct ppc_link_hash_entry *hash2)
8316{
8317 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8318 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8319 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8320
e054468f 8321 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8322 {
3a71aa26
AM
8323 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8324 struct elf_link_hash_entry *h;
8387904d 8325
3a71aa26 8326 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8327 h = elf_follow_link (h);
3a71aa26
AM
8328 if (h == &hash1->elf || h == &hash2->elf)
8329 return TRUE;
a48ebf4d 8330 }
3a71aa26 8331 return FALSE;
951fd09b 8332}
411e1bfb 8333
951fd09b
AM
8334/* Run through all the TLS relocs looking for optimization
8335 opportunities. The linker has been hacked (see ppc64elf.em) to do
8336 a preliminary section layout so that we know the TLS segment
8337 offsets. We can't optimize earlier because some optimizations need
8338 to know the tp offset, and we need to optimize before allocating
8339 dynamic relocations. */
8340
8341bfd_boolean
33c0ec9d 8342ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8343{
8344 bfd *ibfd;
8345 asection *sec;
8346 struct ppc_link_hash_table *htab;
663a1470 8347 unsigned char *toc_ref;
102890f0 8348 int pass;
951fd09b 8349
3cbc1e5e 8350 if (!bfd_link_executable (info))
411e1bfb
AM
8351 return TRUE;
8352
951fd09b 8353 htab = ppc_hash_table (info);
4dfe6ac6
NC
8354 if (htab == NULL)
8355 return FALSE;
8356
663a1470
AM
8357 /* Make two passes over the relocs. On the first pass, mark toc
8358 entries involved with tls relocs, and check that tls relocs
8359 involved in setting up a tls_get_addr call are indeed followed by
8360 such a call. If they are not, we can't do any tls optimization.
8361 On the second pass twiddle tls_mask flags to notify
8362 relocate_section that optimization can be done, and adjust got
8363 and plt refcounts. */
8364 toc_ref = NULL;
8365 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8366 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8367 {
8368 Elf_Internal_Sym *locsyms = NULL;
8369 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8370
102890f0
AM
8371 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8372 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8373 {
8374 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8375 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8376
102890f0
AM
8377 /* Read the relocations. */
8378 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8379 info->keep_memory);
8380 if (relstart == NULL)
2915c55b
JK
8381 {
8382 free (toc_ref);
8383 return FALSE;
8384 }
411e1bfb 8385
102890f0
AM
8386 relend = relstart + sec->reloc_count;
8387 for (rel = relstart; rel < relend; rel++)
8388 {
8389 enum elf_ppc64_reloc_type r_type;
8390 unsigned long r_symndx;
8391 struct elf_link_hash_entry *h;
8392 Elf_Internal_Sym *sym;
8393 asection *sym_sec;
f961d9dd
AM
8394 unsigned char *tls_mask;
8395 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8396 bfd_vma value;
8397 bfd_boolean ok_tprel, is_local;
8398 long toc_ref_index = 0;
8399 int expecting_tls_get_addr = 0;
663a1470 8400 bfd_boolean ret = FALSE;
411e1bfb 8401
102890f0
AM
8402 r_symndx = ELF64_R_SYM (rel->r_info);
8403 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8404 r_symndx, ibfd))
8405 {
8406 err_free_rel:
8407 if (elf_section_data (sec)->relocs != relstart)
8408 free (relstart);
8409 if (toc_ref != NULL)
8410 free (toc_ref);
8411 if (locsyms != NULL
0ffa91dd 8412 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8413 != (unsigned char *) locsyms))
8414 free (locsyms);
663a1470 8415 return ret;
102890f0 8416 }
411e1bfb 8417
102890f0
AM
8418 if (h != NULL)
8419 {
766bc656
AM
8420 if (h->root.type == bfd_link_hash_defined
8421 || h->root.type == bfd_link_hash_defweak)
8422 value = h->root.u.def.value;
8423 else if (h->root.type == bfd_link_hash_undefweak)
8424 value = 0;
8425 else
663a1470
AM
8426 {
8427 found_tls_get_addr_arg = 0;
8428 continue;
8429 }
102890f0
AM
8430 }
8431 else
8432 /* Symbols referenced by TLS relocs must be of type
8433 STT_TLS. So no need for .opd local sym adjust. */
8434 value = sym->st_value;
8435
8436 ok_tprel = FALSE;
8437 is_local = FALSE;
8438 if (h == NULL
8439 || !h->def_dynamic)
8440 {
8441 is_local = TRUE;
766bc656
AM
8442 if (h != NULL
8443 && h->root.type == bfd_link_hash_undefweak)
8444 ok_tprel = TRUE;
c27b8c2a
AM
8445 else if (sym_sec != NULL
8446 && sym_sec->output_section != NULL)
766bc656
AM
8447 {
8448 value += sym_sec->output_offset;
8449 value += sym_sec->output_section->vma;
8450 value -= htab->elf.tls_sec->vma;
8451 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8452 < (bfd_vma) 1 << 32);
8453 }
102890f0 8454 }
951fd09b 8455
102890f0 8456 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8457 /* If this section has old-style __tls_get_addr calls
8458 without marker relocs, then check that each
8459 __tls_get_addr call reloc is preceded by a reloc
8460 that conceivably belongs to the __tls_get_addr arg
8461 setup insn. If we don't find matching arg setup
8462 relocs, don't do any tls optimization. */
8463 if (pass == 0
8464 && sec->has_tls_get_addr_call
8465 && h != NULL
8466 && (h == &htab->tls_get_addr->elf
8467 || h == &htab->tls_get_addr_fd->elf)
8468 && !found_tls_get_addr_arg
8469 && is_branch_reloc (r_type))
8470 {
25f53a85 8471 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8472 "TLS optimization disabled\n"),
8473 ibfd, sec, rel->r_offset);
8474 ret = TRUE;
8475 goto err_free_rel;
8476 }
8477
8478 found_tls_get_addr_arg = 0;
102890f0
AM
8479 switch (r_type)
8480 {
8481 case R_PPC64_GOT_TLSLD16:
8482 case R_PPC64_GOT_TLSLD16_LO:
8483 expecting_tls_get_addr = 1;
663a1470 8484 found_tls_get_addr_arg = 1;
1a0670f3 8485 /* Fall through. */
102890f0
AM
8486
8487 case R_PPC64_GOT_TLSLD16_HI:
8488 case R_PPC64_GOT_TLSLD16_HA:
8489 /* These relocs should never be against a symbol
8490 defined in a shared lib. Leave them alone if
8491 that turns out to be the case. */
8492 if (!is_local)
8493 continue;
411e1bfb 8494
102890f0 8495 /* LD -> LE */
411e1bfb 8496 tls_set = 0;
102890f0
AM
8497 tls_clear = TLS_LD;
8498 tls_type = TLS_TLS | TLS_LD;
8499 break;
411e1bfb 8500
102890f0
AM
8501 case R_PPC64_GOT_TLSGD16:
8502 case R_PPC64_GOT_TLSGD16_LO:
8503 expecting_tls_get_addr = 1;
663a1470 8504 found_tls_get_addr_arg = 1;
1a0670f3 8505 /* Fall through. */
102890f0
AM
8506
8507 case R_PPC64_GOT_TLSGD16_HI:
8508 case R_PPC64_GOT_TLSGD16_HA:
8509 if (ok_tprel)
8510 /* GD -> LE */
411e1bfb 8511 tls_set = 0;
102890f0
AM
8512 else
8513 /* GD -> IE */
8514 tls_set = TLS_TLS | TLS_TPRELGD;
8515 tls_clear = TLS_GD;
8516 tls_type = TLS_TLS | TLS_GD;
8517 break;
8518
8519 case R_PPC64_GOT_TPREL16_DS:
8520 case R_PPC64_GOT_TPREL16_LO_DS:
8521 case R_PPC64_GOT_TPREL16_HI:
8522 case R_PPC64_GOT_TPREL16_HA:
8523 if (ok_tprel)
8524 {
8525 /* IE -> LE */
8526 tls_set = 0;
8527 tls_clear = TLS_TPREL;
8528 tls_type = TLS_TLS | TLS_TPREL;
8529 break;
8530 }
411e1bfb
AM
8531 continue;
8532
727fc41e
AM
8533 case R_PPC64_TLSGD:
8534 case R_PPC64_TLSLD:
663a1470 8535 found_tls_get_addr_arg = 1;
1a0670f3 8536 /* Fall through. */
663a1470
AM
8537
8538 case R_PPC64_TLS:
8539 case R_PPC64_TOC16:
8540 case R_PPC64_TOC16_LO:
102890f0
AM
8541 if (sym_sec == NULL || sym_sec != toc)
8542 continue;
8543
8544 /* Mark this toc entry as referenced by a TLS
8545 code sequence. We can do that now in the
8546 case of R_PPC64_TLS, and after checking for
8547 tls_get_addr for the TOC16 relocs. */
8548 if (toc_ref == NULL)
663a1470
AM
8549 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8550 if (toc_ref == NULL)
8551 goto err_free_rel;
8552
102890f0
AM
8553 if (h != NULL)
8554 value = h->root.u.def.value;
8555 else
8556 value = sym->st_value;
8557 value += rel->r_addend;
73242275
AM
8558 if (value % 8 != 0)
8559 continue;
8560 BFD_ASSERT (value < toc->size
8561 && toc->output_offset % 8 == 0);
663a1470 8562 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8563 if (r_type == R_PPC64_TLS
8564 || r_type == R_PPC64_TLSGD
8565 || r_type == R_PPC64_TLSLD)
102890f0
AM
8566 {
8567 toc_ref[toc_ref_index] = 1;
8568 continue;
8569 }
8570
8571 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8572 continue;
8573
8574 tls_set = 0;
8575 tls_clear = 0;
8576 expecting_tls_get_addr = 2;
8577 break;
8578
8579 case R_PPC64_TPREL64:
8580 if (pass == 0
8581 || sec != toc
8582 || toc_ref == NULL
663a1470 8583 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8584 continue;
8585 if (ok_tprel)
8586 {
8587 /* IE -> LE */
8588 tls_set = TLS_EXPLICIT;
8589 tls_clear = TLS_TPREL;
8590 break;
8591 }
8592 continue;
8593
8594 case R_PPC64_DTPMOD64:
8595 if (pass == 0
8596 || sec != toc
8597 || toc_ref == NULL
663a1470 8598 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8599 continue;
8600 if (rel + 1 < relend
8601 && (rel[1].r_info
8602 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8603 && rel[1].r_offset == rel->r_offset + 8)
8604 {
8605 if (ok_tprel)
8606 /* GD -> LE */
8607 tls_set = TLS_EXPLICIT | TLS_GD;
8608 else
8609 /* GD -> IE */
8610 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8611 tls_clear = TLS_GD;
8612 }
8613 else
8614 {
8615 if (!is_local)
8616 continue;
8617
8618 /* LD -> LE */
8619 tls_set = TLS_EXPLICIT;
8620 tls_clear = TLS_LD;
8621 }
8622 break;
8623
8624 default:
8625 continue;
8626 }
8627
8628 if (pass == 0)
8629 {
727fc41e
AM
8630 if (!expecting_tls_get_addr
8631 || !sec->has_tls_get_addr_call)
102890f0
AM
8632 continue;
8633
3a71aa26
AM
8634 if (rel + 1 < relend
8635 && branch_reloc_hash_match (ibfd, rel + 1,
8636 htab->tls_get_addr,
8637 htab->tls_get_addr_fd))
102890f0 8638 {
3a71aa26 8639 if (expecting_tls_get_addr == 2)
102890f0 8640 {
3a71aa26 8641 /* Check for toc tls entries. */
f961d9dd 8642 unsigned char *toc_tls;
3a71aa26
AM
8643 int retval;
8644
8645 retval = get_tls_mask (&toc_tls, NULL, NULL,
8646 &locsyms,
8647 rel, ibfd);
8648 if (retval == 0)
8649 goto err_free_rel;
663a1470
AM
8650 if (toc_tls != NULL)
8651 {
8652 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8653 found_tls_get_addr_arg = 1;
8654 if (retval > 1)
8655 toc_ref[toc_ref_index] = 1;
8656 }
102890f0 8657 }
3a71aa26 8658 continue;
102890f0
AM
8659 }
8660
8661 if (expecting_tls_get_addr != 1)
8662 continue;
8663
8664 /* Uh oh, we didn't find the expected call. We
8665 could just mark this symbol to exclude it
8666 from tls optimization but it's safer to skip
663a1470 8667 the entire optimization. */
695344c0 8668 /* xgettext:c-format */
25f53a85 8669 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8670 "TLS optimization disabled\n"),
8671 ibfd, sec, rel->r_offset);
8672 ret = TRUE;
8673 goto err_free_rel;
102890f0
AM
8674 }
8675
85f7a9cb 8676 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8677 {
8678 struct plt_entry *ent;
8679 for (ent = htab->tls_get_addr->elf.plt.plist;
8680 ent != NULL;
8681 ent = ent->next)
8682 if (ent->addend == 0)
411e1bfb 8683 {
102890f0 8684 if (ent->plt.refcount > 0)
30038c59 8685 {
102890f0
AM
8686 ent->plt.refcount -= 1;
8687 expecting_tls_get_addr = 0;
30038c59 8688 }
102890f0 8689 break;
411e1bfb 8690 }
102890f0 8691 }
411e1bfb 8692
85f7a9cb 8693 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8694 {
8695 struct plt_entry *ent;
8696 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8697 ent != NULL;
8698 ent = ent->next)
8699 if (ent->addend == 0)
411e1bfb 8700 {
102890f0
AM
8701 if (ent->plt.refcount > 0)
8702 ent->plt.refcount -= 1;
8703 break;
411e1bfb 8704 }
102890f0 8705 }
411e1bfb 8706
102890f0 8707 if (tls_clear == 0)
30038c59
AM
8708 continue;
8709
102890f0
AM
8710 if ((tls_set & TLS_EXPLICIT) == 0)
8711 {
8712 struct got_entry *ent;
411e1bfb 8713
102890f0
AM
8714 /* Adjust got entry for this reloc. */
8715 if (h != NULL)
8716 ent = h->got.glist;
8717 else
8718 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8719
102890f0
AM
8720 for (; ent != NULL; ent = ent->next)
8721 if (ent->addend == rel->r_addend
8722 && ent->owner == ibfd
8723 && ent->tls_type == tls_type)
8724 break;
8725 if (ent == NULL)
8726 abort ();
411e1bfb 8727
102890f0
AM
8728 if (tls_set == 0)
8729 {
8730 /* We managed to get rid of a got entry. */
8731 if (ent->got.refcount > 0)
8732 ent->got.refcount -= 1;
8733 }
8734 }
8735 else
8736 {
8737 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8738 we'll lose one or two dyn relocs. */
8739 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8740 NULL, h, sym))
102890f0 8741 return FALSE;
411e1bfb 8742
102890f0
AM
8743 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8744 {
8745 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8746 NULL, h, sym))
102890f0
AM
8747 return FALSE;
8748 }
8749 }
411e1bfb 8750
102890f0
AM
8751 *tls_mask |= tls_set;
8752 *tls_mask &= ~tls_clear;
8753 }
8c1d1bb8 8754
102890f0
AM
8755 if (elf_section_data (sec)->relocs != relstart)
8756 free (relstart);
8757 }
411e1bfb 8758
663a1470
AM
8759 if (locsyms != NULL
8760 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8761 {
8762 if (!info->keep_memory)
8763 free (locsyms);
8764 else
8765 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8766 }
8767 }
411e1bfb 8768
663a1470
AM
8769 if (toc_ref != NULL)
8770 free (toc_ref);
9a23f96e 8771 htab->do_tls_opt = 1;
b34976b6 8772 return TRUE;
1e2f5b6e 8773}
b34976b6 8774
c5614fa4
AM
8775/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8776 the values of any global symbols in a toc section that has been
8777 edited. Globals in toc sections should be a rarity, so this function
8778 sets a flag if any are found in toc sections other than the one just
de194d85 8779 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8780
8781struct adjust_toc_info
8782{
8783 asection *toc;
8784 unsigned long *skip;
8785 bfd_boolean global_toc_syms;
8786};
8787
ba761f19
AM
8788enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8789
c5614fa4
AM
8790static bfd_boolean
8791adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8792{
8793 struct ppc_link_hash_entry *eh;
8794 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8795 unsigned long i;
c5614fa4 8796
c5614fa4
AM
8797 if (h->root.type != bfd_link_hash_defined
8798 && h->root.type != bfd_link_hash_defweak)
8799 return TRUE;
8800
8801 eh = (struct ppc_link_hash_entry *) h;
8802 if (eh->adjust_done)
8803 return TRUE;
8804
8805 if (eh->elf.root.u.def.section == toc_inf->toc)
8806 {
854b41e7
AM
8807 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8808 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8809 else
854b41e7
AM
8810 i = eh->elf.root.u.def.value >> 3;
8811
ba761f19 8812 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8813 {
4eca0228 8814 _bfd_error_handler
854b41e7
AM
8815 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8816 do
8817 ++i;
ba761f19 8818 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8819 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8820 }
854b41e7
AM
8821
8822 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8823 eh->adjust_done = 1;
8824 }
8825 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8826 toc_inf->global_toc_syms = TRUE;
8827
8828 return TRUE;
8829}
8830
39eeab25
AM
8831/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8832 on a _LO variety toc/got reloc. */
560c8763
AM
8833
8834static bfd_boolean
39eeab25 8835ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8836{
39eeab25
AM
8837 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8838 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8839 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8840 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8841 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8842 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8843 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8844 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8845 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8846 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8847 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8848 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8849 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8850 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8851 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8852 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8853 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8854 /* Exclude lfqu by testing reloc. If relocs are ever
8855 defined for the reduced D field in psq_lu then those
8856 will need testing too. */
8857 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8858 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8859 && (insn & 1) == 0)
8860 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8861 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8862 /* Exclude stfqu. psq_stu as above for psq_lu. */
8863 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8864 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8865 && (insn & 1) == 0));
560c8763
AM
8866}
8867
c5614fa4
AM
8868/* Examine all relocs referencing .toc sections in order to remove
8869 unused .toc entries. */
8870
8871bfd_boolean
33c0ec9d 8872ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8873{
8874 bfd *ibfd;
8875 struct adjust_toc_info toc_inf;
67f0cbdb 8876 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8877
67f0cbdb 8878 htab->do_toc_opt = 1;
c5614fa4 8879 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8880 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8881 {
8882 asection *toc, *sec;
8883 Elf_Internal_Shdr *symtab_hdr;
8884 Elf_Internal_Sym *local_syms;
425b145b 8885 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8886 unsigned long *skip, *drop;
8887 unsigned char *used;
8888 unsigned char *keep, last, some_unused;
8889
854b41e7
AM
8890 if (!is_ppc64_elf (ibfd))
8891 continue;
8892
c5614fa4
AM
8893 toc = bfd_get_section_by_name (ibfd, ".toc");
8894 if (toc == NULL
92b7a70f 8895 || toc->size == 0
dbaa2011
AM
8896 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8897 || discarded_section (toc))
c5614fa4
AM
8898 continue;
8899
425b145b 8900 toc_relocs = NULL;
c5614fa4 8901 local_syms = NULL;
0ffa91dd 8902 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8903
8904 /* Look at sections dropped from the final link. */
8905 skip = NULL;
8906 relstart = NULL;
8907 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8908 {
8909 if (sec->reloc_count == 0
dbaa2011 8910 || !discarded_section (sec)
c5614fa4
AM
8911 || get_opd_info (sec)
8912 || (sec->flags & SEC_ALLOC) == 0
8913 || (sec->flags & SEC_DEBUGGING) != 0)
8914 continue;
8915
8916 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8917 if (relstart == NULL)
8918 goto error_ret;
8919
8920 /* Run through the relocs to see which toc entries might be
8921 unused. */
8922 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8923 {
8924 enum elf_ppc64_reloc_type r_type;
8925 unsigned long r_symndx;
8926 asection *sym_sec;
8927 struct elf_link_hash_entry *h;
8928 Elf_Internal_Sym *sym;
8929 bfd_vma val;
8930
8931 r_type = ELF64_R_TYPE (rel->r_info);
8932 switch (r_type)
8933 {
8934 default:
8935 continue;
8936
8937 case R_PPC64_TOC16:
8938 case R_PPC64_TOC16_LO:
8939 case R_PPC64_TOC16_HI:
8940 case R_PPC64_TOC16_HA:
8941 case R_PPC64_TOC16_DS:
8942 case R_PPC64_TOC16_LO_DS:
8943 break;
8944 }
8945
8946 r_symndx = ELF64_R_SYM (rel->r_info);
8947 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8948 r_symndx, ibfd))
8949 goto error_ret;
8950
8951 if (sym_sec != toc)
8952 continue;
8953
8954 if (h != NULL)
8955 val = h->root.u.def.value;
8956 else
8957 val = sym->st_value;
8958 val += rel->r_addend;
8959
8960 if (val >= toc->size)
8961 continue;
8962
8963 /* Anything in the toc ought to be aligned to 8 bytes.
8964 If not, don't mark as unused. */
8965 if (val & 7)
8966 continue;
8967
8968 if (skip == NULL)
8969 {
854b41e7 8970 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8971 if (skip == NULL)
8972 goto error_ret;
8973 }
8974
ba761f19 8975 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8976 }
8977
8978 if (elf_section_data (sec)->relocs != relstart)
8979 free (relstart);
8980 }
8981
ba761f19
AM
8982 /* For largetoc loads of address constants, we can convert
8983 . addis rx,2,addr@got@ha
8984 . ld ry,addr@got@l(rx)
8985 to
8986 . addis rx,2,addr@toc@ha
8987 . addi ry,rx,addr@toc@l
8988 when addr is within 2G of the toc pointer. This then means
8989 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8990
ba761f19
AM
8991 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8992 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8993 && toc->reloc_count != 0)
8994 {
8995 /* Read toc relocs. */
425b145b
AM
8996 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8997 info->keep_memory);
8998 if (toc_relocs == NULL)
ba761f19
AM
8999 goto error_ret;
9000
425b145b 9001 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9002 {
9003 enum elf_ppc64_reloc_type r_type;
9004 unsigned long r_symndx;
9005 asection *sym_sec;
9006 struct elf_link_hash_entry *h;
9007 Elf_Internal_Sym *sym;
9008 bfd_vma val, addr;
9009
9010 r_type = ELF64_R_TYPE (rel->r_info);
9011 if (r_type != R_PPC64_ADDR64)
9012 continue;
9013
9014 r_symndx = ELF64_R_SYM (rel->r_info);
9015 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9016 r_symndx, ibfd))
9017 goto error_ret;
9018
425b145b 9019 if (sym_sec == NULL
c27b8c2a 9020 || sym_sec->output_section == NULL
dbaa2011 9021 || discarded_section (sym_sec))
425b145b
AM
9022 continue;
9023
afe397ea 9024 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9025 continue;
9026
9027 if (h != NULL)
bddc25c9
AM
9028 {
9029 if (h->type == STT_GNU_IFUNC)
9030 continue;
9031 val = h->root.u.def.value;
9032 }
ba761f19 9033 else
bddc25c9
AM
9034 {
9035 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9036 continue;
9037 val = sym->st_value;
9038 }
ba761f19
AM
9039 val += rel->r_addend;
9040 val += sym_sec->output_section->vma + sym_sec->output_offset;
9041
9042 /* We don't yet know the exact toc pointer value, but we
9043 know it will be somewhere in the toc section. Don't
9044 optimize if the difference from any possible toc
9045 pointer is outside [ff..f80008000, 7fff7fff]. */
9046 addr = toc->output_section->vma + TOC_BASE_OFF;
9047 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9048 continue;
9049
9050 addr = toc->output_section->vma + toc->output_section->rawsize;
9051 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9052 continue;
9053
9054 if (skip == NULL)
9055 {
9056 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9057 if (skip == NULL)
9058 goto error_ret;
9059 }
9060
9061 skip[rel->r_offset >> 3]
425b145b 9062 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9063 }
ba761f19
AM
9064 }
9065
c5614fa4
AM
9066 if (skip == NULL)
9067 continue;
9068
9069 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9070 if (used == NULL)
9071 {
9072 error_ret:
9073 if (local_syms != NULL
9074 && symtab_hdr->contents != (unsigned char *) local_syms)
9075 free (local_syms);
9076 if (sec != NULL
9077 && relstart != NULL
9078 && elf_section_data (sec)->relocs != relstart)
9079 free (relstart);
425b145b
AM
9080 if (toc_relocs != NULL
9081 && elf_section_data (toc)->relocs != toc_relocs)
9082 free (toc_relocs);
c5614fa4
AM
9083 if (skip != NULL)
9084 free (skip);
9085 return FALSE;
9086 }
9087
30038c59
AM
9088 /* Now check all kept sections that might reference the toc.
9089 Check the toc itself last. */
9090 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9091 : ibfd->sections);
c5614fa4 9092 sec != NULL;
c5614fa4 9093 sec = (sec == toc ? NULL
c5614fa4 9094 : sec->next == NULL ? toc
30038c59 9095 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9096 : sec->next))
9097 {
9098 int repeat;
9099
9100 if (sec->reloc_count == 0
dbaa2011 9101 || discarded_section (sec)
c5614fa4
AM
9102 || get_opd_info (sec)
9103 || (sec->flags & SEC_ALLOC) == 0
9104 || (sec->flags & SEC_DEBUGGING) != 0)
9105 continue;
9106
854b41e7
AM
9107 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9108 info->keep_memory);
c5614fa4 9109 if (relstart == NULL)
2915c55b
JK
9110 {
9111 free (used);
9112 goto error_ret;
9113 }
c5614fa4
AM
9114
9115 /* Mark toc entries referenced as used. */
c5614fa4 9116 do
d4f1ee75
AM
9117 {
9118 repeat = 0;
9119 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9120 {
9121 enum elf_ppc64_reloc_type r_type;
9122 unsigned long r_symndx;
9123 asection *sym_sec;
9124 struct elf_link_hash_entry *h;
9125 Elf_Internal_Sym *sym;
9126 bfd_vma val;
9127 enum {no_check, check_lo, check_ha} insn_check;
98528052 9128
d4f1ee75
AM
9129 r_type = ELF64_R_TYPE (rel->r_info);
9130 switch (r_type)
9131 {
9132 default:
9133 insn_check = no_check;
9134 break;
98528052 9135
d4f1ee75
AM
9136 case R_PPC64_GOT_TLSLD16_HA:
9137 case R_PPC64_GOT_TLSGD16_HA:
9138 case R_PPC64_GOT_TPREL16_HA:
9139 case R_PPC64_GOT_DTPREL16_HA:
9140 case R_PPC64_GOT16_HA:
9141 case R_PPC64_TOC16_HA:
9142 insn_check = check_ha;
9143 break;
98528052 9144
d4f1ee75
AM
9145 case R_PPC64_GOT_TLSLD16_LO:
9146 case R_PPC64_GOT_TLSGD16_LO:
9147 case R_PPC64_GOT_TPREL16_LO_DS:
9148 case R_PPC64_GOT_DTPREL16_LO_DS:
9149 case R_PPC64_GOT16_LO:
9150 case R_PPC64_GOT16_LO_DS:
9151 case R_PPC64_TOC16_LO:
9152 case R_PPC64_TOC16_LO_DS:
9153 insn_check = check_lo;
9154 break;
9155 }
560c8763 9156
d4f1ee75
AM
9157 if (insn_check != no_check)
9158 {
9159 bfd_vma off = rel->r_offset & ~3;
9160 unsigned char buf[4];
9161 unsigned int insn;
c5614fa4 9162
d4f1ee75
AM
9163 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9164 {
9165 free (used);
9166 goto error_ret;
9167 }
9168 insn = bfd_get_32 (ibfd, buf);
9169 if (insn_check == check_lo
39eeab25 9170 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9171 : ((insn & ((0x3f << 26) | 0x1f << 16))
9172 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9173 {
9174 char str[12];
9175
9176 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9177 sprintf (str, "%#08x", insn);
9178 info->callbacks->einfo
695344c0 9179 /* xgettext:c-format */
174d0a74 9180 (_("%H: toc optimization is not supported for"
d4f1ee75
AM
9181 " %s instruction.\n"),
9182 ibfd, sec, rel->r_offset & ~3, str);
9183 }
9184 }
c5614fa4 9185
d4f1ee75
AM
9186 switch (r_type)
9187 {
9188 case R_PPC64_TOC16:
9189 case R_PPC64_TOC16_LO:
9190 case R_PPC64_TOC16_HI:
9191 case R_PPC64_TOC16_HA:
9192 case R_PPC64_TOC16_DS:
9193 case R_PPC64_TOC16_LO_DS:
9194 /* In case we're taking addresses of toc entries. */
9195 case R_PPC64_ADDR64:
9196 break;
c5614fa4 9197
d4f1ee75
AM
9198 default:
9199 continue;
9200 }
c5614fa4 9201
d4f1ee75
AM
9202 r_symndx = ELF64_R_SYM (rel->r_info);
9203 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9204 r_symndx, ibfd))
9205 {
9206 free (used);
9207 goto error_ret;
9208 }
c5614fa4 9209
d4f1ee75
AM
9210 if (sym_sec != toc)
9211 continue;
c5614fa4 9212
d4f1ee75
AM
9213 if (h != NULL)
9214 val = h->root.u.def.value;
9215 else
9216 val = sym->st_value;
9217 val += rel->r_addend;
ba761f19 9218
d4f1ee75
AM
9219 if (val >= toc->size)
9220 continue;
ba761f19 9221
d4f1ee75
AM
9222 if ((skip[val >> 3] & can_optimize) != 0)
9223 {
9224 bfd_vma off;
9225 unsigned char opc;
9226
9227 switch (r_type)
9228 {
9229 case R_PPC64_TOC16_HA:
ba761f19 9230 break;
ba761f19 9231
d4f1ee75
AM
9232 case R_PPC64_TOC16_LO_DS:
9233 off = rel->r_offset;
9234 off += (bfd_big_endian (ibfd) ? -2 : 3);
9235 if (!bfd_get_section_contents (ibfd, sec, &opc,
9236 off, 1))
9237 {
9238 free (used);
9239 goto error_ret;
9240 }
9241 if ((opc & (0x3f << 2)) == (58u << 2))
9242 break;
1a0670f3 9243 /* Fall through. */
ba761f19 9244
d4f1ee75
AM
9245 default:
9246 /* Wrong sort of reloc, or not a ld. We may
9247 as well clear ref_from_discarded too. */
9248 skip[val >> 3] = 0;
9249 }
9250 }
9251
9252 if (sec != toc)
9253 used[val >> 3] = 1;
9254 /* For the toc section, we only mark as used if this
9255 entry itself isn't unused. */
9256 else if ((used[rel->r_offset >> 3]
9257 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9258 && !used[val >> 3])
9259 {
9260 /* Do all the relocs again, to catch reference
9261 chains. */
9262 repeat = 1;
9263 used[val >> 3] = 1;
9264 }
9265 }
9266 }
c5614fa4 9267 while (repeat);
854b41e7
AM
9268
9269 if (elf_section_data (sec)->relocs != relstart)
9270 free (relstart);
c5614fa4
AM
9271 }
9272
9273 /* Merge the used and skip arrays. Assume that TOC
9274 doublewords not appearing as either used or unused belong
de194d85 9275 to an entry more than one doubleword in size. */
c5614fa4
AM
9276 for (drop = skip, keep = used, last = 0, some_unused = 0;
9277 drop < skip + (toc->size + 7) / 8;
9278 ++drop, ++keep)
9279 {
9280 if (*keep)
9281 {
ba761f19
AM
9282 *drop &= ~ref_from_discarded;
9283 if ((*drop & can_optimize) != 0)
9284 some_unused = 1;
c5614fa4
AM
9285 last = 0;
9286 }
b140b010 9287 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9288 {
9289 some_unused = 1;
ba761f19 9290 last = ref_from_discarded;
c5614fa4
AM
9291 }
9292 else
9293 *drop = last;
9294 }
9295
9296 free (used);
9297
9298 if (some_unused)
9299 {
9300 bfd_byte *contents, *src;
9301 unsigned long off;
d62b3684 9302 Elf_Internal_Sym *sym;
ba761f19 9303 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9304
9305 /* Shuffle the toc contents, and at the same time convert the
9306 skip array from booleans into offsets. */
9307 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9308 goto error_ret;
9309
9310 elf_section_data (toc)->this_hdr.contents = contents;
9311
9312 for (src = contents, off = 0, drop = skip;
9313 src < contents + toc->size;
9314 src += 8, ++drop)
9315 {
ba761f19
AM
9316 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9317 off += 8;
c5614fa4
AM
9318 else if (off != 0)
9319 {
9320 *drop = off;
9321 memcpy (src - off, src, 8);
9322 }
9323 }
854b41e7 9324 *drop = off;
c5614fa4
AM
9325 toc->rawsize = toc->size;
9326 toc->size = src - contents - off;
9327
ba761f19
AM
9328 /* Adjust addends for relocs against the toc section sym,
9329 and optimize any accesses we can. */
c5614fa4
AM
9330 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9331 {
9332 if (sec->reloc_count == 0
dbaa2011 9333 || discarded_section (sec))
c5614fa4
AM
9334 continue;
9335
9336 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9337 info->keep_memory);
c5614fa4
AM
9338 if (relstart == NULL)
9339 goto error_ret;
9340
9341 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9342 {
9343 enum elf_ppc64_reloc_type r_type;
9344 unsigned long r_symndx;
9345 asection *sym_sec;
9346 struct elf_link_hash_entry *h;
854b41e7 9347 bfd_vma val;
c5614fa4
AM
9348
9349 r_type = ELF64_R_TYPE (rel->r_info);
9350 switch (r_type)
9351 {
9352 default:
9353 continue;
9354
9355 case R_PPC64_TOC16:
9356 case R_PPC64_TOC16_LO:
9357 case R_PPC64_TOC16_HI:
9358 case R_PPC64_TOC16_HA:
9359 case R_PPC64_TOC16_DS:
9360 case R_PPC64_TOC16_LO_DS:
9361 case R_PPC64_ADDR64:
9362 break;
9363 }
9364
9365 r_symndx = ELF64_R_SYM (rel->r_info);
9366 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9367 r_symndx, ibfd))
9368 goto error_ret;
9369
ba761f19 9370 if (sym_sec != toc)
c5614fa4
AM
9371 continue;
9372
ba761f19
AM
9373 if (h != NULL)
9374 val = h->root.u.def.value;
9375 else
9376 {
9377 val = sym->st_value;
9378 if (val != 0)
9379 local_toc_syms = TRUE;
9380 }
9381
9382 val += rel->r_addend;
854b41e7
AM
9383
9384 if (val > toc->rawsize)
9385 val = toc->rawsize;
ba761f19
AM
9386 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9387 continue;
9388 else if ((skip[val >> 3] & can_optimize) != 0)
9389 {
9390 Elf_Internal_Rela *tocrel
425b145b 9391 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9392 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9393
9394 switch (r_type)
9395 {
9396 case R_PPC64_TOC16_HA:
9397 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9398 break;
9399
9400 case R_PPC64_TOC16_LO_DS:
9401 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9402 break;
9403
9404 default:
28942f62
AM
9405 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9406 ppc_howto_init ();
b140b010 9407 info->callbacks->einfo
695344c0 9408 /* xgettext:c-format */
174d0a74 9409 (_("%H: %s references "
b140b010
AM
9410 "optimized away TOC entry\n"),
9411 ibfd, sec, rel->r_offset,
9412 ppc64_elf_howto_table[r_type]->name);
9413 bfd_set_error (bfd_error_bad_value);
9414 goto error_ret;
ba761f19
AM
9415 }
9416 rel->r_addend = tocrel->r_addend;
9417 elf_section_data (sec)->relocs = relstart;
9418 continue;
9419 }
9420
9421 if (h != NULL || sym->st_value != 0)
9422 continue;
854b41e7
AM
9423
9424 rel->r_addend -= skip[val >> 3];
9425 elf_section_data (sec)->relocs = relstart;
c5614fa4 9426 }
854b41e7
AM
9427
9428 if (elf_section_data (sec)->relocs != relstart)
9429 free (relstart);
c5614fa4
AM
9430 }
9431
9432 /* We shouldn't have local or global symbols defined in the TOC,
9433 but handle them anyway. */
df22d223
AM
9434 if (local_syms != NULL)
9435 for (sym = local_syms;
9436 sym < local_syms + symtab_hdr->sh_info;
9437 ++sym)
9438 if (sym->st_value != 0
9439 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9440 {
9441 unsigned long i;
854b41e7 9442
df22d223
AM
9443 if (sym->st_value > toc->rawsize)
9444 i = toc->rawsize >> 3;
9445 else
9446 i = sym->st_value >> 3;
854b41e7 9447
df22d223
AM
9448 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9449 {
9450 if (local_toc_syms)
4eca0228 9451 _bfd_error_handler
df22d223
AM
9452 (_("%s defined on removed toc entry"),
9453 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9454 do
9455 ++i;
9456 while ((skip[i] & (ref_from_discarded | can_optimize)));
9457 sym->st_value = (bfd_vma) i << 3;
9458 }
d62b3684 9459
df22d223
AM
9460 sym->st_value -= skip[i];
9461 symtab_hdr->contents = (unsigned char *) local_syms;
9462 }
c5614fa4 9463
854b41e7 9464 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9465 if (toc_inf.global_toc_syms)
9466 {
9467 toc_inf.toc = toc;
9468 toc_inf.skip = skip;
9469 toc_inf.global_toc_syms = FALSE;
9470 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9471 &toc_inf);
9472 }
854b41e7
AM
9473
9474 if (toc->reloc_count != 0)
9475 {
d4730f92 9476 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9477 Elf_Internal_Rela *wrel;
9478 bfd_size_type sz;
9479
854b41e7 9480 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9481 if (toc_relocs == NULL)
9482 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9483 info->keep_memory);
9484 if (toc_relocs == NULL)
9485 goto error_ret;
9486
425b145b
AM
9487 wrel = toc_relocs;
9488 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9489 if ((skip[rel->r_offset >> 3]
9490 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9491 {
9492 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9493 wrel->r_info = rel->r_info;
9494 wrel->r_addend = rel->r_addend;
9495 ++wrel;
9496 }
9497 else if (!dec_dynrel_count (rel->r_info, toc, info,
9498 &local_syms, NULL, NULL))
9499 goto error_ret;
9500
425b145b
AM
9501 elf_section_data (toc)->relocs = toc_relocs;
9502 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9503 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9504 sz = rel_hdr->sh_entsize;
9505 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9506 }
c5614fa4 9507 }
28be611c
AM
9508 else if (toc_relocs != NULL
9509 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9510 free (toc_relocs);
c5614fa4
AM
9511
9512 if (local_syms != NULL
9513 && symtab_hdr->contents != (unsigned char *) local_syms)
9514 {
9515 if (!info->keep_memory)
9516 free (local_syms);
9517 else
9518 symtab_hdr->contents = (unsigned char *) local_syms;
9519 }
9520 free (skip);
9521 }
9522
9523 return TRUE;
9524}
9525
1bbe0902
AM
9526/* Return true iff input section I references the TOC using
9527 instructions limited to +/-32k offsets. */
9528
9529bfd_boolean
9530ppc64_elf_has_small_toc_reloc (asection *i)
9531{
9532 return (is_ppc64_elf (i->owner)
9533 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9534}
9535
927be08e
AM
9536/* Allocate space for one GOT entry. */
9537
9538static void
9539allocate_got (struct elf_link_hash_entry *h,
9540 struct bfd_link_info *info,
9541 struct got_entry *gent)
9542{
9543 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9544 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9545 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9546 ? 16 : 8);
9547 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9548 ? 2 : 1) * sizeof (Elf64_External_Rela);
9549 asection *got = ppc64_elf_tdata (gent->owner)->got;
9550
9551 gent->got.offset = got->size;
9552 got->size += entsize;
9553
19e08130 9554 if (h->type == STT_GNU_IFUNC)
927be08e 9555 {
33e44f2e 9556 htab->elf.irelplt->size += rentsize;
19e08130 9557 htab->got_reli_size += rentsize;
927be08e 9558 }
f15d0b54
AM
9559 else if (((bfd_link_pic (info)
9560 && !((gent->tls_type & TLS_TPREL) != 0
9561 && bfd_link_executable (info)
9562 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9563 || (htab->elf.dynamic_sections_created
9564 && h->dynindx != -1
9565 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9566 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9567 {
19e08130 9568 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9569 relgot->size += rentsize;
927be08e
AM
9570 }
9571}
9572
7865406b
AM
9573/* This function merges got entries in the same toc group. */
9574
9575static void
9576merge_got_entries (struct got_entry **pent)
9577{
9578 struct got_entry *ent, *ent2;
9579
9580 for (ent = *pent; ent != NULL; ent = ent->next)
9581 if (!ent->is_indirect)
9582 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9583 if (!ent2->is_indirect
9584 && ent2->addend == ent->addend
9585 && ent2->tls_type == ent->tls_type
9586 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9587 {
9588 ent2->is_indirect = TRUE;
9589 ent2->got.ent = ent;
9590 }
9591}
9592
46434633 9593/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9594
9595static bfd_boolean
46434633
AM
9596ensure_undef_dynamic (struct bfd_link_info *info,
9597 struct elf_link_hash_entry *h)
f0158f44
AM
9598{
9599 struct elf_link_hash_table *htab = elf_hash_table (info);
9600
9601 if (htab->dynamic_sections_created
46434633
AM
9602 && ((info->dynamic_undefined_weak != 0
9603 && h->root.type == bfd_link_hash_undefweak)
9604 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9605 && h->dynindx == -1
9606 && !h->forced_local
9607 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9608 return bfd_elf_link_record_dynamic_symbol (info, h);
9609 return TRUE;
9610}
9611
65f38f15
AM
9612/* Allocate space in .plt, .got and associated reloc sections for
9613 dynamic relocs. */
5bd4f169 9614
b34976b6 9615static bfd_boolean
4ce794b7 9616allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9617{
65f38f15
AM
9618 struct bfd_link_info *info;
9619 struct ppc_link_hash_table *htab;
5bd4f169 9620 asection *s;
65f38f15 9621 struct ppc_link_hash_entry *eh;
0b8bcf0d 9622 struct got_entry **pgent, *gent;
5bd4f169 9623
e92d460e 9624 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9625 return TRUE;
5bd4f169 9626
65f38f15
AM
9627 info = (struct bfd_link_info *) inf;
9628 htab = ppc_hash_table (info);
4dfe6ac6
NC
9629 if (htab == NULL)
9630 return FALSE;
5bd4f169 9631
951fd09b
AM
9632 eh = (struct ppc_link_hash_entry *) h;
9633 /* Run through the TLS GD got entries first if we're changing them
9634 to TPREL. */
e7b938ca 9635 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9636 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9637 if (gent->got.refcount > 0
9638 && (gent->tls_type & TLS_GD) != 0)
9639 {
9640 /* This was a GD entry that has been converted to TPREL. If
9641 there happens to be a TPREL entry we can use that one. */
9642 struct got_entry *ent;
9643 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9644 if (ent->got.refcount > 0
9645 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9646 && ent->addend == gent->addend
9647 && ent->owner == gent->owner)
951fd09b
AM
9648 {
9649 gent->got.refcount = 0;
9650 break;
9651 }
9652
9653 /* If not, then we'll be using our own TPREL entry. */
9654 if (gent->got.refcount != 0)
9655 gent->tls_type = TLS_TLS | TLS_TPREL;
9656 }
9657
7865406b
AM
9658 /* Remove any list entry that won't generate a word in the GOT before
9659 we call merge_got_entries. Otherwise we risk merging to empty
9660 entries. */
0b8bcf0d
AM
9661 pgent = &h->got.glist;
9662 while ((gent = *pgent) != NULL)
411e1bfb 9663 if (gent->got.refcount > 0)
7865406b
AM
9664 {
9665 if ((gent->tls_type & TLS_LD) != 0
9666 && !h->def_dynamic)
9667 {
9668 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9669 *pgent = gent->next;
9670 }
9671 else
9672 pgent = &gent->next;
9673 }
9674 else
9675 *pgent = gent->next;
9676
9677 if (!htab->do_multi_toc)
9678 merge_got_entries (&h->got.glist);
9679
9680 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9681 if (!gent->is_indirect)
411e1bfb 9682 {
46434633
AM
9683 /* Make sure this symbol is output as a dynamic symbol. */
9684 if (!ensure_undef_dynamic (info, h))
f0158f44 9685 return FALSE;
65f38f15 9686
0c8d6e5c 9687 if (!is_ppc64_elf (gent->owner))
927be08e 9688 abort ();
0ffa91dd 9689
927be08e 9690 allocate_got (h, info, gent);
411e1bfb 9691 }
65f38f15 9692
954b63d4
AM
9693 /* If no dynamic sections we can't have dynamic relocs, except for
9694 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9695 if (!htab->elf.dynamic_sections_created
9696 && h->type != STT_GNU_IFUNC)
9697 eh->dyn_relocs = NULL;
9698
529fe20e
AM
9699 /* Discard relocs on undefined symbols that must be local. */
9700 else if (h->root.type == bfd_link_hash_undefined
9701 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9702 eh->dyn_relocs = NULL;
9703
954b63d4
AM
9704 /* Also discard relocs on undefined weak syms with non-default
9705 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9706 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9707 eh->dyn_relocs = NULL;
9708
8a2058b5 9709 if (eh->dyn_relocs != NULL)
65f38f15 9710 {
8a2058b5
AM
9711 struct elf_dyn_relocs *p, **pp;
9712
57e7d118
AM
9713 /* In the shared -Bsymbolic case, discard space allocated for
9714 dynamic pc-relative relocs against symbols which turn out to
9715 be defined in regular objects. For the normal shared case,
9716 discard space for relocs that have become local due to symbol
9717 visibility changes. */
9718
9719 if (bfd_link_pic (info))
65f38f15 9720 {
57e7d118
AM
9721 /* Relocs that use pc_count are those that appear on a call
9722 insn, or certain REL relocs (see must_be_dyn_reloc) that
9723 can be generated via assembly. We want calls to
9724 protected symbols to resolve directly to the function
9725 rather than going via the plt. If people want function
9726 pointer comparisons to work as expected then they should
9727 avoid writing weird assembly. */
9728 if (SYMBOL_CALLS_LOCAL (info, h))
9729 {
57e7d118
AM
9730 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9731 {
9732 p->count -= p->pc_count;
9733 p->pc_count = 0;
9734 if (p->count == 0)
9735 *pp = p->next;
9736 else
9737 pp = &p->next;
9738 }
9739 }
65f38f15 9740
954b63d4 9741 if (eh->dyn_relocs != NULL)
5bd4f169 9742 {
46434633
AM
9743 /* Make sure this symbol is output as a dynamic symbol. */
9744 if (!ensure_undef_dynamic (info, h))
f0158f44 9745 return FALSE;
5bd4f169 9746 }
65f38f15 9747 }
529fe20e 9748 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9749 {
8a2058b5 9750 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9751 symbols which turn out to need copy relocs or are not
9752 dynamic. */
529fe20e
AM
9753 if (h->dynamic_adjusted
9754 && !h->def_regular
9755 && !ELF_COMMON_DEF_P (h))
f0158f44 9756 {
46434633
AM
9757 /* Make sure this symbol is output as a dynamic symbol. */
9758 if (!ensure_undef_dynamic (info, h))
f0158f44 9759 return FALSE;
dfbb6ac9 9760
f0158f44
AM
9761 if (h->dynindx == -1)
9762 eh->dyn_relocs = NULL;
9763 }
9764 else
8a2058b5 9765 eh->dyn_relocs = NULL;
57e7d118
AM
9766 }
9767
9768 /* Finally, allocate space. */
9769 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9770 {
9771 asection *sreloc = elf_section_data (p->sec)->sreloc;
9772 if (eh->elf.type == STT_GNU_IFUNC)
9773 sreloc = htab->elf.irelplt;
9774 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9775 }
65f38f15 9776 }
57e7d118
AM
9777
9778 if ((htab->elf.dynamic_sections_created
f0158f44 9779 && h->dynindx != -1)
57e7d118 9780 || h->type == STT_GNU_IFUNC)
65f38f15 9781 {
57e7d118
AM
9782 struct plt_entry *pent;
9783 bfd_boolean doneone = FALSE;
9784 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9785 if (pent->plt.refcount > 0)
9786 {
9787 if (!htab->elf.dynamic_sections_created
9788 || h->dynindx == -1)
9789 {
9790 s = htab->elf.iplt;
9791 pent->plt.offset = s->size;
9792 s->size += PLT_ENTRY_SIZE (htab);
9793 s = htab->elf.irelplt;
9794 }
9795 else
9796 {
9797 /* If this is the first .plt entry, make room for the special
9798 first entry. */
9799 s = htab->elf.splt;
9800 if (s->size == 0)
9801 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9802
57e7d118 9803 pent->plt.offset = s->size;
65f38f15 9804
57e7d118
AM
9805 /* Make room for this entry. */
9806 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9807
57e7d118
AM
9808 /* Make room for the .glink code. */
9809 s = htab->glink;
9810 if (s->size == 0)
9e390558 9811 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9812 if (htab->opd_abi)
9813 {
9814 /* We need bigger stubs past index 32767. */
9e390558 9815 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9816 s->size += 4;
9817 s->size += 2*4;
9818 }
9819 else
9820 s->size += 4;
65f38f15 9821
57e7d118
AM
9822 /* We also need to make an entry in the .rela.plt section. */
9823 s = htab->elf.srelplt;
9824 }
9825 s->size += sizeof (Elf64_External_Rela);
9826 doneone = TRUE;
9827 }
9828 else
9829 pent->plt.offset = (bfd_vma) -1;
9830 if (!doneone)
9831 {
9832 h->plt.plist = NULL;
9833 h->needs_plt = 0;
9834 }
65f38f15 9835 }
57e7d118 9836 else
65f38f15 9837 {
57e7d118
AM
9838 h->plt.plist = NULL;
9839 h->needs_plt = 0;
65f38f15
AM
9840 }
9841
b34976b6 9842 return TRUE;
65f38f15
AM
9843}
9844
9e390558
AM
9845#define PPC_LO(v) ((v) & 0xffff)
9846#define PPC_HI(v) (((v) >> 16) & 0xffff)
9847#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9848
a345bc8d
AM
9849/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9850 to set up space for global entry stubs. These are put in glink,
9851 after the branch table. */
65f38f15 9852
b34976b6 9853static bfd_boolean
a345bc8d 9854size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9855{
a345bc8d
AM
9856 struct bfd_link_info *info;
9857 struct ppc_link_hash_table *htab;
9858 struct plt_entry *pent;
9e390558 9859 asection *s, *plt;
65f38f15 9860
a345bc8d
AM
9861 if (h->root.type == bfd_link_hash_indirect)
9862 return TRUE;
65f38f15 9863
a345bc8d
AM
9864 if (!h->pointer_equality_needed)
9865 return TRUE;
65f38f15 9866
a345bc8d
AM
9867 if (h->def_regular)
9868 return TRUE;
65f38f15 9869
a345bc8d
AM
9870 info = inf;
9871 htab = ppc_hash_table (info);
9872 if (htab == NULL)
9873 return FALSE;
9874
9e390558
AM
9875 s = htab->global_entry;
9876 plt = htab->elf.splt;
a345bc8d
AM
9877 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9878 if (pent->plt.offset != (bfd_vma) -1
9879 && pent->addend == 0)
9880 {
afe397ea
AM
9881 /* For ELFv2, if this symbol is not defined in a regular file
9882 and we are not generating a shared library or pie, then we
9883 need to define the symbol in the executable on a call stub.
9884 This is to avoid text relocations. */
9e390558
AM
9885 bfd_vma off, stub_align, stub_off, stub_size;
9886 unsigned int align_power;
9887
9888 stub_size = 16;
1be5d8d3
AM
9889 if (!htab->params->speculate_indirect_jumps)
9890 stub_size += 8;
9e390558
AM
9891 stub_off = s->size;
9892 if (htab->params->plt_stub_align >= 0)
9893 align_power = htab->params->plt_stub_align;
9894 else
9895 align_power = -htab->params->plt_stub_align;
9896 /* Setting section alignment is delayed until we know it is
9897 non-empty. Otherwise the .text output section will be
9898 aligned at least to plt_stub_align even when no global
9899 entry stubs are needed. */
9900 if (s->alignment_power < align_power)
9901 s->alignment_power = align_power;
9902 stub_align = (bfd_vma) 1 << align_power;
9903 if (htab->params->plt_stub_align >= 0
9904 || ((((stub_off + stub_size - 1) & -stub_align)
9905 - (stub_off & -stub_align))
9906 > ((stub_size - 1) & -stub_align)))
9907 stub_off = (stub_off + stub_align - 1) & -stub_align;
9908 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9909 off -= stub_off + s->output_offset + s->output_section->vma;
9910 /* Note that for --plt-stub-align negative we have a possible
9911 dependency between stub offset and size. Break that
9912 dependency by assuming the max stub size when calculating
9913 the stub offset. */
9914 if (PPC_HA (off) == 0)
9915 stub_size -= 4;
8a2058b5 9916 h->root.type = bfd_link_hash_defined;
afe397ea 9917 h->root.u.def.section = s;
9e390558
AM
9918 h->root.u.def.value = stub_off;
9919 s->size = stub_off + stub_size;
a345bc8d
AM
9920 break;
9921 }
9922 return TRUE;
9923}
9924
9925/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9926 read-only sections. */
9927
9928static bfd_boolean
98bbb1b8 9929maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9930{
98bbb1b8
AM
9931 asection *sec;
9932
a345bc8d
AM
9933 if (h->root.type == bfd_link_hash_indirect)
9934 return TRUE;
9935
98bbb1b8
AM
9936 sec = readonly_dynrelocs (h);
9937 if (sec != NULL)
a345bc8d 9938 {
98bbb1b8
AM
9939 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9940
9941 info->flags |= DF_TEXTREL;
9942 info->callbacks->minfo
63c1f59d
AM
9943 (_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
9944 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9945
9946 /* Not an error, just cut short the traversal. */
9947 return FALSE;
65f38f15 9948 }
b34976b6 9949 return TRUE;
65f38f15
AM
9950}
9951
9952/* Set the sizes of the dynamic sections. */
9953
b34976b6 9954static bfd_boolean
ee67d69a 9955ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9956 struct bfd_link_info *info)
65f38f15
AM
9957{
9958 struct ppc_link_hash_table *htab;
9959 bfd *dynobj;
9960 asection *s;
b34976b6 9961 bfd_boolean relocs;
65f38f15 9962 bfd *ibfd;
7865406b 9963 struct got_entry *first_tlsld;
65f38f15
AM
9964
9965 htab = ppc_hash_table (info);
4dfe6ac6
NC
9966 if (htab == NULL)
9967 return FALSE;
9968
65f38f15
AM
9969 dynobj = htab->elf.dynobj;
9970 if (dynobj == NULL)
9971 abort ();
9972
9973 if (htab->elf.dynamic_sections_created)
9974 {
9975 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9976 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9977 {
3d4d4302 9978 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9979 if (s == NULL)
9980 abort ();
eea6121a 9981 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9982 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9983 }
9984 }
9985
9986 /* Set up .got offsets for local syms, and space for local dynamic
9987 relocs. */
c72f2fb2 9988 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9989 {
411e1bfb
AM
9990 struct got_entry **lgot_ents;
9991 struct got_entry **end_lgot_ents;
e054468f
AM
9992 struct plt_entry **local_plt;
9993 struct plt_entry **end_local_plt;
f961d9dd 9994 unsigned char *lgot_masks;
65f38f15
AM
9995 bfd_size_type locsymcount;
9996 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9997
0c8d6e5c 9998 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9999 continue;
10000
10001 for (s = ibfd->sections; s != NULL; s = s->next)
10002 {
19e08130 10003 struct ppc_dyn_relocs *p;
65f38f15 10004
6edfbbad 10005 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10006 {
ec338859
AM
10007 if (!bfd_is_abs_section (p->sec)
10008 && bfd_is_abs_section (p->sec->output_section))
10009 {
10010 /* Input section has been discarded, either because
10011 it is a copy of a linkonce section or due to
10012 linker script /DISCARD/, so we'll be discarding
10013 the relocs too. */
10014 }
248866a8 10015 else if (p->count != 0)
ec338859 10016 {
19e08130
AM
10017 asection *srel = elf_section_data (p->sec)->sreloc;
10018 if (p->ifunc)
33e44f2e 10019 srel = htab->elf.irelplt;
eea6121a 10020 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10021 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10022 info->flags |= DF_TEXTREL;
ec338859 10023 }
65f38f15
AM
10024 }
10025 }
10026
411e1bfb
AM
10027 lgot_ents = elf_local_got_ents (ibfd);
10028 if (!lgot_ents)
65f38f15
AM
10029 continue;
10030
0ffa91dd 10031 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10032 locsymcount = symtab_hdr->sh_info;
411e1bfb 10033 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10034 local_plt = (struct plt_entry **) end_lgot_ents;
10035 end_local_plt = local_plt + locsymcount;
f961d9dd 10036 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10037 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10038 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10039 {
0b8bcf0d 10040 struct got_entry **pent, *ent;
411e1bfb 10041
0b8bcf0d
AM
10042 pent = lgot_ents;
10043 while ((ent = *pent) != NULL)
411e1bfb
AM
10044 if (ent->got.refcount > 0)
10045 {
e7b938ca 10046 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10047 {
927be08e 10048 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10049 *pent = ent->next;
411e1bfb
AM
10050 }
10051 else
10052 {
19e08130
AM
10053 unsigned int ent_size = 8;
10054 unsigned int rel_size = sizeof (Elf64_External_Rela);
10055
eea6121a 10056 ent->got.offset = s->size;
e7b938ca 10057 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10058 {
19e08130
AM
10059 ent_size *= 2;
10060 rel_size *= 2;
10061 }
10062 s->size += ent_size;
10063 if ((*lgot_masks & PLT_IFUNC) != 0)
10064 {
33e44f2e 10065 htab->elf.irelplt->size += rel_size;
19e08130
AM
10066 htab->got_reli_size += rel_size;
10067 }
f15d0b54
AM
10068 else if (bfd_link_pic (info)
10069 && !((ent->tls_type & TLS_TPREL) != 0
10070 && bfd_link_executable (info)))
19e08130
AM
10071 {
10072 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10073 srel->size += rel_size;
927be08e 10074 }
0b8bcf0d 10075 pent = &ent->next;
411e1bfb
AM
10076 }
10077 }
10078 else
0b8bcf0d 10079 *pent = ent->next;
65f38f15 10080 }
e054468f
AM
10081
10082 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10083 for (; local_plt < end_local_plt; ++local_plt)
10084 {
10085 struct plt_entry *ent;
10086
10087 for (ent = *local_plt; ent != NULL; ent = ent->next)
10088 if (ent->plt.refcount > 0)
10089 {
33e44f2e 10090 s = htab->elf.iplt;
e054468f 10091 ent->plt.offset = s->size;
b9e5796b 10092 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10093
33e44f2e 10094 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10095 }
10096 else
10097 ent->plt.offset = (bfd_vma) -1;
10098 }
65f38f15
AM
10099 }
10100
10101 /* Allocate global sym .plt and .got entries, and space for global
10102 sym dynamic relocs. */
4ce794b7 10103 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10104
0e1862bb 10105 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10106 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10107
7865406b 10108 first_tlsld = NULL;
c72f2fb2 10109 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10110 {
7865406b
AM
10111 struct got_entry *ent;
10112
0c8d6e5c 10113 if (!is_ppc64_elf (ibfd))
102890f0
AM
10114 continue;
10115
7865406b
AM
10116 ent = ppc64_tlsld_got (ibfd);
10117 if (ent->got.refcount > 0)
102890f0 10118 {
7865406b 10119 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10120 {
7865406b
AM
10121 ent->is_indirect = TRUE;
10122 ent->got.ent = first_tlsld;
10123 }
10124 else
10125 {
10126 if (first_tlsld == NULL)
10127 first_tlsld = ent;
10128 s = ppc64_elf_tdata (ibfd)->got;
10129 ent->got.offset = s->size;
10130 ent->owner = ibfd;
10131 s->size += 16;
0e1862bb 10132 if (bfd_link_pic (info))
7865406b
AM
10133 {
10134 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10135 srel->size += sizeof (Elf64_External_Rela);
10136 }
102890f0
AM
10137 }
10138 }
10139 else
7865406b 10140 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10141 }
10142
65f38f15
AM
10143 /* We now have determined the sizes of the various dynamic sections.
10144 Allocate memory for them. */
b34976b6 10145 relocs = FALSE;
65f38f15
AM
10146 for (s = dynobj->sections; s != NULL; s = s->next)
10147 {
10148 if ((s->flags & SEC_LINKER_CREATED) == 0)
10149 continue;
10150
4ce794b7 10151 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10152 /* These haven't been allocated yet; don't strip. */
10153 continue;
33e44f2e
AM
10154 else if (s == htab->elf.sgot
10155 || s == htab->elf.splt
10156 || s == htab->elf.iplt
c456f082 10157 || s == htab->glink
9e390558 10158 || s == htab->global_entry
5474d94f
AM
10159 || s == htab->elf.sdynbss
10160 || s == htab->elf.sdynrelro)
65f38f15
AM
10161 {
10162 /* Strip this section if we don't need it; see the
10163 comment below. */
5bd4f169 10164 }
58d180e8
AM
10165 else if (s == htab->glink_eh_frame)
10166 {
10167 if (!bfd_is_abs_section (s->output_section))
10168 /* Not sized yet. */
10169 continue;
10170 }
70cc837d 10171 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10172 {
c456f082 10173 if (s->size != 0)
5bd4f169 10174 {
33e44f2e 10175 if (s != htab->elf.srelplt)
b34976b6 10176 relocs = TRUE;
5bd4f169
AM
10177
10178 /* We use the reloc_count field as a counter if we need
10179 to copy relocs into the output file. */
10180 s->reloc_count = 0;
10181 }
10182 }
65f38f15 10183 else
5bd4f169
AM
10184 {
10185 /* It's not one of our sections, so don't allocate space. */
10186 continue;
10187 }
10188
eea6121a 10189 if (s->size == 0)
5bd4f169 10190 {
c456f082
AM
10191 /* If we don't need this section, strip it from the
10192 output file. This is mostly to handle .rela.bss and
10193 .rela.plt. We must create both sections in
10194 create_dynamic_sections, because they must be created
10195 before the linker maps input sections to output
10196 sections. The linker does that before
10197 adjust_dynamic_symbol is called, and it is that
10198 function which decides whether anything needs to go
10199 into these sections. */
8423293d 10200 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10201 continue;
10202 }
10203
06bcf541
AM
10204 if (bfd_is_abs_section (s->output_section))
10205 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10206 s->name);
10207
c456f082 10208 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10209 continue;
10210
65f38f15
AM
10211 /* Allocate memory for the section contents. We use bfd_zalloc
10212 here in case unused entries are not reclaimed before the
10213 section's contents are written out. This should not happen,
411e1bfb
AM
10214 but this way if it does we get a R_PPC64_NONE reloc in .rela
10215 sections instead of garbage.
10216 We also rely on the section contents being zero when writing
5474d94f 10217 the GOT and .dynrelro. */
eea6121a 10218 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10219 if (s->contents == NULL)
b34976b6 10220 return FALSE;
5bd4f169
AM
10221 }
10222
c72f2fb2 10223 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10224 {
0c8d6e5c 10225 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10226 continue;
10227
e717da7e 10228 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10229 if (s != NULL && s != htab->elf.sgot)
e717da7e 10230 {
eea6121a 10231 if (s->size == 0)
8423293d 10232 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10233 else
10234 {
eea6121a 10235 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10236 if (s->contents == NULL)
10237 return FALSE;
10238 }
10239 }
10240 s = ppc64_elf_tdata (ibfd)->relgot;
10241 if (s != NULL)
10242 {
eea6121a 10243 if (s->size == 0)
8423293d 10244 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10245 else
10246 {
eea6121a 10247 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10248 if (s->contents == NULL)
10249 return FALSE;
10250 relocs = TRUE;
10251 s->reloc_count = 0;
10252 }
10253 }
10254 }
10255
e86ce104 10256 if (htab->elf.dynamic_sections_created)
5bd4f169 10257 {
e8910a83
AM
10258 bfd_boolean tls_opt;
10259
5bd4f169
AM
10260 /* Add some entries to the .dynamic section. We fill in the
10261 values later, in ppc64_elf_finish_dynamic_sections, but we
10262 must add the entries now so that we get the correct size for
10263 the .dynamic section. The DT_DEBUG entry is filled in by the
10264 dynamic linker and used by the debugger. */
dc810e39 10265#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10266 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10267
0e1862bb 10268 if (bfd_link_executable (info))
5bd4f169 10269 {
dc810e39 10270 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10271 return FALSE;
5bd4f169
AM
10272 }
10273
33e44f2e 10274 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10275 {
dc810e39
AM
10276 if (!add_dynamic_entry (DT_PLTGOT, 0)
10277 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10278 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10279 || !add_dynamic_entry (DT_JMPREL, 0)
10280 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10281 return FALSE;
5bd4f169
AM
10282 }
10283
ee67d69a 10284 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10285 {
10286 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10287 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10288 return FALSE;
19397422
AM
10289 }
10290
7c9cf415 10291 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10292 && htab->tls_get_addr_fd != NULL
10293 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10294 if (tls_opt || !htab->opd_abi)
10295 {
10296 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10297 return FALSE;
10298 }
a7f2871e 10299
5bd4f169
AM
10300 if (relocs)
10301 {
dc810e39
AM
10302 if (!add_dynamic_entry (DT_RELA, 0)
10303 || !add_dynamic_entry (DT_RELASZ, 0)
10304 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10305 return FALSE;
5bd4f169 10306
65f38f15
AM
10307 /* If any dynamic relocs apply to a read-only section,
10308 then we need a DT_TEXTREL entry. */
248866a8 10309 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10310 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10311
65f38f15 10312 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10313 {
65f38f15 10314 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10315 return FALSE;
5bd4f169 10316 }
5bd4f169 10317 }
5bd4f169 10318 }
65f38f15 10319#undef add_dynamic_entry
5bd4f169 10320
b34976b6 10321 return TRUE;
5bd4f169
AM
10322}
10323
a345bc8d
AM
10324/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10325
10326static bfd_boolean
10327ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10328{
10329 if (h->plt.plist != NULL
10330 && !h->def_regular
10331 && !h->pointer_equality_needed)
10332 return FALSE;
10333
10334 return _bfd_elf_hash_symbol (h);
10335}
10336
721956f4 10337/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10338
4ce794b7
AM
10339static inline enum ppc_stub_type
10340ppc_type_of_stub (asection *input_sec,
10341 const Elf_Internal_Rela *rel,
10342 struct ppc_link_hash_entry **hash,
e054468f 10343 struct plt_entry **plt_ent,
6911b7dc
AM
10344 bfd_vma destination,
10345 unsigned long local_off)
5bd4f169 10346{
721956f4
AM
10347 struct ppc_link_hash_entry *h = *hash;
10348 bfd_vma location;
10349 bfd_vma branch_offset;
10350 bfd_vma max_branch_offset;
4ce794b7 10351 enum elf_ppc64_reloc_type r_type;
5bd4f169 10352
721956f4
AM
10353 if (h != NULL)
10354 {
e054468f 10355 struct plt_entry *ent;
7fe2b9a6 10356 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10357 if (h->oh != NULL
10358 && h->oh->is_func_descriptor)
7b8f6675
AM
10359 {
10360 fdh = ppc_follow_link (h->oh);
10361 *hash = fdh;
10362 }
8387904d 10363
e054468f
AM
10364 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10365 if (ent->addend == rel->r_addend
10366 && ent->plt.offset != (bfd_vma) -1)
10367 {
e054468f
AM
10368 *plt_ent = ent;
10369 return ppc_stub_plt_call;
10370 }
5bd4f169 10371
7fe2b9a6
AM
10372 /* Here, we know we don't have a plt entry. If we don't have a
10373 either a defined function descriptor or a defined entry symbol
10374 in a regular object file, then it is pointless trying to make
10375 any other type of stub. */
854b41e7
AM
10376 if (!is_static_defined (&fdh->elf)
10377 && !is_static_defined (&h->elf))
721956f4 10378 return ppc_stub_none;
5d1634d7 10379 }
e054468f
AM
10380 else if (elf_local_got_ents (input_sec->owner) != NULL)
10381 {
10382 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10383 struct plt_entry **local_plt = (struct plt_entry **)
10384 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10385 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10386
10387 if (local_plt[r_symndx] != NULL)
10388 {
10389 struct plt_entry *ent;
10390
10391 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10392 if (ent->addend == rel->r_addend
10393 && ent->plt.offset != (bfd_vma) -1)
10394 {
10395 *plt_ent = ent;
10396 return ppc_stub_plt_call;
10397 }
10398 }
10399 }
5d1634d7 10400
721956f4
AM
10401 /* Determine where the call point is. */
10402 location = (input_sec->output_offset
10403 + input_sec->output_section->vma
10404 + rel->r_offset);
5d1634d7 10405
721956f4
AM
10406 branch_offset = destination - location;
10407 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10408
721956f4
AM
10409 /* Determine if a long branch stub is needed. */
10410 max_branch_offset = 1 << 25;
4ce794b7 10411 if (r_type != R_PPC64_REL24)
721956f4 10412 max_branch_offset = 1 << 15;
5d1634d7 10413
6911b7dc 10414 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10415 /* We need a stub. Figure out whether a long_branch or plt_branch
10416 is needed later. */
10417 return ppc_stub_long_branch;
5d1634d7 10418
721956f4 10419 return ppc_stub_none;
5d1634d7
AM
10420}
10421
794e51c0
AM
10422/* With power7 weakly ordered memory model, it is possible for ld.so
10423 to update a plt entry in one thread and have another thread see a
10424 stale zero toc entry. To avoid this we need some sort of acquire
10425 barrier in the call stub. One solution is to make the load of the
10426 toc word seem to appear to depend on the load of the function entry
10427 word. Another solution is to test for r2 being zero, and branch to
10428 the appropriate glink entry if so.
10429
10430 . fake dep barrier compare
71a39c98
AM
10431 . ld 12,xxx(2) ld 12,xxx(2)
10432 . mtctr 12 mtctr 12
10433 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10434 . add 2,2,11 cmpldi 2,0
10435 . ld 2,xxx+8(2) bnectr+
10436 . bctr b <glink_entry>
10437
10438 The solution involving the compare turns out to be faster, so
10439 that's what we use unless the branch won't reach. */
10440
10441#define ALWAYS_USE_FAKE_DEP 0
10442#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10443
794e51c0
AM
10444static inline unsigned int
10445plt_stub_size (struct ppc_link_hash_table *htab,
10446 struct ppc_stub_hash_entry *stub_entry,
10447 bfd_vma off)
10448{
b9e5796b
AM
10449 unsigned size = 12;
10450
10451 if (ALWAYS_EMIT_R2SAVE
10452 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10453 size += 4;
10454 if (PPC_HA (off) != 0)
794e51c0 10455 size += 4;
1be5d8d3
AM
10456 if (!htab->params->speculate_indirect_jumps)
10457 size += 8;
b9e5796b
AM
10458 if (htab->opd_abi)
10459 {
10460 size += 4;
e7d1c40c 10461 if (htab->params->plt_static_chain)
b9e5796b 10462 size += 4;
bd4d2eaa
AM
10463 if (htab->params->plt_thread_safe
10464 && htab->elf.dynamic_sections_created
10465 && stub_entry->h != NULL
10466 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10467 size += 8;
e7d1c40c 10468 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10469 size += 4;
10470 }
794e51c0
AM
10471 if (stub_entry->h != NULL
10472 && (stub_entry->h == htab->tls_get_addr_fd
10473 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10474 && htab->params->tls_get_addr_opt)
f378ab09
AM
10475 {
10476 size += 7 * 4;
10477 if (ALWAYS_EMIT_R2SAVE
10478 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
1be5d8d3
AM
10479 {
10480 size += 6 * 4;
10481 if (!htab->params->speculate_indirect_jumps)
10482 size -= 4;
10483 }
f378ab09 10484 }
794e51c0
AM
10485 return size;
10486}
10487
2420fff6
AM
10488/* Depending on the sign of plt_stub_align:
10489 If positive, return the padding to align to a 2**plt_stub_align
10490 boundary.
10491 If negative, if this stub would cross fewer 2**plt_stub_align
10492 boundaries if we align, then return the padding needed to do so. */
10493
794e51c0
AM
10494static inline unsigned int
10495plt_stub_pad (struct ppc_link_hash_table *htab,
10496 struct ppc_stub_hash_entry *stub_entry,
10497 bfd_vma plt_off)
10498{
2420fff6 10499 int stub_align;
794e51c0 10500 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10501 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10502
2420fff6
AM
10503 if (htab->params->plt_stub_align >= 0)
10504 {
10505 stub_align = 1 << htab->params->plt_stub_align;
10506 if ((stub_off & (stub_align - 1)) != 0)
10507 return stub_align - (stub_off & (stub_align - 1));
10508 return 0;
10509 }
10510
10511 stub_align = 1 << -htab->params->plt_stub_align;
794e51c0 10512 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10513 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10514 return stub_align - (stub_off & (stub_align - 1));
10515 return 0;
10516}
10517
1be5d8d3
AM
10518static inline bfd_byte *
10519output_bctr (struct ppc_link_hash_table *htab, bfd *obfd, bfd_byte *p)
10520{
10521 if (!htab->params->speculate_indirect_jumps)
10522 {
10523 bfd_put_32 (obfd, CRSETEQ, p);
10524 p += 4;
10525 bfd_put_32 (obfd, BEQCTRM, p);
10526 p += 4;
10527 bfd_put_32 (obfd, B_DOT, p);
10528 p += 4;
10529 }
10530 else
10531 {
10532 bfd_put_32 (obfd, BCTR, p);
10533 p += 4;
10534 }
10535 return p;
10536}
10537
794e51c0
AM
10538/* Build a .plt call stub. */
10539
10540static inline bfd_byte *
10541build_plt_stub (struct ppc_link_hash_table *htab,
10542 struct ppc_stub_hash_entry *stub_entry,
10543 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10544{
e7d1c40c 10545 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10546 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10547 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10548 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10549 && htab->elf.dynamic_sections_created
10550 && stub_entry->h != NULL
10551 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10552 bfd_boolean use_fake_dep = plt_thread_safe;
10553 bfd_vma cmp_branch_off = 0;
10554
10555 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10556 && plt_load_toc
794e51c0 10557 && plt_thread_safe
1be5d8d3 10558 && htab->params->speculate_indirect_jumps
bd4d2eaa
AM
10559 && !((stub_entry->h == htab->tls_get_addr_fd
10560 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10561 && htab->params->tls_get_addr_opt))
794e51c0
AM
10562 {
10563 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10564 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10565 / PLT_ENTRY_SIZE (htab));
9e390558 10566 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10567 bfd_vma to, from;
10568
68d62958
AM
10569 if (pltindex > 32768)
10570 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10571 to = (glinkoff
10572 + htab->glink->output_offset
10573 + htab->glink->output_section->vma);
6f20ed8a 10574 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10575 + 4 * (ALWAYS_EMIT_R2SAVE
10576 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10577 + 4 * (PPC_HA (offset) != 0)
10578 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10579 != PPC_HA (offset))
10580 + 4 * (plt_static_chain != 0)
10581 + 20
6f20ed8a
AM
10582 + stub_entry->group->stub_sec->output_offset
10583 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10584 cmp_branch_off = to - from;
10585 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10586 }
10587
ac2df442
AM
10588 if (PPC_HA (offset) != 0)
10589 {
176a0d42
AM
10590 if (r != NULL)
10591 {
794e51c0
AM
10592 if (ALWAYS_EMIT_R2SAVE
10593 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10594 r[0].r_offset += 4;
176a0d42 10595 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10596 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10597 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10598 r[1].r_addend = r[0].r_addend;
b9e5796b 10599 if (plt_load_toc)
176a0d42 10600 {
b9e5796b 10601 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10602 {
b9e5796b
AM
10603 r[2].r_offset = r[1].r_offset + 4;
10604 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10605 r[2].r_addend = r[0].r_addend;
10606 }
10607 else
10608 {
10609 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10610 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10611 r[2].r_addend = r[0].r_addend + 8;
10612 if (plt_static_chain)
10613 {
10614 r[3].r_offset = r[2].r_offset + 4;
10615 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10616 r[3].r_addend = r[0].r_addend + 16;
10617 }
c7131b65 10618 }
176a0d42
AM
10619 }
10620 }
794e51c0
AM
10621 if (ALWAYS_EMIT_R2SAVE
10622 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10623 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10624 if (plt_load_toc)
10625 {
10626 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10627 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10628 }
10629 else
10630 {
10631 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10632 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10633 }
b9e5796b
AM
10634 if (plt_load_toc
10635 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10636 {
71a39c98 10637 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10638 offset = 0;
10639 }
71a39c98 10640 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10641 if (plt_load_toc)
794e51c0 10642 {
b9e5796b
AM
10643 if (use_fake_dep)
10644 {
10645 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10646 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10647 }
10648 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10649 if (plt_static_chain)
10650 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10651 }
ac2df442
AM
10652 }
10653 else
10654 {
176a0d42
AM
10655 if (r != NULL)
10656 {
794e51c0
AM
10657 if (ALWAYS_EMIT_R2SAVE
10658 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10659 r[0].r_offset += 4;
176a0d42 10660 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10661 if (plt_load_toc)
176a0d42 10662 {
b9e5796b 10663 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10664 {
b9e5796b
AM
10665 r[1].r_offset = r[0].r_offset + 4;
10666 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10667 r[1].r_addend = r[0].r_addend;
10668 }
10669 else
10670 {
10671 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10672 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10673 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10674 if (plt_static_chain)
10675 {
10676 r[2].r_offset = r[1].r_offset + 4;
10677 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10678 r[2].r_addend = r[0].r_addend + 8;
10679 }
c7131b65 10680 }
176a0d42
AM
10681 }
10682 }
794e51c0
AM
10683 if (ALWAYS_EMIT_R2SAVE
10684 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10685 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10686 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10687 if (plt_load_toc
10688 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10689 {
10690 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10691 offset = 0;
10692 }
71a39c98 10693 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10694 if (plt_load_toc)
794e51c0 10695 {
b9e5796b
AM
10696 if (use_fake_dep)
10697 {
10698 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10699 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10700 }
10701 if (plt_static_chain)
10702 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10703 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10704 }
ac2df442 10705 }
b9e5796b 10706 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10707 {
10708 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10709 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10710 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10711 }
10712 else
1be5d8d3 10713 p = output_bctr (htab, obfd, p);
5d1634d7
AM
10714 return p;
10715}
10716
a7f2871e
AM
10717/* Build a special .plt call stub for __tls_get_addr. */
10718
10719#define LD_R11_0R3 0xe9630000
10720#define LD_R12_0R3 0xe9830000
10721#define MR_R0_R3 0x7c601b78
10722#define CMPDI_R11_0 0x2c2b0000
10723#define ADD_R3_R12_R13 0x7c6c6a14
10724#define BEQLR 0x4d820020
10725#define MR_R3_R0 0x7c030378
a7f2871e
AM
10726#define STD_R11_0R1 0xf9610000
10727#define BCTRL 0x4e800421
10728#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10729#define MTLR_R11 0x7d6803a6
10730
10731static inline bfd_byte *
794e51c0
AM
10732build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10733 struct ppc_stub_hash_entry *stub_entry,
10734 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10735{
e7d1c40c 10736 bfd *obfd = htab->params->stub_bfd;
794e51c0 10737
a7f2871e
AM
10738 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10739 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10740 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10741 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10742 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10743 bfd_put_32 (obfd, BEQLR, p), p += 4;
10744 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10745 if (r != NULL)
10746 r[0].r_offset += 7 * 4;
10747 if (!ALWAYS_EMIT_R2SAVE
10748 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10749 return build_plt_stub (htab, stub_entry, p, offset, r);
10750
a7f2871e 10751 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10752 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10753
10754 if (r != NULL)
f378ab09 10755 r[0].r_offset += 2 * 4;
794e51c0 10756 p = build_plt_stub (htab, stub_entry, p, offset, r);
1be5d8d3
AM
10757 if (!htab->params->speculate_indirect_jumps)
10758 {
10759 p -= 4;
10760 bfd_put_32 (obfd, BEQCTRLM, p - 4);
10761 }
10762 else
10763 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10764
a078d95a 10765 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10766 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10767 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10768 bfd_put_32 (obfd, BLR, p), p += 4;
10769
10770 return p;
10771}
10772
176a0d42
AM
10773static Elf_Internal_Rela *
10774get_relocs (asection *sec, int count)
10775{
10776 Elf_Internal_Rela *relocs;
10777 struct bfd_elf_section_data *elfsec_data;
10778
10779 elfsec_data = elf_section_data (sec);
10780 relocs = elfsec_data->relocs;
10781 if (relocs == NULL)
10782 {
10783 bfd_size_type relsize;
10784 relsize = sec->reloc_count * sizeof (*relocs);
10785 relocs = bfd_alloc (sec->owner, relsize);
10786 if (relocs == NULL)
10787 return NULL;
10788 elfsec_data->relocs = relocs;
d4730f92
BS
10789 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10790 sizeof (Elf_Internal_Shdr));
10791 if (elfsec_data->rela.hdr == NULL)
10792 return NULL;
10793 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10794 * sizeof (Elf64_External_Rela));
10795 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10796 sec->reloc_count = 0;
10797 }
10798 relocs += sec->reloc_count;
10799 sec->reloc_count += count;
10800 return relocs;
10801}
10802
aa374f67 10803static bfd_vma
25f53a85 10804get_r2off (struct bfd_link_info *info,
aa374f67
AM
10805 struct ppc_stub_hash_entry *stub_entry)
10806{
25f53a85 10807 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10808 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10809
10810 if (r2off == 0)
10811 {
10812 /* Support linking -R objects. Get the toc pointer from the
10813 opd entry. */
10814 char buf[8];
b9e5796b
AM
10815 if (!htab->opd_abi)
10816 return r2off;
aa374f67
AM
10817 asection *opd = stub_entry->h->elf.root.u.def.section;
10818 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10819
10820 if (strcmp (opd->name, ".opd") != 0
10821 || opd->reloc_count != 0)
10822 {
bc30df16 10823 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10824 stub_entry->h->elf.root.root.string);
aa374f67 10825 bfd_set_error (bfd_error_bad_value);
a7c49797 10826 return (bfd_vma) -1;
aa374f67
AM
10827 }
10828 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10829 return (bfd_vma) -1;
aa374f67 10830 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10831 r2off -= elf_gp (info->output_bfd);
aa374f67 10832 }
6f20ed8a 10833 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10834 return r2off;
10835}
10836
b34976b6 10837static bfd_boolean
4ce794b7 10838ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10839{
721956f4
AM
10840 struct ppc_stub_hash_entry *stub_entry;
10841 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10842 struct bfd_link_info *info;
10843 struct ppc_link_hash_table *htab;
721956f4
AM
10844 bfd_byte *loc;
10845 bfd_byte *p;
ee75fd95 10846 bfd_vma dest, off;
176a0d42 10847 Elf_Internal_Rela *r;
e054468f 10848 asection *plt;
5d1634d7 10849
721956f4
AM
10850 /* Massage our args to the form they really have. */
10851 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10852 info = in_arg;
5d1634d7 10853
5d1634d7 10854 htab = ppc_hash_table (info);
4dfe6ac6
NC
10855 if (htab == NULL)
10856 return FALSE;
5d1634d7 10857
721956f4 10858 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10859 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10860 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10861
4ce794b7 10862 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10863 switch (stub_entry->stub_type)
5d1634d7 10864 {
721956f4 10865 case ppc_stub_long_branch:
ad8e1ba5 10866 case ppc_stub_long_branch_r2off:
721956f4 10867 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10868 dest = (stub_entry->target_value
10869 + stub_entry->target_section->output_offset
10870 + stub_entry->target_section->output_section->vma);
10871 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10872 off = dest;
5d1634d7 10873
721956f4
AM
10874 /* And this is where we are coming from. */
10875 off -= (stub_entry->stub_offset
6f20ed8a
AM
10876 + stub_entry->group->stub_sec->output_offset
10877 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10878
9e390558 10879 p = loc;
ac2df442 10880 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10881 {
25f53a85 10882 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10883
a7c49797 10884 if (r2off == (bfd_vma) -1)
aa374f67
AM
10885 {
10886 htab->stub_error = TRUE;
10887 return FALSE;
10888 }
9e390558
AM
10889 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10890 p += 4;
ac2df442
AM
10891 if (PPC_HA (r2off) != 0)
10892 {
e7d1c40c 10893 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10894 ADDIS_R2_R2 | PPC_HA (r2off), p);
10895 p += 4;
a7c49797
AM
10896 }
10897 if (PPC_LO (r2off) != 0)
10898 {
10899 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10900 ADDI_R2_R2 | PPC_LO (r2off), p);
10901 p += 4;
ac2df442 10902 }
9e390558 10903 off -= p - loc;
ad8e1ba5 10904 }
9e390558
AM
10905 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10906 p += 4;
ad8e1ba5 10907
5c3dead3
AM
10908 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10909 {
bc30df16
AM
10910 info->callbacks->einfo
10911 (_("%P: long branch stub `%s' offset overflow\n"),
10912 stub_entry->root.string);
5c3dead3
AM
10913 htab->stub_error = TRUE;
10914 return FALSE;
10915 }
ee75fd95
AM
10916
10917 if (info->emitrelocations)
10918 {
6f20ed8a 10919 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10920 if (r == NULL)
10921 return FALSE;
9e390558 10922 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10923 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10924 r->r_addend = dest;
10925 if (stub_entry->h != NULL)
10926 {
10927 struct elf_link_hash_entry **hashes;
10928 unsigned long symndx;
10929 struct ppc_link_hash_entry *h;
10930
e7d1c40c 10931 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10932 if (hashes == NULL)
10933 {
10934 bfd_size_type hsize;
10935
10936 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10937 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10938 if (hashes == NULL)
10939 return FALSE;
e7d1c40c 10940 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10941 htab->stub_globals = 1;
10942 }
10943 symndx = htab->stub_globals++;
10944 h = stub_entry->h;
10945 hashes[symndx] = &h->elf;
10946 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10947 if (h->oh != NULL && h->oh->is_func)
b31867b6 10948 h = ppc_follow_link (h->oh);
ee75fd95
AM
10949 if (h->elf.root.u.def.section != stub_entry->target_section)
10950 /* H is an opd symbol. The addend must be zero. */
10951 r->r_addend = 0;
10952 else
10953 {
10954 off = (h->elf.root.u.def.value
10955 + h->elf.root.u.def.section->output_offset
10956 + h->elf.root.u.def.section->output_section->vma);
10957 r->r_addend -= off;
10958 }
10959 }
10960 }
721956f4 10961 break;
e86ce104 10962
721956f4 10963 case ppc_stub_plt_branch:
ad8e1ba5 10964 case ppc_stub_plt_branch_r2off:
721956f4
AM
10965 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10966 stub_entry->root.string + 9,
b34976b6 10967 FALSE, FALSE);
721956f4
AM
10968 if (br_entry == NULL)
10969 {
8de848d8 10970 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10971 stub_entry->root.string);
b34976b6
AM
10972 htab->stub_error = TRUE;
10973 return FALSE;
721956f4
AM
10974 }
10975
176a0d42
AM
10976 dest = (stub_entry->target_value
10977 + stub_entry->target_section->output_offset
10978 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10979 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10980 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10981
176a0d42 10982 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10983 htab->brlt->contents + br_entry->offset);
721956f4 10984
f94498ff 10985 if (br_entry->iter == htab->stub_iteration)
721956f4 10986 {
f94498ff 10987 br_entry->iter = 0;
84f5d08e 10988
f94498ff 10989 if (htab->relbrlt != NULL)
84f5d08e 10990 {
f94498ff
AM
10991 /* Create a reloc for the branch lookup table entry. */
10992 Elf_Internal_Rela rela;
10993 bfd_byte *rl;
10994
10995 rela.r_offset = (br_entry->offset
10996 + htab->brlt->output_offset
10997 + htab->brlt->output_section->vma);
10998 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10999 rela.r_addend = dest;
f94498ff
AM
11000
11001 rl = htab->relbrlt->contents;
11002 rl += (htab->relbrlt->reloc_count++
11003 * sizeof (Elf64_External_Rela));
11004 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11005 }
11006 else if (info->emitrelocations)
11007 {
176a0d42
AM
11008 r = get_relocs (htab->brlt, 1);
11009 if (r == NULL)
11010 return FALSE;
11011 /* brlt, being SEC_LINKER_CREATED does not go through the
11012 normal reloc processing. Symbols and offsets are not
11013 translated from input file to output file form, so
11014 set up the offset per the output file. */
f94498ff
AM
11015 r->r_offset = (br_entry->offset
11016 + htab->brlt->output_offset
11017 + htab->brlt->output_section->vma);
11018 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11019 r->r_addend = dest;
84f5d08e 11020 }
84f5d08e 11021 }
721956f4 11022
176a0d42
AM
11023 dest = (br_entry->offset
11024 + htab->brlt->output_offset
11025 + htab->brlt->output_section->vma);
11026
11027 off = (dest
06bcf541 11028 - elf_gp (info->output_bfd)
6f20ed8a 11029 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11030
ad8e1ba5 11031 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11032 {
25f53a85 11033 info->callbacks->einfo
bc30df16 11034 (_("%P: linkage table error against `%T'\n"),
721956f4 11035 stub_entry->root.string);
5d1634d7 11036 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11037 htab->stub_error = TRUE;
11038 return FALSE;
5d1634d7 11039 }
41bd81ab 11040
176a0d42
AM
11041 if (info->emitrelocations)
11042 {
6f20ed8a 11043 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11044 if (r == NULL)
11045 return FALSE;
6f20ed8a 11046 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11047 if (bfd_big_endian (info->output_bfd))
11048 r[0].r_offset += 2;
00f412ee 11049 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11050 r[0].r_offset += 4;
11051 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11052 r[0].r_addend = dest;
11053 if (PPC_HA (off) != 0)
11054 {
11055 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11056 r[1].r_offset = r[0].r_offset + 4;
11057 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11058 r[1].r_addend = r[0].r_addend;
11059 }
11060 }
11061
9e390558 11062 p = loc;
00f412ee 11063 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11064 {
176a0d42 11065 if (PPC_HA (off) != 0)
ac2df442 11066 {
e7d1c40c 11067 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11068 ADDIS_R12_R2 | PPC_HA (off), p);
11069 p += 4;
e7d1c40c 11070 bfd_put_32 (htab->params->stub_bfd,
9e390558 11071 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11072 }
11073 else
9e390558
AM
11074 bfd_put_32 (htab->params->stub_bfd,
11075 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11076 }
11077 else
11078 {
25f53a85 11079 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11080
a7c49797 11081 if (r2off == (bfd_vma) -1)
aa374f67
AM
11082 {
11083 htab->stub_error = TRUE;
11084 return FALSE;
11085 }
ad8e1ba5 11086
9e390558
AM
11087 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11088 p += 4;
176a0d42 11089 if (PPC_HA (off) != 0)
ac2df442 11090 {
e7d1c40c 11091 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11092 ADDIS_R12_R2 | PPC_HA (off), p);
11093 p += 4;
e7d1c40c 11094 bfd_put_32 (htab->params->stub_bfd,
9e390558 11095 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11096 }
11097 else
9e390558 11098 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11099
11100 if (PPC_HA (r2off) != 0)
11101 {
9e390558 11102 p += 4;
e7d1c40c 11103 bfd_put_32 (htab->params->stub_bfd,
9e390558 11104 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11105 }
11106 if (PPC_LO (r2off) != 0)
11107 {
9e390558 11108 p += 4;
e7d1c40c 11109 bfd_put_32 (htab->params->stub_bfd,
9e390558 11110 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11111 }
ad8e1ba5 11112 }
9e390558
AM
11113 p += 4;
11114 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11115 p += 4;
1be5d8d3 11116 p = output_bctr (htab, htab->params->stub_bfd, p);
721956f4 11117 break;
5d1634d7 11118
721956f4 11119 case ppc_stub_plt_call:
794e51c0 11120 case ppc_stub_plt_call_r2save:
e054468f 11121 if (stub_entry->h != NULL
b31867b6
AM
11122 && stub_entry->h->is_func_descriptor
11123 && stub_entry->h->oh != NULL)
c862ae31 11124 {
b31867b6
AM
11125 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11126
11127 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11128 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11129 if (fh->elf.root.type == bfd_link_hash_undefined
11130 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11131 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11132 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11133 }
11134
721956f4 11135 /* Now build the stub. */
e054468f 11136 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11137 if (dest >= (bfd_vma) -2)
721956f4
AM
11138 abort ();
11139
33e44f2e 11140 plt = htab->elf.splt;
25f23106
AM
11141 if (!htab->elf.dynamic_sections_created
11142 || stub_entry->h == NULL
11143 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11144 plt = htab->elf.iplt;
e054468f
AM
11145
11146 dest += plt->output_offset + plt->output_section->vma;
11147
11148 if (stub_entry->h == NULL
11149 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11150 {
11151 Elf_Internal_Rela rela;
11152 bfd_byte *rl;
11153
11154 rela.r_offset = dest;
ee67d69a
AM
11155 if (htab->opd_abi)
11156 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11157 else
11158 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11159 rela.r_addend = (stub_entry->target_value
11160 + stub_entry->target_section->output_offset
11161 + stub_entry->target_section->output_section->vma);
11162
33e44f2e
AM
11163 rl = (htab->elf.irelplt->contents
11164 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11165 * sizeof (Elf64_External_Rela)));
11166 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f 11167 stub_entry->plt_ent->plt.offset |= 1;
82e66161 11168 htab->local_ifunc_resolver = 1;
e054468f 11169 }
176a0d42
AM
11170
11171 off = (dest
06bcf541 11172 - elf_gp (info->output_bfd)
6f20ed8a 11173 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11174
ad8e1ba5 11175 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11176 {
25f53a85 11177 info->callbacks->einfo
695344c0 11178 /* xgettext:c-format */
bc30df16 11179 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
11180 stub_entry->h != NULL
11181 ? stub_entry->h->elf.root.root.string
11182 : "<local sym>");
721956f4 11183 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11184 htab->stub_error = TRUE;
11185 return FALSE;
721956f4
AM
11186 }
11187
e7d1c40c 11188 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11189 {
11190 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11191
6f20ed8a
AM
11192 stub_entry->group->stub_sec->size += pad;
11193 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11194 loc += pad;
11195 }
11196
176a0d42
AM
11197 r = NULL;
11198 if (info->emitrelocations)
11199 {
6f20ed8a 11200 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11201 ((PPC_HA (off) != 0)
11202 + (htab->opd_abi
e7d1c40c 11203 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11204 && PPC_HA (off + 16) == PPC_HA (off))
11205 : 1)));
176a0d42
AM
11206 if (r == NULL)
11207 return FALSE;
6f20ed8a 11208 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11209 if (bfd_big_endian (info->output_bfd))
11210 r[0].r_offset += 2;
176a0d42
AM
11211 r[0].r_addend = dest;
11212 }
a7f2871e
AM
11213 if (stub_entry->h != NULL
11214 && (stub_entry->h == htab->tls_get_addr_fd
11215 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11216 && htab->params->tls_get_addr_opt)
794e51c0 11217 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11218 else
794e51c0 11219 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11220 break;
11221
a4b6fadd
AM
11222 case ppc_stub_save_res:
11223 return TRUE;
11224
721956f4
AM
11225 default:
11226 BFD_FAIL ();
b34976b6 11227 return FALSE;
721956f4
AM
11228 }
11229
9e390558 11230 stub_entry->group->stub_sec->size += p - loc;
97b639ba 11231
e7d1c40c 11232 if (htab->params->emit_stub_syms)
97b639ba
AM
11233 {
11234 struct elf_link_hash_entry *h;
ee75fd95
AM
11235 size_t len1, len2;
11236 char *name;
11237 const char *const stub_str[] = { "long_branch",
11238 "long_branch_r2off",
11239 "plt_branch",
11240 "plt_branch_r2off",
794e51c0 11241 "plt_call",
ee75fd95
AM
11242 "plt_call" };
11243
11244 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11245 len2 = strlen (stub_entry->root.string);
11246 name = bfd_malloc (len1 + len2 + 2);
11247 if (name == NULL)
11248 return FALSE;
11249 memcpy (name, stub_entry->root.string, 9);
11250 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11251 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11252 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11253 if (h == NULL)
11254 return FALSE;
11255 if (h->root.type == bfd_link_hash_new)
11256 {
11257 h->root.type = bfd_link_hash_defined;
6f20ed8a 11258 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11259 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11260 h->ref_regular = 1;
11261 h->def_regular = 1;
11262 h->ref_regular_nonweak = 1;
11263 h->forced_local = 1;
11264 h->non_elf = 0;
2ec55de3 11265 h->root.linker_def = 1;
97b639ba
AM
11266 }
11267 }
11268
b34976b6 11269 return TRUE;
721956f4
AM
11270}
11271
11272/* As above, but don't actually build the stub. Just bump offset so
11273 we know stub section sizes, and select plt_branch stubs where
11274 long_branch stubs won't do. */
11275
b34976b6 11276static bfd_boolean
4ce794b7 11277ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11278{
11279 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11280 struct bfd_link_info *info;
721956f4
AM
11281 struct ppc_link_hash_table *htab;
11282 bfd_vma off;
11283 int size;
11284
11285 /* Massage our args to the form they really have. */
11286 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11287 info = in_arg;
11288
11289 htab = ppc_hash_table (info);
4dfe6ac6
NC
11290 if (htab == NULL)
11291 return FALSE;
721956f4 11292
a4b6fadd
AM
11293 if (stub_entry->h != NULL
11294 && stub_entry->h->save_res
11295 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11296 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11297 {
11298 /* Don't make stubs to out-of-line register save/restore
11299 functions. Instead, emit copies of the functions. */
11300 stub_entry->group->needs_save_res = 1;
11301 stub_entry->stub_type = ppc_stub_save_res;
11302 return TRUE;
11303 }
11304
794e51c0
AM
11305 if (stub_entry->stub_type == ppc_stub_plt_call
11306 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11307 {
e054468f
AM
11308 asection *plt;
11309 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11310 if (off >= (bfd_vma) -2)
411e1bfb 11311 abort ();
33e44f2e 11312 plt = htab->elf.splt;
25f23106
AM
11313 if (!htab->elf.dynamic_sections_created
11314 || stub_entry->h == NULL
11315 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11316 plt = htab->elf.iplt;
e054468f
AM
11317 off += (plt->output_offset
11318 + plt->output_section->vma
06bcf541 11319 - elf_gp (info->output_bfd)
6f20ed8a 11320 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11321
794e51c0 11322 size = plt_stub_size (htab, stub_entry, off);
d4aaa2a0
AM
11323 if (stub_entry->h != NULL
11324 && (stub_entry->h == htab->tls_get_addr_fd
11325 || stub_entry->h == htab->tls_get_addr)
11326 && htab->params->tls_get_addr_opt
11327 && (ALWAYS_EMIT_R2SAVE
11328 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11329 stub_entry->group->tls_get_addr_opt_bctrl
11330 = stub_entry->group->stub_sec->size + size - 5 * 4;
11331
e7d1c40c 11332 if (htab->params->plt_stub_align)
794e51c0 11333 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11334 if (info->emitrelocations)
11335 {
6f20ed8a 11336 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11337 += ((PPC_HA (off) != 0)
11338 + (htab->opd_abi
e7d1c40c 11339 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11340 && PPC_HA (off + 16) == PPC_HA (off))
11341 : 1));
6f20ed8a 11342 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11343 }
721956f4
AM
11344 }
11345 else
11346 {
ad8e1ba5
AM
11347 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11348 variants. */
ac2df442 11349 bfd_vma r2off = 0;
6911b7dc 11350 bfd_vma local_off = 0;
ac2df442 11351
721956f4
AM
11352 off = (stub_entry->target_value
11353 + stub_entry->target_section->output_offset
11354 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11355 off -= (stub_entry->group->stub_sec->size
11356 + stub_entry->group->stub_sec->output_offset
11357 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11358
ad8e1ba5
AM
11359 /* Reset the stub type from the plt variant in case we now
11360 can reach with a shorter stub. */
11361 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11362 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11363
11364 size = 4;
11365 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11366 {
25f53a85 11367 r2off = get_r2off (info, stub_entry);
a7c49797 11368 if (r2off == (bfd_vma) -1)
aa374f67
AM
11369 {
11370 htab->stub_error = TRUE;
11371 return FALSE;
11372 }
a7c49797 11373 size = 8;
ac2df442 11374 if (PPC_HA (r2off) != 0)
a7c49797
AM
11375 size += 4;
11376 if (PPC_LO (r2off) != 0)
11377 size += 4;
ac2df442 11378 off -= size - 4;
ad8e1ba5
AM
11379 }
11380
6911b7dc
AM
11381 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11382
b9e5796b
AM
11383 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11384 Do the same for -R objects without function descriptors. */
11385 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11386 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11387 && r2off == 0
11388 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11389 {
11390 struct ppc_branch_hash_entry *br_entry;
11391
11392 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11393 stub_entry->root.string + 9,
b34976b6 11394 TRUE, FALSE);
721956f4
AM
11395 if (br_entry == NULL)
11396 {
8de848d8 11397 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11398 stub_entry->root.string);
b34976b6
AM
11399 htab->stub_error = TRUE;
11400 return FALSE;
721956f4
AM
11401 }
11402
11403 if (br_entry->iter != htab->stub_iteration)
11404 {
11405 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11406 br_entry->offset = htab->brlt->size;
11407 htab->brlt->size += 8;
63bc6f6c 11408
ee75fd95 11409 if (htab->relbrlt != NULL)
eea6121a 11410 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11411 else if (info->emitrelocations)
11412 {
11413 htab->brlt->reloc_count += 1;
11414 htab->brlt->flags |= SEC_RELOC;
11415 }
721956f4 11416 }
ad8e1ba5
AM
11417
11418 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11419 off = (br_entry->offset
11420 + htab->brlt->output_offset
11421 + htab->brlt->output_section->vma
06bcf541 11422 - elf_gp (info->output_bfd)
6f20ed8a 11423 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11424
176a0d42
AM
11425 if (info->emitrelocations)
11426 {
6f20ed8a
AM
11427 stub_entry->group->stub_sec->reloc_count
11428 += 1 + (PPC_HA (off) != 0);
11429 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11430 }
11431
00f412ee 11432 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11433 {
11434 size = 12;
176a0d42 11435 if (PPC_HA (off) != 0)
ac2df442
AM
11436 size = 16;
11437 }
11438 else
11439 {
00f412ee 11440 size = 16;
176a0d42 11441 if (PPC_HA (off) != 0)
ac2df442
AM
11442 size += 4;
11443
11444 if (PPC_HA (r2off) != 0)
11445 size += 4;
00f412ee
AM
11446 if (PPC_LO (r2off) != 0)
11447 size += 4;
ac2df442 11448 }
1be5d8d3
AM
11449 if (!htab->params->speculate_indirect_jumps)
11450 size += 8;
721956f4 11451 }
84f5d08e
AM
11452 else if (info->emitrelocations)
11453 {
6f20ed8a
AM
11454 stub_entry->group->stub_sec->reloc_count += 1;
11455 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11456 }
721956f4
AM
11457 }
11458
6f20ed8a 11459 stub_entry->group->stub_sec->size += size;
b34976b6 11460 return TRUE;
721956f4
AM
11461}
11462
11463/* Set up various things so that we can make a list of input sections
11464 for each output section included in the link. Returns -1 on error,
cedb70c5 11465 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11466
11467int
e7d1c40c 11468ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11469{
6f20ed8a 11470 unsigned int id;
721956f4
AM
11471 bfd_size_type amt;
11472 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11473
4dfe6ac6
NC
11474 if (htab == NULL)
11475 return -1;
4c52953f 11476
6f20ed8a
AM
11477 htab->sec_info_arr_size = bfd_get_next_section_id ();
11478 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11479 htab->sec_info = bfd_zmalloc (amt);
11480 if (htab->sec_info == NULL)
721956f4
AM
11481 return -1;
11482
3d6f9012
AM
11483 /* Set toc_off for com, und, abs and ind sections. */
11484 for (id = 0; id < 3; id++)
6f20ed8a 11485 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11486
721956f4
AM
11487 return 1;
11488}
11489
927be08e
AM
11490/* Set up for first pass at multitoc partitioning. */
11491
11492void
11493ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11494{
11495 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11496
1c865ab2 11497 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11498 htab->toc_bfd = NULL;
11499 htab->toc_first_sec = NULL;
11500}
11501
e717da7e
AM
11502/* The linker repeatedly calls this function for each TOC input section
11503 and linker generated GOT section. Group input bfds such that the toc
927be08e 11504 within a group is less than 64k in size. */
ad8e1ba5 11505
927be08e 11506bfd_boolean
4ce794b7 11507ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11508{
11509 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11510 bfd_vma addr, off, limit;
ad8e1ba5 11511
4dfe6ac6
NC
11512 if (htab == NULL)
11513 return FALSE;
11514
927be08e 11515 if (!htab->second_toc_pass)
4c52953f 11516 {
927be08e 11517 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11518 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11519
11520 if (new_bfd)
bf102f86
AM
11521 {
11522 htab->toc_bfd = isec->owner;
11523 htab->toc_first_sec = isec;
11524 }
927be08e 11525
bf102f86
AM
11526 addr = isec->output_offset + isec->output_section->vma;
11527 off = addr - htab->toc_curr;
d77c8a4b
AM
11528 limit = 0x80008000;
11529 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11530 limit = 0x10000;
11531 if (off + isec->size > limit)
bf102f86
AM
11532 {
11533 addr = (htab->toc_first_sec->output_offset
11534 + htab->toc_first_sec->output_section->vma);
11535 htab->toc_curr = addr;
a27e685f 11536 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11537 }
99877b66 11538
927be08e
AM
11539 /* toc_curr is the base address of this toc group. Set elf_gp
11540 for the input section to be the offset relative to the
11541 output toc base plus 0x8000. Making the input elf_gp an
11542 offset allows us to move the toc as a whole without
11543 recalculating input elf_gp. */
06bcf541 11544 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11545 off += TOC_BASE_OFF;
11546
11547 /* Die if someone uses a linker script that doesn't keep input
11548 file .toc and .got together. */
a4fd3de5
AM
11549 if (new_bfd
11550 && elf_gp (isec->owner) != 0
927be08e
AM
11551 && elf_gp (isec->owner) != off)
11552 return FALSE;
11553
11554 elf_gp (isec->owner) = off;
11555 return TRUE;
4c52953f 11556 }
927be08e
AM
11557
11558 /* During the second pass toc_first_sec points to the start of
11559 a toc group, and toc_curr is used to track the old elf_gp.
11560 We use toc_bfd to ensure we only look at each bfd once. */
11561 if (htab->toc_bfd == isec->owner)
11562 return TRUE;
11563 htab->toc_bfd = isec->owner;
11564
11565 if (htab->toc_first_sec == NULL
11566 || htab->toc_curr != elf_gp (isec->owner))
11567 {
11568 htab->toc_curr = elf_gp (isec->owner);
11569 htab->toc_first_sec = isec;
11570 }
11571 addr = (htab->toc_first_sec->output_offset
11572 + htab->toc_first_sec->output_section->vma);
06bcf541 11573 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11574 elf_gp (isec->owner) = off;
11575
11576 return TRUE;
ad8e1ba5
AM
11577}
11578
927be08e
AM
11579/* Called via elf_link_hash_traverse to merge GOT entries for global
11580 symbol H. */
11581
11582static bfd_boolean
11583merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11584{
11585 if (h->root.type == bfd_link_hash_indirect)
11586 return TRUE;
11587
927be08e
AM
11588 merge_got_entries (&h->got.glist);
11589
11590 return TRUE;
11591}
11592
11593/* Called via elf_link_hash_traverse to allocate GOT entries for global
11594 symbol H. */
11595
11596static bfd_boolean
11597reallocate_got (struct elf_link_hash_entry *h, void *inf)
11598{
11599 struct got_entry *gent;
11600
11601 if (h->root.type == bfd_link_hash_indirect)
11602 return TRUE;
11603
927be08e
AM
11604 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11605 if (!gent->is_indirect)
11606 allocate_got (h, (struct bfd_link_info *) inf, gent);
11607 return TRUE;
11608}
11609
11610/* Called on the first multitoc pass after the last call to
11611 ppc64_elf_next_toc_section. This function removes duplicate GOT
11612 entries. */
11613
11614bfd_boolean
11615ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11616{
11617 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11618 struct bfd *ibfd, *ibfd2;
11619 bfd_boolean done_something;
11620
11621 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11622
7865406b
AM
11623 if (!htab->do_multi_toc)
11624 return FALSE;
11625
d0fae19d 11626 /* Merge global sym got entries within a toc group. */
927be08e
AM
11627 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11628
11629 /* And tlsld_got. */
c72f2fb2 11630 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11631 {
11632 struct got_entry *ent, *ent2;
11633
11634 if (!is_ppc64_elf (ibfd))
11635 continue;
11636
11637 ent = ppc64_tlsld_got (ibfd);
11638 if (!ent->is_indirect
11639 && ent->got.offset != (bfd_vma) -1)
11640 {
c72f2fb2 11641 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11642 {
11643 if (!is_ppc64_elf (ibfd2))
11644 continue;
11645
11646 ent2 = ppc64_tlsld_got (ibfd2);
11647 if (!ent2->is_indirect
11648 && ent2->got.offset != (bfd_vma) -1
11649 && elf_gp (ibfd2) == elf_gp (ibfd))
11650 {
11651 ent2->is_indirect = TRUE;
11652 ent2->got.ent = ent;
11653 }
11654 }
11655 }
11656 }
11657
11658 /* Zap sizes of got sections. */
33e44f2e
AM
11659 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11660 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11661 htab->got_reli_size = 0;
11662
c72f2fb2 11663 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11664 {
11665 asection *got, *relgot;
11666
11667 if (!is_ppc64_elf (ibfd))
11668 continue;
11669
11670 got = ppc64_elf_tdata (ibfd)->got;
11671 if (got != NULL)
11672 {
11673 got->rawsize = got->size;
11674 got->size = 0;
11675 relgot = ppc64_elf_tdata (ibfd)->relgot;
11676 relgot->rawsize = relgot->size;
11677 relgot->size = 0;
11678 }
11679 }
11680
11681 /* Now reallocate the got, local syms first. We don't need to
11682 allocate section contents again since we never increase size. */
c72f2fb2 11683 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11684 {
11685 struct got_entry **lgot_ents;
11686 struct got_entry **end_lgot_ents;
11687 struct plt_entry **local_plt;
11688 struct plt_entry **end_local_plt;
f961d9dd 11689 unsigned char *lgot_masks;
927be08e
AM
11690 bfd_size_type locsymcount;
11691 Elf_Internal_Shdr *symtab_hdr;
19e08130 11692 asection *s;
927be08e
AM
11693
11694 if (!is_ppc64_elf (ibfd))
11695 continue;
11696
11697 lgot_ents = elf_local_got_ents (ibfd);
11698 if (!lgot_ents)
11699 continue;
11700
11701 symtab_hdr = &elf_symtab_hdr (ibfd);
11702 locsymcount = symtab_hdr->sh_info;
11703 end_lgot_ents = lgot_ents + locsymcount;
11704 local_plt = (struct plt_entry **) end_lgot_ents;
11705 end_local_plt = local_plt + locsymcount;
f961d9dd 11706 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11707 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11708 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11709 {
11710 struct got_entry *ent;
11711
11712 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11713 {
19e08130
AM
11714 unsigned int ent_size = 8;
11715 unsigned int rel_size = sizeof (Elf64_External_Rela);
11716
d0fae19d
AM
11717 ent->got.offset = s->size;
11718 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11719 {
19e08130
AM
11720 ent_size *= 2;
11721 rel_size *= 2;
11722 }
11723 s->size += ent_size;
11724 if ((*lgot_masks & PLT_IFUNC) != 0)
11725 {
33e44f2e 11726 htab->elf.irelplt->size += rel_size;
19e08130
AM
11727 htab->got_reli_size += rel_size;
11728 }
0e1862bb 11729 else if (bfd_link_pic (info))
19e08130
AM
11730 {
11731 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11732 srel->size += rel_size;
d0fae19d
AM
11733 }
11734 }
927be08e
AM
11735 }
11736 }
11737
11738 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11739
c72f2fb2 11740 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11741 {
11742 struct got_entry *ent;
11743
11744 if (!is_ppc64_elf (ibfd))
11745 continue;
11746
11747 ent = ppc64_tlsld_got (ibfd);
11748 if (!ent->is_indirect
11749 && ent->got.offset != (bfd_vma) -1)
11750 {
11751 asection *s = ppc64_elf_tdata (ibfd)->got;
11752 ent->got.offset = s->size;
11753 s->size += 16;
0e1862bb 11754 if (bfd_link_pic (info))
927be08e
AM
11755 {
11756 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11757 srel->size += sizeof (Elf64_External_Rela);
11758 }
11759 }
11760 }
11761
33e44f2e 11762 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11763 if (!done_something)
c72f2fb2 11764 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11765 {
11766 asection *got;
11767
11768 if (!is_ppc64_elf (ibfd))
11769 continue;
11770
11771 got = ppc64_elf_tdata (ibfd)->got;
11772 if (got != NULL)
11773 {
11774 done_something = got->rawsize != got->size;
11775 if (done_something)
11776 break;
11777 }
11778 }
11779
11780 if (done_something)
e7d1c40c 11781 (*htab->params->layout_sections_again) ();
927be08e
AM
11782
11783 /* Set up for second pass over toc sections to recalculate elf_gp
11784 on input sections. */
11785 htab->toc_bfd = NULL;
11786 htab->toc_first_sec = NULL;
11787 htab->second_toc_pass = TRUE;
11788 return done_something;
11789}
11790
11791/* Called after second pass of multitoc partitioning. */
11792
11793void
11794ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11795{
11796 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11797
11798 /* After the second pass, toc_curr tracks the TOC offset used
11799 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11800 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11801}
11802
9b5ecbd0
AM
11803/* No toc references were found in ISEC. If the code in ISEC makes no
11804 calls, then there's no need to use toc adjusting stubs when branching
11805 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11806 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11807 needed, and 2 if a cyclical call-graph was found but no other reason
11808 for a stub was detected. If called from the top level, a return of
11809 2 means the same as a return of 0. */
9b5ecbd0
AM
11810
11811static int
4ce794b7 11812toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11813{
9b5ecbd0 11814 int ret;
70cc837d
AM
11815
11816 /* Mark this section as checked. */
11817 isec->call_check_done = 1;
9b5ecbd0 11818
772119ce
AM
11819 /* We know none of our code bearing sections will need toc stubs. */
11820 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11821 return 0;
11822
eea6121a 11823 if (isec->size == 0)
082c50f8
AM
11824 return 0;
11825
4c52953f
AM
11826 if (isec->output_section == NULL)
11827 return 0;
11828
4c52953f 11829 ret = 0;
70cc837d 11830 if (isec->reloc_count != 0)
9b5ecbd0 11831 {
70cc837d
AM
11832 Elf_Internal_Rela *relstart, *rel;
11833 Elf_Internal_Sym *local_syms;
11834 struct ppc_link_hash_table *htab;
2917689a 11835
70cc837d
AM
11836 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11837 info->keep_memory);
11838 if (relstart == NULL)
11839 return -1;
90aecf7a 11840
70cc837d
AM
11841 /* Look for branches to outside of this section. */
11842 local_syms = NULL;
11843 htab = ppc_hash_table (info);
11844 if (htab == NULL)
11845 return -1;
4c52953f 11846
70cc837d 11847 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11848 {
70cc837d
AM
11849 enum elf_ppc64_reloc_type r_type;
11850 unsigned long r_symndx;
11851 struct elf_link_hash_entry *h;
11852 struct ppc_link_hash_entry *eh;
11853 Elf_Internal_Sym *sym;
11854 asection *sym_sec;
11855 struct _opd_sec_data *opd;
11856 bfd_vma sym_value;
11857 bfd_vma dest;
11858
11859 r_type = ELF64_R_TYPE (rel->r_info);
11860 if (r_type != R_PPC64_REL24
11861 && r_type != R_PPC64_REL14
11862 && r_type != R_PPC64_REL14_BRTAKEN
11863 && r_type != R_PPC64_REL14_BRNTAKEN)
11864 continue;
4c52953f 11865
70cc837d
AM
11866 r_symndx = ELF64_R_SYM (rel->r_info);
11867 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11868 isec->owner))
4c52953f 11869 {
70cc837d
AM
11870 ret = -1;
11871 break;
11872 }
4c52953f 11873
70cc837d
AM
11874 /* Calls to dynamic lib functions go through a plt call stub
11875 that uses r2. */
11876 eh = (struct ppc_link_hash_entry *) h;
11877 if (eh != NULL
11878 && (eh->elf.plt.plist != NULL
11879 || (eh->oh != NULL
11880 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11881 {
11882 ret = 1;
11883 break;
4c52953f
AM
11884 }
11885
70cc837d
AM
11886 if (sym_sec == NULL)
11887 /* Ignore other undefined symbols. */
4c52953f 11888 continue;
4c52953f 11889
70cc837d
AM
11890 /* Assume branches to other sections not included in the
11891 link need stubs too, to cover -R and absolute syms. */
11892 if (sym_sec->output_section == NULL)
11893 {
11894 ret = 1;
11895 break;
11896 }
4c52953f 11897
70cc837d
AM
11898 if (h == NULL)
11899 sym_value = sym->st_value;
11900 else
11901 {
11902 if (h->root.type != bfd_link_hash_defined
11903 && h->root.type != bfd_link_hash_defweak)
11904 abort ();
11905 sym_value = h->root.u.def.value;
11906 }
11907 sym_value += rel->r_addend;
4c52953f 11908
70cc837d
AM
11909 /* If this branch reloc uses an opd sym, find the code section. */
11910 opd = get_opd_info (sym_sec);
11911 if (opd != NULL)
11912 {
11913 if (h == NULL && opd->adjust != NULL)
11914 {
11915 long adjust;
4c52953f 11916
92a9c616 11917 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11918 if (adjust == -1)
11919 /* Assume deleted functions won't ever be called. */
11920 continue;
11921 sym_value += adjust;
11922 }
4c52953f 11923
aef36ac1
AM
11924 dest = opd_entry_value (sym_sec, sym_value,
11925 &sym_sec, NULL, FALSE);
70cc837d
AM
11926 if (dest == (bfd_vma) -1)
11927 continue;
11928 }
11929 else
11930 dest = (sym_value
11931 + sym_sec->output_offset
11932 + sym_sec->output_section->vma);
4c52953f 11933
70cc837d
AM
11934 /* Ignore branch to self. */
11935 if (sym_sec == isec)
11936 continue;
4c52953f 11937
70cc837d
AM
11938 /* If the called function uses the toc, we need a stub. */
11939 if (sym_sec->has_toc_reloc
11940 || sym_sec->makes_toc_func_call)
4c52953f 11941 {
70cc837d 11942 ret = 1;
4c52953f
AM
11943 break;
11944 }
70cc837d
AM
11945
11946 /* Assume any branch that needs a long branch stub might in fact
11947 need a plt_branch stub. A plt_branch stub uses r2. */
11948 else if (dest - (isec->output_offset
11949 + isec->output_section->vma
6911b7dc
AM
11950 + rel->r_offset) + (1 << 25)
11951 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11952 ? h->other
11953 : sym->st_other))
4c52953f 11954 {
70cc837d
AM
11955 ret = 1;
11956 break;
11957 }
11958
11959 /* If calling back to a section in the process of being
11960 tested, we can't say for sure that no toc adjusting stubs
11961 are needed, so don't return zero. */
11962 else if (sym_sec->call_check_in_progress)
11963 ret = 2;
11964
11965 /* Branches to another section that itself doesn't have any TOC
11966 references are OK. Recursively call ourselves to check. */
11967 else if (!sym_sec->call_check_done)
11968 {
11969 int recur;
11970
11971 /* Mark current section as indeterminate, so that other
11972 sections that call back to current won't be marked as
11973 known. */
11974 isec->call_check_in_progress = 1;
11975 recur = toc_adjusting_stub_needed (info, sym_sec);
11976 isec->call_check_in_progress = 0;
11977
4c52953f
AM
11978 if (recur != 0)
11979 {
70cc837d
AM
11980 ret = recur;
11981 if (recur != 2)
11982 break;
4c52953f
AM
11983 }
11984 }
4c52953f 11985 }
70cc837d
AM
11986
11987 if (local_syms != NULL
11988 && (elf_symtab_hdr (isec->owner).contents
11989 != (unsigned char *) local_syms))
11990 free (local_syms);
11991 if (elf_section_data (isec)->relocs != relstart)
11992 free (relstart);
9b5ecbd0
AM
11993 }
11994
70cc837d
AM
11995 if ((ret & 1) == 0
11996 && isec->map_head.s != NULL
11997 && (strcmp (isec->output_section->name, ".init") == 0
11998 || strcmp (isec->output_section->name, ".fini") == 0))
11999 {
12000 if (isec->map_head.s->has_toc_reloc
12001 || isec->map_head.s->makes_toc_func_call)
12002 ret = 1;
12003 else if (!isec->map_head.s->call_check_done)
12004 {
12005 int recur;
12006 isec->call_check_in_progress = 1;
12007 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12008 isec->call_check_in_progress = 0;
12009 if (recur != 0)
12010 ret = recur;
12011 }
12012 }
12013
12014 if (ret == 1)
12015 isec->makes_toc_func_call = 1;
4c52953f 12016
9b5ecbd0
AM
12017 return ret;
12018}
12019
721956f4
AM
12020/* The linker repeatedly calls this function for each input section,
12021 in the order that input sections are linked into output sections.
12022 Build lists of input sections to determine groupings between which
12023 we may insert linker stubs. */
12024
9b5ecbd0 12025bfd_boolean
4ce794b7 12026ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12027{
12028 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12029
4dfe6ac6
NC
12030 if (htab == NULL)
12031 return FALSE;
12032
734b6cf9 12033 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12034 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12035 {
3d6f9012
AM
12036 /* This happens to make the list in reverse order,
12037 which is what we want. */
6f20ed8a
AM
12038 htab->sec_info[isec->id].u.list
12039 = htab->sec_info[isec->output_section->id].u.list;
12040 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12041 }
ad8e1ba5 12042
4c52953f 12043 if (htab->multi_toc_needed)
9b5ecbd0 12044 {
8b974ba3
AM
12045 /* Analyse sections that aren't already flagged as needing a
12046 valid toc pointer. Exclude .fixup for the linux kernel.
12047 .fixup contains branches, but only back to the function that
12048 hit an exception. */
12049 if (!(isec->has_toc_reloc
12050 || (isec->flags & SEC_CODE) == 0
12051 || strcmp (isec->name, ".fixup") == 0
12052 || isec->call_check_done))
12053 {
12054 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12055 return FALSE;
8b974ba3
AM
12056 }
12057 /* Make all sections use the TOC assigned for this object file.
12058 This will be wrong for pasted sections; We fix that in
12059 check_pasted_section(). */
12060 if (elf_gp (isec->owner) != 0)
12061 htab->toc_curr = elf_gp (isec->owner);
12062 }
12063
6f20ed8a 12064 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12065 return TRUE;
721956f4
AM
12066}
12067
70cc837d
AM
12068/* Check that all .init and .fini sections use the same toc, if they
12069 have toc relocs. */
12070
12071static bfd_boolean
12072check_pasted_section (struct bfd_link_info *info, const char *name)
12073{
12074 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12075
12076 if (o != NULL)
12077 {
12078 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12079 bfd_vma toc_off = 0;
12080 asection *i;
12081
12082 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12083 if (i->has_toc_reloc)
12084 {
12085 if (toc_off == 0)
6f20ed8a
AM
12086 toc_off = htab->sec_info[i->id].toc_off;
12087 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12088 return FALSE;
12089 }
6683a28d
AM
12090
12091 if (toc_off == 0)
12092 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12093 if (i->makes_toc_func_call)
12094 {
6f20ed8a 12095 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12096 break;
12097 }
12098
70cc837d
AM
12099 /* Make sure the whole pasted function uses the same toc offset. */
12100 if (toc_off != 0)
12101 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12102 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12103 }
12104 return TRUE;
12105}
12106
12107bfd_boolean
12108ppc64_elf_check_init_fini (struct bfd_link_info *info)
12109{
12110 return (check_pasted_section (info, ".init")
12111 & check_pasted_section (info, ".fini"));
12112}
12113
721956f4
AM
12114/* See whether we can group stub sections together. Grouping stub
12115 sections may result in fewer stubs. More importantly, we need to
12116 put all .init* and .fini* stubs at the beginning of the .init or
12117 .fini output sections respectively, because glibc splits the
12118 _init and _fini functions into multiple parts. Putting a stub in
12119 the middle of a function is not a good idea. */
12120
6f20ed8a
AM
12121static bfd_boolean
12122group_sections (struct bfd_link_info *info,
4ce794b7
AM
12123 bfd_size_type stub_group_size,
12124 bfd_boolean stubs_always_before_branch)
721956f4 12125{
6f20ed8a
AM
12126 struct ppc_link_hash_table *htab;
12127 asection *osec;
7c8fe5c4
AM
12128 bfd_boolean suppress_size_errors;
12129
6f20ed8a
AM
12130 htab = ppc_hash_table (info);
12131 if (htab == NULL)
12132 return FALSE;
12133
7c8fe5c4 12134 suppress_size_errors = FALSE;
7c8fe5c4
AM
12135 if (stub_group_size == 1)
12136 {
12137 /* Default values. */
12138 if (stubs_always_before_branch)
09f92717 12139 stub_group_size = 0x1e00000;
7c8fe5c4 12140 else
09f92717 12141 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12142 suppress_size_errors = TRUE;
12143 }
12144
6f20ed8a 12145 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12146 {
6f20ed8a
AM
12147 asection *tail;
12148
12149 if (osec->id >= htab->sec_info_arr_size)
12150 continue;
12151
12152 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12153 while (tail != NULL)
721956f4 12154 {
734b6cf9
AM
12155 asection *curr;
12156 asection *prev;
12157 bfd_size_type total;
12158 bfd_boolean big_sec;
12159 bfd_vma curr_toc;
6f20ed8a 12160 struct map_stub *group;
09f92717 12161 bfd_size_type group_size;
734b6cf9
AM
12162
12163 curr = tail;
eea6121a 12164 total = tail->size;
09f92717
AM
12165 group_size = (ppc64_elf_section_data (tail) != NULL
12166 && ppc64_elf_section_data (tail)->has_14bit_branch
12167 ? stub_group_size >> 10 : stub_group_size);
12168
12169 big_sec = total > group_size;
7c8fe5c4 12170 if (big_sec && !suppress_size_errors)
695344c0 12171 /* xgettext:c-format */
4eca0228
AM
12172 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12173 tail->owner, tail);
6f20ed8a 12174 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12175
6f20ed8a 12176 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12177 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12178 < (ppc64_elf_section_data (prev) != NULL
12179 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12180 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12181 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12182 curr = prev;
12183
12184 /* OK, the size from the start of CURR to the end is less
09f92717 12185 than group_size and thus can be handled by one stub
734b6cf9 12186 section. (or the tail section is itself larger than
09f92717
AM
12187 group_size, in which case we may be toast.) We should
12188 really be keeping track of the total size of stubs added
12189 here, as stubs contribute to the final output section
12190 size. That's a little tricky, and this way will only
12191 break if stubs added make the total size more than 2^25,
12192 ie. for the default stub_group_size, if stubs total more
12193 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12194 group = bfd_alloc (curr->owner, sizeof (*group));
12195 if (group == NULL)
12196 return FALSE;
12197 group->link_sec = curr;
12198 group->stub_sec = NULL;
a4b6fadd 12199 group->needs_save_res = 0;
d4aaa2a0 12200 group->tls_get_addr_opt_bctrl = -1u;
a4b6fadd
AM
12201 group->next = htab->group;
12202 htab->group = group;
734b6cf9 12203 do
721956f4 12204 {
6f20ed8a 12205 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12206 /* Set up this stub group. */
6f20ed8a 12207 htab->sec_info[tail->id].u.group = group;
721956f4 12208 }
734b6cf9
AM
12209 while (tail != curr && (tail = prev) != NULL);
12210
09f92717 12211 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12212 bytes before the stub section can be handled by it too.
12213 Don't do this if we have a really large section after the
12214 stubs, as adding more stubs increases the chance that
12215 branches may not reach into the stub section. */
12216 if (!stubs_always_before_branch && !big_sec)
12217 {
12218 total = 0;
12219 while (prev != NULL
12220 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12221 < (ppc64_elf_section_data (prev) != NULL
12222 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12223 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12224 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12225 {
12226 tail = prev;
6f20ed8a
AM
12227 prev = htab->sec_info[tail->id].u.list;
12228 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12229 }
12230 }
12231 tail = prev;
721956f4
AM
12232 }
12233 }
6f20ed8a 12234 return TRUE;
721956f4
AM
12235}
12236
58d180e8
AM
12237static const unsigned char glink_eh_frame_cie[] =
12238{
12239 0, 0, 0, 16, /* length. */
12240 0, 0, 0, 0, /* id. */
12241 1, /* CIE version. */
12242 'z', 'R', 0, /* Augmentation string. */
12243 4, /* Code alignment. */
12244 0x78, /* Data alignment. */
12245 65, /* RA reg. */
12246 1, /* Augmentation size. */
12247 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12248 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12249};
12250
d4aaa2a0
AM
12251static size_t
12252stub_eh_frame_size (struct map_stub *group, size_t align)
12253{
12254 size_t this_size = 17;
12255 if (group->tls_get_addr_opt_bctrl != -1u)
12256 {
12257 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12258 if (to_bctrl < 64)
12259 this_size += 1;
12260 else if (to_bctrl < 256)
12261 this_size += 2;
12262 else if (to_bctrl < 65536)
12263 this_size += 3;
12264 else
12265 this_size += 5;
12266 this_size += 6;
12267 }
12268 this_size = (this_size + align - 1) & -align;
12269 return this_size;
12270}
12271
d969d15f
AM
12272/* Stripping output sections is normally done before dynamic section
12273 symbols have been allocated. This function is called later, and
12274 handles cases like htab->brlt which is mapped to its own output
12275 section. */
12276
12277static void
12278maybe_strip_output (struct bfd_link_info *info, asection *isec)
12279{
12280 if (isec->size == 0
12281 && isec->output_section->size == 0
53d8967a 12282 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12283 && !bfd_section_removed_from_list (info->output_bfd,
12284 isec->output_section)
12285 && elf_section_data (isec->output_section)->dynindx == 0)
12286 {
12287 isec->output_section->flags |= SEC_EXCLUDE;
12288 bfd_section_list_remove (info->output_bfd, isec->output_section);
12289 info->output_bfd->section_count--;
12290 }
12291}
12292
721956f4
AM
12293/* Determine and set the size of the stub section for a final link.
12294
12295 The basic idea here is to examine all the relocations looking for
12296 PC-relative calls to a target that is unreachable with a "bl"
12297 instruction. */
12298
b34976b6 12299bfd_boolean
e7d1c40c 12300ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12301{
12302 bfd_size_type stub_group_size;
b34976b6 12303 bfd_boolean stubs_always_before_branch;
721956f4
AM
12304 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12305
4dfe6ac6
NC
12306 if (htab == NULL)
12307 return FALSE;
12308
0e1862bb 12309 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12310 htab->params->plt_thread_safe = 1;
b9e5796b 12311 if (!htab->opd_abi)
e7d1c40c
AM
12312 htab->params->plt_thread_safe = 0;
12313 else if (htab->params->plt_thread_safe == -1)
794e51c0 12314 {
e2458743 12315 static const char *const thread_starter[] =
794e51c0
AM
12316 {
12317 "pthread_create",
12318 /* libstdc++ */
12319 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12320 /* librt */
12321 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12322 "mq_notify", "create_timer",
12323 /* libanl */
12324 "getaddrinfo_a",
12325 /* libgomp */
2300b5a1 12326 "GOMP_parallel",
794e51c0 12327 "GOMP_parallel_start",
2300b5a1 12328 "GOMP_parallel_loop_static",
794e51c0 12329 "GOMP_parallel_loop_static_start",
2300b5a1 12330 "GOMP_parallel_loop_dynamic",
794e51c0 12331 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12332 "GOMP_parallel_loop_guided",
794e51c0 12333 "GOMP_parallel_loop_guided_start",
2300b5a1 12334 "GOMP_parallel_loop_runtime",
794e51c0 12335 "GOMP_parallel_loop_runtime_start",
2300b5a1 12336 "GOMP_parallel_sections",
68ffbac6 12337 "GOMP_parallel_sections_start",
f9dffbf0
AM
12338 /* libgo */
12339 "__go_go",
794e51c0
AM
12340 };
12341 unsigned i;
12342
a4b6fadd 12343 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12344 {
12345 struct elf_link_hash_entry *h;
12346 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12347 FALSE, FALSE, TRUE);
e7d1c40c
AM
12348 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12349 if (htab->params->plt_thread_safe)
794e51c0
AM
12350 break;
12351 }
12352 }
e7d1c40c
AM
12353 stubs_always_before_branch = htab->params->group_size < 0;
12354 if (htab->params->group_size < 0)
12355 stub_group_size = -htab->params->group_size;
721956f4 12356 else
e7d1c40c 12357 stub_group_size = htab->params->group_size;
721956f4 12358
6f20ed8a
AM
12359 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12360 return FALSE;
721956f4 12361
c9301e31
AM
12362#define STUB_SHRINK_ITER 20
12363 /* Loop until no stubs added. After iteration 20 of this loop we may
12364 exit on a stub section shrinking. This is to break out of a
12365 pathological case where adding stubs on one iteration decreases
12366 section gaps (perhaps due to alignment), which then requires
12367 fewer or smaller stubs on the next iteration. */
12368
721956f4
AM
12369 while (1)
12370 {
12371 bfd *input_bfd;
12372 unsigned int bfd_indx;
a4b6fadd 12373 struct map_stub *group;
721956f4
AM
12374
12375 htab->stub_iteration += 1;
721956f4
AM
12376
12377 for (input_bfd = info->input_bfds, bfd_indx = 0;
12378 input_bfd != NULL;
c72f2fb2 12379 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12380 {
12381 Elf_Internal_Shdr *symtab_hdr;
12382 asection *section;
6cdc0ccc 12383 Elf_Internal_Sym *local_syms = NULL;
721956f4 12384
0c8d6e5c 12385 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12386 continue;
12387
721956f4 12388 /* We'll need the symbol table in a second. */
0ffa91dd 12389 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12390 if (symtab_hdr->sh_info == 0)
12391 continue;
12392
721956f4
AM
12393 /* Walk over each section attached to the input bfd. */
12394 for (section = input_bfd->sections;
12395 section != NULL;
12396 section = section->next)
12397 {
721956f4 12398 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12399
12400 /* If there aren't any relocs, then there's nothing more
12401 to do. */
12402 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12403 || (section->flags & SEC_ALLOC) == 0
12404 || (section->flags & SEC_LOAD) == 0
12405 || (section->flags & SEC_CODE) == 0
721956f4
AM
12406 || section->reloc_count == 0)
12407 continue;
12408
12409 /* If this section is a link-once section that will be
12410 discarded, then don't create any stubs. */
12411 if (section->output_section == NULL
927be08e 12412 || section->output_section->owner != info->output_bfd)
721956f4
AM
12413 continue;
12414
1e2f5b6e
AM
12415 /* Get the relocs. */
12416 internal_relocs
4ce794b7 12417 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12418 info->keep_memory);
721956f4 12419 if (internal_relocs == NULL)
1e2f5b6e 12420 goto error_ret_free_local;
721956f4
AM
12421
12422 /* Now examine each relocation. */
12423 irela = internal_relocs;
12424 irelaend = irela + section->reloc_count;
12425 for (; irela < irelaend; irela++)
12426 {
4ce794b7
AM
12427 enum elf_ppc64_reloc_type r_type;
12428 unsigned int r_indx;
721956f4
AM
12429 enum ppc_stub_type stub_type;
12430 struct ppc_stub_hash_entry *stub_entry;
8387904d 12431 asection *sym_sec, *code_sec;
e054468f 12432 bfd_vma sym_value, code_value;
721956f4 12433 bfd_vma destination;
6911b7dc 12434 unsigned long local_off;
8843416a 12435 bfd_boolean ok_dest;
721956f4 12436 struct ppc_link_hash_entry *hash;
8387904d 12437 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12438 struct elf_link_hash_entry *h;
12439 Elf_Internal_Sym *sym;
721956f4
AM
12440 char *stub_name;
12441 const asection *id_sec;
74f0fb50 12442 struct _opd_sec_data *opd;
e054468f 12443 struct plt_entry *plt_ent;
721956f4
AM
12444
12445 r_type = ELF64_R_TYPE (irela->r_info);
12446 r_indx = ELF64_R_SYM (irela->r_info);
12447
4ce794b7 12448 if (r_type >= R_PPC64_max)
721956f4
AM
12449 {
12450 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12451 goto error_ret_free_internal;
721956f4
AM
12452 }
12453
12454 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12455 if (r_type != R_PPC64_REL24
12456 && r_type != R_PPC64_REL14
12457 && r_type != R_PPC64_REL14_BRTAKEN
12458 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12459 continue;
12460
12461 /* Now determine the call target, its name, value,
12462 section. */
411e1bfb
AM
12463 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12464 r_indx, input_bfd))
12465 goto error_ret_free_internal;
12466 hash = (struct ppc_link_hash_entry *) h;
12467
8843416a 12468 ok_dest = FALSE;
8387904d 12469 fdh = NULL;
7fe2b9a6 12470 sym_value = 0;
411e1bfb 12471 if (hash == NULL)
721956f4 12472 {
411e1bfb 12473 sym_value = sym->st_value;
c27b8c2a
AM
12474 if (sym_sec != NULL
12475 && sym_sec->output_section != NULL)
12476 ok_dest = TRUE;
721956f4 12477 }
7fe2b9a6
AM
12478 else if (hash->elf.root.type == bfd_link_hash_defined
12479 || hash->elf.root.type == bfd_link_hash_defweak)
12480 {
12481 sym_value = hash->elf.root.u.def.value;
12482 if (sym_sec->output_section != NULL)
12483 ok_dest = TRUE;
12484 }
12485 else if (hash->elf.root.type == bfd_link_hash_undefweak
12486 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12487 {
99877b66 12488 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12489 use the func descriptor sym instead if it is
12490 defined. */
ceb1f1ef 12491 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12492 && hash->oh != NULL)
8387904d 12493 {
8c5b4e52 12494 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12495 if (fdh->elf.root.type == bfd_link_hash_defined
12496 || fdh->elf.root.type == bfd_link_hash_defweak)
12497 {
12498 sym_sec = fdh->elf.root.u.def.section;
12499 sym_value = fdh->elf.root.u.def.value;
12500 if (sym_sec->output_section != NULL)
12501 ok_dest = TRUE;
12502 }
99877b66
AM
12503 else
12504 fdh = NULL;
8387904d 12505 }
7fe2b9a6
AM
12506 }
12507 else
12508 {
12509 bfd_set_error (bfd_error_bad_value);
12510 goto error_ret_free_internal;
721956f4
AM
12511 }
12512
8843416a 12513 destination = 0;
6911b7dc 12514 local_off = 0;
8843416a
AM
12515 if (ok_dest)
12516 {
12517 sym_value += irela->r_addend;
12518 destination = (sym_value
12519 + sym_sec->output_offset
12520 + sym_sec->output_section->vma);
6911b7dc
AM
12521 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12522 ? hash->elf.other
12523 : sym->st_other);
8843416a
AM
12524 }
12525
8387904d 12526 code_sec = sym_sec;
e054468f 12527 code_value = sym_value;
74f0fb50
AM
12528 opd = get_opd_info (sym_sec);
12529 if (opd != NULL)
8387904d
AM
12530 {
12531 bfd_vma dest;
12532
74f0fb50 12533 if (hash == NULL && opd->adjust != NULL)
8387904d 12534 {
51aecdc5 12535 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12536 if (adjust == -1)
12537 continue;
e054468f 12538 code_value += adjust;
8387904d
AM
12539 sym_value += adjust;
12540 }
12541 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12542 &code_sec, &code_value, FALSE);
8387904d
AM
12543 if (dest != (bfd_vma) -1)
12544 {
12545 destination = dest;
12546 if (fdh != NULL)
12547 {
12548 /* Fixup old ABI sym to point at code
12549 entry. */
99877b66 12550 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12551 hash->elf.root.u.def.section = code_sec;
e054468f 12552 hash->elf.root.u.def.value = code_value;
8387904d
AM
12553 }
12554 }
12555 }
12556
721956f4 12557 /* Determine what (if any) linker stub is needed. */
e054468f 12558 plt_ent = NULL;
721956f4 12559 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12560 &plt_ent, destination,
12561 local_off);
ad8e1ba5
AM
12562
12563 if (stub_type != ppc_stub_plt_call)
12564 {
12565 /* Check whether we need a TOC adjusting stub.
12566 Since the linker pastes together pieces from
12567 different object files when creating the
12568 _init and _fini functions, it may be that a
12569 call to what looks like a local sym is in
12570 fact a call needing a TOC adjustment. */
8387904d
AM
12571 if (code_sec != NULL
12572 && code_sec->output_section != NULL
6f20ed8a
AM
12573 && (htab->sec_info[code_sec->id].toc_off
12574 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12575 && (code_sec->has_toc_reloc
12576 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12577 stub_type = ppc_stub_long_branch_r2off;
12578 }
12579
721956f4
AM
12580 if (stub_type == ppc_stub_none)
12581 continue;
12582
411e1bfb
AM
12583 /* __tls_get_addr calls might be eliminated. */
12584 if (stub_type != ppc_stub_plt_call
12585 && hash != NULL
8387904d
AM
12586 && (hash == htab->tls_get_addr
12587 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12588 && section->has_tls_reloc
12589 && irela != internal_relocs)
12590 {
12591 /* Get tls info. */
f961d9dd 12592 unsigned char *tls_mask;
411e1bfb 12593
3a71aa26 12594 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12595 irela - 1, input_bfd))
12596 goto error_ret_free_internal;
e7b938ca 12597 if (*tls_mask != 0)
411e1bfb
AM
12598 continue;
12599 }
12600
f378ab09 12601 if (stub_type == ppc_stub_plt_call)
794e51c0 12602 {
6e1816be
AM
12603 if (!htab->opd_abi
12604 && htab->params->plt_localentry0 != 0
12605 && is_elfv2_localentry0 (&hash->elf))
12606 htab->has_plt_localentry0 = 1;
12607 else if (irela + 1 < irelaend
12608 && irela[1].r_offset == irela->r_offset + 4
12609 && (ELF64_R_TYPE (irela[1].r_info)
12610 == R_PPC64_TOCSAVE))
f378ab09
AM
12611 {
12612 if (!tocsave_find (htab, INSERT,
12613 &local_syms, irela + 1, input_bfd))
12614 goto error_ret_free_internal;
12615 }
f378ab09
AM
12616 else
12617 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12618 }
3b421ab3 12619
721956f4 12620 /* Support for grouping stub sections. */
6f20ed8a 12621 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12622
12623 /* Get the name of this stub. */
12624 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12625 if (!stub_name)
12626 goto error_ret_free_internal;
12627
12628 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12629 stub_name, FALSE, FALSE);
721956f4
AM
12630 if (stub_entry != NULL)
12631 {
12632 /* The proper stub has already been created. */
12633 free (stub_name);
794e51c0
AM
12634 if (stub_type == ppc_stub_plt_call_r2save)
12635 stub_entry->stub_type = stub_type;
721956f4
AM
12636 continue;
12637 }
12638
25f53a85 12639 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12640 if (stub_entry == NULL)
12641 {
12642 free (stub_name);
6cdc0ccc
AM
12643 error_ret_free_internal:
12644 if (elf_section_data (section)->relocs == NULL)
12645 free (internal_relocs);
12646 error_ret_free_local:
12647 if (local_syms != NULL
12648 && (symtab_hdr->contents
12649 != (unsigned char *) local_syms))
12650 free (local_syms);
b34976b6 12651 return FALSE;
721956f4
AM
12652 }
12653
ad8e1ba5 12654 stub_entry->stub_type = stub_type;
794e51c0
AM
12655 if (stub_type != ppc_stub_plt_call
12656 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12657 {
12658 stub_entry->target_value = code_value;
12659 stub_entry->target_section = code_sec;
12660 }
12661 else
12662 {
12663 stub_entry->target_value = sym_value;
12664 stub_entry->target_section = sym_sec;
12665 }
721956f4 12666 stub_entry->h = hash;
e054468f 12667 stub_entry->plt_ent = plt_ent;
6911b7dc 12668 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12669
12670 if (stub_entry->h != NULL)
12671 htab->stub_globals += 1;
721956f4
AM
12672 }
12673
12674 /* We're done with the internal relocs, free them. */
6cdc0ccc 12675 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12676 free (internal_relocs);
721956f4 12677 }
6cdc0ccc
AM
12678
12679 if (local_syms != NULL
12680 && symtab_hdr->contents != (unsigned char *) local_syms)
12681 {
12682 if (!info->keep_memory)
12683 free (local_syms);
12684 else
12685 symtab_hdr->contents = (unsigned char *) local_syms;
12686 }
721956f4
AM
12687 }
12688
5c3dead3 12689 /* We may have added some stubs. Find out the new size of the
721956f4 12690 stub sections. */
d4aaa2a0
AM
12691 for (group = htab->group; group != NULL; group = group->next)
12692 if (group->stub_sec != NULL)
ee75fd95 12693 {
d4aaa2a0
AM
12694 asection *stub_sec = group->stub_sec;
12695
ea3d7d1c
AM
12696 if (htab->stub_iteration <= STUB_SHRINK_ITER
12697 || stub_sec->rawsize < stub_sec->size)
12698 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12699 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12700 stub_sec->size = 0;
12701 stub_sec->reloc_count = 0;
84f5d08e 12702 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12703 }
eea6121a
AM
12704
12705 htab->brlt->size = 0;
84f5d08e
AM
12706 htab->brlt->reloc_count = 0;
12707 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12708 if (htab->relbrlt != NULL)
eea6121a 12709 htab->relbrlt->size = 0;
721956f4 12710
63bc6f6c 12711 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12712
a4b6fadd
AM
12713 for (group = htab->group; group != NULL; group = group->next)
12714 if (group->needs_save_res)
12715 group->stub_sec->size += htab->sfpr->size;
12716
176a0d42
AM
12717 if (info->emitrelocations
12718 && htab->glink != NULL && htab->glink->size != 0)
12719 {
12720 htab->glink->reloc_count = 1;
12721 htab->glink->flags |= SEC_RELOC;
12722 }
12723
58d180e8
AM
12724 if (htab->glink_eh_frame != NULL
12725 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12726 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12727 {
2e0ce1c8 12728 size_t size = 0, align = 4;
58d180e8 12729
d4aaa2a0
AM
12730 for (group = htab->group; group != NULL; group = group->next)
12731 if (group->stub_sec != NULL)
12732 size += stub_eh_frame_size (group, align);
58d180e8 12733 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12734 size += (24 + align - 1) & -align;
58d180e8 12735 if (size != 0)
2e0ce1c8
AM
12736 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12737 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12738 size = (size + align - 1) & -align;
58d180e8
AM
12739 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12740 htab->glink_eh_frame->size = size;
12741 }
12742
e7d1c40c 12743 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12744 for (group = htab->group; group != NULL; group = group->next)
12745 if (group->stub_sec != NULL)
691d2e9a
AM
12746 {
12747 int align = abs (htab->params->plt_stub_align);
12748 group->stub_sec->size
12749 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12750 }
d4aaa2a0
AM
12751
12752 for (group = htab->group; group != NULL; group = group->next)
12753 if (group->stub_sec != NULL
12754 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12755 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12756 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12757 break;
12758
d4aaa2a0 12759 if (group == NULL
58d180e8
AM
12760 && (htab->glink_eh_frame == NULL
12761 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12762 break;
12763
721956f4 12764 /* Ask the linker to do its stuff. */
e7d1c40c 12765 (*htab->params->layout_sections_again) ();
721956f4
AM
12766 }
12767
da44f4e5
AM
12768 if (htab->glink_eh_frame != NULL
12769 && htab->glink_eh_frame->size != 0)
12770 {
12771 bfd_vma val;
12772 bfd_byte *p, *last_fde;
12773 size_t last_fde_len, size, align, pad;
d4aaa2a0 12774 struct map_stub *group;
da44f4e5
AM
12775
12776 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12777 if (p == NULL)
12778 return FALSE;
12779 htab->glink_eh_frame->contents = p;
12780 last_fde = p;
2e0ce1c8 12781 align = 4;
da44f4e5
AM
12782
12783 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12784 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12785 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12786 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12787 p += last_fde_len + 4;
da44f4e5 12788
d4aaa2a0
AM
12789 for (group = htab->group; group != NULL; group = group->next)
12790 if (group->stub_sec != NULL)
da44f4e5
AM
12791 {
12792 last_fde = p;
d4aaa2a0 12793 last_fde_len = stub_eh_frame_size (group, align) - 4;
da44f4e5 12794 /* FDE length. */
2e0ce1c8 12795 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12796 p += 4;
12797 /* CIE pointer. */
12798 val = p - htab->glink_eh_frame->contents;
12799 bfd_put_32 (htab->elf.dynobj, val, p);
12800 p += 4;
12801 /* Offset to stub section, written later. */
12802 p += 4;
12803 /* stub section size. */
d4aaa2a0 12804 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12805 p += 4;
12806 /* Augmentation. */
12807 p += 1;
d4aaa2a0
AM
12808 if (group->tls_get_addr_opt_bctrl != -1u)
12809 {
12810 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12811
12812 /* This FDE needs more than just the default.
12813 Describe __tls_get_addr_opt stub LR. */
12814 if (to_bctrl < 64)
12815 *p++ = DW_CFA_advance_loc + to_bctrl;
12816 else if (to_bctrl < 256)
12817 {
12818 *p++ = DW_CFA_advance_loc1;
12819 *p++ = to_bctrl;
12820 }
12821 else if (to_bctrl < 65536)
12822 {
12823 *p++ = DW_CFA_advance_loc2;
12824 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12825 p += 2;
12826 }
12827 else
12828 {
12829 *p++ = DW_CFA_advance_loc4;
12830 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12831 p += 4;
12832 }
12833 *p++ = DW_CFA_offset_extended_sf;
12834 *p++ = 65;
12835 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12836 *p++ = DW_CFA_advance_loc + 4;
12837 *p++ = DW_CFA_restore_extended;
12838 *p++ = 65;
12839 }
da44f4e5 12840 /* Pad. */
d4aaa2a0 12841 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12842 }
12843 if (htab->glink != NULL && htab->glink->size != 0)
12844 {
12845 last_fde = p;
2e0ce1c8 12846 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12847 /* FDE length. */
2e0ce1c8 12848 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12849 p += 4;
12850 /* CIE pointer. */
12851 val = p - htab->glink_eh_frame->contents;
12852 bfd_put_32 (htab->elf.dynobj, val, p);
12853 p += 4;
12854 /* Offset to .glink, written later. */
12855 p += 4;
12856 /* .glink size. */
12857 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12858 p += 4;
12859 /* Augmentation. */
12860 p += 1;
12861
12862 *p++ = DW_CFA_advance_loc + 1;
12863 *p++ = DW_CFA_register;
12864 *p++ = 65;
9f08fa5c 12865 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 12866 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
12867 *p++ = DW_CFA_restore_extended;
12868 *p++ = 65;
2e0ce1c8 12869 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12870 }
12871 /* Subsume any padding into the last FDE if user .eh_frame
12872 sections are aligned more than glink_eh_frame. Otherwise any
12873 zero padding will be seen as a terminator. */
2e0ce1c8 12874 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12875 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12876 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12877 htab->glink_eh_frame->size = size + pad;
12878 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12879 }
12880
d969d15f
AM
12881 maybe_strip_output (info, htab->brlt);
12882 if (htab->glink_eh_frame != NULL)
12883 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12884
b34976b6 12885 return TRUE;
721956f4
AM
12886}
12887
12888/* Called after we have determined section placement. If sections
805fc799 12889 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12890
805fc799 12891bfd_vma
1c865ab2 12892ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12893{
805fc799 12894 asection *s;
a27e685f 12895 bfd_vma TOCstart, adjust;
721956f4 12896
43417696
AM
12897 if (info != NULL)
12898 {
12899 struct elf_link_hash_entry *h;
12900 struct elf_link_hash_table *htab = elf_hash_table (info);
12901
12902 if (is_elf_hash_table (htab)
12903 && htab->hgot != NULL)
12904 h = htab->hgot;
12905 else
12906 {
12907 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12908 if (is_elf_hash_table (htab))
12909 htab->hgot = h;
12910 }
12911 if (h != NULL
12912 && h->root.type == bfd_link_hash_defined
12913 && !h->root.linker_def
12914 && (!is_elf_hash_table (htab)
12915 || h->def_regular))
12916 {
12917 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12918 + h->root.u.def.section->output_offset
12919 + h->root.u.def.section->output_section->vma);
12920 _bfd_set_gp_value (obfd, TOCstart);
12921 return TOCstart;
12922 }
12923 }
12924
805fc799
AM
12925 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12926 order. The TOC starts where the first of these sections starts. */
12927 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12928 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12929 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12930 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12931 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12932 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12933 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12934 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12935 {
12936 /* This may happen for
12937 o references to TOC base (SYM@toc / TOC[tc0]) without a
12938 .toc directive
12939 o bad linker script
12940 o --gc-sections and empty TOC sections
12941
12942 FIXME: Warn user? */
12943
12944 /* Look for a likely section. We probably won't even be
12945 using TOCstart. */
12946 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12947 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12948 | SEC_EXCLUDE))
805fc799
AM
12949 == (SEC_ALLOC | SEC_SMALL_DATA))
12950 break;
721956f4 12951 if (s == NULL)
805fc799 12952 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12953 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12954 == (SEC_ALLOC | SEC_SMALL_DATA))
12955 break;
721956f4 12956 if (s == NULL)
805fc799 12957 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12958 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12959 == SEC_ALLOC)
805fc799 12960 break;
721956f4 12961 if (s == NULL)
805fc799 12962 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12963 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12964 break;
12965 }
721956f4 12966
805fc799
AM
12967 TOCstart = 0;
12968 if (s != NULL)
12969 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12970
a27e685f
AM
12971 /* Force alignment. */
12972 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12973 TOCstart -= adjust;
1c865ab2
AM
12974 _bfd_set_gp_value (obfd, TOCstart);
12975
810d4e75 12976 if (info != NULL && s != NULL)
1c865ab2
AM
12977 {
12978 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12979
810d4e75
AM
12980 if (htab != NULL)
12981 {
12982 if (htab->elf.hgot != NULL)
12983 {
a27e685f 12984 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12985 htab->elf.hgot->root.u.def.section = s;
12986 }
12987 }
12988 else
1c865ab2 12989 {
810d4e75
AM
12990 struct bfd_link_hash_entry *bh = NULL;
12991 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12992 s, TOC_BASE_OFF - adjust,
12993 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12994 }
12995 }
805fc799 12996 return TOCstart;
721956f4
AM
12997}
12998
a345bc8d
AM
12999/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13000 write out any global entry stubs. */
13001
13002static bfd_boolean
13003build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
13004{
13005 struct bfd_link_info *info;
13006 struct ppc_link_hash_table *htab;
13007 struct plt_entry *pent;
13008 asection *s;
13009
13010 if (h->root.type == bfd_link_hash_indirect)
13011 return TRUE;
13012
13013 if (!h->pointer_equality_needed)
13014 return TRUE;
13015
13016 if (h->def_regular)
13017 return TRUE;
13018
13019 info = inf;
13020 htab = ppc_hash_table (info);
13021 if (htab == NULL)
13022 return FALSE;
13023
9e390558 13024 s = htab->global_entry;
a345bc8d
AM
13025 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13026 if (pent->plt.offset != (bfd_vma) -1
13027 && pent->addend == 0)
13028 {
13029 bfd_byte *p;
13030 asection *plt;
13031 bfd_vma off;
13032
a345bc8d 13033 p = s->contents + h->root.u.def.value;
33e44f2e 13034 plt = htab->elf.splt;
a345bc8d
AM
13035 if (!htab->elf.dynamic_sections_created
13036 || h->dynindx == -1)
33e44f2e 13037 plt = htab->elf.iplt;
a345bc8d
AM
13038 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13039 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13040
13041 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13042 {
13043 info->callbacks->einfo
13044 (_("%P: linkage table error against `%T'\n"),
13045 h->root.root.string);
13046 bfd_set_error (bfd_error_bad_value);
13047 htab->stub_error = TRUE;
13048 }
13049
7341d5e2
AM
13050 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13051 if (htab->params->emit_stub_syms)
13052 {
13053 size_t len = strlen (h->root.root.string);
13054 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13055
13056 if (name == NULL)
13057 return FALSE;
13058
13059 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13060 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13061 if (h == NULL)
13062 return FALSE;
13063 if (h->root.type == bfd_link_hash_new)
13064 {
13065 h->root.type = bfd_link_hash_defined;
13066 h->root.u.def.section = s;
13067 h->root.u.def.value = p - s->contents;
13068 h->ref_regular = 1;
13069 h->def_regular = 1;
13070 h->ref_regular_nonweak = 1;
13071 h->forced_local = 1;
13072 h->non_elf = 0;
2ec55de3 13073 h->root.linker_def = 1;
7341d5e2
AM
13074 }
13075 }
13076
a345bc8d
AM
13077 if (PPC_HA (off) != 0)
13078 {
13079 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13080 p += 4;
13081 }
13082 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13083 p += 4;
13084 bfd_put_32 (s->owner, MTCTR_R12, p);
13085 p += 4;
1be5d8d3 13086 output_bctr (htab, s->owner, p);
a345bc8d
AM
13087 break;
13088 }
13089 return TRUE;
13090}
13091
721956f4
AM
13092/* Build all the stubs associated with the current output file.
13093 The stubs are kept in a hash table attached to the main linker
13094 hash table. This function is called via gldelf64ppc_finish. */
13095
b34976b6 13096bfd_boolean
e7d1c40c 13097ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13098 char **stats)
5d1634d7
AM
13099{
13100 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13101 struct map_stub *group;
721956f4 13102 asection *stub_sec;
5d1634d7 13103 bfd_byte *p;
e717da7e 13104 int stub_sec_count = 0;
5d1634d7 13105
4dfe6ac6
NC
13106 if (htab == NULL)
13107 return FALSE;
13108
eea6121a 13109 /* Allocate memory to hold the linker stubs. */
d4aaa2a0
AM
13110 for (group = htab->group; group != NULL; group = group->next)
13111 if ((stub_sec = group->stub_sec) != NULL
eea6121a 13112 && stub_sec->size != 0)
e717da7e 13113 {
e7d1c40c 13114 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13115 if (stub_sec->contents == NULL)
13116 return FALSE;
eea6121a 13117 stub_sec->size = 0;
e717da7e 13118 }
5d1634d7 13119
23eb7e01 13120 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13121 {
9f951329 13122 unsigned int indx;
ad8e1ba5 13123 bfd_vma plt0;
9f951329 13124
721956f4 13125 /* Build the .glink plt call stub. */
e7d1c40c 13126 if (htab->params->emit_stub_syms)
97b639ba
AM
13127 {
13128 struct elf_link_hash_entry *h;
468392fb
AM
13129 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13130 TRUE, FALSE, FALSE);
97b639ba
AM
13131 if (h == NULL)
13132 return FALSE;
13133 if (h->root.type == bfd_link_hash_new)
13134 {
13135 h->root.type = bfd_link_hash_defined;
13136 h->root.u.def.section = htab->glink;
ee4bf8d2 13137 h->root.u.def.value = 8;
f5385ebf
AM
13138 h->ref_regular = 1;
13139 h->def_regular = 1;
13140 h->ref_regular_nonweak = 1;
13141 h->forced_local = 1;
13142 h->non_elf = 0;
2ec55de3 13143 h->root.linker_def = 1;
97b639ba
AM
13144 }
13145 }
33e44f2e
AM
13146 plt0 = (htab->elf.splt->output_section->vma
13147 + htab->elf.splt->output_offset
13148 - 16);
176a0d42
AM
13149 if (info->emitrelocations)
13150 {
13151 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13152 if (r == NULL)
13153 return FALSE;
13154 r->r_offset = (htab->glink->output_offset
13155 + htab->glink->output_section->vma);
13156 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13157 r->r_addend = plt0;
13158 }
4ce794b7 13159 p = htab->glink->contents;
176a0d42 13160 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13161 bfd_put_64 (htab->glink->owner, plt0, p);
13162 p += 8;
b9e5796b
AM
13163 if (htab->opd_abi)
13164 {
13165 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13166 p += 4;
13167 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13168 p += 4;
13169 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13170 p += 4;
13171 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13172 p += 4;
13173 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13174 p += 4;
13175 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13176 p += 4;
13177 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13178 p += 4;
13179 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13180 p += 4;
13181 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13182 p += 4;
13183 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13184 p += 4;
13185 }
13186 else
13187 {
13188 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13189 p += 4;
13190 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13191 p += 4;
13192 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13193 p += 4;
f378ab09
AM
13194 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13195 p += 4;
b9e5796b
AM
13196 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13197 p += 4;
13198 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13199 p += 4;
13200 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13201 p += 4;
13202 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13203 p += 4;
13204 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13205 p += 4;
13206 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13207 p += 4;
13208 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13209 p += 4;
13210 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13211 p += 4;
13212 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13213 p += 4;
13214 }
1be5d8d3 13215 p = output_bctr (htab, htab->glink->owner, p);
c75bc4f7 13216 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13217
9f951329
AM
13218 /* Build the .glink lazy link call stubs. */
13219 indx = 0;
9e390558 13220 while (p < htab->glink->contents + htab->glink->size)
9f951329 13221 {
b9e5796b 13222 if (htab->opd_abi)
9f951329 13223 {
b9e5796b
AM
13224 if (indx < 0x8000)
13225 {
13226 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13227 p += 4;
13228 }
13229 else
13230 {
13231 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13232 p += 4;
13233 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13234 p);
13235 p += 4;
13236 }
9f951329 13237 }
4ce794b7 13238 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13239 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13240 indx++;
9f951329
AM
13241 p += 4;
13242 }
5d1634d7 13243 }
5d1634d7 13244
9e390558
AM
13245 /* Build .glink global entry stubs. */
13246 if (htab->global_entry != NULL && htab->global_entry->size != 0)
13247 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13248
7341d5e2 13249 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13250 {
4ce794b7 13251 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13252 htab->brlt->size);
4ce794b7 13253 if (htab->brlt->contents == NULL)
b34976b6 13254 return FALSE;
721956f4 13255 }
ee75fd95 13256 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13257 {
13258 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13259 htab->relbrlt->size);
63bc6f6c
AM
13260 if (htab->relbrlt->contents == NULL)
13261 return FALSE;
13262 }
5d1634d7 13263
721956f4
AM
13264 /* Build the stubs as directed by the stub hash table. */
13265 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13266
a4b6fadd
AM
13267 for (group = htab->group; group != NULL; group = group->next)
13268 if (group->needs_save_res)
13269 {
13270 stub_sec = group->stub_sec;
13271 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13272 htab->sfpr->size);
13273 if (htab->params->emit_stub_syms)
13274 {
13275 unsigned int i;
13276
13277 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13278 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13279 return FALSE;
13280 }
13281 stub_sec->size += htab->sfpr->size;
13282 }
13283
aa8a7074
AM
13284 if (htab->relbrlt != NULL)
13285 htab->relbrlt->reloc_count = 0;
13286
e7d1c40c 13287 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13288 for (group = htab->group; group != NULL; group = group->next)
13289 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13290 {
13291 int align = abs (htab->params->plt_stub_align);
13292 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13293 }
794e51c0 13294
d4aaa2a0
AM
13295 for (group = htab->group; group != NULL; group = group->next)
13296 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13297 {
13298 stub_sec_count += 1;
c9301e31
AM
13299 if (stub_sec->rawsize != stub_sec->size
13300 && (htab->stub_iteration <= STUB_SHRINK_ITER
13301 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13302 break;
13303 }
5d1634d7 13304
25516cc5 13305 if (group != NULL)
5d1634d7 13306 {
b34976b6 13307 htab->stub_error = TRUE;
8de848d8 13308 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13309 }
721956f4 13310
d2a300cf
AM
13311 if (htab->stub_error)
13312 return FALSE;
13313
13314 if (stats != NULL)
13315 {
db84fff3 13316 size_t len;
d2a300cf
AM
13317 *stats = bfd_malloc (500);
13318 if (*stats == NULL)
13319 return FALSE;
13320
db84fff3
AM
13321 len = sprintf (*stats,
13322 ngettext ("linker stubs in %u group\n",
13323 "linker stubs in %u groups\n",
13324 stub_sec_count),
13325 stub_sec_count);
13326 sprintf (*stats + len, _(" branch %lu\n"
13327 " toc adjust %lu\n"
13328 " long branch %lu\n"
13329 " long toc adj %lu\n"
13330 " plt call %lu\n"
13331 " plt call toc %lu\n"
13332 " global entry %lu"),
4ce794b7
AM
13333 htab->stub_count[ppc_stub_long_branch - 1],
13334 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13335 htab->stub_count[ppc_stub_plt_branch - 1],
13336 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13337 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13338 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13339 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13340 }
13341 return TRUE;
5bd4f169
AM
13342}
13343
60124e18
AM
13344/* What to do when ld finds relocations against symbols defined in
13345 discarded sections. */
13346
13347static unsigned int
13348ppc64_elf_action_discarded (asection *sec)
13349{
13350 if (strcmp (".opd", sec->name) == 0)
13351 return 0;
13352
13353 if (strcmp (".toc", sec->name) == 0)
13354 return 0;
13355
bce50a28
JJ
13356 if (strcmp (".toc1", sec->name) == 0)
13357 return 0;
13358
60124e18
AM
13359 return _bfd_elf_default_action_discarded (sec);
13360}
13361
5bd4f169
AM
13362/* The RELOCATE_SECTION function is called by the ELF backend linker
13363 to handle the relocations for a section.
13364
13365 The relocs are always passed as Rela structures; if the section
13366 actually uses Rel structures, the r_addend field will always be
13367 zero.
13368
13369 This function is responsible for adjust the section contents as
13370 necessary, and (if using Rela relocs and generating a
1049f94e 13371 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13372 necessary.
13373
13374 This function does not have to worry about setting the reloc
13375 address or the reloc symbol index.
13376
13377 LOCAL_SYMS is a pointer to the swapped in local symbols.
13378
13379 LOCAL_SECTIONS is an array giving the section in the input file
13380 corresponding to the st_shndx field of each local symbol.
13381
13382 The global hash table entry for the global symbols can be found
13383 via elf_sym_hashes (input_bfd).
13384
1049f94e 13385 When generating relocatable output, this function must handle
5bd4f169
AM
13386 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13387 going to be the section symbol corresponding to the output
13388 section, which means that the addend must be adjusted
13389 accordingly. */
13390
b34976b6 13391static bfd_boolean
4ce794b7
AM
13392ppc64_elf_relocate_section (bfd *output_bfd,
13393 struct bfd_link_info *info,
13394 bfd *input_bfd,
13395 asection *input_section,
13396 bfd_byte *contents,
13397 Elf_Internal_Rela *relocs,
13398 Elf_Internal_Sym *local_syms,
13399 asection **local_sections)
5bd4f169 13400{
65f38f15 13401 struct ppc_link_hash_table *htab;
5bd4f169
AM
13402 Elf_Internal_Shdr *symtab_hdr;
13403 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13404 Elf_Internal_Rela *rel;
c316a17c 13405 Elf_Internal_Rela *wrel;
5bd4f169 13406 Elf_Internal_Rela *relend;
411e1bfb
AM
13407 Elf_Internal_Rela outrel;
13408 bfd_byte *loc;
411e1bfb 13409 struct got_entry **local_got_ents;
5bd4f169 13410 bfd_vma TOCstart;
b34976b6
AM
13411 bfd_boolean ret = TRUE;
13412 bfd_boolean is_opd;
794e51c0
AM
13413 /* Assume 'at' branch hints. */
13414 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13415 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13416
65f38f15 13417 /* Initialize howto table if needed. */
5bd4f169 13418 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13419 ppc_howto_init ();
13420
65f38f15 13421 htab = ppc_hash_table (info);
4dfe6ac6
NC
13422 if (htab == NULL)
13423 return FALSE;
ee75fd95
AM
13424
13425 /* Don't relocate stub sections. */
e7d1c40c 13426 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13427 return TRUE;
13428
0c8d6e5c 13429 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13430
411e1bfb 13431 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13432 TOCstart = elf_gp (output_bfd);
0ffa91dd 13433 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13434 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13435 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13436
c316a17c 13437 rel = wrel = relocs;
5bd4f169 13438 relend = relocs + input_section->reloc_count;
c316a17c 13439 for (; rel < relend; wrel++, rel++)
5bd4f169 13440 {
04c9666a 13441 enum elf_ppc64_reloc_type r_type;
31c76678 13442 bfd_vma addend;
5bd4f169
AM
13443 bfd_reloc_status_type r;
13444 Elf_Internal_Sym *sym;
13445 asection *sec;
039b3fef
AM
13446 struct elf_link_hash_entry *h_elf;
13447 struct ppc_link_hash_entry *h;
13448 struct ppc_link_hash_entry *fdh;
5bd4f169 13449 const char *sym_name;
0d4792f7 13450 unsigned long r_symndx, toc_symndx;
3a71aa26 13451 bfd_vma toc_addend;
f961d9dd
AM
13452 unsigned char tls_mask, tls_gd, tls_type;
13453 unsigned char sym_type;
5bd4f169 13454 bfd_vma relocation;
b34976b6
AM
13455 bfd_boolean unresolved_reloc;
13456 bfd_boolean warned;
bc30df16 13457 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13458 unsigned int insn;
e11840f9 13459 unsigned int mask;
721956f4
AM
13460 struct ppc_stub_hash_entry *stub_entry;
13461 bfd_vma max_br_offset;
13462 bfd_vma from;
c316a17c 13463 Elf_Internal_Rela orig_rel;
b80eed39
AM
13464 reloc_howto_type *howto;
13465 struct reloc_howto_struct alt_howto;
5bd4f169 13466
c316a17c
AM
13467 again:
13468 orig_rel = *rel;
13469
4ce794b7 13470 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13471 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13472
13473 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13474 symbol of the previous ADDR64 reloc. The symbol gives us the
13475 proper TOC base to use. */
13476 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13477 && wrel != relocs
13478 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13479 && is_opd)
c316a17c 13480 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13481
4ce794b7
AM
13482 sym = NULL;
13483 sec = NULL;
039b3fef 13484 h_elf = NULL;
4ce794b7 13485 sym_name = NULL;
b34976b6
AM
13486 unresolved_reloc = FALSE;
13487 warned = FALSE;
65f38f15 13488
0b13192e 13489 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13490 {
13491 /* It's a local symbol. */
74f0fb50 13492 struct _opd_sec_data *opd;
4025353c 13493
5bd4f169
AM
13494 sym = local_syms + r_symndx;
13495 sec = local_sections[r_symndx];
26c61ae5 13496 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13497 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13498 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13499 opd = get_opd_info (sec);
13500 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13501 {
51aecdc5
AM
13502 long adjust = opd->adjust[OPD_NDX (sym->st_value
13503 + rel->r_addend)];
4025353c
AM
13504 if (adjust == -1)
13505 relocation = 0;
13506 else
4cc603a5
AM
13507 {
13508 /* If this is a relocation against the opd section sym
13509 and we have edited .opd, adjust the reloc addend so
13510 that ld -r and ld --emit-relocs output is correct.
13511 If it is a reloc against some other .opd symbol,
13512 then the symbol value will be adjusted later. */
13513 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13514 rel->r_addend += adjust;
13515 else
13516 relocation += adjust;
13517 }
1e2f5b6e 13518 }
5bd4f169
AM
13519 }
13520 else
13521 {
62d887d4
L
13522 bfd_boolean ignored;
13523
b2a8e766
AM
13524 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13525 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13526 h_elf, sec, relocation,
62d887d4 13527 unresolved_reloc, warned, ignored);
039b3fef
AM
13528 sym_name = h_elf->root.root.string;
13529 sym_type = h_elf->type;
b69fdb4e
AM
13530 if (sec != NULL
13531 && sec->owner == output_bfd
13532 && strcmp (sec->name, ".opd") == 0)
13533 {
13534 /* This is a symbol defined in a linker script. All
13535 such are defined in output sections, even those
13536 defined by simple assignment from a symbol defined in
13537 an input section. Transfer the symbol to an
13538 appropriate input .opd section, so that a branch to
13539 this symbol will be mapped to the location specified
13540 by the opd entry. */
13541 struct bfd_link_order *lo;
13542 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13543 if (lo->type == bfd_indirect_link_order)
13544 {
13545 asection *isec = lo->u.indirect.section;
13546 if (h_elf->root.u.def.value >= isec->output_offset
13547 && h_elf->root.u.def.value < (isec->output_offset
13548 + isec->size))
13549 {
13550 h_elf->root.u.def.value -= isec->output_offset;
13551 h_elf->root.u.def.section = isec;
13552 sec = isec;
13553 break;
13554 }
13555 }
13556 }
5bd4f169 13557 }
039b3fef 13558 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13559
dbaa2011 13560 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13561 {
13562 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13563 input_bfd, input_section,
13564 contents + rel->r_offset);
13565 wrel->r_offset = rel->r_offset;
13566 wrel->r_info = 0;
13567 wrel->r_addend = 0;
13568
13569 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 13570 symbols defined in discarded sections. Not done for
c316a17c
AM
13571 non-debug to preserve relocs in .eh_frame which the
13572 eh_frame editing code expects to be present. */
13573 if (bfd_link_relocatable (info)
13574 && (input_section->flags & SEC_DEBUGGING))
13575 wrel--;
13576
13577 continue;
13578 }
ab96bf03 13579
0e1862bb 13580 if (bfd_link_relocatable (info))
c316a17c 13581 goto copy_reloc;
ab96bf03 13582
f40da81b
AM
13583 if (h != NULL && &h->elf == htab->elf.hgot)
13584 {
6f20ed8a 13585 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13586 sec = bfd_abs_section_ptr;
13587 unresolved_reloc = FALSE;
13588 }
13589
951fd09b
AM
13590 /* TLS optimizations. Replace instruction sequences and relocs
13591 based on information we collected in tls_optimize. We edit
13592 RELOCS so that --emit-relocs will output something sensible
13593 for the final instruction stream. */
13594 tls_mask = 0;
13595 tls_gd = 0;
0d4792f7 13596 toc_symndx = 0;
727fc41e
AM
13597 if (h != NULL)
13598 tls_mask = h->tls_mask;
13599 else if (local_got_ents != NULL)
411e1bfb 13600 {
e054468f
AM
13601 struct plt_entry **local_plt = (struct plt_entry **)
13602 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13603 unsigned char *lgot_masks = (unsigned char *)
e054468f 13604 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13605 tls_mask = lgot_masks[r_symndx];
13606 }
13607 if (tls_mask == 0
13608 && (r_type == R_PPC64_TLS
13609 || r_type == R_PPC64_TLSGD
13610 || r_type == R_PPC64_TLSLD))
13611 {
13612 /* Check for toc tls entries. */
f961d9dd 13613 unsigned char *toc_tls;
0d4792f7 13614
727fc41e
AM
13615 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13616 &local_syms, rel, input_bfd))
13617 return FALSE;
0d4792f7 13618
727fc41e
AM
13619 if (toc_tls)
13620 tls_mask = *toc_tls;
0d4792f7
AM
13621 }
13622
13623 /* Check that tls relocs are used with tls syms, and non-tls
13624 relocs are used with non-tls syms. */
cf35638d 13625 if (r_symndx != STN_UNDEF
0d4792f7
AM
13626 && r_type != R_PPC64_NONE
13627 && (h == NULL
039b3fef
AM
13628 || h->elf.root.type == bfd_link_hash_defined
13629 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13630 && (IS_PPC64_TLS_RELOC (r_type)
13631 != (sym_type == STT_TLS
13632 || (sym_type == STT_SECTION
13633 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13634 {
727fc41e
AM
13635 if (tls_mask != 0
13636 && (r_type == R_PPC64_TLS
13637 || r_type == R_PPC64_TLSGD
13638 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13639 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13640 ;
13641 else
25f53a85 13642 info->callbacks->einfo
1d483afe 13643 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13644 /* xgettext:c-format */
174d0a74 13645 ? _("%H: %s used with TLS symbol `%T'\n")
695344c0 13646 /* xgettext:c-format */
174d0a74 13647 : _("%H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13648 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13649 ppc64_elf_howto_table[r_type]->name,
13650 sym_name);
411e1bfb
AM
13651 }
13652
13653 /* Ensure reloc mapping code below stays sane. */
13654 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13655 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13656 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13657 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13658 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13659 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13660 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13661 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13662 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13663 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13664 abort ();
0d4792f7 13665
411e1bfb
AM
13666 switch (r_type)
13667 {
13668 default:
411e1bfb
AM
13669 break;
13670
ba761f19 13671 case R_PPC64_LO_DS_OPT:
95f0d0d2 13672 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13673 if ((insn & (0x3f << 26)) != 58u << 26)
13674 abort ();
13675 insn += (14u << 26) - (58u << 26);
95f0d0d2 13676 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13677 r_type = R_PPC64_TOC16_LO;
13678 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13679 break;
13680
411e1bfb
AM
13681 case R_PPC64_TOC16:
13682 case R_PPC64_TOC16_LO:
13683 case R_PPC64_TOC16_DS:
13684 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13685 {
13686 /* Check for toc tls entries. */
f961d9dd 13687 unsigned char *toc_tls;
951fd09b 13688 int retval;
411e1bfb 13689
3a71aa26
AM
13690 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13691 &local_syms, rel, input_bfd);
951fd09b 13692 if (retval == 0)
411e1bfb
AM
13693 return FALSE;
13694
13695 if (toc_tls)
13696 {
951fd09b 13697 tls_mask = *toc_tls;
411e1bfb
AM
13698 if (r_type == R_PPC64_TOC16_DS
13699 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13700 {
13701 if (tls_mask != 0
13702 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13703 goto toctprel;
13704 }
411e1bfb 13705 else
951fd09b
AM
13706 {
13707 /* If we found a GD reloc pair, then we might be
13708 doing a GD->IE transition. */
13709 if (retval == 2)
13710 {
13711 tls_gd = TLS_TPRELGD;
13712 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13713 goto tls_ldgd_opt;
951fd09b
AM
13714 }
13715 else if (retval == 3)
13716 {
13717 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13718 goto tls_ldgd_opt;
951fd09b
AM
13719 }
13720 }
411e1bfb
AM
13721 }
13722 }
13723 break;
13724
9d6ded02
AM
13725 case R_PPC64_GOT_TPREL16_HI:
13726 case R_PPC64_GOT_TPREL16_HA:
13727 if (tls_mask != 0
13728 && (tls_mask & TLS_TPREL) == 0)
13729 {
13730 rel->r_offset -= d_offset;
95f0d0d2 13731 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13732 r_type = R_PPC64_NONE;
13733 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13734 }
13735 break;
13736
411e1bfb
AM
13737 case R_PPC64_GOT_TPREL16_DS:
13738 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13739 if (tls_mask != 0
13740 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13741 {
81407a69 13742 toctprel:
95f0d0d2 13743 insn = bfd_get_32 (input_bfd,
c316a17c 13744 contents + rel->r_offset - d_offset);
411e1bfb
AM
13745 insn &= 31 << 21;
13746 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13747 bfd_put_32 (input_bfd, insn,
c316a17c 13748 contents + rel->r_offset - d_offset);
411e1bfb 13749 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13750 if (toc_symndx != 0)
13751 {
13752 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13753 rel->r_addend = toc_addend;
0d4792f7
AM
13754 /* We changed the symbol. Start over in order to
13755 get h, sym, sec etc. right. */
c316a17c 13756 goto again;
0d4792f7
AM
13757 }
13758 else
13759 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13760 }
13761 break;
13762
13763 case R_PPC64_TLS:
951fd09b
AM
13764 if (tls_mask != 0
13765 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13766 {
95f0d0d2 13767 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13768 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13769 if (insn == 0)
411e1bfb 13770 abort ();
95f0d0d2 13771 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13772 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13773 PPC64_TPREL16_LO which is at low-order half-word. */
13774 rel->r_offset += d_offset;
0d4792f7
AM
13775 r_type = R_PPC64_TPREL16_LO;
13776 if (toc_symndx != 0)
13777 {
13778 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13779 rel->r_addend = toc_addend;
0d4792f7
AM
13780 /* We changed the symbol. Start over in order to
13781 get h, sym, sec etc. right. */
c316a17c 13782 goto again;
0d4792f7
AM
13783 }
13784 else
13785 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13786 }
13787 break;
13788
411e1bfb
AM
13789 case R_PPC64_GOT_TLSGD16_HI:
13790 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13791 tls_gd = TLS_TPRELGD;
13792 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13793 goto tls_gdld_hi;
13794 break;
13795
411e1bfb
AM
13796 case R_PPC64_GOT_TLSLD16_HI:
13797 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13798 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13799 {
951fd09b
AM
13800 tls_gdld_hi:
13801 if ((tls_mask & tls_gd) != 0)
13802 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13803 + R_PPC64_GOT_TPREL16_DS);
13804 else
411e1bfb 13805 {
4fe5ca5b 13806 rel->r_offset -= d_offset;
95f0d0d2 13807 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13808 r_type = R_PPC64_NONE;
411e1bfb 13809 }
951fd09b 13810 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13811 }
13812 break;
13813
951fd09b
AM
13814 case R_PPC64_GOT_TLSGD16:
13815 case R_PPC64_GOT_TLSGD16_LO:
13816 tls_gd = TLS_TPRELGD;
13817 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13818 goto tls_ldgd_opt;
951fd09b 13819 break;
411e1bfb 13820
951fd09b
AM
13821 case R_PPC64_GOT_TLSLD16:
13822 case R_PPC64_GOT_TLSLD16_LO:
13823 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13824 {
b9f04fe0 13825 unsigned int insn1, insn2;
102890f0
AM
13826 bfd_vma offset;
13827
13828 tls_ldgd_opt:
727fc41e
AM
13829 offset = (bfd_vma) -1;
13830 /* If not using the newer R_PPC64_TLSGD/LD to mark
13831 __tls_get_addr calls, we must trust that the call
13832 stays with its arg setup insns, ie. that the next
13833 reloc is the __tls_get_addr call associated with
13834 the current reloc. Edit both insns. */
13835 if (input_section->has_tls_get_addr_call
13836 && rel + 1 < relend
13837 && branch_reloc_hash_match (input_bfd, rel + 1,
13838 htab->tls_get_addr,
13839 htab->tls_get_addr_fd))
13840 offset = rel[1].r_offset;
b86ac8e3
AM
13841 /* We read the low GOT_TLS (or TOC16) insn because we
13842 need to keep the destination reg. It may be
13843 something other than the usual r3, and moved to r3
13844 before the call by intervening code. */
95f0d0d2 13845 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13846 contents + rel->r_offset - d_offset);
102890f0 13847 if ((tls_mask & tls_gd) != 0)
411e1bfb 13848 {
102890f0 13849 /* IE */
b86ac8e3 13850 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13851 insn1 |= 58 << 26; /* ld */
13852 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13853 if (offset != (bfd_vma) -1)
f58d5a2d 13854 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13855 if ((tls_mask & TLS_EXPLICIT) == 0)
13856 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13857 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13858 else
102890f0
AM
13859 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13860 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13861 }
13862 else
13863 {
13864 /* LE */
b86ac8e3
AM
13865 insn1 &= 0x1f << 21;
13866 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13867 insn2 = 0x38630000; /* addi 3,3,0 */
13868 if (tls_gd == 0)
951fd09b 13869 {
102890f0 13870 /* Was an LD reloc. */
1d483afe
AM
13871 if (toc_symndx)
13872 sec = local_sections[toc_symndx];
13873 for (r_symndx = 0;
13874 r_symndx < symtab_hdr->sh_info;
13875 r_symndx++)
13876 if (local_sections[r_symndx] == sec)
13877 break;
13878 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13879 r_symndx = STN_UNDEF;
102890f0 13880 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13881 if (r_symndx != STN_UNDEF)
1d483afe
AM
13882 rel->r_addend -= (local_syms[r_symndx].st_value
13883 + sec->output_offset
13884 + sec->output_section->vma);
951fd09b 13885 }
102890f0 13886 else if (toc_symndx != 0)
3a71aa26
AM
13887 {
13888 r_symndx = toc_symndx;
13889 rel->r_addend = toc_addend;
13890 }
102890f0
AM
13891 r_type = R_PPC64_TPREL16_HA;
13892 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13893 if (offset != (bfd_vma) -1)
13894 {
13895 rel[1].r_info = ELF64_R_INFO (r_symndx,
13896 R_PPC64_TPREL16_LO);
13897 rel[1].r_offset = offset + d_offset;
13898 rel[1].r_addend = rel->r_addend;
13899 }
102890f0 13900 }
95f0d0d2 13901 bfd_put_32 (input_bfd, insn1,
3a71aa26 13902 contents + rel->r_offset - d_offset);
727fc41e 13903 if (offset != (bfd_vma) -1)
b9f04fe0 13904 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13905 if ((tls_mask & tls_gd) == 0
13906 && (tls_gd == 0 || toc_symndx != 0))
13907 {
13908 /* We changed the symbol. Start over in order
13909 to get h, sym, sec etc. right. */
c316a17c 13910 goto again;
727fc41e
AM
13911 }
13912 }
13913 break;
13914
13915 case R_PPC64_TLSGD:
13916 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13917 {
b9f04fe0 13918 unsigned int insn2;
727fc41e
AM
13919 bfd_vma offset = rel->r_offset;
13920
13921 if ((tls_mask & TLS_TPRELGD) != 0)
13922 {
13923 /* IE */
13924 r_type = R_PPC64_NONE;
13925 insn2 = 0x7c636a14; /* add 3,3,13 */
13926 }
13927 else
13928 {
13929 /* LE */
13930 if (toc_symndx != 0)
13931 {
13932 r_symndx = toc_symndx;
13933 rel->r_addend = toc_addend;
13934 }
13935 r_type = R_PPC64_TPREL16_LO;
13936 rel->r_offset = offset + d_offset;
13937 insn2 = 0x38630000; /* addi 3,3,0 */
13938 }
13939 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13940 /* Zap the reloc on the _tls_get_addr call too. */
13941 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13942 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13943 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13944 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13945 goto again;
411e1bfb 13946 }
411e1bfb
AM
13947 break;
13948
727fc41e
AM
13949 case R_PPC64_TLSLD:
13950 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13951 {
b9f04fe0 13952 unsigned int insn2;
727fc41e
AM
13953 bfd_vma offset = rel->r_offset;
13954
13955 if (toc_symndx)
13956 sec = local_sections[toc_symndx];
13957 for (r_symndx = 0;
13958 r_symndx < symtab_hdr->sh_info;
13959 r_symndx++)
13960 if (local_sections[r_symndx] == sec)
13961 break;
13962 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13963 r_symndx = STN_UNDEF;
727fc41e 13964 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13965 if (r_symndx != STN_UNDEF)
727fc41e
AM
13966 rel->r_addend -= (local_syms[r_symndx].st_value
13967 + sec->output_offset
13968 + sec->output_section->vma);
13969
13970 r_type = R_PPC64_TPREL16_LO;
13971 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13972 rel->r_offset = offset + d_offset;
13973 /* Zap the reloc on the _tls_get_addr call too. */
13974 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13975 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13976 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13977 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13978 goto again;
727fc41e
AM
13979 }
13980 break;
13981
411e1bfb 13982 case R_PPC64_DTPMOD64:
951fd09b
AM
13983 if (rel + 1 < relend
13984 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13985 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13986 {
951fd09b
AM
13987 if ((tls_mask & TLS_GD) == 0)
13988 {
13989 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13990 if ((tls_mask & TLS_TPRELGD) != 0)
13991 r_type = R_PPC64_TPREL64;
13992 else
13993 {
4ce794b7 13994 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13995 r_type = R_PPC64_NONE;
13996 }
13997 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13998 }
13999 }
14000 else
14001 {
14002 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14003 {
4ce794b7 14004 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14005 r_type = R_PPC64_NONE;
951fd09b 14006 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14007 }
411e1bfb
AM
14008 }
14009 break;
14010
14011 case R_PPC64_TPREL64:
951fd09b 14012 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14013 {
14014 r_type = R_PPC64_NONE;
14015 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14016 }
14017 break;
52a82034 14018
006589cf
AM
14019 case R_PPC64_ENTRY:
14020 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14021 if (!bfd_link_pic (info)
14022 && !info->traditional_format
14023 && relocation + 0x80008000 <= 0xffffffff)
14024 {
14025 unsigned int insn1, insn2;
14026
14027 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14028 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14029 if ((insn1 & ~0xfffc) == LD_R2_0R12
14030 && insn2 == ADD_R2_R2_R12)
14031 {
95f0d0d2 14032 bfd_put_32 (input_bfd,
006589cf
AM
14033 LIS_R2 + PPC_HA (relocation),
14034 contents + rel->r_offset);
95f0d0d2 14035 bfd_put_32 (input_bfd,
006589cf
AM
14036 ADDI_R2_R2 + PPC_LO (relocation),
14037 contents + rel->r_offset + 4);
14038 }
14039 }
14040 else
14041 {
14042 relocation -= (rel->r_offset
14043 + input_section->output_offset
14044 + input_section->output_section->vma);
14045 if (relocation + 0x80008000 <= 0xffffffff)
14046 {
14047 unsigned int insn1, insn2;
14048
14049 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14050 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14051 if ((insn1 & ~0xfffc) == LD_R2_0R12
14052 && insn2 == ADD_R2_R2_R12)
14053 {
95f0d0d2 14054 bfd_put_32 (input_bfd,
006589cf
AM
14055 ADDIS_R2_R12 + PPC_HA (relocation),
14056 contents + rel->r_offset);
95f0d0d2 14057 bfd_put_32 (input_bfd,
006589cf
AM
14058 ADDI_R2_R2 + PPC_LO (relocation),
14059 contents + rel->r_offset + 4);
14060 }
14061 }
14062 }
14063 break;
14064
52a82034
AM
14065 case R_PPC64_REL16_HA:
14066 /* If we are generating a non-PIC executable, edit
14067 . 0: addis 2,12,.TOC.-0b@ha
14068 . addi 2,2,.TOC.-0b@l
14069 used by ELFv2 global entry points to set up r2, to
14070 . lis 2,.TOC.@ha
14071 . addi 2,2,.TOC.@l
14072 if .TOC. is in range. */
0e1862bb 14073 if (!bfd_link_pic (info)
810d4e75 14074 && !info->traditional_format
006589cf 14075 && !htab->opd_abi
4f038ee5 14076 && rel->r_addend == d_offset
52a82034
AM
14077 && h != NULL && &h->elf == htab->elf.hgot
14078 && rel + 1 < relend
14079 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14080 && rel[1].r_offset == rel->r_offset + 4
14081 && rel[1].r_addend == rel->r_addend + 4
14082 && relocation + 0x80008000 <= 0xffffffff)
14083 {
14084 unsigned int insn1, insn2;
14085 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14086 insn1 = bfd_get_32 (input_bfd, contents + offset);
14087 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14088 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14089 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14090 {
14091 r_type = R_PPC64_ADDR16_HA;
14092 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14093 rel->r_addend -= d_offset;
14094 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14095 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14096 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14097 }
14098 }
14099 break;
411e1bfb
AM
14100 }
14101
14102 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14103 insn = 0;
b25116a9
AM
14104 max_br_offset = 1 << 25;
14105 addend = rel->r_addend;
bc30df16 14106 reloc_dest = DEST_NORMAL;
65f38f15 14107 switch (r_type)
5bd4f169
AM
14108 {
14109 default:
65f38f15 14110 break;
5bd4f169 14111
3b421ab3
AM
14112 case R_PPC64_TOCSAVE:
14113 if (relocation + addend == (rel->r_offset
14114 + input_section->output_offset
14115 + input_section->output_section->vma)
14116 && tocsave_find (htab, NO_INSERT,
14117 &local_syms, rel, input_bfd))
14118 {
14119 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14120 if (insn == NOP
14121 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14122 bfd_put_32 (input_bfd,
14123 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14124 contents + rel->r_offset);
14125 }
14126 break;
14127
65f38f15
AM
14128 /* Branch taken prediction relocations. */
14129 case R_PPC64_ADDR14_BRTAKEN:
14130 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14131 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14132 /* Fall through. */
65f38f15 14133
86c76c7b 14134 /* Branch not taken prediction relocations. */
65f38f15
AM
14135 case R_PPC64_ADDR14_BRNTAKEN:
14136 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14137 insn |= bfd_get_32 (input_bfd,
411e1bfb 14138 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14139 /* Fall through. */
86c76c7b 14140
b25116a9
AM
14141 case R_PPC64_REL14:
14142 max_br_offset = 1 << 15;
1a0670f3 14143 /* Fall through. */
5bd4f169 14144
65f38f15 14145 case R_PPC64_REL24:
ad8e1ba5
AM
14146 /* Calls to functions with a different TOC, such as calls to
14147 shared objects, need to alter the TOC pointer. This is
14148 done using a linkage stub. A REL24 branching to these
14149 linkage stubs needs to be followed by a nop, as the nop
14150 will be replaced with an instruction to restore the TOC
14151 base pointer. */
8387904d 14152 fdh = h;
b31867b6
AM
14153 if (h != NULL
14154 && h->oh != NULL
14155 && h->oh->is_func_descriptor)
14156 fdh = ppc_follow_link (h->oh);
31c76678
DK
14157 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14158 htab);
6abec6bc 14159 if (stub_entry != NULL
ad8e1ba5 14160 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14161 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14162 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14163 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14164 {
b25116a9 14165 bfd_boolean can_plt_call = FALSE;
721956f4 14166
6e1816be
AM
14167 if (stub_entry->stub_type == ppc_stub_plt_call
14168 && !htab->opd_abi
14169 && htab->params->plt_localentry0 != 0
14170 && is_elfv2_localentry0 (&h->elf))
14171 {
14172 /* The function doesn't use or change r2. */
14173 can_plt_call = TRUE;
14174 }
14175
f378ab09 14176 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14177 branch and link followed by a nop. The nop is
14178 replaced by an insn to restore r2. */
6e1816be 14179 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14180 {
ba8ca3e7
AM
14181 unsigned long br;
14182
14183 br = bfd_get_32 (input_bfd,
14184 contents + rel->r_offset);
14185 if ((br & 1) != 0)
41bd81ab 14186 {
ba8ca3e7
AM
14187 unsigned long nop;
14188
14189 nop = bfd_get_32 (input_bfd,
14190 contents + rel->r_offset + 4);
14191 if (nop == NOP
14192 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14193 {
ba8ca3e7
AM
14194 if (h != NULL
14195 && (h == htab->tls_get_addr_fd
14196 || h == htab->tls_get_addr)
7c9cf415 14197 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14198 {
14199 /* Special stub used, leave nop alone. */
14200 }
14201 else
a078d95a
AM
14202 bfd_put_32 (input_bfd,
14203 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14204 contents + rel->r_offset + 4);
14205 can_plt_call = TRUE;
a7f2871e 14206 }
41bd81ab 14207 }
5bd4f169 14208 }
721956f4 14209
ba8ca3e7 14210 if (!can_plt_call && h != NULL)
721956f4 14211 {
ba8ca3e7
AM
14212 const char *name = h->elf.root.root.string;
14213
14214 if (*name == '.')
14215 ++name;
14216
14217 if (strncmp (name, "__libc_start_main", 17) == 0
14218 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14219 {
ba8ca3e7
AM
14220 /* Allow crt1 branch to go via a toc adjusting
14221 stub. Other calls that never return could do
14222 the same, if we could detect such. */
b25116a9 14223 can_plt_call = TRUE;
6ab189d5 14224 }
ba8ca3e7
AM
14225 }
14226
14227 if (!can_plt_call)
14228 {
14229 /* g++ as of 20130507 emits self-calls without a
14230 following nop. This is arguably wrong since we
14231 have conflicting information. On the one hand a
14232 global symbol and on the other a local call
14233 sequence, but don't error for this special case.
14234 It isn't possible to cheaply verify we have
14235 exactly such a call. Allow all calls to the same
14236 section. */
14237 asection *code_sec = sec;
14238
14239 if (get_opd_info (sec) != NULL)
ad8e1ba5 14240 {
ba8ca3e7
AM
14241 bfd_vma off = (relocation + addend
14242 - sec->output_section->vma
14243 - sec->output_offset);
bc30df16 14244
ba8ca3e7 14245 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14246 }
ba8ca3e7
AM
14247 if (code_sec == input_section)
14248 can_plt_call = TRUE;
14249 }
14250
14251 if (!can_plt_call)
14252 {
4805fc55
AM
14253 if (stub_entry->stub_type == ppc_stub_plt_call
14254 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14255 info->callbacks->einfo
695344c0 14256 /* xgettext:c-format */
174d0a74 14257 (_("%H: call to `%T' lacks nop, can't restore toc; "
4805fc55
AM
14258 "recompile with -fPIC\n"),
14259 input_bfd, input_section, rel->r_offset, sym_name);
14260 else
14261 info->callbacks->einfo
695344c0 14262 /* xgettext:c-format */
174d0a74 14263 (_("%H: call to `%T' lacks nop, can't restore toc; "
4805fc55
AM
14264 "(-mcmodel=small toc adjust stub)\n"),
14265 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14266
14267 bfd_set_error (bfd_error_bad_value);
14268 ret = FALSE;
721956f4
AM
14269 }
14270
b25116a9 14271 if (can_plt_call
794e51c0
AM
14272 && (stub_entry->stub_type == ppc_stub_plt_call
14273 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14274 unresolved_reloc = FALSE;
14275 }
14276
6abec6bc
AM
14277 if ((stub_entry == NULL
14278 || stub_entry->stub_type == ppc_stub_long_branch
14279 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14280 && get_opd_info (sec) != NULL)
14281 {
14282 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14283 bfd_vma off = (relocation + addend
14284 - sec->output_section->vma
14285 - sec->output_offset);
aef36ac1 14286 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14287 if (dest != (bfd_vma) -1)
14288 {
14289 relocation = dest;
14290 addend = 0;
bc30df16 14291 reloc_dest = DEST_OPD;
8387904d
AM
14292 }
14293 }
14294
b25116a9
AM
14295 /* If the branch is out of reach we ought to have a long
14296 branch stub. */
14297 from = (rel->r_offset
14298 + input_section->output_offset
14299 + input_section->output_section->vma);
14300
6911b7dc
AM
14301 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14302 ? fdh->elf.other
14303 : sym->st_other);
14304
6abec6bc
AM
14305 if (stub_entry != NULL
14306 && (stub_entry->stub_type == ppc_stub_long_branch
14307 || stub_entry->stub_type == ppc_stub_plt_branch)
14308 && (r_type == R_PPC64_ADDR14_BRTAKEN
14309 || r_type == R_PPC64_ADDR14_BRNTAKEN
14310 || (relocation + addend - from + max_br_offset
14311 < 2 * max_br_offset)))
14312 /* Don't use the stub if this branch is in range. */
14313 stub_entry = NULL;
b25116a9
AM
14314
14315 if (stub_entry != NULL)
14316 {
14317 /* Munge up the value and addend so that we call the stub
14318 rather than the procedure directly. */
a4b6fadd
AM
14319 asection *stub_sec = stub_entry->group->stub_sec;
14320
14321 if (stub_entry->stub_type == ppc_stub_save_res)
14322 relocation += (stub_sec->output_offset
14323 + stub_sec->output_section->vma
14324 + stub_sec->size - htab->sfpr->size
14325 - htab->sfpr->output_offset
14326 - htab->sfpr->output_section->vma);
14327 else
14328 relocation = (stub_entry->stub_offset
14329 + stub_sec->output_offset
14330 + stub_sec->output_section->vma);
b25116a9 14331 addend = 0;
bc30df16 14332 reloc_dest = DEST_STUB;
3b421ab3 14333
954b63d4 14334 if ((stub_entry->stub_type == ppc_stub_plt_call
794e51c0
AM
14335 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14336 && (ALWAYS_EMIT_R2SAVE
14337 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14338 && rel + 1 < relend
14339 && rel[1].r_offset == rel->r_offset + 4
14340 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14341 relocation += 4;
b25116a9
AM
14342 }
14343
14344 if (insn != 0)
14345 {
794e51c0 14346 if (is_isa_v2)
721956f4 14347 {
b25116a9
AM
14348 /* Set 'a' bit. This is 0b00010 in BO field for branch
14349 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14350 for branch on CTR insns (BO == 1a00t or 1a01t). */
14351 if ((insn & (0x14 << 21)) == (0x04 << 21))
14352 insn |= 0x02 << 21;
14353 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14354 insn |= 0x08 << 21;
14355 else
14356 break;
14357 }
14358 else
14359 {
14360 /* Invert 'y' bit if not the default. */
4cc603a5 14361 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14362 insn ^= 0x01 << 21;
721956f4 14363 }
b25116a9 14364
95f0d0d2 14365 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14366 }
e86ce104 14367
06da1e8e
AM
14368 /* NOP out calls to undefined weak functions.
14369 We can thus call a weak function without first
14370 checking whether the function is defined. */
b25116a9 14371 else if (h != NULL
039b3fef 14372 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14373 && h->elf.dynindx == -1
b25116a9
AM
14374 && r_type == R_PPC64_REL24
14375 && relocation == 0
4cc603a5 14376 && addend == 0)
e86ce104 14377 {
95f0d0d2 14378 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14379 goto copy_reloc;
e86ce104 14380 }
65f38f15
AM
14381 break;
14382 }
5bd4f169 14383
65f38f15 14384 /* Set `addend'. */
411e1bfb 14385 tls_type = 0;
65f38f15
AM
14386 switch (r_type)
14387 {
14388 default:
25f53a85 14389 info->callbacks->einfo
695344c0 14390 /* xgettext:c-format */
bc30df16 14391 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14392 input_bfd, (int) r_type, sym_name);
5bd4f169 14393
65f38f15 14394 bfd_set_error (bfd_error_bad_value);
b34976b6 14395 ret = FALSE;
c316a17c 14396 goto copy_reloc;
5bd4f169 14397
65f38f15 14398 case R_PPC64_NONE:
411e1bfb 14399 case R_PPC64_TLS:
727fc41e
AM
14400 case R_PPC64_TLSGD:
14401 case R_PPC64_TLSLD:
3b421ab3 14402 case R_PPC64_TOCSAVE:
04c9666a
AM
14403 case R_PPC64_GNU_VTINHERIT:
14404 case R_PPC64_GNU_VTENTRY:
006589cf 14405 case R_PPC64_ENTRY:
c316a17c 14406 goto copy_reloc;
5bd4f169
AM
14407
14408 /* GOT16 relocations. Like an ADDR16 using the symbol's
14409 address in the GOT as relocation value instead of the
411e1bfb 14410 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14411 symbol and put the symbol value there. */
411e1bfb
AM
14412 case R_PPC64_GOT_TLSGD16:
14413 case R_PPC64_GOT_TLSGD16_LO:
14414 case R_PPC64_GOT_TLSGD16_HI:
14415 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14416 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14417 goto dogot;
14418
14419 case R_PPC64_GOT_TLSLD16:
14420 case R_PPC64_GOT_TLSLD16_LO:
14421 case R_PPC64_GOT_TLSLD16_HI:
14422 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14423 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14424 goto dogot;
14425
14426 case R_PPC64_GOT_TPREL16_DS:
14427 case R_PPC64_GOT_TPREL16_LO_DS:
14428 case R_PPC64_GOT_TPREL16_HI:
14429 case R_PPC64_GOT_TPREL16_HA:
14430 tls_type = TLS_TLS | TLS_TPREL;
14431 goto dogot;
14432
14433 case R_PPC64_GOT_DTPREL16_DS:
14434 case R_PPC64_GOT_DTPREL16_LO_DS:
14435 case R_PPC64_GOT_DTPREL16_HI:
14436 case R_PPC64_GOT_DTPREL16_HA:
14437 tls_type = TLS_TLS | TLS_DTPREL;
14438 goto dogot;
14439
65f38f15
AM
14440 case R_PPC64_GOT16:
14441 case R_PPC64_GOT16_LO:
14442 case R_PPC64_GOT16_HI:
14443 case R_PPC64_GOT16_HA:
14444 case R_PPC64_GOT16_DS:
14445 case R_PPC64_GOT16_LO_DS:
411e1bfb 14446 dogot:
5bd4f169
AM
14447 {
14448 /* Relocation is to the entry for this symbol in the global
14449 offset table. */
e717da7e 14450 asection *got;
d881513a 14451 bfd_vma *offp;
5bd4f169 14452 bfd_vma off;
d881513a 14453 unsigned long indx = 0;
927be08e 14454 struct got_entry *ent;
65f38f15 14455
d881513a
AM
14456 if (tls_type == (TLS_TLS | TLS_LD)
14457 && (h == NULL
f5385ebf 14458 || !h->elf.def_dynamic))
927be08e 14459 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14460 else
5bd4f169 14461 {
d881513a
AM
14462 if (h != NULL)
14463 {
f0158f44
AM
14464 if (!htab->elf.dynamic_sections_created
14465 || h->elf.dynindx == -1
14466 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 14467 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
14468 /* This is actually a static link, or it is a
14469 -Bsymbolic link and the symbol is defined
14470 locally, or the symbol was forced to be local
14471 because of a version file. */
14472 ;
14473 else
14474 {
039b3fef 14475 indx = h->elf.dynindx;
d881513a
AM
14476 unresolved_reloc = FALSE;
14477 }
039b3fef 14478 ent = h->elf.got.glist;
d881513a 14479 }
411e1bfb 14480 else
5bd4f169 14481 {
d881513a
AM
14482 if (local_got_ents == NULL)
14483 abort ();
14484 ent = local_got_ents[r_symndx];
5bd4f169 14485 }
d881513a
AM
14486
14487 for (; ent != NULL; ent = ent->next)
31c76678 14488 if (ent->addend == orig_rel.r_addend
e717da7e 14489 && ent->owner == input_bfd
d881513a
AM
14490 && ent->tls_type == tls_type)
14491 break;
5bd4f169 14492 }
411e1bfb 14493
927be08e
AM
14494 if (ent == NULL)
14495 abort ();
14496 if (ent->is_indirect)
14497 ent = ent->got.ent;
14498 offp = &ent->got.offset;
14499 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14500 if (got == NULL)
14501 abort ();
14502
411e1bfb
AM
14503 /* The offset must always be a multiple of 8. We use the
14504 least significant bit to record whether we have already
14505 processed this entry. */
d881513a 14506 off = *offp;
411e1bfb
AM
14507 if ((off & 1) != 0)
14508 off &= ~1;
5bd4f169
AM
14509 else
14510 {
411e1bfb
AM
14511 /* Generate relocs for the dynamic linker, except in
14512 the case of TLSLD where we'll use one entry per
14513 module. */
25f23106
AM
14514 asection *relgot;
14515 bfd_boolean ifunc;
e717da7e 14516
d881513a 14517 *offp = off | 1;
25f23106
AM
14518 relgot = NULL;
14519 ifunc = (h != NULL
14520 ? h->elf.type == STT_GNU_IFUNC
14521 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14522 if (ifunc)
82e66161
AM
14523 {
14524 relgot = htab->elf.irelplt;
14525 if (indx == 0)
14526 htab->local_ifunc_resolver = 1;
14527 else if (is_static_defined (&h->elf))
14528 htab->maybe_local_ifunc_resolver = 1;
14529 }
f0158f44
AM
14530 else if (indx != 0
14531 || (bfd_link_pic (info)
14532 && (h == NULL
21d68fcd 14533 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 14534 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
14535 && !h->elf.def_dynamic))
14536 && !(tls_type == (TLS_TLS | TLS_TPREL)
14537 && bfd_link_executable (info)
14538 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 14539 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14540 if (relgot != NULL)
5bd4f169 14541 {
e717da7e
AM
14542 outrel.r_offset = (got->output_section->vma
14543 + got->output_offset
411e1bfb 14544 + off);
4cc603a5 14545 outrel.r_addend = addend;
d881513a 14546 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14547 {
411e1bfb 14548 outrel.r_addend = 0;
e515b051 14549 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14550 if (tls_type == (TLS_TLS | TLS_GD))
14551 {
e717da7e
AM
14552 loc = relgot->contents;
14553 loc += (relgot->reloc_count++
d881513a
AM
14554 * sizeof (Elf64_External_Rela));
14555 bfd_elf64_swap_reloca_out (output_bfd,
14556 &outrel, loc);
e515b051 14557 outrel.r_offset += 8;
4cc603a5 14558 outrel.r_addend = addend;
d881513a
AM
14559 outrel.r_info
14560 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14561 }
411e1bfb 14562 }
951fd09b 14563 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14564 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14565 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14566 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14567 else if (indx != 0)
14568 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14569 else
81407a69 14570 {
25f23106
AM
14571 if (ifunc)
14572 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14573 else
14574 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14575
14576 /* Write the .got section contents for the sake
14577 of prelink. */
e717da7e 14578 loc = got->contents + off;
23fbd6fa
JJ
14579 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14580 loc);
81407a69 14581 }
81407a69
AM
14582
14583 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14584 {
14585 outrel.r_addend += relocation;
14586 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14587 {
14588 if (htab->elf.tls_sec == NULL)
14589 outrel.r_addend = 0;
14590 else
14591 outrel.r_addend -= htab->elf.tls_sec->vma;
14592 }
e515b051 14593 }
e717da7e
AM
14594 loc = relgot->contents;
14595 loc += (relgot->reloc_count++
411e1bfb
AM
14596 * sizeof (Elf64_External_Rela));
14597 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14598 }
14599
ad8e1ba5 14600 /* Init the .got section contents here if we're not
81407a69 14601 emitting a reloc. */
d881513a 14602 else
411e1bfb 14603 {
4cc603a5 14604 relocation += addend;
f0158f44 14605 if (tls_type != 0)
411e1bfb 14606 {
989f9879
AM
14607 if (htab->elf.tls_sec == NULL)
14608 relocation = 0;
14609 else
14610 {
f0158f44
AM
14611 if (tls_type & TLS_LD)
14612 relocation = 0;
14613 else
14614 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 14615 if (tls_type & TLS_TPREL)
989f9879
AM
14616 relocation += DTP_OFFSET - TP_OFFSET;
14617 }
5bd4f169 14618
f0158f44 14619 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14620 {
14621 bfd_put_64 (output_bfd, relocation,
e717da7e 14622 got->contents + off + 8);
676ee2b5 14623 relocation = 1;
7b609f53 14624 }
411e1bfb
AM
14625 }
14626 bfd_put_64 (output_bfd, relocation,
e717da7e 14627 got->contents + off);
5bd4f169
AM
14628 }
14629 }
14630
65f38f15
AM
14631 if (off >= (bfd_vma) -2)
14632 abort ();
14633
bf102f86 14634 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14635 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14636 }
65f38f15
AM
14637 break;
14638
14639 case R_PPC64_PLT16_HA:
14640 case R_PPC64_PLT16_HI:
14641 case R_PPC64_PLT16_LO:
14642 case R_PPC64_PLT32:
14643 case R_PPC64_PLT64:
14644 /* Relocation is to the entry for this symbol in the
14645 procedure linkage table. */
cbf95972
AM
14646 {
14647 struct plt_entry **plt_list = NULL;
14648 if (h != NULL)
14649 plt_list = &h->elf.plt.plist;
14650 else if (local_got_ents != NULL)
14651 {
14652 struct plt_entry **local_plt = (struct plt_entry **)
14653 (local_got_ents + symtab_hdr->sh_info);
14654 unsigned char *local_got_tls_masks = (unsigned char *)
14655 (local_plt + symtab_hdr->sh_info);
14656 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14657 plt_list = local_plt + r_symndx;
14658 }
14659 if (plt_list)
14660 {
14661 struct plt_entry *ent;
65f38f15 14662
cbf95972
AM
14663 for (ent = *plt_list; ent != NULL; ent = ent->next)
14664 if (ent->plt.offset != (bfd_vma) -1
14665 && ent->addend == orig_rel.r_addend)
14666 {
14667 asection *plt;
14668
14669 plt = htab->elf.splt;
14670 if (!htab->elf.dynamic_sections_created
14671 || h == NULL
14672 || h->elf.dynindx == -1)
14673 plt = htab->elf.iplt;
14674 relocation = (plt->output_section->vma
14675 + plt->output_offset
14676 + ent->plt.offset);
14677 addend = 0;
14678 unresolved_reloc = FALSE;
14679 break;
14680 }
14681 }
14682 }
65f38f15 14683 break;
5bd4f169 14684
0b13192e
AM
14685 case R_PPC64_TOC:
14686 /* Relocation value is TOC base. */
14687 relocation = TOCstart;
cf35638d 14688 if (r_symndx == STN_UNDEF)
6f20ed8a 14689 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14690 else if (unresolved_reloc)
14691 ;
6f20ed8a
AM
14692 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14693 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14694 else
14695 unresolved_reloc = TRUE;
ab96bf03 14696 goto dodyn;
0b13192e 14697
5bd4f169
AM
14698 /* TOC16 relocs. We want the offset relative to the TOC base,
14699 which is the address of the start of the TOC plus 0x8000.
14700 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14701 in this order. */
65f38f15
AM
14702 case R_PPC64_TOC16:
14703 case R_PPC64_TOC16_LO:
14704 case R_PPC64_TOC16_HI:
14705 case R_PPC64_TOC16_DS:
14706 case R_PPC64_TOC16_LO_DS:
14707 case R_PPC64_TOC16_HA:
6f20ed8a 14708 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14709 break;
14710
14711 /* Relocate against the beginning of the section. */
65f38f15
AM
14712 case R_PPC64_SECTOFF:
14713 case R_PPC64_SECTOFF_LO:
14714 case R_PPC64_SECTOFF_HI:
14715 case R_PPC64_SECTOFF_DS:
14716 case R_PPC64_SECTOFF_LO_DS:
14717 case R_PPC64_SECTOFF_HA:
4ce794b7 14718 if (sec != NULL)
65f38f15 14719 addend -= sec->output_section->vma;
5bd4f169
AM
14720 break;
14721
25f23106
AM
14722 case R_PPC64_REL16:
14723 case R_PPC64_REL16_LO:
14724 case R_PPC64_REL16_HI:
14725 case R_PPC64_REL16_HA:
a680de9a 14726 case R_PPC64_REL16DX_HA:
25f23106
AM
14727 break;
14728
721956f4
AM
14729 case R_PPC64_REL14:
14730 case R_PPC64_REL14_BRNTAKEN:
14731 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14732 case R_PPC64_REL24:
14733 break;
14734
411e1bfb
AM
14735 case R_PPC64_TPREL16:
14736 case R_PPC64_TPREL16_LO:
14737 case R_PPC64_TPREL16_HI:
14738 case R_PPC64_TPREL16_HA:
14739 case R_PPC64_TPREL16_DS:
14740 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14741 case R_PPC64_TPREL16_HIGH:
14742 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14743 case R_PPC64_TPREL16_HIGHER:
14744 case R_PPC64_TPREL16_HIGHERA:
14745 case R_PPC64_TPREL16_HIGHEST:
14746 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14747 if (h != NULL
14748 && h->elf.root.type == bfd_link_hash_undefweak
14749 && h->elf.dynindx == -1)
14750 {
14751 /* Make this relocation against an undefined weak symbol
14752 resolve to zero. This is really just a tweak, since
14753 code using weak externs ought to check that they are
14754 defined before using them. */
14755 bfd_byte *p = contents + rel->r_offset - d_offset;
14756
95f0d0d2 14757 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14758 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14759 if (insn != 0)
95f0d0d2 14760 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14761 break;
14762 }
989f9879
AM
14763 if (htab->elf.tls_sec != NULL)
14764 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
14765 /* The TPREL16 relocs shouldn't really be used in shared
14766 libs or with non-local symbols as that will result in
14767 DT_TEXTREL being set, but support them anyway. */
14768 goto dodyn;
411e1bfb
AM
14769
14770 case R_PPC64_DTPREL16:
14771 case R_PPC64_DTPREL16_LO:
14772 case R_PPC64_DTPREL16_HI:
14773 case R_PPC64_DTPREL16_HA:
14774 case R_PPC64_DTPREL16_DS:
14775 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14776 case R_PPC64_DTPREL16_HIGH:
14777 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14778 case R_PPC64_DTPREL16_HIGHER:
14779 case R_PPC64_DTPREL16_HIGHERA:
14780 case R_PPC64_DTPREL16_HIGHEST:
14781 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14782 if (htab->elf.tls_sec != NULL)
14783 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14784 break;
14785
45965137
AM
14786 case R_PPC64_ADDR64_LOCAL:
14787 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14788 ? h->elf.other
14789 : sym->st_other);
14790 break;
14791
e515b051
AM
14792 case R_PPC64_DTPMOD64:
14793 relocation = 1;
14794 addend = 0;
14795 goto dodyn;
14796
411e1bfb 14797 case R_PPC64_TPREL64:
989f9879
AM
14798 if (htab->elf.tls_sec != NULL)
14799 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14800 goto dodyn;
14801
14802 case R_PPC64_DTPREL64:
989f9879
AM
14803 if (htab->elf.tls_sec != NULL)
14804 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14805 /* Fall through. */
411e1bfb 14806
65f38f15
AM
14807 /* Relocations that may need to be propagated if this is a
14808 dynamic object. */
04c9666a 14809 case R_PPC64_REL30:
65f38f15
AM
14810 case R_PPC64_REL32:
14811 case R_PPC64_REL64:
14812 case R_PPC64_ADDR14:
14813 case R_PPC64_ADDR14_BRNTAKEN:
14814 case R_PPC64_ADDR14_BRTAKEN:
14815 case R_PPC64_ADDR16:
14816 case R_PPC64_ADDR16_DS:
14817 case R_PPC64_ADDR16_HA:
14818 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14819 case R_PPC64_ADDR16_HIGH:
14820 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14821 case R_PPC64_ADDR16_HIGHER:
14822 case R_PPC64_ADDR16_HIGHERA:
14823 case R_PPC64_ADDR16_HIGHEST:
14824 case R_PPC64_ADDR16_HIGHESTA:
14825 case R_PPC64_ADDR16_LO:
14826 case R_PPC64_ADDR16_LO_DS:
14827 case R_PPC64_ADDR24:
65f38f15
AM
14828 case R_PPC64_ADDR32:
14829 case R_PPC64_ADDR64:
14830 case R_PPC64_UADDR16:
14831 case R_PPC64_UADDR32:
14832 case R_PPC64_UADDR64:
411e1bfb 14833 dodyn:
5d1634d7 14834 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14835 break;
14836
41bd81ab
AM
14837 if (NO_OPD_RELOCS && is_opd)
14838 break;
14839
8a9e8e72 14840 if (bfd_link_pic (info)
b1b07054
AM
14841 ? ((h == NULL
14842 || h->dyn_relocs != NULL)
14843 && ((h != NULL && pc_dynrelocs (h))
14844 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
14845 : (h != NULL
14846 ? h->dyn_relocs != NULL
d311bc8b 14847 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14848 {
b34976b6 14849 bfd_boolean skip, relocate;
65f38f15 14850 asection *sreloc;
1cf1f670 14851 bfd_vma out_off;
82e66161 14852 long indx = 0;
65f38f15
AM
14853
14854 /* When generating a dynamic object, these relocations
14855 are copied into the output file to be resolved at run
14856 time. */
14857
b34976b6
AM
14858 skip = FALSE;
14859 relocate = FALSE;
65f38f15 14860
1cf1f670
AM
14861 out_off = _bfd_elf_section_offset (output_bfd, info,
14862 input_section, rel->r_offset);
14863 if (out_off == (bfd_vma) -1)
b34976b6 14864 skip = TRUE;
1cf1f670 14865 else if (out_off == (bfd_vma) -2)
b34976b6 14866 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14867 out_off += (input_section->output_section->vma
14868 + input_section->output_offset);
14869 outrel.r_offset = out_off;
411e1bfb 14870 outrel.r_addend = rel->r_addend;
65f38f15 14871
1cf1f670
AM
14872 /* Optimize unaligned reloc use. */
14873 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14874 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14875 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14876 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14877 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14878 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14879 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14880 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14881 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14882
65f38f15 14883 if (skip)
0bb2d96a 14884 memset (&outrel, 0, sizeof outrel);
afe397ea 14885 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14886 && !is_opd
14887 && r_type != R_PPC64_TOC)
14acf4dc 14888 {
82e66161
AM
14889 indx = h->elf.dynindx;
14890 BFD_ASSERT (indx != -1);
14891 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 14892 }
65f38f15
AM
14893 else
14894 {
41bd81ab
AM
14895 /* This symbol is local, or marked to become local,
14896 or this is an opd section reloc which must point
14897 at a local function. */
65f38f15 14898 outrel.r_addend += relocation;
e86ce104 14899 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14900 {
3fad3c7c 14901 if (is_opd && h != NULL)
afbe61cf
AM
14902 {
14903 /* Lie about opd entries. This case occurs
14904 when building shared libraries and we
14905 reference a function in another shared
3fad3c7c
AM
14906 lib. The same thing happens for a weak
14907 definition in an application that's
14908 overridden by a strong definition in a
14909 shared lib. (I believe this is a generic
14910 bug in binutils handling of weak syms.)
14911 In these cases we won't use the opd
1e2f5b6e 14912 entry in this lib. */
b34976b6 14913 unresolved_reloc = FALSE;
afbe61cf 14914 }
25f23106
AM
14915 if (!is_opd
14916 && r_type == R_PPC64_ADDR64
14917 && (h != NULL
14918 ? h->elf.type == STT_GNU_IFUNC
14919 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14920 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14921 else
14922 {
14923 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14924
25f23106
AM
14925 /* We need to relocate .opd contents for ld.so.
14926 Prelink also wants simple and consistent rules
14927 for relocs. This make all RELATIVE relocs have
14928 *r_offset equal to r_addend. */
14929 relocate = TRUE;
14930 }
65f38f15
AM
14931 }
14932 else
14933 {
25f23106
AM
14934 if (h != NULL
14935 ? h->elf.type == STT_GNU_IFUNC
14936 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14937 {
25f53a85 14938 info->callbacks->einfo
695344c0 14939 /* xgettext:c-format */
174d0a74 14940 (_("%H: %s for indirect "
bc30df16 14941 "function `%T' unsupported\n"),
25f53a85 14942 input_bfd, input_section, rel->r_offset,
25f23106
AM
14943 ppc64_elf_howto_table[r_type]->name,
14944 sym_name);
14945 ret = FALSE;
14946 }
cf35638d 14947 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14948 ;
14949 else if (sec == NULL || sec->owner == NULL)
14950 {
14951 bfd_set_error (bfd_error_bad_value);
b34976b6 14952 return FALSE;
65f38f15
AM
14953 }
14954 else
14955 {
14956 asection *osec;
14957
14958 osec = sec->output_section;
14959 indx = elf_section_data (osec)->dynindx;
14960
74541ad4
AM
14961 if (indx == 0)
14962 {
14963 if ((osec->flags & SEC_READONLY) == 0
14964 && htab->elf.data_index_section != NULL)
14965 osec = htab->elf.data_index_section;
14966 else
14967 osec = htab->elf.text_index_section;
14968 indx = elf_section_data (osec)->dynindx;
14969 }
14970 BFD_ASSERT (indx != 0);
14971
65f38f15
AM
14972 /* We are turning this relocation into one
14973 against a section symbol, so subtract out
14974 the output section's address but not the
14975 offset of the input section in the output
14976 section. */
14977 outrel.r_addend -= osec->vma;
14978 }
14979
14980 outrel.r_info = ELF64_R_INFO (indx, r_type);
14981 }
14982 }
14983
14984 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14985 if (h != NULL
14986 ? h->elf.type == STT_GNU_IFUNC
14987 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
14988 {
14989 sreloc = htab->elf.irelplt;
14990 if (indx == 0)
14991 htab->local_ifunc_resolver = 1;
14992 else if (is_static_defined (&h->elf))
14993 htab->maybe_local_ifunc_resolver = 1;
14994 }
65f38f15
AM
14995 if (sreloc == NULL)
14996 abort ();
14997
dfbb6ac9
AM
14998 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14999 >= sreloc->size)
15000 abort ();
947216bf
AM
15001 loc = sreloc->contents;
15002 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15003 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15004
15005 /* If this reloc is against an external symbol, it will
15006 be computed at runtime, so there's no need to do
81407a69
AM
15007 anything now. However, for the sake of prelink ensure
15008 that the section contents are a known value. */
65f38f15 15009 if (! relocate)
81407a69
AM
15010 {
15011 unresolved_reloc = FALSE;
15012 /* The value chosen here is quite arbitrary as ld.so
15013 ignores section contents except for the special
15014 case of .opd where the contents might be accessed
15015 before relocation. Choose zero, as that won't
15016 cause reloc overflow. */
15017 relocation = 0;
15018 addend = 0;
15019 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15020 to improve backward compatibility with older
15021 versions of ld. */
15022 if (r_type == R_PPC64_ADDR64)
15023 addend = outrel.r_addend;
15024 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15025 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15026 addend = outrel.r_offset;
81407a69 15027 }
65f38f15 15028 }
5bd4f169
AM
15029 break;
15030
65f38f15
AM
15031 case R_PPC64_COPY:
15032 case R_PPC64_GLOB_DAT:
15033 case R_PPC64_JMP_SLOT:
25f23106 15034 case R_PPC64_JMP_IREL:
65f38f15
AM
15035 case R_PPC64_RELATIVE:
15036 /* We shouldn't ever see these dynamic relocs in relocatable
15037 files. */
ae9a127f 15038 /* Fall through. */
65f38f15
AM
15039
15040 case R_PPC64_PLTGOT16:
15041 case R_PPC64_PLTGOT16_DS:
15042 case R_PPC64_PLTGOT16_HA:
15043 case R_PPC64_PLTGOT16_HI:
15044 case R_PPC64_PLTGOT16_LO:
15045 case R_PPC64_PLTGOT16_LO_DS:
15046 case R_PPC64_PLTREL32:
15047 case R_PPC64_PLTREL64:
15048 /* These ones haven't been implemented yet. */
15049
25f53a85 15050 info->callbacks->einfo
695344c0 15051 /* xgettext:c-format */
bc30df16 15052 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 15053 input_bfd,
4ce794b7 15054 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15055
15056 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15057 ret = FALSE;
c316a17c 15058 goto copy_reloc;
65f38f15 15059 }
5bd4f169 15060
67f0cbdb
AM
15061 /* Multi-instruction sequences that access the TOC can be
15062 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15063 to nop; addi rb,r2,x; */
9a23f96e 15064 howto = ppc64_elf_howto_table[(int) r_type];
67f0cbdb
AM
15065 switch (r_type)
15066 {
15067 default:
15068 break;
15069
15070 case R_PPC64_GOT_TLSLD16_HI:
15071 case R_PPC64_GOT_TLSGD16_HI:
15072 case R_PPC64_GOT_TPREL16_HI:
15073 case R_PPC64_GOT_DTPREL16_HI:
15074 case R_PPC64_GOT16_HI:
15075 case R_PPC64_TOC16_HI:
15076 /* These relocs would only be useful if building up an
15077 offset to later add to r2, perhaps in an indexed
15078 addressing mode instruction. Don't try to optimize.
15079 Unfortunately, the possibility of someone building up an
15080 offset like this or even with the HA relocs, means that
15081 we need to check the high insn when optimizing the low
15082 insn. */
15083 break;
15084
15085 case R_PPC64_GOT_TLSLD16_HA:
15086 case R_PPC64_GOT_TLSGD16_HA:
15087 case R_PPC64_GOT_TPREL16_HA:
15088 case R_PPC64_GOT_DTPREL16_HA:
15089 case R_PPC64_GOT16_HA:
15090 case R_PPC64_TOC16_HA:
98528052 15091 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15092 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15093 {
15094 bfd_byte *p = contents + (rel->r_offset & ~3);
15095 bfd_put_32 (input_bfd, NOP, p);
15096 }
67f0cbdb
AM
15097 break;
15098
15099 case R_PPC64_GOT_TLSLD16_LO:
15100 case R_PPC64_GOT_TLSGD16_LO:
15101 case R_PPC64_GOT_TPREL16_LO_DS:
15102 case R_PPC64_GOT_DTPREL16_LO_DS:
15103 case R_PPC64_GOT16_LO:
15104 case R_PPC64_GOT16_LO_DS:
15105 case R_PPC64_TOC16_LO:
15106 case R_PPC64_TOC16_LO_DS:
98528052 15107 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15108 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15109 {
15110 bfd_byte *p = contents + (rel->r_offset & ~3);
15111 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15112 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15113 {
15114 /* Transform addic to addi when we change reg. */
15115 insn &= ~((0x3f << 26) | (0x1f << 16));
15116 insn |= (14u << 26) | (2 << 16);
15117 }
15118 else
67f0cbdb 15119 {
98528052
AM
15120 insn &= ~(0x1f << 16);
15121 insn |= 2 << 16;
67f0cbdb 15122 }
560c8763 15123 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15124 }
15125 break;
9a23f96e
AM
15126
15127 case R_PPC64_TPREL16_HA:
15128 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15129 {
15130 bfd_byte *p = contents + (rel->r_offset & ~3);
15131 insn = bfd_get_32 (input_bfd, p);
15132 if ((insn & ((0x3f << 26) | 0x1f << 16))
15133 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15134 /* xgettext:c-format */
15135 info->callbacks->minfo
15136 (_("%H: warning: %s unexpected insn %#x.\n"),
15137 input_bfd, input_section, rel->r_offset, howto->name, insn);
15138 else
15139 bfd_put_32 (input_bfd, NOP, p);
15140 }
15141 break;
15142
15143 case R_PPC64_TPREL16_LO:
15144 case R_PPC64_TPREL16_LO_DS:
15145 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15146 {
15147 bfd_byte *p = contents + (rel->r_offset & ~3);
15148 insn = bfd_get_32 (input_bfd, p);
15149 insn &= ~(0x1f << 16);
15150 insn |= 13 << 16;
15151 bfd_put_32 (input_bfd, insn, p);
15152 }
15153 break;
67f0cbdb
AM
15154 }
15155
65f38f15
AM
15156 /* Do any further special processing. */
15157 switch (r_type)
15158 {
15159 default:
15160 break;
15161
25f23106 15162 case R_PPC64_REL16_HA:
a680de9a 15163 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15164 case R_PPC64_ADDR16_HA:
15165 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15166 case R_PPC64_ADDR16_HIGHERA:
15167 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15168 case R_PPC64_TOC16_HA:
15169 case R_PPC64_SECTOFF_HA:
411e1bfb 15170 case R_PPC64_TPREL16_HA:
f9c6b907 15171 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15172 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15173 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15174 case R_PPC64_DTPREL16_HA:
15175 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15176 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15177 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15178 /* It's just possible that this symbol is a weak symbol
15179 that's not actually defined anywhere. In that case,
15180 'sec' would be NULL, and we should leave the symbol
15181 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15182 if (sec == NULL)
15183 break;
1a0670f3 15184 /* Fall through. */
5c5f6e17
AM
15185
15186 case R_PPC64_GOT16_HA:
15187 case R_PPC64_PLTGOT16_HA:
15188 case R_PPC64_PLT16_HA:
15189 case R_PPC64_GOT_TLSGD16_HA:
15190 case R_PPC64_GOT_TLSLD16_HA:
15191 case R_PPC64_GOT_TPREL16_HA:
15192 case R_PPC64_GOT_DTPREL16_HA:
15193 /* Add 0x10000 if sign bit in 0:15 is set.
15194 Bits 0:15 are not used. */
15195 addend += 0x8000;
65f38f15
AM
15196 break;
15197
15198 case R_PPC64_ADDR16_DS:
15199 case R_PPC64_ADDR16_LO_DS:
15200 case R_PPC64_GOT16_DS:
15201 case R_PPC64_GOT16_LO_DS:
15202 case R_PPC64_PLT16_LO_DS:
15203 case R_PPC64_SECTOFF_DS:
15204 case R_PPC64_SECTOFF_LO_DS:
15205 case R_PPC64_TOC16_DS:
15206 case R_PPC64_TOC16_LO_DS:
15207 case R_PPC64_PLTGOT16_DS:
15208 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15209 case R_PPC64_GOT_TPREL16_DS:
15210 case R_PPC64_GOT_TPREL16_LO_DS:
15211 case R_PPC64_GOT_DTPREL16_DS:
15212 case R_PPC64_GOT_DTPREL16_LO_DS:
15213 case R_PPC64_TPREL16_DS:
15214 case R_PPC64_TPREL16_LO_DS:
15215 case R_PPC64_DTPREL16_DS:
15216 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15217 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15218 mask = 3;
a680de9a
PB
15219 /* If this reloc is against an lq, lxv, or stxv insn, then
15220 the value must be a multiple of 16. This is somewhat of
15221 a hack, but the "correct" way to do this by defining _DQ
15222 forms of all the _DS relocs bloats all reloc switches in
15223 this file. It doesn't make much sense to use these
15224 relocs in data, so testing the insn should be safe. */
15225 if ((insn & (0x3f << 26)) == (56u << 26)
15226 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15227 mask = 15;
a680de9a
PB
15228 relocation += addend;
15229 addend = insn & (mask ^ 3);
15230 if ((relocation & mask) != 0)
65f38f15 15231 {
a680de9a 15232 relocation ^= relocation & mask;
25f53a85 15233 info->callbacks->einfo
695344c0 15234 /* xgettext:c-format */
174d0a74 15235 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15236 input_bfd, input_section, rel->r_offset,
b80eed39 15237 howto->name,
adadcc0c 15238 mask + 1);
65f38f15 15239 bfd_set_error (bfd_error_bad_value);
b34976b6 15240 ret = FALSE;
c316a17c 15241 goto copy_reloc;
65f38f15
AM
15242 }
15243 break;
5bd4f169
AM
15244 }
15245
239e1f3a
AM
15246 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15247 because such sections are not SEC_ALLOC and thus ld.so will
15248 not process them. */
65f38f15 15249 if (unresolved_reloc
239e1f3a 15250 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15251 && h->elf.def_dynamic)
15252 && _bfd_elf_section_offset (output_bfd, info, input_section,
15253 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15254 {
25f53a85 15255 info->callbacks->einfo
695344c0 15256 /* xgettext:c-format */
174d0a74 15257 (_("%H: unresolvable %s against `%T'\n"),
25f53a85 15258 input_bfd, input_section, rel->r_offset,
b80eed39 15259 howto->name,
039b3fef 15260 h->elf.root.root.string);
b34976b6 15261 ret = FALSE;
9c07fe7c 15262 }
5bd4f169 15263
b80eed39
AM
15264 /* 16-bit fields in insns mostly have signed values, but a
15265 few insns have 16-bit unsigned values. Really, we should
15266 have different reloc types. */
15267 if (howto->complain_on_overflow != complain_overflow_dont
15268 && howto->dst_mask == 0xffff
15269 && (input_section->flags & SEC_CODE) != 0)
15270 {
15271 enum complain_overflow complain = complain_overflow_signed;
15272
15273 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15274 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15275 complain = complain_overflow_bitfield;
15276 else if (howto->rightshift == 0
15277 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15278 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15279 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15280 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15281 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15282 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15283 complain = complain_overflow_unsigned;
15284 if (howto->complain_on_overflow != complain)
15285 {
15286 alt_howto = *howto;
15287 alt_howto.complain_on_overflow = complain;
15288 howto = &alt_howto;
15289 }
15290 }
15291
a680de9a
PB
15292 if (r_type == R_PPC64_REL16DX_HA)
15293 {
15294 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15295 if (rel->r_offset + 4 > input_section->size)
15296 r = bfd_reloc_outofrange;
15297 else
15298 {
15299 relocation += addend;
15300 relocation -= (rel->r_offset
15301 + input_section->output_offset
15302 + input_section->output_section->vma);
3de43e7b 15303 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15304 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15305 insn &= ~0x1fffc1;
3de43e7b 15306 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15307 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15308 r = bfd_reloc_ok;
3de43e7b 15309 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15310 r = bfd_reloc_overflow;
15311 }
15312 }
15313 else
15314 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15315 rel->r_offset, relocation, addend);
5bd4f169 15316
ef60b7ff 15317 if (r != bfd_reloc_ok)
5bd4f169 15318 {
bc30df16 15319 char *more_info = NULL;
b80eed39 15320 const char *reloc_name = howto->name;
bc30df16
AM
15321
15322 if (reloc_dest != DEST_NORMAL)
15323 {
15324 more_info = bfd_malloc (strlen (reloc_name) + 8);
15325 if (more_info != NULL)
15326 {
15327 strcpy (more_info, reloc_name);
15328 strcat (more_info, (reloc_dest == DEST_OPD
15329 ? " (OPD)" : " (stub)"));
15330 reloc_name = more_info;
15331 }
15332 }
15333
cd27b276 15334 if (r == bfd_reloc_overflow)
5bd4f169 15335 {
8131c122
AM
15336 /* On code like "if (foo) foo();" don't report overflow
15337 on a branch to zero when foo is undefined. */
15338 if (!warned
15339 && (reloc_dest == DEST_STUB
15340 || !(h != NULL
15341 && (h->elf.root.type == bfd_link_hash_undefweak
15342 || h->elf.root.type == bfd_link_hash_undefined)
15343 && is_branch_reloc (r_type))))
1a72702b
AM
15344 info->callbacks->reloc_overflow (info, &h->elf.root,
15345 sym_name, reloc_name,
15346 orig_rel.r_addend,
15347 input_bfd, input_section,
15348 rel->r_offset);
ef60b7ff
AM
15349 }
15350 else
15351 {
25f53a85 15352 info->callbacks->einfo
695344c0 15353 /* xgettext:c-format */
174d0a74 15354 (_("%H: %s against `%T': error %d\n"),
25f53a85 15355 input_bfd, input_section, rel->r_offset,
bc30df16 15356 reloc_name, sym_name, (int) r);
b34976b6 15357 ret = FALSE;
ef60b7ff 15358 }
bc30df16
AM
15359 if (more_info != NULL)
15360 free (more_info);
5bd4f169 15361 }
c316a17c
AM
15362 copy_reloc:
15363 if (wrel != rel)
15364 *wrel = *rel;
15365 }
15366
15367 if (wrel != rel)
15368 {
15369 Elf_Internal_Shdr *rel_hdr;
15370 size_t deleted = rel - wrel;
15371
15372 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15373 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15374 if (rel_hdr->sh_size == 0)
15375 {
15376 /* It is too late to remove an empty reloc section. Leave
15377 one NONE reloc.
15378 ??? What is wrong with an empty section??? */
15379 rel_hdr->sh_size = rel_hdr->sh_entsize;
15380 deleted -= 1;
15381 }
15382 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15383 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15384 input_section->reloc_count -= deleted;
5bd4f169
AM
15385 }
15386
645ea6a9
AM
15387 /* If we're emitting relocations, then shortly after this function
15388 returns, reloc offsets and addends for this section will be
15389 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15390 file rather than the input. Save a copy of the relocs for
15391 opd_entry_value. */
0e1862bb 15392 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15393 {
15394 bfd_size_type amt;
15395 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15396 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15397 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15398 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15399 if (rel == NULL)
15400 return FALSE;
15401 memcpy (rel, relocs, amt);
15402 }
5bd4f169
AM
15403 return ret;
15404}
15405
754021d0
AM
15406/* Adjust the value of any local symbols in opd sections. */
15407
6e0b88f1 15408static int
754021d0
AM
15409ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15410 const char *name ATTRIBUTE_UNUSED,
15411 Elf_Internal_Sym *elfsym,
15412 asection *input_sec,
15413 struct elf_link_hash_entry *h)
15414{
74f0fb50
AM
15415 struct _opd_sec_data *opd;
15416 long adjust;
754021d0
AM
15417 bfd_vma value;
15418
4025353c 15419 if (h != NULL)
6e0b88f1 15420 return 1;
4025353c 15421
74f0fb50
AM
15422 opd = get_opd_info (input_sec);
15423 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15424 return 1;
754021d0
AM
15425
15426 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15427 if (!bfd_link_relocatable (info))
754021d0
AM
15428 value -= input_sec->output_section->vma;
15429
51aecdc5 15430 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15431 if (adjust == -1)
6e0b88f1
AM
15432 return 2;
15433
15434 elfsym->st_value += adjust;
15435 return 1;
754021d0
AM
15436}
15437
5bd4f169
AM
15438/* Finish up dynamic symbol handling. We set the contents of various
15439 dynamic sections here. */
15440
b34976b6 15441static bfd_boolean
4ce794b7
AM
15442ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15443 struct bfd_link_info *info,
15444 struct elf_link_hash_entry *h,
4aef7643 15445 Elf_Internal_Sym *sym)
5bd4f169 15446{
65f38f15 15447 struct ppc_link_hash_table *htab;
8387904d
AM
15448 struct plt_entry *ent;
15449 Elf_Internal_Rela rela;
15450 bfd_byte *loc;
5bd4f169 15451
65f38f15 15452 htab = ppc_hash_table (info);
4dfe6ac6
NC
15453 if (htab == NULL)
15454 return FALSE;
5bd4f169 15455
8387904d
AM
15456 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15457 if (ent->plt.offset != (bfd_vma) -1)
15458 {
15459 /* This symbol has an entry in the procedure linkage
15460 table. Set it up. */
e054468f
AM
15461 if (!htab->elf.dynamic_sections_created
15462 || h->dynindx == -1)
15463 {
15464 BFD_ASSERT (h->type == STT_GNU_IFUNC
15465 && h->def_regular
15466 && (h->root.type == bfd_link_hash_defined
15467 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15468 rela.r_offset = (htab->elf.iplt->output_section->vma
15469 + htab->elf.iplt->output_offset
25f23106 15470 + ent->plt.offset);
ee67d69a
AM
15471 if (htab->opd_abi)
15472 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15473 else
15474 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15475 rela.r_addend = (h->root.u.def.value
15476 + h->root.u.def.section->output_offset
15477 + h->root.u.def.section->output_section->vma
15478 + ent->addend);
33e44f2e
AM
15479 loc = (htab->elf.irelplt->contents
15480 + (htab->elf.irelplt->reloc_count++
25f23106 15481 * sizeof (Elf64_External_Rela)));
82e66161 15482 htab->local_ifunc_resolver = 1;
e054468f
AM
15483 }
15484 else
15485 {
33e44f2e
AM
15486 rela.r_offset = (htab->elf.splt->output_section->vma
15487 + htab->elf.splt->output_offset
25f23106 15488 + ent->plt.offset);
e054468f
AM
15489 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15490 rela.r_addend = ent->addend;
33e44f2e 15491 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15492 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15493 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
82e66161
AM
15494 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15495 htab->maybe_local_ifunc_resolver = 1;
e054468f 15496 }
8387904d 15497 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15498
15499 if (!htab->opd_abi)
15500 {
15501 if (!h->def_regular)
15502 {
15503 /* Mark the symbol as undefined, rather than as
15504 defined in glink. Leave the value if there were
15505 any relocations where pointer equality matters
15506 (this is a clue for the dynamic linker, to make
15507 function pointer comparisons work between an
15508 application and shared library), otherwise set it
15509 to zero. */
15510 sym->st_shndx = SHN_UNDEF;
15511 if (!h->pointer_equality_needed)
15512 sym->st_value = 0;
15513 else if (!h->ref_regular_nonweak)
15514 {
15515 /* This breaks function pointer comparisons, but
15516 that is better than breaking tests for a NULL
15517 function pointer. */
15518 sym->st_value = 0;
15519 }
15520 }
15521 }
8387904d 15522 }
5bd4f169 15523
f5385ebf 15524 if (h->needs_copy)
5bd4f169 15525 {
65f38f15 15526 /* This symbol needs a copy reloc. Set it up. */
5474d94f 15527 asection *srel;
5bd4f169 15528
65f38f15
AM
15529 if (h->dynindx == -1
15530 || (h->root.type != bfd_link_hash_defined
15531 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15532 || htab->elf.srelbss == NULL
15533 || htab->elf.sreldynrelro == NULL)
65f38f15 15534 abort ();
5bd4f169
AM
15535
15536 rela.r_offset = (h->root.u.def.value
15537 + h->root.u.def.section->output_section->vma
15538 + h->root.u.def.section->output_offset);
15539 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15540 rela.r_addend = 0;
afbf7e8e 15541 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15542 srel = htab->elf.sreldynrelro;
15543 else
15544 srel = htab->elf.srelbss;
15545 loc = srel->contents;
15546 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15547 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15548 }
15549
b34976b6 15550 return TRUE;
5bd4f169
AM
15551}
15552
65f38f15
AM
15553/* Used to decide how to sort relocs in an optimal manner for the
15554 dynamic linker, before writing them out. */
15555
15556static enum elf_reloc_type_class
7e612e98
AM
15557ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15558 const asection *rel_sec,
15559 const Elf_Internal_Rela *rela)
65f38f15 15560{
04c9666a 15561 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15562 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15563
33e44f2e 15564 if (rel_sec == htab->elf.irelplt)
7e612e98 15565 return reloc_class_ifunc;
a33d1f77 15566
4ce794b7 15567 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15568 switch (r_type)
65f38f15
AM
15569 {
15570 case R_PPC64_RELATIVE:
15571 return reloc_class_relative;
15572 case R_PPC64_JMP_SLOT:
15573 return reloc_class_plt;
15574 case R_PPC64_COPY:
15575 return reloc_class_copy;
15576 default:
15577 return reloc_class_normal;
15578 }
15579}
15580
5bd4f169
AM
15581/* Finish up the dynamic sections. */
15582
b34976b6 15583static bfd_boolean
4ce794b7
AM
15584ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15585 struct bfd_link_info *info)
5bd4f169 15586{
65f38f15
AM
15587 struct ppc_link_hash_table *htab;
15588 bfd *dynobj;
5bd4f169 15589 asection *sdyn;
5bd4f169 15590
65f38f15 15591 htab = ppc_hash_table (info);
4dfe6ac6
NC
15592 if (htab == NULL)
15593 return FALSE;
15594
65f38f15 15595 dynobj = htab->elf.dynobj;
3d4d4302 15596 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15597
65f38f15 15598 if (htab->elf.dynamic_sections_created)
5bd4f169 15599 {
5bd4f169
AM
15600 Elf64_External_Dyn *dyncon, *dynconend;
15601
33e44f2e 15602 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15603 abort ();
5bd4f169
AM
15604
15605 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15606 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15607 for (; dyncon < dynconend; dyncon++)
15608 {
15609 Elf_Internal_Dyn dyn;
19397422 15610 asection *s;
5bd4f169
AM
15611
15612 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15613
15614 switch (dyn.d_tag)
15615 {
65f38f15
AM
15616 default:
15617 continue;
5bd4f169 15618
5d1634d7 15619 case DT_PPC64_GLINK:
4ce794b7 15620 s = htab->glink;
6348e046 15621 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15622 /* We stupidly defined DT_PPC64_GLINK to be the start
15623 of glink rather than the first entry point, which is
15624 what ld.so needs, and now have a bigger stub to
15625 support automatic multiple TOCs. */
9e390558 15626 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
15627 break;
15628
19397422
AM
15629 case DT_PPC64_OPD:
15630 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15631 if (s == NULL)
15632 continue;
15633 dyn.d_un.d_ptr = s->vma;
19397422
AM
15634 break;
15635
e8910a83
AM
15636 case DT_PPC64_OPT:
15637 if (htab->do_multi_toc && htab->multi_toc_needed)
15638 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
15639 if (htab->has_plt_localentry0)
15640 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
15641 break;
15642
19397422
AM
15643 case DT_PPC64_OPDSZ:
15644 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15645 if (s == NULL)
15646 continue;
eea6121a 15647 dyn.d_un.d_val = s->size;
19397422
AM
15648 break;
15649
65f38f15 15650 case DT_PLTGOT:
33e44f2e 15651 s = htab->elf.splt;
6348e046 15652 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15653 break;
15654
15655 case DT_JMPREL:
33e44f2e 15656 s = htab->elf.srelplt;
6348e046 15657 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15658 break;
5bd4f169 15659
65f38f15 15660 case DT_PLTRELSZ:
33e44f2e 15661 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15662 break;
82e66161
AM
15663
15664 case DT_TEXTREL:
15665 if (htab->local_ifunc_resolver)
15666 info->callbacks->einfo
15667 (_("%X%P: text relocations and GNU indirect "
15668 "functions will result in a segfault at runtime\n"));
15669 else if (htab->maybe_local_ifunc_resolver)
15670 info->callbacks->einfo
15671 (_("%P: warning: text relocations and GNU indirect "
15672 "functions may result in a segfault at runtime\n"));
15673 continue;
5bd4f169 15674 }
5bd4f169 15675
65f38f15 15676 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15677 }
5bd4f169
AM
15678 }
15679
6528b6eb
AM
15680 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15681 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15682 {
15683 /* Fill in the first entry in the global offset table.
15684 We use it to hold the link-time TOCbase. */
15685 bfd_put_64 (output_bfd,
60ee0d4a 15686 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15687 htab->elf.sgot->contents);
5d1634d7
AM
15688
15689 /* Set .got entry size. */
33e44f2e 15690 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15691 }
15692
6528b6eb
AM
15693 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15694 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15695 {
15696 /* Set .plt entry size. */
33e44f2e 15697 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15698 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15699 }
15700
84f5d08e
AM
15701 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15702 brlt ourselves if emitrelocations. */
15703 if (htab->brlt != NULL
15704 && htab->brlt->reloc_count != 0
15705 && !_bfd_elf_link_output_relocs (output_bfd,
15706 htab->brlt,
d4730f92 15707 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15708 elf_section_data (htab->brlt)->relocs,
15709 NULL))
15710 return FALSE;
15711
176a0d42
AM
15712 if (htab->glink != NULL
15713 && htab->glink->reloc_count != 0
15714 && !_bfd_elf_link_output_relocs (output_bfd,
15715 htab->glink,
d4730f92 15716 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15717 elf_section_data (htab->glink)->relocs,
15718 NULL))
15719 return FALSE;
15720
58d180e8 15721 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15722 && htab->glink_eh_frame->size != 0)
15723 {
15724 bfd_vma val;
15725 bfd_byte *p;
d4aaa2a0 15726 struct map_stub *group;
2e0ce1c8 15727 size_t align = 4;
da44f4e5 15728
2e0ce1c8
AM
15729 p = htab->glink_eh_frame->contents;
15730 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
d4aaa2a0
AM
15731
15732 for (group = htab->group; group != NULL; group = group->next)
15733 if (group->stub_sec != NULL)
da44f4e5 15734 {
da44f4e5 15735 /* Offset to stub section. */
d4aaa2a0
AM
15736 val = (group->stub_sec->output_section->vma
15737 + group->stub_sec->output_offset);
da44f4e5
AM
15738 val -= (htab->glink_eh_frame->output_section->vma
15739 + htab->glink_eh_frame->output_offset
d4aaa2a0 15740 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15741 if (val + 0x80000000 > 0xffffffff)
15742 {
15743 info->callbacks->einfo
15744 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
d4aaa2a0 15745 group->stub_sec->name);
da44f4e5
AM
15746 return FALSE;
15747 }
d4aaa2a0
AM
15748 bfd_put_32 (dynobj, val, p + 8);
15749 p += stub_eh_frame_size (group, align);
da44f4e5
AM
15750 }
15751 if (htab->glink != NULL && htab->glink->size != 0)
15752 {
da44f4e5
AM
15753 /* Offset to .glink. */
15754 val = (htab->glink->output_section->vma
15755 + htab->glink->output_offset
15756 + 8);
15757 val -= (htab->glink_eh_frame->output_section->vma
15758 + htab->glink_eh_frame->output_offset
d4aaa2a0 15759 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15760 if (val + 0x80000000 > 0xffffffff)
15761 {
15762 info->callbacks->einfo
15763 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15764 htab->glink->name);
15765 return FALSE;
15766 }
d4aaa2a0
AM
15767 bfd_put_32 (dynobj, val, p + 8);
15768 p += (24 + align - 1) & -align;
da44f4e5
AM
15769 }
15770
15771 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15772 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15773 htab->glink_eh_frame,
15774 htab->glink_eh_frame->contents))
15775 return FALSE;
15776 }
58d180e8 15777
e717da7e 15778 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15779 since we didn't add them to DYNOBJ. We know dynobj is the first
15780 bfd. */
c72f2fb2 15781 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15782 {
15783 asection *s;
7b53ace3 15784
0c8d6e5c 15785 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15786 continue;
15787
e717da7e
AM
15788 s = ppc64_elf_tdata (dynobj)->got;
15789 if (s != NULL
eea6121a 15790 && s->size != 0
e717da7e
AM
15791 && s->output_section != bfd_abs_section_ptr
15792 && !bfd_set_section_contents (output_bfd, s->output_section,
15793 s->contents, s->output_offset,
eea6121a 15794 s->size))
e717da7e
AM
15795 return FALSE;
15796 s = ppc64_elf_tdata (dynobj)->relgot;
15797 if (s != NULL
eea6121a 15798 && s->size != 0
e717da7e
AM
15799 && s->output_section != bfd_abs_section_ptr
15800 && !bfd_set_section_contents (output_bfd, s->output_section,
15801 s->contents, s->output_offset,
eea6121a 15802 s->size))
e717da7e
AM
15803 return FALSE;
15804 }
f6c52c13 15805
b34976b6 15806 return TRUE;
5bd4f169
AM
15807}
15808
5bd4f169 15809#include "elf64-target.h"
7b8e7dad
AM
15810
15811/* FreeBSD support */
15812
15813#undef TARGET_LITTLE_SYM
15814#undef TARGET_LITTLE_NAME
15815
15816#undef TARGET_BIG_SYM
6d00b590 15817#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15818#undef TARGET_BIG_NAME
15819#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15820
15821#undef ELF_OSABI
15822#define ELF_OSABI ELFOSABI_FREEBSD
15823
15824#undef elf64_bed
15825#define elf64_bed elf64_powerpc_fbsd_bed
15826
15827#include "elf64-target.h"