[ARC] Update warning reporting.
[deliverable/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
702d1671
AM
67#define ELF_COMMONPAGESIZE 0x1000
68#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
69#define elf_info_to_howto ppc64_elf_info_to_howto
70
71#define elf_backend_want_got_sym 0
72#define elf_backend_want_plt_sym 0
73#define elf_backend_plt_alignment 3
74#define elf_backend_plt_not_loaded 1
ad8e1ba5 75#define elf_backend_got_header_size 8
5474d94f 76#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
77#define elf_backend_can_gc_sections 1
78#define elf_backend_can_refcount 1
79#define elf_backend_rela_normal 1
64f52338 80#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 81#define elf_backend_default_execstack 0
ad8e1ba5 82
e717da7e 83#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 84#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 85#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
86#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
88#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 90#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 91#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 92#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
93
94#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
95#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 97#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 98#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 99#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 100#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 101#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 102#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 103#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 104#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 105#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 106#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 107#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 108#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
109#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 111#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
112#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 114#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 115#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 116#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
117#define elf_backend_relocate_section ppc64_elf_relocate_section
118#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 121#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 122#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 123#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 124#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 125#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 126
5bd4f169
AM
127/* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 132#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 133#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
134
135/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 136#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 137
a078d95a
AM
138/* Offsets to some stack save slots. */
139#define STK_LR 16
140#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 141/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
5bd4f169 146/* TOC base pointers offset from start of TOC. */
411e1bfb 147#define TOC_BASE_OFF 0x8000
a27e685f
AM
148/* TOC base alignment. */
149#define TOC_BASE_ALIGN 256
411e1bfb
AM
150
151/* Offset of tp and dtp pointers from start of TLS block. */
152#define TP_OFFSET 0x7000
153#define DTP_OFFSET 0x8000
5bd4f169 154
ad8e1ba5
AM
155/* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 157 insert an addi to adjust r11. */
a078d95a 158#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
159#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
164#define BCTR 0x4e800420 /* bctr */
165
07d6d2b8 166#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
167#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
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 */
05d0e962 189#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
190#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 192#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 193#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962
AM
194#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 196#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
05d0e962
AM
197#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
a345bc8d 200
9e390558
AM
201/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
204 /* 0: */
205 /* .quad plt0-1f */
206 /* __glink: */
207#define MFLR_R12 0x7d8802a6 /* mflr %12 */
208#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
209 /* 1: */
210#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 211 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 212#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
213#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
214 /* ld %12,0(%11) */
215 /* ld %2,8(%11) */
216 /* mtctr %12 */
217 /* ld %11,16(%11) */
ee4bf8d2 218 /* bctr */
b9e5796b
AM
219#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
224
225/* Pad with this. */
226#define NOP 0x60000000
227
721956f4
AM
228/* Some other nops. */
229#define CROR_151515 0x4def7b82
230#define CROR_313131 0x4ffffb82
231
cedb70c5 232/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
233#define LI_R0_0 0x38000000 /* li %r0,0 */
234#define B_DOT 0x48000000 /* b . */
235
236/* After that, we need two instructions to load the index, followed by
237 a branch. */
238#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 239#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 240
deb0e272
AM
241/* Instructions used by the save and restore reg functions. */
242#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
246#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
248#define LI_R12_0 0x39800000 /* li %r12,0 */
249#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
252#define BLR 0x4e800020 /* blr */
253
41bd81ab
AM
254/* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 257 relocate .opd without reloc entries. */
41bd81ab
AM
258#ifndef NO_OPD_RELOCS
259#define NO_OPD_RELOCS 0
260#endif
810d4e75 261
a4b6fadd
AM
262#ifndef ARRAY_SIZE
263#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
264#endif
265
810d4e75
AM
266static inline int
267abiversion (bfd *abfd)
268{
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
270}
271
272static inline void
273set_abiversion (bfd *abfd, int ver)
274{
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
277}
5bd4f169 278\f
f5e87a1d 279#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 280
5bd4f169 281/* Relocation HOWTO's. */
04c9666a 282static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 283
f3185997
NC
284static reloc_howto_type ppc64_elf_howto_raw[] =
285{
5bd4f169
AM
286 /* This reloc does nothing. */
287 HOWTO (R_PPC64_NONE, /* type */
288 0, /* rightshift */
6346d5ca
AM
289 3, /* size (0 = byte, 1 = short, 2 = long) */
290 0, /* bitsize */
b34976b6 291 FALSE, /* pc_relative */
5bd4f169 292 0, /* bitpos */
f5e87a1d 293 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
294 bfd_elf_generic_reloc, /* special_function */
295 "R_PPC64_NONE", /* name */
b34976b6 296 FALSE, /* partial_inplace */
d006db6c 297 0, /* src_mask */
5bd4f169 298 0, /* dst_mask */
b34976b6 299 FALSE), /* pcrel_offset */
5bd4f169
AM
300
301 /* A standard 32 bit relocation. */
302 HOWTO (R_PPC64_ADDR32, /* type */
303 0, /* rightshift */
304 2, /* size (0 = byte, 1 = short, 2 = long) */
305 32, /* bitsize */
b34976b6 306 FALSE, /* pc_relative */
5bd4f169
AM
307 0, /* bitpos */
308 complain_overflow_bitfield, /* complain_on_overflow */
309 bfd_elf_generic_reloc, /* special_function */
310 "R_PPC64_ADDR32", /* name */
b34976b6 311 FALSE, /* partial_inplace */
5bd4f169
AM
312 0, /* src_mask */
313 0xffffffff, /* dst_mask */
b34976b6 314 FALSE), /* pcrel_offset */
5bd4f169
AM
315
316 /* An absolute 26 bit branch; the lower two bits must be zero.
317 FIXME: we don't check that, we just clear them. */
318 HOWTO (R_PPC64_ADDR24, /* type */
319 0, /* rightshift */
320 2, /* size (0 = byte, 1 = short, 2 = long) */
321 26, /* bitsize */
b34976b6 322 FALSE, /* pc_relative */
5bd4f169
AM
323 0, /* bitpos */
324 complain_overflow_bitfield, /* complain_on_overflow */
325 bfd_elf_generic_reloc, /* special_function */
326 "R_PPC64_ADDR24", /* name */
b34976b6 327 FALSE, /* partial_inplace */
d006db6c 328 0, /* src_mask */
f5e87a1d 329 0x03fffffc, /* dst_mask */
b34976b6 330 FALSE), /* pcrel_offset */
5bd4f169
AM
331
332 /* A standard 16 bit relocation. */
333 HOWTO (R_PPC64_ADDR16, /* type */
334 0, /* rightshift */
335 1, /* size (0 = byte, 1 = short, 2 = long) */
336 16, /* bitsize */
b34976b6 337 FALSE, /* pc_relative */
5bd4f169
AM
338 0, /* bitpos */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_PPC64_ADDR16", /* name */
b34976b6 342 FALSE, /* partial_inplace */
5bd4f169
AM
343 0, /* src_mask */
344 0xffff, /* dst_mask */
b34976b6 345 FALSE), /* pcrel_offset */
5bd4f169
AM
346
347 /* A 16 bit relocation without overflow. */
348 HOWTO (R_PPC64_ADDR16_LO, /* type */
349 0, /* rightshift */
350 1, /* size (0 = byte, 1 = short, 2 = long) */
351 16, /* bitsize */
b34976b6 352 FALSE, /* pc_relative */
5bd4f169
AM
353 0, /* bitpos */
354 complain_overflow_dont,/* complain_on_overflow */
355 bfd_elf_generic_reloc, /* special_function */
356 "R_PPC64_ADDR16_LO", /* name */
b34976b6 357 FALSE, /* partial_inplace */
5bd4f169
AM
358 0, /* src_mask */
359 0xffff, /* dst_mask */
b34976b6 360 FALSE), /* pcrel_offset */
5bd4f169
AM
361
362 /* Bits 16-31 of an address. */
363 HOWTO (R_PPC64_ADDR16_HI, /* type */
364 16, /* rightshift */
365 1, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
b34976b6 367 FALSE, /* pc_relative */
5bd4f169 368 0, /* bitpos */
f9c6b907 369 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
370 bfd_elf_generic_reloc, /* special_function */
371 "R_PPC64_ADDR16_HI", /* name */
b34976b6 372 FALSE, /* partial_inplace */
5bd4f169
AM
373 0, /* src_mask */
374 0xffff, /* dst_mask */
b34976b6 375 FALSE), /* pcrel_offset */
5bd4f169
AM
376
377 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
378 bits, treated as a signed number, is negative. */
379 HOWTO (R_PPC64_ADDR16_HA, /* type */
380 16, /* rightshift */
381 1, /* size (0 = byte, 1 = short, 2 = long) */
382 16, /* bitsize */
b34976b6 383 FALSE, /* pc_relative */
5bd4f169 384 0, /* bitpos */
f9c6b907 385 complain_overflow_signed, /* complain_on_overflow */
805fc799 386 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 387 "R_PPC64_ADDR16_HA", /* name */
b34976b6 388 FALSE, /* partial_inplace */
5bd4f169
AM
389 0, /* src_mask */
390 0xffff, /* dst_mask */
b34976b6 391 FALSE), /* pcrel_offset */
5bd4f169
AM
392
393 /* An absolute 16 bit branch; the lower two bits must be zero.
394 FIXME: we don't check that, we just clear them. */
395 HOWTO (R_PPC64_ADDR14, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
b34976b6 399 FALSE, /* pc_relative */
5bd4f169 400 0, /* bitpos */
b80eed39 401 complain_overflow_signed, /* complain_on_overflow */
2441e016 402 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 403 "R_PPC64_ADDR14", /* name */
b34976b6 404 FALSE, /* partial_inplace */
d006db6c 405 0, /* src_mask */
f5e87a1d 406 0x0000fffc, /* dst_mask */
b34976b6 407 FALSE), /* pcrel_offset */
5bd4f169
AM
408
409 /* An absolute 16 bit branch, for which bit 10 should be set to
410 indicate that the branch is expected to be taken. The lower two
411 bits must be zero. */
412 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
b34976b6 416 FALSE, /* pc_relative */
5bd4f169 417 0, /* bitpos */
b80eed39 418 complain_overflow_signed, /* complain_on_overflow */
805fc799 419 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 420 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 421 FALSE, /* partial_inplace */
d006db6c 422 0, /* src_mask */
f5e87a1d 423 0x0000fffc, /* dst_mask */
b34976b6 424 FALSE), /* pcrel_offset */
5bd4f169
AM
425
426 /* An absolute 16 bit branch, for which bit 10 should be set to
427 indicate that the branch is not expected to be taken. The lower
428 two bits must be zero. */
429 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 16, /* bitsize */
b34976b6 433 FALSE, /* pc_relative */
5bd4f169 434 0, /* bitpos */
b80eed39 435 complain_overflow_signed, /* complain_on_overflow */
805fc799 436 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 437 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 438 FALSE, /* partial_inplace */
d006db6c 439 0, /* src_mask */
f5e87a1d 440 0x0000fffc, /* dst_mask */
b34976b6 441 FALSE), /* pcrel_offset */
5bd4f169
AM
442
443 /* A relative 26 bit branch; the lower two bits must be zero. */
444 HOWTO (R_PPC64_REL24, /* type */
445 0, /* rightshift */
446 2, /* size (0 = byte, 1 = short, 2 = long) */
447 26, /* bitsize */
b34976b6 448 TRUE, /* pc_relative */
5bd4f169
AM
449 0, /* bitpos */
450 complain_overflow_signed, /* complain_on_overflow */
2441e016 451 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 452 "R_PPC64_REL24", /* name */
b34976b6 453 FALSE, /* partial_inplace */
d006db6c 454 0, /* src_mask */
f5e87a1d 455 0x03fffffc, /* dst_mask */
b34976b6 456 TRUE), /* pcrel_offset */
5bd4f169 457
05d0e962
AM
458 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
459 HOWTO (R_PPC64_REL24_NOTOC, /* type */
460 0, /* rightshift */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
462 26, /* bitsize */
463 TRUE, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_signed, /* complain_on_overflow */
466 ppc64_elf_branch_reloc, /* special_function */
467 "R_PPC64_REL24_NOTOC", /* name */
468 FALSE, /* partial_inplace */
469 0, /* src_mask */
470 0x03fffffc, /* dst_mask */
471 TRUE), /* pcrel_offset */
472
5bd4f169
AM
473 /* A relative 16 bit branch; the lower two bits must be zero. */
474 HOWTO (R_PPC64_REL14, /* type */
475 0, /* rightshift */
476 2, /* size (0 = byte, 1 = short, 2 = long) */
477 16, /* bitsize */
b34976b6 478 TRUE, /* pc_relative */
5bd4f169
AM
479 0, /* bitpos */
480 complain_overflow_signed, /* complain_on_overflow */
2441e016 481 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 482 "R_PPC64_REL14", /* name */
b34976b6 483 FALSE, /* partial_inplace */
d006db6c 484 0, /* src_mask */
f5e87a1d 485 0x0000fffc, /* dst_mask */
b34976b6 486 TRUE), /* pcrel_offset */
5bd4f169
AM
487
488 /* A relative 16 bit branch. Bit 10 should be set to indicate that
489 the branch is expected to be taken. The lower two bits must be
490 zero. */
491 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
492 0, /* rightshift */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
b34976b6 495 TRUE, /* pc_relative */
5bd4f169
AM
496 0, /* bitpos */
497 complain_overflow_signed, /* complain_on_overflow */
805fc799 498 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 499 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 500 FALSE, /* partial_inplace */
d006db6c 501 0, /* src_mask */
f5e87a1d 502 0x0000fffc, /* dst_mask */
b34976b6 503 TRUE), /* pcrel_offset */
5bd4f169
AM
504
505 /* A relative 16 bit branch. Bit 10 should be set to indicate that
506 the branch is not expected to be taken. The lower two bits must
507 be zero. */
508 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
509 0, /* rightshift */
510 2, /* size (0 = byte, 1 = short, 2 = long) */
511 16, /* bitsize */
b34976b6 512 TRUE, /* pc_relative */
5bd4f169
AM
513 0, /* bitpos */
514 complain_overflow_signed, /* complain_on_overflow */
805fc799 515 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 516 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 517 FALSE, /* partial_inplace */
d006db6c 518 0, /* src_mask */
f5e87a1d 519 0x0000fffc, /* dst_mask */
b34976b6 520 TRUE), /* pcrel_offset */
5bd4f169
AM
521
522 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
523 symbol. */
524 HOWTO (R_PPC64_GOT16, /* type */
525 0, /* rightshift */
526 1, /* size (0 = byte, 1 = short, 2 = long) */
527 16, /* bitsize */
b34976b6 528 FALSE, /* pc_relative */
5bd4f169
AM
529 0, /* bitpos */
530 complain_overflow_signed, /* complain_on_overflow */
805fc799 531 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 532 "R_PPC64_GOT16", /* name */
b34976b6 533 FALSE, /* partial_inplace */
5bd4f169
AM
534 0, /* src_mask */
535 0xffff, /* dst_mask */
b34976b6 536 FALSE), /* pcrel_offset */
5bd4f169
AM
537
538 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
539 the symbol. */
540 HOWTO (R_PPC64_GOT16_LO, /* type */
541 0, /* rightshift */
542 1, /* size (0 = byte, 1 = short, 2 = long) */
543 16, /* bitsize */
b34976b6 544 FALSE, /* pc_relative */
5bd4f169
AM
545 0, /* bitpos */
546 complain_overflow_dont, /* complain_on_overflow */
805fc799 547 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 548 "R_PPC64_GOT16_LO", /* name */
b34976b6 549 FALSE, /* partial_inplace */
5bd4f169
AM
550 0, /* src_mask */
551 0xffff, /* dst_mask */
b34976b6 552 FALSE), /* pcrel_offset */
5bd4f169
AM
553
554 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
555 the symbol. */
556 HOWTO (R_PPC64_GOT16_HI, /* type */
557 16, /* rightshift */
558 1, /* size (0 = byte, 1 = short, 2 = long) */
559 16, /* bitsize */
b34976b6 560 FALSE, /* pc_relative */
5bd4f169 561 0, /* bitpos */
f9c6b907 562 complain_overflow_signed,/* complain_on_overflow */
805fc799 563 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 564 "R_PPC64_GOT16_HI", /* name */
b34976b6 565 FALSE, /* partial_inplace */
5bd4f169
AM
566 0, /* src_mask */
567 0xffff, /* dst_mask */
b34976b6 568 FALSE), /* pcrel_offset */
5bd4f169
AM
569
570 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
571 the symbol. */
572 HOWTO (R_PPC64_GOT16_HA, /* type */
573 16, /* rightshift */
574 1, /* size (0 = byte, 1 = short, 2 = long) */
575 16, /* bitsize */
b34976b6 576 FALSE, /* pc_relative */
5bd4f169 577 0, /* bitpos */
f9c6b907 578 complain_overflow_signed,/* complain_on_overflow */
805fc799 579 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 580 "R_PPC64_GOT16_HA", /* name */
b34976b6 581 FALSE, /* partial_inplace */
5bd4f169
AM
582 0, /* src_mask */
583 0xffff, /* dst_mask */
b34976b6 584 FALSE), /* pcrel_offset */
5bd4f169
AM
585
586 /* This is used only by the dynamic linker. The symbol should exist
587 both in the object being run and in some shared library. The
588 dynamic linker copies the data addressed by the symbol from the
589 shared library into the object, because the object being
590 run has to have the data at some particular address. */
591 HOWTO (R_PPC64_COPY, /* type */
592 0, /* rightshift */
f5e87a1d
AM
593 0, /* this one is variable size */
594 0, /* bitsize */
b34976b6 595 FALSE, /* pc_relative */
5bd4f169 596 0, /* bitpos */
f5e87a1d
AM
597 complain_overflow_dont, /* complain_on_overflow */
598 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 599 "R_PPC64_COPY", /* name */
b34976b6 600 FALSE, /* partial_inplace */
5bd4f169
AM
601 0, /* src_mask */
602 0, /* dst_mask */
b34976b6 603 FALSE), /* pcrel_offset */
5bd4f169
AM
604
605 /* Like R_PPC64_ADDR64, but used when setting global offset table
606 entries. */
607 HOWTO (R_PPC64_GLOB_DAT, /* type */
608 0, /* rightshift */
609 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
610 64, /* bitsize */
b34976b6 611 FALSE, /* pc_relative */
5bd4f169
AM
612 0, /* bitpos */
613 complain_overflow_dont, /* complain_on_overflow */
805fc799 614 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 615 "R_PPC64_GLOB_DAT", /* name */
b34976b6 616 FALSE, /* partial_inplace */
5bd4f169 617 0, /* src_mask */
f5e87a1d 618 ONES (64), /* dst_mask */
b34976b6 619 FALSE), /* pcrel_offset */
5bd4f169
AM
620
621 /* Created by the link editor. Marks a procedure linkage table
622 entry for a symbol. */
623 HOWTO (R_PPC64_JMP_SLOT, /* type */
624 0, /* rightshift */
625 0, /* size (0 = byte, 1 = short, 2 = long) */
626 0, /* bitsize */
b34976b6 627 FALSE, /* pc_relative */
5bd4f169
AM
628 0, /* bitpos */
629 complain_overflow_dont, /* complain_on_overflow */
805fc799 630 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 631 "R_PPC64_JMP_SLOT", /* name */
b34976b6 632 FALSE, /* partial_inplace */
5bd4f169
AM
633 0, /* src_mask */
634 0, /* dst_mask */
b34976b6 635 FALSE), /* pcrel_offset */
5bd4f169
AM
636
637 /* Used only by the dynamic linker. When the object is run, this
638 doubleword64 is set to the load address of the object, plus the
639 addend. */
640 HOWTO (R_PPC64_RELATIVE, /* type */
641 0, /* rightshift */
642 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
643 64, /* bitsize */
b34976b6 644 FALSE, /* pc_relative */
5bd4f169
AM
645 0, /* bitpos */
646 complain_overflow_dont, /* complain_on_overflow */
647 bfd_elf_generic_reloc, /* special_function */
648 "R_PPC64_RELATIVE", /* name */
b34976b6 649 FALSE, /* partial_inplace */
5bd4f169 650 0, /* src_mask */
f5e87a1d 651 ONES (64), /* dst_mask */
b34976b6 652 FALSE), /* pcrel_offset */
5bd4f169
AM
653
654 /* Like R_PPC64_ADDR32, but may be unaligned. */
655 HOWTO (R_PPC64_UADDR32, /* type */
656 0, /* rightshift */
657 2, /* size (0 = byte, 1 = short, 2 = long) */
658 32, /* bitsize */
b34976b6 659 FALSE, /* pc_relative */
5bd4f169
AM
660 0, /* bitpos */
661 complain_overflow_bitfield, /* complain_on_overflow */
662 bfd_elf_generic_reloc, /* special_function */
663 "R_PPC64_UADDR32", /* name */
b34976b6 664 FALSE, /* partial_inplace */
5bd4f169
AM
665 0, /* src_mask */
666 0xffffffff, /* dst_mask */
b34976b6 667 FALSE), /* pcrel_offset */
5bd4f169
AM
668
669 /* Like R_PPC64_ADDR16, but may be unaligned. */
670 HOWTO (R_PPC64_UADDR16, /* type */
671 0, /* rightshift */
672 1, /* size (0 = byte, 1 = short, 2 = long) */
673 16, /* bitsize */
b34976b6 674 FALSE, /* pc_relative */
5bd4f169
AM
675 0, /* bitpos */
676 complain_overflow_bitfield, /* complain_on_overflow */
677 bfd_elf_generic_reloc, /* special_function */
678 "R_PPC64_UADDR16", /* name */
b34976b6 679 FALSE, /* partial_inplace */
5bd4f169
AM
680 0, /* src_mask */
681 0xffff, /* dst_mask */
b34976b6 682 FALSE), /* pcrel_offset */
5bd4f169
AM
683
684 /* 32-bit PC relative. */
685 HOWTO (R_PPC64_REL32, /* type */
686 0, /* rightshift */
687 2, /* size (0 = byte, 1 = short, 2 = long) */
688 32, /* bitsize */
b34976b6 689 TRUE, /* pc_relative */
5bd4f169 690 0, /* bitpos */
5bd4f169
AM
691 complain_overflow_signed, /* complain_on_overflow */
692 bfd_elf_generic_reloc, /* special_function */
693 "R_PPC64_REL32", /* name */
b34976b6 694 FALSE, /* partial_inplace */
5bd4f169
AM
695 0, /* src_mask */
696 0xffffffff, /* dst_mask */
b34976b6 697 TRUE), /* pcrel_offset */
5bd4f169 698
10ed1bba 699 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
700 HOWTO (R_PPC64_PLT32, /* type */
701 0, /* rightshift */
702 2, /* size (0 = byte, 1 = short, 2 = long) */
703 32, /* bitsize */
b34976b6 704 FALSE, /* pc_relative */
5bd4f169
AM
705 0, /* bitpos */
706 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 707 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 708 "R_PPC64_PLT32", /* name */
b34976b6 709 FALSE, /* partial_inplace */
5bd4f169 710 0, /* src_mask */
f5e87a1d 711 0xffffffff, /* dst_mask */
b34976b6 712 FALSE), /* pcrel_offset */
5bd4f169
AM
713
714 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
715 FIXME: R_PPC64_PLTREL32 not supported. */
716 HOWTO (R_PPC64_PLTREL32, /* type */
717 0, /* rightshift */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
719 32, /* bitsize */
b34976b6 720 TRUE, /* pc_relative */
5bd4f169
AM
721 0, /* bitpos */
722 complain_overflow_signed, /* complain_on_overflow */
3ce51288 723 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 724 "R_PPC64_PLTREL32", /* name */
b34976b6 725 FALSE, /* partial_inplace */
5bd4f169 726 0, /* src_mask */
f5e87a1d 727 0xffffffff, /* dst_mask */
b34976b6 728 TRUE), /* pcrel_offset */
5bd4f169
AM
729
730 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
731 the symbol. */
732 HOWTO (R_PPC64_PLT16_LO, /* type */
733 0, /* rightshift */
734 1, /* size (0 = byte, 1 = short, 2 = long) */
735 16, /* bitsize */
b34976b6 736 FALSE, /* pc_relative */
5bd4f169
AM
737 0, /* bitpos */
738 complain_overflow_dont, /* complain_on_overflow */
805fc799 739 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 740 "R_PPC64_PLT16_LO", /* name */
b34976b6 741 FALSE, /* partial_inplace */
5bd4f169
AM
742 0, /* src_mask */
743 0xffff, /* dst_mask */
b34976b6 744 FALSE), /* pcrel_offset */
5bd4f169
AM
745
746 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
747 the symbol. */
748 HOWTO (R_PPC64_PLT16_HI, /* type */
749 16, /* rightshift */
750 1, /* size (0 = byte, 1 = short, 2 = long) */
751 16, /* bitsize */
b34976b6 752 FALSE, /* pc_relative */
5bd4f169 753 0, /* bitpos */
f9c6b907 754 complain_overflow_signed, /* complain_on_overflow */
805fc799 755 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 756 "R_PPC64_PLT16_HI", /* name */
b34976b6 757 FALSE, /* partial_inplace */
5bd4f169
AM
758 0, /* src_mask */
759 0xffff, /* dst_mask */
b34976b6 760 FALSE), /* pcrel_offset */
5bd4f169
AM
761
762 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
763 the symbol. */
764 HOWTO (R_PPC64_PLT16_HA, /* type */
765 16, /* rightshift */
766 1, /* size (0 = byte, 1 = short, 2 = long) */
767 16, /* bitsize */
b34976b6 768 FALSE, /* pc_relative */
5bd4f169 769 0, /* bitpos */
f9c6b907 770 complain_overflow_signed, /* complain_on_overflow */
805fc799 771 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 772 "R_PPC64_PLT16_HA", /* name */
b34976b6 773 FALSE, /* partial_inplace */
5bd4f169
AM
774 0, /* src_mask */
775 0xffff, /* dst_mask */
b34976b6 776 FALSE), /* pcrel_offset */
5bd4f169 777
c061c2d8 778 /* 16-bit section relative relocation. */
5bd4f169
AM
779 HOWTO (R_PPC64_SECTOFF, /* type */
780 0, /* rightshift */
c061c2d8
AM
781 1, /* size (0 = byte, 1 = short, 2 = long) */
782 16, /* bitsize */
b34976b6 783 FALSE, /* pc_relative */
5bd4f169 784 0, /* bitpos */
b80eed39 785 complain_overflow_signed, /* complain_on_overflow */
805fc799 786 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 787 "R_PPC64_SECTOFF", /* name */
b34976b6 788 FALSE, /* partial_inplace */
5bd4f169 789 0, /* src_mask */
c061c2d8 790 0xffff, /* dst_mask */
b34976b6 791 FALSE), /* pcrel_offset */
5bd4f169 792
c061c2d8 793 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
794 HOWTO (R_PPC64_SECTOFF_LO, /* type */
795 0, /* rightshift */
796 1, /* size (0 = byte, 1 = short, 2 = long) */
797 16, /* bitsize */
b34976b6 798 FALSE, /* pc_relative */
5bd4f169
AM
799 0, /* bitpos */
800 complain_overflow_dont, /* complain_on_overflow */
805fc799 801 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 802 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 803 FALSE, /* partial_inplace */
5bd4f169
AM
804 0, /* src_mask */
805 0xffff, /* dst_mask */
b34976b6 806 FALSE), /* pcrel_offset */
5bd4f169
AM
807
808 /* 16-bit upper half section relative relocation. */
809 HOWTO (R_PPC64_SECTOFF_HI, /* type */
810 16, /* rightshift */
811 1, /* size (0 = byte, 1 = short, 2 = long) */
812 16, /* bitsize */
b34976b6 813 FALSE, /* pc_relative */
5bd4f169 814 0, /* bitpos */
f9c6b907 815 complain_overflow_signed, /* complain_on_overflow */
805fc799 816 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 817 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 818 FALSE, /* partial_inplace */
5bd4f169
AM
819 0, /* src_mask */
820 0xffff, /* dst_mask */
b34976b6 821 FALSE), /* pcrel_offset */
5bd4f169
AM
822
823 /* 16-bit upper half adjusted section relative relocation. */
824 HOWTO (R_PPC64_SECTOFF_HA, /* type */
825 16, /* rightshift */
826 1, /* size (0 = byte, 1 = short, 2 = long) */
827 16, /* bitsize */
b34976b6 828 FALSE, /* pc_relative */
5bd4f169 829 0, /* bitpos */
f9c6b907 830 complain_overflow_signed, /* complain_on_overflow */
805fc799 831 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 832 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 833 FALSE, /* partial_inplace */
5bd4f169
AM
834 0, /* src_mask */
835 0xffff, /* dst_mask */
b34976b6 836 FALSE), /* pcrel_offset */
5bd4f169 837
04c9666a
AM
838 /* Like R_PPC64_REL24 without touching the two least significant bits. */
839 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
840 2, /* rightshift */
841 2, /* size (0 = byte, 1 = short, 2 = long) */
842 30, /* bitsize */
b34976b6 843 TRUE, /* pc_relative */
5bd4f169
AM
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
846 bfd_elf_generic_reloc, /* special_function */
04c9666a 847 "R_PPC64_REL30", /* name */
b34976b6 848 FALSE, /* partial_inplace */
d006db6c 849 0, /* src_mask */
5bd4f169 850 0xfffffffc, /* dst_mask */
b34976b6 851 TRUE), /* pcrel_offset */
5bd4f169
AM
852
853 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
854
855 /* A standard 64-bit relocation. */
856 HOWTO (R_PPC64_ADDR64, /* type */
857 0, /* rightshift */
858 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
859 64, /* bitsize */
b34976b6 860 FALSE, /* pc_relative */
5bd4f169
AM
861 0, /* bitpos */
862 complain_overflow_dont, /* complain_on_overflow */
863 bfd_elf_generic_reloc, /* special_function */
864 "R_PPC64_ADDR64", /* name */
b34976b6 865 FALSE, /* partial_inplace */
5bd4f169 866 0, /* src_mask */
f5e87a1d 867 ONES (64), /* dst_mask */
b34976b6 868 FALSE), /* pcrel_offset */
5bd4f169
AM
869
870 /* The bits 32-47 of an address. */
871 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
872 32, /* rightshift */
873 1, /* size (0 = byte, 1 = short, 2 = long) */
874 16, /* bitsize */
b34976b6 875 FALSE, /* pc_relative */
5bd4f169
AM
876 0, /* bitpos */
877 complain_overflow_dont, /* complain_on_overflow */
878 bfd_elf_generic_reloc, /* special_function */
879 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 880 FALSE, /* partial_inplace */
5bd4f169
AM
881 0, /* src_mask */
882 0xffff, /* dst_mask */
b34976b6 883 FALSE), /* pcrel_offset */
5bd4f169
AM
884
885 /* The bits 32-47 of an address, plus 1 if the contents of the low
886 16 bits, treated as a signed number, is negative. */
887 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
888 32, /* rightshift */
889 1, /* size (0 = byte, 1 = short, 2 = long) */
890 16, /* bitsize */
b34976b6 891 FALSE, /* pc_relative */
5bd4f169
AM
892 0, /* bitpos */
893 complain_overflow_dont, /* complain_on_overflow */
805fc799 894 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 895 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 896 FALSE, /* partial_inplace */
5bd4f169
AM
897 0, /* src_mask */
898 0xffff, /* dst_mask */
b34976b6 899 FALSE), /* pcrel_offset */
5bd4f169
AM
900
901 /* The bits 48-63 of an address. */
902 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
903 48, /* rightshift */
904 1, /* size (0 = byte, 1 = short, 2 = long) */
905 16, /* bitsize */
b34976b6 906 FALSE, /* pc_relative */
5bd4f169
AM
907 0, /* bitpos */
908 complain_overflow_dont, /* complain_on_overflow */
909 bfd_elf_generic_reloc, /* special_function */
910 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 911 FALSE, /* partial_inplace */
5bd4f169
AM
912 0, /* src_mask */
913 0xffff, /* dst_mask */
b34976b6 914 FALSE), /* pcrel_offset */
5bd4f169
AM
915
916 /* The bits 48-63 of an address, plus 1 if the contents of the low
917 16 bits, treated as a signed number, is negative. */
918 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
919 48, /* rightshift */
920 1, /* size (0 = byte, 1 = short, 2 = long) */
921 16, /* bitsize */
b34976b6 922 FALSE, /* pc_relative */
5bd4f169
AM
923 0, /* bitpos */
924 complain_overflow_dont, /* complain_on_overflow */
805fc799 925 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 926 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 927 FALSE, /* partial_inplace */
5bd4f169
AM
928 0, /* src_mask */
929 0xffff, /* dst_mask */
b34976b6 930 FALSE), /* pcrel_offset */
5bd4f169
AM
931
932 /* Like ADDR64, but may be unaligned. */
933 HOWTO (R_PPC64_UADDR64, /* type */
934 0, /* rightshift */
935 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
936 64, /* bitsize */
b34976b6 937 FALSE, /* pc_relative */
5bd4f169
AM
938 0, /* bitpos */
939 complain_overflow_dont, /* complain_on_overflow */
940 bfd_elf_generic_reloc, /* special_function */
941 "R_PPC64_UADDR64", /* name */
b34976b6 942 FALSE, /* partial_inplace */
5bd4f169 943 0, /* src_mask */
f5e87a1d 944 ONES (64), /* dst_mask */
b34976b6 945 FALSE), /* pcrel_offset */
5bd4f169
AM
946
947 /* 64-bit relative relocation. */
948 HOWTO (R_PPC64_REL64, /* type */
949 0, /* rightshift */
950 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
951 64, /* bitsize */
b34976b6 952 TRUE, /* pc_relative */
5bd4f169
AM
953 0, /* bitpos */
954 complain_overflow_dont, /* complain_on_overflow */
955 bfd_elf_generic_reloc, /* special_function */
956 "R_PPC64_REL64", /* name */
b34976b6 957 FALSE, /* partial_inplace */
5bd4f169 958 0, /* src_mask */
f5e87a1d 959 ONES (64), /* dst_mask */
b34976b6 960 TRUE), /* pcrel_offset */
5bd4f169 961
cedb70c5 962 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
963 HOWTO (R_PPC64_PLT64, /* type */
964 0, /* rightshift */
965 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
966 64, /* bitsize */
b34976b6 967 FALSE, /* pc_relative */
5bd4f169
AM
968 0, /* bitpos */
969 complain_overflow_dont, /* complain_on_overflow */
805fc799 970 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 971 "R_PPC64_PLT64", /* name */
b34976b6 972 FALSE, /* partial_inplace */
5bd4f169 973 0, /* src_mask */
f5e87a1d 974 ONES (64), /* dst_mask */
b34976b6 975 FALSE), /* pcrel_offset */
5bd4f169
AM
976
977 /* 64-bit PC relative relocation to the symbol's procedure linkage
978 table. */
979 /* FIXME: R_PPC64_PLTREL64 not supported. */
980 HOWTO (R_PPC64_PLTREL64, /* type */
981 0, /* rightshift */
982 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
983 64, /* bitsize */
b34976b6 984 TRUE, /* pc_relative */
5bd4f169
AM
985 0, /* bitpos */
986 complain_overflow_dont, /* complain_on_overflow */
805fc799 987 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 988 "R_PPC64_PLTREL64", /* name */
b34976b6 989 FALSE, /* partial_inplace */
5bd4f169 990 0, /* src_mask */
f5e87a1d 991 ONES (64), /* dst_mask */
b34976b6 992 TRUE), /* pcrel_offset */
5bd4f169
AM
993
994 /* 16 bit TOC-relative relocation. */
995
996 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
997 HOWTO (R_PPC64_TOC16, /* type */
998 0, /* rightshift */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1000 16, /* bitsize */
b34976b6 1001 FALSE, /* pc_relative */
5bd4f169
AM
1002 0, /* bitpos */
1003 complain_overflow_signed, /* complain_on_overflow */
805fc799 1004 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1005 "R_PPC64_TOC16", /* name */
b34976b6 1006 FALSE, /* partial_inplace */
5bd4f169
AM
1007 0, /* src_mask */
1008 0xffff, /* dst_mask */
b34976b6 1009 FALSE), /* pcrel_offset */
5bd4f169
AM
1010
1011 /* 16 bit TOC-relative relocation without overflow. */
1012
1013 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
1014 HOWTO (R_PPC64_TOC16_LO, /* type */
1015 0, /* rightshift */
1016 1, /* size (0 = byte, 1 = short, 2 = long) */
1017 16, /* bitsize */
b34976b6 1018 FALSE, /* pc_relative */
5bd4f169
AM
1019 0, /* bitpos */
1020 complain_overflow_dont, /* complain_on_overflow */
805fc799 1021 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1022 "R_PPC64_TOC16_LO", /* name */
b34976b6 1023 FALSE, /* partial_inplace */
5bd4f169
AM
1024 0, /* src_mask */
1025 0xffff, /* dst_mask */
b34976b6 1026 FALSE), /* pcrel_offset */
5bd4f169
AM
1027
1028 /* 16 bit TOC-relative relocation, high 16 bits. */
1029
1030 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1031 HOWTO (R_PPC64_TOC16_HI, /* type */
1032 16, /* rightshift */
1033 1, /* size (0 = byte, 1 = short, 2 = long) */
1034 16, /* bitsize */
b34976b6 1035 FALSE, /* pc_relative */
5bd4f169 1036 0, /* bitpos */
f9c6b907 1037 complain_overflow_signed, /* complain_on_overflow */
805fc799 1038 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1039 "R_PPC64_TOC16_HI", /* name */
b34976b6 1040 FALSE, /* partial_inplace */
5bd4f169
AM
1041 0, /* src_mask */
1042 0xffff, /* dst_mask */
b34976b6 1043 FALSE), /* pcrel_offset */
5bd4f169
AM
1044
1045 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1046 contents of the low 16 bits, treated as a signed number, is
1047 negative. */
1048
1049 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1050 HOWTO (R_PPC64_TOC16_HA, /* type */
1051 16, /* rightshift */
1052 1, /* size (0 = byte, 1 = short, 2 = long) */
1053 16, /* bitsize */
b34976b6 1054 FALSE, /* pc_relative */
5bd4f169 1055 0, /* bitpos */
f9c6b907 1056 complain_overflow_signed, /* complain_on_overflow */
805fc799 1057 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1058 "R_PPC64_TOC16_HA", /* name */
b34976b6 1059 FALSE, /* partial_inplace */
5bd4f169
AM
1060 0, /* src_mask */
1061 0xffff, /* dst_mask */
b34976b6 1062 FALSE), /* pcrel_offset */
5bd4f169
AM
1063
1064 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1065
1066 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1067 HOWTO (R_PPC64_TOC, /* type */
1068 0, /* rightshift */
1069 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1070 64, /* bitsize */
b34976b6 1071 FALSE, /* pc_relative */
5bd4f169 1072 0, /* bitpos */
b80eed39 1073 complain_overflow_dont, /* complain_on_overflow */
805fc799 1074 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1075 "R_PPC64_TOC", /* name */
b34976b6 1076 FALSE, /* partial_inplace */
5bd4f169 1077 0, /* src_mask */
f5e87a1d 1078 ONES (64), /* dst_mask */
b34976b6 1079 FALSE), /* pcrel_offset */
5bd4f169
AM
1080
1081 /* Like R_PPC64_GOT16, but also informs the link editor that the
1082 value to relocate may (!) refer to a PLT entry which the link
1083 editor (a) may replace with the symbol value. If the link editor
1084 is unable to fully resolve the symbol, it may (b) create a PLT
1085 entry and store the address to the new PLT entry in the GOT.
1086 This permits lazy resolution of function symbols at run time.
1087 The link editor may also skip all of this and just (c) emit a
1088 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1089 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1090 HOWTO (R_PPC64_PLTGOT16, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
b34976b6 1094 FALSE, /* pc_relative */
5bd4f169
AM
1095 0, /* bitpos */
1096 complain_overflow_signed, /* complain_on_overflow */
805fc799 1097 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1098 "R_PPC64_PLTGOT16", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xffff, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_PLTGOT16, but without overflow. */
1105 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1106 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1107 0, /* rightshift */
1108 1, /* size (0 = byte, 1 = short, 2 = long) */
1109 16, /* bitsize */
1110 FALSE, /* pc_relative */
1111 0, /* bitpos */
1112 complain_overflow_dont, /* complain_on_overflow */
1113 ppc64_elf_unhandled_reloc, /* special_function */
1114 "R_PPC64_PLTGOT16_LO", /* name */
1115 FALSE, /* partial_inplace */
1116 0, /* src_mask */
1117 0xffff, /* dst_mask */
1118 FALSE), /* pcrel_offset */
1119
1120 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1121 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1122 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1123 16, /* rightshift */
1124 1, /* size (0 = byte, 1 = short, 2 = long) */
1125 16, /* bitsize */
1126 FALSE, /* pc_relative */
1127 0, /* bitpos */
f9c6b907 1128 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1129 ppc64_elf_unhandled_reloc, /* special_function */
1130 "R_PPC64_PLTGOT16_HI", /* name */
1131 FALSE, /* partial_inplace */
1132 0, /* src_mask */
1133 0xffff, /* dst_mask */
1134 FALSE), /* pcrel_offset */
1135
1136 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1137 1 if the contents of the low 16 bits, treated as a signed number,
1138 is negative. */
1139 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1140 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1141 16, /* rightshift */
1142 1, /* size (0 = byte, 1 = short, 2 = long) */
1143 16, /* bitsize */
1144 FALSE, /* pc_relative */
1145 0, /* bitpos */
f9c6b907 1146 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1147 ppc64_elf_unhandled_reloc, /* special_function */
1148 "R_PPC64_PLTGOT16_HA", /* name */
1149 FALSE, /* partial_inplace */
1150 0, /* src_mask */
1151 0xffff, /* dst_mask */
1152 FALSE), /* pcrel_offset */
1153
1154 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1155 HOWTO (R_PPC64_ADDR16_DS, /* type */
1156 0, /* rightshift */
1157 1, /* size (0 = byte, 1 = short, 2 = long) */
1158 16, /* bitsize */
1159 FALSE, /* pc_relative */
1160 0, /* bitpos */
b80eed39 1161 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1162 bfd_elf_generic_reloc, /* special_function */
1163 "R_PPC64_ADDR16_DS", /* name */
1164 FALSE, /* partial_inplace */
1165 0, /* src_mask */
1166 0xfffc, /* dst_mask */
1167 FALSE), /* pcrel_offset */
1168
1169 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1170 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1171 0, /* rightshift */
1172 1, /* size (0 = byte, 1 = short, 2 = long) */
1173 16, /* bitsize */
1174 FALSE, /* pc_relative */
1175 0, /* bitpos */
1176 complain_overflow_dont,/* complain_on_overflow */
1177 bfd_elf_generic_reloc, /* special_function */
1178 "R_PPC64_ADDR16_LO_DS",/* name */
1179 FALSE, /* partial_inplace */
1180 0, /* src_mask */
1181 0xfffc, /* dst_mask */
1182 FALSE), /* pcrel_offset */
1183
1184 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1185 HOWTO (R_PPC64_GOT16_DS, /* type */
1186 0, /* rightshift */
1187 1, /* size (0 = byte, 1 = short, 2 = long) */
1188 16, /* bitsize */
1189 FALSE, /* pc_relative */
1190 0, /* bitpos */
1191 complain_overflow_signed, /* complain_on_overflow */
1192 ppc64_elf_unhandled_reloc, /* special_function */
1193 "R_PPC64_GOT16_DS", /* name */
1194 FALSE, /* partial_inplace */
1195 0, /* src_mask */
1196 0xfffc, /* dst_mask */
1197 FALSE), /* pcrel_offset */
1198
1199 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1200 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1201 0, /* rightshift */
1202 1, /* size (0 = byte, 1 = short, 2 = long) */
1203 16, /* bitsize */
1204 FALSE, /* pc_relative */
1205 0, /* bitpos */
1206 complain_overflow_dont, /* complain_on_overflow */
1207 ppc64_elf_unhandled_reloc, /* special_function */
1208 "R_PPC64_GOT16_LO_DS", /* name */
1209 FALSE, /* partial_inplace */
1210 0, /* src_mask */
1211 0xfffc, /* dst_mask */
1212 FALSE), /* pcrel_offset */
1213
1214 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1215 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1216 0, /* rightshift */
1217 1, /* size (0 = byte, 1 = short, 2 = long) */
1218 16, /* bitsize */
1219 FALSE, /* pc_relative */
1220 0, /* bitpos */
1221 complain_overflow_dont, /* complain_on_overflow */
1222 ppc64_elf_unhandled_reloc, /* special_function */
1223 "R_PPC64_PLT16_LO_DS", /* name */
1224 FALSE, /* partial_inplace */
1225 0, /* src_mask */
1226 0xfffc, /* dst_mask */
1227 FALSE), /* pcrel_offset */
1228
1229 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1230 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1231 0, /* rightshift */
1232 1, /* size (0 = byte, 1 = short, 2 = long) */
1233 16, /* bitsize */
1234 FALSE, /* pc_relative */
1235 0, /* bitpos */
b80eed39 1236 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1237 ppc64_elf_sectoff_reloc, /* special_function */
1238 "R_PPC64_SECTOFF_DS", /* name */
1239 FALSE, /* partial_inplace */
1240 0, /* src_mask */
1241 0xfffc, /* dst_mask */
1242 FALSE), /* pcrel_offset */
1243
1244 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1245 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1246 0, /* rightshift */
1247 1, /* size (0 = byte, 1 = short, 2 = long) */
1248 16, /* bitsize */
1249 FALSE, /* pc_relative */
1250 0, /* bitpos */
1251 complain_overflow_dont, /* complain_on_overflow */
1252 ppc64_elf_sectoff_reloc, /* special_function */
1253 "R_PPC64_SECTOFF_LO_DS",/* name */
1254 FALSE, /* partial_inplace */
1255 0, /* src_mask */
1256 0xfffc, /* dst_mask */
1257 FALSE), /* pcrel_offset */
1258
1259 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1260 HOWTO (R_PPC64_TOC16_DS, /* type */
1261 0, /* rightshift */
1262 1, /* size (0 = byte, 1 = short, 2 = long) */
1263 16, /* bitsize */
1264 FALSE, /* pc_relative */
1265 0, /* bitpos */
1266 complain_overflow_signed, /* complain_on_overflow */
1267 ppc64_elf_toc_reloc, /* special_function */
1268 "R_PPC64_TOC16_DS", /* name */
1269 FALSE, /* partial_inplace */
1270 0, /* src_mask */
1271 0xfffc, /* dst_mask */
1272 FALSE), /* pcrel_offset */
1273
1274 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1275 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1276 0, /* rightshift */
1277 1, /* size (0 = byte, 1 = short, 2 = long) */
1278 16, /* bitsize */
1279 FALSE, /* pc_relative */
1280 0, /* bitpos */
1281 complain_overflow_dont, /* complain_on_overflow */
1282 ppc64_elf_toc_reloc, /* special_function */
1283 "R_PPC64_TOC16_LO_DS", /* name */
1284 FALSE, /* partial_inplace */
1285 0, /* src_mask */
1286 0xfffc, /* dst_mask */
1287 FALSE), /* pcrel_offset */
1288
1289 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1290 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1291 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1292 0, /* rightshift */
1293 1, /* size (0 = byte, 1 = short, 2 = long) */
1294 16, /* bitsize */
1295 FALSE, /* pc_relative */
1296 0, /* bitpos */
1297 complain_overflow_signed, /* complain_on_overflow */
1298 ppc64_elf_unhandled_reloc, /* special_function */
1299 "R_PPC64_PLTGOT16_DS", /* name */
1300 FALSE, /* partial_inplace */
1301 0, /* src_mask */
1302 0xfffc, /* dst_mask */
1303 FALSE), /* pcrel_offset */
1304
1305 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1306 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1307 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1308 0, /* rightshift */
1309 1, /* size (0 = byte, 1 = short, 2 = long) */
1310 16, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 ppc64_elf_unhandled_reloc, /* special_function */
1315 "R_PPC64_PLTGOT16_LO_DS",/* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0xfffc, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1320
727fc41e 1321 /* Marker relocs for TLS. */
411e1bfb
AM
1322 HOWTO (R_PPC64_TLS,
1323 0, /* rightshift */
1324 2, /* size (0 = byte, 1 = short, 2 = long) */
1325 32, /* bitsize */
1326 FALSE, /* pc_relative */
1327 0, /* bitpos */
1328 complain_overflow_dont, /* complain_on_overflow */
1329 bfd_elf_generic_reloc, /* special_function */
1330 "R_PPC64_TLS", /* name */
1331 FALSE, /* partial_inplace */
1332 0, /* src_mask */
1333 0, /* dst_mask */
1334 FALSE), /* pcrel_offset */
1335
727fc41e
AM
1336 HOWTO (R_PPC64_TLSGD,
1337 0, /* rightshift */
1338 2, /* size (0 = byte, 1 = short, 2 = long) */
1339 32, /* bitsize */
1340 FALSE, /* pc_relative */
1341 0, /* bitpos */
1342 complain_overflow_dont, /* complain_on_overflow */
1343 bfd_elf_generic_reloc, /* special_function */
1344 "R_PPC64_TLSGD", /* name */
1345 FALSE, /* partial_inplace */
1346 0, /* src_mask */
1347 0, /* dst_mask */
1348 FALSE), /* pcrel_offset */
1349
1350 HOWTO (R_PPC64_TLSLD,
1351 0, /* rightshift */
1352 2, /* size (0 = byte, 1 = short, 2 = long) */
1353 32, /* bitsize */
1354 FALSE, /* pc_relative */
1355 0, /* bitpos */
1356 complain_overflow_dont, /* complain_on_overflow */
1357 bfd_elf_generic_reloc, /* special_function */
1358 "R_PPC64_TLSLD", /* name */
1359 FALSE, /* partial_inplace */
1360 0, /* src_mask */
1361 0, /* dst_mask */
1362 FALSE), /* pcrel_offset */
1363
23cedd1d
AM
1364 /* Marker reloc for optimizing r2 save in prologue rather than on
1365 each plt call stub. */
3b421ab3
AM
1366 HOWTO (R_PPC64_TOCSAVE,
1367 0, /* rightshift */
1368 2, /* size (0 = byte, 1 = short, 2 = long) */
1369 32, /* bitsize */
1370 FALSE, /* pc_relative */
1371 0, /* bitpos */
1372 complain_overflow_dont, /* complain_on_overflow */
1373 bfd_elf_generic_reloc, /* special_function */
1374 "R_PPC64_TOCSAVE", /* name */
1375 FALSE, /* partial_inplace */
1376 0, /* src_mask */
1377 0, /* dst_mask */
1378 FALSE), /* pcrel_offset */
1379
23cedd1d
AM
1380 /* Marker relocs on inline plt call instructions. */
1381 HOWTO (R_PPC64_PLTSEQ,
1382 0, /* rightshift */
1383 2, /* size (0 = byte, 1 = short, 2 = long) */
1384 32, /* bitsize */
1385 FALSE, /* pc_relative */
1386 0, /* bitpos */
1387 complain_overflow_dont, /* complain_on_overflow */
1388 bfd_elf_generic_reloc, /* special_function */
1389 "R_PPC64_PLTSEQ", /* name */
1390 FALSE, /* partial_inplace */
1391 0, /* src_mask */
1392 0, /* dst_mask */
1393 FALSE), /* pcrel_offset */
1394
1395 HOWTO (R_PPC64_PLTCALL,
1396 0, /* rightshift */
1397 2, /* size (0 = byte, 1 = short, 2 = long) */
1398 32, /* bitsize */
1399 FALSE, /* pc_relative */
1400 0, /* bitpos */
1401 complain_overflow_dont, /* complain_on_overflow */
1402 bfd_elf_generic_reloc, /* special_function */
1403 "R_PPC64_PLTCALL", /* name */
1404 FALSE, /* partial_inplace */
1405 0, /* src_mask */
1406 0, /* dst_mask */
1407 FALSE), /* pcrel_offset */
1408
411e1bfb
AM
1409 /* Computes the load module index of the load module that contains the
1410 definition of its TLS sym. */
1411 HOWTO (R_PPC64_DTPMOD64,
1412 0, /* rightshift */
1413 4, /* size (0 = byte, 1 = short, 2 = long) */
1414 64, /* bitsize */
1415 FALSE, /* pc_relative */
1416 0, /* bitpos */
1417 complain_overflow_dont, /* complain_on_overflow */
1418 ppc64_elf_unhandled_reloc, /* special_function */
1419 "R_PPC64_DTPMOD64", /* name */
1420 FALSE, /* partial_inplace */
1421 0, /* src_mask */
1422 ONES (64), /* dst_mask */
1423 FALSE), /* pcrel_offset */
1424
1425 /* Computes a dtv-relative displacement, the difference between the value
1426 of sym+add and the base address of the thread-local storage block that
1427 contains the definition of sym, minus 0x8000. */
1428 HOWTO (R_PPC64_DTPREL64,
1429 0, /* rightshift */
1430 4, /* size (0 = byte, 1 = short, 2 = long) */
1431 64, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
1434 complain_overflow_dont, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_DTPREL64", /* name */
1437 FALSE, /* partial_inplace */
1438 0, /* src_mask */
1439 ONES (64), /* dst_mask */
1440 FALSE), /* pcrel_offset */
1441
1442 /* A 16 bit dtprel reloc. */
1443 HOWTO (R_PPC64_DTPREL16,
1444 0, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1446 16, /* bitsize */
1447 FALSE, /* pc_relative */
1448 0, /* bitpos */
1449 complain_overflow_signed, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_DTPREL16", /* name */
1452 FALSE, /* partial_inplace */
1453 0, /* src_mask */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1456
1457 /* Like DTPREL16, but no overflow. */
1458 HOWTO (R_PPC64_DTPREL16_LO,
1459 0, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_DTPREL16_LO", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1471
1472 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_DTPREL16_HI,
1474 16, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1476 16, /* bitsize */
1477 FALSE, /* pc_relative */
1478 0, /* bitpos */
f9c6b907 1479 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_DTPREL16_HI", /* name */
1482 FALSE, /* partial_inplace */
1483 0, /* src_mask */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1486
1487 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_DTPREL16_HA,
1489 16, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
f9c6b907 1494 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_DTPREL16_HA", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1501
1502 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1503 HOWTO (R_PPC64_DTPREL16_HIGHER,
1504 32, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1506 16, /* bitsize */
1507 FALSE, /* pc_relative */
1508 0, /* bitpos */
1509 complain_overflow_dont, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_DTPREL16_HIGHER", /* name */
1512 FALSE, /* partial_inplace */
1513 0, /* src_mask */
1514 0xffff, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1516
1517 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1518 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1519 32, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1521 16, /* bitsize */
1522 FALSE, /* pc_relative */
1523 0, /* bitpos */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_DTPREL16_HIGHERA", /* name */
1527 FALSE, /* partial_inplace */
1528 0, /* src_mask */
1529 0xffff, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1531
1532 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1533 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1534 48, /* rightshift */
1535 1, /* size (0 = byte, 1 = short, 2 = long) */
1536 16, /* bitsize */
1537 FALSE, /* pc_relative */
1538 0, /* bitpos */
1539 complain_overflow_dont, /* complain_on_overflow */
1540 ppc64_elf_unhandled_reloc, /* special_function */
1541 "R_PPC64_DTPREL16_HIGHEST", /* name */
1542 FALSE, /* partial_inplace */
1543 0, /* src_mask */
1544 0xffff, /* dst_mask */
1545 FALSE), /* pcrel_offset */
1546
1547 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1548 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1549 48, /* rightshift */
1550 1, /* size (0 = byte, 1 = short, 2 = long) */
1551 16, /* bitsize */
1552 FALSE, /* pc_relative */
1553 0, /* bitpos */
1554 complain_overflow_dont, /* complain_on_overflow */
1555 ppc64_elf_unhandled_reloc, /* special_function */
1556 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1557 FALSE, /* partial_inplace */
1558 0, /* src_mask */
1559 0xffff, /* dst_mask */
1560 FALSE), /* pcrel_offset */
1561
1562 /* Like DTPREL16, but for insns with a DS field. */
1563 HOWTO (R_PPC64_DTPREL16_DS,
1564 0, /* rightshift */
1565 1, /* size (0 = byte, 1 = short, 2 = long) */
1566 16, /* bitsize */
1567 FALSE, /* pc_relative */
1568 0, /* bitpos */
1569 complain_overflow_signed, /* complain_on_overflow */
1570 ppc64_elf_unhandled_reloc, /* special_function */
1571 "R_PPC64_DTPREL16_DS", /* name */
1572 FALSE, /* partial_inplace */
1573 0, /* src_mask */
1574 0xfffc, /* dst_mask */
1575 FALSE), /* pcrel_offset */
1576
1577 /* Like DTPREL16_DS, but no overflow. */
1578 HOWTO (R_PPC64_DTPREL16_LO_DS,
1579 0, /* rightshift */
1580 1, /* size (0 = byte, 1 = short, 2 = long) */
1581 16, /* bitsize */
1582 FALSE, /* pc_relative */
1583 0, /* bitpos */
1584 complain_overflow_dont, /* complain_on_overflow */
1585 ppc64_elf_unhandled_reloc, /* special_function */
1586 "R_PPC64_DTPREL16_LO_DS", /* name */
1587 FALSE, /* partial_inplace */
1588 0, /* src_mask */
1589 0xfffc, /* dst_mask */
1590 FALSE), /* pcrel_offset */
1591
1592 /* Computes a tp-relative displacement, the difference between the value of
1593 sym+add and the value of the thread pointer (r13). */
1594 HOWTO (R_PPC64_TPREL64,
1595 0, /* rightshift */
1596 4, /* size (0 = byte, 1 = short, 2 = long) */
1597 64, /* bitsize */
1598 FALSE, /* pc_relative */
1599 0, /* bitpos */
1600 complain_overflow_dont, /* complain_on_overflow */
1601 ppc64_elf_unhandled_reloc, /* special_function */
1602 "R_PPC64_TPREL64", /* name */
1603 FALSE, /* partial_inplace */
1604 0, /* src_mask */
1605 ONES (64), /* dst_mask */
1606 FALSE), /* pcrel_offset */
1607
1608 /* A 16 bit tprel reloc. */
1609 HOWTO (R_PPC64_TPREL16,
1610 0, /* rightshift */
1611 1, /* size (0 = byte, 1 = short, 2 = long) */
1612 16, /* bitsize */
1613 FALSE, /* pc_relative */
1614 0, /* bitpos */
1615 complain_overflow_signed, /* complain_on_overflow */
1616 ppc64_elf_unhandled_reloc, /* special_function */
1617 "R_PPC64_TPREL16", /* name */
1618 FALSE, /* partial_inplace */
1619 0, /* src_mask */
1620 0xffff, /* dst_mask */
1621 FALSE), /* pcrel_offset */
1622
1623 /* Like TPREL16, but no overflow. */
1624 HOWTO (R_PPC64_TPREL16_LO,
1625 0, /* rightshift */
1626 1, /* size (0 = byte, 1 = short, 2 = long) */
1627 16, /* bitsize */
1628 FALSE, /* pc_relative */
1629 0, /* bitpos */
1630 complain_overflow_dont, /* complain_on_overflow */
1631 ppc64_elf_unhandled_reloc, /* special_function */
1632 "R_PPC64_TPREL16_LO", /* name */
1633 FALSE, /* partial_inplace */
1634 0, /* src_mask */
1635 0xffff, /* dst_mask */
1636 FALSE), /* pcrel_offset */
1637
1638 /* Like TPREL16_LO, but next higher group of 16 bits. */
1639 HOWTO (R_PPC64_TPREL16_HI,
1640 16, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1642 16, /* bitsize */
1643 FALSE, /* pc_relative */
1644 0, /* bitpos */
f9c6b907 1645 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_TPREL16_HI", /* name */
1648 FALSE, /* partial_inplace */
1649 0, /* src_mask */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1652
1653 /* Like TPREL16_HI, but adjust for low 16 bits. */
1654 HOWTO (R_PPC64_TPREL16_HA,
1655 16, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 16, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
f9c6b907 1660 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_TPREL16_HA", /* name */
1663 FALSE, /* partial_inplace */
1664 0, /* src_mask */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667
1668 /* Like TPREL16_HI, but next higher group of 16 bits. */
1669 HOWTO (R_PPC64_TPREL16_HIGHER,
1670 32, /* rightshift */
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1672 16, /* bitsize */
1673 FALSE, /* pc_relative */
1674 0, /* bitpos */
1675 complain_overflow_dont, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_TPREL16_HIGHER", /* name */
1678 FALSE, /* partial_inplace */
1679 0, /* src_mask */
1680 0xffff, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1682
1683 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1684 HOWTO (R_PPC64_TPREL16_HIGHERA,
1685 32, /* rightshift */
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1687 16, /* bitsize */
1688 FALSE, /* pc_relative */
1689 0, /* bitpos */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_TPREL16_HIGHERA", /* name */
1693 FALSE, /* partial_inplace */
1694 0, /* src_mask */
1695 0xffff, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1697
1698 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1699 HOWTO (R_PPC64_TPREL16_HIGHEST,
1700 48, /* rightshift */
1701 1, /* size (0 = byte, 1 = short, 2 = long) */
1702 16, /* bitsize */
1703 FALSE, /* pc_relative */
1704 0, /* bitpos */
1705 complain_overflow_dont, /* complain_on_overflow */
1706 ppc64_elf_unhandled_reloc, /* special_function */
1707 "R_PPC64_TPREL16_HIGHEST", /* name */
1708 FALSE, /* partial_inplace */
1709 0, /* src_mask */
1710 0xffff, /* dst_mask */
1711 FALSE), /* pcrel_offset */
1712
1713 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1714 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1715 48, /* rightshift */
1716 1, /* size (0 = byte, 1 = short, 2 = long) */
1717 16, /* bitsize */
1718 FALSE, /* pc_relative */
1719 0, /* bitpos */
1720 complain_overflow_dont, /* complain_on_overflow */
1721 ppc64_elf_unhandled_reloc, /* special_function */
1722 "R_PPC64_TPREL16_HIGHESTA", /* name */
1723 FALSE, /* partial_inplace */
1724 0, /* src_mask */
1725 0xffff, /* dst_mask */
1726 FALSE), /* pcrel_offset */
1727
1728 /* Like TPREL16, but for insns with a DS field. */
1729 HOWTO (R_PPC64_TPREL16_DS,
1730 0, /* rightshift */
1731 1, /* size (0 = byte, 1 = short, 2 = long) */
1732 16, /* bitsize */
1733 FALSE, /* pc_relative */
1734 0, /* bitpos */
1735 complain_overflow_signed, /* complain_on_overflow */
1736 ppc64_elf_unhandled_reloc, /* special_function */
1737 "R_PPC64_TPREL16_DS", /* name */
1738 FALSE, /* partial_inplace */
1739 0, /* src_mask */
1740 0xfffc, /* dst_mask */
1741 FALSE), /* pcrel_offset */
1742
1743 /* Like TPREL16_DS, but no overflow. */
1744 HOWTO (R_PPC64_TPREL16_LO_DS,
1745 0, /* rightshift */
1746 1, /* size (0 = byte, 1 = short, 2 = long) */
1747 16, /* bitsize */
1748 FALSE, /* pc_relative */
1749 0, /* bitpos */
1750 complain_overflow_dont, /* complain_on_overflow */
1751 ppc64_elf_unhandled_reloc, /* special_function */
1752 "R_PPC64_TPREL16_LO_DS", /* name */
1753 FALSE, /* partial_inplace */
1754 0, /* src_mask */
1755 0xfffc, /* dst_mask */
1756 FALSE), /* pcrel_offset */
1757
1758 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1759 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1760 to the first entry relative to the TOC base (r2). */
1761 HOWTO (R_PPC64_GOT_TLSGD16,
1762 0, /* rightshift */
1763 1, /* size (0 = byte, 1 = short, 2 = long) */
1764 16, /* bitsize */
1765 FALSE, /* pc_relative */
1766 0, /* bitpos */
1767 complain_overflow_signed, /* complain_on_overflow */
1768 ppc64_elf_unhandled_reloc, /* special_function */
1769 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1770 FALSE, /* partial_inplace */
5bd4f169
AM
1771 0, /* src_mask */
1772 0xffff, /* dst_mask */
b34976b6 1773 FALSE), /* pcrel_offset */
5bd4f169 1774
411e1bfb
AM
1775 /* Like GOT_TLSGD16, but no overflow. */
1776 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1777 0, /* rightshift */
1778 1, /* size (0 = byte, 1 = short, 2 = long) */
1779 16, /* bitsize */
b34976b6 1780 FALSE, /* pc_relative */
5bd4f169
AM
1781 0, /* bitpos */
1782 complain_overflow_dont, /* complain_on_overflow */
805fc799 1783 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1784 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1785 FALSE, /* partial_inplace */
5bd4f169
AM
1786 0, /* src_mask */
1787 0xffff, /* dst_mask */
b34976b6 1788 FALSE), /* pcrel_offset */
5bd4f169 1789
411e1bfb
AM
1790 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1791 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1792 16, /* rightshift */
1793 1, /* size (0 = byte, 1 = short, 2 = long) */
1794 16, /* bitsize */
b34976b6 1795 FALSE, /* pc_relative */
5bd4f169 1796 0, /* bitpos */
f9c6b907 1797 complain_overflow_signed, /* complain_on_overflow */
805fc799 1798 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1799 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1800 FALSE, /* partial_inplace */
5bd4f169
AM
1801 0, /* src_mask */
1802 0xffff, /* dst_mask */
b34976b6 1803 FALSE), /* pcrel_offset */
5bd4f169 1804
411e1bfb
AM
1805 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1806 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1807 16, /* rightshift */
1808 1, /* size (0 = byte, 1 = short, 2 = long) */
1809 16, /* bitsize */
b34976b6 1810 FALSE, /* pc_relative */
5bd4f169 1811 0, /* bitpos */
f9c6b907 1812 complain_overflow_signed, /* complain_on_overflow */
805fc799 1813 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1814 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1815 FALSE, /* partial_inplace */
5bd4f169
AM
1816 0, /* src_mask */
1817 0xffff, /* dst_mask */
b34976b6 1818 FALSE), /* pcrel_offset */
5bd4f169 1819
411e1bfb
AM
1820 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1821 with values (sym+add)@dtpmod and zero, and computes the offset to the
1822 first entry relative to the TOC base (r2). */
1823 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1824 0, /* rightshift */
1825 1, /* size (0 = byte, 1 = short, 2 = long) */
1826 16, /* bitsize */
b34976b6 1827 FALSE, /* pc_relative */
5bd4f169 1828 0, /* bitpos */
411e1bfb
AM
1829 complain_overflow_signed, /* complain_on_overflow */
1830 ppc64_elf_unhandled_reloc, /* special_function */
1831 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1832 FALSE, /* partial_inplace */
d006db6c 1833 0, /* src_mask */
411e1bfb 1834 0xffff, /* dst_mask */
b34976b6 1835 FALSE), /* pcrel_offset */
5bd4f169 1836
411e1bfb
AM
1837 /* Like GOT_TLSLD16, but no overflow. */
1838 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1839 0, /* rightshift */
1840 1, /* size (0 = byte, 1 = short, 2 = long) */
1841 16, /* bitsize */
b34976b6 1842 FALSE, /* pc_relative */
5bd4f169 1843 0, /* bitpos */
411e1bfb
AM
1844 complain_overflow_dont, /* complain_on_overflow */
1845 ppc64_elf_unhandled_reloc, /* special_function */
1846 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1847 FALSE, /* partial_inplace */
d006db6c 1848 0, /* src_mask */
411e1bfb 1849 0xffff, /* dst_mask */
b34976b6 1850 FALSE), /* pcrel_offset */
5bd4f169 1851
411e1bfb
AM
1852 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1853 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1854 16, /* rightshift */
5bd4f169
AM
1855 1, /* size (0 = byte, 1 = short, 2 = long) */
1856 16, /* bitsize */
b34976b6 1857 FALSE, /* pc_relative */
5bd4f169 1858 0, /* bitpos */
f9c6b907 1859 complain_overflow_signed, /* complain_on_overflow */
805fc799 1860 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1861 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1862 FALSE, /* partial_inplace */
d006db6c 1863 0, /* src_mask */
411e1bfb 1864 0xffff, /* dst_mask */
b34976b6 1865 FALSE), /* pcrel_offset */
5bd4f169 1866
411e1bfb
AM
1867 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1868 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1869 16, /* rightshift */
5bd4f169
AM
1870 1, /* size (0 = byte, 1 = short, 2 = long) */
1871 16, /* bitsize */
b34976b6 1872 FALSE, /* pc_relative */
5bd4f169 1873 0, /* bitpos */
f9c6b907 1874 complain_overflow_signed, /* complain_on_overflow */
805fc799 1875 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1876 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1877 FALSE, /* partial_inplace */
d006db6c 1878 0, /* src_mask */
411e1bfb 1879 0xffff, /* dst_mask */
b34976b6 1880 FALSE), /* pcrel_offset */
5bd4f169 1881
411e1bfb
AM
1882 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1883 the offset to the entry relative to the TOC base (r2). */
1884 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1885 0, /* rightshift */
1886 1, /* size (0 = byte, 1 = short, 2 = long) */
1887 16, /* bitsize */
b34976b6 1888 FALSE, /* pc_relative */
5bd4f169 1889 0, /* bitpos */
411e1bfb 1890 complain_overflow_signed, /* complain_on_overflow */
805fc799 1891 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1892 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1893 FALSE, /* partial_inplace */
d006db6c 1894 0, /* src_mask */
5bd4f169 1895 0xfffc, /* dst_mask */
b34976b6 1896 FALSE), /* pcrel_offset */
5bd4f169 1897
411e1bfb
AM
1898 /* Like GOT_DTPREL16_DS, but no overflow. */
1899 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1900 0, /* rightshift */
c061c2d8
AM
1901 1, /* size (0 = byte, 1 = short, 2 = long) */
1902 16, /* bitsize */
b34976b6 1903 FALSE, /* pc_relative */
5bd4f169 1904 0, /* bitpos */
411e1bfb
AM
1905 complain_overflow_dont, /* complain_on_overflow */
1906 ppc64_elf_unhandled_reloc, /* special_function */
1907 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1908 FALSE, /* partial_inplace */
d006db6c 1909 0, /* src_mask */
c061c2d8 1910 0xfffc, /* dst_mask */
b34976b6 1911 FALSE), /* pcrel_offset */
5bd4f169 1912
411e1bfb
AM
1913 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1914 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1915 16, /* rightshift */
5bd4f169
AM
1916 1, /* size (0 = byte, 1 = short, 2 = long) */
1917 16, /* bitsize */
b34976b6 1918 FALSE, /* pc_relative */
5bd4f169 1919 0, /* bitpos */
f9c6b907 1920 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1921 ppc64_elf_unhandled_reloc, /* special_function */
1922 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1923 FALSE, /* partial_inplace */
d006db6c 1924 0, /* src_mask */
411e1bfb 1925 0xffff, /* dst_mask */
b34976b6 1926 FALSE), /* pcrel_offset */
5bd4f169 1927
411e1bfb
AM
1928 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1929 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1930 16, /* rightshift */
1931 1, /* size (0 = byte, 1 = short, 2 = long) */
1932 16, /* bitsize */
1933 FALSE, /* pc_relative */
1934 0, /* bitpos */
f9c6b907 1935 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1936 ppc64_elf_unhandled_reloc, /* special_function */
1937 "R_PPC64_GOT_DTPREL16_HA", /* name */
1938 FALSE, /* partial_inplace */
1939 0, /* src_mask */
1940 0xffff, /* dst_mask */
1941 FALSE), /* pcrel_offset */
1942
1943 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1944 offset to the entry relative to the TOC base (r2). */
1945 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1946 0, /* rightshift */
1947 1, /* size (0 = byte, 1 = short, 2 = long) */
1948 16, /* bitsize */
b34976b6 1949 FALSE, /* pc_relative */
5bd4f169
AM
1950 0, /* bitpos */
1951 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1952 ppc64_elf_unhandled_reloc, /* special_function */
1953 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1954 FALSE, /* partial_inplace */
d006db6c 1955 0, /* src_mask */
ad8e1ba5 1956 0xfffc, /* dst_mask */
b34976b6 1957 FALSE), /* pcrel_offset */
5bd4f169 1958
411e1bfb
AM
1959 /* Like GOT_TPREL16_DS, but no overflow. */
1960 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1961 0, /* rightshift */
1962 1, /* size (0 = byte, 1 = short, 2 = long) */
1963 16, /* bitsize */
b34976b6 1964 FALSE, /* pc_relative */
5bd4f169
AM
1965 0, /* bitpos */
1966 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1967 ppc64_elf_unhandled_reloc, /* special_function */
1968 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1969 FALSE, /* partial_inplace */
d006db6c 1970 0, /* src_mask */
ad8e1ba5 1971 0xfffc, /* dst_mask */
b34976b6 1972 FALSE), /* pcrel_offset */
5bd4f169 1973
411e1bfb
AM
1974 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1975 HOWTO (R_PPC64_GOT_TPREL16_HI,
1976 16, /* rightshift */
5bd4f169
AM
1977 1, /* size (0 = byte, 1 = short, 2 = long) */
1978 16, /* bitsize */
b34976b6 1979 FALSE, /* pc_relative */
5bd4f169 1980 0, /* bitpos */
f9c6b907 1981 complain_overflow_signed, /* complain_on_overflow */
805fc799 1982 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1983 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1984 FALSE, /* partial_inplace */
d006db6c 1985 0, /* src_mask */
411e1bfb 1986 0xffff, /* dst_mask */
b34976b6 1987 FALSE), /* pcrel_offset */
5bd4f169 1988
411e1bfb
AM
1989 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1990 HOWTO (R_PPC64_GOT_TPREL16_HA,
1991 16, /* rightshift */
5bd4f169
AM
1992 1, /* size (0 = byte, 1 = short, 2 = long) */
1993 16, /* bitsize */
b34976b6 1994 FALSE, /* pc_relative */
5bd4f169 1995 0, /* bitpos */
f9c6b907 1996 complain_overflow_signed, /* complain_on_overflow */
805fc799 1997 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1998 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1999 FALSE, /* partial_inplace */
d006db6c 2000 0, /* src_mask */
411e1bfb 2001 0xffff, /* dst_mask */
b34976b6 2002 FALSE), /* pcrel_offset */
5bd4f169 2003
25f23106
AM
2004 HOWTO (R_PPC64_JMP_IREL, /* type */
2005 0, /* rightshift */
2006 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2007 0, /* bitsize */
2008 FALSE, /* pc_relative */
2009 0, /* bitpos */
2010 complain_overflow_dont, /* complain_on_overflow */
2011 ppc64_elf_unhandled_reloc, /* special_function */
2012 "R_PPC64_JMP_IREL", /* name */
2013 FALSE, /* partial_inplace */
2014 0, /* src_mask */
2015 0, /* dst_mask */
2016 FALSE), /* pcrel_offset */
2017
e054468f
AM
2018 HOWTO (R_PPC64_IRELATIVE, /* type */
2019 0, /* rightshift */
2020 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2021 64, /* bitsize */
2022 FALSE, /* pc_relative */
2023 0, /* bitpos */
2024 complain_overflow_dont, /* complain_on_overflow */
2025 bfd_elf_generic_reloc, /* special_function */
2026 "R_PPC64_IRELATIVE", /* name */
2027 FALSE, /* partial_inplace */
2028 0, /* src_mask */
2029 ONES (64), /* dst_mask */
2030 FALSE), /* pcrel_offset */
2031
25f23106
AM
2032 /* A 16 bit relative relocation. */
2033 HOWTO (R_PPC64_REL16, /* type */
2034 0, /* rightshift */
2035 1, /* size (0 = byte, 1 = short, 2 = long) */
2036 16, /* bitsize */
2037 TRUE, /* pc_relative */
2038 0, /* bitpos */
b80eed39 2039 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2040 bfd_elf_generic_reloc, /* special_function */
2041 "R_PPC64_REL16", /* name */
2042 FALSE, /* partial_inplace */
2043 0, /* src_mask */
2044 0xffff, /* dst_mask */
2045 TRUE), /* pcrel_offset */
2046
2047 /* A 16 bit relative relocation without overflow. */
2048 HOWTO (R_PPC64_REL16_LO, /* type */
2049 0, /* rightshift */
2050 1, /* size (0 = byte, 1 = short, 2 = long) */
2051 16, /* bitsize */
2052 TRUE, /* pc_relative */
2053 0, /* bitpos */
2054 complain_overflow_dont,/* complain_on_overflow */
2055 bfd_elf_generic_reloc, /* special_function */
2056 "R_PPC64_REL16_LO", /* name */
2057 FALSE, /* partial_inplace */
2058 0, /* src_mask */
2059 0xffff, /* dst_mask */
2060 TRUE), /* pcrel_offset */
2061
2062 /* The high order 16 bits of a relative address. */
2063 HOWTO (R_PPC64_REL16_HI, /* type */
2064 16, /* rightshift */
2065 1, /* size (0 = byte, 1 = short, 2 = long) */
2066 16, /* bitsize */
2067 TRUE, /* pc_relative */
2068 0, /* bitpos */
f9c6b907 2069 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2070 bfd_elf_generic_reloc, /* special_function */
2071 "R_PPC64_REL16_HI", /* name */
2072 FALSE, /* partial_inplace */
2073 0, /* src_mask */
2074 0xffff, /* dst_mask */
2075 TRUE), /* pcrel_offset */
2076
2077 /* The high order 16 bits of a relative address, plus 1 if the contents of
2078 the low 16 bits, treated as a signed number, is negative. */
2079 HOWTO (R_PPC64_REL16_HA, /* type */
2080 16, /* rightshift */
2081 1, /* size (0 = byte, 1 = short, 2 = long) */
2082 16, /* bitsize */
2083 TRUE, /* pc_relative */
2084 0, /* bitpos */
f9c6b907 2085 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2086 ppc64_elf_ha_reloc, /* special_function */
2087 "R_PPC64_REL16_HA", /* name */
2088 FALSE, /* partial_inplace */
2089 0, /* src_mask */
2090 0xffff, /* dst_mask */
2091 TRUE), /* pcrel_offset */
2092
a680de9a
PB
2093 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2094 HOWTO (R_PPC64_REL16DX_HA, /* type */
2095 16, /* rightshift */
2096 2, /* size (0 = byte, 1 = short, 2 = long) */
2097 16, /* bitsize */
2098 TRUE, /* pc_relative */
2099 0, /* bitpos */
2100 complain_overflow_signed, /* complain_on_overflow */
2101 ppc64_elf_ha_reloc, /* special_function */
2102 "R_PPC64_REL16DX_HA", /* name */
2103 FALSE, /* partial_inplace */
2104 0, /* src_mask */
2105 0x1fffc1, /* dst_mask */
2106 TRUE), /* pcrel_offset */
2107
7ba71655
AM
2108 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2109 HOWTO (R_PPC64_16DX_HA, /* type */
2110 16, /* rightshift */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2112 16, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_signed, /* complain_on_overflow */
2116 ppc64_elf_ha_reloc, /* special_function */
2117 "R_PPC64_16DX_HA", /* name */
2118 FALSE, /* partial_inplace */
2119 0, /* src_mask */
2120 0x1fffc1, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122
f9c6b907
AM
2123 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2124 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2125 16, /* rightshift */
2126 1, /* size (0 = byte, 1 = short, 2 = long) */
2127 16, /* bitsize */
2128 FALSE, /* pc_relative */
2129 0, /* bitpos */
2130 complain_overflow_dont, /* complain_on_overflow */
2131 bfd_elf_generic_reloc, /* special_function */
2132 "R_PPC64_ADDR16_HIGH", /* name */
2133 FALSE, /* partial_inplace */
2134 0, /* src_mask */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137
2138 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2139 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2140 16, /* rightshift */
2141 1, /* size (0 = byte, 1 = short, 2 = long) */
2142 16, /* bitsize */
2143 FALSE, /* pc_relative */
2144 0, /* bitpos */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 ppc64_elf_ha_reloc, /* special_function */
2147 "R_PPC64_ADDR16_HIGHA", /* name */
2148 FALSE, /* partial_inplace */
2149 0, /* src_mask */
2150 0xffff, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2152
2153 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2154 HOWTO (R_PPC64_DTPREL16_HIGH,
2155 16, /* rightshift */
2156 1, /* size (0 = byte, 1 = short, 2 = long) */
2157 16, /* bitsize */
2158 FALSE, /* pc_relative */
2159 0, /* bitpos */
2160 complain_overflow_dont, /* complain_on_overflow */
2161 ppc64_elf_unhandled_reloc, /* special_function */
2162 "R_PPC64_DTPREL16_HIGH", /* name */
2163 FALSE, /* partial_inplace */
2164 0, /* src_mask */
2165 0xffff, /* dst_mask */
2166 FALSE), /* pcrel_offset */
2167
2168 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2169 HOWTO (R_PPC64_DTPREL16_HIGHA,
2170 16, /* rightshift */
2171 1, /* size (0 = byte, 1 = short, 2 = long) */
2172 16, /* bitsize */
2173 FALSE, /* pc_relative */
2174 0, /* bitpos */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 ppc64_elf_unhandled_reloc, /* special_function */
2177 "R_PPC64_DTPREL16_HIGHA", /* name */
2178 FALSE, /* partial_inplace */
2179 0, /* src_mask */
2180 0xffff, /* dst_mask */
2181 FALSE), /* pcrel_offset */
2182
2183 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2184 HOWTO (R_PPC64_TPREL16_HIGH,
2185 16, /* rightshift */
2186 1, /* size (0 = byte, 1 = short, 2 = long) */
2187 16, /* bitsize */
2188 FALSE, /* pc_relative */
2189 0, /* bitpos */
2190 complain_overflow_dont, /* complain_on_overflow */
2191 ppc64_elf_unhandled_reloc, /* special_function */
2192 "R_PPC64_TPREL16_HIGH", /* name */
2193 FALSE, /* partial_inplace */
2194 0, /* src_mask */
2195 0xffff, /* dst_mask */
2196 FALSE), /* pcrel_offset */
2197
2198 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2199 HOWTO (R_PPC64_TPREL16_HIGHA,
2200 16, /* rightshift */
2201 1, /* size (0 = byte, 1 = short, 2 = long) */
2202 16, /* bitsize */
2203 FALSE, /* pc_relative */
2204 0, /* bitpos */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 ppc64_elf_unhandled_reloc, /* special_function */
2207 "R_PPC64_TPREL16_HIGHA", /* name */
2208 FALSE, /* partial_inplace */
2209 0, /* src_mask */
2210 0xffff, /* dst_mask */
2211 FALSE), /* pcrel_offset */
2212
006589cf
AM
2213 /* Marker reloc on ELFv2 large-model function entry. */
2214 HOWTO (R_PPC64_ENTRY,
2215 0, /* rightshift */
2216 2, /* size (0 = byte, 1 = short, 2 = long) */
2217 32, /* bitsize */
2218 FALSE, /* pc_relative */
2219 0, /* bitpos */
2220 complain_overflow_dont, /* complain_on_overflow */
2221 bfd_elf_generic_reloc, /* special_function */
2222 "R_PPC64_ENTRY", /* name */
2223 FALSE, /* partial_inplace */
2224 0, /* src_mask */
2225 0, /* dst_mask */
2226 FALSE), /* pcrel_offset */
2227
45965137
AM
2228 /* Like ADDR64, but use local entry point of function. */
2229 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2230 0, /* rightshift */
2231 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2232 64, /* bitsize */
2233 FALSE, /* pc_relative */
2234 0, /* bitpos */
2235 complain_overflow_dont, /* complain_on_overflow */
2236 bfd_elf_generic_reloc, /* special_function */
2237 "R_PPC64_ADDR64_LOCAL", /* name */
2238 FALSE, /* partial_inplace */
2239 0, /* src_mask */
2240 ONES (64), /* dst_mask */
2241 FALSE), /* pcrel_offset */
2242
5bd4f169
AM
2243 /* GNU extension to record C++ vtable hierarchy. */
2244 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2245 0, /* rightshift */
2246 0, /* size (0 = byte, 1 = short, 2 = long) */
2247 0, /* bitsize */
b34976b6 2248 FALSE, /* pc_relative */
5bd4f169
AM
2249 0, /* bitpos */
2250 complain_overflow_dont, /* complain_on_overflow */
2251 NULL, /* special_function */
2252 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2253 FALSE, /* partial_inplace */
5bd4f169
AM
2254 0, /* src_mask */
2255 0, /* dst_mask */
b34976b6 2256 FALSE), /* pcrel_offset */
5bd4f169
AM
2257
2258 /* GNU extension to record C++ vtable member usage. */
2259 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2260 0, /* rightshift */
2261 0, /* size (0 = byte, 1 = short, 2 = long) */
2262 0, /* bitsize */
b34976b6 2263 FALSE, /* pc_relative */
5bd4f169
AM
2264 0, /* bitpos */
2265 complain_overflow_dont, /* complain_on_overflow */
2266 NULL, /* special_function */
2267 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2268 FALSE, /* partial_inplace */
5bd4f169
AM
2269 0, /* src_mask */
2270 0, /* dst_mask */
b34976b6 2271 FALSE), /* pcrel_offset */
5bd4f169
AM
2272};
2273
2274\f
2275/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2276 be done. */
2277
2278static void
4ce794b7 2279ppc_howto_init (void)
5bd4f169
AM
2280{
2281 unsigned int i, type;
2282
a4b6fadd 2283 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2284 {
2285 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2286 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2287 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2288 }
2289}
2290
2291static reloc_howto_type *
f3185997 2292ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 2293 bfd_reloc_code_real_type code)
5bd4f169 2294{
411e1bfb 2295 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2296
2297 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2298 /* Initialize howto table if needed. */
2299 ppc_howto_init ();
2300
4ce794b7 2301 switch (code)
5bd4f169
AM
2302 {
2303 default:
f3185997 2304 /* xgettext:c-format */
e8f5af78 2305 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
f3185997 2306 bfd_set_error (bfd_error_bad_value);
4ce794b7 2307 return NULL;
5bd4f169 2308
411e1bfb
AM
2309 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2310 break;
2311 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2312 break;
2313 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2314 break;
2315 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2316 break;
2317 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2318 break;
2319 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2320 break;
f9c6b907
AM
2321 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2322 break;
411e1bfb 2323 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2324 break;
f9c6b907
AM
2325 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2326 break;
411e1bfb 2327 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2328 break;
411e1bfb 2329 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2330 break;
411e1bfb 2331 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2332 break;
411e1bfb 2333 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2334 break;
05d0e962
AM
2335 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
2336 break;
411e1bfb 2337 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2338 break;
411e1bfb 2339 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2340 break;
411e1bfb 2341 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2342 break;
411e1bfb 2343 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2344 break;
411e1bfb 2345 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2346 break;
411e1bfb 2347 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2348 break;
411e1bfb 2349 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2350 break;
411e1bfb 2351 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2352 break;
411e1bfb 2353 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2354 break;
411e1bfb 2355 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2356 break;
411e1bfb 2357 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2358 break;
411e1bfb 2359 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2360 break;
411e1bfb 2361 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2362 break;
411e1bfb 2363 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2364 break;
411e1bfb 2365 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2366 break;
411e1bfb 2367 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2368 break;
411e1bfb 2369 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2370 break;
411e1bfb 2371 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2372 break;
411e1bfb 2373 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2374 break;
411e1bfb 2375 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2376 break;
411e1bfb 2377 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2378 break;
411e1bfb 2379 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2380 break;
411e1bfb 2381 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2382 break;
411e1bfb 2383 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2384 break;
411e1bfb 2385 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2386 break;
411e1bfb 2387 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2388 break;
411e1bfb 2389 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2390 break;
411e1bfb 2391 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2392 break;
411e1bfb 2393 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2394 break;
411e1bfb 2395 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2396 break;
411e1bfb 2397 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2398 break;
411e1bfb 2399 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2400 break;
411e1bfb 2401 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2402 break;
411e1bfb 2403 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2404 break;
411e1bfb 2405 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2406 break;
411e1bfb 2407 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2408 break;
411e1bfb 2409 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2410 break;
411e1bfb 2411 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2412 break;
411e1bfb 2413 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2414 break;
411e1bfb 2415 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2416 break;
411e1bfb 2417 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2418 break;
411e1bfb 2419 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2420 break;
411e1bfb 2421 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2422 break;
411e1bfb 2423 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2424 break;
411e1bfb 2425 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2426 break;
411e1bfb 2427 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2428 break;
411e1bfb 2429 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2430 break;
411e1bfb 2431 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2432 break;
411e1bfb 2433 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2434 break;
727fc41e
AM
2435 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2436 break;
2437 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2438 break;
411e1bfb 2439 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2440 break;
411e1bfb 2441 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2442 break;
411e1bfb 2443 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2444 break;
411e1bfb 2445 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2446 break;
f9c6b907
AM
2447 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2448 break;
411e1bfb 2449 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2450 break;
f9c6b907
AM
2451 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2452 break;
411e1bfb 2453 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2454 break;
411e1bfb
AM
2455 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2456 break;
2457 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2458 break;
2459 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2460 break;
f9c6b907
AM
2461 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2462 break;
411e1bfb
AM
2463 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2464 break;
f9c6b907
AM
2465 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2466 break;
411e1bfb
AM
2467 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2468 break;
2469 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2470 break;
2471 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2472 break;
2473 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2474 break;
2475 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2476 break;
2477 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2478 break;
2479 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2480 break;
2481 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2482 break;
2483 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2484 break;
2485 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2486 break;
2487 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2488 break;
2489 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2490 break;
2491 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2492 break;
2493 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2494 break;
2495 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2496 break;
2497 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2498 break;
2499 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2500 break;
2501 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2502 break;
2503 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2504 break;
2505 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2506 break;
2507 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2508 break;
2509 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2510 break;
2511 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2512 break;
2513 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2514 break;
2515 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2516 break;
2517 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2518 break;
2519 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2520 break;
2521 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2522 break;
2523 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2524 break;
25f23106
AM
2525 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2526 break;
2527 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2528 break;
2529 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2530 break;
2531 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2532 break;
7ba71655
AM
2533 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2534 break;
a680de9a
PB
2535 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2536 break;
006589cf
AM
2537 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2538 break;
45965137
AM
2539 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2540 break;
411e1bfb
AM
2541 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2542 break;
2543 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2544 break;
2545 }
2546
4ce794b7 2547 return ppc64_elf_howto_table[r];
5bd4f169
AM
2548};
2549
157090f7
AM
2550static reloc_howto_type *
2551ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2552 const char *r_name)
2553{
2554 unsigned int i;
2555
a4b6fadd 2556 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2557 if (ppc64_elf_howto_raw[i].name != NULL
2558 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2559 return &ppc64_elf_howto_raw[i];
2560
f3185997 2561
157090f7
AM
2562 return NULL;
2563}
2564
5bd4f169
AM
2565/* Set the howto pointer for a PowerPC ELF reloc. */
2566
f3185997 2567static bfd_boolean
4aef7643 2568ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 2569 Elf_Internal_Rela *dst)
5bd4f169 2570{
65f38f15
AM
2571 unsigned int type;
2572
ef60b7ff 2573 /* Initialize howto table if needed. */
5bd4f169 2574 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2575 ppc_howto_init ();
2576
65f38f15 2577 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2578 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2579 {
695344c0 2580 /* xgettext:c-format */
0aa13fee 2581 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 2582 abfd, type);
f3185997
NC
2583 bfd_set_error (bfd_error_bad_value);
2584 return FALSE;
d0fb9a8d 2585 }
65f38f15 2586 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
2587 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2588 {
2589 /* xgettext:c-format */
2590 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2591 abfd, type);
2592 bfd_set_error (bfd_error_bad_value);
2593 return FALSE;
2594 }
2595
2596 return TRUE;
5bd4f169
AM
2597}
2598
04c9666a 2599/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2600
2601static bfd_reloc_status_type
4ce794b7
AM
2602ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2603 void *data, asection *input_section,
2604 bfd *output_bfd, char **error_message)
5bd4f169 2605{
a680de9a
PB
2606 enum elf_ppc64_reloc_type r_type;
2607 long insn;
2608 bfd_size_type octets;
3de43e7b 2609 bfd_vma value;
a680de9a 2610
805fc799
AM
2611 /* If this is a relocatable link (output_bfd test tells us), just
2612 call the generic function. Any adjustment will be done at final
2613 link time. */
2614 if (output_bfd != NULL)
cedb70c5 2615 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2616 input_section, output_bfd, error_message);
2617
2618 /* Adjust the addend for sign extension of the low 16 bits.
2619 We won't actually be using the low 16 bits, so trashing them
2620 doesn't matter. */
2621 reloc_entry->addend += 0x8000;
a680de9a
PB
2622 r_type = reloc_entry->howto->type;
2623 if (r_type != R_PPC64_REL16DX_HA)
2624 return bfd_reloc_continue;
2625
2626 value = 0;
2627 if (!bfd_is_com_section (symbol->section))
2628 value = symbol->value;
2629 value += (reloc_entry->addend
2630 + symbol->section->output_offset
2631 + symbol->section->output_section->vma);
2632 value -= (reloc_entry->address
2633 + input_section->output_offset
2634 + input_section->output_section->vma);
3de43e7b 2635 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
2636
2637 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2638 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2639 insn &= ~0x1fffc1;
3de43e7b 2640 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 2641 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 2642 if (value + 0x8000 > 0xffff)
a680de9a
PB
2643 return bfd_reloc_overflow;
2644 return bfd_reloc_ok;
805fc799 2645}
5bd4f169 2646
2441e016
AM
2647static bfd_reloc_status_type
2648ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2649 void *data, asection *input_section,
2650 bfd *output_bfd, char **error_message)
2651{
2652 if (output_bfd != NULL)
2653 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2654 input_section, output_bfd, error_message);
2655
699733f6
AM
2656 if (strcmp (symbol->section->name, ".opd") == 0
2657 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2658 {
2659 bfd_vma dest = opd_entry_value (symbol->section,
2660 symbol->value + reloc_entry->addend,
aef36ac1 2661 NULL, NULL, FALSE);
2441e016
AM
2662 if (dest != (bfd_vma) -1)
2663 reloc_entry->addend = dest - (symbol->value
2664 + symbol->section->output_section->vma
2665 + symbol->section->output_offset);
2666 }
810d4e75
AM
2667 else
2668 {
2669 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2670
2671 if (symbol->section->owner != abfd
9f284bf9 2672 && symbol->section->owner != NULL
810d4e75
AM
2673 && abiversion (symbol->section->owner) >= 2)
2674 {
2675 unsigned int i;
2676
2677 for (i = 0; i < symbol->section->owner->symcount; ++i)
2678 {
2679 asymbol *symdef = symbol->section->owner->outsymbols[i];
2680
2681 if (strcmp (symdef->name, symbol->name) == 0)
2682 {
2683 elfsym = (elf_symbol_type *) symdef;
2684 break;
2685 }
2686 }
2687 }
2688 reloc_entry->addend
2689 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2690 }
2441e016
AM
2691 return bfd_reloc_continue;
2692}
2693
805fc799 2694static bfd_reloc_status_type
4ce794b7
AM
2695ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2696 void *data, asection *input_section,
2697 bfd *output_bfd, char **error_message)
805fc799
AM
2698{
2699 long insn;
04c9666a 2700 enum elf_ppc64_reloc_type r_type;
805fc799 2701 bfd_size_type octets;
794e51c0
AM
2702 /* Assume 'at' branch hints. */
2703 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2704
2705 /* If this is a relocatable link (output_bfd test tells us), just
2706 call the generic function. Any adjustment will be done at final
2707 link time. */
5bd4f169 2708 if (output_bfd != NULL)
cedb70c5 2709 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2710 input_section, output_bfd, error_message);
2711
2712 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2713 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2714 insn &= ~(0x01 << 21);
4ce794b7 2715 r_type = reloc_entry->howto->type;
805fc799
AM
2716 if (r_type == R_PPC64_ADDR14_BRTAKEN
2717 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2718 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2719
794e51c0 2720 if (is_isa_v2)
5bd4f169 2721 {
805fc799
AM
2722 /* Set 'a' bit. This is 0b00010 in BO field for branch
2723 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2724 for branch on CTR insns (BO == 1a00t or 1a01t). */
2725 if ((insn & (0x14 << 21)) == (0x04 << 21))
2726 insn |= 0x02 << 21;
2727 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2728 insn |= 0x08 << 21;
2729 else
2441e016 2730 goto out;
5bd4f169 2731 }
805fc799
AM
2732 else
2733 {
2734 bfd_vma target = 0;
2735 bfd_vma from;
5bd4f169 2736
805fc799
AM
2737 if (!bfd_is_com_section (symbol->section))
2738 target = symbol->value;
2739 target += symbol->section->output_section->vma;
2740 target += symbol->section->output_offset;
2741 target += reloc_entry->addend;
5bd4f169 2742
805fc799
AM
2743 from = (reloc_entry->address
2744 + input_section->output_offset
2745 + input_section->output_section->vma);
5bd4f169 2746
805fc799
AM
2747 /* Invert 'y' bit if not the default. */
2748 if ((bfd_signed_vma) (target - from) < 0)
2749 insn ^= 0x01 << 21;
2750 }
4ce794b7 2751 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2752 out:
2753 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2754 input_section, output_bfd, error_message);
805fc799 2755}
5bd4f169 2756
805fc799 2757static bfd_reloc_status_type
4ce794b7
AM
2758ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2759 void *data, asection *input_section,
2760 bfd *output_bfd, char **error_message)
805fc799
AM
2761{
2762 /* If this is a relocatable link (output_bfd test tells us), just
2763 call the generic function. Any adjustment will be done at final
2764 link time. */
2765 if (output_bfd != NULL)
cedb70c5 2766 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2767 input_section, output_bfd, error_message);
5bd4f169 2768
805fc799
AM
2769 /* Subtract the symbol section base address. */
2770 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2771 return bfd_reloc_continue;
2772}
2773
805fc799 2774static bfd_reloc_status_type
4ce794b7
AM
2775ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2776 void *data, asection *input_section,
2777 bfd *output_bfd, char **error_message)
805fc799
AM
2778{
2779 /* If this is a relocatable link (output_bfd test tells us), just
2780 call the generic function. Any adjustment will be done at final
2781 link time. */
2782 if (output_bfd != NULL)
cedb70c5 2783 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2784 input_section, output_bfd, error_message);
2785
2786 /* Subtract the symbol section base address. */
2787 reloc_entry->addend -= symbol->section->output_section->vma;
2788
2789 /* Adjust the addend for sign extension of the low 16 bits. */
2790 reloc_entry->addend += 0x8000;
2791 return bfd_reloc_continue;
2792}
2793
2794static bfd_reloc_status_type
4ce794b7
AM
2795ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2796 void *data, asection *input_section,
2797 bfd *output_bfd, char **error_message)
805fc799
AM
2798{
2799 bfd_vma TOCstart;
2800
2801 /* If this is a relocatable link (output_bfd test tells us), just
2802 call the generic function. Any adjustment will be done at final
2803 link time. */
2804 if (output_bfd != NULL)
cedb70c5 2805 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2806 input_section, output_bfd, error_message);
2807
2808 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2809 if (TOCstart == 0)
1c865ab2 2810 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2811
2812 /* Subtract the TOC base address. */
2813 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2814 return bfd_reloc_continue;
2815}
2816
2817static bfd_reloc_status_type
4ce794b7
AM
2818ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2819 void *data, asection *input_section,
2820 bfd *output_bfd, char **error_message)
805fc799
AM
2821{
2822 bfd_vma TOCstart;
2823
2824 /* If this is a relocatable link (output_bfd test tells us), just
2825 call the generic function. Any adjustment will be done at final
2826 link time. */
2827 if (output_bfd != NULL)
cedb70c5 2828 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2829 input_section, output_bfd, error_message);
2830
2831 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2832 if (TOCstart == 0)
1c865ab2 2833 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2834
2835 /* Subtract the TOC base address. */
2836 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2837
2838 /* Adjust the addend for sign extension of the low 16 bits. */
2839 reloc_entry->addend += 0x8000;
2840 return bfd_reloc_continue;
2841}
2842
2843static bfd_reloc_status_type
4ce794b7
AM
2844ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2845 void *data, asection *input_section,
2846 bfd *output_bfd, char **error_message)
805fc799
AM
2847{
2848 bfd_vma TOCstart;
2849 bfd_size_type octets;
2850
2851 /* If this is a relocatable link (output_bfd test tells us), just
2852 call the generic function. Any adjustment will be done at final
2853 link time. */
2854 if (output_bfd != NULL)
cedb70c5 2855 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2856 input_section, output_bfd, error_message);
2857
2858 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2859 if (TOCstart == 0)
1c865ab2 2860 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2861
2862 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2863 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2864 return bfd_reloc_ok;
2865}
2866
2867static bfd_reloc_status_type
4ce794b7
AM
2868ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2869 void *data, asection *input_section,
2870 bfd *output_bfd, char **error_message)
805fc799
AM
2871{
2872 /* If this is a relocatable link (output_bfd test tells us), just
2873 call the generic function. Any adjustment will be done at final
2874 link time. */
2875 if (output_bfd != NULL)
cedb70c5 2876 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2877 input_section, output_bfd, error_message);
2878
2879 if (error_message != NULL)
2880 {
2881 static char buf[60];
2882 sprintf (buf, "generic linker can't handle %s",
2883 reloc_entry->howto->name);
2884 *error_message = buf;
2885 }
2886 return bfd_reloc_dangerous;
2887}
2888
927be08e
AM
2889/* Track GOT entries needed for a given symbol. We might need more
2890 than one got entry per symbol. */
2891struct got_entry
2892{
2893 struct got_entry *next;
2894
2895 /* The symbol addend that we'll be placing in the GOT. */
2896 bfd_vma addend;
2897
2898 /* Unlike other ELF targets, we use separate GOT entries for the same
2899 symbol referenced from different input files. This is to support
2900 automatic multiple TOC/GOT sections, where the TOC base can vary
2901 from one input file to another. After partitioning into TOC groups
2902 we merge entries within the group.
2903
2904 Point to the BFD owning this GOT entry. */
2905 bfd *owner;
2906
2907 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2908 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2909 unsigned char tls_type;
927be08e
AM
2910
2911 /* Non-zero if got.ent points to real entry. */
f961d9dd 2912 unsigned char is_indirect;
927be08e
AM
2913
2914 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2915 union
2916 {
2917 bfd_signed_vma refcount;
2918 bfd_vma offset;
2919 struct got_entry *ent;
2920 } got;
2921};
2922
2923/* The same for PLT. */
2924struct plt_entry
2925{
2926 struct plt_entry *next;
2927
2928 bfd_vma addend;
2929
2930 union
2931 {
2932 bfd_signed_vma refcount;
2933 bfd_vma offset;
2934 } plt;
2935};
2936
e717da7e
AM
2937struct ppc64_elf_obj_tdata
2938{
2939 struct elf_obj_tdata elf;
2940
2941 /* Shortcuts to dynamic linker sections. */
2942 asection *got;
2943 asection *relgot;
2944
b3fac117
AM
2945 /* Used during garbage collection. We attach global symbols defined
2946 on removed .opd entries to this section so that the sym is removed. */
2947 asection *deleted_section;
81688140 2948
927be08e 2949 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2950 sections means we potentially need one of these for each input bfd. */
927be08e 2951 struct got_entry tlsld_got;
8860955f 2952
729eabd5
AM
2953 union {
2954 /* A copy of relocs before they are modified for --emit-relocs. */
2955 Elf_Internal_Rela *relocs;
2956
2957 /* Section contents. */
2958 bfd_byte *contents;
2959 } opd;
d77c8a4b
AM
2960
2961 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2962 the reloc to be in the range -32768 to 32767. */
98528052
AM
2963 unsigned int has_small_toc_reloc : 1;
2964
560c8763
AM
2965 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2966 instruction not one we handle. */
2967 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2968};
2969
2970#define ppc64_elf_tdata(bfd) \
2971 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2972
2973#define ppc64_tlsld_got(bfd) \
2974 (&ppc64_elf_tdata (bfd)->tlsld_got)
2975
0c8d6e5c
AM
2976#define is_ppc64_elf(bfd) \
2977 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2978 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2979
e717da7e
AM
2980/* Override the generic function because we store some extras. */
2981
2982static bfd_boolean
2983ppc64_elf_mkobject (bfd *abfd)
2984{
0ffa91dd 2985 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2986 PPC64_ELF_DATA);
e717da7e
AM
2987}
2988
feee612b 2989/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2990 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2991
b34976b6 2992static bfd_boolean
4ce794b7 2993ppc64_elf_object_p (bfd *abfd)
feee612b 2994{
14b57c7c
AM
2995 if (!abfd->arch_info->the_default)
2996 return TRUE;
2997
2998 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2999 {
3000 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
3001
3002 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
3003 {
3004 /* Relies on arch after 32 bit default being 64 bit default. */
3005 abfd->arch_info = abfd->arch_info->next;
3006 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
3007 }
3008 }
14b57c7c 3009 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
3010}
3011
d37c89e5
AM
3012/* Support for core dump NOTE sections. */
3013
3014static bfd_boolean
3015ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3016{
eea6121a 3017 size_t offset, size;
d37c89e5
AM
3018
3019 if (note->descsz != 504)
3020 return FALSE;
3021
3022 /* pr_cursig */
228e534f 3023 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
3024
3025 /* pr_pid */
228e534f 3026 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
3027
3028 /* pr_reg */
3029 offset = 112;
eea6121a 3030 size = 384;
d37c89e5
AM
3031
3032 /* Make a ".reg/999" section. */
3033 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 3034 size, note->descpos + offset);
d37c89e5
AM
3035}
3036
3037static bfd_boolean
3038ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3039{
3040 if (note->descsz != 136)
3041 return FALSE;
3042
228e534f 3043 elf_tdata (abfd)->core->pid
bc989cdc 3044 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 3045 elf_tdata (abfd)->core->program
d37c89e5 3046 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 3047 elf_tdata (abfd)->core->command
d37c89e5
AM
3048 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3049
3050 return TRUE;
3051}
3052
183e98be
AM
3053static char *
3054ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3055 ...)
3056{
3057 switch (note_type)
3058 {
3059 default:
3060 return NULL;
3061
3062 case NT_PRPSINFO:
3063 {
9ef6d1e3 3064 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
3065 va_list ap;
3066
3067 va_start (ap, note_type);
75cd47ed 3068 memset (data, 0, sizeof (data));
183e98be 3069 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 3070#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 3071 DIAGNOSTIC_PUSH;
be3e27bb 3072 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
3073 -Wstringop-truncation:
3074 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3075 */
95da9854
L
3076 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3077#endif
183e98be 3078 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 3079#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 3080 DIAGNOSTIC_POP;
fe75810f 3081#endif
183e98be
AM
3082 va_end (ap);
3083 return elfcore_write_note (abfd, buf, bufsiz,
3084 "CORE", note_type, data, sizeof (data));
3085 }
3086
3087 case NT_PRSTATUS:
3088 {
3089 char data[504];
3090 va_list ap;
3091 long pid;
3092 int cursig;
3093 const void *greg;
3094
3095 va_start (ap, note_type);
3096 memset (data, 0, 112);
3097 pid = va_arg (ap, long);
3098 bfd_put_32 (abfd, pid, data + 32);
3099 cursig = va_arg (ap, int);
3100 bfd_put_16 (abfd, cursig, data + 12);
3101 greg = va_arg (ap, const void *);
3102 memcpy (data + 112, greg, 384);
3103 memset (data + 496, 0, 8);
3104 va_end (ap);
3105 return elfcore_write_note (abfd, buf, bufsiz,
3106 "CORE", note_type, data, sizeof (data));
3107 }
3108 }
3109}
3110
5d35169e
AM
3111/* Add extra PPC sections. */
3112
b35d266b 3113static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3114{
07d6d2b8
AM
3115 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3116 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3117 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
3118 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3119 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3120 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 3121 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3122};
3123
7c8fe5c4
AM
3124enum _ppc64_sec_type {
3125 sec_normal = 0,
3126 sec_opd = 1,
3127 sec_toc = 2
3128};
3129
f0abc2a1
AM
3130struct _ppc64_elf_section_data
3131{
3132 struct bfd_elf_section_data elf;
411e1bfb 3133
f0abc2a1
AM
3134 union
3135 {
51aecdc5
AM
3136 /* An array with one entry for each opd function descriptor,
3137 and some spares since opd entries may be either 16 or 24 bytes. */
3138#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3139 struct _opd_sec_data
3140 {
3141 /* Points to the function code section for local opd entries. */
3142 asection **func_sec;
3143
3144 /* After editing .opd, adjust references to opd local syms. */
3145 long *adjust;
3146 } opd;
7c8fe5c4 3147
3a71aa26
AM
3148 /* An array for toc sections, indexed by offset/8. */
3149 struct _toc_sec_data
3150 {
3151 /* Specifies the relocation symbol index used at a given toc offset. */
3152 unsigned *symndx;
3153
3154 /* And the relocation addend. */
3155 bfd_vma *add;
3156 } toc;
7c8fe5c4
AM
3157 } u;
3158
3159 enum _ppc64_sec_type sec_type:2;
411e1bfb 3160
7c8fe5c4
AM
3161 /* Flag set when small branches are detected. Used to
3162 select suitable defaults for the stub group size. */
3163 unsigned int has_14bit_branch:1;
3e04d765
AM
3164
3165 /* Flag set when PLTCALL relocs are detected. */
3166 unsigned int has_pltcall:1;
f0abc2a1
AM
3167};
3168
3169#define ppc64_elf_section_data(sec) \
411e1bfb 3170 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3171
3172static bfd_boolean
4ce794b7 3173ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3174{
f592407e
AM
3175 if (!sec->used_by_bfd)
3176 {
3177 struct _ppc64_elf_section_data *sdata;
3178 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3179
f592407e
AM
3180 sdata = bfd_zalloc (abfd, amt);
3181 if (sdata == NULL)
3182 return FALSE;
3183 sec->used_by_bfd = sdata;
3184 }
f0abc2a1
AM
3185
3186 return _bfd_elf_new_section_hook (abfd, sec);
3187}
4025353c 3188
74f0fb50 3189static struct _opd_sec_data *
4025353c
AM
3190get_opd_info (asection * sec)
3191{
3192 if (sec != NULL
3193 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3194 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3195 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3196 return NULL;
3197}
90e3cdf2
JJ
3198\f
3199/* Parameters for the qsort hook. */
90e3cdf2 3200static bfd_boolean synthetic_relocatable;
cd285db5 3201static asection *synthetic_opd;
90e3cdf2 3202
699733f6 3203/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3204
3205static int
3206compare_symbols (const void *ap, const void *bp)
3207{
3208 const asymbol *a = * (const asymbol **) ap;
3209 const asymbol *b = * (const asymbol **) bp;
3210
699733f6
AM
3211 /* Section symbols first. */
3212 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3213 return -1;
699733f6 3214 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3215 return 1;
3216
699733f6 3217 /* then .opd symbols. */
cd285db5
AM
3218 if (synthetic_opd != NULL)
3219 {
3220 if (strcmp (a->section->name, ".opd") == 0
3221 && strcmp (b->section->name, ".opd") != 0)
3222 return -1;
3223 if (strcmp (a->section->name, ".opd") != 0
3224 && strcmp (b->section->name, ".opd") == 0)
3225 return 1;
3226 }
90e3cdf2 3227
699733f6 3228 /* then other code symbols. */
90e3cdf2
JJ
3229 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3230 == (SEC_CODE | SEC_ALLOC)
3231 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3232 != (SEC_CODE | SEC_ALLOC))
3233 return -1;
3234
3235 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3236 != (SEC_CODE | SEC_ALLOC)
3237 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3238 == (SEC_CODE | SEC_ALLOC))
3239 return 1;
3240
3241 if (synthetic_relocatable)
3242 {
3243 if (a->section->id < b->section->id)
3244 return -1;
3245
3246 if (a->section->id > b->section->id)
3247 return 1;
3248 }
3249
3250 if (a->value + a->section->vma < b->value + b->section->vma)
3251 return -1;
3252
3253 if (a->value + a->section->vma > b->value + b->section->vma)
3254 return 1;
3255
4d35a0aa
AM
3256 /* For syms with the same value, prefer strong dynamic global function
3257 syms over other syms. */
3258 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3259 return -1;
3260
3261 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3262 return 1;
3263
3264 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3265 return -1;
3266
3267 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3268 return 1;
3269
3270 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3271 return -1;
3272
3273 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3274 return 1;
3275
3276 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3277 return -1;
3278
3279 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3280 return 1;
3281
aaed6f5b 3282 return a > b;
90e3cdf2
JJ
3283}
3284
699733f6 3285/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3286
699733f6 3287static asymbol *
7292b3ac 3288sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3289{
699733f6 3290 long mid;
90e3cdf2 3291
7292b3ac 3292 if (id == (unsigned) -1)
699733f6
AM
3293 {
3294 while (lo < hi)
3295 {
3296 mid = (lo + hi) >> 1;
3297 if (syms[mid]->value + syms[mid]->section->vma < value)
3298 lo = mid + 1;
3299 else if (syms[mid]->value + syms[mid]->section->vma > value)
3300 hi = mid;
3301 else
3302 return syms[mid];
3303 }
3304 }
3305 else
3306 {
3307 while (lo < hi)
3308 {
3309 mid = (lo + hi) >> 1;
3310 if (syms[mid]->section->id < id)
3311 lo = mid + 1;
3312 else if (syms[mid]->section->id > id)
3313 hi = mid;
3314 else if (syms[mid]->value < value)
3315 lo = mid + 1;
3316 else if (syms[mid]->value > value)
3317 hi = mid;
3318 else
3319 return syms[mid];
3320 }
3321 }
3322 return NULL;
90e3cdf2
JJ
3323}
3324
468392fb
AM
3325static bfd_boolean
3326section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3327{
3328 bfd_vma vma = *(bfd_vma *) ptr;
3329 return ((section->flags & SEC_ALLOC) != 0
3330 && section->vma <= vma
3331 && vma < section->vma + section->size);
3332}
3333
699733f6 3334/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3335 entry syms. Also generate @plt symbols for the glink branch table.
3336 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3337
3338static long
a7535cf3
AM
3339ppc64_elf_get_synthetic_symtab (bfd *abfd,
3340 long static_count, asymbol **static_syms,
3341 long dyn_count, asymbol **dyn_syms,
c9727e01 3342 asymbol **ret)
90e3cdf2
JJ
3343{
3344 asymbol *s;
0ccf57bd 3345 size_t i, j, count;
90e3cdf2 3346 char *names;
0ccf57bd 3347 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3348 asection *opd = NULL;
90e3cdf2 3349 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3350 asymbol **syms;
ee67d69a 3351 int abi = abiversion (abfd);
90e3cdf2
JJ
3352
3353 *ret = NULL;
3354
ee67d69a
AM
3355 if (abi < 2)
3356 {
3357 opd = bfd_get_section_by_name (abfd, ".opd");
3358 if (opd == NULL && abi == 1)
3359 return 0;
3360 }
90e3cdf2 3361
a5259595
AM
3362 syms = NULL;
3363 codesecsym = 0;
3364 codesecsymend = 0;
3365 secsymend = 0;
3366 opdsymend = 0;
3367 symcount = 0;
3368 if (opd != NULL)
c9727e01 3369 {
a5259595
AM
3370 symcount = static_count;
3371 if (!relocatable)
3372 symcount += dyn_count;
3373 if (symcount == 0)
3374 return 0;
c9727e01 3375
a5259595
AM
3376 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3377 if (syms == NULL)
3378 return -1;
90e3cdf2 3379
a5259595
AM
3380 if (!relocatable && static_count != 0 && dyn_count != 0)
3381 {
3382 /* Use both symbol tables. */
3383 memcpy (syms, static_syms, static_count * sizeof (*syms));
3384 memcpy (syms + static_count, dyn_syms,
3385 (dyn_count + 1) * sizeof (*syms));
3386 }
3387 else if (!relocatable && static_count == 0)
3388 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3389 else
3390 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3391
0ccf57bd
AM
3392 /* Trim uninteresting symbols. Interesting symbols are section,
3393 function, and notype symbols. */
3394 for (i = 0, j = 0; i < symcount; ++i)
3395 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3396 | BSF_RELC | BSF_SRELC)) == 0)
3397 syms[j++] = syms[i];
3398 symcount = j;
3399
a5259595
AM
3400 synthetic_relocatable = relocatable;
3401 synthetic_opd = opd;
3402 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3403
a5259595
AM
3404 if (!relocatable && symcount > 1)
3405 {
bfa5bd2a
PA
3406 /* Trim duplicate syms, since we may have merged the normal
3407 and dynamic symbols. Actually, we only care about syms
3408 that have different values, so trim any with the same
3409 value. Don't consider ifunc and ifunc resolver symbols
3410 duplicates however, because GDB wants to know whether a
3411 text symbol is an ifunc resolver. */
a5259595 3412 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
3413 {
3414 const asymbol *s0 = syms[i - 1];
3415 const asymbol *s1 = syms[i];
3416
3417 if ((s0->value + s0->section->vma
3418 != s1->value + s1->section->vma)
3419 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3420 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3421 syms[j++] = syms[i];
3422 }
a5259595
AM
3423 symcount = j;
3424 }
699733f6 3425
a5259595
AM
3426 i = 0;
3427 /* Note that here and in compare_symbols we can't compare opd and
3428 sym->section directly. With separate debug info files, the
3429 symbols will be extracted from the debug file while abfd passed
3430 to this function is the real binary. */
0ccf57bd 3431 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
3432 ++i;
3433 codesecsym = i;
3434
3435 for (; i < symcount; ++i)
3436 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3437 | SEC_THREAD_LOCAL))
3438 != (SEC_CODE | SEC_ALLOC))
3439 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3440 break;
3441 codesecsymend = i;
3442
3443 for (; i < symcount; ++i)
3444 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3445 break;
3446 secsymend = i;
3447
3448 for (; i < symcount; ++i)
3449 if (strcmp (syms[i]->section->name, ".opd") != 0)
3450 break;
3451 opdsymend = i;
3452
3453 for (; i < symcount; ++i)
3454 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3455 != (SEC_CODE | SEC_ALLOC))
3456 break;
3457 symcount = i;
3458 }
c9727e01 3459 count = 0;
90e3cdf2 3460
699733f6 3461 if (relocatable)
90e3cdf2 3462 {
699733f6
AM
3463 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3464 arelent *r;
3465 size_t size;
0ccf57bd 3466 size_t relcount;
90e3cdf2 3467
468392fb
AM
3468 if (opdsymend == secsymend)
3469 goto done;
3470
699733f6 3471 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3472 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3473 if (relcount == 0)
c9727e01 3474 goto done;
90e3cdf2 3475
7356fed5
AM
3476 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3477 {
3478 count = -1;
3479 goto done;
3480 }
3481
699733f6 3482 size = 0;
595da8c5 3483 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3484 {
3485 asymbol *sym;
90e3cdf2 3486
595da8c5 3487 while (r < opd->relocation + relcount
699733f6
AM
3488 && r->address < syms[i]->value + opd->vma)
3489 ++r;
90e3cdf2 3490
595da8c5 3491 if (r == opd->relocation + relcount)
699733f6 3492 break;
90e3cdf2 3493
699733f6
AM
3494 if (r->address != syms[i]->value + opd->vma)
3495 continue;
90e3cdf2 3496
699733f6
AM
3497 if (r->howto->type != R_PPC64_ADDR64)
3498 continue;
90e3cdf2 3499
699733f6
AM
3500 sym = *r->sym_ptr_ptr;
3501 if (!sym_exists_at (syms, opdsymend, symcount,
3502 sym->section->id, sym->value + r->addend))
3503 {
3504 ++count;
3505 size += sizeof (asymbol);
3506 size += strlen (syms[i]->name) + 2;
3507 }
3508 }
90e3cdf2 3509
c4b0b099
AM
3510 if (size == 0)
3511 goto done;
699733f6
AM
3512 s = *ret = bfd_malloc (size);
3513 if (s == NULL)
3514 {
7356fed5 3515 count = -1;
c9727e01 3516 goto done;
699733f6 3517 }
90e3cdf2 3518
699733f6 3519 names = (char *) (s + count);
90e3cdf2 3520
595da8c5 3521 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3522 {
699733f6 3523 asymbol *sym;
90e3cdf2 3524
595da8c5 3525 while (r < opd->relocation + relcount
699733f6
AM
3526 && r->address < syms[i]->value + opd->vma)
3527 ++r;
90e3cdf2 3528
595da8c5 3529 if (r == opd->relocation + relcount)
699733f6
AM
3530 break;
3531
3532 if (r->address != syms[i]->value + opd->vma)
3533 continue;
3534
3535 if (r->howto->type != R_PPC64_ADDR64)
3536 continue;
90e3cdf2 3537
699733f6
AM
3538 sym = *r->sym_ptr_ptr;
3539 if (!sym_exists_at (syms, opdsymend, symcount,
3540 sym->section->id, sym->value + r->addend))
3541 {
3542 size_t len;
3543
3544 *s = *syms[i];
6ba2a415 3545 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3546 s->section = sym->section;
3547 s->value = sym->value + r->addend;
3548 s->name = names;
3549 *names++ = '.';
3550 len = strlen (syms[i]->name);
3551 memcpy (names, syms[i]->name, len + 1);
3552 names += len + 1;
6f610d07
UW
3553 /* Have udata.p point back to the original symbol this
3554 synthetic symbol was derived from. */
3555 s->udata.p = syms[i];
699733f6
AM
3556 s++;
3557 }
3558 }
3559 }
3560 else
90e3cdf2 3561 {
468392fb 3562 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3563 bfd_byte *contents = NULL;
699733f6 3564 size_t size;
0ccf57bd 3565 size_t plt_count = 0;
468392fb
AM
3566 bfd_vma glink_vma = 0, resolv_vma = 0;
3567 asection *dynamic, *glink = NULL, *relplt = NULL;
3568 arelent *p;
90e3cdf2 3569
ee67d69a 3570 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3571 {
c4b0b099
AM
3572 free_contents_and_exit_err:
3573 count = -1;
ee67d69a 3574 free_contents_and_exit:
699733f6 3575 if (contents)
ee67d69a 3576 free (contents);
c9727e01 3577 goto done;
699733f6 3578 }
90e3cdf2 3579
699733f6
AM
3580 size = 0;
3581 for (i = secsymend; i < opdsymend; ++i)
3582 {
3583 bfd_vma ent;
90e3cdf2 3584
5ef11c02
AM
3585 /* Ignore bogus symbols. */
3586 if (syms[i]->value > opd->size - 8)
3587 continue;
3588
699733f6
AM
3589 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3590 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3591 {
3592 ++count;
3593 size += sizeof (asymbol);
3594 size += strlen (syms[i]->name) + 2;
3595 }
3596 }
90e3cdf2 3597
468392fb 3598 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3599 if (dyn_count != 0
3600 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3601 {
3602 bfd_byte *dynbuf, *extdyn, *extdynend;
3603 size_t extdynsize;
3604 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3605
3606 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3607 goto free_contents_and_exit_err;
468392fb
AM
3608
3609 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3610 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3611
3612 extdyn = dynbuf;
3613 extdynend = extdyn + dynamic->size;
3614 for (; extdyn < extdynend; extdyn += extdynsize)
3615 {
3616 Elf_Internal_Dyn dyn;
3617 (*swap_dyn_in) (abfd, extdyn, &dyn);
3618
3619 if (dyn.d_tag == DT_NULL)
3620 break;
3621
3622 if (dyn.d_tag == DT_PPC64_GLINK)
3623 {
9e390558
AM
3624 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3625 See comment in ppc64_elf_finish_dynamic_sections. */
3626 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3627 /* The .glink section usually does not survive the final
3628 link; search for the section (usually .text) where the
3629 glink stubs now reside. */
3630 glink = bfd_sections_find_if (abfd, section_covers_vma,
3631 &glink_vma);
3632 break;
3633 }
3634 }
3635
3636 free (dynbuf);
3637 }
3638
3639 if (glink != NULL)
3640 {
3641 /* Determine __glink trampoline by reading the relative branch
3642 from the first glink stub. */
3643 bfd_byte buf[4];
b9e5796b
AM
3644 unsigned int off = 0;
3645
3646 while (bfd_get_section_contents (abfd, glink, buf,
3647 glink_vma + off - glink->vma, 4))
468392fb
AM
3648 {
3649 unsigned int insn = bfd_get_32 (abfd, buf);
3650 insn ^= B_DOT;
3651 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3652 {
3653 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3654 break;
3655 }
3656 off += 4;
3657 if (off > 4)
3658 break;
468392fb
AM
3659 }
3660
3661 if (resolv_vma)
3662 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3663
066ee829
AM
3664 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3665 if (relplt != NULL)
3666 {
3667 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3668 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3669 goto free_contents_and_exit_err;
68ffbac6 3670
066ee829
AM
3671 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3672 size += plt_count * sizeof (asymbol);
468392fb 3673
066ee829
AM
3674 p = relplt->relocation;
3675 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3676 {
3677 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3678 if (p->addend != 0)
3679 size += sizeof ("+0x") - 1 + 16;
3680 }
066ee829 3681 }
468392fb
AM
3682 }
3683
c4b0b099
AM
3684 if (size == 0)
3685 goto free_contents_and_exit;
699733f6
AM
3686 s = *ret = bfd_malloc (size);
3687 if (s == NULL)
c4b0b099 3688 goto free_contents_and_exit_err;
90e3cdf2 3689
468392fb 3690 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3691
699733f6 3692 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3693 {
699733f6 3694 bfd_vma ent;
90e3cdf2 3695
5ef11c02
AM
3696 if (syms[i]->value > opd->size - 8)
3697 continue;
3698
699733f6
AM
3699 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3700 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3701 {
0ccf57bd 3702 size_t lo, hi;
699733f6 3703 size_t len;
c9727e01 3704 asection *sec = abfd->sections;
90e3cdf2 3705
699733f6
AM
3706 *s = *syms[i];
3707 lo = codesecsym;
3708 hi = codesecsymend;
3709 while (lo < hi)
3710 {
0ccf57bd 3711 size_t mid = (lo + hi) >> 1;
699733f6
AM
3712 if (syms[mid]->section->vma < ent)
3713 lo = mid + 1;
3714 else if (syms[mid]->section->vma > ent)
3715 hi = mid;
3716 else
c9727e01
AM
3717 {
3718 sec = syms[mid]->section;
3719 break;
3720 }
699733f6
AM
3721 }
3722
c9727e01 3723 if (lo >= hi && lo > codesecsym)
699733f6 3724 sec = syms[lo - 1]->section;
699733f6
AM
3725
3726 for (; sec != NULL; sec = sec->next)
3727 {
3728 if (sec->vma > ent)
3729 break;
63524580
JK
3730 /* SEC_LOAD may not be set if SEC is from a separate debug
3731 info file. */
3732 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3733 break;
3734 if ((sec->flags & SEC_CODE) != 0)
3735 s->section = sec;
3736 }
6ba2a415 3737 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3738 s->value = ent - s->section->vma;
3739 s->name = names;
3740 *names++ = '.';
3741 len = strlen (syms[i]->name);
3742 memcpy (names, syms[i]->name, len + 1);
3743 names += len + 1;
6f610d07
UW
3744 /* Have udata.p point back to the original symbol this
3745 synthetic symbol was derived from. */
3746 s->udata.p = syms[i];
699733f6 3747 s++;
90e3cdf2 3748 }
90e3cdf2 3749 }
699733f6 3750 free (contents);
468392fb
AM
3751
3752 if (glink != NULL && relplt != NULL)
3753 {
3754 if (resolv_vma)
3755 {
3756 /* Add a symbol for the main glink trampoline. */
86a4952b 3757 memset (s, 0, sizeof *s);
468392fb 3758 s->the_bfd = abfd;
6ba2a415 3759 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3760 s->section = glink;
3761 s->value = resolv_vma - glink->vma;
3762 s->name = names;
3763 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3764 names += sizeof ("__glink_PLTresolve");
3765 s++;
3766 count++;
3767 }
3768
3769 /* FIXME: It would be very much nicer to put sym@plt on the
3770 stub rather than on the glink branch table entry. The
3771 objdump disassembler would then use a sensible symbol
3772 name on plt calls. The difficulty in doing so is
3773 a) finding the stubs, and,
3774 b) matching stubs against plt entries, and,
3775 c) there can be multiple stubs for a given plt entry.
3776
3777 Solving (a) could be done by code scanning, but older
3778 ppc64 binaries used different stubs to current code.
3779 (b) is the tricky one since you need to known the toc
3780 pointer for at least one function that uses a pic stub to
3781 be able to calculate the plt address referenced.
3782 (c) means gdb would need to set multiple breakpoints (or
3783 find the glink branch itself) when setting breakpoints
3784 for pending shared library loads. */
3785 p = relplt->relocation;
3786 for (i = 0; i < plt_count; i++, p++)
3787 {
3788 size_t len;
3789
3790 *s = **p->sym_ptr_ptr;
3791 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3792 we are defining a symbol, ensure one of them is set. */
3793 if ((s->flags & BSF_LOCAL) == 0)
3794 s->flags |= BSF_GLOBAL;
6ba2a415 3795 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3796 s->section = glink;
3797 s->value = glink_vma - glink->vma;
3798 s->name = names;
3799 s->udata.p = NULL;
3800 len = strlen ((*p->sym_ptr_ptr)->name);
3801 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3802 names += len;
e054468f
AM
3803 if (p->addend != 0)
3804 {
3805 memcpy (names, "+0x", sizeof ("+0x") - 1);
3806 names += sizeof ("+0x") - 1;
3807 bfd_sprintf_vma (abfd, names, p->addend);
3808 names += strlen (names);
3809 }
468392fb
AM
3810 memcpy (names, "@plt", sizeof ("@plt"));
3811 names += sizeof ("@plt");
3812 s++;
b9e5796b
AM
3813 if (abi < 2)
3814 {
3815 glink_vma += 8;
3816 if (i >= 0x8000)
3817 glink_vma += 4;
3818 }
3819 else
468392fb
AM
3820 glink_vma += 4;
3821 }
3822 count += plt_count;
3823 }
90e3cdf2
JJ
3824 }
3825
c9727e01 3826 done:
a7535cf3 3827 free (syms);
90e3cdf2
JJ
3828 return count;
3829}
5bd4f169 3830\f
65f38f15
AM
3831/* The following functions are specific to the ELF linker, while
3832 functions above are used generally. Those named ppc64_elf_* are
3833 called by the main ELF linker code. They appear in this file more
3834 or less in the order in which they are called. eg.
3835 ppc64_elf_check_relocs is called early in the link process,
3836 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3837 called.
3838
3839 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3840 functions have both a function code symbol and a function descriptor
3841 symbol. A call to foo in a relocatable object file looks like:
3842
3843 . .text
3844 . x:
3845 . bl .foo
3846 . nop
3847
3848 The function definition in another object file might be:
3849
3850 . .section .opd
3851 . foo: .quad .foo
3852 . .quad .TOC.@tocbase
3853 . .quad 0
3854 .
3855 . .text
3856 . .foo: blr
3857
3858 When the linker resolves the call during a static link, the branch
3859 unsurprisingly just goes to .foo and the .opd information is unused.
3860 If the function definition is in a shared library, things are a little
3861 different: The call goes via a plt call stub, the opd information gets
3862 copied to the plt, and the linker patches the nop.
3863
3864 . x:
3865 . bl .foo_stub
3866 . ld 2,40(1)
3867 .
3868 .
3869 . .foo_stub:
71a39c98
AM
3870 . std 2,40(1) # in practice, the call stub
3871 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3872 . addi 11,11,Lfoo@toc@l # this is the general idea
3873 . ld 12,0(11)
3874 . ld 2,8(11)
3875 . mtctr 12
3876 . ld 11,16(11)
e86ce104
AM
3877 . bctr
3878 .
3879 . .section .plt
3880 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3881
3882 The "reloc ()" notation is supposed to indicate that the linker emits
3883 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3884 copying.
3885
3886 What are the difficulties here? Well, firstly, the relocations
3887 examined by the linker in check_relocs are against the function code
3888 sym .foo, while the dynamic relocation in the plt is emitted against
3889 the function descriptor symbol, foo. Somewhere along the line, we need
3890 to carefully copy dynamic link information from one symbol to the other.
3891 Secondly, the generic part of the elf linker will make .foo a dynamic
3892 symbol as is normal for most other backends. We need foo dynamic
3893 instead, at least for an application final link. However, when
3894 creating a shared library containing foo, we need to have both symbols
3895 dynamic so that references to .foo are satisfied during the early
3896 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3897 definition from some other object, eg. a static library.
3898
3899 Update: As of August 2004, we support a new convention. Function
3900 calls may use the function descriptor symbol, ie. "bl foo". This
3901 behaves exactly as "bl .foo". */
65f38f15 3902
7c8bbca5
AM
3903/* Of those relocs that might be copied as dynamic relocs, this
3904 function selects those that must be copied when linking a shared
3905 library or PIE, even when the symbol is local. */
65f38f15 3906
1d483afe
AM
3907static int
3908must_be_dyn_reloc (struct bfd_link_info *info,
3909 enum elf_ppc64_reloc_type r_type)
3910{
3911 switch (r_type)
3912 {
3913 default:
7c8bbca5
AM
3914 /* Only relative relocs can be resolved when the object load
3915 address isn't fixed. DTPREL64 is excluded because the
3916 dynamic linker needs to differentiate global dynamic from
3917 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3918 return 1;
3919
3920 case R_PPC64_REL32:
3921 case R_PPC64_REL64:
3922 case R_PPC64_REL30:
3923 return 0;
3924
3925 case R_PPC64_TPREL16:
3926 case R_PPC64_TPREL16_LO:
3927 case R_PPC64_TPREL16_HI:
3928 case R_PPC64_TPREL16_HA:
3929 case R_PPC64_TPREL16_DS:
3930 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3931 case R_PPC64_TPREL16_HIGH:
3932 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3933 case R_PPC64_TPREL16_HIGHER:
3934 case R_PPC64_TPREL16_HIGHERA:
3935 case R_PPC64_TPREL16_HIGHEST:
3936 case R_PPC64_TPREL16_HIGHESTA:
3937 case R_PPC64_TPREL64:
7c8bbca5
AM
3938 /* These relocations are relative but in a shared library the
3939 linker doesn't know the thread pointer base. */
3940 return bfd_link_dll (info);
1d483afe
AM
3941 }
3942}
65f38f15 3943
f4656909
AM
3944/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3945 copying dynamic variables from a shared lib into an app's dynbss
3946 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3947 shared lib. With code that gcc generates, it's vital that this be
3948 enabled; In the PowerPC64 ABI, the address of a function is actually
3949 the address of a function descriptor, which resides in the .opd
3950 section. gcc uses the descriptor directly rather than going via the
3951 GOT as some other ABI's do, which means that initialized function
3952 pointers must reference the descriptor. Thus, a function pointer
3953 initialized to the address of a function in a shared library will
3954 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3955 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3956 presents a problem as a plt entry for that function is also
3957 initialized from the function descriptor symbol and the copy reloc
3958 may not be initialized first. */
a23b6845 3959#define ELIMINATE_COPY_RELOCS 1
f4656909 3960
721956f4 3961/* Section name for stubs is the associated section name plus this
29942be8
NC
3962 string. */
3963#define STUB_SUFFIX ".stub"
721956f4
AM
3964
3965/* Linker stubs.
3966 ppc_stub_long_branch:
3967 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3968 destination, but a 24 bit branch in a stub section will reach.
3969 . b dest
3970
3971 ppc_stub_plt_branch:
3972 Similar to the above, but a 24 bit branch in the stub section won't
3973 reach its destination.
71a39c98
AM
3974 . addis %r11,%r2,xxx@toc@ha
3975 . ld %r12,xxx@toc@l(%r11)
3976 . mtctr %r12
721956f4
AM
3977 . bctr
3978
3979 ppc_stub_plt_call:
2c66dc6c
AM
3980 Used to call a function in a shared library. If it so happens that
3981 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3982 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 3983 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
3984 . addis %r11,%r2,xxx@toc@ha
3985 . ld %r12,xxx+0@toc@l(%r11)
3986 . mtctr %r12
3987 . ld %r2,xxx+8@toc@l(%r11)
3988 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3989 . bctr
ad8e1ba5
AM
3990
3991 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3992 code to adjust the value and save r2 to support multiple toc sections.
3993 A ppc_stub_long_branch with an r2 offset looks like:
3994 . std %r2,40(%r1)
3995 . addis %r2,%r2,off@ha
3996 . addi %r2,%r2,off@l
3997 . b dest
3998
3999 A ppc_stub_plt_branch with an r2 offset looks like:
4000 . std %r2,40(%r1)
71a39c98
AM
4001 . addis %r11,%r2,xxx@toc@ha
4002 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
4003 . addis %r2,%r2,off@ha
4004 . addi %r2,%r2,off@l
71a39c98 4005 . mtctr %r12
ad8e1ba5 4006 . bctr
ac2df442 4007
05d0e962
AM
4008 All of the above stubs are shown as their ELFv1 variants. ELFv2
4009 variants exist too, simpler for plt calls since a new toc pointer
4010 and static chain are not loaded by the stub. In addition, ELFv2
4011 has some more complex stubs to handle calls marked with NOTOC
4012 relocs from functions where r2 is not a valid toc pointer. These
4013 come in two flavours, the ones shown below, and _both variants that
4014 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
4015 one call is from a function where r2 is used as the toc pointer but
4016 needs a toc adjusting stub for small-model multi-toc, and another
4017 call is from a function where r2 is not valid.
4018 ppc_stub_long_branch_notoc:
4019 . mflr %r12
4020 . bcl 20,31,1f
4021 . 1:
4022 . mflr %r11
4023 . mtlr %r12
4024 . lis %r12,xxx-1b@highest
4025 . ori %r12,xxx-1b@higher
4026 . sldi %r12,%r12,32
4027 . oris %r12,%r12,xxx-1b@hi
4028 . ori %r12,%r12,xxx-1b@l
4029 . add %r12,%r11,%r12
4030 . b dest
4031
4032 ppc_stub_plt_branch_notoc:
4033 . mflr %r12
4034 . bcl 20,31,1f
4035 . 1:
4036 . mflr %r11
4037 . mtlr %r12
4038 . lis %r12,xxx-1b@highest
4039 . ori %r12,xxx-1b@higher
4040 . sldi %r12,%r12,32
4041 . oris %r12,%r12,xxx-1b@hi
4042 . ori %r12,%r12,xxx-1b@l
4043 . add %r12,%r11,%r12
4044 . mtctr %r12
4045 . bctr
4046
4047 ppc_stub_plt_call_notoc:
4048 . mflr %r12
4049 . bcl 20,31,1f
4050 . 1:
4051 . mflr %r11
4052 . mtlr %r12
4053 . lis %r12,xxx-1b@highest
4054 . ori %r12,xxx-1b@higher
4055 . sldi %r12,%r12,32
4056 . oris %r12,%r12,xxx-1b@hi
4057 . ori %r12,%r12,xxx-1b@l
4058 . ldx %r12,%r11,%r12
4059 . mtctr %r12
4060 . bctr
4061
4062 In cases where the high instructions would add zero, they are
4063 omitted and following instructions modified in some cases.
4064
4065 For a given stub group (a set of sections all using the same toc
4066 pointer value) there will be just one stub type used for any
4067 particular function symbol. For example, if printf is called from
4068 code with the tocsave optimization (ie. r2 saved in function
4069 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
4070 and from other code without the tocsave optimization requiring a
4071 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
4072 type will be created. Calls with the tocsave optimization will
4073 enter this stub after the instruction saving r2. A similar
4074 situation exists when calls are marked with R_PPC64_REL24_NOTOC
4075 relocations. These require a ppc_stub_plt_call_notoc linkage stub
4076 to call an external function like printf. If other calls to printf
4077 require a ppc_stub_plt_call linkage stub then a single
4078 ppc_stub_plt_call_notoc linkage stub will be used for both types of
4079 call. If other calls to printf require a ppc_stub_plt_call_r2save
4080 linkage stub then a single ppc_stub_plt_call_both linkage stub will
4081 be created and calls not requiring r2 to be saved will enter the
4082 stub after the r2 save instruction. There is an analogous
4083 hierarchy of long branch and plt branch stubs for local call
4084 linkage. */
721956f4
AM
4085
4086enum ppc_stub_type {
4087 ppc_stub_none,
4088 ppc_stub_long_branch,
ad8e1ba5 4089 ppc_stub_long_branch_r2off,
05d0e962
AM
4090 ppc_stub_long_branch_notoc,
4091 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 4092 ppc_stub_plt_branch,
ad8e1ba5 4093 ppc_stub_plt_branch_r2off,
05d0e962
AM
4094 ppc_stub_plt_branch_notoc,
4095 ppc_stub_plt_branch_both,
794e51c0 4096 ppc_stub_plt_call,
7341d5e2 4097 ppc_stub_plt_call_r2save,
05d0e962
AM
4098 ppc_stub_plt_call_notoc,
4099 ppc_stub_plt_call_both,
a4b6fadd
AM
4100 ppc_stub_global_entry,
4101 ppc_stub_save_res
721956f4
AM
4102};
4103
6f20ed8a
AM
4104/* Information on stub grouping. */
4105struct map_stub
4106{
4107 /* The stub section. */
4108 asection *stub_sec;
4109 /* This is the section to which stubs in the group will be attached. */
4110 asection *link_sec;
a4b6fadd
AM
4111 /* Next group. */
4112 struct map_stub *next;
4113 /* Whether to emit a copy of register save/restore functions in this
4114 group. */
4115 int needs_save_res;
d4aaa2a0
AM
4116 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
4117 or -1u if no such stub with bctrl exists. */
4118 unsigned int tls_get_addr_opt_bctrl;
6f20ed8a
AM
4119};
4120
721956f4
AM
4121struct ppc_stub_hash_entry {
4122
4123 /* Base hash table entry structure. */
4124 struct bfd_hash_entry root;
4125
ad8e1ba5
AM
4126 enum ppc_stub_type stub_type;
4127
6f20ed8a
AM
4128 /* Group information. */
4129 struct map_stub *group;
721956f4
AM
4130
4131 /* Offset within stub_sec of the beginning of this stub. */
4132 bfd_vma stub_offset;
4133
4134 /* Given the symbol's value and its section we can determine its final
4135 value when building the stubs (so the stub knows where to jump. */
4136 bfd_vma target_value;
4137 asection *target_section;
4138
721956f4
AM
4139 /* The symbol table entry, if any, that this was derived from. */
4140 struct ppc_link_hash_entry *h;
e054468f 4141 struct plt_entry *plt_ent;
721956f4 4142
2d7ad24e
AM
4143 /* Symbol type. */
4144 unsigned char symtype;
4145
6911b7dc
AM
4146 /* Symbol st_other. */
4147 unsigned char other;
721956f4
AM
4148};
4149
4150struct ppc_branch_hash_entry {
4151
4152 /* Base hash table entry structure. */
4153 struct bfd_hash_entry root;
4154
c456f082 4155 /* Offset within branch lookup table. */
721956f4
AM
4156 unsigned int offset;
4157
4158 /* Generation marker. */
4159 unsigned int iter;
4160};
65f38f15 4161
19e08130
AM
4162/* Used to track dynamic relocations for local symbols. */
4163struct ppc_dyn_relocs
4164{
4165 struct ppc_dyn_relocs *next;
4166
4167 /* The input section of the reloc. */
4168 asection *sec;
4169
4170 /* Total number of relocs copied for the input section. */
4171 unsigned int count : 31;
4172
4173 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4174 unsigned int ifunc : 1;
4175};
4176
65f38f15
AM
4177struct ppc_link_hash_entry
4178{
4179 struct elf_link_hash_entry elf;
4180
b3fac117
AM
4181 union {
4182 /* A pointer to the most recently used stub hash entry against this
4183 symbol. */
4184 struct ppc_stub_hash_entry *stub_cache;
4185
4186 /* A pointer to the next symbol starting with a '.' */
4187 struct ppc_link_hash_entry *next_dot_sym;
4188 } u;
721956f4 4189
65f38f15 4190 /* Track dynamic relocs copied for this symbol. */
6061a67d 4191 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4192
721956f4 4193 /* Link between function code and descriptor symbols. */
34814b9f 4194 struct ppc_link_hash_entry *oh;
721956f4 4195
e86ce104
AM
4196 /* Flag function code and descriptor symbols. */
4197 unsigned int is_func:1;
4198 unsigned int is_func_descriptor:1;
908b32fc 4199 unsigned int fake:1;
411e1bfb 4200
c5614fa4
AM
4201 /* Whether global opd/toc sym has been adjusted or not.
4202 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4203 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4204 unsigned int adjust_done:1;
4205
a4b6fadd
AM
4206 /* Set if this is an out-of-line register save/restore function,
4207 with non-standard calling convention. */
4208 unsigned int save_res:1;
4209
8b5f1ed8
AM
4210 /* Set if a duplicate symbol with non-zero localentry is detected,
4211 even when the duplicate symbol does not provide a definition. */
4212 unsigned int non_zero_localentry:1;
4213
411e1bfb 4214 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
4215 Bits are or'd into the mask as the corresponding relocs are
4216 encountered during check_relocs, with TLS_TLS being set when any
4217 of the other TLS bits are set. tls_optimize clears bits when
4218 optimizing to indicate the corresponding GOT entry type is not
4219 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4220 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4221 separate flag rather than setting TPREL just for convenience in
4222 distinguishing the two cases.
4223 These flags are also kept for local symbols. */
4224#define TLS_TLS 1 /* Any TLS reloc. */
4225#define TLS_GD 2 /* GD reloc. */
4226#define TLS_LD 4 /* LD reloc. */
4227#define TLS_TPREL 8 /* TPREL reloc, => IE. */
4228#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4229#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 4230#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 4231#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 4232 unsigned char tls_mask;
37da22e5
AM
4233
4234 /* The above field is also used to mark function symbols. In which
4235 case TLS_TLS will be 0. */
4236#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 4237#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 4238#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
4239};
4240
4241/* ppc64 ELF linker hash table. */
4242
4243struct ppc_link_hash_table
4244{
4245 struct elf_link_hash_table elf;
4246
721956f4
AM
4247 /* The stub hash table. */
4248 struct bfd_hash_table stub_hash_table;
4249
4250 /* Another hash table for plt_branch stubs. */
4251 struct bfd_hash_table branch_hash_table;
4252
3b421ab3
AM
4253 /* Hash table for function prologue tocsave. */
4254 htab_t tocsave_htab;
4255
e7d1c40c
AM
4256 /* Various options and other info passed from the linker. */
4257 struct ppc64_elf_params *params;
721956f4 4258
6f20ed8a
AM
4259 /* The size of sec_info below. */
4260 unsigned int sec_info_arr_size;
4261
4262 /* Per-section array of extra section info. Done this way rather
4263 than as part of ppc64_elf_section_data so we have the info for
4264 non-ppc64 sections. */
4265 struct
4266 {
4267 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4268 bfd_vma toc_off;
6f20ed8a
AM
4269
4270 union
4271 {
4272 /* The section group that this section belongs to. */
4273 struct map_stub *group;
4274 /* A temp section list pointer. */
4275 asection *list;
4276 } u;
4277 } *sec_info;
721956f4 4278
a4b6fadd
AM
4279 /* Linked list of groups. */
4280 struct map_stub *group;
4281
ad8e1ba5
AM
4282 /* Temp used when calculating TOC pointers. */
4283 bfd_vma toc_curr;
bf102f86
AM
4284 bfd *toc_bfd;
4285 asection *toc_first_sec;
ad8e1ba5 4286
b3fac117
AM
4287 /* Used when adding symbols. */
4288 struct ppc_link_hash_entry *dot_syms;
4289
33e44f2e 4290 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4291 asection *glink;
9e390558 4292 asection *global_entry;
82bd7b59 4293 asection *sfpr;
2d7ad24e
AM
4294 asection *pltlocal;
4295 asection *relpltlocal;
4ce794b7
AM
4296 asection *brlt;
4297 asection *relbrlt;
58d180e8 4298 asection *glink_eh_frame;
ec338859 4299
8387904d
AM
4300 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4301 struct ppc_link_hash_entry *tls_get_addr;
4302 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4303
927be08e
AM
4304 /* The size of reliplt used by got entry relocs. */
4305 bfd_size_type got_reli_size;
4306
9b5ecbd0 4307 /* Statistics. */
7341d5e2 4308 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4309
ee75fd95
AM
4310 /* Number of stubs against global syms. */
4311 unsigned long stub_globals;
4312
ee67d69a
AM
4313 /* Set if we're linking code with function descriptors. */
4314 unsigned int opd_abi:1;
4315
4c52953f 4316 /* Support for multiple toc sections. */
33c0ec9d 4317 unsigned int do_multi_toc:1;
4c52953f 4318 unsigned int multi_toc_needed:1;
927be08e 4319 unsigned int second_toc_pass:1;
67f0cbdb 4320 unsigned int do_toc_opt:1;
4c52953f 4321
9a23f96e
AM
4322 /* Set if tls optimization is enabled. */
4323 unsigned int do_tls_opt:1;
4324
3e04d765
AM
4325 /* Set if inline plt calls should be converted to direct calls. */
4326 unsigned int can_convert_all_inline_plt:1;
4327
5d1634d7 4328 /* Set on error. */
99877b66 4329 unsigned int stub_error:1;
721956f4 4330
8c5b4e52
AM
4331 /* Whether func_desc_adjust needs to be run over symbols. */
4332 unsigned int need_func_desc_adj:1;
721956f4 4333
82e66161
AM
4334 /* Whether there exist local gnu indirect function resolvers,
4335 referenced by dynamic relocations. */
4336 unsigned int local_ifunc_resolver:1;
4337 unsigned int maybe_local_ifunc_resolver:1;
4338
f378ab09
AM
4339 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4340 unsigned int has_plt_localentry0:1;
4341
721956f4
AM
4342 /* Incremented every time we size stubs. */
4343 unsigned int stub_iteration;
5d1634d7 4344
87d72d41
AM
4345 /* Small local sym cache. */
4346 struct sym_cache sym_cache;
65f38f15
AM
4347};
4348
4c52953f
AM
4349/* Rename some of the generic section flags to better document how they
4350 are used here. */
b0dddeec
AM
4351
4352/* Nonzero if this section has TLS related relocations. */
4353#define has_tls_reloc sec_flg0
4354
23cedd1d 4355/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
4356#define has_tls_get_addr_call sec_flg1
4357
4358/* Nonzero if this section has any toc or got relocs. */
4359#define has_toc_reloc sec_flg2
4360
4361/* Nonzero if this section has a call to another section that uses
4362 the toc or got. */
d77c8a4b 4363#define makes_toc_func_call sec_flg3
b0dddeec
AM
4364
4365/* Recursion protection when determining above flag. */
d77c8a4b 4366#define call_check_in_progress sec_flg4
70cc837d 4367#define call_check_done sec_flg5
4c52953f 4368
65f38f15
AM
4369/* Get the ppc64 ELF linker hash table from a link_info structure. */
4370
4371#define ppc_hash_table(p) \
4dfe6ac6
NC
4372 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4373 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4374
721956f4
AM
4375#define ppc_stub_hash_lookup(table, string, create, copy) \
4376 ((struct ppc_stub_hash_entry *) \
4377 bfd_hash_lookup ((table), (string), (create), (copy)))
4378
4379#define ppc_branch_hash_lookup(table, string, create, copy) \
4380 ((struct ppc_branch_hash_entry *) \
4381 bfd_hash_lookup ((table), (string), (create), (copy)))
4382
4383/* Create an entry in the stub hash table. */
4384
4385static struct bfd_hash_entry *
4ce794b7
AM
4386stub_hash_newfunc (struct bfd_hash_entry *entry,
4387 struct bfd_hash_table *table,
4388 const char *string)
721956f4
AM
4389{
4390 /* Allocate the structure if it has not already been allocated by a
4391 subclass. */
4392 if (entry == NULL)
4393 {
4394 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4395 if (entry == NULL)
4396 return entry;
4397 }
4398
4399 /* Call the allocation method of the superclass. */
4400 entry = bfd_hash_newfunc (entry, table, string);
4401 if (entry != NULL)
4402 {
4403 struct ppc_stub_hash_entry *eh;
4404
4405 /* Initialize the local fields. */
4406 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4407 eh->stub_type = ppc_stub_none;
6f20ed8a 4408 eh->group = NULL;
721956f4
AM
4409 eh->stub_offset = 0;
4410 eh->target_value = 0;
4411 eh->target_section = NULL;
721956f4 4412 eh->h = NULL;
6911b7dc 4413 eh->plt_ent = NULL;
6911b7dc 4414 eh->other = 0;
721956f4
AM
4415 }
4416
4417 return entry;
4418}
4419
4420/* Create an entry in the branch hash table. */
4421
4422static struct bfd_hash_entry *
4ce794b7
AM
4423branch_hash_newfunc (struct bfd_hash_entry *entry,
4424 struct bfd_hash_table *table,
4425 const char *string)
721956f4
AM
4426{
4427 /* Allocate the structure if it has not already been allocated by a
4428 subclass. */
4429 if (entry == NULL)
4430 {
4431 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4432 if (entry == NULL)
4433 return entry;
4434 }
4435
4436 /* Call the allocation method of the superclass. */
4437 entry = bfd_hash_newfunc (entry, table, string);
4438 if (entry != NULL)
4439 {
4440 struct ppc_branch_hash_entry *eh;
4441
4442 /* Initialize the local fields. */
4443 eh = (struct ppc_branch_hash_entry *) entry;
4444 eh->offset = 0;
4445 eh->iter = 0;
4446 }
4447
4448 return entry;
4449}
4450
65f38f15
AM
4451/* Create an entry in a ppc64 ELF linker hash table. */
4452
4453static struct bfd_hash_entry *
4ce794b7
AM
4454link_hash_newfunc (struct bfd_hash_entry *entry,
4455 struct bfd_hash_table *table,
4456 const char *string)
65f38f15
AM
4457{
4458 /* Allocate the structure if it has not already been allocated by a
4459 subclass. */
4460 if (entry == NULL)
4461 {
4462 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4463 if (entry == NULL)
4464 return entry;
4465 }
4466
4467 /* Call the allocation method of the superclass. */
4468 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4469 if (entry != NULL)
4470 {
4471 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4472
b3fac117 4473 memset (&eh->u.stub_cache, 0,
908b32fc 4474 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4475 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4476
4477 /* When making function calls, old ABI code references function entry
4478 points (dot symbols), while new ABI code references the function
4479 descriptor symbol. We need to make any combination of reference and
4480 definition work together, without breaking archive linking.
4481
4482 For a defined function "foo" and an undefined call to "bar":
4483 An old object defines "foo" and ".foo", references ".bar" (possibly
4484 "bar" too).
4485 A new object defines "foo" and references "bar".
4486
4487 A new object thus has no problem with its undefined symbols being
4488 satisfied by definitions in an old object. On the other hand, the
4489 old object won't have ".bar" satisfied by a new object.
4490
4491 Keep a list of newly added dot-symbols. */
4492
4493 if (string[0] == '.')
4494 {
4495 struct ppc_link_hash_table *htab;
4496
4497 htab = (struct ppc_link_hash_table *) table;
4498 eh->u.next_dot_sym = htab->dot_syms;
4499 htab->dot_syms = eh;
4500 }
65f38f15
AM
4501 }
4502
4503 return entry;
4504}
4505
3b421ab3
AM
4506struct tocsave_entry {
4507 asection *sec;
4508 bfd_vma offset;
4509};
4510
4511static hashval_t
4512tocsave_htab_hash (const void *p)
4513{
4514 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4515 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4516}
4517
4518static int
4519tocsave_htab_eq (const void *p1, const void *p2)
4520{
4521 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4522 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4523 return e1->sec == e2->sec && e1->offset == e2->offset;
4524}
4525
68faa637
AM
4526/* Destroy a ppc64 ELF linker hash table. */
4527
4528static void
d495ab0d 4529ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4530{
d495ab0d 4531 struct ppc_link_hash_table *htab;
68faa637 4532
d495ab0d 4533 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4534 if (htab->tocsave_htab)
4535 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4536 bfd_hash_table_free (&htab->branch_hash_table);
4537 bfd_hash_table_free (&htab->stub_hash_table);
4538 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4539}
4540
65f38f15
AM
4541/* Create a ppc64 ELF linker hash table. */
4542
4543static struct bfd_link_hash_table *
4ce794b7 4544ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4545{
4546 struct ppc_link_hash_table *htab;
4547 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4548
4ce794b7 4549 htab = bfd_zmalloc (amt);
65f38f15
AM
4550 if (htab == NULL)
4551 return NULL;
4552
66eb6687 4553 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4554 sizeof (struct ppc_link_hash_entry),
4555 PPC64_ELF_DATA))
65f38f15 4556 {
e2d34d7d 4557 free (htab);
65f38f15
AM
4558 return NULL;
4559 }
4560
721956f4 4561 /* Init the stub hash table too. */
66eb6687
AM
4562 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4563 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4564 {
d495ab0d 4565 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4566 return NULL;
4567 }
721956f4
AM
4568
4569 /* And the branch hash table. */
66eb6687
AM
4570 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4571 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4572 {
4573 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4574 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4575 return NULL;
4576 }
721956f4 4577
3b421ab3
AM
4578 htab->tocsave_htab = htab_try_create (1024,
4579 tocsave_htab_hash,
4580 tocsave_htab_eq,
4581 NULL);
4582 if (htab->tocsave_htab == NULL)
2915c55b 4583 {
d495ab0d 4584 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4585 return NULL;
4586 }
d495ab0d 4587 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4588
3254fd24
AM
4589 /* Initializing two fields of the union is just cosmetic. We really
4590 only care about glist, but when compiled on a 32-bit host the
4591 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4592 debugger inspection of these fields look nicer. */
a6aa5195
AM
4593 htab->elf.init_got_refcount.refcount = 0;
4594 htab->elf.init_got_refcount.glist = NULL;
4595 htab->elf.init_plt_refcount.refcount = 0;
4596 htab->elf.init_plt_refcount.glist = NULL;
4597 htab->elf.init_got_offset.offset = 0;
4598 htab->elf.init_got_offset.glist = NULL;
4599 htab->elf.init_plt_offset.offset = 0;
4600 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4601
65f38f15
AM
4602 return &htab->elf.root;
4603}
4604
bfeb4a28
AM
4605/* Create sections for linker generated code. */
4606
4607static bfd_boolean
4608create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4609{
4610 struct ppc_link_hash_table *htab;
4611 flagword flags;
4612
4613 htab = ppc_hash_table (info);
4614
bfeb4a28
AM
4615 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4616 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4617 if (htab->params->save_restore_funcs)
4618 {
4619 /* Create .sfpr for code to save and restore fp regs. */
4620 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4621 flags);
4622 if (htab->sfpr == NULL
4623 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4624 return FALSE;
4625 }
4626
4627 if (bfd_link_relocatable (info))
4628 return TRUE;
bfeb4a28
AM
4629
4630 /* Create .glink for lazy dynamic linking support. */
4631 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4632 flags);
4633 if (htab->glink == NULL
4634 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4635 return FALSE;
4636
9e390558
AM
4637 /* The part of .glink used by global entry stubs, separate so that
4638 it can be aligned appropriately without affecting htab->glink. */
4639 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4640 flags);
4641 if (htab->global_entry == NULL
4642 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4643 return FALSE;
4644
bfeb4a28
AM
4645 if (!info->no_ld_generated_unwind_info)
4646 {
4647 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4648 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4649 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4650 ".eh_frame",
4651 flags);
4652 if (htab->glink_eh_frame == NULL
4653 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4654 return FALSE;
4655 }
4656
4657 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4658 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4659 if (htab->elf.iplt == NULL
4660 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4661 return FALSE;
4662
4663 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4664 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4665 htab->elf.irelplt
4666 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4667 if (htab->elf.irelplt == NULL
4668 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4669 return FALSE;
4670
4671 /* Create branch lookup table for plt_branch stubs. */
4672 flags = (SEC_ALLOC | SEC_LOAD
4673 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4674 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4675 flags);
4676 if (htab->brlt == NULL
4677 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4678 return FALSE;
4679
2d7ad24e
AM
4680 /* Local plt entries, put in .branch_lt but a separate section for
4681 convenience. */
4682 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4683 flags);
4684 if (htab->pltlocal == NULL
4685 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4686 return FALSE;
4687
0e1862bb 4688 if (!bfd_link_pic (info))
bfeb4a28
AM
4689 return TRUE;
4690
4691 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4692 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
4693 htab->relbrlt
4694 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28
AM
4695 if (htab->relbrlt == NULL
4696 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4697 return FALSE;
4698
2d7ad24e
AM
4699 htab->relpltlocal
4700 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4701 if (htab->relpltlocal == NULL
4702 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4703 return FALSE;
4704
bfeb4a28
AM
4705 return TRUE;
4706}
4707
e717da7e
AM
4708/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4709
bfeb4a28 4710bfd_boolean
e7d1c40c
AM
4711ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4712 struct ppc64_elf_params *params)
e717da7e
AM
4713{
4714 struct ppc_link_hash_table *htab;
4715
e7d1c40c 4716 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4717
4718/* Always hook our dynamic sections into the first bfd, which is the
4719 linker created stub bfd. This ensures that the GOT header is at
4720 the start of the output TOC section. */
4721 htab = ppc_hash_table (info);
e7d1c40c
AM
4722 htab->elf.dynobj = params->stub_bfd;
4723 htab->params = params;
bfeb4a28 4724
bfeb4a28 4725 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4726}
4727
721956f4
AM
4728/* Build a name for an entry in the stub hash table. */
4729
4730static char *
4ce794b7
AM
4731ppc_stub_name (const asection *input_section,
4732 const asection *sym_sec,
4733 const struct ppc_link_hash_entry *h,
4734 const Elf_Internal_Rela *rel)
721956f4
AM
4735{
4736 char *stub_name;
bcaa2f82 4737 ssize_t len;
721956f4
AM
4738
4739 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4740 offsets from a sym as a branch target? In fact, we could
4741 probably assume the addend is always zero. */
4742 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4743
4744 if (h)
4745 {
4746 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4747 stub_name = bfd_malloc (len);
46de2a7c
AM
4748 if (stub_name == NULL)
4749 return stub_name;
4750
bcaa2f82
AM
4751 len = sprintf (stub_name, "%08x.%s+%x",
4752 input_section->id & 0xffffffff,
4753 h->elf.root.root.string,
4754 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4755 }
4756 else
4757 {
ad8e1ba5 4758 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4759 stub_name = bfd_malloc (len);
46de2a7c
AM
4760 if (stub_name == NULL)
4761 return stub_name;
4762
bcaa2f82
AM
4763 len = sprintf (stub_name, "%08x.%x:%x+%x",
4764 input_section->id & 0xffffffff,
4765 sym_sec->id & 0xffffffff,
4766 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4767 (int) rel->r_addend & 0xffffffff);
721956f4 4768 }
bcaa2f82 4769 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4770 stub_name[len - 2] = 0;
721956f4
AM
4771 return stub_name;
4772}
4773
4774/* Look up an entry in the stub hash. Stub entries are cached because
4775 creating the stub name takes a bit of time. */
4776
4777static struct ppc_stub_hash_entry *
4ce794b7
AM
4778ppc_get_stub_entry (const asection *input_section,
4779 const asection *sym_sec,
039b3fef 4780 struct ppc_link_hash_entry *h,
4ce794b7
AM
4781 const Elf_Internal_Rela *rel,
4782 struct ppc_link_hash_table *htab)
721956f4
AM
4783{
4784 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4785 struct map_stub *group;
721956f4
AM
4786
4787 /* If this input section is part of a group of sections sharing one
4788 stub section, then use the id of the first section in the group.
4789 Stub names need to include a section id, as there may well be
4790 more than one stub used to reach say, printf, and we need to
4791 distinguish between them. */
6f20ed8a 4792 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4793 if (group == NULL)
4794 return NULL;
721956f4 4795
b3fac117
AM
4796 if (h != NULL && h->u.stub_cache != NULL
4797 && h->u.stub_cache->h == h
6f20ed8a 4798 && h->u.stub_cache->group == group)
721956f4 4799 {
b3fac117 4800 stub_entry = h->u.stub_cache;
721956f4
AM
4801 }
4802 else
4803 {
4804 char *stub_name;
4805
6f20ed8a 4806 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4807 if (stub_name == NULL)
4808 return NULL;
4809
4810 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4811 stub_name, FALSE, FALSE);
721956f4 4812 if (h != NULL)
b3fac117 4813 h->u.stub_cache = stub_entry;
721956f4
AM
4814
4815 free (stub_name);
4816 }
4817
4818 return stub_entry;
4819}
4820
4821/* Add a new stub entry to the stub hash. Not all fields of the new
4822 stub entry are initialised. */
4823
4824static struct ppc_stub_hash_entry *
4ce794b7
AM
4825ppc_add_stub (const char *stub_name,
4826 asection *section,
25f53a85 4827 struct bfd_link_info *info)
721956f4 4828{
25f53a85 4829 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4830 struct map_stub *group;
721956f4
AM
4831 asection *link_sec;
4832 asection *stub_sec;
4833 struct ppc_stub_hash_entry *stub_entry;
4834
6f20ed8a
AM
4835 group = htab->sec_info[section->id].u.group;
4836 link_sec = group->link_sec;
4837 stub_sec = group->stub_sec;
721956f4
AM
4838 if (stub_sec == NULL)
4839 {
6f20ed8a
AM
4840 size_t namelen;
4841 bfd_size_type len;
4842 char *s_name;
721956f4 4843
6f20ed8a
AM
4844 namelen = strlen (link_sec->name);
4845 len = namelen + sizeof (STUB_SUFFIX);
4846 s_name = bfd_alloc (htab->params->stub_bfd, len);
4847 if (s_name == NULL)
4848 return NULL;
721956f4 4849
6f20ed8a
AM
4850 memcpy (s_name, link_sec->name, namelen);
4851 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4852 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4853 if (stub_sec == NULL)
4854 return NULL;
4855 group->stub_sec = stub_sec;
721956f4
AM
4856 }
4857
4858 /* Enter this entry into the linker stub hash table. */
4859 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4860 TRUE, FALSE);
721956f4
AM
4861 if (stub_entry == NULL)
4862 {
695344c0 4863 /* xgettext:c-format */
cf97bcb0
AM
4864 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4865 section->owner, stub_name);
721956f4
AM
4866 return NULL;
4867 }
4868
6f20ed8a 4869 stub_entry->group = group;
721956f4 4870 stub_entry->stub_offset = 0;
721956f4
AM
4871 return stub_entry;
4872}
4873
e717da7e
AM
4874/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4875 not already done. */
65f38f15 4876
b34976b6 4877static bfd_boolean
e717da7e 4878create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4879{
e717da7e
AM
4880 asection *got, *relgot;
4881 flagword flags;
4882 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4883
0c8d6e5c 4884 if (!is_ppc64_elf (abfd))
0ffa91dd 4885 return FALSE;
4dfe6ac6
NC
4886 if (htab == NULL)
4887 return FALSE;
0ffa91dd 4888
33e44f2e
AM
4889 if (!htab->elf.sgot
4890 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4891 return FALSE;
e717da7e
AM
4892
4893 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4894 | SEC_LINKER_CREATED);
4895
c456f082 4896 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4897 if (!got
e717da7e
AM
4898 || !bfd_set_section_alignment (abfd, got, 3))
4899 return FALSE;
65f38f15 4900
c456f082
AM
4901 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4902 flags | SEC_READONLY);
e717da7e 4903 if (!relgot
e717da7e 4904 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4905 return FALSE;
e717da7e
AM
4906
4907 ppc64_elf_tdata (abfd)->got = got;
4908 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4909 return TRUE;
65f38f15 4910}
5bd4f169 4911
b31867b6
AM
4912/* Follow indirect and warning symbol links. */
4913
4914static inline struct bfd_link_hash_entry *
4915follow_link (struct bfd_link_hash_entry *h)
4916{
4917 while (h->type == bfd_link_hash_indirect
4918 || h->type == bfd_link_hash_warning)
4919 h = h->u.i.link;
4920 return h;
4921}
4922
4923static inline struct elf_link_hash_entry *
4924elf_follow_link (struct elf_link_hash_entry *h)
4925{
4926 return (struct elf_link_hash_entry *) follow_link (&h->root);
4927}
4928
4929static inline struct ppc_link_hash_entry *
4930ppc_follow_link (struct ppc_link_hash_entry *h)
4931{
4932 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4933}
4934
40d16e0b
AM
4935/* Merge PLT info on FROM with that on TO. */
4936
4937static void
4938move_plt_plist (struct ppc_link_hash_entry *from,
4939 struct ppc_link_hash_entry *to)
4940{
4941 if (from->elf.plt.plist != NULL)
4942 {
4943 if (to->elf.plt.plist != NULL)
4944 {
4945 struct plt_entry **entp;
4946 struct plt_entry *ent;
4947
4948 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4949 {
4950 struct plt_entry *dent;
4951
4952 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4953 if (dent->addend == ent->addend)
4954 {
4955 dent->plt.refcount += ent->plt.refcount;
4956 *entp = ent->next;
4957 break;
4958 }
4959 if (dent == NULL)
4960 entp = &ent->next;
4961 }
4962 *entp = to->elf.plt.plist;
4963 }
4964
4965 to->elf.plt.plist = from->elf.plt.plist;
4966 from->elf.plt.plist = NULL;
4967 }
4968}
4969
65f38f15
AM
4970/* Copy the extra info we tack onto an elf_link_hash_entry. */
4971
4972static void
fcfa13d2
AM
4973ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4974 struct elf_link_hash_entry *dir,
4975 struct elf_link_hash_entry *ind)
65f38f15
AM
4976{
4977 struct ppc_link_hash_entry *edir, *eind;
4978
4979 edir = (struct ppc_link_hash_entry *) dir;
4980 eind = (struct ppc_link_hash_entry *) ind;
4981
c79d6685
AM
4982 edir->is_func |= eind->is_func;
4983 edir->is_func_descriptor |= eind->is_func_descriptor;
4984 edir->tls_mask |= eind->tls_mask;
4985 if (eind->oh != NULL)
4986 edir->oh = ppc_follow_link (eind->oh);
4987
474436e6 4988 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4989 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4990 edir->elf.ref_regular |= eind->elf.ref_regular;
4991 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4992 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4993 edir->elf.needs_plt |= eind->elf.needs_plt;
4994 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4995
d311bc8b
AM
4996 /* If we were called to copy over info for a weak sym, don't copy
4997 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4998 in order to simplify readonly_dynrelocs and save a field in the
4999 symbol hash entry, but that means dyn_relocs can't be used in any
5000 tests about a specific symbol, or affect other symbol flags which
ab2477e1 5001 are then tested. */
d311bc8b 5002 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 5003 return;
d311bc8b 5004
411e1bfb 5005 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 5006 if (eind->dyn_relocs != NULL)
65f38f15 5007 {
bbd7ec4a
AM
5008 if (edir->dyn_relocs != NULL)
5009 {
6061a67d
AM
5010 struct elf_dyn_relocs **pp;
5011 struct elf_dyn_relocs *p;
bbd7ec4a 5012
fcfa13d2 5013 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
5014 list. Merge any entries against the same section. */
5015 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5016 {
6061a67d 5017 struct elf_dyn_relocs *q;
bbd7ec4a
AM
5018
5019 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5020 if (q->sec == p->sec)
5021 {
5022 q->pc_count += p->pc_count;
5023 q->count += p->count;
5024 *pp = p->next;
5025 break;
5026 }
5027 if (q == NULL)
5028 pp = &p->next;
5029 }
5030 *pp = edir->dyn_relocs;
5031 }
5032
65f38f15
AM
5033 edir->dyn_relocs = eind->dyn_relocs;
5034 eind->dyn_relocs = NULL;
5035 }
65f38f15 5036
81848ca0
AM
5037 /* Copy over got entries that we may have already seen to the
5038 symbol which just became indirect. */
411e1bfb
AM
5039 if (eind->elf.got.glist != NULL)
5040 {
5041 if (edir->elf.got.glist != NULL)
5042 {
5043 struct got_entry **entp;
5044 struct got_entry *ent;
5045
5046 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
5047 {
5048 struct got_entry *dent;
5049
5050 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
5051 if (dent->addend == ent->addend
e717da7e 5052 && dent->owner == ent->owner
411e1bfb
AM
5053 && dent->tls_type == ent->tls_type)
5054 {
5055 dent->got.refcount += ent->got.refcount;
5056 *entp = ent->next;
5057 break;
5058 }
5059 if (dent == NULL)
5060 entp = &ent->next;
5061 }
5062 *entp = edir->elf.got.glist;
5063 }
5064
5065 edir->elf.got.glist = eind->elf.got.glist;
5066 eind->elf.got.glist = NULL;
5067 }
5068
5069 /* And plt entries. */
40d16e0b 5070 move_plt_plist (eind, edir);
411e1bfb 5071
fcfa13d2 5072 if (eind->elf.dynindx != -1)
411e1bfb 5073 {
fcfa13d2
AM
5074 if (edir->elf.dynindx != -1)
5075 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
5076 edir->elf.dynstr_index);
411e1bfb
AM
5077 edir->elf.dynindx = eind->elf.dynindx;
5078 edir->elf.dynstr_index = eind->elf.dynstr_index;
5079 eind->elf.dynindx = -1;
5080 eind->elf.dynstr_index = 0;
5081 }
411e1bfb
AM
5082}
5083
8387904d
AM
5084/* Find the function descriptor hash entry from the given function code
5085 hash entry FH. Link the entries via their OH fields. */
5086
5087static struct ppc_link_hash_entry *
b31867b6 5088lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
5089{
5090 struct ppc_link_hash_entry *fdh = fh->oh;
5091
5092 if (fdh == NULL)
5093 {
5094 const char *fd_name = fh->elf.root.root.string + 1;
5095
5096 fdh = (struct ppc_link_hash_entry *)
5097 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
5098 if (fdh == NULL)
5099 return fdh;
5100
5101 fdh->is_func_descriptor = 1;
5102 fdh->oh = fh;
5103 fh->is_func = 1;
5104 fh->oh = fdh;
8387904d
AM
5105 }
5106
8c5b4e52
AM
5107 fdh = ppc_follow_link (fdh);
5108 fdh->is_func_descriptor = 1;
5109 fdh->oh = fh;
5110 return fdh;
8387904d
AM
5111}
5112
8c5b4e52 5113/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
5114
5115static struct ppc_link_hash_entry *
5116make_fdh (struct bfd_link_info *info,
908b32fc 5117 struct ppc_link_hash_entry *fh)
bb700d78 5118{
8c5b4e52
AM
5119 bfd *abfd = fh->elf.root.u.undef.abfd;
5120 struct bfd_link_hash_entry *bh = NULL;
bb700d78 5121 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
5122 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5123 ? BSF_WEAK
5124 : BSF_GLOBAL);
5125
5126 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5127 fh->elf.root.root.string + 1,
5128 flags, bfd_und_section_ptr, 0,
5129 NULL, FALSE, FALSE, &bh))
bb700d78
AM
5130 return NULL;
5131
5132 fdh = (struct ppc_link_hash_entry *) bh;
5133 fdh->elf.non_elf = 0;
908b32fc
AM
5134 fdh->fake = 1;
5135 fdh->is_func_descriptor = 1;
5136 fdh->oh = fh;
5137 fh->is_func = 1;
5138 fh->oh = fdh;
bb700d78
AM
5139 return fdh;
5140}
5141
8387904d
AM
5142/* Fix function descriptor symbols defined in .opd sections to be
5143 function type. */
555cd476
AM
5144
5145static bfd_boolean
c16153ae 5146ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 5147 struct bfd_link_info *info,
555cd476 5148 Elf_Internal_Sym *isym,
6911b7dc 5149 const char **name,
555cd476
AM
5150 flagword *flags ATTRIBUTE_UNUSED,
5151 asection **sec,
b53dfeb2 5152 bfd_vma *value)
555cd476 5153{
b53dfeb2 5154 if (*sec != NULL
f1885d1e 5155 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
5156 {
5157 asection *code_sec;
5158
5159 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5160 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5161 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5162
5163 /* If the symbol is a function defined in .opd, and the function
5164 code is in a discarded group, let it appear to be undefined. */
0e1862bb 5165 if (!bfd_link_relocatable (info)
b53dfeb2
AM
5166 && (*sec)->reloc_count != 0
5167 && opd_entry_value (*sec, *value, &code_sec, NULL,
5168 FALSE) != (bfd_vma) -1
5169 && discarded_section (code_sec))
5170 {
5171 *sec = bfd_und_section_ptr;
5172 isym->st_shndx = SHN_UNDEF;
5173 }
5174 }
dbd1e97e
AM
5175 else if (*sec != NULL
5176 && strcmp ((*sec)->name, ".toc") == 0
5177 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5178 {
5179 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5180 if (htab != NULL)
5181 htab->params->object_in_toc = 1;
5182 }
433817dd 5183
6911b7dc
AM
5184 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5185 {
5186 if (abiversion (ibfd) == 0)
5187 set_abiversion (ibfd, 2);
5188 else if (abiversion (ibfd) == 1)
5189 {
cf97bcb0
AM
5190 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5191 " for ABI version 1"), *name);
6911b7dc
AM
5192 bfd_set_error (bfd_error_bad_value);
5193 return FALSE;
5194 }
5195 }
5196
555cd476
AM
5197 return TRUE;
5198}
5199
6911b7dc
AM
5200/* Merge non-visibility st_other attributes: local entry point. */
5201
5202static void
5203ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5204 const Elf_Internal_Sym *isym,
5205 bfd_boolean definition,
5206 bfd_boolean dynamic)
5207{
f378ab09 5208 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5209 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5210 | ELF_ST_VISIBILITY (h->other));
5211}
5212
8c5b4e52
AM
5213/* Hook called on merging a symbol. We use this to clear "fake" since
5214 we now have a real symbol. */
5215
5216static bfd_boolean
5217ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5218 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5219 asection **psec ATTRIBUTE_UNUSED,
5220 bfd_boolean newdef ATTRIBUTE_UNUSED,
5221 bfd_boolean olddef ATTRIBUTE_UNUSED,
5222 bfd *oldbfd ATTRIBUTE_UNUSED,
5223 const asection *oldsec ATTRIBUTE_UNUSED)
5224{
5225 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5226 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5227 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5228 return TRUE;
5229}
5230
8387904d 5231/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5232 inclusion of a new ABI object archive that defines "bar".
5233 NAME is a symbol defined in an archive. Return a symbol in the hash
5234 table that might be satisfied by the archive symbols. */
8387904d
AM
5235
5236static struct elf_link_hash_entry *
5237ppc64_elf_archive_symbol_lookup (bfd *abfd,
5238 struct bfd_link_info *info,
5239 const char *name)
5240{
5241 struct elf_link_hash_entry *h;
5242 char *dot_name;
5243 size_t len;
5244
5245 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5246 if (h != NULL
5247 /* Don't return this sym if it is a fake function descriptor
5248 created by add_symbol_adjust. */
8c5b4e52 5249 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5250 return h;
5251
5252 if (name[0] == '.')
5253 return h;
5254
5255 len = strlen (name);
5256 dot_name = bfd_alloc (abfd, len + 2);
5257 if (dot_name == NULL)
e99955cd 5258 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5259 dot_name[0] = '.';
5260 memcpy (dot_name + 1, name, len + 1);
5261 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5262 bfd_release (abfd, dot_name);
5263 return h;
5264}
5265
5266/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5267 new ABI object defines "bar". Well, at least, undefined dot symbols
5268 are made weak. This stops later archive searches from including an
5269 object if we already have a function descriptor definition. It also
35b0ce59
AM
5270 prevents the linker complaining about undefined symbols.
5271 We also check and correct mismatched symbol visibility here. The
5272 most restrictive visibility of the function descriptor and the
5273 function entry symbol is used. */
8387904d
AM
5274
5275static bfd_boolean
b3fac117 5276add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5277{
8387904d
AM
5278 struct ppc_link_hash_table *htab;
5279 struct ppc_link_hash_entry *fdh;
5280
b3fac117
AM
5281 if (eh->elf.root.type == bfd_link_hash_warning)
5282 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5283
8400d40d
AM
5284 if (eh->elf.root.type == bfd_link_hash_indirect)
5285 return TRUE;
5286
b3fac117
AM
5287 if (eh->elf.root.root.string[0] != '.')
5288 abort ();
8387904d 5289
b3fac117 5290 htab = ppc_hash_table (info);
4dfe6ac6
NC
5291 if (htab == NULL)
5292 return FALSE;
5293
b31867b6 5294 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5295 if (fdh == NULL
5296 && !bfd_link_relocatable (info)
5297 && (eh->elf.root.type == bfd_link_hash_undefined
5298 || eh->elf.root.type == bfd_link_hash_undefweak)
5299 && eh->elf.ref_regular)
5300 {
5301 /* Make an undefined function descriptor sym, in order to
5302 pull in an --as-needed shared lib. Archives are handled
5303 elsewhere. */
5304 fdh = make_fdh (info, eh);
5305 if (fdh == NULL)
5306 return FALSE;
bb700d78 5307 }
8c5b4e52
AM
5308
5309 if (fdh != NULL)
8387904d 5310 {
35b0ce59
AM
5311 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5312 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5313
5314 /* Make both descriptor and entry symbol have the most
5315 constraining visibility of either symbol. */
35b0ce59
AM
5316 if (entry_vis < descr_vis)
5317 fdh->elf.other += entry_vis - descr_vis;
5318 else if (entry_vis > descr_vis)
5319 eh->elf.other += descr_vis - entry_vis;
5320
8c5b4e52
AM
5321 /* Propagate reference flags from entry symbol to function
5322 descriptor symbol. */
bc4e12de 5323 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5324 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5325 fdh->elf.ref_regular |= eh->elf.ref_regular;
5326 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5327
5328 if (!fdh->elf.forced_local
5329 && fdh->elf.dynindx == -1
5330 && fdh->elf.versioned != versioned_hidden
5331 && (bfd_link_dll (info)
5332 || fdh->elf.def_dynamic
5333 || fdh->elf.ref_dynamic)
5334 && (eh->elf.ref_regular
5335 || eh->elf.def_regular))
5336 {
5337 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5338 return FALSE;
35b0ce59 5339 }
8387904d 5340 }
99877b66 5341
8387904d
AM
5342 return TRUE;
5343}
5344
f6c7c3e8
AM
5345/* Set up opd section info and abiversion for IBFD, and process list
5346 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5347
8387904d 5348static bfd_boolean
f6c7c3e8 5349ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5350{
99877b66 5351 struct ppc_link_hash_table *htab;
b3fac117 5352 struct ppc_link_hash_entry **p, *eh;
459609d6 5353 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5354
459609d6 5355 if (opd != NULL && opd->size != 0)
b3fac117 5356 {
b9399fcf
AM
5357 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5358 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5359
459609d6
AM
5360 if (abiversion (ibfd) == 0)
5361 set_abiversion (ibfd, 1);
8a2058b5 5362 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5363 {
695344c0 5364 /* xgettext:c-format */
cf97bcb0
AM
5365 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5366 ibfd, abiversion (ibfd));
459609d6
AM
5367 bfd_set_error (bfd_error_bad_value);
5368 return FALSE;
f6c7c3e8 5369 }
b9399fcf 5370 }
f6c7c3e8 5371
b9399fcf
AM
5372 if (is_ppc64_elf (info->output_bfd))
5373 {
5374 /* For input files without an explicit abiversion in e_flags
5375 we should have flagged any with symbol st_other bits set
5376 as ELFv1 and above flagged those with .opd as ELFv2.
5377 Set the output abiversion if not yet set, and for any input
5378 still ambiguous, take its abiversion from the output.
5379 Differences in ABI are reported later. */
5380 if (abiversion (info->output_bfd) == 0)
5381 set_abiversion (info->output_bfd, abiversion (ibfd));
5382 else if (abiversion (ibfd) == 0)
5383 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5384 }
5385
459609d6
AM
5386 htab = ppc_hash_table (info);
5387 if (htab == NULL)
b9399fcf 5388 return TRUE;
459609d6 5389
b9399fcf
AM
5390 if (opd != NULL && opd->size != 0
5391 && (ibfd->flags & DYNAMIC) == 0
5392 && (opd->flags & SEC_RELOC) != 0
5393 && opd->reloc_count != 0
5394 && !bfd_is_abs_section (opd->output_section)
5395 && info->gc_sections)
5396 {
5397 /* Garbage collection needs some extra help with .opd sections.
5398 We don't want to necessarily keep everything referenced by
5399 relocs in .opd, as that would keep all functions. Instead,
5400 if we reference an .opd symbol (a function descriptor), we
5401 want to keep the function code symbol's section. This is
5402 easy for global symbols, but for local syms we need to keep
5403 information about the associated function section. */
5404 bfd_size_type amt;
5405 asection **opd_sym_map;
5406 Elf_Internal_Shdr *symtab_hdr;
5407 Elf_Internal_Rela *relocs, *rel_end, *rel;
5408
5409 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5410 opd_sym_map = bfd_zalloc (ibfd, amt);
5411 if (opd_sym_map == NULL)
5412 return FALSE;
5413 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5414 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5415 info->keep_memory);
5416 if (relocs == NULL)
5417 return FALSE;
5418 symtab_hdr = &elf_symtab_hdr (ibfd);
5419 rel_end = relocs + opd->reloc_count - 1;
5420 for (rel = relocs; rel < rel_end; rel++)
5421 {
5422 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5423 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5424
5425 if (r_type == R_PPC64_ADDR64
5426 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5427 && r_symndx < symtab_hdr->sh_info)
5428 {
5429 Elf_Internal_Sym *isym;
5430 asection *s;
5431
5432 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5433 if (isym == NULL)
5434 {
5435 if (elf_section_data (opd)->relocs != relocs)
5436 free (relocs);
5437 return FALSE;
5438 }
5439
5440 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5441 if (s != NULL && s != opd)
5442 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5443 }
5444 }
5445 if (elf_section_data (opd)->relocs != relocs)
5446 free (relocs);
5447 }
459609d6
AM
5448
5449 p = &htab->dot_syms;
5450 while ((eh = *p) != NULL)
5451 {
5452 *p = NULL;
5453 if (&eh->elf == htab->elf.hgot)
5454 ;
5455 else if (htab->elf.hgot == NULL
5456 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5457 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5458 else if (abiversion (ibfd) <= 1)
5459 {
5460 htab->need_func_desc_adj = 1;
5461 if (!add_symbol_adjust (eh, info))
5462 return FALSE;
5463 }
459609d6
AM
5464 p = &eh->u.next_dot_sym;
5465 }
b3fac117 5466 return TRUE;
8387904d
AM
5467}
5468
97fed1c9
JJ
5469/* Undo hash table changes when an --as-needed input file is determined
5470 not to be needed. */
5471
5472static bfd_boolean
e5034e59
AM
5473ppc64_elf_notice_as_needed (bfd *ibfd,
5474 struct bfd_link_info *info,
5475 enum notice_asneeded_action act)
97fed1c9 5476{
e5034e59
AM
5477 if (act == notice_not_needed)
5478 {
5479 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5480
e5034e59
AM
5481 if (htab == NULL)
5482 return FALSE;
4dfe6ac6 5483
e5034e59
AM
5484 htab->dot_syms = NULL;
5485 }
5486 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5487}
5488
aa374f67
AM
5489/* If --just-symbols against a final linked binary, then assume we need
5490 toc adjusting stubs when calling functions defined there. */
5491
5492static void
5493ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5494{
5495 if ((sec->flags & SEC_CODE) != 0
5496 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5497 && is_ppc64_elf (sec->owner))
5498 {
2c3f079f
AM
5499 if (abiversion (sec->owner) >= 2
5500 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5501 sec->has_toc_reloc = 1;
5502 }
5503 _bfd_elf_link_just_syms (sec, info);
5504}
5505
e054468f 5506static struct plt_entry **
4ce794b7
AM
5507update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5508 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5509{
5510 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5511 struct plt_entry **local_plt;
f961d9dd 5512 unsigned char *local_got_tls_masks;
411e1bfb
AM
5513
5514 if (local_got_ents == NULL)
5515 {
5516 bfd_size_type size = symtab_hdr->sh_info;
5517
e054468f
AM
5518 size *= (sizeof (*local_got_ents)
5519 + sizeof (*local_plt)
5520 + sizeof (*local_got_tls_masks));
4ce794b7 5521 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5522 if (local_got_ents == NULL)
e054468f 5523 return NULL;
411e1bfb
AM
5524 elf_local_got_ents (abfd) = local_got_ents;
5525 }
5526
37da22e5 5527 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5528 {
5529 struct got_entry *ent;
5530
5531 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5532 if (ent->addend == r_addend
5533 && ent->owner == abfd
5534 && ent->tls_type == tls_type)
411e1bfb
AM
5535 break;
5536 if (ent == NULL)
5537 {
5538 bfd_size_type amt = sizeof (*ent);
4ce794b7 5539 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5540 if (ent == NULL)
5541 return FALSE;
5542 ent->next = local_got_ents[r_symndx];
5543 ent->addend = r_addend;
e717da7e 5544 ent->owner = abfd;
411e1bfb 5545 ent->tls_type = tls_type;
927be08e 5546 ent->is_indirect = FALSE;
411e1bfb
AM
5547 ent->got.refcount = 0;
5548 local_got_ents[r_symndx] = ent;
5549 }
5550 ent->got.refcount += 1;
5551 }
5552
e054468f 5553 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5554 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 5555 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
5556
5557 return local_plt + r_symndx;
65f38f15
AM
5558}
5559
411e1bfb 5560static bfd_boolean
e054468f 5561update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5562{
411e1bfb 5563 struct plt_entry *ent;
1e2f5b6e 5564
e054468f 5565 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5566 if (ent->addend == addend)
5567 break;
5568 if (ent == NULL)
1e2f5b6e 5569 {
411e1bfb 5570 bfd_size_type amt = sizeof (*ent);
4ce794b7 5571 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5572 if (ent == NULL)
5573 return FALSE;
e054468f 5574 ent->next = *plist;
411e1bfb
AM
5575 ent->addend = addend;
5576 ent->plt.refcount = 0;
e054468f 5577 *plist = ent;
1e2f5b6e 5578 }
411e1bfb 5579 ent->plt.refcount += 1;
b34976b6 5580 return TRUE;
1e2f5b6e
AM
5581}
5582
e054468f
AM
5583static bfd_boolean
5584is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5585{
5586 return (r_type == R_PPC64_REL24
05d0e962 5587 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
5588 || r_type == R_PPC64_REL14
5589 || r_type == R_PPC64_REL14_BRTAKEN
5590 || r_type == R_PPC64_REL14_BRNTAKEN
5591 || r_type == R_PPC64_ADDR24
5592 || r_type == R_PPC64_ADDR14
5593 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d
AM
5594 || r_type == R_PPC64_ADDR14_BRNTAKEN
5595 || r_type == R_PPC64_PLTCALL);
5596}
5597
5598/* Relocs on inline plt call sequence insns prior to the call. */
5599
5600static bfd_boolean
5601is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5602{
5603 return (r_type == R_PPC64_PLT16_HA
5604 || r_type == R_PPC64_PLT16_HI
5605 || r_type == R_PPC64_PLT16_LO
5606 || r_type == R_PPC64_PLT16_LO_DS
5607 || r_type == R_PPC64_PLTSEQ);
e054468f
AM
5608}
5609
5bd4f169 5610/* Look through the relocs for a section during the first phase, and
65f38f15 5611 calculate needed space in the global offset table, procedure
5d1634d7 5612 linkage table, and dynamic reloc sections. */
5bd4f169 5613
b34976b6 5614static bfd_boolean
4ce794b7
AM
5615ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5616 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5617{
65f38f15 5618 struct ppc_link_hash_table *htab;
5bd4f169 5619 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5620 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5621 const Elf_Internal_Rela *rel;
5622 const Elf_Internal_Rela *rel_end;
5bd4f169 5623 asection *sreloc;
3a71aa26 5624 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5625 bfd_boolean is_opd;
5bd4f169 5626
0e1862bb 5627 if (bfd_link_relocatable (info))
b34976b6 5628 return TRUE;
5bd4f169 5629
680a3378
AM
5630 /* Don't do anything special with non-loaded, non-alloced sections.
5631 In particular, any relocs in such sections should not affect GOT
5632 and PLT reference counting (ie. we don't allow them to create GOT
5633 or PLT entries), there's no possibility or desire to optimize TLS
5634 relocs, and there's not much point in propagating relocs to shared
5635 libs that the dynamic linker won't relocate. */
5636 if ((sec->flags & SEC_ALLOC) == 0)
5637 return TRUE;
5638
0c8d6e5c 5639 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5640
65f38f15 5641 htab = ppc_hash_table (info);
4dfe6ac6
NC
5642 if (htab == NULL)
5643 return FALSE;
5644
3a71aa26
AM
5645 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5646 FALSE, FALSE, TRUE);
5647 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5648 FALSE, FALSE, TRUE);
0ffa91dd 5649 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5650 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5651 sreloc = NULL;
b9399fcf 5652 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5653 rel_end = relocs + sec->reloc_count;
5654 for (rel = relocs; rel < rel_end; rel++)
5655 {
5656 unsigned long r_symndx;
5657 struct elf_link_hash_entry *h;
04c9666a 5658 enum elf_ppc64_reloc_type r_type;
727fc41e 5659 int tls_type;
7c8fe5c4 5660 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5661 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5662
5663 r_symndx = ELF64_R_SYM (rel->r_info);
5664 if (r_symndx < symtab_hdr->sh_info)
5665 h = NULL;
5666 else
973a3492
L
5667 {
5668 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5669 h = elf_follow_link (h);
1c865ab2
AM
5670
5671 if (h == htab->elf.hgot)
5672 sec->has_toc_reloc = 1;
973a3492 5673 }
5bd4f169 5674
727fc41e 5675 tls_type = 0;
e054468f 5676 ifunc = NULL;
25f23106
AM
5677 if (h != NULL)
5678 {
5679 if (h->type == STT_GNU_IFUNC)
5680 {
5681 h->needs_plt = 1;
5682 ifunc = &h->plt.plist;
5683 }
5684 }
5685 else
5686 {
5687 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5688 abfd, r_symndx);
5689 if (isym == NULL)
5690 return FALSE;
5691
5692 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5693 {
5694 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5
AM
5695 rel->r_addend,
5696 NON_GOT | PLT_IFUNC);
25f23106
AM
5697 if (ifunc == NULL)
5698 return FALSE;
5699 }
5700 }
727fc41e 5701
cbf95972 5702 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5703 switch (r_type)
5bd4f169 5704 {
727fc41e
AM
5705 case R_PPC64_TLSGD:
5706 case R_PPC64_TLSLD:
5707 /* These special tls relocs tie a call to __tls_get_addr with
5708 its parameter symbol. */
37da22e5
AM
5709 if (h != NULL)
5710 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5711 else
5712 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5713 rel->r_addend,
5714 NON_GOT | TLS_TLS | TLS_MARK))
5715 return FALSE;
5716 sec->has_tls_reloc = 1;
727fc41e
AM
5717 break;
5718
411e1bfb
AM
5719 case R_PPC64_GOT_TLSLD16:
5720 case R_PPC64_GOT_TLSLD16_LO:
5721 case R_PPC64_GOT_TLSLD16_HI:
5722 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5723 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5724 goto dogottls;
5725
5726 case R_PPC64_GOT_TLSGD16:
5727 case R_PPC64_GOT_TLSGD16_LO:
5728 case R_PPC64_GOT_TLSGD16_HI:
5729 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5730 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5731 goto dogottls;
5732
5733 case R_PPC64_GOT_TPREL16_DS:
5734 case R_PPC64_GOT_TPREL16_LO_DS:
5735 case R_PPC64_GOT_TPREL16_HI:
5736 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5737 if (bfd_link_dll (info))
411e1bfb
AM
5738 info->flags |= DF_STATIC_TLS;
5739 tls_type = TLS_TLS | TLS_TPREL;
5740 goto dogottls;
5741
5742 case R_PPC64_GOT_DTPREL16_DS:
5743 case R_PPC64_GOT_DTPREL16_LO_DS:
5744 case R_PPC64_GOT_DTPREL16_HI:
5745 case R_PPC64_GOT_DTPREL16_HA:
5746 tls_type = TLS_TLS | TLS_DTPREL;
5747 dogottls:
5748 sec->has_tls_reloc = 1;
1a0670f3 5749 /* Fall through */
411e1bfb 5750
5bd4f169 5751 case R_PPC64_GOT16:
5bd4f169 5752 case R_PPC64_GOT16_DS:
65f38f15
AM
5753 case R_PPC64_GOT16_HA:
5754 case R_PPC64_GOT16_HI:
5755 case R_PPC64_GOT16_LO:
5bd4f169 5756 case R_PPC64_GOT16_LO_DS:
65f38f15 5757 /* This symbol requires a global offset table entry. */
4c52953f 5758 sec->has_toc_reloc = 1;
33c0ec9d
AM
5759 if (r_type == R_PPC64_GOT_TLSLD16
5760 || r_type == R_PPC64_GOT_TLSGD16
5761 || r_type == R_PPC64_GOT_TPREL16_DS
5762 || r_type == R_PPC64_GOT_DTPREL16_DS
5763 || r_type == R_PPC64_GOT16
5764 || r_type == R_PPC64_GOT16_DS)
5765 {
5766 htab->do_multi_toc = 1;
d77c8a4b 5767 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5768 }
5769
e717da7e
AM
5770 if (ppc64_elf_tdata (abfd)->got == NULL
5771 && !create_got_section (abfd, info))
b34976b6 5772 return FALSE;
5bd4f169
AM
5773
5774 if (h != NULL)
5775 {
411e1bfb
AM
5776 struct ppc_link_hash_entry *eh;
5777 struct got_entry *ent;
65f38f15 5778
411e1bfb
AM
5779 eh = (struct ppc_link_hash_entry *) h;
5780 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5781 if (ent->addend == rel->r_addend
e717da7e 5782 && ent->owner == abfd
411e1bfb
AM
5783 && ent->tls_type == tls_type)
5784 break;
5785 if (ent == NULL)
5bd4f169 5786 {
411e1bfb 5787 bfd_size_type amt = sizeof (*ent);
4ce794b7 5788 ent = bfd_alloc (abfd, amt);
411e1bfb 5789 if (ent == NULL)
b34976b6 5790 return FALSE;
411e1bfb
AM
5791 ent->next = eh->elf.got.glist;
5792 ent->addend = rel->r_addend;
e717da7e 5793 ent->owner = abfd;
411e1bfb 5794 ent->tls_type = tls_type;
927be08e 5795 ent->is_indirect = FALSE;
411e1bfb
AM
5796 ent->got.refcount = 0;
5797 eh->elf.got.glist = ent;
5bd4f169 5798 }
411e1bfb 5799 ent->got.refcount += 1;
e7b938ca 5800 eh->tls_mask |= tls_type;
5bd4f169 5801 }
411e1bfb
AM
5802 else
5803 /* This is a global offset table entry for a local symbol. */
5804 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5805 rel->r_addend, tls_type))
5806 return FALSE;
a345bc8d
AM
5807
5808 /* We may also need a plt entry if the symbol turns out to be
5809 an ifunc. */
0e1862bb 5810 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5811 {
5812 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5813 return FALSE;
5814 }
5bd4f169
AM
5815 break;
5816
5bd4f169 5817 case R_PPC64_PLT16_HA:
65f38f15
AM
5818 case R_PPC64_PLT16_HI:
5819 case R_PPC64_PLT16_LO:
08be3224 5820 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
5821 case R_PPC64_PLT32:
5822 case R_PPC64_PLT64:
cbf95972
AM
5823 /* This symbol requires a procedure linkage table entry. */
5824 plt_list = ifunc;
5825 if (h != NULL)
e054468f 5826 {
e054468f
AM
5827 h->needs_plt = 1;
5828 if (h->root.root.string[0] == '.'
5829 && h->root.root.string[1] != '\0')
5830 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 5831 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5832 plt_list = &h->plt.plist;
5833 }
5834 if (plt_list == NULL)
2d7ad24e
AM
5835 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5836 rel->r_addend,
5837 NON_GOT | PLT_KEEP);
cbf95972
AM
5838 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5839 return FALSE;
5bd4f169
AM
5840 break;
5841
5842 /* The following relocations don't need to propagate the
5843 relocation if linking a shared object since they are
5844 section relative. */
5845 case R_PPC64_SECTOFF:
5846 case R_PPC64_SECTOFF_LO:
5847 case R_PPC64_SECTOFF_HI:
5848 case R_PPC64_SECTOFF_HA:
5849 case R_PPC64_SECTOFF_DS:
5850 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5851 case R_PPC64_DTPREL16:
5852 case R_PPC64_DTPREL16_LO:
5853 case R_PPC64_DTPREL16_HI:
5854 case R_PPC64_DTPREL16_HA:
5855 case R_PPC64_DTPREL16_DS:
5856 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5857 case R_PPC64_DTPREL16_HIGH:
5858 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5859 case R_PPC64_DTPREL16_HIGHER:
5860 case R_PPC64_DTPREL16_HIGHERA:
5861 case R_PPC64_DTPREL16_HIGHEST:
5862 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5863 break;
5864
ad8e1ba5 5865 /* Nor do these. */
25f23106
AM
5866 case R_PPC64_REL16:
5867 case R_PPC64_REL16_LO:
5868 case R_PPC64_REL16_HI:
5869 case R_PPC64_REL16_HA:
a680de9a 5870 case R_PPC64_REL16DX_HA:
25f23106
AM
5871 break;
5872
45965137
AM
5873 /* Not supported as a dynamic relocation. */
5874 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5875 if (bfd_link_pic (info))
45965137
AM
5876 {
5877 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5878 ppc_howto_init ();
695344c0 5879 /* xgettext:c-format */
174d0a74 5880 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5881 "in shared libraries and PIEs\n"),
45965137
AM
5882 abfd, sec, rel->r_offset,
5883 ppc64_elf_howto_table[r_type]->name);
5884 bfd_set_error (bfd_error_bad_value);
5885 return FALSE;
5886 }
5887 break;
5888
ad8e1ba5 5889 case R_PPC64_TOC16:
33c0ec9d
AM
5890 case R_PPC64_TOC16_DS:
5891 htab->do_multi_toc = 1;
d77c8a4b 5892 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5893 /* Fall through. */
ad8e1ba5
AM
5894 case R_PPC64_TOC16_LO:
5895 case R_PPC64_TOC16_HI:
5896 case R_PPC64_TOC16_HA:
ad8e1ba5 5897 case R_PPC64_TOC16_LO_DS:
4c52953f 5898 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5899 break;
5900
006589cf
AM
5901 /* Marker reloc. */
5902 case R_PPC64_ENTRY:
5903 break;
5904
5bd4f169
AM
5905 /* This relocation describes the C++ object vtable hierarchy.
5906 Reconstruct it for later use during GC. */
5907 case R_PPC64_GNU_VTINHERIT:
c152c796 5908 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5909 return FALSE;
5bd4f169
AM
5910 break;
5911
5912 /* This relocation describes which C++ vtable entries are actually
5913 used. Record for later use during GC. */
5914 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5915 BFD_ASSERT (h != NULL);
5916 if (h != NULL
5917 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5918 return FALSE;
5bd4f169
AM
5919 break;
5920
721956f4
AM
5921 case R_PPC64_REL14:
5922 case R_PPC64_REL14_BRTAKEN:
5923 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5924 {
5925 asection *dest = NULL;
5926
5927 /* Heuristic: If jumping outside our section, chances are
5928 we are going to need a stub. */
5929 if (h != NULL)
5930 {
5931 /* If the sym is weak it may be overridden later, so
5932 don't assume we know where a weak sym lives. */
5933 if (h->root.type == bfd_link_hash_defined)
5934 dest = h->root.u.def.section;
5935 }
5936 else
87d72d41
AM
5937 {
5938 Elf_Internal_Sym *isym;
5939
5940 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5941 abfd, r_symndx);
5942 if (isym == NULL)
5943 return FALSE;
5944
5945 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5946 }
5947
220c76dd 5948 if (dest != sec)
7c8fe5c4 5949 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5950 }
3e04d765
AM
5951 goto rel24;
5952
5953 case R_PPC64_PLTCALL:
5954 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5955 /* Fall through. */
5956
5d1634d7 5957 case R_PPC64_REL24:
05d0e962 5958 case R_PPC64_REL24_NOTOC:
3e04d765 5959 rel24:
cbf95972
AM
5960 plt_list = ifunc;
5961 if (h != NULL)
5d1634d7 5962 {
e054468f
AM
5963 h->needs_plt = 1;
5964 if (h->root.root.string[0] == '.'
5965 && h->root.root.string[1] != '\0')
5966 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5967
3a71aa26 5968 if (h == tga || h == dottga)
cbf95972
AM
5969 {
5970 sec->has_tls_reloc = 1;
5971 if (rel != relocs
5972 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5973 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5974 /* We have a new-style __tls_get_addr call with
5975 a marker reloc. */
5976 ;
5977 else
5978 /* Mark this section as having an old-style call. */
5979 sec->has_tls_get_addr_call = 1;
5980 }
5981 plt_list = &h->plt.plist;
411e1bfb 5982 }
cbf95972
AM
5983
5984 /* We may need a .plt entry if the function this reloc
5985 refers to is in a shared lib. */
5986 if (plt_list
5987 && !update_plt_info (abfd, plt_list, rel->r_addend))
5988 return FALSE;
411e1bfb
AM
5989 break;
5990
cbf95972
AM
5991 case R_PPC64_ADDR14:
5992 case R_PPC64_ADDR14_BRNTAKEN:
5993 case R_PPC64_ADDR14_BRTAKEN:
5994 case R_PPC64_ADDR24:
5995 goto dodyn;
5996
411e1bfb
AM
5997 case R_PPC64_TPREL64:
5998 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5999 if (bfd_link_dll (info))
411e1bfb
AM
6000 info->flags |= DF_STATIC_TLS;
6001 goto dotlstoc;
6002
6003 case R_PPC64_DTPMOD64:
6004 if (rel + 1 < rel_end
6005 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
6006 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 6007 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 6008 else
951fd09b 6009 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
6010 goto dotlstoc;
6011
6012 case R_PPC64_DTPREL64:
6013 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
6014 if (rel != relocs
6015 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
6016 && rel[-1].r_offset == rel->r_offset - 8)
6017 /* This is the second reloc of a dtpmod, dtprel pair.
6018 Don't mark with TLS_DTPREL. */
6019 goto dodyn;
6020
6021 dotlstoc:
6022 sec->has_tls_reloc = 1;
6023 if (h != NULL)
6024 {
6025 struct ppc_link_hash_entry *eh;
6026 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6027 eh->tls_mask |= tls_type;
411e1bfb
AM
6028 }
6029 else
6030 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
6031 rel->r_addend, tls_type))
6032 return FALSE;
6033
7c8fe5c4
AM
6034 ppc64_sec = ppc64_elf_section_data (sec);
6035 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 6036 {
3a71aa26
AM
6037 bfd_size_type amt;
6038
e7b938ca 6039 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
6040 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
6041 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
6042 if (ppc64_sec->u.toc.symndx == NULL)
6043 return FALSE;
6044 amt = sec->size * sizeof (bfd_vma) / 8;
6045 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
6046 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 6047 return FALSE;
7c8fe5c4
AM
6048 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
6049 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
6050 }
6051 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
6052 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
6053 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
6054
6055 /* Mark the second slot of a GD or LD entry.
6056 -1 to indicate GD and -2 to indicate LD. */
6057 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 6058 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 6059 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 6060 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
6061 goto dodyn;
6062
6063 case R_PPC64_TPREL16:
6064 case R_PPC64_TPREL16_LO:
6065 case R_PPC64_TPREL16_HI:
6066 case R_PPC64_TPREL16_HA:
6067 case R_PPC64_TPREL16_DS:
6068 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6069 case R_PPC64_TPREL16_HIGH:
6070 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
6071 case R_PPC64_TPREL16_HIGHER:
6072 case R_PPC64_TPREL16_HIGHERA:
6073 case R_PPC64_TPREL16_HIGHEST:
6074 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
6075 if (bfd_link_dll (info))
6076 info->flags |= DF_STATIC_TLS;
6077 goto dodyn;
5d1634d7 6078
e86ce104 6079 case R_PPC64_ADDR64:
b9399fcf 6080 if (is_opd
1e2f5b6e 6081 && rel + 1 < rel_end
4ce794b7 6082 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 6083 {
8387904d 6084 if (h != NULL)
8c5b4e52 6085 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 6086 }
e86ce104
AM
6087 /* Fall through. */
6088
65f38f15
AM
6089 case R_PPC64_ADDR16:
6090 case R_PPC64_ADDR16_DS:
6091 case R_PPC64_ADDR16_HA:
6092 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6093 case R_PPC64_ADDR16_HIGH:
6094 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
6095 case R_PPC64_ADDR16_HIGHER:
6096 case R_PPC64_ADDR16_HIGHERA:
6097 case R_PPC64_ADDR16_HIGHEST:
6098 case R_PPC64_ADDR16_HIGHESTA:
6099 case R_PPC64_ADDR16_LO:
6100 case R_PPC64_ADDR16_LO_DS:
0e1862bb 6101 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
6102 && rel->r_addend == 0)
6103 {
6104 /* We may need a .plt entry if this reloc refers to a
6105 function in a shared lib. */
6106 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
6107 return FALSE;
6108 h->pointer_equality_needed = 1;
6109 }
6110 /* Fall through. */
6111
6112 case R_PPC64_REL30:
6113 case R_PPC64_REL32:
6114 case R_PPC64_REL64:
65f38f15 6115 case R_PPC64_ADDR32:
65f38f15
AM
6116 case R_PPC64_UADDR16:
6117 case R_PPC64_UADDR32:
6118 case R_PPC64_UADDR64:
5bd4f169 6119 case R_PPC64_TOC:
0e1862bb 6120 if (h != NULL && !bfd_link_pic (info))
81848ca0 6121 /* We may need a copy reloc. */
f5385ebf 6122 h->non_got_ref = 1;
81848ca0 6123
41bd81ab 6124 /* Don't propagate .opd relocs. */
b9399fcf 6125 if (NO_OPD_RELOCS && is_opd)
e86ce104 6126 break;
e86ce104 6127
65f38f15
AM
6128 /* If we are creating a shared library, and this is a reloc
6129 against a global symbol, or a non PC relative reloc
6130 against a local symbol, then we need to copy the reloc
6131 into the shared library. However, if we are linking with
6132 -Bsymbolic, we do not need to copy a reloc against a
6133 global symbol which is defined in an object we are
6134 including in the link (i.e., DEF_REGULAR is set). At
6135 this point we have not seen all the input files, so it is
6136 possible that DEF_REGULAR is not set now but will be set
6137 later (it is never cleared). In case of a weak definition,
6138 DEF_REGULAR may be cleared later by a strong definition in
6139 a shared library. We account for that possibility below by
f4656909 6140 storing information in the dyn_relocs field of the hash
65f38f15
AM
6141 table entry. A similar situation occurs when creating
6142 shared libraries and symbol visibility changes render the
6143 symbol local.
6144
6145 If on the other hand, we are creating an executable, we
6146 may need to keep relocations for symbols satisfied by a
6147 dynamic library if we manage to avoid copy relocs for the
6148 symbol. */
411e1bfb 6149 dodyn:
0e1862bb 6150 if ((bfd_link_pic (info)
1d483afe 6151 && (must_be_dyn_reloc (info, r_type)
65f38f15 6152 || (h != NULL
198f1157 6153 && (!SYMBOLIC_BIND (info, h)
65f38f15 6154 || h->root.type == bfd_link_hash_defweak
f5385ebf 6155 || !h->def_regular))))
f4656909 6156 || (ELIMINATE_COPY_RELOCS
0e1862bb 6157 && !bfd_link_pic (info)
65f38f15
AM
6158 && h != NULL
6159 && (h->root.type == bfd_link_hash_defweak
25f23106 6160 || !h->def_regular))
0e1862bb 6161 || (!bfd_link_pic (info)
25f23106 6162 && ifunc != NULL))
5bd4f169 6163 {
65f38f15
AM
6164 /* We must copy these reloc types into the output file.
6165 Create a reloc section in dynobj and make room for
6166 this reloc. */
5bd4f169
AM
6167 if (sreloc == NULL)
6168 {
83bac4b0
NC
6169 sreloc = _bfd_elf_make_dynamic_reloc_section
6170 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 6171
5bd4f169 6172 if (sreloc == NULL)
83bac4b0 6173 return FALSE;
5bd4f169
AM
6174 }
6175
65f38f15
AM
6176 /* If this is a global symbol, we count the number of
6177 relocations we need for this symbol. */
6178 if (h != NULL)
6179 {
19e08130
AM
6180 struct elf_dyn_relocs *p;
6181 struct elf_dyn_relocs **head;
6182
ec338859 6183 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
6184 p = *head;
6185 if (p == NULL || p->sec != sec)
6186 {
6187 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6188 if (p == NULL)
6189 return FALSE;
6190 p->next = *head;
6191 *head = p;
6192 p->sec = sec;
6193 p->count = 0;
6194 p->pc_count = 0;
6195 }
6196 p->count += 1;
6197 if (!must_be_dyn_reloc (info, r_type))
6198 p->pc_count += 1;
65f38f15
AM
6199 }
6200 else
6201 {
ec338859
AM
6202 /* Track dynamic relocs needed for local syms too.
6203 We really need local syms available to do this
6204 easily. Oh well. */
19e08130
AM
6205 struct ppc_dyn_relocs *p;
6206 struct ppc_dyn_relocs **head;
6207 bfd_boolean is_ifunc;
ec338859 6208 asection *s;
6edfbbad 6209 void *vpp;
87d72d41 6210 Elf_Internal_Sym *isym;
6edfbbad 6211
87d72d41
AM
6212 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6213 abfd, r_symndx);
6214 if (isym == NULL)
b34976b6 6215 return FALSE;
ec338859 6216
87d72d41
AM
6217 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6218 if (s == NULL)
6219 s = sec;
6220
6edfbbad 6221 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
6222 head = (struct ppc_dyn_relocs **) vpp;
6223 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6224 p = *head;
6225 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6226 p = p->next;
6227 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6228 {
6229 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6230 if (p == NULL)
6231 return FALSE;
6232 p->next = *head;
6233 *head = p;
6234 p->sec = sec;
6235 p->ifunc = is_ifunc;
6236 p->count = 0;
6237 }
6238 p->count += 1;
ec338859 6239 }
65f38f15 6240 }
5bd4f169 6241 break;
65f38f15
AM
6242
6243 default:
96e0dda4 6244 break;
5bd4f169
AM
6245 }
6246 }
6247
b34976b6 6248 return TRUE;
5bd4f169
AM
6249}
6250
ee67d69a
AM
6251/* Merge backend specific data from an object file to the output
6252 object file when linking. */
6253
6254static bfd_boolean
50e03d47 6255ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6256{
50e03d47 6257 bfd *obfd = info->output_bfd;
ee67d69a
AM
6258 unsigned long iflags, oflags;
6259
6260 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6261 return TRUE;
6262
6263 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6264 return TRUE;
6265
50e03d47 6266 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6267 return FALSE;
6268
6269 iflags = elf_elfheader (ibfd)->e_flags;
6270 oflags = elf_elfheader (obfd)->e_flags;
6271
f6c7c3e8 6272 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6273 {
4eca0228 6274 _bfd_error_handler
695344c0 6275 /* xgettext:c-format */
871b3ab2 6276 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
6277 bfd_set_error (bfd_error_bad_value);
6278 return FALSE;
6279 }
f6c7c3e8 6280 else if (iflags != oflags && iflags != 0)
ee67d69a 6281 {
4eca0228 6282 _bfd_error_handler
695344c0 6283 /* xgettext:c-format */
871b3ab2 6284 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
6285 ibfd, iflags, oflags);
6286 bfd_set_error (bfd_error_bad_value);
6287 return FALSE;
6288 }
6289
4a91d0ba
AM
6290 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
6291 return FALSE;
005d79fd 6292
ee67d69a 6293 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 6294 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6295}
6296
6297static bfd_boolean
6298ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6299{
6300 /* Print normal ELF private data. */
6301 _bfd_elf_print_private_bfd_data (abfd, ptr);
6302
6303 if (elf_elfheader (abfd)->e_flags != 0)
6304 {
6305 FILE *file = ptr;
6306
ee67d69a
AM
6307 fprintf (file, _("private flags = 0x%lx:"),
6308 elf_elfheader (abfd)->e_flags);
6309
6310 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6311 fprintf (file, _(" [abiv%ld]"),
6312 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6313 fputc ('\n', file);
6314 }
6315
6316 return TRUE;
6317}
6318
8387904d 6319/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6320 of the code entry point, and its section, which must be in the same
6321 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6322
6323static bfd_vma
6324opd_entry_value (asection *opd_sec,
6325 bfd_vma offset,
6326 asection **code_sec,
aef36ac1
AM
6327 bfd_vma *code_off,
6328 bfd_boolean in_code_sec)
8387904d
AM
6329{
6330 bfd *opd_bfd = opd_sec->owner;
8860955f 6331 Elf_Internal_Rela *relocs;
8387904d 6332 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6333 bfd_vma val;
8387904d 6334
9f296da3
AM
6335 /* No relocs implies we are linking a --just-symbols object, or looking
6336 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6337 if (opd_sec->reloc_count == 0)
6338 {
729eabd5 6339 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6340
729eabd5
AM
6341 if (contents == NULL)
6342 {
6343 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6344 return (bfd_vma) -1;
6345 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6346 }
ee1e4ede 6347
dbb3fbbb 6348 /* PR 17512: file: 64b9dfbb. */
451dfd38 6349 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6350 return (bfd_vma) -1;
6351
729eabd5 6352 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6353 if (code_sec != NULL)
6354 {
6355 asection *sec, *likely = NULL;
ee1e4ede 6356
aef36ac1 6357 if (in_code_sec)
4b85d634 6358 {
aef36ac1
AM
6359 sec = *code_sec;
6360 if (sec->vma <= val
6361 && val < sec->vma + sec->size)
6362 likely = sec;
6363 else
6364 val = -1;
6365 }
6366 else
6367 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6368 if (sec->vma <= val
6369 && (sec->flags & SEC_LOAD) != 0
6370 && (sec->flags & SEC_ALLOC) != 0)
6371 likely = sec;
6372 if (likely != NULL)
6373 {
6374 *code_sec = likely;
6375 if (code_off != NULL)
6376 *code_off = val - likely->vma;
4b85d634
AM
6377 }
6378 }
aef36ac1 6379 return val;
4b85d634
AM
6380 }
6381
0c8d6e5c 6382 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6383
729eabd5 6384 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6385 if (relocs == NULL)
6386 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6387 /* PR 17512: file: df8e1fd6. */
6388 if (relocs == NULL)
6389 return (bfd_vma) -1;
645ea6a9 6390
8387904d 6391 /* Go find the opd reloc at the sym address. */
8860955f 6392 lo = relocs;
8387904d 6393 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6394 val = (bfd_vma) -1;
8387904d
AM
6395 while (lo < hi)
6396 {
6397 look = lo + (hi - lo) / 2;
6398 if (look->r_offset < offset)
6399 lo = look + 1;
6400 else if (look->r_offset > offset)
6401 hi = look;
6402 else
6403 {
0ffa91dd
NC
6404 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6405
8387904d
AM
6406 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6407 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6408 {
6409 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6410 asection *sec = NULL;
8387904d 6411
b53dfeb2
AM
6412 if (symndx >= symtab_hdr->sh_info
6413 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6414 {
6415 struct elf_link_hash_entry **sym_hashes;
6416 struct elf_link_hash_entry *rh;
6417
6418 sym_hashes = elf_sym_hashes (opd_bfd);
6419 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6420 if (rh != NULL)
6421 {
6422 rh = elf_follow_link (rh);
bb854a36
AM
6423 if (rh->root.type != bfd_link_hash_defined
6424 && rh->root.type != bfd_link_hash_defweak)
6425 break;
6426 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6427 {
bb854a36
AM
6428 val = rh->root.u.def.value;
6429 sec = rh->root.u.def.section;
b53dfeb2
AM
6430 }
6431 }
6432 }
6433
6434 if (sec == NULL)
6435 {
6436 Elf_Internal_Sym *sym;
6437
6438 if (symndx < symtab_hdr->sh_info)
6439 {
6440 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6441 if (sym == NULL)
6442 {
6443 size_t symcnt = symtab_hdr->sh_info;
6444 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6445 symcnt, 0,
6446 NULL, NULL, NULL);
6447 if (sym == NULL)
6448 break;
6449 symtab_hdr->contents = (bfd_byte *) sym;
6450 }
6451 sym += symndx;
128205bb
AM
6452 }
6453 else
6454 {
b53dfeb2
AM
6455 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6456 1, symndx,
6457 NULL, NULL, NULL);
128205bb
AM
6458 if (sym == NULL)
6459 break;
128205bb 6460 }
b53dfeb2
AM
6461 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6462 if (sec == NULL)
6463 break;
6464 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6465 val = sym->st_value;
8387904d 6466 }
b53dfeb2 6467
8387904d
AM
6468 val += look->r_addend;
6469 if (code_off != NULL)
6470 *code_off = val;
6471 if (code_sec != NULL)
aef36ac1
AM
6472 {
6473 if (in_code_sec && *code_sec != sec)
6474 return -1;
6475 else
6476 *code_sec = sec;
6477 }
b53dfeb2 6478 if (sec->output_section != NULL)
8387904d 6479 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6480 }
6481 break;
6482 }
6483 }
645ea6a9 6484
645ea6a9 6485 return val;
8387904d
AM
6486}
6487
aef36ac1
AM
6488/* If the ELF symbol SYM might be a function in SEC, return the
6489 function size and set *CODE_OFF to the function's entry point,
6490 otherwise return zero. */
9f296da3 6491
aef36ac1
AM
6492static bfd_size_type
6493ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6494 bfd_vma *code_off)
9f296da3 6495{
aef36ac1
AM
6496 bfd_size_type size;
6497
6498 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6499 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6500 return 0;
6501
6502 size = 0;
6503 if (!(sym->flags & BSF_SYNTHETIC))
6504 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6505
6506 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6507 {
b07bca4e
AM
6508 struct _opd_sec_data *opd = get_opd_info (sym->section);
6509 bfd_vma symval = sym->value;
6510
6511 if (opd != NULL
6512 && opd->adjust != NULL
6513 && elf_section_data (sym->section)->relocs != NULL)
6514 {
6515 /* opd_entry_value will use cached relocs that have been
6516 adjusted, but with raw symbols. That means both local
6517 and global symbols need adjusting. */
6518 long adjust = opd->adjust[OPD_NDX (symval)];
6519 if (adjust == -1)
6520 return 0;
6521 symval += adjust;
6522 }
6523
6524 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6525 &sec, code_off, TRUE) == (bfd_vma) -1)
6526 return 0;
6527 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6528 symbol. This size has nothing to do with the code size of the
6529 function, which is what we're supposed to return, but the
6530 code size isn't available without looking up the dot-sym.
6531 However, doing that would be a waste of time particularly
6532 since elf_find_function will look at the dot-sym anyway.
6533 Now, elf_find_function will keep the largest size of any
6534 function sym found at the code address of interest, so return
6535 1 here to avoid it incorrectly caching a larger function size
6536 for a small function. This does mean we return the wrong
6537 size for a new-ABI function of size 24, but all that does is
6538 disable caching for such functions. */
6539 if (size == 24)
6540 size = 1;
9f296da3 6541 }
aef36ac1
AM
6542 else
6543 {
6544 if (sym->section != sec)
6545 return 0;
6546 *code_off = sym->value;
6547 }
6548 if (size == 0)
6549 size = 1;
6550 return size;
9f296da3
AM
6551}
6552
f378ab09
AM
6553/* Return true if symbol is a strong function defined in an ELFv2
6554 object with st_other localentry bits of zero, ie. its local entry
6555 point coincides with its global entry point. */
6556
6557static bfd_boolean
6558is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6559{
6560 return (h != NULL
6561 && h->type == STT_FUNC
6562 && h->root.type == bfd_link_hash_defined
6563 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6564 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6565 && is_ppc64_elf (h->root.u.def.section->owner)
6566 && abiversion (h->root.u.def.section->owner) >= 2);
6567}
6568
854b41e7
AM
6569/* Return true if symbol is defined in a regular object file. */
6570
6571static bfd_boolean
6572is_static_defined (struct elf_link_hash_entry *h)
6573{
6574 return ((h->root.type == bfd_link_hash_defined
6575 || h->root.type == bfd_link_hash_defweak)
6576 && h->root.u.def.section != NULL
6577 && h->root.u.def.section->output_section != NULL);
6578}
6579
b31867b6
AM
6580/* If FDH is a function descriptor symbol, return the associated code
6581 entry symbol if it is defined. Return NULL otherwise. */
6582
6583static struct ppc_link_hash_entry *
6584defined_code_entry (struct ppc_link_hash_entry *fdh)
6585{
6586 if (fdh->is_func_descriptor)
6587 {
6588 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6589 if (fh->elf.root.type == bfd_link_hash_defined
6590 || fh->elf.root.type == bfd_link_hash_defweak)
6591 return fh;
6592 }
6593 return NULL;
6594}
6595
6596/* If FH is a function code entry symbol, return the associated
6597 function descriptor symbol if it is defined. Return NULL otherwise. */
6598
6599static struct ppc_link_hash_entry *
6600defined_func_desc (struct ppc_link_hash_entry *fh)
6601{
6602 if (fh->oh != NULL
6603 && fh->oh->is_func_descriptor)
6604 {
6605 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6606 if (fdh->elf.root.type == bfd_link_hash_defined
6607 || fdh->elf.root.type == bfd_link_hash_defweak)
6608 return fdh;
6609 }
6610 return NULL;
6611}
6612
8c5b4e52
AM
6613static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6614
6615/* Garbage collect sections, after first dealing with dot-symbols. */
6616
6617static bfd_boolean
6618ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6619{
6620 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6621
6622 if (htab != NULL && htab->need_func_desc_adj)
6623 {
6624 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6625 htab->need_func_desc_adj = 0;
6626 }
6627 return bfd_elf_gc_sections (abfd, info);
6628}
6629
74f0fb50
AM
6630/* Mark all our entry sym sections, both opd and code section. */
6631
6632static void
6633ppc64_elf_gc_keep (struct bfd_link_info *info)
6634{
6635 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6636 struct bfd_sym_chain *sym;
6637
4dfe6ac6
NC
6638 if (htab == NULL)
6639 return;
6640
74f0fb50
AM
6641 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6642 {
b31867b6 6643 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6644 asection *sec;
6645
6646 eh = (struct ppc_link_hash_entry *)
b31867b6 6647 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6648 if (eh == NULL)
6649 continue;
6650 if (eh->elf.root.type != bfd_link_hash_defined
6651 && eh->elf.root.type != bfd_link_hash_defweak)
6652 continue;
6653
b31867b6
AM
6654 fh = defined_code_entry (eh);
6655 if (fh != NULL)
74f0fb50 6656 {
b31867b6 6657 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6658 sec->flags |= SEC_KEEP;
6659 }
6660 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6661 && opd_entry_value (eh->elf.root.u.def.section,
6662 eh->elf.root.u.def.value,
aef36ac1 6663 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6664 sec->flags |= SEC_KEEP;
6665
6666 sec = eh->elf.root.u.def.section;
6667 sec->flags |= SEC_KEEP;
6668 }
6669}
6670
64d03ab5
AM
6671/* Mark sections containing dynamically referenced symbols. When
6672 building shared libraries, we must assume that any visible symbol is
6673 referenced. */
6674
6675static bfd_boolean
6676ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6677{
6678 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6679 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6680 struct ppc_link_hash_entry *fdh;
b407645f 6681 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6682
64d03ab5 6683 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6684 fdh = defined_func_desc (eh);
6685 if (fdh != NULL)
6686 eh = fdh;
64d03ab5
AM
6687
6688 if ((eh->elf.root.type == bfd_link_hash_defined
6689 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6690 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6691 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6692 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6693 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6694 && (!bfd_link_executable (info)
e278ae05 6695 || info->gc_keep_exported
b407645f
AM
6696 || info->export_dynamic
6697 || (eh->elf.dynamic
6698 && d != NULL
6699 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6700 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6701 || !bfd_hide_sym_by_version (info->version_info,
6702 eh->elf.root.root.string)))))
64d03ab5
AM
6703 {
6704 asection *code_sec;
b31867b6 6705 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6706
6707 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6708
6709 /* Function descriptor syms cause the associated
6710 function code sym section to be marked. */
b31867b6
AM
6711 fh = defined_code_entry (eh);
6712 if (fh != NULL)
6713 {
6714 code_sec = fh->elf.root.u.def.section;
6715 code_sec->flags |= SEC_KEEP;
6716 }
64d03ab5
AM
6717 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6718 && opd_entry_value (eh->elf.root.u.def.section,
6719 eh->elf.root.u.def.value,
aef36ac1 6720 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6721 code_sec->flags |= SEC_KEEP;
6722 }
6723
6724 return TRUE;
6725}
6726
5bd4f169
AM
6727/* Return the section that should be marked against GC for a given
6728 relocation. */
6729
6730static asection *
4ce794b7 6731ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6732 struct bfd_link_info *info,
4ce794b7
AM
6733 Elf_Internal_Rela *rel,
6734 struct elf_link_hash_entry *h,
6735 Elf_Internal_Sym *sym)
5bd4f169 6736{
ccfa59ea
AM
6737 asection *rsec;
6738
ccfa59ea
AM
6739 /* Syms return NULL if we're marking .opd, so we avoid marking all
6740 function sections, as all functions are referenced in .opd. */
6741 rsec = NULL;
6742 if (get_opd_info (sec) != NULL)
6743 return rsec;
1e2f5b6e 6744
5bd4f169
AM
6745 if (h != NULL)
6746 {
04c9666a 6747 enum elf_ppc64_reloc_type r_type;
b31867b6 6748 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6749
4ce794b7 6750 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6751 switch (r_type)
5bd4f169
AM
6752 {
6753 case R_PPC64_GNU_VTINHERIT:
6754 case R_PPC64_GNU_VTENTRY:
6755 break;
6756
6757 default:
6758 switch (h->root.type)
6759 {
6760 case bfd_link_hash_defined:
6761 case bfd_link_hash_defweak:
ccfa59ea 6762 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6763 fdh = defined_func_desc (eh);
6764 if (fdh != NULL)
8c5b4e52
AM
6765 {
6766 /* -mcall-aixdesc code references the dot-symbol on
6767 a call reloc. Mark the function descriptor too
6768 against garbage collection. */
6769 fdh->elf.mark = 1;
60d67dc8
AM
6770 if (fdh->elf.is_weakalias)
6771 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6772 eh = fdh;
6773 }
1e2f5b6e
AM
6774
6775 /* Function descriptor syms cause the associated
6776 function code sym section to be marked. */
b31867b6
AM
6777 fh = defined_code_entry (eh);
6778 if (fh != NULL)
ccfa59ea
AM
6779 {
6780 /* They also mark their opd section. */
74f0fb50 6781 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6782
b31867b6 6783 rsec = fh->elf.root.u.def.section;
ccfa59ea 6784 }
8387904d
AM
6785 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6786 && opd_entry_value (eh->elf.root.u.def.section,
6787 eh->elf.root.u.def.value,
aef36ac1 6788 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6789 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6790 else
1e2f5b6e
AM
6791 rsec = h->root.u.def.section;
6792 break;
5bd4f169
AM
6793
6794 case bfd_link_hash_common:
1e2f5b6e
AM
6795 rsec = h->root.u.c.p->section;
6796 break;
5bd4f169
AM
6797
6798 default:
fb34365b 6799 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6800 }
6801 }
6802 }
6803 else
6804 {
74f0fb50 6805 struct _opd_sec_data *opd;
1e2f5b6e
AM
6806
6807 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6808 opd = get_opd_info (rsec);
6809 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6810 {
74f0fb50 6811 rsec->gc_mark = 1;
ccfa59ea 6812
51aecdc5 6813 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6814 }
5bd4f169
AM
6815 }
6816
1e2f5b6e 6817 return rsec;
5bd4f169
AM
6818}
6819
deb0e272
AM
6820/* The maximum size of .sfpr. */
6821#define SFPR_MAX (218*4)
6822
6823struct sfpr_def_parms
6824{
699733f6
AM
6825 const char name[12];
6826 unsigned char lo, hi;
deb0e272
AM
6827 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6828 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6829};
6830
a4b6fadd
AM
6831/* Auto-generate _save*, _rest* functions in .sfpr.
6832 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6833 instead. */
deb0e272 6834
4dfe6ac6 6835static bfd_boolean
a4b6fadd
AM
6836sfpr_define (struct bfd_link_info *info,
6837 const struct sfpr_def_parms *parm,
6838 asection *stub_sec)
deb0e272
AM
6839{
6840 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6841 unsigned int i;
6842 size_t len = strlen (parm->name);
6843 bfd_boolean writing = FALSE;
699733f6 6844 char sym[16];
deb0e272 6845
4dfe6ac6
NC
6846 if (htab == NULL)
6847 return FALSE;
6848
deb0e272
AM
6849 memcpy (sym, parm->name, len);
6850 sym[len + 2] = 0;
6851
6852 for (i = parm->lo; i <= parm->hi; i++)
6853 {
a4b6fadd 6854 struct ppc_link_hash_entry *h;
deb0e272
AM
6855
6856 sym[len + 0] = i / 10 + '0';
6857 sym[len + 1] = i % 10 + '0';
a4b6fadd 6858 h = (struct ppc_link_hash_entry *)
b32547cd 6859 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6860 if (stub_sec != NULL)
deb0e272 6861 {
a4b6fadd
AM
6862 if (h != NULL
6863 && h->elf.root.type == bfd_link_hash_defined
6864 && h->elf.root.u.def.section == htab->sfpr)
6865 {
6866 struct elf_link_hash_entry *s;
6867 char buf[32];
6868 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6869 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6870 if (s == NULL)
6871 return FALSE;
6872 if (s->root.type == bfd_link_hash_new
6873 || (s->root.type = bfd_link_hash_defined
6874 && s->root.u.def.section == stub_sec))
6875 {
6876 s->root.type = bfd_link_hash_defined;
6877 s->root.u.def.section = stub_sec;
7dda8d3c 6878 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6879 + h->elf.root.u.def.value);
6880 s->ref_regular = 1;
6881 s->def_regular = 1;
6882 s->ref_regular_nonweak = 1;
6883 s->forced_local = 1;
6884 s->non_elf = 0;
6885 s->root.linker_def = 1;
6886 }
6887 }
6888 continue;
6889 }
6890 if (h != NULL)
6891 {
6892 h->save_res = 1;
6893 if (!h->elf.def_regular)
deb0e272 6894 {
a4b6fadd
AM
6895 h->elf.root.type = bfd_link_hash_defined;
6896 h->elf.root.u.def.section = htab->sfpr;
6897 h->elf.root.u.def.value = htab->sfpr->size;
6898 h->elf.type = STT_FUNC;
6899 h->elf.def_regular = 1;
b32547cd 6900 h->elf.non_elf = 0;
a4b6fadd
AM
6901 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6902 writing = TRUE;
deb0e272 6903 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6904 {
6905 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6906 if (htab->sfpr->contents == NULL)
6907 return FALSE;
6908 }
deb0e272
AM
6909 }
6910 }
6911 if (writing)
6912 {
6913 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6914 if (i != parm->hi)
6915 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6916 else
6917 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6918 htab->sfpr->size = p - htab->sfpr->contents;
6919 }
6920 }
6921
6922 return TRUE;
6923}
6924
6925static bfd_byte *
6926savegpr0 (bfd *abfd, bfd_byte *p, int r)
6927{
6928 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6929 return p + 4;
6930}
6931
6932static bfd_byte *
6933savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6934{
6935 p = savegpr0 (abfd, p, r);
a078d95a 6936 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6937 p = p + 4;
6938 bfd_put_32 (abfd, BLR, p);
6939 return p + 4;
6940}
6941
6942static bfd_byte *
6943restgpr0 (bfd *abfd, bfd_byte *p, int r)
6944{
6945 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6946 return p + 4;
6947}
6948
6949static bfd_byte *
6950restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6951{
a078d95a 6952 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6953 p = p + 4;
6954 p = restgpr0 (abfd, p, r);
6955 bfd_put_32 (abfd, MTLR_R0, p);
6956 p = p + 4;
6957 if (r == 29)
6958 {
6959 p = restgpr0 (abfd, p, 30);
6960 p = restgpr0 (abfd, p, 31);
6961 }
6962 bfd_put_32 (abfd, BLR, p);
6963 return p + 4;
6964}
6965
6966static bfd_byte *
6967savegpr1 (bfd *abfd, bfd_byte *p, int r)
6968{
6969 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6970 return p + 4;
6971}
6972
6973static bfd_byte *
6974savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6975{
6976 p = savegpr1 (abfd, p, r);
6977 bfd_put_32 (abfd, BLR, p);
6978 return p + 4;
6979}
6980
6981static bfd_byte *
6982restgpr1 (bfd *abfd, bfd_byte *p, int r)
6983{
6984 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6985 return p + 4;
6986}
6987
6988static bfd_byte *
6989restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6990{
6991 p = restgpr1 (abfd, p, r);
6992 bfd_put_32 (abfd, BLR, p);
6993 return p + 4;
6994}
6995
6996static bfd_byte *
6997savefpr (bfd *abfd, bfd_byte *p, int r)
6998{
6999 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7000 return p + 4;
7001}
7002
7003static bfd_byte *
7004savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
7005{
7006 p = savefpr (abfd, p, r);
a078d95a 7007 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
7008 p = p + 4;
7009 bfd_put_32 (abfd, BLR, p);
7010 return p + 4;
7011}
7012
7013static bfd_byte *
7014restfpr (bfd *abfd, bfd_byte *p, int r)
7015{
7016 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7017 return p + 4;
7018}
7019
7020static bfd_byte *
7021restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
7022{
a078d95a 7023 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
7024 p = p + 4;
7025 p = restfpr (abfd, p, r);
7026 bfd_put_32 (abfd, MTLR_R0, p);
7027 p = p + 4;
7028 if (r == 29)
7029 {
7030 p = restfpr (abfd, p, 30);
7031 p = restfpr (abfd, p, 31);
7032 }
7033 bfd_put_32 (abfd, BLR, p);
7034 return p + 4;
7035}
7036
7037static bfd_byte *
7038savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
7039{
7040 p = savefpr (abfd, p, r);
7041 bfd_put_32 (abfd, BLR, p);
7042 return p + 4;
7043}
7044
7045static bfd_byte *
7046restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
7047{
7048 p = restfpr (abfd, p, r);
7049 bfd_put_32 (abfd, BLR, p);
7050 return p + 4;
7051}
7052
7053static bfd_byte *
7054savevr (bfd *abfd, bfd_byte *p, int r)
7055{
7056 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7057 p = p + 4;
7058 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
7059 return p + 4;
7060}
7061
7062static bfd_byte *
7063savevr_tail (bfd *abfd, bfd_byte *p, int r)
7064{
7065 p = savevr (abfd, p, r);
7066 bfd_put_32 (abfd, BLR, p);
7067 return p + 4;
7068}
7069
7070static bfd_byte *
7071restvr (bfd *abfd, bfd_byte *p, int r)
7072{
7073 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7074 p = p + 4;
7075 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
7076 return p + 4;
7077}
7078
7079static bfd_byte *
7080restvr_tail (bfd *abfd, bfd_byte *p, int r)
7081{
7082 p = restvr (abfd, p, r);
7083 bfd_put_32 (abfd, BLR, p);
7084 return p + 4;
7085}
7086
e86ce104
AM
7087/* Called via elf_link_hash_traverse to transfer dynamic linking
7088 information on function code symbol entries to their corresponding
7089 function descriptor symbol entries. */
deb0e272 7090
b34976b6 7091static bfd_boolean
4ce794b7 7092func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 7093{
e86ce104 7094 struct bfd_link_info *info;
65f38f15 7095 struct ppc_link_hash_table *htab;
50bc7936
AM
7096 struct ppc_link_hash_entry *fh;
7097 struct ppc_link_hash_entry *fdh;
7098 bfd_boolean force_local;
5bd4f169 7099
50bc7936
AM
7100 fh = (struct ppc_link_hash_entry *) h;
7101 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 7102 return TRUE;
e86ce104 7103
8c5b4e52
AM
7104 if (!fh->is_func)
7105 return TRUE;
7106
7107 if (fh->elf.root.root.string[0] != '.'
7108 || fh->elf.root.root.string[1] == '\0')
7109 return TRUE;
7110
4ce794b7 7111 info = inf;
65f38f15 7112 htab = ppc_hash_table (info);
4dfe6ac6
NC
7113 if (htab == NULL)
7114 return FALSE;
5bd4f169 7115
8c5b4e52
AM
7116 /* Find the corresponding function descriptor symbol. */
7117 fdh = lookup_fdh (fh, htab);
7118
c09bdfe5
AM
7119 /* Resolve undefined references to dot-symbols as the value
7120 in the function descriptor, if we have one in a regular object.
7121 This is to satisfy cases like ".quad .foo". Calls to functions
7122 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7123 if ((fh->elf.root.type == bfd_link_hash_undefined
7124 || fh->elf.root.type == bfd_link_hash_undefweak)
7125 && (fdh->elf.root.type == bfd_link_hash_defined
7126 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7127 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7128 && opd_entry_value (fdh->elf.root.u.def.section,
7129 fdh->elf.root.u.def.value,
c09bdfe5 7130 &fh->elf.root.u.def.section,
aef36ac1 7131 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7132 {
b31867b6 7133 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7134 fh->elf.forced_local = 1;
b31867b6
AM
7135 fh->elf.def_regular = fdh->elf.def_regular;
7136 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7137 }
7138
8c5b4e52
AM
7139 if (!fh->elf.dynamic)
7140 {
7141 struct plt_entry *ent;
5bd4f169 7142
8c5b4e52
AM
7143 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7144 if (ent->plt.refcount > 0)
7145 break;
7146 if (ent == NULL)
7147 return TRUE;
7148 }
5bd4f169 7149
8c5b4e52 7150 /* Create a descriptor as undefined if necessary. */
50bc7936 7151 if (fdh == NULL
0e1862bb 7152 && !bfd_link_executable (info)
50bc7936
AM
7153 && (fh->elf.root.type == bfd_link_hash_undefined
7154 || fh->elf.root.type == bfd_link_hash_undefweak))
7155 {
908b32fc 7156 fdh = make_fdh (info, fh);
bb700d78
AM
7157 if (fdh == NULL)
7158 return FALSE;
50bc7936 7159 }
648cca2c 7160
8c5b4e52 7161 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7162 if (fdh != NULL
7163 && fdh->fake
8c5b4e52
AM
7164 && (fh->elf.root.type == bfd_link_hash_defined
7165 || fh->elf.root.type == bfd_link_hash_defweak))
7166 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7167
8c5b4e52
AM
7168 /* Transfer dynamic linking information to the function descriptor. */
7169 if (fdh != NULL)
7170 {
f5385ebf
AM
7171 fdh->elf.ref_regular |= fh->elf.ref_regular;
7172 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7173 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7174 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7175 fdh->elf.dynamic |= fh->elf.dynamic;
7176 fdh->elf.needs_plt |= (fh->elf.needs_plt
7177 || fh->elf.type == STT_FUNC
7178 || fh->elf.type == STT_GNU_IFUNC);
7179 move_plt_plist (fh, fdh);
7180
7181 if (!fdh->elf.forced_local
7182 && fh->elf.dynindx != -1)
7183 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7184 return FALSE;
e86ce104
AM
7185 }
7186
50bc7936
AM
7187 /* Now that the info is on the function descriptor, clear the
7188 function code sym info. Any function code syms for which we
7189 don't have a definition in a regular file, we force local.
7190 This prevents a shared library from exporting syms that have
7191 been imported from another library. Function code syms that
7192 are really in the library we must leave global to prevent the
7193 linker dragging in a definition from a static library. */
93f3fa99
AM
7194 force_local = (!fh->elf.def_regular
7195 || fdh == NULL
7196 || !fdh->elf.def_regular
7197 || fdh->elf.forced_local);
50bc7936
AM
7198 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7199
b34976b6 7200 return TRUE;
e86ce104 7201}
40b8271b 7202
a4b6fadd
AM
7203static const struct sfpr_def_parms save_res_funcs[] =
7204 {
7205 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7206 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7207 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7208 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7209 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7210 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7211 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7212 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7213 { "._savef", 14, 31, savefpr, savefpr1_tail },
7214 { "._restf", 14, 31, restfpr, restfpr1_tail },
7215 { "_savevr_", 20, 31, savevr, savevr_tail },
7216 { "_restvr_", 20, 31, restvr, restvr_tail }
7217 };
7218
e86ce104 7219/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7220 this hook to a) provide some gcc support functions, and b) transfer
7221 dynamic linking information gathered so far on function code symbol
7222 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7223
b34976b6 7224static bfd_boolean
4ce794b7
AM
7225ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7226 struct bfd_link_info *info)
e86ce104
AM
7227{
7228 struct ppc_link_hash_table *htab;
7229
7230 htab = ppc_hash_table (info);
4dfe6ac6
NC
7231 if (htab == NULL)
7232 return FALSE;
7233
b32547cd
AM
7234 /* Provide any missing _save* and _rest* functions. */
7235 if (htab->sfpr != NULL)
7236 {
7237 unsigned int i;
7238
7239 htab->sfpr->size = 0;
7240 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7241 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7242 return FALSE;
7243 if (htab->sfpr->size == 0)
7244 htab->sfpr->flags |= SEC_EXCLUDE;
7245 }
7246
7247 if (bfd_link_relocatable (info))
7248 return TRUE;
7249
7250 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7251 {
7252 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7253 /* Make .TOC. defined so as to prevent it being made dynamic.
7254 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7255 if (!htab->elf.hgot->def_regular
7256 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7257 {
7258 htab->elf.hgot->root.type = bfd_link_hash_defined;
7259 htab->elf.hgot->root.u.def.value = 0;
7260 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7261 htab->elf.hgot->def_regular = 1;
7262 htab->elf.hgot->root.linker_def = 1;
7263 }
dba6fa9b 7264 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7265 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7266 | STV_HIDDEN);
7267 }
c66bb0ee 7268
8c5b4e52
AM
7269 if (htab->need_func_desc_adj)
7270 {
7271 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7272 htab->need_func_desc_adj = 0;
7273 }
805fc799 7274
b34976b6 7275 return TRUE;
e86ce104
AM
7276}
7277
98bbb1b8 7278/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7279
98bbb1b8 7280static asection *
a345bc8d
AM
7281readonly_dynrelocs (struct elf_link_hash_entry *h)
7282{
7283 struct ppc_link_hash_entry *eh;
7284 struct elf_dyn_relocs *p;
7285
7286 eh = (struct ppc_link_hash_entry *) h;
7287 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7288 {
7289 asection *s = p->sec->output_section;
7290
7291 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7292 return p->sec;
a345bc8d 7293 }
98bbb1b8 7294 return NULL;
a345bc8d
AM
7295}
7296
d311bc8b 7297/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7298 aliases, that apply to read-only sections. Cannot be used after
7299 size_dynamic_sections. */
d311bc8b
AM
7300
7301static bfd_boolean
7302alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7303{
7304 struct ppc_link_hash_entry *eh;
7305
7306 eh = (struct ppc_link_hash_entry *) h;
7307 do
7308 {
7309 if (readonly_dynrelocs (&eh->elf))
7310 return TRUE;
ab2477e1 7311 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
d311bc8b
AM
7312 } while (eh != NULL && &eh->elf != h);
7313
7314 return FALSE;
7315}
8a2058b5 7316
8a9e8e72
AM
7317/* Return whether EH has pc-relative dynamic relocs. */
7318
7319static bfd_boolean
7320pc_dynrelocs (struct ppc_link_hash_entry *eh)
7321{
7322 struct elf_dyn_relocs *p;
7323
7324 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7325 if (p->pc_count != 0)
7326 return TRUE;
7327 return FALSE;
7328}
7329
8a2058b5
AM
7330/* Return true if a global entry stub will be created for H. Valid
7331 for ELFv2 before plt entries have been allocated. */
7332
7333static bfd_boolean
7334global_entry_stub (struct elf_link_hash_entry *h)
7335{
7336 struct plt_entry *pent;
7337
7338 if (!h->pointer_equality_needed
7339 || h->def_regular)
7340 return FALSE;
7341
7342 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7343 if (pent->plt.refcount > 0
7344 && pent->addend == 0)
7345 return TRUE;
7346
7347 return FALSE;
7348}
7349
e86ce104
AM
7350/* Adjust a symbol defined by a dynamic object and referenced by a
7351 regular object. The current definition is in some section of the
7352 dynamic object, but we're not including those sections. We have to
7353 change the definition to something the rest of the link can
7354 understand. */
7355
b34976b6 7356static bfd_boolean
4ce794b7
AM
7357ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7358 struct elf_link_hash_entry *h)
e86ce104
AM
7359{
7360 struct ppc_link_hash_table *htab;
5474d94f 7361 asection *s, *srel;
e86ce104
AM
7362
7363 htab = ppc_hash_table (info);
4dfe6ac6
NC
7364 if (htab == NULL)
7365 return FALSE;
e86ce104
AM
7366
7367 /* Deal with function syms. */
7368 if (h->type == STT_FUNC
e054468f 7369 || h->type == STT_GNU_IFUNC
f5385ebf 7370 || h->needs_plt)
e86ce104 7371 {
529fe20e
AM
7372 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7373 || SYMBOL_CALLS_LOCAL (info, h)
7374 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7375 /* Discard dyn_relocs when non-pic if we've decided that a
7376 function symbol is local and not an ifunc. We keep dynamic
7377 relocs for ifuncs when local rather than always emitting a
7378 plt call stub for them and defining the symbol on the call
7379 stub. We can't do that for ELFv1 anyway (a function symbol
7380 is defined on a descriptor, not code) and it can be faster at
7381 run-time due to not needing to bounce through a stub. The
7382 dyn_relocs for ifuncs will be applied even in a static
7383 executable. */
7384 if (!bfd_link_pic (info)
7385 && h->type != STT_GNU_IFUNC
7386 && local)
7387 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7388
e86ce104
AM
7389 /* Clear procedure linkage table information for any symbol that
7390 won't need a .plt entry. */
411e1bfb
AM
7391 struct plt_entry *ent;
7392 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7393 if (ent->plt.refcount > 0)
7394 break;
8387904d 7395 if (ent == NULL
2d7ad24e
AM
7396 || (h->type != STT_GNU_IFUNC
7397 && local
3e04d765
AM
7398 && (htab->can_convert_all_inline_plt
7399 || (((struct ppc_link_hash_entry *) h)->tls_mask
7400 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 7401 {
411e1bfb 7402 h->plt.plist = NULL;
f5385ebf 7403 h->needs_plt = 0;
d1eca1e4 7404 h->pointer_equality_needed = 0;
40b8271b 7405 }
8a2058b5 7406 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7407 {
d1eca1e4
AM
7408 /* Taking a function's address in a read/write section
7409 doesn't require us to define the function symbol in the
7410 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7411 be used instead. The reason we prefer a few more dynamic
7412 relocs is that calling via a global entry stub costs a
7413 few more instructions, and pointer_equality_needed causes
7414 extra work in ld.so when resolving these symbols. */
529fe20e 7415 if (global_entry_stub (h))
d1eca1e4 7416 {
ab2477e1 7417 if (!readonly_dynrelocs (h))
529fe20e
AM
7418 {
7419 h->pointer_equality_needed = 0;
04383fd1
AM
7420 /* If we haven't seen a branch reloc and the symbol
7421 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
7422 if (!h->needs_plt)
7423 h->plt.plist = NULL;
7424 }
7425 else if (!bfd_link_pic (info))
7426 /* We are going to be defining the function symbol on the
7427 plt stub, so no dyn_relocs needed when non-pic. */
7428 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7429 }
7430
3988aed5
AM
7431 /* ELFv2 function symbols can't have copy relocs. */
7432 return TRUE;
7433 }
7434 else if (!h->needs_plt
ab2477e1 7435 && !readonly_dynrelocs (h))
3988aed5 7436 {
04383fd1
AM
7437 /* If we haven't seen a branch reloc and the symbol isn't an
7438 ifunc then we don't need a plt entry. */
3988aed5
AM
7439 h->plt.plist = NULL;
7440 h->pointer_equality_needed = 0;
a345bc8d
AM
7441 return TRUE;
7442 }
5bd4f169 7443 }
bbd7ec4a 7444 else
411e1bfb 7445 h->plt.plist = NULL;
5bd4f169
AM
7446
7447 /* If this is a weak symbol, and there is a real definition, the
7448 processor independent code will have arranged for us to see the
7449 real definition first, and we can just use the same value. */
60d67dc8 7450 if (h->is_weakalias)
5bd4f169 7451 {
60d67dc8
AM
7452 struct elf_link_hash_entry *def = weakdef (h);
7453 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7454 h->root.u.def.section = def->root.u.def.section;
7455 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7456 if (def->root.u.def.section == htab->elf.sdynbss
7457 || def->root.u.def.section == htab->elf.sdynrelro)
7458 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7459 return TRUE;
5bd4f169
AM
7460 }
7461
5bd4f169
AM
7462 /* If we are creating a shared library, we must presume that the
7463 only references to the symbol are via the global offset table.
7464 For such cases we need not do anything here; the relocations will
7465 be handled correctly by relocate_section. */
0e1862bb 7466 if (bfd_link_pic (info))
b34976b6 7467 return TRUE;
5bd4f169 7468
65f38f15
AM
7469 /* If there are no references to this symbol that do not use the
7470 GOT, we don't need to generate a copy reloc. */
f5385ebf 7471 if (!h->non_got_ref)
b34976b6 7472 return TRUE;
65f38f15 7473
b186458a 7474 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7475 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7476
d93d1c80
AM
7477 /* If -z nocopyreloc was given, don't generate them either. */
7478 || info->nocopyreloc
a127494f 7479
dce2246a 7480 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7481 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7482 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7483
d93d1c80
AM
7484 /* Protected variables do not work with .dynbss. The copy in
7485 .dynbss won't be used by the shared library with the protected
7486 definition for the variable. Text relocations are preferable
7487 to an incorrect program. */
7488 || h->protected_def)
529fe20e 7489 return TRUE;
a127494f 7490
5d35169e 7491 if (h->plt.plist != NULL)
97b639ba
AM
7492 {
7493 /* We should never get here, but unfortunately there are versions
7494 of gcc out there that improperly (for this ABI) put initialized
7495 function pointers, vtable refs and suchlike in read-only
7496 sections. Allow them to proceed, but warn that this might
7497 break at runtime. */
25f53a85 7498 info->callbacks->einfo
c1c8c1ef 7499 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 7500 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7501 h->root.root.string);
7502 }
5d35169e
AM
7503
7504 /* This is a reference to a symbol defined by a dynamic object which
7505 is not a function. */
7506
5bd4f169
AM
7507 /* We must allocate the symbol in our .dynbss section, which will
7508 become part of the .bss section of the executable. There will be
7509 an entry for this symbol in the .dynsym section. The dynamic
7510 object will contain position independent code, so all references
7511 from the dynamic object to this symbol will go through the global
7512 offset table. The dynamic linker will use the .dynsym entry to
7513 determine the address it must put in the global offset table, so
7514 both the dynamic object and the regular object will refer to the
7515 same memory location for the variable. */
5474d94f
AM
7516 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7517 {
7518 s = htab->elf.sdynrelro;
7519 srel = htab->elf.sreldynrelro;
7520 }
7521 else
7522 {
7523 s = htab->elf.sdynbss;
7524 srel = htab->elf.srelbss;
7525 }
1d7e9d18 7526 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7527 {
4a7e5234
AM
7528 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7529 linker to copy the initial value out of the dynamic object
7530 and into the runtime process image. */
5474d94f 7531 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7532 h->needs_copy = 1;
5bd4f169
AM
7533 }
7534
529fe20e
AM
7535 /* We no longer want dyn_relocs. */
7536 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7537 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7538}
7539
e86ce104
AM
7540/* If given a function descriptor symbol, hide both the function code
7541 sym and the descriptor. */
7542static void
4ce794b7
AM
7543ppc64_elf_hide_symbol (struct bfd_link_info *info,
7544 struct elf_link_hash_entry *h,
7545 bfd_boolean force_local)
e86ce104 7546{
34814b9f 7547 struct ppc_link_hash_entry *eh;
e86ce104
AM
7548 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7549
34814b9f
AM
7550 eh = (struct ppc_link_hash_entry *) h;
7551 if (eh->is_func_descriptor)
e86ce104 7552 {
34814b9f 7553 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7554
721956f4 7555 if (fh == NULL)
d1329ca3
AM
7556 {
7557 const char *p, *q;
b8ac2841 7558 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7559 char save;
7560
7561 /* We aren't supposed to use alloca in BFD because on
7562 systems which do not have alloca the version in libiberty
7563 calls xmalloc, which might cause the program to crash
7564 when it runs out of memory. This function doesn't have a
7565 return status, so there's no way to gracefully return an
7566 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7567 accessed; It's either a string in an ELF string table,
7568 or allocated in an objalloc structure. */
d1329ca3 7569
34814b9f 7570 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7571 save = *p;
7572 *(char *) p = '.';
34814b9f 7573 fh = (struct ppc_link_hash_entry *)
b8ac2841 7574 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7575 *(char *) p = save;
7576
7577 /* Unfortunately, if it so happens that the string we were
7578 looking for was allocated immediately before this string,
7579 then we overwrote the string terminator. That's the only
7580 reason the lookup should fail. */
7581 if (fh == NULL)
7582 {
34814b9f
AM
7583 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7584 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7585 --q, --p;
34814b9f
AM
7586 if (q < eh->elf.root.root.string && *p == '.')
7587 fh = (struct ppc_link_hash_entry *)
b8ac2841 7588 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7589 }
7590 if (fh != NULL)
7591 {
34814b9f
AM
7592 eh->oh = fh;
7593 fh->oh = eh;
d1329ca3
AM
7594 }
7595 }
e86ce104 7596 if (fh != NULL)
34814b9f 7597 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7598 }
7599}
7600
411e1bfb 7601static bfd_boolean
8843416a
AM
7602get_sym_h (struct elf_link_hash_entry **hp,
7603 Elf_Internal_Sym **symp,
7604 asection **symsecp,
f961d9dd 7605 unsigned char **tls_maskp,
8843416a
AM
7606 Elf_Internal_Sym **locsymsp,
7607 unsigned long r_symndx,
7608 bfd *ibfd)
411e1bfb 7609{
0ffa91dd 7610 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7611
7612 if (r_symndx >= symtab_hdr->sh_info)
7613 {
7614 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7615 struct elf_link_hash_entry *h;
7616
7617 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7618 h = elf_follow_link (h);
411e1bfb
AM
7619
7620 if (hp != NULL)
7621 *hp = h;
7622
7623 if (symp != NULL)
7624 *symp = NULL;
7625
7626 if (symsecp != NULL)
7627 {
7628 asection *symsec = NULL;
7629 if (h->root.type == bfd_link_hash_defined
7630 || h->root.type == bfd_link_hash_defweak)
7631 symsec = h->root.u.def.section;
7632 *symsecp = symsec;
7633 }
7634
e7b938ca 7635 if (tls_maskp != NULL)
411e1bfb
AM
7636 {
7637 struct ppc_link_hash_entry *eh;
7638
7639 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7640 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7641 }
7642 }
7643 else
7644 {
7645 Elf_Internal_Sym *sym;
7646 Elf_Internal_Sym *locsyms = *locsymsp;
7647
7648 if (locsyms == NULL)
7649 {
7650 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7651 if (locsyms == NULL)
7652 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7653 symtab_hdr->sh_info,
7654 0, NULL, NULL, NULL);
7655 if (locsyms == NULL)
7656 return FALSE;
7657 *locsymsp = locsyms;
7658 }
7659 sym = locsyms + r_symndx;
7660
7661 if (hp != NULL)
7662 *hp = NULL;
7663
7664 if (symp != NULL)
7665 *symp = sym;
7666
7667 if (symsecp != NULL)
cb33740c 7668 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7669
e7b938ca 7670 if (tls_maskp != NULL)
411e1bfb
AM
7671 {
7672 struct got_entry **lgot_ents;
f961d9dd 7673 unsigned char *tls_mask;
411e1bfb 7674
e7b938ca 7675 tls_mask = NULL;
411e1bfb
AM
7676 lgot_ents = elf_local_got_ents (ibfd);
7677 if (lgot_ents != NULL)
7678 {
e054468f
AM
7679 struct plt_entry **local_plt = (struct plt_entry **)
7680 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7681 unsigned char *lgot_masks = (unsigned char *)
e054468f 7682 (local_plt + symtab_hdr->sh_info);
e7b938ca 7683 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7684 }
e7b938ca 7685 *tls_maskp = tls_mask;
411e1bfb
AM
7686 }
7687 }
7688 return TRUE;
7689}
7690
e7b938ca 7691/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7692 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7693 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7694
7695static int
f961d9dd 7696get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7697 unsigned long *toc_symndx,
7698 bfd_vma *toc_addend,
0d4792f7 7699 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7700 const Elf_Internal_Rela *rel,
7701 bfd *ibfd)
411e1bfb
AM
7702{
7703 unsigned long r_symndx;
0d4792f7 7704 int next_r;
411e1bfb
AM
7705 struct elf_link_hash_entry *h;
7706 Elf_Internal_Sym *sym;
7707 asection *sec;
7708 bfd_vma off;
7709
7710 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7711 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7712 return 0;
411e1bfb 7713
37da22e5
AM
7714 if ((*tls_maskp != NULL
7715 && (**tls_maskp & TLS_TLS) != 0
7716 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7717 || sec == NULL
6bee8834 7718 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7719 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7720 return 1;
411e1bfb
AM
7721
7722 /* Look inside a TOC section too. */
7723 if (h != NULL)
7724 {
7725 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7726 off = h->root.u.def.value;
7727 }
7728 else
7729 off = sym->st_value;
7730 off += rel->r_addend;
7731 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7732 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7733 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7734 if (toc_symndx != NULL)
7735 *toc_symndx = r_symndx;
3a71aa26
AM
7736 if (toc_addend != NULL)
7737 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7738 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7739 return 0;
854b41e7 7740 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7741 && (next_r == -1 || next_r == -2))
7742 return 1 - next_r;
951fd09b 7743 return 1;
411e1bfb
AM
7744}
7745
3b421ab3
AM
7746/* Find (or create) an entry in the tocsave hash table. */
7747
7748static struct tocsave_entry *
7749tocsave_find (struct ppc_link_hash_table *htab,
7750 enum insert_option insert,
7751 Elf_Internal_Sym **local_syms,
7752 const Elf_Internal_Rela *irela,
7753 bfd *ibfd)
7754{
7755 unsigned long r_indx;
7756 struct elf_link_hash_entry *h;
7757 Elf_Internal_Sym *sym;
7758 struct tocsave_entry ent, *p;
7759 hashval_t hash;
7760 struct tocsave_entry **slot;
7761
7762 r_indx = ELF64_R_SYM (irela->r_info);
7763 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7764 return NULL;
7765 if (ent.sec == NULL || ent.sec->output_section == NULL)
7766 {
4eca0228 7767 _bfd_error_handler
871b3ab2 7768 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7769 return NULL;
7770 }
7771
7772 if (h != NULL)
7773 ent.offset = h->root.u.def.value;
7774 else
7775 ent.offset = sym->st_value;
7776 ent.offset += irela->r_addend;
7777
7778 hash = tocsave_htab_hash (&ent);
7779 slot = ((struct tocsave_entry **)
7780 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7781 if (slot == NULL)
7782 return NULL;
7783
7784 if (*slot == NULL)
7785 {
7786 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7787 if (p == NULL)
7788 return NULL;
7789 *p = ent;
7790 *slot = p;
7791 }
7792 return *slot;
7793}
7794
754021d0 7795/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7796 code for the old ABI, these will already have been done. */
754021d0
AM
7797
7798static bfd_boolean
7799adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7800{
7801 struct ppc_link_hash_entry *eh;
7802 asection *sym_sec;
74f0fb50 7803 struct _opd_sec_data *opd;
754021d0
AM
7804
7805 if (h->root.type == bfd_link_hash_indirect)
7806 return TRUE;
7807
754021d0
AM
7808 if (h->root.type != bfd_link_hash_defined
7809 && h->root.type != bfd_link_hash_defweak)
7810 return TRUE;
7811
7812 eh = (struct ppc_link_hash_entry *) h;
7813 if (eh->adjust_done)
7814 return TRUE;
7815
7816 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7817 opd = get_opd_info (sym_sec);
7818 if (opd != NULL && opd->adjust != NULL)
754021d0 7819 {
51aecdc5 7820 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7821 if (adjust == -1)
7822 {
7823 /* This entry has been deleted. */
b3fac117 7824 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7825 if (dsec == NULL)
7826 {
7827 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7828 if (discarded_section (dsec))
81688140 7829 {
b3fac117 7830 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7831 break;
7832 }
7833 }
4025353c 7834 eh->elf.root.u.def.value = 0;
81688140 7835 eh->elf.root.u.def.section = dsec;
4025353c
AM
7836 }
7837 else
7838 eh->elf.root.u.def.value += adjust;
754021d0
AM
7839 eh->adjust_done = 1;
7840 }
7841 return TRUE;
7842}
7843
8c1d1bb8 7844/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7845 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7846 have already been determined. */
7847
7848static bfd_boolean
7849dec_dynrel_count (bfd_vma r_info,
7850 asection *sec,
7851 struct bfd_link_info *info,
7852 Elf_Internal_Sym **local_syms,
7853 struct elf_link_hash_entry *h,
19e08130 7854 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7855{
7856 enum elf_ppc64_reloc_type r_type;
19e08130 7857 asection *sym_sec = NULL;
8c1d1bb8
AM
7858
7859 /* Can this reloc be dynamic? This switch, and later tests here
7860 should be kept in sync with the code in check_relocs. */
7861 r_type = ELF64_R_TYPE (r_info);
7862 switch (r_type)
7863 {
7864 default:
7865 return TRUE;
7866
7867 case R_PPC64_TPREL16:
7868 case R_PPC64_TPREL16_LO:
7869 case R_PPC64_TPREL16_HI:
7870 case R_PPC64_TPREL16_HA:
7871 case R_PPC64_TPREL16_DS:
7872 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7873 case R_PPC64_TPREL16_HIGH:
7874 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7875 case R_PPC64_TPREL16_HIGHER:
7876 case R_PPC64_TPREL16_HIGHERA:
7877 case R_PPC64_TPREL16_HIGHEST:
7878 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7879 case R_PPC64_TPREL64:
7880 case R_PPC64_DTPMOD64:
7881 case R_PPC64_DTPREL64:
7882 case R_PPC64_ADDR64:
7883 case R_PPC64_REL30:
7884 case R_PPC64_REL32:
7885 case R_PPC64_REL64:
7886 case R_PPC64_ADDR14:
7887 case R_PPC64_ADDR14_BRNTAKEN:
7888 case R_PPC64_ADDR14_BRTAKEN:
7889 case R_PPC64_ADDR16:
7890 case R_PPC64_ADDR16_DS:
7891 case R_PPC64_ADDR16_HA:
7892 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7893 case R_PPC64_ADDR16_HIGH:
7894 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7895 case R_PPC64_ADDR16_HIGHER:
7896 case R_PPC64_ADDR16_HIGHERA:
7897 case R_PPC64_ADDR16_HIGHEST:
7898 case R_PPC64_ADDR16_HIGHESTA:
7899 case R_PPC64_ADDR16_LO:
7900 case R_PPC64_ADDR16_LO_DS:
7901 case R_PPC64_ADDR24:
7902 case R_PPC64_ADDR32:
7903 case R_PPC64_UADDR16:
7904 case R_PPC64_UADDR32:
7905 case R_PPC64_UADDR64:
7906 case R_PPC64_TOC:
7907 break;
7908 }
7909
7910 if (local_syms != NULL)
7911 {
7912 unsigned long r_symndx;
8c1d1bb8
AM
7913 bfd *ibfd = sec->owner;
7914
7915 r_symndx = ELF64_R_SYM (r_info);
7916 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7917 return FALSE;
7918 }
7919
0e1862bb 7920 if ((bfd_link_pic (info)
1d483afe 7921 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7922 || (h != NULL
198f1157 7923 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7924 || h->root.type == bfd_link_hash_defweak
7925 || !h->def_regular))))
7926 || (ELIMINATE_COPY_RELOCS
0e1862bb 7927 && !bfd_link_pic (info)
8c1d1bb8
AM
7928 && h != NULL
7929 && (h->root.type == bfd_link_hash_defweak
7930 || !h->def_regular)))
7931 ;
7932 else
7933 return TRUE;
7934
7935 if (h != NULL)
6edfbbad 7936 {
19e08130
AM
7937 struct elf_dyn_relocs *p;
7938 struct elf_dyn_relocs **pp;
7939 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7940
7941 /* elf_gc_sweep may have already removed all dyn relocs associated
7942 with local syms for a given section. Also, symbol flags are
7943 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7944 report a dynreloc miscount. */
7945 if (*pp == NULL && info->gc_sections)
7946 return TRUE;
7947
7948 while ((p = *pp) != NULL)
60124e18 7949 {
19e08130
AM
7950 if (p->sec == sec)
7951 {
7952 if (!must_be_dyn_reloc (info, r_type))
7953 p->pc_count -= 1;
7954 p->count -= 1;
7955 if (p->count == 0)
7956 *pp = p->next;
7957 return TRUE;
7958 }
7959 pp = &p->next;
60124e18 7960 }
6edfbbad 7961 }
19e08130
AM
7962 else
7963 {
7964 struct ppc_dyn_relocs *p;
7965 struct ppc_dyn_relocs **pp;
7966 void *vpp;
7967 bfd_boolean is_ifunc;
8c1d1bb8 7968
19e08130
AM
7969 if (local_syms == NULL)
7970 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7971 if (sym_sec == NULL)
7972 sym_sec = sec;
c57da1a7 7973
19e08130
AM
7974 vpp = &elf_section_data (sym_sec)->local_dynrel;
7975 pp = (struct ppc_dyn_relocs **) vpp;
7976
7977 if (*pp == NULL && info->gc_sections)
7978 return TRUE;
7979
7980 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7981 while ((p = *pp) != NULL)
8c1d1bb8 7982 {
19e08130
AM
7983 if (p->sec == sec && p->ifunc == is_ifunc)
7984 {
7985 p->count -= 1;
7986 if (p->count == 0)
7987 *pp = p->next;
7988 return TRUE;
7989 }
7990 pp = &p->next;
8c1d1bb8 7991 }
8c1d1bb8
AM
7992 }
7993
695344c0 7994 /* xgettext:c-format */
cf97bcb0
AM
7995 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7996 sec->owner, sec);
8c1d1bb8
AM
7997 bfd_set_error (bfd_error_bad_value);
7998 return FALSE;
7999}
8000
754021d0
AM
8001/* Remove unused Official Procedure Descriptor entries. Currently we
8002 only remove those associated with functions in discarded link-once
8003 sections, or weakly defined functions that have been overridden. It
8004 would be possible to remove many more entries for statically linked
8005 applications. */
8006
b34976b6 8007bfd_boolean
e7d1c40c 8008ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
8009{
8010 bfd *ibfd;
754021d0 8011 bfd_boolean some_edited = FALSE;
3f764659 8012 asection *need_pad = NULL;
e7d1c40c
AM
8013 struct ppc_link_hash_table *htab;
8014
8015 htab = ppc_hash_table (info);
8016 if (htab == NULL)
8017 return FALSE;
1e2f5b6e 8018
c72f2fb2 8019 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
8020 {
8021 asection *sec;
8022 Elf_Internal_Rela *relstart, *rel, *relend;
8023 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 8024 Elf_Internal_Sym *local_syms;
74f0fb50 8025 struct _opd_sec_data *opd;
51aecdc5 8026 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 8027 bfd_size_type cnt_16b = 0;
1e2f5b6e 8028
854b41e7
AM
8029 if (!is_ppc64_elf (ibfd))
8030 continue;
8031
1e2f5b6e 8032 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 8033 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
8034 continue;
8035
dbaa2011 8036 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
8037 continue;
8038
1e2f5b6e
AM
8039 if (sec->output_section == bfd_abs_section_ptr)
8040 continue;
8041
8042 /* Look through the section relocs. */
8043 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
8044 continue;
8045
6cdc0ccc 8046 local_syms = NULL;
0ffa91dd 8047 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
8048
8049 /* Read the relocations. */
4ce794b7 8050 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 8051 info->keep_memory);
1e2f5b6e 8052 if (relstart == NULL)
b34976b6 8053 return FALSE;
1e2f5b6e
AM
8054
8055 /* First run through the relocs to check they are sane, and to
8056 determine whether we need to edit this opd section. */
b34976b6 8057 need_edit = FALSE;
51aecdc5 8058 broken = FALSE;
3f764659 8059 need_pad = sec;
1e2f5b6e 8060 relend = relstart + sec->reloc_count;
50bc7936 8061 for (rel = relstart; rel < relend; )
1e2f5b6e 8062 {
04c9666a 8063 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
8064 unsigned long r_symndx;
8065 asection *sym_sec;
8066 struct elf_link_hash_entry *h;
8067 Elf_Internal_Sym *sym;
51aecdc5 8068 bfd_vma offset;
1e2f5b6e 8069
51aecdc5 8070 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
8071 only interested in the reloc pointing to a function entry
8072 point. */
51aecdc5
AM
8073 offset = rel->r_offset;
8074 if (rel + 1 == relend
8075 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
8076 {
8077 /* If someone messes with .opd alignment then after a
8078 "ld -r" we might have padding in the middle of .opd.
8079 Also, there's nothing to prevent someone putting
8080 something silly in .opd with the assembler. No .opd
b34976b6 8081 optimization for them! */
3f764659 8082 broken_opd:
4eca0228 8083 _bfd_error_handler
871b3ab2 8084 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 8085 broken = TRUE;
1e2f5b6e
AM
8086 break;
8087 }
8088
50bc7936
AM
8089 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
8090 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
8091 {
4eca0228 8092 _bfd_error_handler
695344c0 8093 /* xgettext:c-format */
871b3ab2 8094 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 8095 ibfd, r_type);
51aecdc5 8096 broken = TRUE;
50bc7936
AM
8097 break;
8098 }
8099
1e2f5b6e 8100 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
8101 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8102 r_symndx, ibfd))
50bc7936 8103 goto error_ret;
1e2f5b6e
AM
8104
8105 if (sym_sec == NULL || sym_sec->owner == NULL)
8106 {
411e1bfb
AM
8107 const char *sym_name;
8108 if (h != NULL)
8109 sym_name = h->root.root.string;
8110 else
26c61ae5
L
8111 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8112 sym_sec);
411e1bfb 8113
4eca0228 8114 _bfd_error_handler
695344c0 8115 /* xgettext:c-format */
871b3ab2 8116 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 8117 ibfd, sym_name);
51aecdc5 8118 broken = TRUE;
1e2f5b6e
AM
8119 break;
8120 }
8121
51020317
AM
8122 /* opd entries are always for functions defined in the
8123 current input bfd. If the symbol isn't defined in the
8124 input bfd, then we won't be using the function in this
8125 bfd; It must be defined in a linkonce section in another
8126 bfd, or is weak. It's also possible that we are
8127 discarding the function due to a linker script /DISCARD/,
8128 which we test for via the output_section. */
8129 if (sym_sec->owner != ibfd
8130 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 8131 need_edit = TRUE;
1e2f5b6e 8132
50bc7936 8133 rel += 2;
51aecdc5
AM
8134 if (rel + 1 == relend
8135 || (rel + 2 < relend
8136 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8137 ++rel;
8138
8139 if (rel == relend)
3f764659
JJ
8140 {
8141 if (sec->size == offset + 24)
8142 {
8143 need_pad = NULL;
8144 break;
8145 }
51aecdc5 8146 if (sec->size == offset + 16)
3f764659
JJ
8147 {
8148 cnt_16b++;
8149 break;
8150 }
8151 goto broken_opd;
8152 }
3f764659
JJ
8153 else if (rel + 1 < relend
8154 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8155 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8156 {
51aecdc5
AM
8157 if (rel[0].r_offset == offset + 16)
8158 cnt_16b++;
8159 else if (rel[0].r_offset != offset + 24)
8160 goto broken_opd;
3f764659
JJ
8161 }
8162 else
8163 goto broken_opd;
1e2f5b6e
AM
8164 }
8165
e7d1c40c 8166 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8167
51aecdc5 8168 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8169 {
8170 Elf_Internal_Rela *write_rel;
d4730f92 8171 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8172 bfd_byte *rptr, *wptr;
983bddc8 8173 bfd_byte *new_contents;
74f0fb50
AM
8174 bfd_size_type amt;
8175
983bddc8 8176 new_contents = NULL;
51aecdc5 8177 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8178 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8179 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8180 if (opd->adjust == NULL)
8181 return FALSE;
1e2f5b6e
AM
8182
8183 /* This seems a waste of time as input .opd sections are all
8184 zeros as generated by gcc, but I suppose there's no reason
8185 this will always be so. We might start putting something in
8186 the third word of .opd entries. */
8187 if ((sec->flags & SEC_IN_MEMORY) == 0)
8188 {
eea6121a
AM
8189 bfd_byte *loc;
8190 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8191 {
eea6121a
AM
8192 if (loc != NULL)
8193 free (loc);
50bc7936 8194 error_ret:
6cdc0ccc
AM
8195 if (local_syms != NULL
8196 && symtab_hdr->contents != (unsigned char *) local_syms)
8197 free (local_syms);
6cdc0ccc
AM
8198 if (elf_section_data (sec)->relocs != relstart)
8199 free (relstart);
b34976b6 8200 return FALSE;
6cdc0ccc 8201 }
1e2f5b6e
AM
8202 sec->contents = loc;
8203 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8204 }
8205
8206 elf_section_data (sec)->relocs = relstart;
8207
3f764659 8208 new_contents = sec->contents;
3f764659
JJ
8209 if (add_aux_fields)
8210 {
8211 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8212 if (new_contents == NULL)
8213 return FALSE;
51aecdc5 8214 need_pad = NULL;
3f764659 8215 }
b4f4e59f
AM
8216 wptr = new_contents;
8217 rptr = sec->contents;
1e2f5b6e 8218 write_rel = relstart;
51aecdc5 8219 for (rel = relstart; rel < relend; )
1e2f5b6e 8220 {
50bc7936
AM
8221 unsigned long r_symndx;
8222 asection *sym_sec;
8223 struct elf_link_hash_entry *h;
51aecdc5 8224 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8225 Elf_Internal_Sym *sym;
51aecdc5
AM
8226 long opd_ent_size;
8227 Elf_Internal_Rela *next_rel;
8228 bfd_boolean skip;
50bc7936
AM
8229
8230 r_symndx = ELF64_R_SYM (rel->r_info);
8231 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8232 r_symndx, ibfd))
50bc7936
AM
8233 goto error_ret;
8234
51aecdc5
AM
8235 next_rel = rel + 2;
8236 if (next_rel + 1 == relend
8237 || (next_rel + 2 < relend
8238 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8239 ++next_rel;
8240
8241 /* See if the .opd entry is full 24 byte or
8242 16 byte (with fd_aux entry overlapped with next
8243 fd_func). */
8244 opd_ent_size = 24;
8245 if (next_rel == relend)
1e2f5b6e 8246 {
51aecdc5 8247 if (sec->size == rel->r_offset + 16)
3f764659 8248 opd_ent_size = 16;
51aecdc5
AM
8249 }
8250 else if (next_rel->r_offset == rel->r_offset + 16)
8251 opd_ent_size = 16;
3f764659 8252
51aecdc5
AM
8253 if (h != NULL
8254 && h->root.root.string[0] == '.')
8255 {
8c5b4e52
AM
8256 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8257 if (fdh != NULL)
8258 {
8259 fdh = ppc_follow_link (fdh);
8260 if (fdh->elf.root.type != bfd_link_hash_defined
8261 && fdh->elf.root.type != bfd_link_hash_defweak)
8262 fdh = NULL;
8263 }
51aecdc5 8264 }
1e2f5b6e 8265
51aecdc5
AM
8266 skip = (sym_sec->owner != ibfd
8267 || sym_sec->output_section == bfd_abs_section_ptr);
8268 if (skip)
8269 {
8270 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8271 {
51aecdc5
AM
8272 /* Arrange for the function descriptor sym
8273 to be dropped. */
8274 fdh->elf.root.u.def.value = 0;
8275 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8276 }
51aecdc5 8277 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8278
0e1862bb 8279 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8280 rel = next_rel;
8281 else
8282 while (1)
8283 {
8284 if (!dec_dynrel_count (rel->r_info, sec, info,
8285 NULL, h, sym))
8286 goto error_ret;
754021d0 8287
51aecdc5
AM
8288 if (++rel == next_rel)
8289 break;
1e2f5b6e 8290
51aecdc5
AM
8291 r_symndx = ELF64_R_SYM (rel->r_info);
8292 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8293 r_symndx, ibfd))
8294 goto error_ret;
8295 }
50bc7936
AM
8296 }
8297 else
1e2f5b6e 8298 {
51aecdc5
AM
8299 /* We'll be keeping this opd entry. */
8300 long adjust;
8301
8302 if (fdh != NULL)
8303 {
8304 /* Redefine the function descriptor symbol to
8305 this location in the opd section. It is
8306 necessary to update the value here rather
8307 than using an array of adjustments as we do
8308 for local symbols, because various places
8309 in the generic ELF code use the value
8310 stored in u.def.value. */
8311 fdh->elf.root.u.def.value = wptr - new_contents;
8312 fdh->adjust_done = 1;
8313 }
8314
8315 /* Local syms are a bit tricky. We could
8316 tweak them as they can be cached, but
8317 we'd need to look through the local syms
8318 for the function descriptor sym which we
8319 don't have at the moment. So keep an
8320 array of adjustments. */
8321 adjust = (wptr - new_contents) - (rptr - sec->contents);
8322 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8323
8324 if (wptr != rptr)
8325 memcpy (wptr, rptr, opd_ent_size);
8326 wptr += opd_ent_size;
8327 if (add_aux_fields && opd_ent_size == 16)
8328 {
8329 memset (wptr, '\0', 8);
8330 wptr += 8;
8331 }
8332
50bc7936 8333 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8334 new opd entries. */
8335 for ( ; rel != next_rel; ++rel)
8336 {
8337 rel->r_offset += adjust;
8338 if (write_rel != rel)
8339 memcpy (write_rel, rel, sizeof (*rel));
8340 ++write_rel;
8341 }
1e2f5b6e 8342 }
51aecdc5
AM
8343
8344 rptr += opd_ent_size;
1e2f5b6e
AM
8345 }
8346
3f764659 8347 sec->size = wptr - new_contents;
1e2f5b6e 8348 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8349 if (add_aux_fields)
8350 {
8351 free (sec->contents);
8352 sec->contents = new_contents;
8353 }
8354
05bf9422 8355 /* Fudge the header size too, as this is used later in
cdcf6e38 8356 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8357 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8358 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8359 some_edited = TRUE;
1e2f5b6e 8360 }
6cdc0ccc 8361 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8362 free (relstart);
6cdc0ccc 8363
411e1bfb
AM
8364 if (local_syms != NULL
8365 && symtab_hdr->contents != (unsigned char *) local_syms)
8366 {
8367 if (!info->keep_memory)
8368 free (local_syms);
8369 else
8370 symtab_hdr->contents = (unsigned char *) local_syms;
8371 }
8372 }
8373
754021d0
AM
8374 if (some_edited)
8375 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8376
3f764659
JJ
8377 /* If we are doing a final link and the last .opd entry is just 16 byte
8378 long, add a 8 byte padding after it. */
0e1862bb 8379 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8380 {
8381 bfd_byte *p;
8382
8383 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8384 {
8385 BFD_ASSERT (need_pad->size > 0);
8386
8387 p = bfd_malloc (need_pad->size + 8);
8388 if (p == NULL)
8389 return FALSE;
699733f6 8390
3f764659
JJ
8391 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8392 p, 0, need_pad->size))
8393 return FALSE;
8394
8395 need_pad->contents = p;
8396 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8397 }
8398 else
8399 {
8400 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8401 if (p == NULL)
8402 return FALSE;
8403
8404 need_pad->contents = p;
8405 }
8406
8407 memset (need_pad->contents + need_pad->size, 0, 8);
8408 need_pad->size += 8;
8409 }
8410
411e1bfb
AM
8411 return TRUE;
8412}
8413
3e04d765
AM
8414/* Analyze inline PLT call relocations to see whether calls to locally
8415 defined functions can be converted to direct calls. */
8416
8417bfd_boolean
8418ppc64_elf_inline_plt (struct bfd_link_info *info)
8419{
8420 struct ppc_link_hash_table *htab;
8421 bfd *ibfd;
8422 asection *sec;
8423 bfd_vma low_vma, high_vma, limit;
8424
8425 htab = ppc_hash_table (info);
8426 if (htab == NULL)
8427 return FALSE;
8428
8429 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8430 reduced somewhat to cater for possible stubs that might be added
8431 between the call and its destination. */
8432 if (htab->params->group_size < 0)
8433 {
8434 limit = -htab->params->group_size;
8435 if (limit == 1)
8436 limit = 0x1e00000;
8437 }
8438 else
8439 {
8440 limit = htab->params->group_size;
8441 if (limit == 1)
8442 limit = 0x1c00000;
8443 }
8444
8445 low_vma = -1;
8446 high_vma = 0;
8447 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8448 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8449 {
8450 if (low_vma > sec->vma)
8451 low_vma = sec->vma;
8452 if (high_vma < sec->vma + sec->size)
8453 high_vma = sec->vma + sec->size;
8454 }
8455
8456 /* If a "bl" can reach anywhere in local code sections, then we can
8457 convert all inline PLT sequences to direct calls when the symbol
8458 is local. */
8459 if (high_vma - low_vma < limit)
8460 {
8461 htab->can_convert_all_inline_plt = 1;
8462 return TRUE;
8463 }
8464
8465 /* Otherwise, go looking through relocs for cases where a direct
8466 call won't reach. Mark the symbol on any such reloc to disable
8467 the optimization and keep the PLT entry as it seems likely that
8468 this will be better than creating trampolines. Note that this
8469 will disable the optimization for all inline PLT calls to a
8470 particular symbol, not just those that won't reach. The
8471 difficulty in doing a more precise optimization is that the
8472 linker needs to make a decision depending on whether a
8473 particular R_PPC64_PLTCALL insn can be turned into a direct
8474 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8475 the sequence, and there is nothing that ties those relocs
8476 together except their symbol. */
8477
8478 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8479 {
8480 Elf_Internal_Shdr *symtab_hdr;
8481 Elf_Internal_Sym *local_syms;
8482
8483 if (!is_ppc64_elf (ibfd))
8484 continue;
8485
8486 local_syms = NULL;
8487 symtab_hdr = &elf_symtab_hdr (ibfd);
8488
8489 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8490 if (ppc64_elf_section_data (sec)->has_pltcall
8491 && !bfd_is_abs_section (sec->output_section))
8492 {
8493 Elf_Internal_Rela *relstart, *rel, *relend;
8494
8495 /* Read the relocations. */
8496 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8497 info->keep_memory);
8498 if (relstart == NULL)
8499 return FALSE;
8500
8501 relend = relstart + sec->reloc_count;
8502 for (rel = relstart; rel < relend; )
8503 {
8504 enum elf_ppc64_reloc_type r_type;
8505 unsigned long r_symndx;
8506 asection *sym_sec;
8507 struct elf_link_hash_entry *h;
8508 Elf_Internal_Sym *sym;
8509 unsigned char *tls_maskp;
8510
8511 r_type = ELF64_R_TYPE (rel->r_info);
8512 if (r_type != R_PPC64_PLTCALL)
8513 continue;
8514
8515 r_symndx = ELF64_R_SYM (rel->r_info);
8516 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8517 r_symndx, ibfd))
8518 {
8519 if (elf_section_data (sec)->relocs != relstart)
8520 free (relstart);
8521 if (local_syms != NULL
8522 && symtab_hdr->contents != (unsigned char *) local_syms)
8523 free (local_syms);
8524 return FALSE;
8525 }
8526
8527 if (sym_sec != NULL && sym_sec->output_section != NULL)
8528 {
8529 bfd_vma from, to;
8530 if (h != NULL)
8531 to = h->root.u.def.value;
8532 else
8533 to = sym->st_value;
8534 to += (rel->r_addend
8535 + sym_sec->output_offset
8536 + sym_sec->output_section->vma);
8537 from = (rel->r_offset
8538 + sec->output_offset
8539 + sec->output_section->vma);
8540 if (to - from + limit < 2 * limit)
8541 *tls_maskp &= ~PLT_KEEP;
8542 }
8543 }
8544 if (elf_section_data (sec)->relocs != relstart)
8545 free (relstart);
8546 }
8547
8548 if (local_syms != NULL
8549 && symtab_hdr->contents != (unsigned char *) local_syms)
8550 {
8551 if (!info->keep_memory)
8552 free (local_syms);
8553 else
8554 symtab_hdr->contents = (unsigned char *) local_syms;
8555 }
8556 }
8557
8558 return TRUE;
8559}
8560
e1918d23 8561/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8562
e1918d23 8563asection *
e7d1c40c 8564ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8565{
411e1bfb
AM
8566 struct ppc_link_hash_table *htab;
8567
411e1bfb 8568 htab = ppc_hash_table (info);
4dfe6ac6
NC
8569 if (htab == NULL)
8570 return NULL;
8571
ee67d69a
AM
8572 if (abiversion (info->output_bfd) == 1)
8573 htab->opd_abi = 1;
8574
e7d1c40c 8575 if (htab->params->no_multi_toc)
33c0ec9d
AM
8576 htab->do_multi_toc = 0;
8577 else if (!htab->do_multi_toc)
e7d1c40c 8578 htab->params->no_multi_toc = 1;
33c0ec9d 8579
8b5f1ed8
AM
8580 /* Default to --no-plt-localentry, as this option can cause problems
8581 with symbol interposition. For example, glibc libpthread.so and
8582 libc.so duplicate many pthread symbols, with a fallback
8583 implementation in libc.so. In some cases the fallback does more
8584 work than the pthread implementation. __pthread_condattr_destroy
8585 is one such symbol: the libpthread.so implementation is
8586 localentry:0 while the libc.so implementation is localentry:8.
8587 An app that "cleverly" uses dlopen to only load necessary
8588 libraries at runtime may omit loading libpthread.so when not
8589 running multi-threaded, which then results in the libc.so
8590 fallback symbols being used and ld.so complaining. Now there
8591 are workarounds in ld (see non_zero_localentry) to detect the
8592 pthread situation, but that may not be the only case where
8593 --plt-localentry can cause trouble. */
f378ab09 8594 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8595 htab->params->plt_localentry0 = 0;
d44c746a
AM
8596 if (htab->params->plt_localentry0
8597 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8598 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
8599 _bfd_error_handler
8600 (_("warning: --plt-localentry is especially dangerous without "
8601 "ld.so support to detect ABI violations"));
f378ab09 8602
3a71aa26
AM
8603 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8604 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8605 FALSE, FALSE, TRUE));
a7f2871e
AM
8606 /* Move dynamic linking info to the function descriptor sym. */
8607 if (htab->tls_get_addr != NULL)
8608 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8609 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8610 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8611 FALSE, FALSE, TRUE));
7c9cf415 8612 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8613 {
8614 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8615
8616 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8617 FALSE, FALSE, TRUE);
8618 if (opt != NULL)
8619 func_desc_adjust (opt, info);
8620 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8621 FALSE, FALSE, TRUE);
8622 if (opt_fd != NULL
8623 && (opt_fd->root.type == bfd_link_hash_defined
8624 || opt_fd->root.type == bfd_link_hash_defweak))
8625 {
8626 /* If glibc supports an optimized __tls_get_addr call stub,
8627 signalled by the presence of __tls_get_addr_opt, and we'll
8628 be calling __tls_get_addr via a plt call stub, then
8629 make __tls_get_addr point to __tls_get_addr_opt. */
8630 tga_fd = &htab->tls_get_addr_fd->elf;
8631 if (htab->elf.dynamic_sections_created
8632 && tga_fd != NULL
8633 && (tga_fd->type == STT_FUNC
8634 || tga_fd->needs_plt)
8635 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8636 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8637 {
8638 struct plt_entry *ent;
8639
8640 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8641 if (ent->plt.refcount > 0)
8642 break;
8643 if (ent != NULL)
8644 {
8645 tga_fd->root.type = bfd_link_hash_indirect;
8646 tga_fd->root.u.i.link = &opt_fd->root;
8647 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8648 opt_fd->mark = 1;
a7f2871e
AM
8649 if (opt_fd->dynindx != -1)
8650 {
8651 /* Use __tls_get_addr_opt in dynamic relocations. */
8652 opt_fd->dynindx = -1;
8653 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8654 opt_fd->dynstr_index);
8655 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8656 return NULL;
a7f2871e
AM
8657 }
8658 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8659 tga = &htab->tls_get_addr->elf;
8660 if (opt != NULL && tga != NULL)
8661 {
8662 tga->root.type = bfd_link_hash_indirect;
8663 tga->root.u.i.link = &opt->root;
8664 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8665 opt->mark = 1;
a7f2871e
AM
8666 _bfd_elf_link_hash_hide_symbol (info, opt,
8667 tga->forced_local);
8668 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8669 }
8670 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8671 htab->tls_get_addr_fd->is_func_descriptor = 1;
8672 if (htab->tls_get_addr != NULL)
8673 {
8674 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8675 htab->tls_get_addr->is_func = 1;
8676 }
8677 }
8678 }
8679 }
7c9cf415
AM
8680 else if (htab->params->tls_get_addr_opt < 0)
8681 htab->params->tls_get_addr_opt = 0;
a7f2871e 8682 }
33c0ec9d 8683 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8684}
8387904d 8685
3a71aa26
AM
8686/* Return TRUE iff REL is a branch reloc with a global symbol matching
8687 HASH1 or HASH2. */
8387904d 8688
3a71aa26
AM
8689static bfd_boolean
8690branch_reloc_hash_match (const bfd *ibfd,
8691 const Elf_Internal_Rela *rel,
8692 const struct ppc_link_hash_entry *hash1,
8693 const struct ppc_link_hash_entry *hash2)
8694{
8695 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8696 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8697 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8698
e054468f 8699 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8700 {
3a71aa26
AM
8701 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8702 struct elf_link_hash_entry *h;
8387904d 8703
3a71aa26 8704 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8705 h = elf_follow_link (h);
3a71aa26
AM
8706 if (h == &hash1->elf || h == &hash2->elf)
8707 return TRUE;
a48ebf4d 8708 }
3a71aa26 8709 return FALSE;
951fd09b 8710}
411e1bfb 8711
951fd09b
AM
8712/* Run through all the TLS relocs looking for optimization
8713 opportunities. The linker has been hacked (see ppc64elf.em) to do
8714 a preliminary section layout so that we know the TLS segment
8715 offsets. We can't optimize earlier because some optimizations need
8716 to know the tp offset, and we need to optimize before allocating
8717 dynamic relocations. */
8718
8719bfd_boolean
33c0ec9d 8720ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8721{
8722 bfd *ibfd;
8723 asection *sec;
8724 struct ppc_link_hash_table *htab;
663a1470 8725 unsigned char *toc_ref;
102890f0 8726 int pass;
951fd09b 8727
3cbc1e5e 8728 if (!bfd_link_executable (info))
411e1bfb
AM
8729 return TRUE;
8730
951fd09b 8731 htab = ppc_hash_table (info);
4dfe6ac6
NC
8732 if (htab == NULL)
8733 return FALSE;
8734
663a1470
AM
8735 /* Make two passes over the relocs. On the first pass, mark toc
8736 entries involved with tls relocs, and check that tls relocs
8737 involved in setting up a tls_get_addr call are indeed followed by
8738 such a call. If they are not, we can't do any tls optimization.
8739 On the second pass twiddle tls_mask flags to notify
8740 relocate_section that optimization can be done, and adjust got
8741 and plt refcounts. */
8742 toc_ref = NULL;
8743 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8744 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8745 {
8746 Elf_Internal_Sym *locsyms = NULL;
8747 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8748
102890f0
AM
8749 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8750 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8751 {
8752 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8753 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8754
102890f0
AM
8755 /* Read the relocations. */
8756 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8757 info->keep_memory);
8758 if (relstart == NULL)
2915c55b
JK
8759 {
8760 free (toc_ref);
8761 return FALSE;
8762 }
411e1bfb 8763
102890f0
AM
8764 relend = relstart + sec->reloc_count;
8765 for (rel = relstart; rel < relend; rel++)
8766 {
8767 enum elf_ppc64_reloc_type r_type;
8768 unsigned long r_symndx;
8769 struct elf_link_hash_entry *h;
8770 Elf_Internal_Sym *sym;
8771 asection *sym_sec;
f961d9dd
AM
8772 unsigned char *tls_mask;
8773 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8774 bfd_vma value;
8775 bfd_boolean ok_tprel, is_local;
8776 long toc_ref_index = 0;
8777 int expecting_tls_get_addr = 0;
663a1470 8778 bfd_boolean ret = FALSE;
411e1bfb 8779
102890f0
AM
8780 r_symndx = ELF64_R_SYM (rel->r_info);
8781 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8782 r_symndx, ibfd))
8783 {
8784 err_free_rel:
8785 if (elf_section_data (sec)->relocs != relstart)
8786 free (relstart);
8787 if (toc_ref != NULL)
8788 free (toc_ref);
8789 if (locsyms != NULL
0ffa91dd 8790 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8791 != (unsigned char *) locsyms))
8792 free (locsyms);
663a1470 8793 return ret;
102890f0 8794 }
411e1bfb 8795
102890f0
AM
8796 if (h != NULL)
8797 {
766bc656
AM
8798 if (h->root.type == bfd_link_hash_defined
8799 || h->root.type == bfd_link_hash_defweak)
8800 value = h->root.u.def.value;
8801 else if (h->root.type == bfd_link_hash_undefweak)
8802 value = 0;
8803 else
663a1470
AM
8804 {
8805 found_tls_get_addr_arg = 0;
8806 continue;
8807 }
102890f0
AM
8808 }
8809 else
8810 /* Symbols referenced by TLS relocs must be of type
8811 STT_TLS. So no need for .opd local sym adjust. */
8812 value = sym->st_value;
8813
8814 ok_tprel = FALSE;
8815 is_local = FALSE;
8816 if (h == NULL
8817 || !h->def_dynamic)
8818 {
8819 is_local = TRUE;
766bc656
AM
8820 if (h != NULL
8821 && h->root.type == bfd_link_hash_undefweak)
8822 ok_tprel = TRUE;
c27b8c2a
AM
8823 else if (sym_sec != NULL
8824 && sym_sec->output_section != NULL)
766bc656
AM
8825 {
8826 value += sym_sec->output_offset;
8827 value += sym_sec->output_section->vma;
8828 value -= htab->elf.tls_sec->vma;
8829 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8830 < (bfd_vma) 1 << 32);
8831 }
102890f0 8832 }
951fd09b 8833
102890f0 8834 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8835 /* If this section has old-style __tls_get_addr calls
8836 without marker relocs, then check that each
8837 __tls_get_addr call reloc is preceded by a reloc
8838 that conceivably belongs to the __tls_get_addr arg
8839 setup insn. If we don't find matching arg setup
8840 relocs, don't do any tls optimization. */
8841 if (pass == 0
8842 && sec->has_tls_get_addr_call
8843 && h != NULL
8844 && (h == &htab->tls_get_addr->elf
8845 || h == &htab->tls_get_addr_fd->elf)
8846 && !found_tls_get_addr_arg
8847 && is_branch_reloc (r_type))
8848 {
25f53a85 8849 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8850 "TLS optimization disabled\n"),
8851 ibfd, sec, rel->r_offset);
8852 ret = TRUE;
8853 goto err_free_rel;
8854 }
8855
8856 found_tls_get_addr_arg = 0;
102890f0
AM
8857 switch (r_type)
8858 {
8859 case R_PPC64_GOT_TLSLD16:
8860 case R_PPC64_GOT_TLSLD16_LO:
8861 expecting_tls_get_addr = 1;
663a1470 8862 found_tls_get_addr_arg = 1;
1a0670f3 8863 /* Fall through. */
102890f0
AM
8864
8865 case R_PPC64_GOT_TLSLD16_HI:
8866 case R_PPC64_GOT_TLSLD16_HA:
8867 /* These relocs should never be against a symbol
8868 defined in a shared lib. Leave them alone if
8869 that turns out to be the case. */
8870 if (!is_local)
8871 continue;
411e1bfb 8872
102890f0 8873 /* LD -> LE */
411e1bfb 8874 tls_set = 0;
102890f0
AM
8875 tls_clear = TLS_LD;
8876 tls_type = TLS_TLS | TLS_LD;
8877 break;
411e1bfb 8878
102890f0
AM
8879 case R_PPC64_GOT_TLSGD16:
8880 case R_PPC64_GOT_TLSGD16_LO:
8881 expecting_tls_get_addr = 1;
663a1470 8882 found_tls_get_addr_arg = 1;
1a0670f3 8883 /* Fall through. */
102890f0
AM
8884
8885 case R_PPC64_GOT_TLSGD16_HI:
8886 case R_PPC64_GOT_TLSGD16_HA:
8887 if (ok_tprel)
8888 /* GD -> LE */
411e1bfb 8889 tls_set = 0;
102890f0
AM
8890 else
8891 /* GD -> IE */
8892 tls_set = TLS_TLS | TLS_TPRELGD;
8893 tls_clear = TLS_GD;
8894 tls_type = TLS_TLS | TLS_GD;
8895 break;
8896
8897 case R_PPC64_GOT_TPREL16_DS:
8898 case R_PPC64_GOT_TPREL16_LO_DS:
8899 case R_PPC64_GOT_TPREL16_HI:
8900 case R_PPC64_GOT_TPREL16_HA:
8901 if (ok_tprel)
8902 {
8903 /* IE -> LE */
8904 tls_set = 0;
8905 tls_clear = TLS_TPREL;
8906 tls_type = TLS_TLS | TLS_TPREL;
8907 break;
8908 }
411e1bfb
AM
8909 continue;
8910
727fc41e
AM
8911 case R_PPC64_TLSGD:
8912 case R_PPC64_TLSLD:
23cedd1d
AM
8913 if (rel + 1 < relend
8914 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8915 {
8916 if (pass != 0
8917 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8918 {
8919 r_symndx = ELF64_R_SYM (rel[1].r_info);
8920 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8921 r_symndx, ibfd))
8922 goto err_free_rel;
8923 if (h != NULL)
8924 {
8925 struct plt_entry *ent = NULL;
8926
8927 for (ent = h->plt.plist;
8928 ent != NULL;
8929 ent = ent->next)
8930 if (ent->addend == rel[1].r_addend)
8931 break;
8932
8933 if (ent != NULL
8934 && ent->plt.refcount > 0)
8935 ent->plt.refcount -= 1;
8936 }
8937 }
8938 continue;
8939 }
663a1470 8940 found_tls_get_addr_arg = 1;
1a0670f3 8941 /* Fall through. */
663a1470
AM
8942
8943 case R_PPC64_TLS:
8944 case R_PPC64_TOC16:
8945 case R_PPC64_TOC16_LO:
102890f0
AM
8946 if (sym_sec == NULL || sym_sec != toc)
8947 continue;
8948
8949 /* Mark this toc entry as referenced by a TLS
8950 code sequence. We can do that now in the
8951 case of R_PPC64_TLS, and after checking for
8952 tls_get_addr for the TOC16 relocs. */
8953 if (toc_ref == NULL)
663a1470
AM
8954 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8955 if (toc_ref == NULL)
8956 goto err_free_rel;
8957
102890f0
AM
8958 if (h != NULL)
8959 value = h->root.u.def.value;
8960 else
8961 value = sym->st_value;
8962 value += rel->r_addend;
73242275
AM
8963 if (value % 8 != 0)
8964 continue;
8965 BFD_ASSERT (value < toc->size
8966 && toc->output_offset % 8 == 0);
663a1470 8967 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8968 if (r_type == R_PPC64_TLS
8969 || r_type == R_PPC64_TLSGD
8970 || r_type == R_PPC64_TLSLD)
102890f0
AM
8971 {
8972 toc_ref[toc_ref_index] = 1;
8973 continue;
8974 }
8975
8976 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8977 continue;
8978
8979 tls_set = 0;
8980 tls_clear = 0;
8981 expecting_tls_get_addr = 2;
8982 break;
8983
8984 case R_PPC64_TPREL64:
8985 if (pass == 0
8986 || sec != toc
8987 || toc_ref == NULL
663a1470 8988 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8989 continue;
8990 if (ok_tprel)
8991 {
8992 /* IE -> LE */
8993 tls_set = TLS_EXPLICIT;
8994 tls_clear = TLS_TPREL;
8995 break;
8996 }
8997 continue;
8998
8999 case R_PPC64_DTPMOD64:
9000 if (pass == 0
9001 || sec != toc
9002 || toc_ref == NULL
663a1470 9003 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
9004 continue;
9005 if (rel + 1 < relend
9006 && (rel[1].r_info
9007 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
9008 && rel[1].r_offset == rel->r_offset + 8)
9009 {
9010 if (ok_tprel)
9011 /* GD -> LE */
9012 tls_set = TLS_EXPLICIT | TLS_GD;
9013 else
9014 /* GD -> IE */
9015 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
9016 tls_clear = TLS_GD;
9017 }
9018 else
9019 {
9020 if (!is_local)
9021 continue;
9022
9023 /* LD -> LE */
9024 tls_set = TLS_EXPLICIT;
9025 tls_clear = TLS_LD;
9026 }
9027 break;
9028
9029 default:
9030 continue;
9031 }
9032
9033 if (pass == 0)
9034 {
727fc41e
AM
9035 if (!expecting_tls_get_addr
9036 || !sec->has_tls_get_addr_call)
102890f0
AM
9037 continue;
9038
3a71aa26
AM
9039 if (rel + 1 < relend
9040 && branch_reloc_hash_match (ibfd, rel + 1,
9041 htab->tls_get_addr,
9042 htab->tls_get_addr_fd))
102890f0 9043 {
3a71aa26 9044 if (expecting_tls_get_addr == 2)
102890f0 9045 {
3a71aa26 9046 /* Check for toc tls entries. */
f961d9dd 9047 unsigned char *toc_tls;
3a71aa26
AM
9048 int retval;
9049
9050 retval = get_tls_mask (&toc_tls, NULL, NULL,
9051 &locsyms,
9052 rel, ibfd);
9053 if (retval == 0)
9054 goto err_free_rel;
663a1470
AM
9055 if (toc_tls != NULL)
9056 {
37da22e5
AM
9057 if ((*toc_tls & TLS_TLS) != 0
9058 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
9059 found_tls_get_addr_arg = 1;
9060 if (retval > 1)
9061 toc_ref[toc_ref_index] = 1;
9062 }
102890f0 9063 }
3a71aa26 9064 continue;
102890f0
AM
9065 }
9066
102890f0
AM
9067 /* Uh oh, we didn't find the expected call. We
9068 could just mark this symbol to exclude it
9069 from tls optimization but it's safer to skip
663a1470 9070 the entire optimization. */
695344c0 9071 /* xgettext:c-format */
25f53a85 9072 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
9073 "TLS optimization disabled\n"),
9074 ibfd, sec, rel->r_offset);
9075 ret = TRUE;
9076 goto err_free_rel;
102890f0
AM
9077 }
9078
37da22e5
AM
9079 /* If we don't have old-style __tls_get_addr calls
9080 without TLSGD/TLSLD marker relocs, and we haven't
9081 found a new-style __tls_get_addr call with a
9082 marker for this symbol, then we either have a
9083 broken object file or an -mlongcall style
9084 indirect call to __tls_get_addr without a marker.
9085 Disable optimization in this case. */
9086 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
9087 && (tls_set & TLS_EXPLICIT) == 0
9088 && !sec->has_tls_get_addr_call
9089 && ((*tls_mask & (TLS_TLS | TLS_MARK))
9090 != (TLS_TLS | TLS_MARK)))
9091 continue;
9092
23cedd1d 9093 if (expecting_tls_get_addr)
102890f0 9094 {
23cedd1d
AM
9095 struct plt_entry *ent = NULL;
9096
9097 if (htab->tls_get_addr != NULL)
9098 for (ent = htab->tls_get_addr->elf.plt.plist;
9099 ent != NULL;
9100 ent = ent->next)
9101 if (ent->addend == 0)
102890f0 9102 break;
411e1bfb 9103
23cedd1d
AM
9104 if (ent == NULL && htab->tls_get_addr_fd != NULL)
9105 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
9106 ent != NULL;
9107 ent = ent->next)
9108 if (ent->addend == 0)
102890f0 9109 break;
23cedd1d
AM
9110
9111 if (ent != NULL
9112 && ent->plt.refcount > 0)
9113 ent->plt.refcount -= 1;
102890f0 9114 }
411e1bfb 9115
102890f0 9116 if (tls_clear == 0)
30038c59
AM
9117 continue;
9118
102890f0
AM
9119 if ((tls_set & TLS_EXPLICIT) == 0)
9120 {
9121 struct got_entry *ent;
411e1bfb 9122
102890f0
AM
9123 /* Adjust got entry for this reloc. */
9124 if (h != NULL)
9125 ent = h->got.glist;
9126 else
9127 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 9128
102890f0
AM
9129 for (; ent != NULL; ent = ent->next)
9130 if (ent->addend == rel->r_addend
9131 && ent->owner == ibfd
9132 && ent->tls_type == tls_type)
9133 break;
9134 if (ent == NULL)
9135 abort ();
411e1bfb 9136
102890f0
AM
9137 if (tls_set == 0)
9138 {
9139 /* We managed to get rid of a got entry. */
9140 if (ent->got.refcount > 0)
9141 ent->got.refcount -= 1;
9142 }
9143 }
9144 else
9145 {
9146 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9147 we'll lose one or two dyn relocs. */
9148 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 9149 NULL, h, sym))
102890f0 9150 return FALSE;
411e1bfb 9151
102890f0
AM
9152 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9153 {
9154 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 9155 NULL, h, sym))
102890f0
AM
9156 return FALSE;
9157 }
9158 }
411e1bfb 9159
102890f0
AM
9160 *tls_mask |= tls_set;
9161 *tls_mask &= ~tls_clear;
9162 }
8c1d1bb8 9163
102890f0
AM
9164 if (elf_section_data (sec)->relocs != relstart)
9165 free (relstart);
9166 }
411e1bfb 9167
663a1470
AM
9168 if (locsyms != NULL
9169 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9170 {
9171 if (!info->keep_memory)
9172 free (locsyms);
9173 else
9174 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9175 }
9176 }
411e1bfb 9177
663a1470
AM
9178 if (toc_ref != NULL)
9179 free (toc_ref);
9a23f96e 9180 htab->do_tls_opt = 1;
b34976b6 9181 return TRUE;
1e2f5b6e 9182}
b34976b6 9183
c5614fa4
AM
9184/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9185 the values of any global symbols in a toc section that has been
9186 edited. Globals in toc sections should be a rarity, so this function
9187 sets a flag if any are found in toc sections other than the one just
de194d85 9188 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
9189
9190struct adjust_toc_info
9191{
9192 asection *toc;
9193 unsigned long *skip;
9194 bfd_boolean global_toc_syms;
9195};
9196
ba761f19
AM
9197enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9198
c5614fa4
AM
9199static bfd_boolean
9200adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9201{
9202 struct ppc_link_hash_entry *eh;
9203 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 9204 unsigned long i;
c5614fa4 9205
c5614fa4
AM
9206 if (h->root.type != bfd_link_hash_defined
9207 && h->root.type != bfd_link_hash_defweak)
9208 return TRUE;
9209
9210 eh = (struct ppc_link_hash_entry *) h;
9211 if (eh->adjust_done)
9212 return TRUE;
9213
9214 if (eh->elf.root.u.def.section == toc_inf->toc)
9215 {
854b41e7
AM
9216 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9217 i = toc_inf->toc->rawsize >> 3;
c5614fa4 9218 else
854b41e7
AM
9219 i = eh->elf.root.u.def.value >> 3;
9220
ba761f19 9221 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 9222 {
4eca0228 9223 _bfd_error_handler
854b41e7
AM
9224 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9225 do
9226 ++i;
ba761f19 9227 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 9228 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 9229 }
854b41e7
AM
9230
9231 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
9232 eh->adjust_done = 1;
9233 }
9234 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9235 toc_inf->global_toc_syms = TRUE;
9236
9237 return TRUE;
9238}
9239
39eeab25
AM
9240/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9241 on a _LO variety toc/got reloc. */
560c8763
AM
9242
9243static bfd_boolean
39eeab25 9244ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 9245{
39eeab25
AM
9246 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9247 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
9248 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9249 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9250 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9251 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9252 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9253 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9254 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9255 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9256 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9257 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9258 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9259 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9260 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
9261 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9262 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9263 /* Exclude lfqu by testing reloc. If relocs are ever
9264 defined for the reduced D field in psq_lu then those
9265 will need testing too. */
9266 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9267 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9268 && (insn & 1) == 0)
9269 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9270 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9271 /* Exclude stfqu. psq_stu as above for psq_lu. */
9272 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9273 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9274 && (insn & 1) == 0));
560c8763
AM
9275}
9276
c5614fa4
AM
9277/* Examine all relocs referencing .toc sections in order to remove
9278 unused .toc entries. */
9279
9280bfd_boolean
33c0ec9d 9281ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
9282{
9283 bfd *ibfd;
9284 struct adjust_toc_info toc_inf;
67f0cbdb 9285 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 9286
67f0cbdb 9287 htab->do_toc_opt = 1;
c5614fa4 9288 toc_inf.global_toc_syms = TRUE;
c72f2fb2 9289 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
9290 {
9291 asection *toc, *sec;
9292 Elf_Internal_Shdr *symtab_hdr;
9293 Elf_Internal_Sym *local_syms;
425b145b 9294 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
9295 unsigned long *skip, *drop;
9296 unsigned char *used;
9297 unsigned char *keep, last, some_unused;
9298
854b41e7
AM
9299 if (!is_ppc64_elf (ibfd))
9300 continue;
9301
c5614fa4
AM
9302 toc = bfd_get_section_by_name (ibfd, ".toc");
9303 if (toc == NULL
92b7a70f 9304 || toc->size == 0
dbaa2011
AM
9305 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9306 || discarded_section (toc))
c5614fa4
AM
9307 continue;
9308
425b145b 9309 toc_relocs = NULL;
c5614fa4 9310 local_syms = NULL;
0ffa91dd 9311 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
9312
9313 /* Look at sections dropped from the final link. */
9314 skip = NULL;
9315 relstart = NULL;
9316 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9317 {
9318 if (sec->reloc_count == 0
dbaa2011 9319 || !discarded_section (sec)
c5614fa4
AM
9320 || get_opd_info (sec)
9321 || (sec->flags & SEC_ALLOC) == 0
9322 || (sec->flags & SEC_DEBUGGING) != 0)
9323 continue;
9324
9325 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9326 if (relstart == NULL)
9327 goto error_ret;
9328
9329 /* Run through the relocs to see which toc entries might be
9330 unused. */
9331 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9332 {
9333 enum elf_ppc64_reloc_type r_type;
9334 unsigned long r_symndx;
9335 asection *sym_sec;
9336 struct elf_link_hash_entry *h;
9337 Elf_Internal_Sym *sym;
9338 bfd_vma val;
9339
9340 r_type = ELF64_R_TYPE (rel->r_info);
9341 switch (r_type)
9342 {
9343 default:
9344 continue;
9345
9346 case R_PPC64_TOC16:
9347 case R_PPC64_TOC16_LO:
9348 case R_PPC64_TOC16_HI:
9349 case R_PPC64_TOC16_HA:
9350 case R_PPC64_TOC16_DS:
9351 case R_PPC64_TOC16_LO_DS:
9352 break;
9353 }
9354
9355 r_symndx = ELF64_R_SYM (rel->r_info);
9356 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9357 r_symndx, ibfd))
9358 goto error_ret;
9359
9360 if (sym_sec != toc)
9361 continue;
9362
9363 if (h != NULL)
9364 val = h->root.u.def.value;
9365 else
9366 val = sym->st_value;
9367 val += rel->r_addend;
9368
9369 if (val >= toc->size)
9370 continue;
9371
9372 /* Anything in the toc ought to be aligned to 8 bytes.
9373 If not, don't mark as unused. */
9374 if (val & 7)
9375 continue;
9376
9377 if (skip == NULL)
9378 {
854b41e7 9379 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9380 if (skip == NULL)
9381 goto error_ret;
9382 }
9383
ba761f19 9384 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9385 }
9386
9387 if (elf_section_data (sec)->relocs != relstart)
9388 free (relstart);
9389 }
9390
ba761f19
AM
9391 /* For largetoc loads of address constants, we can convert
9392 . addis rx,2,addr@got@ha
9393 . ld ry,addr@got@l(rx)
9394 to
9395 . addis rx,2,addr@toc@ha
9396 . addi ry,rx,addr@toc@l
9397 when addr is within 2G of the toc pointer. This then means
9398 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9399
ba761f19
AM
9400 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9401 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9402 && toc->reloc_count != 0)
9403 {
9404 /* Read toc relocs. */
425b145b
AM
9405 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9406 info->keep_memory);
9407 if (toc_relocs == NULL)
ba761f19
AM
9408 goto error_ret;
9409
425b145b 9410 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9411 {
9412 enum elf_ppc64_reloc_type r_type;
9413 unsigned long r_symndx;
9414 asection *sym_sec;
9415 struct elf_link_hash_entry *h;
9416 Elf_Internal_Sym *sym;
9417 bfd_vma val, addr;
9418
9419 r_type = ELF64_R_TYPE (rel->r_info);
9420 if (r_type != R_PPC64_ADDR64)
9421 continue;
9422
9423 r_symndx = ELF64_R_SYM (rel->r_info);
9424 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9425 r_symndx, ibfd))
9426 goto error_ret;
9427
425b145b 9428 if (sym_sec == NULL
c27b8c2a 9429 || sym_sec->output_section == NULL
dbaa2011 9430 || discarded_section (sym_sec))
425b145b
AM
9431 continue;
9432
afe397ea 9433 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9434 continue;
9435
9436 if (h != NULL)
bddc25c9
AM
9437 {
9438 if (h->type == STT_GNU_IFUNC)
9439 continue;
9440 val = h->root.u.def.value;
9441 }
ba761f19 9442 else
bddc25c9
AM
9443 {
9444 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9445 continue;
9446 val = sym->st_value;
9447 }
ba761f19
AM
9448 val += rel->r_addend;
9449 val += sym_sec->output_section->vma + sym_sec->output_offset;
9450
9451 /* We don't yet know the exact toc pointer value, but we
9452 know it will be somewhere in the toc section. Don't
9453 optimize if the difference from any possible toc
9454 pointer is outside [ff..f80008000, 7fff7fff]. */
9455 addr = toc->output_section->vma + TOC_BASE_OFF;
9456 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9457 continue;
9458
9459 addr = toc->output_section->vma + toc->output_section->rawsize;
9460 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9461 continue;
9462
9463 if (skip == NULL)
9464 {
9465 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9466 if (skip == NULL)
9467 goto error_ret;
9468 }
9469
9470 skip[rel->r_offset >> 3]
425b145b 9471 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9472 }
ba761f19
AM
9473 }
9474
c5614fa4
AM
9475 if (skip == NULL)
9476 continue;
9477
9478 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9479 if (used == NULL)
9480 {
9481 error_ret:
9482 if (local_syms != NULL
9483 && symtab_hdr->contents != (unsigned char *) local_syms)
9484 free (local_syms);
9485 if (sec != NULL
9486 && relstart != NULL
9487 && elf_section_data (sec)->relocs != relstart)
9488 free (relstart);
425b145b
AM
9489 if (toc_relocs != NULL
9490 && elf_section_data (toc)->relocs != toc_relocs)
9491 free (toc_relocs);
c5614fa4
AM
9492 if (skip != NULL)
9493 free (skip);
9494 return FALSE;
9495 }
9496
30038c59
AM
9497 /* Now check all kept sections that might reference the toc.
9498 Check the toc itself last. */
9499 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9500 : ibfd->sections);
c5614fa4 9501 sec != NULL;
c5614fa4 9502 sec = (sec == toc ? NULL
c5614fa4 9503 : sec->next == NULL ? toc
30038c59 9504 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9505 : sec->next))
9506 {
9507 int repeat;
9508
9509 if (sec->reloc_count == 0
dbaa2011 9510 || discarded_section (sec)
c5614fa4
AM
9511 || get_opd_info (sec)
9512 || (sec->flags & SEC_ALLOC) == 0
9513 || (sec->flags & SEC_DEBUGGING) != 0)
9514 continue;
9515
854b41e7
AM
9516 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9517 info->keep_memory);
c5614fa4 9518 if (relstart == NULL)
2915c55b
JK
9519 {
9520 free (used);
9521 goto error_ret;
9522 }
c5614fa4
AM
9523
9524 /* Mark toc entries referenced as used. */
c5614fa4 9525 do
d4f1ee75
AM
9526 {
9527 repeat = 0;
9528 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9529 {
9530 enum elf_ppc64_reloc_type r_type;
9531 unsigned long r_symndx;
9532 asection *sym_sec;
9533 struct elf_link_hash_entry *h;
9534 Elf_Internal_Sym *sym;
9535 bfd_vma val;
9536 enum {no_check, check_lo, check_ha} insn_check;
98528052 9537
d4f1ee75
AM
9538 r_type = ELF64_R_TYPE (rel->r_info);
9539 switch (r_type)
9540 {
9541 default:
9542 insn_check = no_check;
9543 break;
98528052 9544
d4f1ee75
AM
9545 case R_PPC64_GOT_TLSLD16_HA:
9546 case R_PPC64_GOT_TLSGD16_HA:
9547 case R_PPC64_GOT_TPREL16_HA:
9548 case R_PPC64_GOT_DTPREL16_HA:
9549 case R_PPC64_GOT16_HA:
9550 case R_PPC64_TOC16_HA:
9551 insn_check = check_ha;
9552 break;
98528052 9553
d4f1ee75
AM
9554 case R_PPC64_GOT_TLSLD16_LO:
9555 case R_PPC64_GOT_TLSGD16_LO:
9556 case R_PPC64_GOT_TPREL16_LO_DS:
9557 case R_PPC64_GOT_DTPREL16_LO_DS:
9558 case R_PPC64_GOT16_LO:
9559 case R_PPC64_GOT16_LO_DS:
9560 case R_PPC64_TOC16_LO:
9561 case R_PPC64_TOC16_LO_DS:
9562 insn_check = check_lo;
9563 break;
9564 }
560c8763 9565
d4f1ee75
AM
9566 if (insn_check != no_check)
9567 {
9568 bfd_vma off = rel->r_offset & ~3;
9569 unsigned char buf[4];
9570 unsigned int insn;
c5614fa4 9571
d4f1ee75
AM
9572 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9573 {
9574 free (used);
9575 goto error_ret;
9576 }
9577 insn = bfd_get_32 (ibfd, buf);
9578 if (insn_check == check_lo
39eeab25 9579 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9580 : ((insn & ((0x3f << 26) | 0x1f << 16))
9581 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9582 {
9583 char str[12];
9584
9585 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9586 sprintf (str, "%#08x", insn);
9587 info->callbacks->einfo
695344c0 9588 /* xgettext:c-format */
174d0a74 9589 (_("%H: toc optimization is not supported for"
cf97bcb0 9590 " %s instruction\n"),
d4f1ee75
AM
9591 ibfd, sec, rel->r_offset & ~3, str);
9592 }
9593 }
c5614fa4 9594
d4f1ee75
AM
9595 switch (r_type)
9596 {
9597 case R_PPC64_TOC16:
9598 case R_PPC64_TOC16_LO:
9599 case R_PPC64_TOC16_HI:
9600 case R_PPC64_TOC16_HA:
9601 case R_PPC64_TOC16_DS:
9602 case R_PPC64_TOC16_LO_DS:
9603 /* In case we're taking addresses of toc entries. */
9604 case R_PPC64_ADDR64:
9605 break;
c5614fa4 9606
d4f1ee75
AM
9607 default:
9608 continue;
9609 }
c5614fa4 9610
d4f1ee75
AM
9611 r_symndx = ELF64_R_SYM (rel->r_info);
9612 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9613 r_symndx, ibfd))
9614 {
9615 free (used);
9616 goto error_ret;
9617 }
c5614fa4 9618
d4f1ee75
AM
9619 if (sym_sec != toc)
9620 continue;
c5614fa4 9621
d4f1ee75
AM
9622 if (h != NULL)
9623 val = h->root.u.def.value;
9624 else
9625 val = sym->st_value;
9626 val += rel->r_addend;
ba761f19 9627
d4f1ee75
AM
9628 if (val >= toc->size)
9629 continue;
ba761f19 9630
d4f1ee75
AM
9631 if ((skip[val >> 3] & can_optimize) != 0)
9632 {
9633 bfd_vma off;
9634 unsigned char opc;
9635
9636 switch (r_type)
9637 {
9638 case R_PPC64_TOC16_HA:
ba761f19 9639 break;
ba761f19 9640
d4f1ee75
AM
9641 case R_PPC64_TOC16_LO_DS:
9642 off = rel->r_offset;
9643 off += (bfd_big_endian (ibfd) ? -2 : 3);
9644 if (!bfd_get_section_contents (ibfd, sec, &opc,
9645 off, 1))
9646 {
9647 free (used);
9648 goto error_ret;
9649 }
9650 if ((opc & (0x3f << 2)) == (58u << 2))
9651 break;
1a0670f3 9652 /* Fall through. */
ba761f19 9653
d4f1ee75
AM
9654 default:
9655 /* Wrong sort of reloc, or not a ld. We may
9656 as well clear ref_from_discarded too. */
9657 skip[val >> 3] = 0;
9658 }
9659 }
9660
9661 if (sec != toc)
9662 used[val >> 3] = 1;
9663 /* For the toc section, we only mark as used if this
9664 entry itself isn't unused. */
9665 else if ((used[rel->r_offset >> 3]
9666 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9667 && !used[val >> 3])
9668 {
9669 /* Do all the relocs again, to catch reference
9670 chains. */
9671 repeat = 1;
9672 used[val >> 3] = 1;
9673 }
9674 }
9675 }
c5614fa4 9676 while (repeat);
854b41e7
AM
9677
9678 if (elf_section_data (sec)->relocs != relstart)
9679 free (relstart);
c5614fa4
AM
9680 }
9681
9682 /* Merge the used and skip arrays. Assume that TOC
9683 doublewords not appearing as either used or unused belong
de194d85 9684 to an entry more than one doubleword in size. */
c5614fa4
AM
9685 for (drop = skip, keep = used, last = 0, some_unused = 0;
9686 drop < skip + (toc->size + 7) / 8;
9687 ++drop, ++keep)
9688 {
9689 if (*keep)
9690 {
ba761f19
AM
9691 *drop &= ~ref_from_discarded;
9692 if ((*drop & can_optimize) != 0)
9693 some_unused = 1;
c5614fa4
AM
9694 last = 0;
9695 }
b140b010 9696 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9697 {
9698 some_unused = 1;
ba761f19 9699 last = ref_from_discarded;
c5614fa4
AM
9700 }
9701 else
9702 *drop = last;
9703 }
9704
9705 free (used);
9706
9707 if (some_unused)
9708 {
9709 bfd_byte *contents, *src;
9710 unsigned long off;
d62b3684 9711 Elf_Internal_Sym *sym;
ba761f19 9712 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9713
9714 /* Shuffle the toc contents, and at the same time convert the
9715 skip array from booleans into offsets. */
9716 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9717 goto error_ret;
9718
9719 elf_section_data (toc)->this_hdr.contents = contents;
9720
9721 for (src = contents, off = 0, drop = skip;
9722 src < contents + toc->size;
9723 src += 8, ++drop)
9724 {
ba761f19
AM
9725 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9726 off += 8;
c5614fa4
AM
9727 else if (off != 0)
9728 {
9729 *drop = off;
9730 memcpy (src - off, src, 8);
9731 }
9732 }
854b41e7 9733 *drop = off;
c5614fa4
AM
9734 toc->rawsize = toc->size;
9735 toc->size = src - contents - off;
9736
ba761f19
AM
9737 /* Adjust addends for relocs against the toc section sym,
9738 and optimize any accesses we can. */
c5614fa4
AM
9739 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9740 {
9741 if (sec->reloc_count == 0
dbaa2011 9742 || discarded_section (sec))
c5614fa4
AM
9743 continue;
9744
9745 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9746 info->keep_memory);
c5614fa4
AM
9747 if (relstart == NULL)
9748 goto error_ret;
9749
9750 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9751 {
9752 enum elf_ppc64_reloc_type r_type;
9753 unsigned long r_symndx;
9754 asection *sym_sec;
9755 struct elf_link_hash_entry *h;
854b41e7 9756 bfd_vma val;
c5614fa4
AM
9757
9758 r_type = ELF64_R_TYPE (rel->r_info);
9759 switch (r_type)
9760 {
9761 default:
9762 continue;
9763
9764 case R_PPC64_TOC16:
9765 case R_PPC64_TOC16_LO:
9766 case R_PPC64_TOC16_HI:
9767 case R_PPC64_TOC16_HA:
9768 case R_PPC64_TOC16_DS:
9769 case R_PPC64_TOC16_LO_DS:
9770 case R_PPC64_ADDR64:
9771 break;
9772 }
9773
9774 r_symndx = ELF64_R_SYM (rel->r_info);
9775 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9776 r_symndx, ibfd))
9777 goto error_ret;
9778
ba761f19 9779 if (sym_sec != toc)
c5614fa4
AM
9780 continue;
9781
ba761f19
AM
9782 if (h != NULL)
9783 val = h->root.u.def.value;
9784 else
9785 {
9786 val = sym->st_value;
9787 if (val != 0)
9788 local_toc_syms = TRUE;
9789 }
9790
9791 val += rel->r_addend;
854b41e7
AM
9792
9793 if (val > toc->rawsize)
9794 val = toc->rawsize;
ba761f19
AM
9795 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9796 continue;
9797 else if ((skip[val >> 3] & can_optimize) != 0)
9798 {
9799 Elf_Internal_Rela *tocrel
425b145b 9800 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9801 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9802
9803 switch (r_type)
9804 {
9805 case R_PPC64_TOC16_HA:
9806 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9807 break;
9808
9809 case R_PPC64_TOC16_LO_DS:
9810 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9811 break;
9812
9813 default:
28942f62
AM
9814 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9815 ppc_howto_init ();
b140b010 9816 info->callbacks->einfo
695344c0 9817 /* xgettext:c-format */
174d0a74 9818 (_("%H: %s references "
b140b010
AM
9819 "optimized away TOC entry\n"),
9820 ibfd, sec, rel->r_offset,
9821 ppc64_elf_howto_table[r_type]->name);
9822 bfd_set_error (bfd_error_bad_value);
9823 goto error_ret;
ba761f19
AM
9824 }
9825 rel->r_addend = tocrel->r_addend;
9826 elf_section_data (sec)->relocs = relstart;
9827 continue;
9828 }
9829
9830 if (h != NULL || sym->st_value != 0)
9831 continue;
854b41e7
AM
9832
9833 rel->r_addend -= skip[val >> 3];
9834 elf_section_data (sec)->relocs = relstart;
c5614fa4 9835 }
854b41e7
AM
9836
9837 if (elf_section_data (sec)->relocs != relstart)
9838 free (relstart);
c5614fa4
AM
9839 }
9840
9841 /* We shouldn't have local or global symbols defined in the TOC,
9842 but handle them anyway. */
df22d223
AM
9843 if (local_syms != NULL)
9844 for (sym = local_syms;
9845 sym < local_syms + symtab_hdr->sh_info;
9846 ++sym)
9847 if (sym->st_value != 0
9848 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9849 {
9850 unsigned long i;
854b41e7 9851
df22d223
AM
9852 if (sym->st_value > toc->rawsize)
9853 i = toc->rawsize >> 3;
9854 else
9855 i = sym->st_value >> 3;
854b41e7 9856
df22d223
AM
9857 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9858 {
9859 if (local_toc_syms)
4eca0228 9860 _bfd_error_handler
df22d223
AM
9861 (_("%s defined on removed toc entry"),
9862 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9863 do
9864 ++i;
9865 while ((skip[i] & (ref_from_discarded | can_optimize)));
9866 sym->st_value = (bfd_vma) i << 3;
9867 }
d62b3684 9868
df22d223
AM
9869 sym->st_value -= skip[i];
9870 symtab_hdr->contents = (unsigned char *) local_syms;
9871 }
c5614fa4 9872
854b41e7 9873 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9874 if (toc_inf.global_toc_syms)
9875 {
9876 toc_inf.toc = toc;
9877 toc_inf.skip = skip;
9878 toc_inf.global_toc_syms = FALSE;
9879 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9880 &toc_inf);
9881 }
854b41e7
AM
9882
9883 if (toc->reloc_count != 0)
9884 {
d4730f92 9885 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9886 Elf_Internal_Rela *wrel;
9887 bfd_size_type sz;
9888
854b41e7 9889 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9890 if (toc_relocs == NULL)
9891 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9892 info->keep_memory);
9893 if (toc_relocs == NULL)
9894 goto error_ret;
9895
425b145b
AM
9896 wrel = toc_relocs;
9897 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9898 if ((skip[rel->r_offset >> 3]
9899 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9900 {
9901 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9902 wrel->r_info = rel->r_info;
9903 wrel->r_addend = rel->r_addend;
9904 ++wrel;
9905 }
9906 else if (!dec_dynrel_count (rel->r_info, toc, info,
9907 &local_syms, NULL, NULL))
9908 goto error_ret;
9909
425b145b
AM
9910 elf_section_data (toc)->relocs = toc_relocs;
9911 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9912 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9913 sz = rel_hdr->sh_entsize;
9914 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9915 }
c5614fa4 9916 }
28be611c
AM
9917 else if (toc_relocs != NULL
9918 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9919 free (toc_relocs);
c5614fa4
AM
9920
9921 if (local_syms != NULL
9922 && symtab_hdr->contents != (unsigned char *) local_syms)
9923 {
9924 if (!info->keep_memory)
9925 free (local_syms);
9926 else
9927 symtab_hdr->contents = (unsigned char *) local_syms;
9928 }
9929 free (skip);
9930 }
9931
9932 return TRUE;
9933}
9934
1bbe0902
AM
9935/* Return true iff input section I references the TOC using
9936 instructions limited to +/-32k offsets. */
9937
9938bfd_boolean
9939ppc64_elf_has_small_toc_reloc (asection *i)
9940{
9941 return (is_ppc64_elf (i->owner)
9942 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9943}
9944
927be08e
AM
9945/* Allocate space for one GOT entry. */
9946
9947static void
9948allocate_got (struct elf_link_hash_entry *h,
9949 struct bfd_link_info *info,
9950 struct got_entry *gent)
9951{
9952 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9953 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9954 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9955 ? 16 : 8);
9956 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9957 ? 2 : 1) * sizeof (Elf64_External_Rela);
9958 asection *got = ppc64_elf_tdata (gent->owner)->got;
9959
9960 gent->got.offset = got->size;
9961 got->size += entsize;
9962
19e08130 9963 if (h->type == STT_GNU_IFUNC)
927be08e 9964 {
33e44f2e 9965 htab->elf.irelplt->size += rentsize;
19e08130 9966 htab->got_reli_size += rentsize;
927be08e 9967 }
f15d0b54
AM
9968 else if (((bfd_link_pic (info)
9969 && !((gent->tls_type & TLS_TPREL) != 0
9970 && bfd_link_executable (info)
9971 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9972 || (htab->elf.dynamic_sections_created
9973 && h->dynindx != -1
9974 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9975 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9976 {
19e08130 9977 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9978 relgot->size += rentsize;
927be08e
AM
9979 }
9980}
9981
7865406b
AM
9982/* This function merges got entries in the same toc group. */
9983
9984static void
9985merge_got_entries (struct got_entry **pent)
9986{
9987 struct got_entry *ent, *ent2;
9988
9989 for (ent = *pent; ent != NULL; ent = ent->next)
9990 if (!ent->is_indirect)
9991 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9992 if (!ent2->is_indirect
9993 && ent2->addend == ent->addend
9994 && ent2->tls_type == ent->tls_type
9995 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9996 {
9997 ent2->is_indirect = TRUE;
9998 ent2->got.ent = ent;
9999 }
10000}
10001
46434633 10002/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
10003
10004static bfd_boolean
46434633
AM
10005ensure_undef_dynamic (struct bfd_link_info *info,
10006 struct elf_link_hash_entry *h)
f0158f44
AM
10007{
10008 struct elf_link_hash_table *htab = elf_hash_table (info);
10009
10010 if (htab->dynamic_sections_created
46434633
AM
10011 && ((info->dynamic_undefined_weak != 0
10012 && h->root.type == bfd_link_hash_undefweak)
10013 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
10014 && h->dynindx == -1
10015 && !h->forced_local
10016 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
10017 return bfd_elf_link_record_dynamic_symbol (info, h);
10018 return TRUE;
10019}
10020
65f38f15
AM
10021/* Allocate space in .plt, .got and associated reloc sections for
10022 dynamic relocs. */
5bd4f169 10023
b34976b6 10024static bfd_boolean
4ce794b7 10025allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 10026{
65f38f15
AM
10027 struct bfd_link_info *info;
10028 struct ppc_link_hash_table *htab;
5bd4f169 10029 asection *s;
65f38f15 10030 struct ppc_link_hash_entry *eh;
0b8bcf0d 10031 struct got_entry **pgent, *gent;
5bd4f169 10032
e92d460e 10033 if (h->root.type == bfd_link_hash_indirect)
b34976b6 10034 return TRUE;
5bd4f169 10035
65f38f15
AM
10036 info = (struct bfd_link_info *) inf;
10037 htab = ppc_hash_table (info);
4dfe6ac6
NC
10038 if (htab == NULL)
10039 return FALSE;
5bd4f169 10040
951fd09b
AM
10041 eh = (struct ppc_link_hash_entry *) h;
10042 /* Run through the TLS GD got entries first if we're changing them
10043 to TPREL. */
37da22e5 10044 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
10045 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10046 if (gent->got.refcount > 0
10047 && (gent->tls_type & TLS_GD) != 0)
10048 {
10049 /* This was a GD entry that has been converted to TPREL. If
10050 there happens to be a TPREL entry we can use that one. */
10051 struct got_entry *ent;
10052 for (ent = h->got.glist; ent != NULL; ent = ent->next)
10053 if (ent->got.refcount > 0
10054 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
10055 && ent->addend == gent->addend
10056 && ent->owner == gent->owner)
951fd09b
AM
10057 {
10058 gent->got.refcount = 0;
10059 break;
10060 }
10061
10062 /* If not, then we'll be using our own TPREL entry. */
10063 if (gent->got.refcount != 0)
10064 gent->tls_type = TLS_TLS | TLS_TPREL;
10065 }
10066
7865406b
AM
10067 /* Remove any list entry that won't generate a word in the GOT before
10068 we call merge_got_entries. Otherwise we risk merging to empty
10069 entries. */
0b8bcf0d
AM
10070 pgent = &h->got.glist;
10071 while ((gent = *pgent) != NULL)
411e1bfb 10072 if (gent->got.refcount > 0)
7865406b
AM
10073 {
10074 if ((gent->tls_type & TLS_LD) != 0
10075 && !h->def_dynamic)
10076 {
10077 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
10078 *pgent = gent->next;
10079 }
10080 else
10081 pgent = &gent->next;
10082 }
10083 else
10084 *pgent = gent->next;
10085
10086 if (!htab->do_multi_toc)
10087 merge_got_entries (&h->got.glist);
10088
10089 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10090 if (!gent->is_indirect)
411e1bfb 10091 {
46434633
AM
10092 /* Make sure this symbol is output as a dynamic symbol. */
10093 if (!ensure_undef_dynamic (info, h))
f0158f44 10094 return FALSE;
65f38f15 10095
0c8d6e5c 10096 if (!is_ppc64_elf (gent->owner))
927be08e 10097 abort ();
0ffa91dd 10098
927be08e 10099 allocate_got (h, info, gent);
411e1bfb 10100 }
65f38f15 10101
954b63d4
AM
10102 /* If no dynamic sections we can't have dynamic relocs, except for
10103 IFUNCs which are handled even in static executables. */
8a2058b5
AM
10104 if (!htab->elf.dynamic_sections_created
10105 && h->type != STT_GNU_IFUNC)
10106 eh->dyn_relocs = NULL;
10107
529fe20e
AM
10108 /* Discard relocs on undefined symbols that must be local. */
10109 else if (h->root.type == bfd_link_hash_undefined
10110 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10111 eh->dyn_relocs = NULL;
10112
954b63d4
AM
10113 /* Also discard relocs on undefined weak syms with non-default
10114 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 10115 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
10116 eh->dyn_relocs = NULL;
10117
8a2058b5 10118 if (eh->dyn_relocs != NULL)
65f38f15 10119 {
8a2058b5
AM
10120 struct elf_dyn_relocs *p, **pp;
10121
57e7d118
AM
10122 /* In the shared -Bsymbolic case, discard space allocated for
10123 dynamic pc-relative relocs against symbols which turn out to
10124 be defined in regular objects. For the normal shared case,
10125 discard space for relocs that have become local due to symbol
10126 visibility changes. */
10127
10128 if (bfd_link_pic (info))
65f38f15 10129 {
57e7d118
AM
10130 /* Relocs that use pc_count are those that appear on a call
10131 insn, or certain REL relocs (see must_be_dyn_reloc) that
10132 can be generated via assembly. We want calls to
10133 protected symbols to resolve directly to the function
10134 rather than going via the plt. If people want function
10135 pointer comparisons to work as expected then they should
10136 avoid writing weird assembly. */
10137 if (SYMBOL_CALLS_LOCAL (info, h))
10138 {
57e7d118
AM
10139 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10140 {
10141 p->count -= p->pc_count;
10142 p->pc_count = 0;
10143 if (p->count == 0)
10144 *pp = p->next;
10145 else
10146 pp = &p->next;
10147 }
10148 }
65f38f15 10149
954b63d4 10150 if (eh->dyn_relocs != NULL)
5bd4f169 10151 {
46434633
AM
10152 /* Make sure this symbol is output as a dynamic symbol. */
10153 if (!ensure_undef_dynamic (info, h))
f0158f44 10154 return FALSE;
5bd4f169 10155 }
65f38f15 10156 }
529fe20e 10157 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 10158 {
8a2058b5 10159 /* For the non-pic case, discard space for relocs against
57e7d118
AM
10160 symbols which turn out to need copy relocs or are not
10161 dynamic. */
529fe20e
AM
10162 if (h->dynamic_adjusted
10163 && !h->def_regular
10164 && !ELF_COMMON_DEF_P (h))
f0158f44 10165 {
46434633
AM
10166 /* Make sure this symbol is output as a dynamic symbol. */
10167 if (!ensure_undef_dynamic (info, h))
f0158f44 10168 return FALSE;
dfbb6ac9 10169
f0158f44
AM
10170 if (h->dynindx == -1)
10171 eh->dyn_relocs = NULL;
10172 }
10173 else
8a2058b5 10174 eh->dyn_relocs = NULL;
57e7d118
AM
10175 }
10176
10177 /* Finally, allocate space. */
10178 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10179 {
10180 asection *sreloc = elf_section_data (p->sec)->sreloc;
10181 if (eh->elf.type == STT_GNU_IFUNC)
10182 sreloc = htab->elf.irelplt;
10183 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 10184 }
65f38f15 10185 }
57e7d118 10186
2d7ad24e
AM
10187 /* We might need a PLT entry when the symbol
10188 a) is dynamic, or
10189 b) is an ifunc, or
10190 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10191 d) has plt16 relocs and we are linking statically. */
10192 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10193 || h->type == STT_GNU_IFUNC
10194 || (h->needs_plt && h->dynamic_adjusted)
10195 || (h->needs_plt
10196 && h->def_regular
10197 && !htab->elf.dynamic_sections_created
3e04d765 10198 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
10199 && (((struct ppc_link_hash_entry *) h)->tls_mask
10200 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10201 {
57e7d118
AM
10202 struct plt_entry *pent;
10203 bfd_boolean doneone = FALSE;
10204 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10205 if (pent->plt.refcount > 0)
10206 {
10207 if (!htab->elf.dynamic_sections_created
10208 || h->dynindx == -1)
10209 {
2d7ad24e
AM
10210 if (h->type == STT_GNU_IFUNC)
10211 {
10212 s = htab->elf.iplt;
10213 pent->plt.offset = s->size;
10214 s->size += PLT_ENTRY_SIZE (htab);
10215 s = htab->elf.irelplt;
10216 }
10217 else
10218 {
10219 s = htab->pltlocal;
10220 pent->plt.offset = s->size;
10221 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10222 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10223 }
57e7d118
AM
10224 }
10225 else
10226 {
10227 /* If this is the first .plt entry, make room for the special
10228 first entry. */
10229 s = htab->elf.splt;
10230 if (s->size == 0)
10231 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10232
57e7d118 10233 pent->plt.offset = s->size;
65f38f15 10234
57e7d118
AM
10235 /* Make room for this entry. */
10236 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10237
57e7d118
AM
10238 /* Make room for the .glink code. */
10239 s = htab->glink;
10240 if (s->size == 0)
9e390558 10241 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10242 if (htab->opd_abi)
10243 {
10244 /* We need bigger stubs past index 32767. */
9e390558 10245 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10246 s->size += 4;
10247 s->size += 2*4;
10248 }
10249 else
10250 s->size += 4;
65f38f15 10251
57e7d118
AM
10252 /* We also need to make an entry in the .rela.plt section. */
10253 s = htab->elf.srelplt;
10254 }
2d7ad24e
AM
10255 if (s != NULL)
10256 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
10257 doneone = TRUE;
10258 }
10259 else
10260 pent->plt.offset = (bfd_vma) -1;
10261 if (!doneone)
10262 {
10263 h->plt.plist = NULL;
10264 h->needs_plt = 0;
10265 }
65f38f15 10266 }
57e7d118 10267 else
65f38f15 10268 {
57e7d118
AM
10269 h->plt.plist = NULL;
10270 h->needs_plt = 0;
65f38f15
AM
10271 }
10272
b34976b6 10273 return TRUE;
65f38f15
AM
10274}
10275
9e390558
AM
10276#define PPC_LO(v) ((v) & 0xffff)
10277#define PPC_HI(v) (((v) >> 16) & 0xffff)
10278#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10279
a345bc8d
AM
10280/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10281 to set up space for global entry stubs. These are put in glink,
10282 after the branch table. */
65f38f15 10283
b34976b6 10284static bfd_boolean
a345bc8d 10285size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10286{
a345bc8d
AM
10287 struct bfd_link_info *info;
10288 struct ppc_link_hash_table *htab;
10289 struct plt_entry *pent;
9e390558 10290 asection *s, *plt;
65f38f15 10291
a345bc8d
AM
10292 if (h->root.type == bfd_link_hash_indirect)
10293 return TRUE;
65f38f15 10294
a345bc8d
AM
10295 if (!h->pointer_equality_needed)
10296 return TRUE;
65f38f15 10297
a345bc8d
AM
10298 if (h->def_regular)
10299 return TRUE;
65f38f15 10300
a345bc8d
AM
10301 info = inf;
10302 htab = ppc_hash_table (info);
10303 if (htab == NULL)
10304 return FALSE;
10305
9e390558
AM
10306 s = htab->global_entry;
10307 plt = htab->elf.splt;
a345bc8d
AM
10308 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10309 if (pent->plt.offset != (bfd_vma) -1
10310 && pent->addend == 0)
10311 {
afe397ea
AM
10312 /* For ELFv2, if this symbol is not defined in a regular file
10313 and we are not generating a shared library or pie, then we
10314 need to define the symbol in the executable on a call stub.
10315 This is to avoid text relocations. */
9e390558
AM
10316 bfd_vma off, stub_align, stub_off, stub_size;
10317 unsigned int align_power;
10318
10319 stub_size = 16;
10320 stub_off = s->size;
10321 if (htab->params->plt_stub_align >= 0)
10322 align_power = htab->params->plt_stub_align;
10323 else
10324 align_power = -htab->params->plt_stub_align;
10325 /* Setting section alignment is delayed until we know it is
10326 non-empty. Otherwise the .text output section will be
10327 aligned at least to plt_stub_align even when no global
10328 entry stubs are needed. */
10329 if (s->alignment_power < align_power)
10330 s->alignment_power = align_power;
10331 stub_align = (bfd_vma) 1 << align_power;
10332 if (htab->params->plt_stub_align >= 0
10333 || ((((stub_off + stub_size - 1) & -stub_align)
10334 - (stub_off & -stub_align))
10335 > ((stub_size - 1) & -stub_align)))
10336 stub_off = (stub_off + stub_align - 1) & -stub_align;
10337 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10338 off -= stub_off + s->output_offset + s->output_section->vma;
10339 /* Note that for --plt-stub-align negative we have a possible
10340 dependency between stub offset and size. Break that
10341 dependency by assuming the max stub size when calculating
10342 the stub offset. */
10343 if (PPC_HA (off) == 0)
10344 stub_size -= 4;
8a2058b5 10345 h->root.type = bfd_link_hash_defined;
afe397ea 10346 h->root.u.def.section = s;
9e390558
AM
10347 h->root.u.def.value = stub_off;
10348 s->size = stub_off + stub_size;
a345bc8d
AM
10349 break;
10350 }
10351 return TRUE;
10352}
10353
10354/* Set DF_TEXTREL if we find any dynamic relocs that apply to
10355 read-only sections. */
10356
10357static bfd_boolean
98bbb1b8 10358maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 10359{
98bbb1b8
AM
10360 asection *sec;
10361
a345bc8d
AM
10362 if (h->root.type == bfd_link_hash_indirect)
10363 return TRUE;
10364
98bbb1b8
AM
10365 sec = readonly_dynrelocs (h);
10366 if (sec != NULL)
a345bc8d 10367 {
98bbb1b8
AM
10368 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10369
10370 info->flags |= DF_TEXTREL;
10371 info->callbacks->minfo
c1c8c1ef 10372 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 10373 sec->owner, h->root.root.string, sec);
a345bc8d
AM
10374
10375 /* Not an error, just cut short the traversal. */
10376 return FALSE;
65f38f15 10377 }
b34976b6 10378 return TRUE;
65f38f15
AM
10379}
10380
10381/* Set the sizes of the dynamic sections. */
10382
b34976b6 10383static bfd_boolean
ee67d69a 10384ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10385 struct bfd_link_info *info)
65f38f15
AM
10386{
10387 struct ppc_link_hash_table *htab;
10388 bfd *dynobj;
10389 asection *s;
b34976b6 10390 bfd_boolean relocs;
65f38f15 10391 bfd *ibfd;
7865406b 10392 struct got_entry *first_tlsld;
65f38f15
AM
10393
10394 htab = ppc_hash_table (info);
4dfe6ac6
NC
10395 if (htab == NULL)
10396 return FALSE;
10397
65f38f15
AM
10398 dynobj = htab->elf.dynobj;
10399 if (dynobj == NULL)
10400 abort ();
10401
10402 if (htab->elf.dynamic_sections_created)
10403 {
10404 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10405 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10406 {
3d4d4302 10407 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10408 if (s == NULL)
10409 abort ();
eea6121a 10410 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10411 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10412 }
10413 }
10414
10415 /* Set up .got offsets for local syms, and space for local dynamic
10416 relocs. */
c72f2fb2 10417 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10418 {
411e1bfb
AM
10419 struct got_entry **lgot_ents;
10420 struct got_entry **end_lgot_ents;
e054468f
AM
10421 struct plt_entry **local_plt;
10422 struct plt_entry **end_local_plt;
f961d9dd 10423 unsigned char *lgot_masks;
65f38f15
AM
10424 bfd_size_type locsymcount;
10425 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10426
0c8d6e5c 10427 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10428 continue;
10429
10430 for (s = ibfd->sections; s != NULL; s = s->next)
10431 {
19e08130 10432 struct ppc_dyn_relocs *p;
65f38f15 10433
6edfbbad 10434 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10435 {
ec338859
AM
10436 if (!bfd_is_abs_section (p->sec)
10437 && bfd_is_abs_section (p->sec->output_section))
10438 {
10439 /* Input section has been discarded, either because
10440 it is a copy of a linkonce section or due to
10441 linker script /DISCARD/, so we'll be discarding
10442 the relocs too. */
10443 }
248866a8 10444 else if (p->count != 0)
ec338859 10445 {
19e08130
AM
10446 asection *srel = elf_section_data (p->sec)->sreloc;
10447 if (p->ifunc)
33e44f2e 10448 srel = htab->elf.irelplt;
eea6121a 10449 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10450 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10451 info->flags |= DF_TEXTREL;
ec338859 10452 }
65f38f15
AM
10453 }
10454 }
10455
411e1bfb
AM
10456 lgot_ents = elf_local_got_ents (ibfd);
10457 if (!lgot_ents)
65f38f15
AM
10458 continue;
10459
0ffa91dd 10460 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10461 locsymcount = symtab_hdr->sh_info;
411e1bfb 10462 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10463 local_plt = (struct plt_entry **) end_lgot_ents;
10464 end_local_plt = local_plt + locsymcount;
f961d9dd 10465 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10466 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10467 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10468 {
0b8bcf0d 10469 struct got_entry **pent, *ent;
411e1bfb 10470
0b8bcf0d
AM
10471 pent = lgot_ents;
10472 while ((ent = *pent) != NULL)
411e1bfb
AM
10473 if (ent->got.refcount > 0)
10474 {
e7b938ca 10475 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10476 {
927be08e 10477 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10478 *pent = ent->next;
411e1bfb
AM
10479 }
10480 else
10481 {
19e08130
AM
10482 unsigned int ent_size = 8;
10483 unsigned int rel_size = sizeof (Elf64_External_Rela);
10484
eea6121a 10485 ent->got.offset = s->size;
e7b938ca 10486 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10487 {
19e08130
AM
10488 ent_size *= 2;
10489 rel_size *= 2;
10490 }
10491 s->size += ent_size;
37da22e5 10492 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10493 {
33e44f2e 10494 htab->elf.irelplt->size += rel_size;
19e08130
AM
10495 htab->got_reli_size += rel_size;
10496 }
f15d0b54
AM
10497 else if (bfd_link_pic (info)
10498 && !((ent->tls_type & TLS_TPREL) != 0
10499 && bfd_link_executable (info)))
19e08130
AM
10500 {
10501 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10502 srel->size += rel_size;
927be08e 10503 }
0b8bcf0d 10504 pent = &ent->next;
411e1bfb
AM
10505 }
10506 }
10507 else
0b8bcf0d 10508 *pent = ent->next;
65f38f15 10509 }
e054468f 10510
2d7ad24e
AM
10511 /* Allocate space for plt calls to local syms. */
10512 lgot_masks = (unsigned char *) end_local_plt;
10513 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10514 {
10515 struct plt_entry *ent;
10516
10517 for (ent = *local_plt; ent != NULL; ent = ent->next)
10518 if (ent->plt.refcount > 0)
10519 {
2d7ad24e
AM
10520 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10521 {
10522 s = htab->elf.iplt;
10523 ent->plt.offset = s->size;
10524 s->size += PLT_ENTRY_SIZE (htab);
10525 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10526 }
3e04d765
AM
10527 else if (htab->can_convert_all_inline_plt
10528 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10529 ent->plt.offset = (bfd_vma) -1;
10530 else
10531 {
10532 s = htab->pltlocal;
10533 ent->plt.offset = s->size;
10534 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10535 if (bfd_link_pic (info))
10536 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10537 }
e054468f
AM
10538 }
10539 else
10540 ent->plt.offset = (bfd_vma) -1;
10541 }
65f38f15
AM
10542 }
10543
10544 /* Allocate global sym .plt and .got entries, and space for global
10545 sym dynamic relocs. */
4ce794b7 10546 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10547
0e1862bb 10548 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10549 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10550
7865406b 10551 first_tlsld = NULL;
c72f2fb2 10552 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10553 {
7865406b
AM
10554 struct got_entry *ent;
10555
0c8d6e5c 10556 if (!is_ppc64_elf (ibfd))
102890f0
AM
10557 continue;
10558
7865406b
AM
10559 ent = ppc64_tlsld_got (ibfd);
10560 if (ent->got.refcount > 0)
102890f0 10561 {
7865406b 10562 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10563 {
7865406b
AM
10564 ent->is_indirect = TRUE;
10565 ent->got.ent = first_tlsld;
10566 }
10567 else
10568 {
10569 if (first_tlsld == NULL)
10570 first_tlsld = ent;
10571 s = ppc64_elf_tdata (ibfd)->got;
10572 ent->got.offset = s->size;
10573 ent->owner = ibfd;
10574 s->size += 16;
0e1862bb 10575 if (bfd_link_pic (info))
7865406b
AM
10576 {
10577 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10578 srel->size += sizeof (Elf64_External_Rela);
10579 }
102890f0
AM
10580 }
10581 }
10582 else
7865406b 10583 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10584 }
10585
65f38f15
AM
10586 /* We now have determined the sizes of the various dynamic sections.
10587 Allocate memory for them. */
b34976b6 10588 relocs = FALSE;
65f38f15
AM
10589 for (s = dynobj->sections; s != NULL; s = s->next)
10590 {
10591 if ((s->flags & SEC_LINKER_CREATED) == 0)
10592 continue;
10593
4ce794b7 10594 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10595 /* These haven't been allocated yet; don't strip. */
10596 continue;
33e44f2e
AM
10597 else if (s == htab->elf.sgot
10598 || s == htab->elf.splt
10599 || s == htab->elf.iplt
2d7ad24e 10600 || s == htab->pltlocal
c456f082 10601 || s == htab->glink
9e390558 10602 || s == htab->global_entry
5474d94f
AM
10603 || s == htab->elf.sdynbss
10604 || s == htab->elf.sdynrelro)
65f38f15
AM
10605 {
10606 /* Strip this section if we don't need it; see the
10607 comment below. */
5bd4f169 10608 }
58d180e8
AM
10609 else if (s == htab->glink_eh_frame)
10610 {
10611 if (!bfd_is_abs_section (s->output_section))
10612 /* Not sized yet. */
10613 continue;
10614 }
70cc837d 10615 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10616 {
c456f082 10617 if (s->size != 0)
5bd4f169 10618 {
33e44f2e 10619 if (s != htab->elf.srelplt)
b34976b6 10620 relocs = TRUE;
5bd4f169
AM
10621
10622 /* We use the reloc_count field as a counter if we need
10623 to copy relocs into the output file. */
10624 s->reloc_count = 0;
10625 }
10626 }
65f38f15 10627 else
5bd4f169
AM
10628 {
10629 /* It's not one of our sections, so don't allocate space. */
10630 continue;
10631 }
10632
eea6121a 10633 if (s->size == 0)
5bd4f169 10634 {
c456f082
AM
10635 /* If we don't need this section, strip it from the
10636 output file. This is mostly to handle .rela.bss and
10637 .rela.plt. We must create both sections in
10638 create_dynamic_sections, because they must be created
10639 before the linker maps input sections to output
10640 sections. The linker does that before
10641 adjust_dynamic_symbol is called, and it is that
10642 function which decides whether anything needs to go
10643 into these sections. */
8423293d 10644 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10645 continue;
10646 }
10647
06bcf541
AM
10648 if (bfd_is_abs_section (s->output_section))
10649 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10650 s->name);
10651
c456f082 10652 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10653 continue;
10654
65f38f15
AM
10655 /* Allocate memory for the section contents. We use bfd_zalloc
10656 here in case unused entries are not reclaimed before the
10657 section's contents are written out. This should not happen,
411e1bfb
AM
10658 but this way if it does we get a R_PPC64_NONE reloc in .rela
10659 sections instead of garbage.
10660 We also rely on the section contents being zero when writing
5474d94f 10661 the GOT and .dynrelro. */
eea6121a 10662 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10663 if (s->contents == NULL)
b34976b6 10664 return FALSE;
5bd4f169
AM
10665 }
10666
c72f2fb2 10667 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10668 {
0c8d6e5c 10669 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10670 continue;
10671
e717da7e 10672 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10673 if (s != NULL && s != htab->elf.sgot)
e717da7e 10674 {
eea6121a 10675 if (s->size == 0)
8423293d 10676 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10677 else
10678 {
eea6121a 10679 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10680 if (s->contents == NULL)
10681 return FALSE;
10682 }
10683 }
10684 s = ppc64_elf_tdata (ibfd)->relgot;
10685 if (s != NULL)
10686 {
eea6121a 10687 if (s->size == 0)
8423293d 10688 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10689 else
10690 {
eea6121a 10691 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10692 if (s->contents == NULL)
10693 return FALSE;
10694 relocs = TRUE;
10695 s->reloc_count = 0;
10696 }
10697 }
10698 }
10699
e86ce104 10700 if (htab->elf.dynamic_sections_created)
5bd4f169 10701 {
e8910a83
AM
10702 bfd_boolean tls_opt;
10703
5bd4f169
AM
10704 /* Add some entries to the .dynamic section. We fill in the
10705 values later, in ppc64_elf_finish_dynamic_sections, but we
10706 must add the entries now so that we get the correct size for
10707 the .dynamic section. The DT_DEBUG entry is filled in by the
10708 dynamic linker and used by the debugger. */
dc810e39 10709#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10710 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10711
0e1862bb 10712 if (bfd_link_executable (info))
5bd4f169 10713 {
dc810e39 10714 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10715 return FALSE;
5bd4f169
AM
10716 }
10717
33e44f2e 10718 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10719 {
dc810e39
AM
10720 if (!add_dynamic_entry (DT_PLTGOT, 0)
10721 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10722 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10723 || !add_dynamic_entry (DT_JMPREL, 0)
10724 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10725 return FALSE;
5bd4f169
AM
10726 }
10727
ee67d69a 10728 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10729 {
10730 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10731 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10732 return FALSE;
19397422
AM
10733 }
10734
7c9cf415 10735 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10736 && htab->tls_get_addr_fd != NULL
10737 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10738 if (tls_opt || !htab->opd_abi)
10739 {
10740 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10741 return FALSE;
10742 }
a7f2871e 10743
5bd4f169
AM
10744 if (relocs)
10745 {
dc810e39
AM
10746 if (!add_dynamic_entry (DT_RELA, 0)
10747 || !add_dynamic_entry (DT_RELASZ, 0)
10748 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10749 return FALSE;
5bd4f169 10750
65f38f15
AM
10751 /* If any dynamic relocs apply to a read-only section,
10752 then we need a DT_TEXTREL entry. */
248866a8 10753 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10754 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10755
65f38f15 10756 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10757 {
65f38f15 10758 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10759 return FALSE;
5bd4f169 10760 }
5bd4f169 10761 }
5bd4f169 10762 }
65f38f15 10763#undef add_dynamic_entry
5bd4f169 10764
b34976b6 10765 return TRUE;
5bd4f169
AM
10766}
10767
a345bc8d
AM
10768/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10769
10770static bfd_boolean
10771ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10772{
10773 if (h->plt.plist != NULL
10774 && !h->def_regular
10775 && !h->pointer_equality_needed)
10776 return FALSE;
10777
10778 return _bfd_elf_hash_symbol (h);
10779}
10780
721956f4 10781/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10782
4ce794b7
AM
10783static inline enum ppc_stub_type
10784ppc_type_of_stub (asection *input_sec,
10785 const Elf_Internal_Rela *rel,
10786 struct ppc_link_hash_entry **hash,
e054468f 10787 struct plt_entry **plt_ent,
6911b7dc
AM
10788 bfd_vma destination,
10789 unsigned long local_off)
5bd4f169 10790{
721956f4
AM
10791 struct ppc_link_hash_entry *h = *hash;
10792 bfd_vma location;
10793 bfd_vma branch_offset;
10794 bfd_vma max_branch_offset;
4ce794b7 10795 enum elf_ppc64_reloc_type r_type;
5bd4f169 10796
721956f4
AM
10797 if (h != NULL)
10798 {
e054468f 10799 struct plt_entry *ent;
7fe2b9a6 10800 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10801 if (h->oh != NULL
10802 && h->oh->is_func_descriptor)
7b8f6675
AM
10803 {
10804 fdh = ppc_follow_link (h->oh);
10805 *hash = fdh;
10806 }
8387904d 10807
e054468f
AM
10808 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10809 if (ent->addend == rel->r_addend
10810 && ent->plt.offset != (bfd_vma) -1)
10811 {
e054468f
AM
10812 *plt_ent = ent;
10813 return ppc_stub_plt_call;
10814 }
5bd4f169 10815
7fe2b9a6
AM
10816 /* Here, we know we don't have a plt entry. If we don't have a
10817 either a defined function descriptor or a defined entry symbol
10818 in a regular object file, then it is pointless trying to make
10819 any other type of stub. */
854b41e7
AM
10820 if (!is_static_defined (&fdh->elf)
10821 && !is_static_defined (&h->elf))
721956f4 10822 return ppc_stub_none;
5d1634d7 10823 }
e054468f
AM
10824 else if (elf_local_got_ents (input_sec->owner) != NULL)
10825 {
10826 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10827 struct plt_entry **local_plt = (struct plt_entry **)
10828 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10829 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10830
10831 if (local_plt[r_symndx] != NULL)
10832 {
10833 struct plt_entry *ent;
10834
10835 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10836 if (ent->addend == rel->r_addend
10837 && ent->plt.offset != (bfd_vma) -1)
10838 {
10839 *plt_ent = ent;
10840 return ppc_stub_plt_call;
10841 }
10842 }
10843 }
5d1634d7 10844
721956f4
AM
10845 /* Determine where the call point is. */
10846 location = (input_sec->output_offset
10847 + input_sec->output_section->vma
10848 + rel->r_offset);
5d1634d7 10849
721956f4
AM
10850 branch_offset = destination - location;
10851 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10852
721956f4
AM
10853 /* Determine if a long branch stub is needed. */
10854 max_branch_offset = 1 << 25;
23cedd1d
AM
10855 if (r_type == R_PPC64_REL14
10856 || r_type == R_PPC64_REL14_BRTAKEN
10857 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10858 max_branch_offset = 1 << 15;
5d1634d7 10859
6911b7dc 10860 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10861 /* We need a stub. Figure out whether a long_branch or plt_branch
10862 is needed later. */
10863 return ppc_stub_long_branch;
5d1634d7 10864
721956f4 10865 return ppc_stub_none;
5d1634d7
AM
10866}
10867
05d0e962
AM
10868/* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
10869 loads r12 from r11+r12 (LOAD true).
10870 . lis %r12,xxx-1b@highest
10871 . ori %r12,xxx-1b@higher
10872 . sldi %r12,%r12,32
10873 . oris %r12,%r12,xxx-1b@hi
10874 . ori %r12,%r12,xxx-1b@l
10875 . add %r12,%r11,%r12 */
10876
10877static bfd_byte *
10878build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10879{
10880 if (off + 0x8000 < 0x10000)
10881 {
10882 if (load)
10883 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10884 else
10885 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10886 p += 4;
10887 }
10888 else if (off + 0x80008000ULL < 0x100000000ULL)
10889 {
10890 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10891 p += 4;
10892 if (load)
10893 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10894 else
10895 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10896 p += 4;
10897 }
10898 else
10899 {
10900 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10901 {
10902 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10903 p += 4;
10904 }
10905 else
10906 {
10907 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10908 p += 4;
10909 if (((off >> 32) & 0xffff) != 0)
10910 {
10911 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10912 p += 4;
10913 }
10914 }
10915 if (((off >> 32) & 0xffffffffULL) != 0)
10916 {
10917 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10918 p += 4;
10919 }
10920 if (PPC_HI (off) != 0)
10921 {
10922 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10923 p += 4;
10924 }
10925 if (PPC_LO (off) != 0)
10926 {
10927 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10928 p += 4;
10929 }
10930 if (load)
10931 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10932 else
10933 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10934 p += 4;
10935 }
10936 return p;
10937}
10938
10939static unsigned int
10940size_offset (bfd_vma off)
10941{
10942 unsigned int size;
10943 if (off + 0x8000 < 0x10000)
10944 size = 4;
10945 else if (off + 0x80008000ULL < 0x100000000ULL)
10946 size = 8;
10947 else
10948 {
10949 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10950 size = 4;
10951 else
10952 {
10953 size = 4;
10954 if (((off >> 32) & 0xffff) != 0)
10955 size += 4;
10956 }
10957 if (((off >> 32) & 0xffffffffULL) != 0)
10958 size += 4;
10959 if (PPC_HI (off) != 0)
10960 size += 4;
10961 if (PPC_LO (off) != 0)
10962 size += 4;
10963 size += 4;
10964 }
10965 return size;
10966}
10967
794e51c0
AM
10968/* With power7 weakly ordered memory model, it is possible for ld.so
10969 to update a plt entry in one thread and have another thread see a
10970 stale zero toc entry. To avoid this we need some sort of acquire
10971 barrier in the call stub. One solution is to make the load of the
10972 toc word seem to appear to depend on the load of the function entry
10973 word. Another solution is to test for r2 being zero, and branch to
10974 the appropriate glink entry if so.
10975
10976 . fake dep barrier compare
71a39c98
AM
10977 . ld 12,xxx(2) ld 12,xxx(2)
10978 . mtctr 12 mtctr 12
10979 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10980 . add 2,2,11 cmpldi 2,0
10981 . ld 2,xxx+8(2) bnectr+
10982 . bctr b <glink_entry>
10983
10984 The solution involving the compare turns out to be faster, so
10985 that's what we use unless the branch won't reach. */
10986
10987#define ALWAYS_USE_FAKE_DEP 0
10988#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10989
794e51c0
AM
10990static inline unsigned int
10991plt_stub_size (struct ppc_link_hash_table *htab,
10992 struct ppc_stub_hash_entry *stub_entry,
10993 bfd_vma off)
10994{
05d0e962 10995 unsigned size;
b9e5796b 10996
05d0e962
AM
10997 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10998 {
10999 size = 24 + size_offset (off);
11000 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
11001 size += 4;
11002 return size;
11003 }
11004
11005 size = 12;
b9e5796b
AM
11006 if (ALWAYS_EMIT_R2SAVE
11007 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11008 size += 4;
11009 if (PPC_HA (off) != 0)
794e51c0 11010 size += 4;
b9e5796b
AM
11011 if (htab->opd_abi)
11012 {
11013 size += 4;
e7d1c40c 11014 if (htab->params->plt_static_chain)
b9e5796b 11015 size += 4;
bd4d2eaa
AM
11016 if (htab->params->plt_thread_safe
11017 && htab->elf.dynamic_sections_created
11018 && stub_entry->h != NULL
11019 && stub_entry->h->elf.dynindx != -1)
b9e5796b 11020 size += 8;
e7d1c40c 11021 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
11022 size += 4;
11023 }
794e51c0
AM
11024 if (stub_entry->h != NULL
11025 && (stub_entry->h == htab->tls_get_addr_fd
11026 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11027 && htab->params->tls_get_addr_opt)
f378ab09
AM
11028 {
11029 size += 7 * 4;
11030 if (ALWAYS_EMIT_R2SAVE
11031 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 11032 size += 6 * 4;
f378ab09 11033 }
794e51c0
AM
11034 return size;
11035}
11036
2420fff6
AM
11037/* Depending on the sign of plt_stub_align:
11038 If positive, return the padding to align to a 2**plt_stub_align
11039 boundary.
11040 If negative, if this stub would cross fewer 2**plt_stub_align
11041 boundaries if we align, then return the padding needed to do so. */
11042
794e51c0
AM
11043static inline unsigned int
11044plt_stub_pad (struct ppc_link_hash_table *htab,
11045 struct ppc_stub_hash_entry *stub_entry,
11046 bfd_vma plt_off)
11047{
2420fff6 11048 int stub_align;
1aa42141 11049 unsigned stub_size;
6f20ed8a 11050 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 11051
2420fff6
AM
11052 if (htab->params->plt_stub_align >= 0)
11053 {
11054 stub_align = 1 << htab->params->plt_stub_align;
11055 if ((stub_off & (stub_align - 1)) != 0)
11056 return stub_align - (stub_off & (stub_align - 1));
11057 return 0;
11058 }
11059
11060 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 11061 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 11062 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 11063 > ((stub_size - 1) & -stub_align))
794e51c0
AM
11064 return stub_align - (stub_off & (stub_align - 1));
11065 return 0;
11066}
11067
11068/* Build a .plt call stub. */
11069
11070static inline bfd_byte *
11071build_plt_stub (struct ppc_link_hash_table *htab,
11072 struct ppc_stub_hash_entry *stub_entry,
11073 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11074{
e7d1c40c 11075 bfd *obfd = htab->params->stub_bfd;
b9e5796b 11076 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 11077 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
11078 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
11079 && htab->elf.dynamic_sections_created
11080 && stub_entry->h != NULL
11081 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
11082 bfd_boolean use_fake_dep = plt_thread_safe;
11083 bfd_vma cmp_branch_off = 0;
11084
11085 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11086 && plt_load_toc
794e51c0 11087 && plt_thread_safe
bd4d2eaa
AM
11088 && !((stub_entry->h == htab->tls_get_addr_fd
11089 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11090 && htab->params->tls_get_addr_opt))
794e51c0
AM
11091 {
11092 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11093 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11094 / PLT_ENTRY_SIZE (htab));
9e390558 11095 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11096 bfd_vma to, from;
11097
68d62958
AM
11098 if (pltindex > 32768)
11099 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11100 to = (glinkoff
11101 + htab->glink->output_offset
11102 + htab->glink->output_section->vma);
6f20ed8a 11103 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
11104 + 4 * (ALWAYS_EMIT_R2SAVE
11105 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11106 + 4 * (PPC_HA (offset) != 0)
11107 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11108 != PPC_HA (offset))
11109 + 4 * (plt_static_chain != 0)
11110 + 20
6f20ed8a
AM
11111 + stub_entry->group->stub_sec->output_offset
11112 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11113 cmp_branch_off = to - from;
11114 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11115 }
11116
ac2df442
AM
11117 if (PPC_HA (offset) != 0)
11118 {
176a0d42
AM
11119 if (r != NULL)
11120 {
794e51c0
AM
11121 if (ALWAYS_EMIT_R2SAVE
11122 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11123 r[0].r_offset += 4;
176a0d42 11124 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11125 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11126 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11127 r[1].r_addend = r[0].r_addend;
b9e5796b 11128 if (plt_load_toc)
176a0d42 11129 {
b9e5796b 11130 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11131 {
b9e5796b
AM
11132 r[2].r_offset = r[1].r_offset + 4;
11133 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11134 r[2].r_addend = r[0].r_addend;
11135 }
11136 else
11137 {
11138 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11139 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11140 r[2].r_addend = r[0].r_addend + 8;
11141 if (plt_static_chain)
11142 {
11143 r[3].r_offset = r[2].r_offset + 4;
11144 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11145 r[3].r_addend = r[0].r_addend + 16;
11146 }
c7131b65 11147 }
176a0d42
AM
11148 }
11149 }
794e51c0
AM
11150 if (ALWAYS_EMIT_R2SAVE
11151 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11152 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11153 if (plt_load_toc)
11154 {
11155 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11156 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11157 }
11158 else
11159 {
11160 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11161 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11162 }
b9e5796b
AM
11163 if (plt_load_toc
11164 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11165 {
71a39c98 11166 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11167 offset = 0;
11168 }
71a39c98 11169 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11170 if (plt_load_toc)
794e51c0 11171 {
b9e5796b
AM
11172 if (use_fake_dep)
11173 {
11174 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11175 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11176 }
11177 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11178 if (plt_static_chain)
11179 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11180 }
ac2df442
AM
11181 }
11182 else
11183 {
176a0d42
AM
11184 if (r != NULL)
11185 {
794e51c0
AM
11186 if (ALWAYS_EMIT_R2SAVE
11187 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11188 r[0].r_offset += 4;
176a0d42 11189 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11190 if (plt_load_toc)
176a0d42 11191 {
b9e5796b 11192 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11193 {
b9e5796b
AM
11194 r[1].r_offset = r[0].r_offset + 4;
11195 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11196 r[1].r_addend = r[0].r_addend;
11197 }
11198 else
11199 {
11200 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11201 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11202 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11203 if (plt_static_chain)
11204 {
11205 r[2].r_offset = r[1].r_offset + 4;
11206 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11207 r[2].r_addend = r[0].r_addend + 8;
11208 }
c7131b65 11209 }
176a0d42
AM
11210 }
11211 }
794e51c0
AM
11212 if (ALWAYS_EMIT_R2SAVE
11213 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11214 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11215 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11216 if (plt_load_toc
11217 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11218 {
11219 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11220 offset = 0;
11221 }
71a39c98 11222 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11223 if (plt_load_toc)
794e51c0 11224 {
b9e5796b
AM
11225 if (use_fake_dep)
11226 {
11227 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11228 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11229 }
11230 if (plt_static_chain)
11231 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11232 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11233 }
ac2df442 11234 }
b9e5796b 11235 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11236 {
11237 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11238 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11239 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11240 }
11241 else
407aa07c 11242 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11243 return p;
11244}
11245
a7f2871e
AM
11246/* Build a special .plt call stub for __tls_get_addr. */
11247
11248#define LD_R11_0R3 0xe9630000
11249#define LD_R12_0R3 0xe9830000
11250#define MR_R0_R3 0x7c601b78
11251#define CMPDI_R11_0 0x2c2b0000
11252#define ADD_R3_R12_R13 0x7c6c6a14
11253#define BEQLR 0x4d820020
11254#define MR_R3_R0 0x7c030378
a7f2871e
AM
11255#define STD_R11_0R1 0xf9610000
11256#define BCTRL 0x4e800421
11257#define LD_R11_0R1 0xe9610000
a7f2871e
AM
11258#define MTLR_R11 0x7d6803a6
11259
11260static inline bfd_byte *
794e51c0
AM
11261build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11262 struct ppc_stub_hash_entry *stub_entry,
11263 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 11264{
e7d1c40c 11265 bfd *obfd = htab->params->stub_bfd;
794e51c0 11266
a7f2871e
AM
11267 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11268 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11269 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11270 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11271 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11272 bfd_put_32 (obfd, BEQLR, p), p += 4;
11273 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
11274 if (r != NULL)
11275 r[0].r_offset += 7 * 4;
11276 if (!ALWAYS_EMIT_R2SAVE
11277 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11278 return build_plt_stub (htab, stub_entry, p, offset, r);
11279
a7f2871e 11280 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 11281 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11282
11283 if (r != NULL)
f378ab09 11284 r[0].r_offset += 2 * 4;
794e51c0 11285 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 11286 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 11287
a078d95a 11288 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 11289 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11290 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11291 bfd_put_32 (obfd, BLR, p), p += 4;
11292
11293 return p;
11294}
11295
176a0d42
AM
11296static Elf_Internal_Rela *
11297get_relocs (asection *sec, int count)
11298{
11299 Elf_Internal_Rela *relocs;
11300 struct bfd_elf_section_data *elfsec_data;
11301
11302 elfsec_data = elf_section_data (sec);
11303 relocs = elfsec_data->relocs;
11304 if (relocs == NULL)
11305 {
11306 bfd_size_type relsize;
11307 relsize = sec->reloc_count * sizeof (*relocs);
11308 relocs = bfd_alloc (sec->owner, relsize);
11309 if (relocs == NULL)
11310 return NULL;
11311 elfsec_data->relocs = relocs;
d4730f92
BS
11312 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11313 sizeof (Elf_Internal_Shdr));
11314 if (elfsec_data->rela.hdr == NULL)
11315 return NULL;
11316 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11317 * sizeof (Elf64_External_Rela));
11318 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11319 sec->reloc_count = 0;
11320 }
11321 relocs += sec->reloc_count;
11322 sec->reloc_count += count;
11323 return relocs;
11324}
11325
aa374f67 11326static bfd_vma
25f53a85 11327get_r2off (struct bfd_link_info *info,
aa374f67
AM
11328 struct ppc_stub_hash_entry *stub_entry)
11329{
25f53a85 11330 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11331 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11332
11333 if (r2off == 0)
11334 {
11335 /* Support linking -R objects. Get the toc pointer from the
11336 opd entry. */
11337 char buf[8];
b9e5796b
AM
11338 if (!htab->opd_abi)
11339 return r2off;
aa374f67
AM
11340 asection *opd = stub_entry->h->elf.root.u.def.section;
11341 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11342
11343 if (strcmp (opd->name, ".opd") != 0
11344 || opd->reloc_count != 0)
11345 {
c1c8c1ef 11346 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
25f53a85 11347 stub_entry->h->elf.root.root.string);
aa374f67 11348 bfd_set_error (bfd_error_bad_value);
a7c49797 11349 return (bfd_vma) -1;
aa374f67
AM
11350 }
11351 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11352 return (bfd_vma) -1;
aa374f67 11353 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11354 r2off -= elf_gp (info->output_bfd);
aa374f67 11355 }
6f20ed8a 11356 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11357 return r2off;
11358}
11359
b34976b6 11360static bfd_boolean
4ce794b7 11361ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11362{
721956f4
AM
11363 struct ppc_stub_hash_entry *stub_entry;
11364 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11365 struct bfd_link_info *info;
11366 struct ppc_link_hash_table *htab;
721956f4
AM
11367 bfd_byte *loc;
11368 bfd_byte *p;
1aa42141 11369 bfd_vma targ, off;
176a0d42 11370 Elf_Internal_Rela *r;
e054468f 11371 asection *plt;
5d1634d7 11372
721956f4
AM
11373 /* Massage our args to the form they really have. */
11374 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11375 info = in_arg;
5d1634d7 11376
5d1634d7 11377 htab = ppc_hash_table (info);
4dfe6ac6
NC
11378 if (htab == NULL)
11379 return FALSE;
5d1634d7 11380
1aa42141 11381 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11382 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11383
4ce794b7 11384 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11385 switch (stub_entry->stub_type)
5d1634d7 11386 {
721956f4 11387 case ppc_stub_long_branch:
ad8e1ba5 11388 case ppc_stub_long_branch_r2off:
721956f4 11389 /* Branches are relative. This is where we are going to. */
1aa42141 11390 targ = (stub_entry->target_value
6911b7dc
AM
11391 + stub_entry->target_section->output_offset
11392 + stub_entry->target_section->output_section->vma);
1aa42141 11393 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11394
721956f4 11395 /* And this is where we are coming from. */
1aa42141
AM
11396 off = (stub_entry->stub_offset
11397 + stub_entry->group->stub_sec->output_offset
11398 + stub_entry->group->stub_sec->output_section->vma);
11399 off = targ - off;
e86ce104 11400
9e390558 11401 p = loc;
ac2df442 11402 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11403 {
25f53a85 11404 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11405
a7c49797 11406 if (r2off == (bfd_vma) -1)
aa374f67
AM
11407 {
11408 htab->stub_error = TRUE;
11409 return FALSE;
11410 }
9e390558
AM
11411 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11412 p += 4;
ac2df442
AM
11413 if (PPC_HA (r2off) != 0)
11414 {
e7d1c40c 11415 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11416 ADDIS_R2_R2 | PPC_HA (r2off), p);
11417 p += 4;
a7c49797
AM
11418 }
11419 if (PPC_LO (r2off) != 0)
11420 {
11421 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11422 ADDI_R2_R2 | PPC_LO (r2off), p);
11423 p += 4;
ac2df442 11424 }
9e390558 11425 off -= p - loc;
ad8e1ba5 11426 }
9e390558
AM
11427 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11428 p += 4;
ad8e1ba5 11429
5c3dead3
AM
11430 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11431 {
cf97bcb0
AM
11432 _bfd_error_handler
11433 (_("long branch stub `%s' offset overflow"),
bc30df16 11434 stub_entry->root.string);
5c3dead3
AM
11435 htab->stub_error = TRUE;
11436 return FALSE;
11437 }
ee75fd95
AM
11438
11439 if (info->emitrelocations)
11440 {
6f20ed8a 11441 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11442 if (r == NULL)
11443 return FALSE;
9e390558 11444 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11445 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11446 r->r_addend = targ;
ee75fd95
AM
11447 if (stub_entry->h != NULL)
11448 {
11449 struct elf_link_hash_entry **hashes;
11450 unsigned long symndx;
11451 struct ppc_link_hash_entry *h;
11452
e7d1c40c 11453 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
11454 if (hashes == NULL)
11455 {
11456 bfd_size_type hsize;
11457
11458 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 11459 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
11460 if (hashes == NULL)
11461 return FALSE;
e7d1c40c 11462 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
11463 htab->stub_globals = 1;
11464 }
11465 symndx = htab->stub_globals++;
11466 h = stub_entry->h;
11467 hashes[symndx] = &h->elf;
11468 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11469 if (h->oh != NULL && h->oh->is_func)
b31867b6 11470 h = ppc_follow_link (h->oh);
ee75fd95
AM
11471 if (h->elf.root.u.def.section != stub_entry->target_section)
11472 /* H is an opd symbol. The addend must be zero. */
11473 r->r_addend = 0;
11474 else
11475 {
11476 off = (h->elf.root.u.def.value
11477 + h->elf.root.u.def.section->output_offset
11478 + h->elf.root.u.def.section->output_section->vma);
11479 r->r_addend -= off;
11480 }
11481 }
11482 }
721956f4 11483 break;
e86ce104 11484
721956f4 11485 case ppc_stub_plt_branch:
ad8e1ba5 11486 case ppc_stub_plt_branch_r2off:
721956f4
AM
11487 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11488 stub_entry->root.string + 9,
b34976b6 11489 FALSE, FALSE);
721956f4
AM
11490 if (br_entry == NULL)
11491 {
cf97bcb0
AM
11492 _bfd_error_handler (_("can't find branch stub `%s'"),
11493 stub_entry->root.string);
b34976b6
AM
11494 htab->stub_error = TRUE;
11495 return FALSE;
721956f4
AM
11496 }
11497
1aa42141 11498 targ = (stub_entry->target_value
176a0d42
AM
11499 + stub_entry->target_section->output_offset
11500 + stub_entry->target_section->output_section->vma);
6911b7dc 11501 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11502 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11503
1aa42141 11504 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11505 htab->brlt->contents + br_entry->offset);
721956f4 11506
f94498ff 11507 if (br_entry->iter == htab->stub_iteration)
721956f4 11508 {
f94498ff 11509 br_entry->iter = 0;
84f5d08e 11510
f94498ff 11511 if (htab->relbrlt != NULL)
84f5d08e 11512 {
f94498ff
AM
11513 /* Create a reloc for the branch lookup table entry. */
11514 Elf_Internal_Rela rela;
11515 bfd_byte *rl;
11516
11517 rela.r_offset = (br_entry->offset
11518 + htab->brlt->output_offset
11519 + htab->brlt->output_section->vma);
11520 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11521 rela.r_addend = targ;
f94498ff
AM
11522
11523 rl = htab->relbrlt->contents;
11524 rl += (htab->relbrlt->reloc_count++
11525 * sizeof (Elf64_External_Rela));
11526 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11527 }
11528 else if (info->emitrelocations)
11529 {
176a0d42
AM
11530 r = get_relocs (htab->brlt, 1);
11531 if (r == NULL)
11532 return FALSE;
11533 /* brlt, being SEC_LINKER_CREATED does not go through the
11534 normal reloc processing. Symbols and offsets are not
11535 translated from input file to output file form, so
11536 set up the offset per the output file. */
f94498ff
AM
11537 r->r_offset = (br_entry->offset
11538 + htab->brlt->output_offset
11539 + htab->brlt->output_section->vma);
11540 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11541 r->r_addend = targ;
84f5d08e 11542 }
84f5d08e 11543 }
721956f4 11544
1aa42141 11545 targ = (br_entry->offset
176a0d42
AM
11546 + htab->brlt->output_offset
11547 + htab->brlt->output_section->vma);
11548
1aa42141
AM
11549 off = (elf_gp (info->output_bfd)
11550 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11551 off = targ - off;
721956f4 11552
ad8e1ba5 11553 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11554 {
25f53a85 11555 info->callbacks->einfo
c1c8c1ef 11556 (_("%P: linkage table error against `%pT'\n"),
721956f4 11557 stub_entry->root.string);
5d1634d7 11558 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11559 htab->stub_error = TRUE;
11560 return FALSE;
5d1634d7 11561 }
41bd81ab 11562
176a0d42
AM
11563 if (info->emitrelocations)
11564 {
6f20ed8a 11565 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11566 if (r == NULL)
11567 return FALSE;
6f20ed8a 11568 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11569 if (bfd_big_endian (info->output_bfd))
11570 r[0].r_offset += 2;
00f412ee 11571 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11572 r[0].r_offset += 4;
11573 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11574 r[0].r_addend = targ;
176a0d42
AM
11575 if (PPC_HA (off) != 0)
11576 {
11577 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11578 r[1].r_offset = r[0].r_offset + 4;
11579 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11580 r[1].r_addend = r[0].r_addend;
11581 }
11582 }
11583
9e390558 11584 p = loc;
00f412ee 11585 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11586 {
176a0d42 11587 if (PPC_HA (off) != 0)
ac2df442 11588 {
e7d1c40c 11589 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11590 ADDIS_R12_R2 | PPC_HA (off), p);
11591 p += 4;
e7d1c40c 11592 bfd_put_32 (htab->params->stub_bfd,
9e390558 11593 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11594 }
11595 else
9e390558
AM
11596 bfd_put_32 (htab->params->stub_bfd,
11597 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11598 }
11599 else
11600 {
25f53a85 11601 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11602
a7c49797 11603 if (r2off == (bfd_vma) -1)
aa374f67
AM
11604 {
11605 htab->stub_error = TRUE;
11606 return FALSE;
11607 }
ad8e1ba5 11608
9e390558
AM
11609 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11610 p += 4;
176a0d42 11611 if (PPC_HA (off) != 0)
ac2df442 11612 {
e7d1c40c 11613 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11614 ADDIS_R12_R2 | PPC_HA (off), p);
11615 p += 4;
e7d1c40c 11616 bfd_put_32 (htab->params->stub_bfd,
9e390558 11617 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11618 }
11619 else
9e390558 11620 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11621
11622 if (PPC_HA (r2off) != 0)
11623 {
9e390558 11624 p += 4;
e7d1c40c 11625 bfd_put_32 (htab->params->stub_bfd,
9e390558 11626 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11627 }
11628 if (PPC_LO (r2off) != 0)
11629 {
9e390558 11630 p += 4;
e7d1c40c 11631 bfd_put_32 (htab->params->stub_bfd,
9e390558 11632 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11633 }
ad8e1ba5 11634 }
9e390558
AM
11635 p += 4;
11636 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11637 p += 4;
407aa07c
AM
11638 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11639 p += 4;
721956f4 11640 break;
5d1634d7 11641
05d0e962
AM
11642 case ppc_stub_long_branch_notoc:
11643 case ppc_stub_long_branch_both:
11644 case ppc_stub_plt_branch_notoc:
11645 case ppc_stub_plt_branch_both:
11646 case ppc_stub_plt_call_notoc:
11647 case ppc_stub_plt_call_both:
11648 p = loc;
11649 off = (8 + stub_entry->stub_offset
11650 + stub_entry->group->stub_sec->output_offset
11651 + stub_entry->group->stub_sec->output_section->vma);
11652 if (stub_entry->stub_type == ppc_stub_long_branch_both
11653 || stub_entry->stub_type == ppc_stub_plt_branch_both
11654 || stub_entry->stub_type == ppc_stub_plt_call_both)
11655 {
11656 off += 4;
11657 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11658 p += 4;
11659 }
11660 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11661 {
11662 targ = stub_entry->plt_ent->plt.offset & ~1;
11663 if (targ >= (bfd_vma) -2)
11664 abort ();
11665
11666 plt = htab->elf.splt;
11667 if (!htab->elf.dynamic_sections_created
11668 || stub_entry->h == NULL
11669 || stub_entry->h->elf.dynindx == -1)
11670 {
11671 if (stub_entry->symtype == STT_GNU_IFUNC)
11672 plt = htab->elf.iplt;
11673 else
11674 plt = htab->pltlocal;
11675 }
11676 targ += plt->output_offset + plt->output_section->vma;
11677 }
11678 else
11679 targ = (stub_entry->target_value
11680 + stub_entry->target_section->output_offset
11681 + stub_entry->target_section->output_section->vma);
11682 off = targ - off;
11683 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
11684 p += 4;
11685 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
11686 p += 4;
11687 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
11688 p += 4;
11689 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
11690 p += 4;
11691 p = build_offset (htab->params->stub_bfd, p, off,
11692 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11693 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
11694 {
11695 off += 8;
11696 bfd_put_32 (htab->params->stub_bfd,
11697 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11698 }
11699 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
11700 {
11701 off += 12;
11702 bfd_put_32 (htab->params->stub_bfd,
11703 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11704 }
11705 else
11706 {
11707 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11708 p += 4;
11709 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11710 }
11711 p += 4;
11712 break;
11713
721956f4 11714 case ppc_stub_plt_call:
794e51c0 11715 case ppc_stub_plt_call_r2save:
e054468f 11716 if (stub_entry->h != NULL
b31867b6
AM
11717 && stub_entry->h->is_func_descriptor
11718 && stub_entry->h->oh != NULL)
c862ae31 11719 {
b31867b6
AM
11720 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11721
11722 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11723 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11724 if (fh->elf.root.type == bfd_link_hash_undefined
11725 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11726 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11727 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11728 }
11729
721956f4 11730 /* Now build the stub. */
1aa42141
AM
11731 targ = stub_entry->plt_ent->plt.offset & ~1;
11732 if (targ >= (bfd_vma) -2)
721956f4
AM
11733 abort ();
11734
33e44f2e 11735 plt = htab->elf.splt;
25f23106
AM
11736 if (!htab->elf.dynamic_sections_created
11737 || stub_entry->h == NULL
11738 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11739 {
11740 if (stub_entry->symtype == STT_GNU_IFUNC)
11741 plt = htab->elf.iplt;
11742 else
11743 plt = htab->pltlocal;
11744 }
1aa42141 11745 targ += plt->output_offset + plt->output_section->vma;
e054468f 11746
1aa42141
AM
11747 off = (elf_gp (info->output_bfd)
11748 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11749 off = targ - off;
721956f4 11750
ad8e1ba5 11751 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11752 {
25f53a85 11753 info->callbacks->einfo
695344c0 11754 /* xgettext:c-format */
c1c8c1ef 11755 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11756 stub_entry->h != NULL
11757 ? stub_entry->h->elf.root.root.string
11758 : "<local sym>");
721956f4 11759 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11760 htab->stub_error = TRUE;
11761 return FALSE;
721956f4
AM
11762 }
11763
176a0d42
AM
11764 r = NULL;
11765 if (info->emitrelocations)
11766 {
6f20ed8a 11767 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11768 ((PPC_HA (off) != 0)
11769 + (htab->opd_abi
e7d1c40c 11770 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11771 && PPC_HA (off + 16) == PPC_HA (off))
11772 : 1)));
176a0d42
AM
11773 if (r == NULL)
11774 return FALSE;
6f20ed8a 11775 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11776 if (bfd_big_endian (info->output_bfd))
11777 r[0].r_offset += 2;
1aa42141 11778 r[0].r_addend = targ;
176a0d42 11779 }
a7f2871e
AM
11780 if (stub_entry->h != NULL
11781 && (stub_entry->h == htab->tls_get_addr_fd
11782 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11783 && htab->params->tls_get_addr_opt)
794e51c0 11784 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11785 else
794e51c0 11786 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11787 break;
11788
a4b6fadd
AM
11789 case ppc_stub_save_res:
11790 return TRUE;
11791
721956f4
AM
11792 default:
11793 BFD_FAIL ();
b34976b6 11794 return FALSE;
721956f4
AM
11795 }
11796
1aa42141 11797 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11798
e7d1c40c 11799 if (htab->params->emit_stub_syms)
97b639ba
AM
11800 {
11801 struct elf_link_hash_entry *h;
ee75fd95
AM
11802 size_t len1, len2;
11803 char *name;
11804 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11805 "long_branch",
11806 "long_branch",
3f6ff479
AM
11807 "long_branch",
11808 "plt_branch",
ee75fd95 11809 "plt_branch",
05d0e962
AM
11810 "plt_branch",
11811 "plt_branch",
11812 "plt_call",
11813 "plt_call",
794e51c0 11814 "plt_call",
ee75fd95
AM
11815 "plt_call" };
11816
11817 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11818 len2 = strlen (stub_entry->root.string);
11819 name = bfd_malloc (len1 + len2 + 2);
11820 if (name == NULL)
11821 return FALSE;
11822 memcpy (name, stub_entry->root.string, 9);
11823 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11824 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11825 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11826 if (h == NULL)
11827 return FALSE;
11828 if (h->root.type == bfd_link_hash_new)
11829 {
11830 h->root.type = bfd_link_hash_defined;
6f20ed8a 11831 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11832 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11833 h->ref_regular = 1;
11834 h->def_regular = 1;
11835 h->ref_regular_nonweak = 1;
11836 h->forced_local = 1;
11837 h->non_elf = 0;
2ec55de3 11838 h->root.linker_def = 1;
97b639ba
AM
11839 }
11840 }
11841
b34976b6 11842 return TRUE;
721956f4
AM
11843}
11844
11845/* As above, but don't actually build the stub. Just bump offset so
11846 we know stub section sizes, and select plt_branch stubs where
11847 long_branch stubs won't do. */
11848
b34976b6 11849static bfd_boolean
4ce794b7 11850ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11851{
11852 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11853 struct bfd_link_info *info;
721956f4 11854 struct ppc_link_hash_table *htab;
1aa42141 11855 bfd_vma targ, off;
721956f4
AM
11856 int size;
11857
11858 /* Massage our args to the form they really have. */
11859 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11860 info = in_arg;
11861
11862 htab = ppc_hash_table (info);
4dfe6ac6
NC
11863 if (htab == NULL)
11864 return FALSE;
721956f4 11865
1aa42141
AM
11866 /* Make a note of the offset within the stubs for this entry. */
11867 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11868
a4b6fadd
AM
11869 if (stub_entry->h != NULL
11870 && stub_entry->h->save_res
11871 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11872 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11873 {
11874 /* Don't make stubs to out-of-line register save/restore
11875 functions. Instead, emit copies of the functions. */
11876 stub_entry->group->needs_save_res = 1;
11877 stub_entry->stub_type = ppc_stub_save_res;
11878 return TRUE;
11879 }
11880
05d0e962
AM
11881 if (stub_entry->stub_type >= ppc_stub_plt_call
11882 && stub_entry->stub_type <= ppc_stub_plt_call_both)
721956f4 11883 {
e054468f 11884 asection *plt;
1aa42141
AM
11885 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11886 if (targ >= (bfd_vma) -2)
411e1bfb 11887 abort ();
33e44f2e 11888 plt = htab->elf.splt;
25f23106
AM
11889 if (!htab->elf.dynamic_sections_created
11890 || stub_entry->h == NULL
11891 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11892 {
11893 if (stub_entry->symtype == STT_GNU_IFUNC)
11894 plt = htab->elf.iplt;
11895 else
11896 plt = htab->pltlocal;
11897 }
1aa42141
AM
11898 targ += plt->output_offset + plt->output_section->vma;
11899
05d0e962
AM
11900 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11901 {
11902 off = (8 + stub_entry->stub_offset
11903 + stub_entry->group->stub_sec->output_offset
11904 + stub_entry->group->stub_sec->output_section->vma);
11905 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
11906 off += 4;
11907 }
11908 else
11909 off = (elf_gp (info->output_bfd)
11910 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
1aa42141
AM
11911
11912 if (htab->params->plt_stub_align != 0)
11913 {
05d0e962 11914 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
1aa42141
AM
11915
11916 stub_entry->group->stub_sec->size += pad;
11917 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
05d0e962
AM
11918 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11919 off += pad;
1aa42141 11920 }
721956f4 11921
05d0e962 11922 off = targ - off;
794e51c0 11923 size = plt_stub_size (htab, stub_entry, off);
1aa42141 11924
05d0e962 11925 if (stub_entry->stub_type < ppc_stub_plt_call_notoc)
176a0d42 11926 {
05d0e962
AM
11927 if (stub_entry->h != NULL
11928 && (stub_entry->h == htab->tls_get_addr_fd
11929 || stub_entry->h == htab->tls_get_addr)
11930 && htab->params->tls_get_addr_opt
11931 && (ALWAYS_EMIT_R2SAVE
11932 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11933 stub_entry->group->tls_get_addr_opt_bctrl
11934 = stub_entry->stub_offset + size - 5 * 4;
11935
11936 if (info->emitrelocations)
11937 {
11938 stub_entry->group->stub_sec->reloc_count
11939 += ((PPC_HA (off) != 0)
11940 + (htab->opd_abi
11941 ? 2 + (htab->params->plt_static_chain
11942 && PPC_HA (off + 16) == PPC_HA (off))
11943 : 1));
11944 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11945 }
176a0d42 11946 }
721956f4
AM
11947 }
11948 else
11949 {
ad8e1ba5
AM
11950 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11951 variants. */
ac2df442 11952 bfd_vma r2off = 0;
6911b7dc 11953 bfd_vma local_off = 0;
ac2df442 11954
1aa42141
AM
11955 targ = (stub_entry->target_value
11956 + stub_entry->target_section->output_offset
11957 + stub_entry->target_section->output_section->vma);
11958 off = (stub_entry->stub_offset
11959 + stub_entry->group->stub_sec->output_offset
11960 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11961
ad8e1ba5
AM
11962 /* Reset the stub type from the plt variant in case we now
11963 can reach with a shorter stub. */
11964 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11965 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11966
11967 size = 4;
11968 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11969 {
25f53a85 11970 r2off = get_r2off (info, stub_entry);
a7c49797 11971 if (r2off == (bfd_vma) -1)
aa374f67
AM
11972 {
11973 htab->stub_error = TRUE;
11974 return FALSE;
11975 }
a7c49797 11976 size = 8;
ac2df442 11977 if (PPC_HA (r2off) != 0)
a7c49797
AM
11978 size += 4;
11979 if (PPC_LO (r2off) != 0)
11980 size += 4;
1aa42141 11981 off += size - 4;
ad8e1ba5 11982 }
05d0e962
AM
11983 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
11984 {
11985 size = 20 + size_offset (targ - (off + 8));
11986 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
11987 size += 4;
11988 off += size - 4;
11989 }
1aa42141 11990 off = targ - off;
ad8e1ba5 11991
05d0e962 11992 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
721956f4 11993 {
05d0e962 11994 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11995 {
05d0e962
AM
11996 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11997 - ppc_stub_long_branch_notoc);
11998 size += 4;
721956f4 11999 }
05d0e962
AM
12000 }
12001 else
12002 {
12003 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 12004
05d0e962
AM
12005 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12006 Do the same for -R objects without function descriptors. */
12007 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12008 && r2off == 0
12009 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12010 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
721956f4 12011 {
05d0e962 12012 struct ppc_branch_hash_entry *br_entry;
63bc6f6c 12013
05d0e962
AM
12014 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12015 stub_entry->root.string + 9,
12016 TRUE, FALSE);
12017 if (br_entry == NULL)
84f5d08e 12018 {
05d0e962
AM
12019 _bfd_error_handler (_("can't build branch stub `%s'"),
12020 stub_entry->root.string);
12021 htab->stub_error = TRUE;
12022 return FALSE;
84f5d08e 12023 }
ad8e1ba5 12024
05d0e962
AM
12025 if (br_entry->iter != htab->stub_iteration)
12026 {
12027 br_entry->iter = htab->stub_iteration;
12028 br_entry->offset = htab->brlt->size;
12029 htab->brlt->size += 8;
12030
12031 if (htab->relbrlt != NULL)
12032 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12033 else if (info->emitrelocations)
12034 {
12035 htab->brlt->reloc_count += 1;
12036 htab->brlt->flags |= SEC_RELOC;
12037 }
12038 }
ac2df442 12039
05d0e962
AM
12040 targ = (br_entry->offset
12041 + htab->brlt->output_offset
12042 + htab->brlt->output_section->vma);
12043 off = (elf_gp (info->output_bfd)
12044 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12045 off = targ - off;
176a0d42 12046
05d0e962
AM
12047 if (info->emitrelocations)
12048 {
12049 stub_entry->group->stub_sec->reloc_count
12050 += 1 + (PPC_HA (off) != 0);
12051 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12052 }
12053
12054 stub_entry->stub_type
12055 += ppc_stub_plt_branch - ppc_stub_long_branch;
12056 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12057 {
12058 size = 12;
12059 if (PPC_HA (off) != 0)
12060 size = 16;
12061 }
12062 else
12063 {
12064 size = 16;
12065 if (PPC_HA (off) != 0)
12066 size += 4;
12067
12068 if (PPC_HA (r2off) != 0)
12069 size += 4;
12070 if (PPC_LO (r2off) != 0)
12071 size += 4;
12072 }
ac2df442 12073 }
05d0e962 12074 else if (info->emitrelocations)
ac2df442 12075 {
05d0e962
AM
12076 stub_entry->group->stub_sec->reloc_count += 1;
12077 stub_entry->group->stub_sec->flags |= SEC_RELOC;
ac2df442 12078 }
721956f4
AM
12079 }
12080 }
12081
6f20ed8a 12082 stub_entry->group->stub_sec->size += size;
b34976b6 12083 return TRUE;
721956f4
AM
12084}
12085
12086/* Set up various things so that we can make a list of input sections
12087 for each output section included in the link. Returns -1 on error,
cedb70c5 12088 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12089
12090int
e7d1c40c 12091ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12092{
6f20ed8a 12093 unsigned int id;
721956f4
AM
12094 bfd_size_type amt;
12095 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12096
4dfe6ac6
NC
12097 if (htab == NULL)
12098 return -1;
4c52953f 12099
7cf7fcc8 12100 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12101 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12102 htab->sec_info = bfd_zmalloc (amt);
12103 if (htab->sec_info == NULL)
721956f4
AM
12104 return -1;
12105
3d6f9012
AM
12106 /* Set toc_off for com, und, abs and ind sections. */
12107 for (id = 0; id < 3; id++)
6f20ed8a 12108 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12109
721956f4
AM
12110 return 1;
12111}
12112
927be08e
AM
12113/* Set up for first pass at multitoc partitioning. */
12114
12115void
12116ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12117{
12118 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12119
1c865ab2 12120 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12121 htab->toc_bfd = NULL;
12122 htab->toc_first_sec = NULL;
12123}
12124
e717da7e
AM
12125/* The linker repeatedly calls this function for each TOC input section
12126 and linker generated GOT section. Group input bfds such that the toc
927be08e 12127 within a group is less than 64k in size. */
ad8e1ba5 12128
927be08e 12129bfd_boolean
4ce794b7 12130ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12131{
12132 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12133 bfd_vma addr, off, limit;
ad8e1ba5 12134
4dfe6ac6
NC
12135 if (htab == NULL)
12136 return FALSE;
12137
927be08e 12138 if (!htab->second_toc_pass)
4c52953f 12139 {
927be08e 12140 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
12141 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12142
12143 if (new_bfd)
bf102f86
AM
12144 {
12145 htab->toc_bfd = isec->owner;
12146 htab->toc_first_sec = isec;
12147 }
927be08e 12148
bf102f86
AM
12149 addr = isec->output_offset + isec->output_section->vma;
12150 off = addr - htab->toc_curr;
d77c8a4b
AM
12151 limit = 0x80008000;
12152 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12153 limit = 0x10000;
12154 if (off + isec->size > limit)
bf102f86
AM
12155 {
12156 addr = (htab->toc_first_sec->output_offset
12157 + htab->toc_first_sec->output_section->vma);
12158 htab->toc_curr = addr;
a27e685f 12159 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12160 }
99877b66 12161
927be08e
AM
12162 /* toc_curr is the base address of this toc group. Set elf_gp
12163 for the input section to be the offset relative to the
12164 output toc base plus 0x8000. Making the input elf_gp an
12165 offset allows us to move the toc as a whole without
12166 recalculating input elf_gp. */
06bcf541 12167 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12168 off += TOC_BASE_OFF;
12169
12170 /* Die if someone uses a linker script that doesn't keep input
12171 file .toc and .got together. */
a4fd3de5
AM
12172 if (new_bfd
12173 && elf_gp (isec->owner) != 0
927be08e
AM
12174 && elf_gp (isec->owner) != off)
12175 return FALSE;
12176
12177 elf_gp (isec->owner) = off;
12178 return TRUE;
4c52953f 12179 }
927be08e
AM
12180
12181 /* During the second pass toc_first_sec points to the start of
12182 a toc group, and toc_curr is used to track the old elf_gp.
12183 We use toc_bfd to ensure we only look at each bfd once. */
12184 if (htab->toc_bfd == isec->owner)
12185 return TRUE;
12186 htab->toc_bfd = isec->owner;
12187
12188 if (htab->toc_first_sec == NULL
12189 || htab->toc_curr != elf_gp (isec->owner))
12190 {
12191 htab->toc_curr = elf_gp (isec->owner);
12192 htab->toc_first_sec = isec;
12193 }
12194 addr = (htab->toc_first_sec->output_offset
12195 + htab->toc_first_sec->output_section->vma);
06bcf541 12196 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12197 elf_gp (isec->owner) = off;
12198
12199 return TRUE;
ad8e1ba5
AM
12200}
12201
927be08e
AM
12202/* Called via elf_link_hash_traverse to merge GOT entries for global
12203 symbol H. */
12204
12205static bfd_boolean
12206merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12207{
12208 if (h->root.type == bfd_link_hash_indirect)
12209 return TRUE;
12210
927be08e
AM
12211 merge_got_entries (&h->got.glist);
12212
12213 return TRUE;
12214}
12215
12216/* Called via elf_link_hash_traverse to allocate GOT entries for global
12217 symbol H. */
12218
12219static bfd_boolean
12220reallocate_got (struct elf_link_hash_entry *h, void *inf)
12221{
12222 struct got_entry *gent;
12223
12224 if (h->root.type == bfd_link_hash_indirect)
12225 return TRUE;
12226
927be08e
AM
12227 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12228 if (!gent->is_indirect)
12229 allocate_got (h, (struct bfd_link_info *) inf, gent);
12230 return TRUE;
12231}
12232
12233/* Called on the first multitoc pass after the last call to
12234 ppc64_elf_next_toc_section. This function removes duplicate GOT
12235 entries. */
12236
12237bfd_boolean
12238ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12239{
12240 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12241 struct bfd *ibfd, *ibfd2;
12242 bfd_boolean done_something;
12243
12244 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12245
7865406b
AM
12246 if (!htab->do_multi_toc)
12247 return FALSE;
12248
d0fae19d 12249 /* Merge global sym got entries within a toc group. */
927be08e
AM
12250 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12251
12252 /* And tlsld_got. */
c72f2fb2 12253 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12254 {
12255 struct got_entry *ent, *ent2;
12256
12257 if (!is_ppc64_elf (ibfd))
12258 continue;
12259
12260 ent = ppc64_tlsld_got (ibfd);
12261 if (!ent->is_indirect
12262 && ent->got.offset != (bfd_vma) -1)
12263 {
c72f2fb2 12264 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12265 {
12266 if (!is_ppc64_elf (ibfd2))
12267 continue;
12268
12269 ent2 = ppc64_tlsld_got (ibfd2);
12270 if (!ent2->is_indirect
12271 && ent2->got.offset != (bfd_vma) -1
12272 && elf_gp (ibfd2) == elf_gp (ibfd))
12273 {
12274 ent2->is_indirect = TRUE;
12275 ent2->got.ent = ent;
12276 }
12277 }
12278 }
12279 }
12280
12281 /* Zap sizes of got sections. */
33e44f2e
AM
12282 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12283 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12284 htab->got_reli_size = 0;
12285
c72f2fb2 12286 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12287 {
12288 asection *got, *relgot;
12289
12290 if (!is_ppc64_elf (ibfd))
12291 continue;
12292
12293 got = ppc64_elf_tdata (ibfd)->got;
12294 if (got != NULL)
12295 {
12296 got->rawsize = got->size;
12297 got->size = 0;
12298 relgot = ppc64_elf_tdata (ibfd)->relgot;
12299 relgot->rawsize = relgot->size;
12300 relgot->size = 0;
12301 }
12302 }
12303
12304 /* Now reallocate the got, local syms first. We don't need to
12305 allocate section contents again since we never increase size. */
c72f2fb2 12306 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12307 {
12308 struct got_entry **lgot_ents;
12309 struct got_entry **end_lgot_ents;
12310 struct plt_entry **local_plt;
12311 struct plt_entry **end_local_plt;
f961d9dd 12312 unsigned char *lgot_masks;
927be08e
AM
12313 bfd_size_type locsymcount;
12314 Elf_Internal_Shdr *symtab_hdr;
19e08130 12315 asection *s;
927be08e
AM
12316
12317 if (!is_ppc64_elf (ibfd))
12318 continue;
12319
12320 lgot_ents = elf_local_got_ents (ibfd);
12321 if (!lgot_ents)
12322 continue;
12323
12324 symtab_hdr = &elf_symtab_hdr (ibfd);
12325 locsymcount = symtab_hdr->sh_info;
12326 end_lgot_ents = lgot_ents + locsymcount;
12327 local_plt = (struct plt_entry **) end_lgot_ents;
12328 end_local_plt = local_plt + locsymcount;
f961d9dd 12329 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12330 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12331 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12332 {
12333 struct got_entry *ent;
12334
12335 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12336 {
19e08130
AM
12337 unsigned int ent_size = 8;
12338 unsigned int rel_size = sizeof (Elf64_External_Rela);
12339
d0fae19d
AM
12340 ent->got.offset = s->size;
12341 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12342 {
19e08130
AM
12343 ent_size *= 2;
12344 rel_size *= 2;
12345 }
12346 s->size += ent_size;
37da22e5 12347 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12348 {
33e44f2e 12349 htab->elf.irelplt->size += rel_size;
19e08130
AM
12350 htab->got_reli_size += rel_size;
12351 }
6a3858a6
AM
12352 else if (bfd_link_pic (info)
12353 && !((ent->tls_type & TLS_TPREL) != 0
12354 && bfd_link_executable (info)))
19e08130
AM
12355 {
12356 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12357 srel->size += rel_size;
d0fae19d
AM
12358 }
12359 }
927be08e
AM
12360 }
12361 }
12362
12363 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12364
c72f2fb2 12365 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12366 {
12367 struct got_entry *ent;
12368
12369 if (!is_ppc64_elf (ibfd))
12370 continue;
12371
12372 ent = ppc64_tlsld_got (ibfd);
12373 if (!ent->is_indirect
12374 && ent->got.offset != (bfd_vma) -1)
12375 {
12376 asection *s = ppc64_elf_tdata (ibfd)->got;
12377 ent->got.offset = s->size;
12378 s->size += 16;
0e1862bb 12379 if (bfd_link_pic (info))
927be08e
AM
12380 {
12381 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12382 srel->size += sizeof (Elf64_External_Rela);
12383 }
12384 }
12385 }
12386
33e44f2e 12387 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12388 if (!done_something)
c72f2fb2 12389 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12390 {
12391 asection *got;
12392
12393 if (!is_ppc64_elf (ibfd))
12394 continue;
12395
12396 got = ppc64_elf_tdata (ibfd)->got;
12397 if (got != NULL)
12398 {
12399 done_something = got->rawsize != got->size;
12400 if (done_something)
12401 break;
12402 }
12403 }
12404
12405 if (done_something)
e7d1c40c 12406 (*htab->params->layout_sections_again) ();
927be08e
AM
12407
12408 /* Set up for second pass over toc sections to recalculate elf_gp
12409 on input sections. */
12410 htab->toc_bfd = NULL;
12411 htab->toc_first_sec = NULL;
12412 htab->second_toc_pass = TRUE;
12413 return done_something;
12414}
12415
12416/* Called after second pass of multitoc partitioning. */
12417
12418void
12419ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12420{
12421 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12422
12423 /* After the second pass, toc_curr tracks the TOC offset used
12424 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12425 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12426}
12427
9b5ecbd0
AM
12428/* No toc references were found in ISEC. If the code in ISEC makes no
12429 calls, then there's no need to use toc adjusting stubs when branching
12430 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12431 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12432 needed, and 2 if a cyclical call-graph was found but no other reason
12433 for a stub was detected. If called from the top level, a return of
12434 2 means the same as a return of 0. */
9b5ecbd0
AM
12435
12436static int
4ce794b7 12437toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12438{
9b5ecbd0 12439 int ret;
70cc837d
AM
12440
12441 /* Mark this section as checked. */
12442 isec->call_check_done = 1;
9b5ecbd0 12443
772119ce
AM
12444 /* We know none of our code bearing sections will need toc stubs. */
12445 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12446 return 0;
12447
eea6121a 12448 if (isec->size == 0)
082c50f8
AM
12449 return 0;
12450
4c52953f
AM
12451 if (isec->output_section == NULL)
12452 return 0;
12453
4c52953f 12454 ret = 0;
70cc837d 12455 if (isec->reloc_count != 0)
9b5ecbd0 12456 {
70cc837d
AM
12457 Elf_Internal_Rela *relstart, *rel;
12458 Elf_Internal_Sym *local_syms;
12459 struct ppc_link_hash_table *htab;
2917689a 12460
70cc837d
AM
12461 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12462 info->keep_memory);
12463 if (relstart == NULL)
12464 return -1;
90aecf7a 12465
70cc837d
AM
12466 /* Look for branches to outside of this section. */
12467 local_syms = NULL;
12468 htab = ppc_hash_table (info);
12469 if (htab == NULL)
12470 return -1;
4c52953f 12471
70cc837d 12472 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12473 {
70cc837d
AM
12474 enum elf_ppc64_reloc_type r_type;
12475 unsigned long r_symndx;
12476 struct elf_link_hash_entry *h;
12477 struct ppc_link_hash_entry *eh;
12478 Elf_Internal_Sym *sym;
12479 asection *sym_sec;
12480 struct _opd_sec_data *opd;
12481 bfd_vma sym_value;
12482 bfd_vma dest;
12483
12484 r_type = ELF64_R_TYPE (rel->r_info);
12485 if (r_type != R_PPC64_REL24
05d0e962 12486 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12487 && r_type != R_PPC64_REL14
12488 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d
AM
12489 && r_type != R_PPC64_REL14_BRNTAKEN
12490 && r_type != R_PPC64_PLTCALL)
70cc837d 12491 continue;
4c52953f 12492
70cc837d
AM
12493 r_symndx = ELF64_R_SYM (rel->r_info);
12494 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12495 isec->owner))
4c52953f 12496 {
70cc837d
AM
12497 ret = -1;
12498 break;
12499 }
4c52953f 12500
70cc837d
AM
12501 /* Calls to dynamic lib functions go through a plt call stub
12502 that uses r2. */
12503 eh = (struct ppc_link_hash_entry *) h;
12504 if (eh != NULL
12505 && (eh->elf.plt.plist != NULL
12506 || (eh->oh != NULL
12507 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12508 {
12509 ret = 1;
12510 break;
4c52953f
AM
12511 }
12512
70cc837d
AM
12513 if (sym_sec == NULL)
12514 /* Ignore other undefined symbols. */
4c52953f 12515 continue;
4c52953f 12516
70cc837d
AM
12517 /* Assume branches to other sections not included in the
12518 link need stubs too, to cover -R and absolute syms. */
12519 if (sym_sec->output_section == NULL)
12520 {
12521 ret = 1;
12522 break;
12523 }
4c52953f 12524
70cc837d
AM
12525 if (h == NULL)
12526 sym_value = sym->st_value;
12527 else
12528 {
12529 if (h->root.type != bfd_link_hash_defined
12530 && h->root.type != bfd_link_hash_defweak)
12531 abort ();
12532 sym_value = h->root.u.def.value;
12533 }
12534 sym_value += rel->r_addend;
4c52953f 12535
70cc837d
AM
12536 /* If this branch reloc uses an opd sym, find the code section. */
12537 opd = get_opd_info (sym_sec);
12538 if (opd != NULL)
12539 {
12540 if (h == NULL && opd->adjust != NULL)
12541 {
12542 long adjust;
4c52953f 12543
92a9c616 12544 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12545 if (adjust == -1)
12546 /* Assume deleted functions won't ever be called. */
12547 continue;
12548 sym_value += adjust;
12549 }
4c52953f 12550
aef36ac1
AM
12551 dest = opd_entry_value (sym_sec, sym_value,
12552 &sym_sec, NULL, FALSE);
70cc837d
AM
12553 if (dest == (bfd_vma) -1)
12554 continue;
12555 }
12556 else
12557 dest = (sym_value
12558 + sym_sec->output_offset
12559 + sym_sec->output_section->vma);
4c52953f 12560
70cc837d
AM
12561 /* Ignore branch to self. */
12562 if (sym_sec == isec)
12563 continue;
4c52953f 12564
70cc837d
AM
12565 /* If the called function uses the toc, we need a stub. */
12566 if (sym_sec->has_toc_reloc
12567 || sym_sec->makes_toc_func_call)
4c52953f 12568 {
70cc837d 12569 ret = 1;
4c52953f
AM
12570 break;
12571 }
70cc837d
AM
12572
12573 /* Assume any branch that needs a long branch stub might in fact
12574 need a plt_branch stub. A plt_branch stub uses r2. */
12575 else if (dest - (isec->output_offset
12576 + isec->output_section->vma
6911b7dc
AM
12577 + rel->r_offset) + (1 << 25)
12578 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12579 ? h->other
12580 : sym->st_other))
4c52953f 12581 {
70cc837d
AM
12582 ret = 1;
12583 break;
12584 }
12585
12586 /* If calling back to a section in the process of being
12587 tested, we can't say for sure that no toc adjusting stubs
12588 are needed, so don't return zero. */
12589 else if (sym_sec->call_check_in_progress)
12590 ret = 2;
12591
12592 /* Branches to another section that itself doesn't have any TOC
12593 references are OK. Recursively call ourselves to check. */
12594 else if (!sym_sec->call_check_done)
12595 {
12596 int recur;
12597
12598 /* Mark current section as indeterminate, so that other
12599 sections that call back to current won't be marked as
12600 known. */
12601 isec->call_check_in_progress = 1;
12602 recur = toc_adjusting_stub_needed (info, sym_sec);
12603 isec->call_check_in_progress = 0;
12604
4c52953f
AM
12605 if (recur != 0)
12606 {
70cc837d
AM
12607 ret = recur;
12608 if (recur != 2)
12609 break;
4c52953f
AM
12610 }
12611 }
4c52953f 12612 }
70cc837d
AM
12613
12614 if (local_syms != NULL
12615 && (elf_symtab_hdr (isec->owner).contents
12616 != (unsigned char *) local_syms))
12617 free (local_syms);
12618 if (elf_section_data (isec)->relocs != relstart)
12619 free (relstart);
9b5ecbd0
AM
12620 }
12621
70cc837d
AM
12622 if ((ret & 1) == 0
12623 && isec->map_head.s != NULL
12624 && (strcmp (isec->output_section->name, ".init") == 0
12625 || strcmp (isec->output_section->name, ".fini") == 0))
12626 {
12627 if (isec->map_head.s->has_toc_reloc
12628 || isec->map_head.s->makes_toc_func_call)
12629 ret = 1;
12630 else if (!isec->map_head.s->call_check_done)
12631 {
12632 int recur;
12633 isec->call_check_in_progress = 1;
12634 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12635 isec->call_check_in_progress = 0;
12636 if (recur != 0)
12637 ret = recur;
12638 }
12639 }
12640
12641 if (ret == 1)
12642 isec->makes_toc_func_call = 1;
4c52953f 12643
9b5ecbd0
AM
12644 return ret;
12645}
12646
721956f4
AM
12647/* The linker repeatedly calls this function for each input section,
12648 in the order that input sections are linked into output sections.
12649 Build lists of input sections to determine groupings between which
12650 we may insert linker stubs. */
12651
9b5ecbd0 12652bfd_boolean
4ce794b7 12653ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12654{
12655 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12656
4dfe6ac6
NC
12657 if (htab == NULL)
12658 return FALSE;
12659
734b6cf9 12660 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12661 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12662 {
3d6f9012
AM
12663 /* This happens to make the list in reverse order,
12664 which is what we want. */
6f20ed8a
AM
12665 htab->sec_info[isec->id].u.list
12666 = htab->sec_info[isec->output_section->id].u.list;
12667 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12668 }
ad8e1ba5 12669
4c52953f 12670 if (htab->multi_toc_needed)
9b5ecbd0 12671 {
8b974ba3
AM
12672 /* Analyse sections that aren't already flagged as needing a
12673 valid toc pointer. Exclude .fixup for the linux kernel.
12674 .fixup contains branches, but only back to the function that
12675 hit an exception. */
12676 if (!(isec->has_toc_reloc
12677 || (isec->flags & SEC_CODE) == 0
12678 || strcmp (isec->name, ".fixup") == 0
12679 || isec->call_check_done))
12680 {
12681 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12682 return FALSE;
8b974ba3
AM
12683 }
12684 /* Make all sections use the TOC assigned for this object file.
12685 This will be wrong for pasted sections; We fix that in
12686 check_pasted_section(). */
12687 if (elf_gp (isec->owner) != 0)
12688 htab->toc_curr = elf_gp (isec->owner);
12689 }
12690
6f20ed8a 12691 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12692 return TRUE;
721956f4
AM
12693}
12694
70cc837d
AM
12695/* Check that all .init and .fini sections use the same toc, if they
12696 have toc relocs. */
12697
12698static bfd_boolean
12699check_pasted_section (struct bfd_link_info *info, const char *name)
12700{
12701 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12702
12703 if (o != NULL)
12704 {
12705 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12706 bfd_vma toc_off = 0;
12707 asection *i;
12708
12709 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12710 if (i->has_toc_reloc)
12711 {
12712 if (toc_off == 0)
6f20ed8a
AM
12713 toc_off = htab->sec_info[i->id].toc_off;
12714 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12715 return FALSE;
12716 }
6683a28d
AM
12717
12718 if (toc_off == 0)
12719 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12720 if (i->makes_toc_func_call)
12721 {
6f20ed8a 12722 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12723 break;
12724 }
12725
70cc837d
AM
12726 /* Make sure the whole pasted function uses the same toc offset. */
12727 if (toc_off != 0)
12728 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12729 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12730 }
12731 return TRUE;
12732}
12733
12734bfd_boolean
12735ppc64_elf_check_init_fini (struct bfd_link_info *info)
12736{
12737 return (check_pasted_section (info, ".init")
12738 & check_pasted_section (info, ".fini"));
12739}
12740
721956f4
AM
12741/* See whether we can group stub sections together. Grouping stub
12742 sections may result in fewer stubs. More importantly, we need to
12743 put all .init* and .fini* stubs at the beginning of the .init or
12744 .fini output sections respectively, because glibc splits the
12745 _init and _fini functions into multiple parts. Putting a stub in
12746 the middle of a function is not a good idea. */
12747
6f20ed8a
AM
12748static bfd_boolean
12749group_sections (struct bfd_link_info *info,
4ce794b7
AM
12750 bfd_size_type stub_group_size,
12751 bfd_boolean stubs_always_before_branch)
721956f4 12752{
6f20ed8a
AM
12753 struct ppc_link_hash_table *htab;
12754 asection *osec;
7c8fe5c4
AM
12755 bfd_boolean suppress_size_errors;
12756
6f20ed8a
AM
12757 htab = ppc_hash_table (info);
12758 if (htab == NULL)
12759 return FALSE;
12760
7c8fe5c4 12761 suppress_size_errors = FALSE;
7c8fe5c4
AM
12762 if (stub_group_size == 1)
12763 {
12764 /* Default values. */
12765 if (stubs_always_before_branch)
09f92717 12766 stub_group_size = 0x1e00000;
7c8fe5c4 12767 else
09f92717 12768 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12769 suppress_size_errors = TRUE;
12770 }
12771
6f20ed8a 12772 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12773 {
6f20ed8a
AM
12774 asection *tail;
12775
12776 if (osec->id >= htab->sec_info_arr_size)
12777 continue;
12778
12779 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12780 while (tail != NULL)
721956f4 12781 {
734b6cf9
AM
12782 asection *curr;
12783 asection *prev;
12784 bfd_size_type total;
12785 bfd_boolean big_sec;
12786 bfd_vma curr_toc;
6f20ed8a 12787 struct map_stub *group;
09f92717 12788 bfd_size_type group_size;
734b6cf9
AM
12789
12790 curr = tail;
eea6121a 12791 total = tail->size;
09f92717
AM
12792 group_size = (ppc64_elf_section_data (tail) != NULL
12793 && ppc64_elf_section_data (tail)->has_14bit_branch
12794 ? stub_group_size >> 10 : stub_group_size);
12795
12796 big_sec = total > group_size;
7c8fe5c4 12797 if (big_sec && !suppress_size_errors)
695344c0 12798 /* xgettext:c-format */
871b3ab2 12799 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12800 tail->owner, tail);
6f20ed8a 12801 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12802
6f20ed8a 12803 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12804 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12805 < (ppc64_elf_section_data (prev) != NULL
12806 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12807 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12808 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12809 curr = prev;
12810
12811 /* OK, the size from the start of CURR to the end is less
09f92717 12812 than group_size and thus can be handled by one stub
734b6cf9 12813 section. (or the tail section is itself larger than
09f92717
AM
12814 group_size, in which case we may be toast.) We should
12815 really be keeping track of the total size of stubs added
12816 here, as stubs contribute to the final output section
12817 size. That's a little tricky, and this way will only
12818 break if stubs added make the total size more than 2^25,
12819 ie. for the default stub_group_size, if stubs total more
12820 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12821 group = bfd_alloc (curr->owner, sizeof (*group));
12822 if (group == NULL)
12823 return FALSE;
12824 group->link_sec = curr;
12825 group->stub_sec = NULL;
a4b6fadd 12826 group->needs_save_res = 0;
d4aaa2a0 12827 group->tls_get_addr_opt_bctrl = -1u;
a4b6fadd
AM
12828 group->next = htab->group;
12829 htab->group = group;
734b6cf9 12830 do
721956f4 12831 {
6f20ed8a 12832 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12833 /* Set up this stub group. */
6f20ed8a 12834 htab->sec_info[tail->id].u.group = group;
721956f4 12835 }
734b6cf9
AM
12836 while (tail != curr && (tail = prev) != NULL);
12837
09f92717 12838 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12839 bytes before the stub section can be handled by it too.
12840 Don't do this if we have a really large section after the
12841 stubs, as adding more stubs increases the chance that
12842 branches may not reach into the stub section. */
12843 if (!stubs_always_before_branch && !big_sec)
12844 {
12845 total = 0;
12846 while (prev != NULL
12847 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12848 < (ppc64_elf_section_data (prev) != NULL
12849 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12850 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12851 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12852 {
12853 tail = prev;
6f20ed8a
AM
12854 prev = htab->sec_info[tail->id].u.list;
12855 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12856 }
12857 }
12858 tail = prev;
721956f4
AM
12859 }
12860 }
6f20ed8a 12861 return TRUE;
721956f4
AM
12862}
12863
58d180e8
AM
12864static const unsigned char glink_eh_frame_cie[] =
12865{
12866 0, 0, 0, 16, /* length. */
12867 0, 0, 0, 0, /* id. */
12868 1, /* CIE version. */
12869 'z', 'R', 0, /* Augmentation string. */
12870 4, /* Code alignment. */
12871 0x78, /* Data alignment. */
12872 65, /* RA reg. */
12873 1, /* Augmentation size. */
12874 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12875 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12876};
12877
d4aaa2a0
AM
12878static size_t
12879stub_eh_frame_size (struct map_stub *group, size_t align)
12880{
12881 size_t this_size = 17;
12882 if (group->tls_get_addr_opt_bctrl != -1u)
12883 {
12884 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12885 if (to_bctrl < 64)
12886 this_size += 1;
12887 else if (to_bctrl < 256)
12888 this_size += 2;
12889 else if (to_bctrl < 65536)
12890 this_size += 3;
12891 else
12892 this_size += 5;
12893 this_size += 6;
12894 }
12895 this_size = (this_size + align - 1) & -align;
12896 return this_size;
12897}
12898
d969d15f
AM
12899/* Stripping output sections is normally done before dynamic section
12900 symbols have been allocated. This function is called later, and
12901 handles cases like htab->brlt which is mapped to its own output
12902 section. */
12903
12904static void
12905maybe_strip_output (struct bfd_link_info *info, asection *isec)
12906{
12907 if (isec->size == 0
12908 && isec->output_section->size == 0
53d8967a 12909 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12910 && !bfd_section_removed_from_list (info->output_bfd,
12911 isec->output_section)
12912 && elf_section_data (isec->output_section)->dynindx == 0)
12913 {
12914 isec->output_section->flags |= SEC_EXCLUDE;
12915 bfd_section_list_remove (info->output_bfd, isec->output_section);
12916 info->output_bfd->section_count--;
12917 }
12918}
12919
721956f4
AM
12920/* Determine and set the size of the stub section for a final link.
12921
12922 The basic idea here is to examine all the relocations looking for
12923 PC-relative calls to a target that is unreachable with a "bl"
12924 instruction. */
12925
b34976b6 12926bfd_boolean
e7d1c40c 12927ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12928{
12929 bfd_size_type stub_group_size;
b34976b6 12930 bfd_boolean stubs_always_before_branch;
721956f4
AM
12931 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12932
4dfe6ac6
NC
12933 if (htab == NULL)
12934 return FALSE;
12935
0e1862bb 12936 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12937 htab->params->plt_thread_safe = 1;
b9e5796b 12938 if (!htab->opd_abi)
e7d1c40c
AM
12939 htab->params->plt_thread_safe = 0;
12940 else if (htab->params->plt_thread_safe == -1)
794e51c0 12941 {
e2458743 12942 static const char *const thread_starter[] =
794e51c0
AM
12943 {
12944 "pthread_create",
12945 /* libstdc++ */
12946 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12947 /* librt */
12948 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12949 "mq_notify", "create_timer",
12950 /* libanl */
12951 "getaddrinfo_a",
12952 /* libgomp */
2300b5a1 12953 "GOMP_parallel",
794e51c0 12954 "GOMP_parallel_start",
2300b5a1 12955 "GOMP_parallel_loop_static",
794e51c0 12956 "GOMP_parallel_loop_static_start",
2300b5a1 12957 "GOMP_parallel_loop_dynamic",
794e51c0 12958 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12959 "GOMP_parallel_loop_guided",
794e51c0 12960 "GOMP_parallel_loop_guided_start",
2300b5a1 12961 "GOMP_parallel_loop_runtime",
794e51c0 12962 "GOMP_parallel_loop_runtime_start",
2300b5a1 12963 "GOMP_parallel_sections",
68ffbac6 12964 "GOMP_parallel_sections_start",
f9dffbf0
AM
12965 /* libgo */
12966 "__go_go",
794e51c0
AM
12967 };
12968 unsigned i;
12969
a4b6fadd 12970 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12971 {
12972 struct elf_link_hash_entry *h;
12973 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12974 FALSE, FALSE, TRUE);
e7d1c40c
AM
12975 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12976 if (htab->params->plt_thread_safe)
794e51c0
AM
12977 break;
12978 }
12979 }
e7d1c40c
AM
12980 stubs_always_before_branch = htab->params->group_size < 0;
12981 if (htab->params->group_size < 0)
12982 stub_group_size = -htab->params->group_size;
721956f4 12983 else
e7d1c40c 12984 stub_group_size = htab->params->group_size;
721956f4 12985
6f20ed8a
AM
12986 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12987 return FALSE;
721956f4 12988
c9301e31
AM
12989#define STUB_SHRINK_ITER 20
12990 /* Loop until no stubs added. After iteration 20 of this loop we may
12991 exit on a stub section shrinking. This is to break out of a
12992 pathological case where adding stubs on one iteration decreases
12993 section gaps (perhaps due to alignment), which then requires
12994 fewer or smaller stubs on the next iteration. */
12995
721956f4
AM
12996 while (1)
12997 {
12998 bfd *input_bfd;
12999 unsigned int bfd_indx;
a4b6fadd 13000 struct map_stub *group;
721956f4
AM
13001
13002 htab->stub_iteration += 1;
721956f4
AM
13003
13004 for (input_bfd = info->input_bfds, bfd_indx = 0;
13005 input_bfd != NULL;
c72f2fb2 13006 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13007 {
13008 Elf_Internal_Shdr *symtab_hdr;
13009 asection *section;
6cdc0ccc 13010 Elf_Internal_Sym *local_syms = NULL;
721956f4 13011
0c8d6e5c 13012 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13013 continue;
13014
721956f4 13015 /* We'll need the symbol table in a second. */
0ffa91dd 13016 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13017 if (symtab_hdr->sh_info == 0)
13018 continue;
13019
721956f4
AM
13020 /* Walk over each section attached to the input bfd. */
13021 for (section = input_bfd->sections;
13022 section != NULL;
13023 section = section->next)
13024 {
721956f4 13025 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
13026
13027 /* If there aren't any relocs, then there's nothing more
13028 to do. */
13029 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13030 || (section->flags & SEC_ALLOC) == 0
13031 || (section->flags & SEC_LOAD) == 0
13032 || (section->flags & SEC_CODE) == 0
721956f4
AM
13033 || section->reloc_count == 0)
13034 continue;
13035
13036 /* If this section is a link-once section that will be
13037 discarded, then don't create any stubs. */
13038 if (section->output_section == NULL
927be08e 13039 || section->output_section->owner != info->output_bfd)
721956f4
AM
13040 continue;
13041
1e2f5b6e
AM
13042 /* Get the relocs. */
13043 internal_relocs
4ce794b7 13044 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13045 info->keep_memory);
721956f4 13046 if (internal_relocs == NULL)
1e2f5b6e 13047 goto error_ret_free_local;
721956f4
AM
13048
13049 /* Now examine each relocation. */
13050 irela = internal_relocs;
13051 irelaend = irela + section->reloc_count;
13052 for (; irela < irelaend; irela++)
13053 {
4ce794b7
AM
13054 enum elf_ppc64_reloc_type r_type;
13055 unsigned int r_indx;
721956f4
AM
13056 enum ppc_stub_type stub_type;
13057 struct ppc_stub_hash_entry *stub_entry;
8387904d 13058 asection *sym_sec, *code_sec;
e054468f 13059 bfd_vma sym_value, code_value;
721956f4 13060 bfd_vma destination;
6911b7dc 13061 unsigned long local_off;
8843416a 13062 bfd_boolean ok_dest;
721956f4 13063 struct ppc_link_hash_entry *hash;
8387904d 13064 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13065 struct elf_link_hash_entry *h;
13066 Elf_Internal_Sym *sym;
721956f4
AM
13067 char *stub_name;
13068 const asection *id_sec;
74f0fb50 13069 struct _opd_sec_data *opd;
e054468f 13070 struct plt_entry *plt_ent;
721956f4
AM
13071
13072 r_type = ELF64_R_TYPE (irela->r_info);
13073 r_indx = ELF64_R_SYM (irela->r_info);
13074
4ce794b7 13075 if (r_type >= R_PPC64_max)
721956f4
AM
13076 {
13077 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13078 goto error_ret_free_internal;
721956f4
AM
13079 }
13080
13081 /* Only look for stubs on branch instructions. */
4ce794b7 13082 if (r_type != R_PPC64_REL24
05d0e962 13083 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
13084 && r_type != R_PPC64_REL14
13085 && r_type != R_PPC64_REL14_BRTAKEN
13086 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
13087 continue;
13088
13089 /* Now determine the call target, its name, value,
13090 section. */
411e1bfb
AM
13091 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13092 r_indx, input_bfd))
13093 goto error_ret_free_internal;
13094 hash = (struct ppc_link_hash_entry *) h;
13095
8843416a 13096 ok_dest = FALSE;
8387904d 13097 fdh = NULL;
7fe2b9a6 13098 sym_value = 0;
411e1bfb 13099 if (hash == NULL)
721956f4 13100 {
411e1bfb 13101 sym_value = sym->st_value;
c27b8c2a
AM
13102 if (sym_sec != NULL
13103 && sym_sec->output_section != NULL)
13104 ok_dest = TRUE;
721956f4 13105 }
7fe2b9a6
AM
13106 else if (hash->elf.root.type == bfd_link_hash_defined
13107 || hash->elf.root.type == bfd_link_hash_defweak)
13108 {
13109 sym_value = hash->elf.root.u.def.value;
13110 if (sym_sec->output_section != NULL)
13111 ok_dest = TRUE;
13112 }
13113 else if (hash->elf.root.type == bfd_link_hash_undefweak
13114 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13115 {
99877b66 13116 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13117 use the func descriptor sym instead if it is
13118 defined. */
ceb1f1ef 13119 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13120 && hash->oh != NULL)
8387904d 13121 {
8c5b4e52 13122 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13123 if (fdh->elf.root.type == bfd_link_hash_defined
13124 || fdh->elf.root.type == bfd_link_hash_defweak)
13125 {
13126 sym_sec = fdh->elf.root.u.def.section;
13127 sym_value = fdh->elf.root.u.def.value;
13128 if (sym_sec->output_section != NULL)
13129 ok_dest = TRUE;
13130 }
99877b66
AM
13131 else
13132 fdh = NULL;
8387904d 13133 }
7fe2b9a6
AM
13134 }
13135 else
13136 {
13137 bfd_set_error (bfd_error_bad_value);
13138 goto error_ret_free_internal;
721956f4
AM
13139 }
13140
8843416a 13141 destination = 0;
6911b7dc 13142 local_off = 0;
8843416a
AM
13143 if (ok_dest)
13144 {
13145 sym_value += irela->r_addend;
13146 destination = (sym_value
13147 + sym_sec->output_offset
13148 + sym_sec->output_section->vma);
6911b7dc
AM
13149 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13150 ? hash->elf.other
13151 : sym->st_other);
8843416a
AM
13152 }
13153
8387904d 13154 code_sec = sym_sec;
e054468f 13155 code_value = sym_value;
74f0fb50
AM
13156 opd = get_opd_info (sym_sec);
13157 if (opd != NULL)
8387904d
AM
13158 {
13159 bfd_vma dest;
13160
74f0fb50 13161 if (hash == NULL && opd->adjust != NULL)
8387904d 13162 {
51aecdc5 13163 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13164 if (adjust == -1)
13165 continue;
e054468f 13166 code_value += adjust;
8387904d
AM
13167 sym_value += adjust;
13168 }
13169 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 13170 &code_sec, &code_value, FALSE);
8387904d
AM
13171 if (dest != (bfd_vma) -1)
13172 {
13173 destination = dest;
13174 if (fdh != NULL)
13175 {
13176 /* Fixup old ABI sym to point at code
13177 entry. */
99877b66 13178 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13179 hash->elf.root.u.def.section = code_sec;
e054468f 13180 hash->elf.root.u.def.value = code_value;
8387904d
AM
13181 }
13182 }
13183 }
13184
721956f4 13185 /* Determine what (if any) linker stub is needed. */
e054468f 13186 plt_ent = NULL;
721956f4 13187 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13188 &plt_ent, destination,
13189 local_off);
ad8e1ba5 13190
05d0e962
AM
13191 if (r_type == R_PPC64_REL24_NOTOC)
13192 {
13193 if (stub_type == ppc_stub_plt_call)
13194 stub_type = ppc_stub_plt_call_notoc;
13195 else if (stub_type == ppc_stub_long_branch
13196 || (code_sec != NULL
13197 && code_sec->output_section != NULL
13198 && (((hash ? hash->elf.other : sym->st_other)
13199 & STO_PPC64_LOCAL_MASK)
13200 != 1 << STO_PPC64_LOCAL_BIT)))
13201 stub_type = ppc_stub_long_branch_notoc;
13202 }
13203 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13204 {
13205 /* Check whether we need a TOC adjusting stub.
13206 Since the linker pastes together pieces from
13207 different object files when creating the
13208 _init and _fini functions, it may be that a
13209 call to what looks like a local sym is in
13210 fact a call needing a TOC adjustment. */
33cb30a1
AM
13211 if ((code_sec != NULL
13212 && code_sec->output_section != NULL
13213 && (htab->sec_info[code_sec->id].toc_off
13214 != htab->sec_info[section->id].toc_off)
13215 && (code_sec->has_toc_reloc
13216 || code_sec->makes_toc_func_call))
13217 || (((hash ? hash->elf.other : sym->st_other)
13218 & STO_PPC64_LOCAL_MASK)
13219 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13220 stub_type = ppc_stub_long_branch_r2off;
13221 }
13222
721956f4
AM
13223 if (stub_type == ppc_stub_none)
13224 continue;
13225
411e1bfb
AM
13226 /* __tls_get_addr calls might be eliminated. */
13227 if (stub_type != ppc_stub_plt_call
05d0e962 13228 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13229 && hash != NULL
8387904d
AM
13230 && (hash == htab->tls_get_addr
13231 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
13232 && section->has_tls_reloc
13233 && irela != internal_relocs)
13234 {
13235 /* Get tls info. */
f961d9dd 13236 unsigned char *tls_mask;
411e1bfb 13237
3a71aa26 13238 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13239 irela - 1, input_bfd))
13240 goto error_ret_free_internal;
37da22e5 13241 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
13242 continue;
13243 }
13244
f378ab09 13245 if (stub_type == ppc_stub_plt_call)
794e51c0 13246 {
6e1816be
AM
13247 if (!htab->opd_abi
13248 && htab->params->plt_localentry0 != 0
13249 && is_elfv2_localentry0 (&hash->elf))
13250 htab->has_plt_localentry0 = 1;
13251 else if (irela + 1 < irelaend
13252 && irela[1].r_offset == irela->r_offset + 4
13253 && (ELF64_R_TYPE (irela[1].r_info)
13254 == R_PPC64_TOCSAVE))
f378ab09
AM
13255 {
13256 if (!tocsave_find (htab, INSERT,
13257 &local_syms, irela + 1, input_bfd))
13258 goto error_ret_free_internal;
13259 }
f378ab09
AM
13260 else
13261 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13262 }
3b421ab3 13263
721956f4 13264 /* Support for grouping stub sections. */
6f20ed8a 13265 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13266
13267 /* Get the name of this stub. */
13268 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13269 if (!stub_name)
13270 goto error_ret_free_internal;
13271
13272 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13273 stub_name, FALSE, FALSE);
721956f4
AM
13274 if (stub_entry != NULL)
13275 {
05d0e962
AM
13276 enum ppc_stub_type old_type;
13277 /* A stub has already been created, but it may
13278 not be the required type. We shouldn't be
13279 transitioning from plt_call to long_branch
13280 stubs or vice versa, but we might be
13281 upgrading from plt_call to plt_call_r2save or
13282 from long_branch to long_branch_r2off. */
721956f4 13283 free (stub_name);
05d0e962
AM
13284 old_type = stub_entry->stub_type;
13285 switch (old_type)
13286 {
13287 default:
13288 abort ();
13289
13290 case ppc_stub_save_res:
13291 continue;
13292
13293 case ppc_stub_plt_call:
13294 case ppc_stub_plt_call_r2save:
13295 case ppc_stub_plt_call_notoc:
13296 case ppc_stub_plt_call_both:
13297 if (stub_type == ppc_stub_plt_call)
13298 continue;
13299 else if (stub_type == ppc_stub_plt_call_r2save)
13300 {
13301 if (old_type == ppc_stub_plt_call_notoc)
13302 stub_type = ppc_stub_plt_call_both;
13303 }
13304 else if (stub_type == ppc_stub_plt_call_notoc)
13305 {
13306 if (old_type == ppc_stub_plt_call_r2save)
13307 stub_type = ppc_stub_plt_call_both;
13308 }
13309 else
13310 abort ();
13311 break;
13312
13313 case ppc_stub_plt_branch:
13314 case ppc_stub_plt_branch_r2off:
13315 case ppc_stub_plt_branch_notoc:
13316 case ppc_stub_plt_branch_both:
13317 old_type += (ppc_stub_long_branch
13318 - ppc_stub_plt_branch);
13319 /* Fall through. */
13320 case ppc_stub_long_branch:
13321 case ppc_stub_long_branch_r2off:
13322 case ppc_stub_long_branch_notoc:
13323 case ppc_stub_long_branch_both:
13324 if (stub_type == ppc_stub_long_branch)
13325 continue;
13326 else if (stub_type == ppc_stub_long_branch_r2off)
13327 {
13328 if (old_type == ppc_stub_long_branch_notoc)
13329 stub_type = ppc_stub_long_branch_both;
13330 }
13331 else if (stub_type == ppc_stub_long_branch_notoc)
13332 {
13333 if (old_type == ppc_stub_long_branch_r2off)
13334 stub_type = ppc_stub_long_branch_both;
13335 }
13336 else
13337 abort ();
13338 break;
13339 }
13340 if (old_type < stub_type)
794e51c0 13341 stub_entry->stub_type = stub_type;
721956f4
AM
13342 continue;
13343 }
13344
25f53a85 13345 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13346 if (stub_entry == NULL)
13347 {
13348 free (stub_name);
6cdc0ccc
AM
13349 error_ret_free_internal:
13350 if (elf_section_data (section)->relocs == NULL)
13351 free (internal_relocs);
13352 error_ret_free_local:
13353 if (local_syms != NULL
13354 && (symtab_hdr->contents
13355 != (unsigned char *) local_syms))
13356 free (local_syms);
b34976b6 13357 return FALSE;
721956f4
AM
13358 }
13359
ad8e1ba5 13360 stub_entry->stub_type = stub_type;
05d0e962
AM
13361 if (stub_type >= ppc_stub_plt_call
13362 && stub_type <= ppc_stub_plt_call_both)
e054468f 13363 {
05d0e962
AM
13364 stub_entry->target_value = sym_value;
13365 stub_entry->target_section = sym_sec;
e054468f
AM
13366 }
13367 else
13368 {
05d0e962
AM
13369 stub_entry->target_value = code_value;
13370 stub_entry->target_section = code_sec;
e054468f 13371 }
721956f4 13372 stub_entry->h = hash;
e054468f 13373 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13374 stub_entry->symtype
13375 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13376 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
13377
13378 if (stub_entry->h != NULL)
13379 htab->stub_globals += 1;
721956f4
AM
13380 }
13381
13382 /* We're done with the internal relocs, free them. */
6cdc0ccc 13383 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13384 free (internal_relocs);
721956f4 13385 }
6cdc0ccc
AM
13386
13387 if (local_syms != NULL
13388 && symtab_hdr->contents != (unsigned char *) local_syms)
13389 {
13390 if (!info->keep_memory)
13391 free (local_syms);
13392 else
13393 symtab_hdr->contents = (unsigned char *) local_syms;
13394 }
721956f4
AM
13395 }
13396
5c3dead3 13397 /* We may have added some stubs. Find out the new size of the
721956f4 13398 stub sections. */
d4aaa2a0
AM
13399 for (group = htab->group; group != NULL; group = group->next)
13400 if (group->stub_sec != NULL)
ee75fd95 13401 {
d4aaa2a0
AM
13402 asection *stub_sec = group->stub_sec;
13403
ea3d7d1c
AM
13404 if (htab->stub_iteration <= STUB_SHRINK_ITER
13405 || stub_sec->rawsize < stub_sec->size)
13406 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13407 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
13408 stub_sec->size = 0;
13409 stub_sec->reloc_count = 0;
84f5d08e 13410 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 13411 }
eea6121a 13412
ba21f564
AM
13413 if (htab->stub_iteration <= STUB_SHRINK_ITER
13414 || htab->brlt->rawsize < htab->brlt->size)
13415 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13416 htab->brlt->size = 0;
84f5d08e
AM
13417 htab->brlt->reloc_count = 0;
13418 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13419 if (htab->relbrlt != NULL)
eea6121a 13420 htab->relbrlt->size = 0;
721956f4 13421
63bc6f6c 13422 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13423
a4b6fadd
AM
13424 for (group = htab->group; group != NULL; group = group->next)
13425 if (group->needs_save_res)
13426 group->stub_sec->size += htab->sfpr->size;
13427
176a0d42
AM
13428 if (info->emitrelocations
13429 && htab->glink != NULL && htab->glink->size != 0)
13430 {
13431 htab->glink->reloc_count = 1;
13432 htab->glink->flags |= SEC_RELOC;
13433 }
13434
58d180e8
AM
13435 if (htab->glink_eh_frame != NULL
13436 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13437 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13438 {
2e0ce1c8 13439 size_t size = 0, align = 4;
58d180e8 13440
d4aaa2a0
AM
13441 for (group = htab->group; group != NULL; group = group->next)
13442 if (group->stub_sec != NULL)
13443 size += stub_eh_frame_size (group, align);
58d180e8 13444 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13445 size += (24 + align - 1) & -align;
58d180e8 13446 if (size != 0)
2e0ce1c8
AM
13447 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13448 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13449 size = (size + align - 1) & -align;
58d180e8
AM
13450 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13451 htab->glink_eh_frame->size = size;
13452 }
13453
e7d1c40c 13454 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13455 for (group = htab->group; group != NULL; group = group->next)
13456 if (group->stub_sec != NULL)
691d2e9a
AM
13457 {
13458 int align = abs (htab->params->plt_stub_align);
13459 group->stub_sec->size
13460 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13461 }
d4aaa2a0
AM
13462
13463 for (group = htab->group; group != NULL; group = group->next)
13464 if (group->stub_sec != NULL
13465 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13466 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13467 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13468 break;
13469
d4aaa2a0 13470 if (group == NULL
ba21f564
AM
13471 && (htab->brlt->rawsize == htab->brlt->size
13472 || (htab->stub_iteration > STUB_SHRINK_ITER
13473 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13474 && (htab->glink_eh_frame == NULL
13475 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13476 break;
13477
721956f4 13478 /* Ask the linker to do its stuff. */
e7d1c40c 13479 (*htab->params->layout_sections_again) ();
721956f4
AM
13480 }
13481
da44f4e5
AM
13482 if (htab->glink_eh_frame != NULL
13483 && htab->glink_eh_frame->size != 0)
13484 {
13485 bfd_vma val;
13486 bfd_byte *p, *last_fde;
13487 size_t last_fde_len, size, align, pad;
d4aaa2a0 13488 struct map_stub *group;
da44f4e5
AM
13489
13490 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13491 if (p == NULL)
13492 return FALSE;
13493 htab->glink_eh_frame->contents = p;
13494 last_fde = p;
2e0ce1c8 13495 align = 4;
da44f4e5
AM
13496
13497 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13498 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13499 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13500 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13501 p += last_fde_len + 4;
da44f4e5 13502
d4aaa2a0
AM
13503 for (group = htab->group; group != NULL; group = group->next)
13504 if (group->stub_sec != NULL)
da44f4e5
AM
13505 {
13506 last_fde = p;
d4aaa2a0 13507 last_fde_len = stub_eh_frame_size (group, align) - 4;
da44f4e5 13508 /* FDE length. */
2e0ce1c8 13509 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13510 p += 4;
13511 /* CIE pointer. */
13512 val = p - htab->glink_eh_frame->contents;
13513 bfd_put_32 (htab->elf.dynobj, val, p);
13514 p += 4;
13515 /* Offset to stub section, written later. */
13516 p += 4;
13517 /* stub section size. */
d4aaa2a0 13518 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13519 p += 4;
13520 /* Augmentation. */
13521 p += 1;
d4aaa2a0
AM
13522 if (group->tls_get_addr_opt_bctrl != -1u)
13523 {
13524 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13525
13526 /* This FDE needs more than just the default.
13527 Describe __tls_get_addr_opt stub LR. */
13528 if (to_bctrl < 64)
13529 *p++ = DW_CFA_advance_loc + to_bctrl;
13530 else if (to_bctrl < 256)
13531 {
13532 *p++ = DW_CFA_advance_loc1;
13533 *p++ = to_bctrl;
13534 }
13535 else if (to_bctrl < 65536)
13536 {
13537 *p++ = DW_CFA_advance_loc2;
13538 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13539 p += 2;
13540 }
13541 else
13542 {
13543 *p++ = DW_CFA_advance_loc4;
13544 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13545 p += 4;
13546 }
13547 *p++ = DW_CFA_offset_extended_sf;
13548 *p++ = 65;
13549 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13550 *p++ = DW_CFA_advance_loc + 4;
13551 *p++ = DW_CFA_restore_extended;
13552 *p++ = 65;
13553 }
da44f4e5 13554 /* Pad. */
d4aaa2a0 13555 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13556 }
13557 if (htab->glink != NULL && htab->glink->size != 0)
13558 {
13559 last_fde = p;
2e0ce1c8 13560 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13561 /* FDE length. */
2e0ce1c8 13562 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13563 p += 4;
13564 /* CIE pointer. */
13565 val = p - htab->glink_eh_frame->contents;
13566 bfd_put_32 (htab->elf.dynobj, val, p);
13567 p += 4;
13568 /* Offset to .glink, written later. */
13569 p += 4;
13570 /* .glink size. */
13571 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13572 p += 4;
13573 /* Augmentation. */
13574 p += 1;
13575
13576 *p++ = DW_CFA_advance_loc + 1;
13577 *p++ = DW_CFA_register;
13578 *p++ = 65;
9f08fa5c 13579 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13580 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13581 *p++ = DW_CFA_restore_extended;
13582 *p++ = 65;
2e0ce1c8 13583 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13584 }
13585 /* Subsume any padding into the last FDE if user .eh_frame
13586 sections are aligned more than glink_eh_frame. Otherwise any
13587 zero padding will be seen as a terminator. */
2e0ce1c8 13588 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13589 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13590 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13591 htab->glink_eh_frame->size = size + pad;
13592 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13593 }
13594
d969d15f
AM
13595 maybe_strip_output (info, htab->brlt);
13596 if (htab->glink_eh_frame != NULL)
13597 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13598
b34976b6 13599 return TRUE;
721956f4
AM
13600}
13601
13602/* Called after we have determined section placement. If sections
805fc799 13603 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13604
805fc799 13605bfd_vma
1c865ab2 13606ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13607{
805fc799 13608 asection *s;
a27e685f 13609 bfd_vma TOCstart, adjust;
721956f4 13610
43417696
AM
13611 if (info != NULL)
13612 {
13613 struct elf_link_hash_entry *h;
13614 struct elf_link_hash_table *htab = elf_hash_table (info);
13615
13616 if (is_elf_hash_table (htab)
13617 && htab->hgot != NULL)
13618 h = htab->hgot;
13619 else
13620 {
13621 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13622 if (is_elf_hash_table (htab))
13623 htab->hgot = h;
13624 }
13625 if (h != NULL
13626 && h->root.type == bfd_link_hash_defined
13627 && !h->root.linker_def
13628 && (!is_elf_hash_table (htab)
13629 || h->def_regular))
13630 {
13631 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13632 + h->root.u.def.section->output_offset
13633 + h->root.u.def.section->output_section->vma);
13634 _bfd_set_gp_value (obfd, TOCstart);
13635 return TOCstart;
13636 }
13637 }
13638
805fc799
AM
13639 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13640 order. The TOC starts where the first of these sections starts. */
13641 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13642 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13643 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13644 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13645 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13646 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13647 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13648 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13649 {
13650 /* This may happen for
13651 o references to TOC base (SYM@toc / TOC[tc0]) without a
13652 .toc directive
13653 o bad linker script
13654 o --gc-sections and empty TOC sections
13655
13656 FIXME: Warn user? */
13657
13658 /* Look for a likely section. We probably won't even be
13659 using TOCstart. */
13660 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13661 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13662 | SEC_EXCLUDE))
805fc799
AM
13663 == (SEC_ALLOC | SEC_SMALL_DATA))
13664 break;
721956f4 13665 if (s == NULL)
805fc799 13666 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13667 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13668 == (SEC_ALLOC | SEC_SMALL_DATA))
13669 break;
721956f4 13670 if (s == NULL)
805fc799 13671 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13672 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13673 == SEC_ALLOC)
805fc799 13674 break;
721956f4 13675 if (s == NULL)
805fc799 13676 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13677 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13678 break;
13679 }
721956f4 13680
805fc799
AM
13681 TOCstart = 0;
13682 if (s != NULL)
13683 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13684
a27e685f
AM
13685 /* Force alignment. */
13686 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13687 TOCstart -= adjust;
1c865ab2
AM
13688 _bfd_set_gp_value (obfd, TOCstart);
13689
810d4e75 13690 if (info != NULL && s != NULL)
1c865ab2
AM
13691 {
13692 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13693
810d4e75
AM
13694 if (htab != NULL)
13695 {
13696 if (htab->elf.hgot != NULL)
13697 {
a27e685f 13698 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13699 htab->elf.hgot->root.u.def.section = s;
13700 }
13701 }
13702 else
1c865ab2 13703 {
810d4e75
AM
13704 struct bfd_link_hash_entry *bh = NULL;
13705 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13706 s, TOC_BASE_OFF - adjust,
13707 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13708 }
13709 }
805fc799 13710 return TOCstart;
721956f4
AM
13711}
13712
a345bc8d 13713/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13714 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13715
13716static bfd_boolean
49c09209 13717build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13718{
13719 struct bfd_link_info *info;
13720 struct ppc_link_hash_table *htab;
49c09209 13721 struct plt_entry *ent;
a345bc8d
AM
13722 asection *s;
13723
13724 if (h->root.type == bfd_link_hash_indirect)
13725 return TRUE;
13726
49c09209
AM
13727 info = inf;
13728 htab = ppc_hash_table (info);
13729 if (htab == NULL)
13730 return FALSE;
13731
13732 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13733 if (ent->plt.offset != (bfd_vma) -1)
13734 {
13735 /* This symbol has an entry in the procedure linkage
13736 table. Set it up. */
13737 Elf_Internal_Rela rela;
2d7ad24e 13738 asection *plt, *relplt;
49c09209
AM
13739 bfd_byte *loc;
13740
13741 if (!htab->elf.dynamic_sections_created
13742 || h->dynindx == -1)
13743 {
13744 if (!(h->def_regular
13745 && (h->root.type == bfd_link_hash_defined
13746 || h->root.type == bfd_link_hash_defweak)))
13747 continue;
2d7ad24e
AM
13748 if (h->type == STT_GNU_IFUNC)
13749 {
13750 plt = htab->elf.iplt;
13751 relplt = htab->elf.irelplt;
13752 htab->local_ifunc_resolver = 1;
13753 if (htab->opd_abi)
13754 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13755 else
13756 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13757 }
49c09209 13758 else
2d7ad24e
AM
13759 {
13760 plt = htab->pltlocal;
13761 if (bfd_link_pic (info))
13762 {
13763 relplt = htab->relpltlocal;
13764 if (htab->opd_abi)
13765 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13766 else
13767 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13768 }
13769 else
13770 relplt = NULL;
13771 }
49c09209
AM
13772 rela.r_addend = (h->root.u.def.value
13773 + h->root.u.def.section->output_offset
13774 + h->root.u.def.section->output_section->vma
13775 + ent->addend);
2d7ad24e
AM
13776
13777 if (relplt == NULL)
13778 {
13779 loc = plt->contents + ent->plt.offset;
13780 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13781 if (htab->opd_abi)
13782 {
13783 bfd_vma toc = elf_gp (info->output_bfd);
13784 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13785 bfd_put_64 (info->output_bfd, toc, loc + 8);
13786 }
13787 }
13788 else
13789 {
13790 rela.r_offset = (plt->output_section->vma
13791 + plt->output_offset
13792 + ent->plt.offset);
13793 loc = relplt->contents + (relplt->reloc_count++
13794 * sizeof (Elf64_External_Rela));
13795 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13796 }
49c09209
AM
13797 }
13798 else
13799 {
13800 rela.r_offset = (htab->elf.splt->output_section->vma
13801 + htab->elf.splt->output_offset
13802 + ent->plt.offset);
13803 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13804 rela.r_addend = ent->addend;
13805 loc = (htab->elf.srelplt->contents
13806 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13807 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13808 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13809 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13810 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13811 }
49c09209
AM
13812 }
13813
a345bc8d
AM
13814 if (!h->pointer_equality_needed)
13815 return TRUE;
13816
13817 if (h->def_regular)
13818 return TRUE;
13819
9e390558 13820 s = htab->global_entry;
49c09209
AM
13821 if (s == NULL || s->size == 0)
13822 return TRUE;
13823
13824 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13825 if (ent->plt.offset != (bfd_vma) -1
13826 && ent->addend == 0)
a345bc8d
AM
13827 {
13828 bfd_byte *p;
13829 asection *plt;
13830 bfd_vma off;
13831
a345bc8d 13832 p = s->contents + h->root.u.def.value;
33e44f2e 13833 plt = htab->elf.splt;
a345bc8d
AM
13834 if (!htab->elf.dynamic_sections_created
13835 || h->dynindx == -1)
2d7ad24e
AM
13836 {
13837 if (h->type == STT_GNU_IFUNC)
13838 plt = htab->elf.iplt;
13839 else
13840 plt = htab->pltlocal;
13841 }
49c09209 13842 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13843 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13844
13845 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13846 {
13847 info->callbacks->einfo
c1c8c1ef 13848 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13849 h->root.root.string);
13850 bfd_set_error (bfd_error_bad_value);
13851 htab->stub_error = TRUE;
13852 }
13853
7341d5e2
AM
13854 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13855 if (htab->params->emit_stub_syms)
13856 {
13857 size_t len = strlen (h->root.root.string);
13858 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13859
13860 if (name == NULL)
13861 return FALSE;
13862
13863 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13864 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13865 if (h == NULL)
13866 return FALSE;
13867 if (h->root.type == bfd_link_hash_new)
13868 {
13869 h->root.type = bfd_link_hash_defined;
13870 h->root.u.def.section = s;
13871 h->root.u.def.value = p - s->contents;
13872 h->ref_regular = 1;
13873 h->def_regular = 1;
13874 h->ref_regular_nonweak = 1;
13875 h->forced_local = 1;
13876 h->non_elf = 0;
2ec55de3 13877 h->root.linker_def = 1;
7341d5e2
AM
13878 }
13879 }
13880
a345bc8d
AM
13881 if (PPC_HA (off) != 0)
13882 {
13883 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13884 p += 4;
13885 }
13886 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13887 p += 4;
13888 bfd_put_32 (s->owner, MTCTR_R12, p);
13889 p += 4;
407aa07c 13890 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13891 break;
13892 }
13893 return TRUE;
13894}
13895
49c09209
AM
13896/* Write PLT relocs for locals. */
13897
13898static bfd_boolean
13899write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13900{
13901 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13902 bfd *ibfd;
13903
13904 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13905 {
13906 struct got_entry **lgot_ents, **end_lgot_ents;
13907 struct plt_entry **local_plt, **lplt, **end_local_plt;
13908 Elf_Internal_Shdr *symtab_hdr;
13909 bfd_size_type locsymcount;
13910 Elf_Internal_Sym *local_syms = NULL;
13911 struct plt_entry *ent;
13912
13913 if (!is_ppc64_elf (ibfd))
13914 continue;
13915
13916 lgot_ents = elf_local_got_ents (ibfd);
13917 if (!lgot_ents)
13918 continue;
13919
13920 symtab_hdr = &elf_symtab_hdr (ibfd);
13921 locsymcount = symtab_hdr->sh_info;
13922 end_lgot_ents = lgot_ents + locsymcount;
13923 local_plt = (struct plt_entry **) end_lgot_ents;
13924 end_local_plt = local_plt + locsymcount;
13925 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13926 for (ent = *lplt; ent != NULL; ent = ent->next)
13927 if (ent->plt.offset != (bfd_vma) -1)
13928 {
13929 Elf_Internal_Sym *sym;
13930 asection *sym_sec;
13931 asection *plt, *relplt;
13932 bfd_byte *loc;
13933 bfd_vma val;
49c09209
AM
13934
13935 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13936 lplt - local_plt, ibfd))
13937 {
13938 if (local_syms != NULL
13939 && symtab_hdr->contents != (unsigned char *) local_syms)
13940 free (local_syms);
13941 return FALSE;
13942 }
13943
13944 val = sym->st_value + ent->addend;
13945 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13946 if (sym_sec != NULL && sym_sec->output_section != NULL)
13947 val += sym_sec->output_offset + sym_sec->output_section->vma;
13948
2d7ad24e
AM
13949 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13950 {
13951 htab->local_ifunc_resolver = 1;
13952 plt = htab->elf.iplt;
13953 relplt = htab->elf.irelplt;
13954 }
13955 else
13956 {
13957 plt = htab->pltlocal;
13958 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13959 }
49c09209 13960
2d7ad24e
AM
13961 if (relplt == NULL)
13962 {
13963 loc = plt->contents + ent->plt.offset;
13964 bfd_put_64 (info->output_bfd, val, loc);
13965 if (htab->opd_abi)
13966 {
13967 bfd_vma toc = elf_gp (ibfd);
13968 bfd_put_64 (info->output_bfd, toc, loc + 8);
13969 }
13970 }
49c09209 13971 else
2d7ad24e
AM
13972 {
13973 Elf_Internal_Rela rela;
13974 rela.r_offset = (ent->plt.offset
13975 + plt->output_offset
13976 + plt->output_section->vma);
13977 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13978 {
13979 if (htab->opd_abi)
13980 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13981 else
13982 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13983 }
13984 else
13985 {
13986 if (htab->opd_abi)
13987 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13988 else
13989 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13990 }
13991 rela.r_addend = val;
13992 loc = relplt->contents + (relplt->reloc_count++
13993 * sizeof (Elf64_External_Rela));
13994 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13995 }
49c09209
AM
13996 }
13997
13998 if (local_syms != NULL
13999 && symtab_hdr->contents != (unsigned char *) local_syms)
14000 {
14001 if (!info->keep_memory)
14002 free (local_syms);
14003 else
14004 symtab_hdr->contents = (unsigned char *) local_syms;
14005 }
14006 }
14007 return TRUE;
14008}
14009
721956f4
AM
14010/* Build all the stubs associated with the current output file.
14011 The stubs are kept in a hash table attached to the main linker
14012 hash table. This function is called via gldelf64ppc_finish. */
14013
b34976b6 14014bfd_boolean
e7d1c40c 14015ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14016 char **stats)
5d1634d7
AM
14017{
14018 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14019 struct map_stub *group;
721956f4 14020 asection *stub_sec;
5d1634d7 14021 bfd_byte *p;
e717da7e 14022 int stub_sec_count = 0;
5d1634d7 14023
4dfe6ac6
NC
14024 if (htab == NULL)
14025 return FALSE;
14026
eea6121a 14027 /* Allocate memory to hold the linker stubs. */
d4aaa2a0
AM
14028 for (group = htab->group; group != NULL; group = group->next)
14029 if ((stub_sec = group->stub_sec) != NULL
eea6121a 14030 && stub_sec->size != 0)
e717da7e 14031 {
e7d1c40c 14032 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
14033 if (stub_sec->contents == NULL)
14034 return FALSE;
eea6121a 14035 stub_sec->size = 0;
e717da7e 14036 }
5d1634d7 14037
23eb7e01 14038 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14039 {
9f951329 14040 unsigned int indx;
ad8e1ba5 14041 bfd_vma plt0;
9f951329 14042
721956f4 14043 /* Build the .glink plt call stub. */
e7d1c40c 14044 if (htab->params->emit_stub_syms)
97b639ba
AM
14045 {
14046 struct elf_link_hash_entry *h;
468392fb
AM
14047 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14048 TRUE, FALSE, FALSE);
97b639ba
AM
14049 if (h == NULL)
14050 return FALSE;
14051 if (h->root.type == bfd_link_hash_new)
14052 {
14053 h->root.type = bfd_link_hash_defined;
14054 h->root.u.def.section = htab->glink;
ee4bf8d2 14055 h->root.u.def.value = 8;
f5385ebf
AM
14056 h->ref_regular = 1;
14057 h->def_regular = 1;
14058 h->ref_regular_nonweak = 1;
14059 h->forced_local = 1;
14060 h->non_elf = 0;
2ec55de3 14061 h->root.linker_def = 1;
97b639ba
AM
14062 }
14063 }
33e44f2e
AM
14064 plt0 = (htab->elf.splt->output_section->vma
14065 + htab->elf.splt->output_offset
14066 - 16);
176a0d42
AM
14067 if (info->emitrelocations)
14068 {
14069 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14070 if (r == NULL)
14071 return FALSE;
14072 r->r_offset = (htab->glink->output_offset
14073 + htab->glink->output_section->vma);
14074 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14075 r->r_addend = plt0;
14076 }
4ce794b7 14077 p = htab->glink->contents;
176a0d42 14078 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14079 bfd_put_64 (htab->glink->owner, plt0, p);
14080 p += 8;
b9e5796b
AM
14081 if (htab->opd_abi)
14082 {
14083 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14084 p += 4;
14085 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14086 p += 4;
14087 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14088 p += 4;
14089 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14090 p += 4;
14091 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14092 p += 4;
14093 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14094 p += 4;
14095 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14096 p += 4;
14097 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14098 p += 4;
14099 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14100 p += 4;
14101 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14102 p += 4;
14103 }
14104 else
14105 {
14106 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14107 p += 4;
14108 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14109 p += 4;
14110 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14111 p += 4;
f378ab09
AM
14112 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14113 p += 4;
b9e5796b
AM
14114 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14115 p += 4;
14116 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14117 p += 4;
14118 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14119 p += 4;
14120 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14121 p += 4;
14122 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
14123 p += 4;
14124 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14125 p += 4;
14126 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14127 p += 4;
14128 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14129 p += 4;
14130 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14131 p += 4;
14132 }
407aa07c
AM
14133 bfd_put_32 (htab->glink->owner, BCTR, p);
14134 p += 4;
c75bc4f7 14135 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14136
9f951329
AM
14137 /* Build the .glink lazy link call stubs. */
14138 indx = 0;
9e390558 14139 while (p < htab->glink->contents + htab->glink->size)
9f951329 14140 {
b9e5796b 14141 if (htab->opd_abi)
9f951329 14142 {
b9e5796b
AM
14143 if (indx < 0x8000)
14144 {
14145 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14146 p += 4;
14147 }
14148 else
14149 {
14150 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14151 p += 4;
14152 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14153 p);
14154 p += 4;
14155 }
9f951329 14156 }
4ce794b7 14157 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14158 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14159 indx++;
9f951329
AM
14160 p += 4;
14161 }
5d1634d7 14162 }
5d1634d7 14163
49c09209
AM
14164 /* Build .glink global entry stubs, and PLT relocs for globals. */
14165 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14166
14167 if (!write_plt_relocs_for_local_syms (info))
14168 return FALSE;
9e390558 14169
7341d5e2 14170 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 14171 {
4ce794b7 14172 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 14173 htab->brlt->size);
4ce794b7 14174 if (htab->brlt->contents == NULL)
b34976b6 14175 return FALSE;
721956f4 14176 }
ee75fd95 14177 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
14178 {
14179 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 14180 htab->relbrlt->size);
63bc6f6c
AM
14181 if (htab->relbrlt->contents == NULL)
14182 return FALSE;
14183 }
5d1634d7 14184
721956f4
AM
14185 /* Build the stubs as directed by the stub hash table. */
14186 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 14187
a4b6fadd
AM
14188 for (group = htab->group; group != NULL; group = group->next)
14189 if (group->needs_save_res)
7dda8d3c 14190 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 14191
aa8a7074
AM
14192 if (htab->relbrlt != NULL)
14193 htab->relbrlt->reloc_count = 0;
14194
e7d1c40c 14195 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14196 for (group = htab->group; group != NULL; group = group->next)
14197 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
14198 {
14199 int align = abs (htab->params->plt_stub_align);
14200 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14201 }
794e51c0 14202
7dda8d3c
AM
14203 for (group = htab->group; group != NULL; group = group->next)
14204 if (group->needs_save_res)
14205 {
14206 stub_sec = group->stub_sec;
14207 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14208 htab->sfpr->contents, htab->sfpr->size);
14209 if (htab->params->emit_stub_syms)
14210 {
14211 unsigned int i;
14212
14213 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14214 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14215 return FALSE;
14216 }
14217 }
14218
d4aaa2a0
AM
14219 for (group = htab->group; group != NULL; group = group->next)
14220 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14221 {
14222 stub_sec_count += 1;
c9301e31
AM
14223 if (stub_sec->rawsize != stub_sec->size
14224 && (htab->stub_iteration <= STUB_SHRINK_ITER
14225 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14226 break;
14227 }
5d1634d7 14228
25516cc5 14229 if (group != NULL)
5d1634d7 14230 {
b34976b6 14231 htab->stub_error = TRUE;
cf97bcb0 14232 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14233 }
721956f4 14234
d2a300cf
AM
14235 if (htab->stub_error)
14236 return FALSE;
14237
14238 if (stats != NULL)
14239 {
db84fff3 14240 size_t len;
d2a300cf
AM
14241 *stats = bfd_malloc (500);
14242 if (*stats == NULL)
14243 return FALSE;
14244
db84fff3
AM
14245 len = sprintf (*stats,
14246 ngettext ("linker stubs in %u group\n",
14247 "linker stubs in %u groups\n",
14248 stub_sec_count),
14249 stub_sec_count);
05d0e962
AM
14250 sprintf (*stats + len, _(" branch %lu\n"
14251 " branch toc adj %lu\n"
14252 " branch notoc %lu\n"
14253 " branch both %lu\n"
14254 " long branch %lu\n"
14255 " long toc adj %lu\n"
14256 " long notoc %lu\n"
14257 " long both %lu\n"
14258 " plt call %lu\n"
14259 " plt call save %lu\n"
14260 " plt call notoc %lu\n"
14261 " plt call both %lu\n"
14262 " global entry %lu"),
4ce794b7
AM
14263 htab->stub_count[ppc_stub_long_branch - 1],
14264 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14265 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14266 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14267 htab->stub_count[ppc_stub_plt_branch - 1],
14268 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14269 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14270 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14271 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14272 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14273 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14274 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14275 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14276 }
14277 return TRUE;
5bd4f169
AM
14278}
14279
60124e18
AM
14280/* What to do when ld finds relocations against symbols defined in
14281 discarded sections. */
14282
14283static unsigned int
14284ppc64_elf_action_discarded (asection *sec)
14285{
14286 if (strcmp (".opd", sec->name) == 0)
14287 return 0;
14288
14289 if (strcmp (".toc", sec->name) == 0)
14290 return 0;
14291
bce50a28
JJ
14292 if (strcmp (".toc1", sec->name) == 0)
14293 return 0;
14294
60124e18
AM
14295 return _bfd_elf_default_action_discarded (sec);
14296}
14297
5bd4f169
AM
14298/* The RELOCATE_SECTION function is called by the ELF backend linker
14299 to handle the relocations for a section.
14300
14301 The relocs are always passed as Rela structures; if the section
14302 actually uses Rel structures, the r_addend field will always be
14303 zero.
14304
14305 This function is responsible for adjust the section contents as
14306 necessary, and (if using Rela relocs and generating a
1049f94e 14307 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14308 necessary.
14309
14310 This function does not have to worry about setting the reloc
14311 address or the reloc symbol index.
14312
14313 LOCAL_SYMS is a pointer to the swapped in local symbols.
14314
14315 LOCAL_SECTIONS is an array giving the section in the input file
14316 corresponding to the st_shndx field of each local symbol.
14317
14318 The global hash table entry for the global symbols can be found
14319 via elf_sym_hashes (input_bfd).
14320
1049f94e 14321 When generating relocatable output, this function must handle
5bd4f169
AM
14322 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14323 going to be the section symbol corresponding to the output
14324 section, which means that the addend must be adjusted
14325 accordingly. */
14326
b34976b6 14327static bfd_boolean
4ce794b7
AM
14328ppc64_elf_relocate_section (bfd *output_bfd,
14329 struct bfd_link_info *info,
14330 bfd *input_bfd,
14331 asection *input_section,
14332 bfd_byte *contents,
14333 Elf_Internal_Rela *relocs,
14334 Elf_Internal_Sym *local_syms,
14335 asection **local_sections)
5bd4f169 14336{
65f38f15 14337 struct ppc_link_hash_table *htab;
5bd4f169
AM
14338 Elf_Internal_Shdr *symtab_hdr;
14339 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14340 Elf_Internal_Rela *rel;
c316a17c 14341 Elf_Internal_Rela *wrel;
5bd4f169 14342 Elf_Internal_Rela *relend;
411e1bfb
AM
14343 Elf_Internal_Rela outrel;
14344 bfd_byte *loc;
411e1bfb 14345 struct got_entry **local_got_ents;
5bd4f169 14346 bfd_vma TOCstart;
b34976b6
AM
14347 bfd_boolean ret = TRUE;
14348 bfd_boolean is_opd;
794e51c0
AM
14349 /* Assume 'at' branch hints. */
14350 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 14351 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14352
65f38f15 14353 /* Initialize howto table if needed. */
5bd4f169 14354 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14355 ppc_howto_init ();
14356
65f38f15 14357 htab = ppc_hash_table (info);
4dfe6ac6
NC
14358 if (htab == NULL)
14359 return FALSE;
ee75fd95
AM
14360
14361 /* Don't relocate stub sections. */
e7d1c40c 14362 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14363 return TRUE;
14364
0c8d6e5c 14365 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 14366
411e1bfb 14367 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14368 TOCstart = elf_gp (output_bfd);
0ffa91dd 14369 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14370 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14371 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14372
c316a17c 14373 rel = wrel = relocs;
5bd4f169 14374 relend = relocs + input_section->reloc_count;
c316a17c 14375 for (; rel < relend; wrel++, rel++)
5bd4f169 14376 {
04c9666a 14377 enum elf_ppc64_reloc_type r_type;
31c76678 14378 bfd_vma addend;
5bd4f169
AM
14379 bfd_reloc_status_type r;
14380 Elf_Internal_Sym *sym;
14381 asection *sec;
039b3fef
AM
14382 struct elf_link_hash_entry *h_elf;
14383 struct ppc_link_hash_entry *h;
14384 struct ppc_link_hash_entry *fdh;
5bd4f169 14385 const char *sym_name;
0d4792f7 14386 unsigned long r_symndx, toc_symndx;
3a71aa26 14387 bfd_vma toc_addend;
f961d9dd
AM
14388 unsigned char tls_mask, tls_gd, tls_type;
14389 unsigned char sym_type;
5bd4f169 14390 bfd_vma relocation;
23cedd1d 14391 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14392 bfd_boolean warned;
bc30df16 14393 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14394 unsigned int insn;
e11840f9 14395 unsigned int mask;
721956f4
AM
14396 struct ppc_stub_hash_entry *stub_entry;
14397 bfd_vma max_br_offset;
14398 bfd_vma from;
c316a17c 14399 Elf_Internal_Rela orig_rel;
b80eed39
AM
14400 reloc_howto_type *howto;
14401 struct reloc_howto_struct alt_howto;
5bd4f169 14402
c316a17c
AM
14403 again:
14404 orig_rel = *rel;
14405
4ce794b7 14406 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14407 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14408
14409 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14410 symbol of the previous ADDR64 reloc. The symbol gives us the
14411 proper TOC base to use. */
14412 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14413 && wrel != relocs
14414 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14415 && is_opd)
c316a17c 14416 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14417
4ce794b7
AM
14418 sym = NULL;
14419 sec = NULL;
039b3fef 14420 h_elf = NULL;
4ce794b7 14421 sym_name = NULL;
b34976b6
AM
14422 unresolved_reloc = FALSE;
14423 warned = FALSE;
65f38f15 14424
0b13192e 14425 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14426 {
14427 /* It's a local symbol. */
74f0fb50 14428 struct _opd_sec_data *opd;
4025353c 14429
5bd4f169
AM
14430 sym = local_syms + r_symndx;
14431 sec = local_sections[r_symndx];
26c61ae5 14432 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14433 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14434 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14435 opd = get_opd_info (sec);
14436 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14437 {
51aecdc5
AM
14438 long adjust = opd->adjust[OPD_NDX (sym->st_value
14439 + rel->r_addend)];
4025353c
AM
14440 if (adjust == -1)
14441 relocation = 0;
14442 else
4cc603a5
AM
14443 {
14444 /* If this is a relocation against the opd section sym
14445 and we have edited .opd, adjust the reloc addend so
14446 that ld -r and ld --emit-relocs output is correct.
14447 If it is a reloc against some other .opd symbol,
14448 then the symbol value will be adjusted later. */
14449 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14450 rel->r_addend += adjust;
14451 else
14452 relocation += adjust;
14453 }
1e2f5b6e 14454 }
5bd4f169
AM
14455 }
14456 else
14457 {
62d887d4
L
14458 bfd_boolean ignored;
14459
b2a8e766
AM
14460 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14461 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14462 h_elf, sec, relocation,
62d887d4 14463 unresolved_reloc, warned, ignored);
039b3fef
AM
14464 sym_name = h_elf->root.root.string;
14465 sym_type = h_elf->type;
b69fdb4e
AM
14466 if (sec != NULL
14467 && sec->owner == output_bfd
14468 && strcmp (sec->name, ".opd") == 0)
14469 {
14470 /* This is a symbol defined in a linker script. All
14471 such are defined in output sections, even those
14472 defined by simple assignment from a symbol defined in
14473 an input section. Transfer the symbol to an
14474 appropriate input .opd section, so that a branch to
14475 this symbol will be mapped to the location specified
14476 by the opd entry. */
14477 struct bfd_link_order *lo;
14478 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14479 if (lo->type == bfd_indirect_link_order)
14480 {
14481 asection *isec = lo->u.indirect.section;
14482 if (h_elf->root.u.def.value >= isec->output_offset
14483 && h_elf->root.u.def.value < (isec->output_offset
14484 + isec->size))
14485 {
14486 h_elf->root.u.def.value -= isec->output_offset;
14487 h_elf->root.u.def.section = isec;
14488 sec = isec;
14489 break;
14490 }
14491 }
14492 }
5bd4f169 14493 }
039b3fef 14494 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14495
dbaa2011 14496 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14497 {
14498 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14499 input_bfd, input_section,
14500 contents + rel->r_offset);
14501 wrel->r_offset = rel->r_offset;
14502 wrel->r_info = 0;
14503 wrel->r_addend = 0;
14504
14505 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14506 symbols defined in discarded sections. Not done for
c316a17c
AM
14507 non-debug to preserve relocs in .eh_frame which the
14508 eh_frame editing code expects to be present. */
14509 if (bfd_link_relocatable (info)
14510 && (input_section->flags & SEC_DEBUGGING))
14511 wrel--;
14512
14513 continue;
14514 }
ab96bf03 14515
0e1862bb 14516 if (bfd_link_relocatable (info))
c316a17c 14517 goto copy_reloc;
ab96bf03 14518
f40da81b
AM
14519 if (h != NULL && &h->elf == htab->elf.hgot)
14520 {
6f20ed8a 14521 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14522 sec = bfd_abs_section_ptr;
14523 unresolved_reloc = FALSE;
14524 }
14525
951fd09b
AM
14526 /* TLS optimizations. Replace instruction sequences and relocs
14527 based on information we collected in tls_optimize. We edit
14528 RELOCS so that --emit-relocs will output something sensible
14529 for the final instruction stream. */
14530 tls_mask = 0;
14531 tls_gd = 0;
0d4792f7 14532 toc_symndx = 0;
727fc41e
AM
14533 if (h != NULL)
14534 tls_mask = h->tls_mask;
14535 else if (local_got_ents != NULL)
411e1bfb 14536 {
e054468f
AM
14537 struct plt_entry **local_plt = (struct plt_entry **)
14538 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14539 unsigned char *lgot_masks = (unsigned char *)
e054468f 14540 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14541 tls_mask = lgot_masks[r_symndx];
14542 }
37da22e5 14543 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14544 && (r_type == R_PPC64_TLS
14545 || r_type == R_PPC64_TLSGD
14546 || r_type == R_PPC64_TLSLD))
14547 {
14548 /* Check for toc tls entries. */
f961d9dd 14549 unsigned char *toc_tls;
0d4792f7 14550
727fc41e
AM
14551 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14552 &local_syms, rel, input_bfd))
14553 return FALSE;
0d4792f7 14554
727fc41e
AM
14555 if (toc_tls)
14556 tls_mask = *toc_tls;
0d4792f7
AM
14557 }
14558
14559 /* Check that tls relocs are used with tls syms, and non-tls
14560 relocs are used with non-tls syms. */
cf35638d 14561 if (r_symndx != STN_UNDEF
0d4792f7
AM
14562 && r_type != R_PPC64_NONE
14563 && (h == NULL
039b3fef
AM
14564 || h->elf.root.type == bfd_link_hash_defined
14565 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
14566 && (IS_PPC64_TLS_RELOC (r_type)
14567 != (sym_type == STT_TLS
14568 || (sym_type == STT_SECTION
14569 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 14570 {
37da22e5 14571 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14572 && (r_type == R_PPC64_TLS
14573 || r_type == R_PPC64_TLSGD
14574 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14575 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14576 ;
14577 else
25f53a85 14578 info->callbacks->einfo
1d483afe 14579 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14580 /* xgettext:c-format */
c1c8c1ef 14581 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14582 /* xgettext:c-format */
c1c8c1ef 14583 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14584 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14585 ppc64_elf_howto_table[r_type]->name,
14586 sym_name);
411e1bfb
AM
14587 }
14588
14589 /* Ensure reloc mapping code below stays sane. */
14590 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14591 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14592 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14593 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14594 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14595 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14596 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14597 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14598 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14599 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14600 abort ();
0d4792f7 14601
411e1bfb
AM
14602 switch (r_type)
14603 {
14604 default:
411e1bfb
AM
14605 break;
14606
ba761f19 14607 case R_PPC64_LO_DS_OPT:
95f0d0d2 14608 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14609 if ((insn & (0x3f << 26)) != 58u << 26)
14610 abort ();
14611 insn += (14u << 26) - (58u << 26);
95f0d0d2 14612 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14613 r_type = R_PPC64_TOC16_LO;
14614 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14615 break;
14616
411e1bfb
AM
14617 case R_PPC64_TOC16:
14618 case R_PPC64_TOC16_LO:
14619 case R_PPC64_TOC16_DS:
14620 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14621 {
14622 /* Check for toc tls entries. */
f961d9dd 14623 unsigned char *toc_tls;
951fd09b 14624 int retval;
411e1bfb 14625
3a71aa26
AM
14626 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14627 &local_syms, rel, input_bfd);
951fd09b 14628 if (retval == 0)
411e1bfb
AM
14629 return FALSE;
14630
14631 if (toc_tls)
14632 {
951fd09b 14633 tls_mask = *toc_tls;
411e1bfb
AM
14634 if (r_type == R_PPC64_TOC16_DS
14635 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14636 {
37da22e5 14637 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14638 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14639 goto toctprel;
14640 }
411e1bfb 14641 else
951fd09b
AM
14642 {
14643 /* If we found a GD reloc pair, then we might be
14644 doing a GD->IE transition. */
14645 if (retval == 2)
14646 {
14647 tls_gd = TLS_TPRELGD;
37da22e5
AM
14648 if ((tls_mask & TLS_TLS) != 0
14649 && (tls_mask & TLS_GD) == 0)
102890f0 14650 goto tls_ldgd_opt;
951fd09b
AM
14651 }
14652 else if (retval == 3)
14653 {
37da22e5
AM
14654 if ((tls_mask & TLS_TLS) != 0
14655 && (tls_mask & TLS_LD) == 0)
102890f0 14656 goto tls_ldgd_opt;
951fd09b
AM
14657 }
14658 }
411e1bfb
AM
14659 }
14660 }
14661 break;
14662
9d6ded02
AM
14663 case R_PPC64_GOT_TPREL16_HI:
14664 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14665 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14666 && (tls_mask & TLS_TPREL) == 0)
14667 {
14668 rel->r_offset -= d_offset;
95f0d0d2 14669 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14670 r_type = R_PPC64_NONE;
14671 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14672 }
14673 break;
14674
411e1bfb
AM
14675 case R_PPC64_GOT_TPREL16_DS:
14676 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14677 if ((tls_mask & TLS_TLS) != 0
951fd09b 14678 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14679 {
81407a69 14680 toctprel:
95f0d0d2 14681 insn = bfd_get_32 (input_bfd,
c316a17c 14682 contents + rel->r_offset - d_offset);
411e1bfb
AM
14683 insn &= 31 << 21;
14684 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14685 bfd_put_32 (input_bfd, insn,
c316a17c 14686 contents + rel->r_offset - d_offset);
411e1bfb 14687 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14688 if (toc_symndx != 0)
14689 {
14690 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14691 rel->r_addend = toc_addend;
0d4792f7
AM
14692 /* We changed the symbol. Start over in order to
14693 get h, sym, sec etc. right. */
c316a17c 14694 goto again;
0d4792f7
AM
14695 }
14696 else
14697 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14698 }
14699 break;
14700
14701 case R_PPC64_TLS:
37da22e5 14702 if ((tls_mask & TLS_TLS) != 0
951fd09b 14703 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14704 {
95f0d0d2 14705 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
14706 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14707 if (insn == 0)
411e1bfb 14708 abort ();
95f0d0d2 14709 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 14710 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
14711 PPC64_TPREL16_LO which is at low-order half-word. */
14712 rel->r_offset += d_offset;
0d4792f7
AM
14713 r_type = R_PPC64_TPREL16_LO;
14714 if (toc_symndx != 0)
14715 {
14716 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14717 rel->r_addend = toc_addend;
0d4792f7
AM
14718 /* We changed the symbol. Start over in order to
14719 get h, sym, sec etc. right. */
c316a17c 14720 goto again;
0d4792f7
AM
14721 }
14722 else
14723 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14724 }
14725 break;
14726
411e1bfb
AM
14727 case R_PPC64_GOT_TLSGD16_HI:
14728 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14729 tls_gd = TLS_TPRELGD;
37da22e5 14730 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14731 goto tls_gdld_hi;
14732 break;
14733
411e1bfb
AM
14734 case R_PPC64_GOT_TLSLD16_HI:
14735 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14736 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14737 {
951fd09b
AM
14738 tls_gdld_hi:
14739 if ((tls_mask & tls_gd) != 0)
14740 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14741 + R_PPC64_GOT_TPREL16_DS);
14742 else
411e1bfb 14743 {
4fe5ca5b 14744 rel->r_offset -= d_offset;
95f0d0d2 14745 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14746 r_type = R_PPC64_NONE;
411e1bfb 14747 }
951fd09b 14748 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14749 }
14750 break;
14751
951fd09b
AM
14752 case R_PPC64_GOT_TLSGD16:
14753 case R_PPC64_GOT_TLSGD16_LO:
14754 tls_gd = TLS_TPRELGD;
37da22e5 14755 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14756 goto tls_ldgd_opt;
951fd09b 14757 break;
411e1bfb 14758
951fd09b
AM
14759 case R_PPC64_GOT_TLSLD16:
14760 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14761 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14762 {
b9f04fe0 14763 unsigned int insn1, insn2;
102890f0
AM
14764 bfd_vma offset;
14765
14766 tls_ldgd_opt:
727fc41e
AM
14767 offset = (bfd_vma) -1;
14768 /* If not using the newer R_PPC64_TLSGD/LD to mark
14769 __tls_get_addr calls, we must trust that the call
14770 stays with its arg setup insns, ie. that the next
14771 reloc is the __tls_get_addr call associated with
14772 the current reloc. Edit both insns. */
14773 if (input_section->has_tls_get_addr_call
14774 && rel + 1 < relend
14775 && branch_reloc_hash_match (input_bfd, rel + 1,
14776 htab->tls_get_addr,
14777 htab->tls_get_addr_fd))
14778 offset = rel[1].r_offset;
b86ac8e3
AM
14779 /* We read the low GOT_TLS (or TOC16) insn because we
14780 need to keep the destination reg. It may be
14781 something other than the usual r3, and moved to r3
14782 before the call by intervening code. */
95f0d0d2 14783 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14784 contents + rel->r_offset - d_offset);
102890f0 14785 if ((tls_mask & tls_gd) != 0)
411e1bfb 14786 {
102890f0 14787 /* IE */
b86ac8e3 14788 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14789 insn1 |= 58 << 26; /* ld */
14790 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14791 if (offset != (bfd_vma) -1)
f58d5a2d 14792 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
14793 if ((tls_mask & TLS_EXPLICIT) == 0)
14794 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14795 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 14796 else
102890f0
AM
14797 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14798 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14799 }
14800 else
14801 {
14802 /* LE */
b86ac8e3
AM
14803 insn1 &= 0x1f << 21;
14804 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14805 insn2 = 0x38630000; /* addi 3,3,0 */
14806 if (tls_gd == 0)
951fd09b 14807 {
102890f0 14808 /* Was an LD reloc. */
1d483afe
AM
14809 if (toc_symndx)
14810 sec = local_sections[toc_symndx];
14811 for (r_symndx = 0;
14812 r_symndx < symtab_hdr->sh_info;
14813 r_symndx++)
14814 if (local_sections[r_symndx] == sec)
14815 break;
14816 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14817 r_symndx = STN_UNDEF;
102890f0 14818 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14819 if (r_symndx != STN_UNDEF)
1d483afe
AM
14820 rel->r_addend -= (local_syms[r_symndx].st_value
14821 + sec->output_offset
14822 + sec->output_section->vma);
951fd09b 14823 }
102890f0 14824 else if (toc_symndx != 0)
3a71aa26
AM
14825 {
14826 r_symndx = toc_symndx;
14827 rel->r_addend = toc_addend;
14828 }
102890f0
AM
14829 r_type = R_PPC64_TPREL16_HA;
14830 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14831 if (offset != (bfd_vma) -1)
14832 {
14833 rel[1].r_info = ELF64_R_INFO (r_symndx,
14834 R_PPC64_TPREL16_LO);
14835 rel[1].r_offset = offset + d_offset;
14836 rel[1].r_addend = rel->r_addend;
14837 }
102890f0 14838 }
95f0d0d2 14839 bfd_put_32 (input_bfd, insn1,
3a71aa26 14840 contents + rel->r_offset - d_offset);
727fc41e 14841 if (offset != (bfd_vma) -1)
b9f04fe0 14842 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
14843 if ((tls_mask & tls_gd) == 0
14844 && (tls_gd == 0 || toc_symndx != 0))
14845 {
14846 /* We changed the symbol. Start over in order
14847 to get h, sym, sec etc. right. */
c316a17c 14848 goto again;
727fc41e
AM
14849 }
14850 }
14851 break;
14852
14853 case R_PPC64_TLSGD:
37da22e5 14854 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14855 && rel + 1 < relend)
727fc41e 14856 {
b9f04fe0 14857 unsigned int insn2;
727fc41e
AM
14858 bfd_vma offset = rel->r_offset;
14859
23cedd1d
AM
14860 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14861 {
14862 bfd_put_32 (output_bfd, NOP, contents + offset);
14863 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14864 break;
14865 }
14866
14867 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14868 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14869
727fc41e
AM
14870 if ((tls_mask & TLS_TPRELGD) != 0)
14871 {
14872 /* IE */
14873 r_type = R_PPC64_NONE;
14874 insn2 = 0x7c636a14; /* add 3,3,13 */
14875 }
14876 else
14877 {
14878 /* LE */
14879 if (toc_symndx != 0)
14880 {
14881 r_symndx = toc_symndx;
14882 rel->r_addend = toc_addend;
14883 }
14884 r_type = R_PPC64_TPREL16_LO;
14885 rel->r_offset = offset + d_offset;
14886 insn2 = 0x38630000; /* addi 3,3,0 */
14887 }
14888 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14889 /* Zap the reloc on the _tls_get_addr call too. */
14890 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14891 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14892 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 14893 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 14894 goto again;
411e1bfb 14895 }
411e1bfb
AM
14896 break;
14897
727fc41e 14898 case R_PPC64_TLSLD:
37da22e5 14899 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14900 && rel + 1 < relend)
727fc41e 14901 {
b9f04fe0 14902 unsigned int insn2;
727fc41e
AM
14903 bfd_vma offset = rel->r_offset;
14904
23cedd1d
AM
14905 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14906 {
14907 bfd_put_32 (output_bfd, NOP, contents + offset);
14908 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14909 break;
14910 }
14911
14912 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14913 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14914
727fc41e
AM
14915 if (toc_symndx)
14916 sec = local_sections[toc_symndx];
14917 for (r_symndx = 0;
14918 r_symndx < symtab_hdr->sh_info;
14919 r_symndx++)
14920 if (local_sections[r_symndx] == sec)
14921 break;
14922 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14923 r_symndx = STN_UNDEF;
727fc41e 14924 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14925 if (r_symndx != STN_UNDEF)
727fc41e
AM
14926 rel->r_addend -= (local_syms[r_symndx].st_value
14927 + sec->output_offset
14928 + sec->output_section->vma);
14929
14930 r_type = R_PPC64_TPREL16_LO;
14931 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14932 rel->r_offset = offset + d_offset;
14933 /* Zap the reloc on the _tls_get_addr call too. */
14934 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14935 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 14936 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 14937 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 14938 goto again;
727fc41e
AM
14939 }
14940 break;
14941
411e1bfb 14942 case R_PPC64_DTPMOD64:
951fd09b
AM
14943 if (rel + 1 < relend
14944 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14945 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14946 {
951fd09b
AM
14947 if ((tls_mask & TLS_GD) == 0)
14948 {
14949 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14950 if ((tls_mask & TLS_TPRELGD) != 0)
14951 r_type = R_PPC64_TPREL64;
14952 else
14953 {
4ce794b7 14954 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14955 r_type = R_PPC64_NONE;
14956 }
14957 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14958 }
14959 }
14960 else
14961 {
14962 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14963 {
4ce794b7 14964 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14965 r_type = R_PPC64_NONE;
951fd09b 14966 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14967 }
411e1bfb
AM
14968 }
14969 break;
14970
14971 case R_PPC64_TPREL64:
951fd09b 14972 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14973 {
14974 r_type = R_PPC64_NONE;
14975 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14976 }
14977 break;
52a82034 14978
006589cf
AM
14979 case R_PPC64_ENTRY:
14980 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14981 if (!bfd_link_pic (info)
14982 && !info->traditional_format
14983 && relocation + 0x80008000 <= 0xffffffff)
14984 {
14985 unsigned int insn1, insn2;
14986
14987 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14988 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14989 if ((insn1 & ~0xfffc) == LD_R2_0R12
14990 && insn2 == ADD_R2_R2_R12)
14991 {
95f0d0d2 14992 bfd_put_32 (input_bfd,
006589cf
AM
14993 LIS_R2 + PPC_HA (relocation),
14994 contents + rel->r_offset);
95f0d0d2 14995 bfd_put_32 (input_bfd,
006589cf
AM
14996 ADDI_R2_R2 + PPC_LO (relocation),
14997 contents + rel->r_offset + 4);
14998 }
14999 }
15000 else
15001 {
15002 relocation -= (rel->r_offset
15003 + input_section->output_offset
15004 + input_section->output_section->vma);
15005 if (relocation + 0x80008000 <= 0xffffffff)
15006 {
15007 unsigned int insn1, insn2;
15008
15009 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15010 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15011 if ((insn1 & ~0xfffc) == LD_R2_0R12
15012 && insn2 == ADD_R2_R2_R12)
15013 {
95f0d0d2 15014 bfd_put_32 (input_bfd,
006589cf
AM
15015 ADDIS_R2_R12 + PPC_HA (relocation),
15016 contents + rel->r_offset);
95f0d0d2 15017 bfd_put_32 (input_bfd,
006589cf
AM
15018 ADDI_R2_R2 + PPC_LO (relocation),
15019 contents + rel->r_offset + 4);
15020 }
15021 }
15022 }
15023 break;
15024
52a82034
AM
15025 case R_PPC64_REL16_HA:
15026 /* If we are generating a non-PIC executable, edit
15027 . 0: addis 2,12,.TOC.-0b@ha
15028 . addi 2,2,.TOC.-0b@l
15029 used by ELFv2 global entry points to set up r2, to
15030 . lis 2,.TOC.@ha
15031 . addi 2,2,.TOC.@l
15032 if .TOC. is in range. */
0e1862bb 15033 if (!bfd_link_pic (info)
810d4e75 15034 && !info->traditional_format
006589cf 15035 && !htab->opd_abi
4f038ee5 15036 && rel->r_addend == d_offset
52a82034
AM
15037 && h != NULL && &h->elf == htab->elf.hgot
15038 && rel + 1 < relend
15039 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15040 && rel[1].r_offset == rel->r_offset + 4
15041 && rel[1].r_addend == rel->r_addend + 4
15042 && relocation + 0x80008000 <= 0xffffffff)
15043 {
15044 unsigned int insn1, insn2;
15045 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
15046 insn1 = bfd_get_32 (input_bfd, contents + offset);
15047 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15048 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15049 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15050 {
15051 r_type = R_PPC64_ADDR16_HA;
15052 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15053 rel->r_addend -= d_offset;
15054 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15055 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15056 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15057 }
15058 }
15059 break;
411e1bfb
AM
15060 }
15061
15062 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15063 insn = 0;
b25116a9
AM
15064 max_br_offset = 1 << 25;
15065 addend = rel->r_addend;
bc30df16 15066 reloc_dest = DEST_NORMAL;
65f38f15 15067 switch (r_type)
5bd4f169
AM
15068 {
15069 default:
65f38f15 15070 break;
5bd4f169 15071
3b421ab3
AM
15072 case R_PPC64_TOCSAVE:
15073 if (relocation + addend == (rel->r_offset
15074 + input_section->output_offset
15075 + input_section->output_section->vma)
15076 && tocsave_find (htab, NO_INSERT,
15077 &local_syms, rel, input_bfd))
15078 {
15079 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15080 if (insn == NOP
15081 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15082 bfd_put_32 (input_bfd,
15083 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15084 contents + rel->r_offset);
15085 }
15086 break;
15087
65f38f15
AM
15088 /* Branch taken prediction relocations. */
15089 case R_PPC64_ADDR14_BRTAKEN:
15090 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15091 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15092 /* Fall through. */
65f38f15 15093
86c76c7b 15094 /* Branch not taken prediction relocations. */
65f38f15
AM
15095 case R_PPC64_ADDR14_BRNTAKEN:
15096 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15097 insn |= bfd_get_32 (input_bfd,
411e1bfb 15098 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15099 /* Fall through. */
86c76c7b 15100
b25116a9
AM
15101 case R_PPC64_REL14:
15102 max_br_offset = 1 << 15;
1a0670f3 15103 /* Fall through. */
5bd4f169 15104
65f38f15 15105 case R_PPC64_REL24:
05d0e962 15106 case R_PPC64_REL24_NOTOC:
23cedd1d 15107 case R_PPC64_PLTCALL:
ad8e1ba5
AM
15108 /* Calls to functions with a different TOC, such as calls to
15109 shared objects, need to alter the TOC pointer. This is
15110 done using a linkage stub. A REL24 branching to these
15111 linkage stubs needs to be followed by a nop, as the nop
15112 will be replaced with an instruction to restore the TOC
15113 base pointer. */
8387904d 15114 fdh = h;
b31867b6
AM
15115 if (h != NULL
15116 && h->oh != NULL
15117 && h->oh->is_func_descriptor)
15118 fdh = ppc_follow_link (h->oh);
31c76678
DK
15119 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15120 htab);
23cedd1d
AM
15121 if (r_type == R_PPC64_PLTCALL
15122 && stub_entry != NULL
05d0e962
AM
15123 && stub_entry->stub_type >= ppc_stub_plt_call
15124 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15125 stub_entry = NULL;
15126
6abec6bc 15127 if (stub_entry != NULL
ad8e1ba5 15128 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 15129 || stub_entry->stub_type == ppc_stub_plt_call_r2save
05d0e962 15130 || stub_entry->stub_type == ppc_stub_plt_call_both
ad8e1ba5 15131 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15132 || stub_entry->stub_type == ppc_stub_plt_branch_both
15133 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15134 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15135 {
b25116a9 15136 bfd_boolean can_plt_call = FALSE;
721956f4 15137
6e1816be
AM
15138 if (stub_entry->stub_type == ppc_stub_plt_call
15139 && !htab->opd_abi
15140 && htab->params->plt_localentry0 != 0
15141 && is_elfv2_localentry0 (&h->elf))
15142 {
15143 /* The function doesn't use or change r2. */
15144 can_plt_call = TRUE;
15145 }
05d0e962
AM
15146 else if (r_type == R_PPC64_REL24_NOTOC)
15147 {
15148 /* NOTOC calls don't need to restore r2. */
15149 can_plt_call = TRUE;
15150 }
6e1816be 15151
f378ab09 15152 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15153 branch and link followed by a nop. The nop is
15154 replaced by an insn to restore r2. */
6e1816be 15155 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15156 {
ba8ca3e7
AM
15157 unsigned long br;
15158
15159 br = bfd_get_32 (input_bfd,
15160 contents + rel->r_offset);
15161 if ((br & 1) != 0)
41bd81ab 15162 {
ba8ca3e7
AM
15163 unsigned long nop;
15164
15165 nop = bfd_get_32 (input_bfd,
15166 contents + rel->r_offset + 4);
23cedd1d
AM
15167 if (nop == LD_R2_0R1 + STK_TOC (htab))
15168 can_plt_call = TRUE;
15169 else if (nop == NOP
15170 || nop == CROR_151515
15171 || nop == CROR_313131)
a7f2871e 15172 {
ba8ca3e7
AM
15173 if (h != NULL
15174 && (h == htab->tls_get_addr_fd
15175 || h == htab->tls_get_addr)
7c9cf415 15176 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15177 {
15178 /* Special stub used, leave nop alone. */
15179 }
15180 else
a078d95a
AM
15181 bfd_put_32 (input_bfd,
15182 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15183 contents + rel->r_offset + 4);
15184 can_plt_call = TRUE;
a7f2871e 15185 }
41bd81ab 15186 }
5bd4f169 15187 }
721956f4 15188
ba8ca3e7 15189 if (!can_plt_call && h != NULL)
721956f4 15190 {
ba8ca3e7
AM
15191 const char *name = h->elf.root.root.string;
15192
15193 if (*name == '.')
15194 ++name;
15195
15196 if (strncmp (name, "__libc_start_main", 17) == 0
15197 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15198 {
ba8ca3e7
AM
15199 /* Allow crt1 branch to go via a toc adjusting
15200 stub. Other calls that never return could do
15201 the same, if we could detect such. */
b25116a9 15202 can_plt_call = TRUE;
6ab189d5 15203 }
ba8ca3e7
AM
15204 }
15205
15206 if (!can_plt_call)
15207 {
15208 /* g++ as of 20130507 emits self-calls without a
15209 following nop. This is arguably wrong since we
15210 have conflicting information. On the one hand a
15211 global symbol and on the other a local call
15212 sequence, but don't error for this special case.
15213 It isn't possible to cheaply verify we have
15214 exactly such a call. Allow all calls to the same
15215 section. */
15216 asection *code_sec = sec;
15217
15218 if (get_opd_info (sec) != NULL)
ad8e1ba5 15219 {
ba8ca3e7
AM
15220 bfd_vma off = (relocation + addend
15221 - sec->output_section->vma
15222 - sec->output_offset);
bc30df16 15223
ba8ca3e7 15224 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15225 }
ba8ca3e7
AM
15226 if (code_sec == input_section)
15227 can_plt_call = TRUE;
15228 }
15229
15230 if (!can_plt_call)
15231 {
05d0e962
AM
15232 if (stub_entry->stub_type >= ppc_stub_plt_call
15233 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15234 info->callbacks->einfo
695344c0 15235 /* xgettext:c-format */
c1c8c1ef 15236 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
15237 "recompile with -fPIC\n"),
15238 input_bfd, input_section, rel->r_offset, sym_name);
15239 else
15240 info->callbacks->einfo
695344c0 15241 /* xgettext:c-format */
c1c8c1ef 15242 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
15243 "(-mcmodel=small toc adjust stub)\n"),
15244 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15245
15246 bfd_set_error (bfd_error_bad_value);
15247 ret = FALSE;
721956f4
AM
15248 }
15249
b25116a9 15250 if (can_plt_call
05d0e962
AM
15251 && stub_entry->stub_type >= ppc_stub_plt_call
15252 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15253 unresolved_reloc = FALSE;
15254 }
15255
6abec6bc
AM
15256 if ((stub_entry == NULL
15257 || stub_entry->stub_type == ppc_stub_long_branch
15258 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15259 && get_opd_info (sec) != NULL)
15260 {
15261 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15262 bfd_vma off = (relocation + addend
15263 - sec->output_section->vma
15264 - sec->output_offset);
aef36ac1 15265 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15266 if (dest != (bfd_vma) -1)
15267 {
15268 relocation = dest;
15269 addend = 0;
bc30df16 15270 reloc_dest = DEST_OPD;
8387904d
AM
15271 }
15272 }
15273
b25116a9
AM
15274 /* If the branch is out of reach we ought to have a long
15275 branch stub. */
15276 from = (rel->r_offset
15277 + input_section->output_offset
15278 + input_section->output_section->vma);
15279
6911b7dc
AM
15280 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15281 ? fdh->elf.other
15282 : sym->st_other);
15283
6abec6bc
AM
15284 if (stub_entry != NULL
15285 && (stub_entry->stub_type == ppc_stub_long_branch
15286 || stub_entry->stub_type == ppc_stub_plt_branch)
15287 && (r_type == R_PPC64_ADDR14_BRTAKEN
15288 || r_type == R_PPC64_ADDR14_BRNTAKEN
15289 || (relocation + addend - from + max_br_offset
15290 < 2 * max_br_offset)))
15291 /* Don't use the stub if this branch is in range. */
15292 stub_entry = NULL;
b25116a9 15293
05d0e962
AM
15294 if (stub_entry != NULL
15295 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15296 || stub_entry->stub_type == ppc_stub_long_branch_both
15297 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15298 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15299 && (r_type != R_PPC64_REL24_NOTOC
15300 || ((fdh ? fdh->elf.other : sym->st_other)
15301 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
15302 && (relocation + addend - from + max_br_offset
15303 < 2 * max_br_offset))
15304 stub_entry = NULL;
15305
15306 if (stub_entry != NULL
15307 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15308 || stub_entry->stub_type == ppc_stub_long_branch_both
15309 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15310 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15311 && r_type == R_PPC64_REL24_NOTOC
15312 && (relocation + addend - from + max_br_offset
15313 < 2 * max_br_offset))
15314 stub_entry = NULL;
15315
b25116a9
AM
15316 if (stub_entry != NULL)
15317 {
15318 /* Munge up the value and addend so that we call the stub
15319 rather than the procedure directly. */
a4b6fadd
AM
15320 asection *stub_sec = stub_entry->group->stub_sec;
15321
15322 if (stub_entry->stub_type == ppc_stub_save_res)
15323 relocation += (stub_sec->output_offset
15324 + stub_sec->output_section->vma
15325 + stub_sec->size - htab->sfpr->size
15326 - htab->sfpr->output_offset
15327 - htab->sfpr->output_section->vma);
15328 else
15329 relocation = (stub_entry->stub_offset
15330 + stub_sec->output_offset
15331 + stub_sec->output_section->vma);
b25116a9 15332 addend = 0;
bc30df16 15333 reloc_dest = DEST_STUB;
3b421ab3 15334
05d0e962
AM
15335 if (((stub_entry->stub_type == ppc_stub_plt_call
15336 && ALWAYS_EMIT_R2SAVE)
15337 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15338 || stub_entry->stub_type == ppc_stub_plt_call_both)
3b421ab3
AM
15339 && rel + 1 < relend
15340 && rel[1].r_offset == rel->r_offset + 4
15341 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15342 relocation += 4;
05d0e962
AM
15343 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15344 || stub_entry->stub_type == ppc_stub_plt_branch_both
15345 || stub_entry->stub_type == ppc_stub_plt_call_both)
15346 && r_type == R_PPC64_REL24_NOTOC)
15347 relocation += 4;
b25116a9
AM
15348 }
15349
15350 if (insn != 0)
15351 {
794e51c0 15352 if (is_isa_v2)
721956f4 15353 {
b25116a9
AM
15354 /* Set 'a' bit. This is 0b00010 in BO field for branch
15355 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15356 for branch on CTR insns (BO == 1a00t or 1a01t). */
15357 if ((insn & (0x14 << 21)) == (0x04 << 21))
15358 insn |= 0x02 << 21;
15359 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15360 insn |= 0x08 << 21;
15361 else
15362 break;
15363 }
15364 else
15365 {
15366 /* Invert 'y' bit if not the default. */
4cc603a5 15367 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15368 insn ^= 0x01 << 21;
721956f4 15369 }
b25116a9 15370
95f0d0d2 15371 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15372 }
e86ce104 15373
06da1e8e
AM
15374 /* NOP out calls to undefined weak functions.
15375 We can thus call a weak function without first
15376 checking whether the function is defined. */
b25116a9 15377 else if (h != NULL
039b3fef 15378 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15379 && h->elf.dynindx == -1
05d0e962
AM
15380 && (r_type == R_PPC64_REL24
15381 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15382 && relocation == 0
4cc603a5 15383 && addend == 0)
e86ce104 15384 {
95f0d0d2 15385 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15386 goto copy_reloc;
e86ce104 15387 }
65f38f15
AM
15388 break;
15389 }
5bd4f169 15390
65f38f15 15391 /* Set `addend'. */
411e1bfb 15392 tls_type = 0;
23cedd1d 15393 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15394 switch (r_type)
15395 {
15396 default:
cf97bcb0
AM
15397 /* xgettext:c-format */
15398 _bfd_error_handler (_("%pB: %s unsupported"),
15399 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15400
65f38f15 15401 bfd_set_error (bfd_error_bad_value);
b34976b6 15402 ret = FALSE;
c316a17c 15403 goto copy_reloc;
5bd4f169 15404
65f38f15 15405 case R_PPC64_NONE:
411e1bfb 15406 case R_PPC64_TLS:
727fc41e
AM
15407 case R_PPC64_TLSGD:
15408 case R_PPC64_TLSLD:
3b421ab3 15409 case R_PPC64_TOCSAVE:
04c9666a
AM
15410 case R_PPC64_GNU_VTINHERIT:
15411 case R_PPC64_GNU_VTENTRY:
006589cf 15412 case R_PPC64_ENTRY:
c316a17c 15413 goto copy_reloc;
5bd4f169
AM
15414
15415 /* GOT16 relocations. Like an ADDR16 using the symbol's
15416 address in the GOT as relocation value instead of the
411e1bfb 15417 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15418 symbol and put the symbol value there. */
411e1bfb
AM
15419 case R_PPC64_GOT_TLSGD16:
15420 case R_PPC64_GOT_TLSGD16_LO:
15421 case R_PPC64_GOT_TLSGD16_HI:
15422 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 15423 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15424 goto dogot;
15425
15426 case R_PPC64_GOT_TLSLD16:
15427 case R_PPC64_GOT_TLSLD16_LO:
15428 case R_PPC64_GOT_TLSLD16_HI:
15429 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 15430 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15431 goto dogot;
15432
15433 case R_PPC64_GOT_TPREL16_DS:
15434 case R_PPC64_GOT_TPREL16_LO_DS:
15435 case R_PPC64_GOT_TPREL16_HI:
15436 case R_PPC64_GOT_TPREL16_HA:
15437 tls_type = TLS_TLS | TLS_TPREL;
15438 goto dogot;
15439
15440 case R_PPC64_GOT_DTPREL16_DS:
15441 case R_PPC64_GOT_DTPREL16_LO_DS:
15442 case R_PPC64_GOT_DTPREL16_HI:
15443 case R_PPC64_GOT_DTPREL16_HA:
15444 tls_type = TLS_TLS | TLS_DTPREL;
15445 goto dogot;
15446
65f38f15
AM
15447 case R_PPC64_GOT16:
15448 case R_PPC64_GOT16_LO:
15449 case R_PPC64_GOT16_HI:
15450 case R_PPC64_GOT16_HA:
15451 case R_PPC64_GOT16_DS:
15452 case R_PPC64_GOT16_LO_DS:
411e1bfb 15453 dogot:
5bd4f169
AM
15454 {
15455 /* Relocation is to the entry for this symbol in the global
15456 offset table. */
e717da7e 15457 asection *got;
d881513a 15458 bfd_vma *offp;
5bd4f169 15459 bfd_vma off;
d881513a 15460 unsigned long indx = 0;
927be08e 15461 struct got_entry *ent;
65f38f15 15462
d881513a
AM
15463 if (tls_type == (TLS_TLS | TLS_LD)
15464 && (h == NULL
f5385ebf 15465 || !h->elf.def_dynamic))
927be08e 15466 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15467 else
5bd4f169 15468 {
d881513a
AM
15469 if (h != NULL)
15470 {
f0158f44
AM
15471 if (!htab->elf.dynamic_sections_created
15472 || h->elf.dynindx == -1
15473 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15474 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15475 /* This is actually a static link, or it is a
15476 -Bsymbolic link and the symbol is defined
15477 locally, or the symbol was forced to be local
15478 because of a version file. */
15479 ;
15480 else
15481 {
039b3fef 15482 indx = h->elf.dynindx;
d881513a
AM
15483 unresolved_reloc = FALSE;
15484 }
039b3fef 15485 ent = h->elf.got.glist;
d881513a 15486 }
411e1bfb 15487 else
5bd4f169 15488 {
d881513a
AM
15489 if (local_got_ents == NULL)
15490 abort ();
15491 ent = local_got_ents[r_symndx];
5bd4f169 15492 }
d881513a
AM
15493
15494 for (; ent != NULL; ent = ent->next)
31c76678 15495 if (ent->addend == orig_rel.r_addend
e717da7e 15496 && ent->owner == input_bfd
d881513a
AM
15497 && ent->tls_type == tls_type)
15498 break;
5bd4f169 15499 }
411e1bfb 15500
927be08e
AM
15501 if (ent == NULL)
15502 abort ();
15503 if (ent->is_indirect)
15504 ent = ent->got.ent;
15505 offp = &ent->got.offset;
15506 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15507 if (got == NULL)
15508 abort ();
15509
411e1bfb
AM
15510 /* The offset must always be a multiple of 8. We use the
15511 least significant bit to record whether we have already
15512 processed this entry. */
d881513a 15513 off = *offp;
411e1bfb
AM
15514 if ((off & 1) != 0)
15515 off &= ~1;
5bd4f169
AM
15516 else
15517 {
411e1bfb
AM
15518 /* Generate relocs for the dynamic linker, except in
15519 the case of TLSLD where we'll use one entry per
15520 module. */
25f23106
AM
15521 asection *relgot;
15522 bfd_boolean ifunc;
e717da7e 15523
d881513a 15524 *offp = off | 1;
25f23106
AM
15525 relgot = NULL;
15526 ifunc = (h != NULL
15527 ? h->elf.type == STT_GNU_IFUNC
15528 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15529 if (ifunc)
82e66161
AM
15530 {
15531 relgot = htab->elf.irelplt;
15532 if (indx == 0)
15533 htab->local_ifunc_resolver = 1;
15534 else if (is_static_defined (&h->elf))
15535 htab->maybe_local_ifunc_resolver = 1;
15536 }
f0158f44
AM
15537 else if (indx != 0
15538 || (bfd_link_pic (info)
15539 && (h == NULL
21d68fcd 15540 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15541 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15542 && !h->elf.def_dynamic))
15543 && !(tls_type == (TLS_TLS | TLS_TPREL)
15544 && bfd_link_executable (info)
15545 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15546 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15547 if (relgot != NULL)
5bd4f169 15548 {
e717da7e
AM
15549 outrel.r_offset = (got->output_section->vma
15550 + got->output_offset
411e1bfb 15551 + off);
4cc603a5 15552 outrel.r_addend = addend;
d881513a 15553 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15554 {
411e1bfb 15555 outrel.r_addend = 0;
e515b051 15556 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15557 if (tls_type == (TLS_TLS | TLS_GD))
15558 {
e717da7e
AM
15559 loc = relgot->contents;
15560 loc += (relgot->reloc_count++
d881513a
AM
15561 * sizeof (Elf64_External_Rela));
15562 bfd_elf64_swap_reloca_out (output_bfd,
15563 &outrel, loc);
e515b051 15564 outrel.r_offset += 8;
4cc603a5 15565 outrel.r_addend = addend;
d881513a
AM
15566 outrel.r_info
15567 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15568 }
411e1bfb 15569 }
951fd09b 15570 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15571 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15572 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15573 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15574 else if (indx != 0)
15575 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15576 else
81407a69 15577 {
25f23106
AM
15578 if (ifunc)
15579 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15580 else
15581 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15582
15583 /* Write the .got section contents for the sake
15584 of prelink. */
e717da7e 15585 loc = got->contents + off;
23fbd6fa
JJ
15586 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15587 loc);
81407a69 15588 }
81407a69
AM
15589
15590 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15591 {
15592 outrel.r_addend += relocation;
15593 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15594 {
15595 if (htab->elf.tls_sec == NULL)
15596 outrel.r_addend = 0;
15597 else
15598 outrel.r_addend -= htab->elf.tls_sec->vma;
15599 }
e515b051 15600 }
e717da7e
AM
15601 loc = relgot->contents;
15602 loc += (relgot->reloc_count++
411e1bfb
AM
15603 * sizeof (Elf64_External_Rela));
15604 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15605 }
15606
ad8e1ba5 15607 /* Init the .got section contents here if we're not
81407a69 15608 emitting a reloc. */
d881513a 15609 else
411e1bfb 15610 {
4cc603a5 15611 relocation += addend;
f0158f44 15612 if (tls_type != 0)
411e1bfb 15613 {
989f9879
AM
15614 if (htab->elf.tls_sec == NULL)
15615 relocation = 0;
15616 else
15617 {
f0158f44
AM
15618 if (tls_type & TLS_LD)
15619 relocation = 0;
15620 else
15621 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15622 if (tls_type & TLS_TPREL)
989f9879
AM
15623 relocation += DTP_OFFSET - TP_OFFSET;
15624 }
5bd4f169 15625
f0158f44 15626 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15627 {
15628 bfd_put_64 (output_bfd, relocation,
e717da7e 15629 got->contents + off + 8);
676ee2b5 15630 relocation = 1;
7b609f53 15631 }
411e1bfb
AM
15632 }
15633 bfd_put_64 (output_bfd, relocation,
e717da7e 15634 got->contents + off);
5bd4f169
AM
15635 }
15636 }
15637
65f38f15
AM
15638 if (off >= (bfd_vma) -2)
15639 abort ();
15640
bf102f86 15641 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 15642 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15643 }
65f38f15
AM
15644 break;
15645
15646 case R_PPC64_PLT16_HA:
15647 case R_PPC64_PLT16_HI:
15648 case R_PPC64_PLT16_LO:
08be3224 15649 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
15650 case R_PPC64_PLT32:
15651 case R_PPC64_PLT64:
23cedd1d
AM
15652 case R_PPC64_PLTSEQ:
15653 case R_PPC64_PLTCALL:
65f38f15
AM
15654 /* Relocation is to the entry for this symbol in the
15655 procedure linkage table. */
23cedd1d 15656 unresolved_reloc = TRUE;
cbf95972
AM
15657 {
15658 struct plt_entry **plt_list = NULL;
15659 if (h != NULL)
15660 plt_list = &h->elf.plt.plist;
15661 else if (local_got_ents != NULL)
15662 {
15663 struct plt_entry **local_plt = (struct plt_entry **)
15664 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15665 plt_list = local_plt + r_symndx;
cbf95972
AM
15666 }
15667 if (plt_list)
15668 {
15669 struct plt_entry *ent;
65f38f15 15670
cbf95972
AM
15671 for (ent = *plt_list; ent != NULL; ent = ent->next)
15672 if (ent->plt.offset != (bfd_vma) -1
15673 && ent->addend == orig_rel.r_addend)
15674 {
15675 asection *plt;
08be3224 15676 bfd_vma got;
cbf95972
AM
15677
15678 plt = htab->elf.splt;
15679 if (!htab->elf.dynamic_sections_created
15680 || h == NULL
15681 || h->elf.dynindx == -1)
2d7ad24e
AM
15682 {
15683 if (h != NULL
15684 ? h->elf.type == STT_GNU_IFUNC
15685 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15686 plt = htab->elf.iplt;
15687 else
15688 plt = htab->pltlocal;
15689 }
15690 relocation = (plt->output_section->vma
15691 + plt->output_offset
15692 + ent->plt.offset);
08be3224
AM
15693 if (r_type == R_PPC64_PLT16_HA
15694 || r_type ==R_PPC64_PLT16_HI
15695 || r_type ==R_PPC64_PLT16_LO
15696 || r_type ==R_PPC64_PLT16_LO_DS)
15697 {
15698 got = (elf_gp (output_bfd)
15699 + htab->sec_info[input_section->id].toc_off);
15700 relocation -= got;
15701 }
cbf95972
AM
15702 addend = 0;
15703 unresolved_reloc = FALSE;
15704 break;
15705 }
15706 }
15707 }
65f38f15 15708 break;
5bd4f169 15709
0b13192e
AM
15710 case R_PPC64_TOC:
15711 /* Relocation value is TOC base. */
15712 relocation = TOCstart;
cf35638d 15713 if (r_symndx == STN_UNDEF)
6f20ed8a 15714 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15715 else if (unresolved_reloc)
15716 ;
6f20ed8a
AM
15717 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15718 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15719 else
15720 unresolved_reloc = TRUE;
ab96bf03 15721 goto dodyn;
0b13192e 15722
5bd4f169
AM
15723 /* TOC16 relocs. We want the offset relative to the TOC base,
15724 which is the address of the start of the TOC plus 0x8000.
15725 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15726 in this order. */
65f38f15
AM
15727 case R_PPC64_TOC16:
15728 case R_PPC64_TOC16_LO:
15729 case R_PPC64_TOC16_HI:
15730 case R_PPC64_TOC16_DS:
15731 case R_PPC64_TOC16_LO_DS:
15732 case R_PPC64_TOC16_HA:
6f20ed8a 15733 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15734 break;
15735
15736 /* Relocate against the beginning of the section. */
65f38f15
AM
15737 case R_PPC64_SECTOFF:
15738 case R_PPC64_SECTOFF_LO:
15739 case R_PPC64_SECTOFF_HI:
15740 case R_PPC64_SECTOFF_DS:
15741 case R_PPC64_SECTOFF_LO_DS:
15742 case R_PPC64_SECTOFF_HA:
4ce794b7 15743 if (sec != NULL)
65f38f15 15744 addend -= sec->output_section->vma;
5bd4f169
AM
15745 break;
15746
25f23106
AM
15747 case R_PPC64_REL16:
15748 case R_PPC64_REL16_LO:
15749 case R_PPC64_REL16_HI:
15750 case R_PPC64_REL16_HA:
a680de9a 15751 case R_PPC64_REL16DX_HA:
25f23106
AM
15752 break;
15753
721956f4
AM
15754 case R_PPC64_REL14:
15755 case R_PPC64_REL14_BRNTAKEN:
15756 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15757 case R_PPC64_REL24:
05d0e962 15758 case R_PPC64_REL24_NOTOC:
5d1634d7
AM
15759 break;
15760
411e1bfb
AM
15761 case R_PPC64_TPREL16:
15762 case R_PPC64_TPREL16_LO:
15763 case R_PPC64_TPREL16_HI:
15764 case R_PPC64_TPREL16_HA:
15765 case R_PPC64_TPREL16_DS:
15766 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15767 case R_PPC64_TPREL16_HIGH:
15768 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15769 case R_PPC64_TPREL16_HIGHER:
15770 case R_PPC64_TPREL16_HIGHERA:
15771 case R_PPC64_TPREL16_HIGHEST:
15772 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
15773 if (h != NULL
15774 && h->elf.root.type == bfd_link_hash_undefweak
15775 && h->elf.dynindx == -1)
15776 {
15777 /* Make this relocation against an undefined weak symbol
15778 resolve to zero. This is really just a tweak, since
15779 code using weak externs ought to check that they are
15780 defined before using them. */
15781 bfd_byte *p = contents + rel->r_offset - d_offset;
15782
95f0d0d2 15783 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15784 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15785 if (insn != 0)
95f0d0d2 15786 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15787 break;
15788 }
989f9879
AM
15789 if (htab->elf.tls_sec != NULL)
15790 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15791 /* The TPREL16 relocs shouldn't really be used in shared
15792 libs or with non-local symbols as that will result in
15793 DT_TEXTREL being set, but support them anyway. */
15794 goto dodyn;
411e1bfb
AM
15795
15796 case R_PPC64_DTPREL16:
15797 case R_PPC64_DTPREL16_LO:
15798 case R_PPC64_DTPREL16_HI:
15799 case R_PPC64_DTPREL16_HA:
15800 case R_PPC64_DTPREL16_DS:
15801 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15802 case R_PPC64_DTPREL16_HIGH:
15803 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15804 case R_PPC64_DTPREL16_HIGHER:
15805 case R_PPC64_DTPREL16_HIGHERA:
15806 case R_PPC64_DTPREL16_HIGHEST:
15807 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15808 if (htab->elf.tls_sec != NULL)
15809 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15810 break;
15811
45965137
AM
15812 case R_PPC64_ADDR64_LOCAL:
15813 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15814 ? h->elf.other
15815 : sym->st_other);
15816 break;
15817
e515b051
AM
15818 case R_PPC64_DTPMOD64:
15819 relocation = 1;
15820 addend = 0;
15821 goto dodyn;
15822
411e1bfb 15823 case R_PPC64_TPREL64:
989f9879
AM
15824 if (htab->elf.tls_sec != NULL)
15825 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15826 goto dodyn;
15827
15828 case R_PPC64_DTPREL64:
989f9879
AM
15829 if (htab->elf.tls_sec != NULL)
15830 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15831 /* Fall through. */
411e1bfb 15832
65f38f15
AM
15833 /* Relocations that may need to be propagated if this is a
15834 dynamic object. */
04c9666a 15835 case R_PPC64_REL30:
65f38f15
AM
15836 case R_PPC64_REL32:
15837 case R_PPC64_REL64:
15838 case R_PPC64_ADDR14:
15839 case R_PPC64_ADDR14_BRNTAKEN:
15840 case R_PPC64_ADDR14_BRTAKEN:
15841 case R_PPC64_ADDR16:
15842 case R_PPC64_ADDR16_DS:
15843 case R_PPC64_ADDR16_HA:
15844 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15845 case R_PPC64_ADDR16_HIGH:
15846 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15847 case R_PPC64_ADDR16_HIGHER:
15848 case R_PPC64_ADDR16_HIGHERA:
15849 case R_PPC64_ADDR16_HIGHEST:
15850 case R_PPC64_ADDR16_HIGHESTA:
15851 case R_PPC64_ADDR16_LO:
15852 case R_PPC64_ADDR16_LO_DS:
15853 case R_PPC64_ADDR24:
65f38f15
AM
15854 case R_PPC64_ADDR32:
15855 case R_PPC64_ADDR64:
15856 case R_PPC64_UADDR16:
15857 case R_PPC64_UADDR32:
15858 case R_PPC64_UADDR64:
411e1bfb 15859 dodyn:
5d1634d7 15860 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15861 break;
15862
41bd81ab
AM
15863 if (NO_OPD_RELOCS && is_opd)
15864 break;
15865
8a9e8e72 15866 if (bfd_link_pic (info)
b1b07054
AM
15867 ? ((h == NULL
15868 || h->dyn_relocs != NULL)
15869 && ((h != NULL && pc_dynrelocs (h))
15870 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15871 : (h != NULL
15872 ? h->dyn_relocs != NULL
d311bc8b 15873 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15874 {
b34976b6 15875 bfd_boolean skip, relocate;
65f38f15 15876 asection *sreloc;
1cf1f670 15877 bfd_vma out_off;
82e66161 15878 long indx = 0;
65f38f15
AM
15879
15880 /* When generating a dynamic object, these relocations
15881 are copied into the output file to be resolved at run
15882 time. */
15883
b34976b6
AM
15884 skip = FALSE;
15885 relocate = FALSE;
65f38f15 15886
1cf1f670
AM
15887 out_off = _bfd_elf_section_offset (output_bfd, info,
15888 input_section, rel->r_offset);
15889 if (out_off == (bfd_vma) -1)
b34976b6 15890 skip = TRUE;
1cf1f670 15891 else if (out_off == (bfd_vma) -2)
b34976b6 15892 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15893 out_off += (input_section->output_section->vma
15894 + input_section->output_offset);
15895 outrel.r_offset = out_off;
411e1bfb 15896 outrel.r_addend = rel->r_addend;
65f38f15 15897
1cf1f670
AM
15898 /* Optimize unaligned reloc use. */
15899 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15900 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15901 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15902 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15903 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15904 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15905 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15906 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15907 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15908
65f38f15 15909 if (skip)
0bb2d96a 15910 memset (&outrel, 0, sizeof outrel);
afe397ea 15911 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15912 && !is_opd
15913 && r_type != R_PPC64_TOC)
14acf4dc 15914 {
82e66161
AM
15915 indx = h->elf.dynindx;
15916 BFD_ASSERT (indx != -1);
15917 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15918 }
65f38f15
AM
15919 else
15920 {
41bd81ab
AM
15921 /* This symbol is local, or marked to become local,
15922 or this is an opd section reloc which must point
15923 at a local function. */
65f38f15 15924 outrel.r_addend += relocation;
e86ce104 15925 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15926 {
3fad3c7c 15927 if (is_opd && h != NULL)
afbe61cf
AM
15928 {
15929 /* Lie about opd entries. This case occurs
15930 when building shared libraries and we
15931 reference a function in another shared
3fad3c7c
AM
15932 lib. The same thing happens for a weak
15933 definition in an application that's
15934 overridden by a strong definition in a
15935 shared lib. (I believe this is a generic
15936 bug in binutils handling of weak syms.)
15937 In these cases we won't use the opd
1e2f5b6e 15938 entry in this lib. */
b34976b6 15939 unresolved_reloc = FALSE;
afbe61cf 15940 }
25f23106
AM
15941 if (!is_opd
15942 && r_type == R_PPC64_ADDR64
15943 && (h != NULL
15944 ? h->elf.type == STT_GNU_IFUNC
15945 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15946 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15947 else
15948 {
15949 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 15950
25f23106
AM
15951 /* We need to relocate .opd contents for ld.so.
15952 Prelink also wants simple and consistent rules
15953 for relocs. This make all RELATIVE relocs have
15954 *r_offset equal to r_addend. */
15955 relocate = TRUE;
15956 }
65f38f15
AM
15957 }
15958 else
15959 {
25f23106
AM
15960 if (h != NULL
15961 ? h->elf.type == STT_GNU_IFUNC
15962 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15963 {
25f53a85 15964 info->callbacks->einfo
695344c0 15965 /* xgettext:c-format */
174d0a74 15966 (_("%H: %s for indirect "
c1c8c1ef 15967 "function `%pT' unsupported\n"),
25f53a85 15968 input_bfd, input_section, rel->r_offset,
25f23106
AM
15969 ppc64_elf_howto_table[r_type]->name,
15970 sym_name);
15971 ret = FALSE;
15972 }
cf35638d 15973 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
15974 ;
15975 else if (sec == NULL || sec->owner == NULL)
15976 {
15977 bfd_set_error (bfd_error_bad_value);
b34976b6 15978 return FALSE;
65f38f15
AM
15979 }
15980 else
15981 {
15982 asection *osec;
15983
15984 osec = sec->output_section;
15985 indx = elf_section_data (osec)->dynindx;
15986
74541ad4
AM
15987 if (indx == 0)
15988 {
15989 if ((osec->flags & SEC_READONLY) == 0
15990 && htab->elf.data_index_section != NULL)
15991 osec = htab->elf.data_index_section;
15992 else
15993 osec = htab->elf.text_index_section;
15994 indx = elf_section_data (osec)->dynindx;
15995 }
15996 BFD_ASSERT (indx != 0);
15997
65f38f15
AM
15998 /* We are turning this relocation into one
15999 against a section symbol, so subtract out
16000 the output section's address but not the
16001 offset of the input section in the output
16002 section. */
16003 outrel.r_addend -= osec->vma;
16004 }
16005
16006 outrel.r_info = ELF64_R_INFO (indx, r_type);
16007 }
16008 }
16009
16010 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16011 if (h != NULL
16012 ? h->elf.type == STT_GNU_IFUNC
16013 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16014 {
16015 sreloc = htab->elf.irelplt;
16016 if (indx == 0)
16017 htab->local_ifunc_resolver = 1;
16018 else if (is_static_defined (&h->elf))
16019 htab->maybe_local_ifunc_resolver = 1;
16020 }
65f38f15
AM
16021 if (sreloc == NULL)
16022 abort ();
16023
dfbb6ac9
AM
16024 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16025 >= sreloc->size)
16026 abort ();
947216bf
AM
16027 loc = sreloc->contents;
16028 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16029 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16030
16031 /* If this reloc is against an external symbol, it will
16032 be computed at runtime, so there's no need to do
81407a69
AM
16033 anything now. However, for the sake of prelink ensure
16034 that the section contents are a known value. */
65f38f15 16035 if (! relocate)
81407a69
AM
16036 {
16037 unresolved_reloc = FALSE;
16038 /* The value chosen here is quite arbitrary as ld.so
16039 ignores section contents except for the special
16040 case of .opd where the contents might be accessed
16041 before relocation. Choose zero, as that won't
16042 cause reloc overflow. */
16043 relocation = 0;
16044 addend = 0;
16045 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16046 to improve backward compatibility with older
16047 versions of ld. */
16048 if (r_type == R_PPC64_ADDR64)
16049 addend = outrel.r_addend;
16050 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16051 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16052 addend = outrel.r_offset;
81407a69 16053 }
65f38f15 16054 }
5bd4f169
AM
16055 break;
16056
65f38f15
AM
16057 case R_PPC64_COPY:
16058 case R_PPC64_GLOB_DAT:
16059 case R_PPC64_JMP_SLOT:
25f23106 16060 case R_PPC64_JMP_IREL:
65f38f15
AM
16061 case R_PPC64_RELATIVE:
16062 /* We shouldn't ever see these dynamic relocs in relocatable
16063 files. */
ae9a127f 16064 /* Fall through. */
65f38f15
AM
16065
16066 case R_PPC64_PLTGOT16:
16067 case R_PPC64_PLTGOT16_DS:
16068 case R_PPC64_PLTGOT16_HA:
16069 case R_PPC64_PLTGOT16_HI:
16070 case R_PPC64_PLTGOT16_LO:
16071 case R_PPC64_PLTGOT16_LO_DS:
16072 case R_PPC64_PLTREL32:
16073 case R_PPC64_PLTREL64:
16074 /* These ones haven't been implemented yet. */
16075
25f53a85 16076 info->callbacks->einfo
695344c0 16077 /* xgettext:c-format */
c1c8c1ef 16078 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16079 input_bfd,
4ce794b7 16080 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16081
16082 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16083 ret = FALSE;
c316a17c 16084 goto copy_reloc;
65f38f15 16085 }
5bd4f169 16086
67f0cbdb
AM
16087 /* Multi-instruction sequences that access the TOC can be
16088 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16089 to nop; addi rb,r2,x; */
67f0cbdb
AM
16090 switch (r_type)
16091 {
16092 default:
16093 break;
16094
16095 case R_PPC64_GOT_TLSLD16_HI:
16096 case R_PPC64_GOT_TLSGD16_HI:
16097 case R_PPC64_GOT_TPREL16_HI:
16098 case R_PPC64_GOT_DTPREL16_HI:
16099 case R_PPC64_GOT16_HI:
16100 case R_PPC64_TOC16_HI:
16101 /* These relocs would only be useful if building up an
16102 offset to later add to r2, perhaps in an indexed
16103 addressing mode instruction. Don't try to optimize.
16104 Unfortunately, the possibility of someone building up an
16105 offset like this or even with the HA relocs, means that
16106 we need to check the high insn when optimizing the low
16107 insn. */
16108 break;
16109
23cedd1d
AM
16110 case R_PPC64_PLTCALL:
16111 if (unresolved_reloc)
16112 {
16113 /* No plt entry. Make this into a direct call. */
16114 bfd_byte *p = contents + rel->r_offset;
16115 insn = bfd_get_32 (input_bfd, p);
16116 insn &= 1;
16117 bfd_put_32 (input_bfd, B_DOT | insn, p);
16118 bfd_put_32 (input_bfd, NOP, p + 4);
16119 unresolved_reloc = save_unresolved_reloc;
16120 r_type = R_PPC64_REL24;
16121 }
16122 break;
16123
16124 case R_PPC64_PLTSEQ:
16125 if (unresolved_reloc)
16126 {
16127 unresolved_reloc = FALSE;
16128 goto nop_it;
16129 }
16130 break;
16131
16132 case R_PPC64_PLT16_HA:
16133 if (unresolved_reloc)
16134 {
16135 unresolved_reloc = FALSE;
16136 goto nop_it;
16137 }
16138 /* Fall through. */
67f0cbdb
AM
16139 case R_PPC64_GOT_TLSLD16_HA:
16140 case R_PPC64_GOT_TLSGD16_HA:
16141 case R_PPC64_GOT_TPREL16_HA:
16142 case R_PPC64_GOT_DTPREL16_HA:
16143 case R_PPC64_GOT16_HA:
16144 case R_PPC64_TOC16_HA:
98528052 16145 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16146 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16147 {
23cedd1d
AM
16148 bfd_byte *p;
16149 nop_it:
16150 p = contents + (rel->r_offset & ~3);
98528052 16151 bfd_put_32 (input_bfd, NOP, p);
d830549d 16152 goto copy_reloc;
98528052 16153 }
67f0cbdb
AM
16154 break;
16155
23cedd1d
AM
16156 case R_PPC64_PLT16_LO:
16157 case R_PPC64_PLT16_LO_DS:
16158 if (unresolved_reloc)
16159 {
16160 unresolved_reloc = FALSE;
16161 goto nop_it;
16162 }
16163 /* Fall through. */
67f0cbdb
AM
16164 case R_PPC64_GOT_TLSLD16_LO:
16165 case R_PPC64_GOT_TLSGD16_LO:
16166 case R_PPC64_GOT_TPREL16_LO_DS:
16167 case R_PPC64_GOT_DTPREL16_LO_DS:
16168 case R_PPC64_GOT16_LO:
16169 case R_PPC64_GOT16_LO_DS:
16170 case R_PPC64_TOC16_LO:
16171 case R_PPC64_TOC16_LO_DS:
98528052 16172 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16173 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16174 {
16175 bfd_byte *p = contents + (rel->r_offset & ~3);
16176 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16177 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16178 {
16179 /* Transform addic to addi when we change reg. */
16180 insn &= ~((0x3f << 26) | (0x1f << 16));
16181 insn |= (14u << 26) | (2 << 16);
16182 }
16183 else
67f0cbdb 16184 {
98528052
AM
16185 insn &= ~(0x1f << 16);
16186 insn |= 2 << 16;
67f0cbdb 16187 }
560c8763 16188 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16189 }
16190 break;
9a23f96e
AM
16191
16192 case R_PPC64_TPREL16_HA:
16193 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16194 {
16195 bfd_byte *p = contents + (rel->r_offset & ~3);
16196 insn = bfd_get_32 (input_bfd, p);
16197 if ((insn & ((0x3f << 26) | 0x1f << 16))
16198 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16199 /* xgettext:c-format */
16200 info->callbacks->minfo
16201 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16202 input_bfd, input_section, rel->r_offset,
16203 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16204 else
d830549d
AM
16205 {
16206 bfd_put_32 (input_bfd, NOP, p);
16207 goto copy_reloc;
16208 }
9a23f96e
AM
16209 }
16210 break;
16211
16212 case R_PPC64_TPREL16_LO:
16213 case R_PPC64_TPREL16_LO_DS:
16214 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16215 {
16216 bfd_byte *p = contents + (rel->r_offset & ~3);
16217 insn = bfd_get_32 (input_bfd, p);
16218 insn &= ~(0x1f << 16);
16219 insn |= 13 << 16;
16220 bfd_put_32 (input_bfd, insn, p);
16221 }
16222 break;
67f0cbdb
AM
16223 }
16224
65f38f15
AM
16225 /* Do any further special processing. */
16226 switch (r_type)
16227 {
16228 default:
16229 break;
16230
25f23106 16231 case R_PPC64_REL16_HA:
a680de9a 16232 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16233 case R_PPC64_ADDR16_HA:
16234 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16235 case R_PPC64_ADDR16_HIGHERA:
16236 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16237 case R_PPC64_TOC16_HA:
16238 case R_PPC64_SECTOFF_HA:
411e1bfb 16239 case R_PPC64_TPREL16_HA:
f9c6b907 16240 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16241 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16242 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16243 case R_PPC64_DTPREL16_HA:
16244 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16245 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16246 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16247 /* It's just possible that this symbol is a weak symbol
16248 that's not actually defined anywhere. In that case,
16249 'sec' would be NULL, and we should leave the symbol
16250 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16251 if (sec == NULL)
16252 break;
1a0670f3 16253 /* Fall through. */
5c5f6e17
AM
16254
16255 case R_PPC64_GOT16_HA:
16256 case R_PPC64_PLTGOT16_HA:
16257 case R_PPC64_PLT16_HA:
16258 case R_PPC64_GOT_TLSGD16_HA:
16259 case R_PPC64_GOT_TLSLD16_HA:
16260 case R_PPC64_GOT_TPREL16_HA:
16261 case R_PPC64_GOT_DTPREL16_HA:
16262 /* Add 0x10000 if sign bit in 0:15 is set.
16263 Bits 0:15 are not used. */
16264 addend += 0x8000;
65f38f15
AM
16265 break;
16266
16267 case R_PPC64_ADDR16_DS:
16268 case R_PPC64_ADDR16_LO_DS:
16269 case R_PPC64_GOT16_DS:
16270 case R_PPC64_GOT16_LO_DS:
16271 case R_PPC64_PLT16_LO_DS:
16272 case R_PPC64_SECTOFF_DS:
16273 case R_PPC64_SECTOFF_LO_DS:
16274 case R_PPC64_TOC16_DS:
16275 case R_PPC64_TOC16_LO_DS:
16276 case R_PPC64_PLTGOT16_DS:
16277 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16278 case R_PPC64_GOT_TPREL16_DS:
16279 case R_PPC64_GOT_TPREL16_LO_DS:
16280 case R_PPC64_GOT_DTPREL16_DS:
16281 case R_PPC64_GOT_DTPREL16_LO_DS:
16282 case R_PPC64_TPREL16_DS:
16283 case R_PPC64_TPREL16_LO_DS:
16284 case R_PPC64_DTPREL16_DS:
16285 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16286 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16287 mask = 3;
a680de9a
PB
16288 /* If this reloc is against an lq, lxv, or stxv insn, then
16289 the value must be a multiple of 16. This is somewhat of
16290 a hack, but the "correct" way to do this by defining _DQ
16291 forms of all the _DS relocs bloats all reloc switches in
16292 this file. It doesn't make much sense to use these
16293 relocs in data, so testing the insn should be safe. */
16294 if ((insn & (0x3f << 26)) == (56u << 26)
16295 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16296 mask = 15;
a680de9a
PB
16297 relocation += addend;
16298 addend = insn & (mask ^ 3);
16299 if ((relocation & mask) != 0)
65f38f15 16300 {
a680de9a 16301 relocation ^= relocation & mask;
25f53a85 16302 info->callbacks->einfo
695344c0 16303 /* xgettext:c-format */
174d0a74 16304 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16305 input_bfd, input_section, rel->r_offset,
d830549d 16306 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16307 mask + 1);
65f38f15 16308 bfd_set_error (bfd_error_bad_value);
b34976b6 16309 ret = FALSE;
c316a17c 16310 goto copy_reloc;
65f38f15
AM
16311 }
16312 break;
5bd4f169
AM
16313 }
16314
239e1f3a
AM
16315 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16316 because such sections are not SEC_ALLOC and thus ld.so will
16317 not process them. */
d830549d 16318 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16319 if (unresolved_reloc
239e1f3a 16320 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16321 && h->elf.def_dynamic)
16322 && _bfd_elf_section_offset (output_bfd, info, input_section,
16323 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16324 {
25f53a85 16325 info->callbacks->einfo
695344c0 16326 /* xgettext:c-format */
c1c8c1ef 16327 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16328 input_bfd, input_section, rel->r_offset,
b80eed39 16329 howto->name,
039b3fef 16330 h->elf.root.root.string);
b34976b6 16331 ret = FALSE;
9c07fe7c 16332 }
5bd4f169 16333
b80eed39
AM
16334 /* 16-bit fields in insns mostly have signed values, but a
16335 few insns have 16-bit unsigned values. Really, we should
16336 have different reloc types. */
16337 if (howto->complain_on_overflow != complain_overflow_dont
16338 && howto->dst_mask == 0xffff
16339 && (input_section->flags & SEC_CODE) != 0)
16340 {
16341 enum complain_overflow complain = complain_overflow_signed;
16342
16343 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16344 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16345 complain = complain_overflow_bitfield;
16346 else if (howto->rightshift == 0
16347 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16348 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16349 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16350 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16351 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16352 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16353 complain = complain_overflow_unsigned;
16354 if (howto->complain_on_overflow != complain)
16355 {
16356 alt_howto = *howto;
16357 alt_howto.complain_on_overflow = complain;
16358 howto = &alt_howto;
16359 }
16360 }
16361
a680de9a
PB
16362 if (r_type == R_PPC64_REL16DX_HA)
16363 {
16364 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
16365 if (rel->r_offset + 4 > input_section->size)
16366 r = bfd_reloc_outofrange;
16367 else
16368 {
16369 relocation += addend;
16370 relocation -= (rel->r_offset
16371 + input_section->output_offset
16372 + input_section->output_section->vma);
3de43e7b 16373 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16374 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16375 insn &= ~0x1fffc1;
3de43e7b 16376 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16377 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16378 r = bfd_reloc_ok;
3de43e7b 16379 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16380 r = bfd_reloc_overflow;
16381 }
16382 }
16383 else
16384 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
16385 rel->r_offset, relocation, addend);
5bd4f169 16386
ef60b7ff 16387 if (r != bfd_reloc_ok)
5bd4f169 16388 {
bc30df16 16389 char *more_info = NULL;
b80eed39 16390 const char *reloc_name = howto->name;
bc30df16
AM
16391
16392 if (reloc_dest != DEST_NORMAL)
16393 {
16394 more_info = bfd_malloc (strlen (reloc_name) + 8);
16395 if (more_info != NULL)
16396 {
16397 strcpy (more_info, reloc_name);
16398 strcat (more_info, (reloc_dest == DEST_OPD
16399 ? " (OPD)" : " (stub)"));
16400 reloc_name = more_info;
16401 }
16402 }
16403
cd27b276 16404 if (r == bfd_reloc_overflow)
5bd4f169 16405 {
8131c122
AM
16406 /* On code like "if (foo) foo();" don't report overflow
16407 on a branch to zero when foo is undefined. */
16408 if (!warned
16409 && (reloc_dest == DEST_STUB
16410 || !(h != NULL
16411 && (h->elf.root.type == bfd_link_hash_undefweak
16412 || h->elf.root.type == bfd_link_hash_undefined)
16413 && is_branch_reloc (r_type))))
1a72702b
AM
16414 info->callbacks->reloc_overflow (info, &h->elf.root,
16415 sym_name, reloc_name,
16416 orig_rel.r_addend,
16417 input_bfd, input_section,
16418 rel->r_offset);
ef60b7ff
AM
16419 }
16420 else
16421 {
25f53a85 16422 info->callbacks->einfo
695344c0 16423 /* xgettext:c-format */
c1c8c1ef 16424 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16425 input_bfd, input_section, rel->r_offset,
bc30df16 16426 reloc_name, sym_name, (int) r);
b34976b6 16427 ret = FALSE;
ef60b7ff 16428 }
bc30df16
AM
16429 if (more_info != NULL)
16430 free (more_info);
5bd4f169 16431 }
c316a17c
AM
16432 copy_reloc:
16433 if (wrel != rel)
16434 *wrel = *rel;
16435 }
16436
16437 if (wrel != rel)
16438 {
16439 Elf_Internal_Shdr *rel_hdr;
16440 size_t deleted = rel - wrel;
16441
16442 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16443 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16444 if (rel_hdr->sh_size == 0)
16445 {
16446 /* It is too late to remove an empty reloc section. Leave
16447 one NONE reloc.
16448 ??? What is wrong with an empty section??? */
16449 rel_hdr->sh_size = rel_hdr->sh_entsize;
16450 deleted -= 1;
16451 }
16452 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16453 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16454 input_section->reloc_count -= deleted;
5bd4f169
AM
16455 }
16456
645ea6a9
AM
16457 /* If we're emitting relocations, then shortly after this function
16458 returns, reloc offsets and addends for this section will be
16459 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16460 file rather than the input. Save a copy of the relocs for
16461 opd_entry_value. */
0e1862bb 16462 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16463 {
16464 bfd_size_type amt;
16465 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16466 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16467 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16468 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16469 if (rel == NULL)
16470 return FALSE;
16471 memcpy (rel, relocs, amt);
16472 }
5bd4f169
AM
16473 return ret;
16474}
16475
754021d0
AM
16476/* Adjust the value of any local symbols in opd sections. */
16477
6e0b88f1 16478static int
754021d0
AM
16479ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16480 const char *name ATTRIBUTE_UNUSED,
16481 Elf_Internal_Sym *elfsym,
16482 asection *input_sec,
16483 struct elf_link_hash_entry *h)
16484{
74f0fb50
AM
16485 struct _opd_sec_data *opd;
16486 long adjust;
754021d0
AM
16487 bfd_vma value;
16488
4025353c 16489 if (h != NULL)
6e0b88f1 16490 return 1;
4025353c 16491
74f0fb50
AM
16492 opd = get_opd_info (input_sec);
16493 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16494 return 1;
754021d0
AM
16495
16496 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16497 if (!bfd_link_relocatable (info))
754021d0
AM
16498 value -= input_sec->output_section->vma;
16499
51aecdc5 16500 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16501 if (adjust == -1)
6e0b88f1
AM
16502 return 2;
16503
16504 elfsym->st_value += adjust;
16505 return 1;
754021d0
AM
16506}
16507
5bd4f169
AM
16508/* Finish up dynamic symbol handling. We set the contents of various
16509 dynamic sections here. */
16510
b34976b6 16511static bfd_boolean
4ce794b7
AM
16512ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16513 struct bfd_link_info *info,
16514 struct elf_link_hash_entry *h,
4aef7643 16515 Elf_Internal_Sym *sym)
5bd4f169 16516{
65f38f15 16517 struct ppc_link_hash_table *htab;
8387904d 16518 struct plt_entry *ent;
5bd4f169 16519
65f38f15 16520 htab = ppc_hash_table (info);
4dfe6ac6
NC
16521 if (htab == NULL)
16522 return FALSE;
5bd4f169 16523
49c09209
AM
16524 if (!htab->opd_abi && !h->def_regular)
16525 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16526 if (ent->plt.offset != (bfd_vma) -1)
16527 {
16528 /* Mark the symbol as undefined, rather than as
16529 defined in glink. Leave the value if there were
16530 any relocations where pointer equality matters
16531 (this is a clue for the dynamic linker, to make
16532 function pointer comparisons work between an
16533 application and shared library), otherwise set it
16534 to zero. */
16535 sym->st_shndx = SHN_UNDEF;
16536 if (!h->pointer_equality_needed)
16537 sym->st_value = 0;
16538 else if (!h->ref_regular_nonweak)
16539 {
16540 /* This breaks function pointer comparisons, but
16541 that is better than breaking tests for a NULL
16542 function pointer. */
16543 sym->st_value = 0;
16544 }
16545 break;
16546 }
5bd4f169 16547
f5385ebf 16548 if (h->needs_copy)
5bd4f169 16549 {
65f38f15 16550 /* This symbol needs a copy reloc. Set it up. */
49c09209 16551 Elf_Internal_Rela rela;
5474d94f 16552 asection *srel;
49c09209 16553 bfd_byte *loc;
5bd4f169 16554
65f38f15
AM
16555 if (h->dynindx == -1
16556 || (h->root.type != bfd_link_hash_defined
16557 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16558 || htab->elf.srelbss == NULL
16559 || htab->elf.sreldynrelro == NULL)
65f38f15 16560 abort ();
5bd4f169
AM
16561
16562 rela.r_offset = (h->root.u.def.value
16563 + h->root.u.def.section->output_section->vma
16564 + h->root.u.def.section->output_offset);
16565 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16566 rela.r_addend = 0;
afbf7e8e 16567 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16568 srel = htab->elf.sreldynrelro;
16569 else
16570 srel = htab->elf.srelbss;
16571 loc = srel->contents;
16572 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16573 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16574 }
16575
b34976b6 16576 return TRUE;
5bd4f169
AM
16577}
16578
65f38f15
AM
16579/* Used to decide how to sort relocs in an optimal manner for the
16580 dynamic linker, before writing them out. */
16581
16582static enum elf_reloc_type_class
7e612e98
AM
16583ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16584 const asection *rel_sec,
16585 const Elf_Internal_Rela *rela)
65f38f15 16586{
04c9666a 16587 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16588 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16589
33e44f2e 16590 if (rel_sec == htab->elf.irelplt)
7e612e98 16591 return reloc_class_ifunc;
a33d1f77 16592
4ce794b7 16593 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16594 switch (r_type)
65f38f15
AM
16595 {
16596 case R_PPC64_RELATIVE:
16597 return reloc_class_relative;
16598 case R_PPC64_JMP_SLOT:
16599 return reloc_class_plt;
16600 case R_PPC64_COPY:
16601 return reloc_class_copy;
16602 default:
16603 return reloc_class_normal;
16604 }
16605}
16606
5bd4f169
AM
16607/* Finish up the dynamic sections. */
16608
b34976b6 16609static bfd_boolean
4ce794b7
AM
16610ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16611 struct bfd_link_info *info)
5bd4f169 16612{
65f38f15
AM
16613 struct ppc_link_hash_table *htab;
16614 bfd *dynobj;
5bd4f169 16615 asection *sdyn;
5bd4f169 16616
65f38f15 16617 htab = ppc_hash_table (info);
4dfe6ac6
NC
16618 if (htab == NULL)
16619 return FALSE;
16620
65f38f15 16621 dynobj = htab->elf.dynobj;
3d4d4302 16622 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16623
65f38f15 16624 if (htab->elf.dynamic_sections_created)
5bd4f169 16625 {
5bd4f169
AM
16626 Elf64_External_Dyn *dyncon, *dynconend;
16627
33e44f2e 16628 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16629 abort ();
5bd4f169
AM
16630
16631 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16632 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16633 for (; dyncon < dynconend; dyncon++)
16634 {
16635 Elf_Internal_Dyn dyn;
19397422 16636 asection *s;
5bd4f169
AM
16637
16638 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16639
16640 switch (dyn.d_tag)
16641 {
65f38f15
AM
16642 default:
16643 continue;
5bd4f169 16644
5d1634d7 16645 case DT_PPC64_GLINK:
4ce794b7 16646 s = htab->glink;
6348e046 16647 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16648 /* We stupidly defined DT_PPC64_GLINK to be the start
16649 of glink rather than the first entry point, which is
16650 what ld.so needs, and now have a bigger stub to
16651 support automatic multiple TOCs. */
9e390558 16652 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16653 break;
16654
19397422
AM
16655 case DT_PPC64_OPD:
16656 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16657 if (s == NULL)
16658 continue;
16659 dyn.d_un.d_ptr = s->vma;
19397422
AM
16660 break;
16661
e8910a83
AM
16662 case DT_PPC64_OPT:
16663 if (htab->do_multi_toc && htab->multi_toc_needed)
16664 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16665 if (htab->has_plt_localentry0)
16666 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16667 break;
16668
19397422
AM
16669 case DT_PPC64_OPDSZ:
16670 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16671 if (s == NULL)
16672 continue;
eea6121a 16673 dyn.d_un.d_val = s->size;
19397422
AM
16674 break;
16675
65f38f15 16676 case DT_PLTGOT:
33e44f2e 16677 s = htab->elf.splt;
6348e046 16678 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16679 break;
16680
16681 case DT_JMPREL:
33e44f2e 16682 s = htab->elf.srelplt;
6348e046 16683 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16684 break;
5bd4f169 16685
65f38f15 16686 case DT_PLTRELSZ:
33e44f2e 16687 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16688 break;
82e66161
AM
16689
16690 case DT_TEXTREL:
16691 if (htab->local_ifunc_resolver)
16692 info->callbacks->einfo
16693 (_("%X%P: text relocations and GNU indirect "
16694 "functions will result in a segfault at runtime\n"));
16695 else if (htab->maybe_local_ifunc_resolver)
16696 info->callbacks->einfo
16697 (_("%P: warning: text relocations and GNU indirect "
16698 "functions may result in a segfault at runtime\n"));
16699 continue;
5bd4f169 16700 }
5bd4f169 16701
65f38f15 16702 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16703 }
5bd4f169
AM
16704 }
16705
6528b6eb
AM
16706 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16707 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16708 {
16709 /* Fill in the first entry in the global offset table.
16710 We use it to hold the link-time TOCbase. */
16711 bfd_put_64 (output_bfd,
60ee0d4a 16712 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16713 htab->elf.sgot->contents);
5d1634d7
AM
16714
16715 /* Set .got entry size. */
33e44f2e 16716 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
16717 }
16718
6528b6eb
AM
16719 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16720 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16721 {
16722 /* Set .plt entry size. */
33e44f2e 16723 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16724 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16725 }
16726
84f5d08e
AM
16727 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16728 brlt ourselves if emitrelocations. */
16729 if (htab->brlt != NULL
16730 && htab->brlt->reloc_count != 0
16731 && !_bfd_elf_link_output_relocs (output_bfd,
16732 htab->brlt,
d4730f92 16733 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16734 elf_section_data (htab->brlt)->relocs,
16735 NULL))
16736 return FALSE;
16737
176a0d42
AM
16738 if (htab->glink != NULL
16739 && htab->glink->reloc_count != 0
16740 && !_bfd_elf_link_output_relocs (output_bfd,
16741 htab->glink,
d4730f92 16742 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16743 elf_section_data (htab->glink)->relocs,
16744 NULL))
16745 return FALSE;
16746
58d180e8 16747 if (htab->glink_eh_frame != NULL
da44f4e5
AM
16748 && htab->glink_eh_frame->size != 0)
16749 {
16750 bfd_vma val;
16751 bfd_byte *p;
d4aaa2a0 16752 struct map_stub *group;
2e0ce1c8 16753 size_t align = 4;
da44f4e5 16754
2e0ce1c8
AM
16755 p = htab->glink_eh_frame->contents;
16756 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
d4aaa2a0
AM
16757
16758 for (group = htab->group; group != NULL; group = group->next)
16759 if (group->stub_sec != NULL)
da44f4e5 16760 {
da44f4e5 16761 /* Offset to stub section. */
d4aaa2a0
AM
16762 val = (group->stub_sec->output_section->vma
16763 + group->stub_sec->output_offset);
da44f4e5
AM
16764 val -= (htab->glink_eh_frame->output_section->vma
16765 + htab->glink_eh_frame->output_offset
d4aaa2a0 16766 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
16767 if (val + 0x80000000 > 0xffffffff)
16768 {
cf97bcb0
AM
16769 _bfd_error_handler
16770 (_("%s offset too large for .eh_frame sdata4 encoding"),
d4aaa2a0 16771 group->stub_sec->name);
da44f4e5
AM
16772 return FALSE;
16773 }
d4aaa2a0
AM
16774 bfd_put_32 (dynobj, val, p + 8);
16775 p += stub_eh_frame_size (group, align);
da44f4e5
AM
16776 }
16777 if (htab->glink != NULL && htab->glink->size != 0)
16778 {
da44f4e5
AM
16779 /* Offset to .glink. */
16780 val = (htab->glink->output_section->vma
16781 + htab->glink->output_offset
16782 + 8);
16783 val -= (htab->glink_eh_frame->output_section->vma
16784 + htab->glink_eh_frame->output_offset
d4aaa2a0 16785 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
16786 if (val + 0x80000000 > 0xffffffff)
16787 {
cf97bcb0
AM
16788 _bfd_error_handler
16789 (_("%s offset too large for .eh_frame sdata4 encoding"),
da44f4e5
AM
16790 htab->glink->name);
16791 return FALSE;
16792 }
d4aaa2a0
AM
16793 bfd_put_32 (dynobj, val, p + 8);
16794 p += (24 + align - 1) & -align;
da44f4e5
AM
16795 }
16796
16797 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16798 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16799 htab->glink_eh_frame,
16800 htab->glink_eh_frame->contents))
16801 return FALSE;
16802 }
58d180e8 16803
e717da7e 16804 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16805 since we didn't add them to DYNOBJ. We know dynobj is the first
16806 bfd. */
c72f2fb2 16807 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16808 {
16809 asection *s;
7b53ace3 16810
0c8d6e5c 16811 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16812 continue;
16813
e717da7e
AM
16814 s = ppc64_elf_tdata (dynobj)->got;
16815 if (s != NULL
eea6121a 16816 && s->size != 0
e717da7e
AM
16817 && s->output_section != bfd_abs_section_ptr
16818 && !bfd_set_section_contents (output_bfd, s->output_section,
16819 s->contents, s->output_offset,
eea6121a 16820 s->size))
e717da7e
AM
16821 return FALSE;
16822 s = ppc64_elf_tdata (dynobj)->relgot;
16823 if (s != NULL
eea6121a 16824 && s->size != 0
e717da7e
AM
16825 && s->output_section != bfd_abs_section_ptr
16826 && !bfd_set_section_contents (output_bfd, s->output_section,
16827 s->contents, s->output_offset,
eea6121a 16828 s->size))
e717da7e
AM
16829 return FALSE;
16830 }
f6c52c13 16831
b34976b6 16832 return TRUE;
5bd4f169
AM
16833}
16834
5bd4f169 16835#include "elf64-target.h"
7b8e7dad
AM
16836
16837/* FreeBSD support */
16838
16839#undef TARGET_LITTLE_SYM
16840#undef TARGET_LITTLE_NAME
16841
16842#undef TARGET_BIG_SYM
6d00b590 16843#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16844#undef TARGET_BIG_NAME
16845#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16846
16847#undef ELF_OSABI
16848#define ELF_OSABI ELFOSABI_FREEBSD
16849
16850#undef elf64_bed
16851#define elf64_bed elf64_powerpc_fbsd_bed
16852
16853#include "elf64-target.h"
This page took 2.989713 seconds and 4 git commands to generate.