Pack reloc_howto_struct
[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 */
2cdcc330
AM
2305 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
2306 (int) code);
f3185997 2307 bfd_set_error (bfd_error_bad_value);
4ce794b7 2308 return NULL;
5bd4f169 2309
411e1bfb
AM
2310 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2311 break;
2312 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2313 break;
2314 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2315 break;
2316 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2317 break;
2318 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2319 break;
2320 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2321 break;
f9c6b907
AM
2322 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2323 break;
411e1bfb 2324 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2325 break;
f9c6b907
AM
2326 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2327 break;
411e1bfb 2328 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2329 break;
411e1bfb 2330 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2331 break;
411e1bfb 2332 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2333 break;
411e1bfb 2334 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2335 break;
05d0e962
AM
2336 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
2337 break;
411e1bfb 2338 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2339 break;
411e1bfb 2340 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2341 break;
411e1bfb 2342 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2343 break;
411e1bfb 2344 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2345 break;
411e1bfb 2346 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2347 break;
411e1bfb 2348 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2349 break;
411e1bfb 2350 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2351 break;
411e1bfb 2352 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2353 break;
411e1bfb 2354 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2355 break;
411e1bfb 2356 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2357 break;
411e1bfb 2358 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2359 break;
411e1bfb 2360 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2361 break;
411e1bfb 2362 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2363 break;
411e1bfb 2364 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2365 break;
411e1bfb 2366 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2367 break;
411e1bfb 2368 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2369 break;
411e1bfb 2370 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2371 break;
411e1bfb 2372 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2373 break;
411e1bfb 2374 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2375 break;
411e1bfb 2376 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2377 break;
411e1bfb 2378 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2379 break;
411e1bfb 2380 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2381 break;
411e1bfb 2382 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2383 break;
411e1bfb 2384 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2385 break;
411e1bfb 2386 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2387 break;
411e1bfb 2388 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2389 break;
411e1bfb 2390 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2391 break;
411e1bfb 2392 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2393 break;
411e1bfb 2394 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2395 break;
411e1bfb 2396 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2397 break;
411e1bfb 2398 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2399 break;
411e1bfb 2400 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2401 break;
411e1bfb 2402 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2403 break;
411e1bfb 2404 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2405 break;
411e1bfb 2406 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2407 break;
411e1bfb 2408 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2409 break;
411e1bfb 2410 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2411 break;
411e1bfb 2412 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2413 break;
411e1bfb 2414 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2415 break;
411e1bfb 2416 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2417 break;
411e1bfb 2418 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2419 break;
411e1bfb 2420 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2421 break;
411e1bfb 2422 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2423 break;
411e1bfb 2424 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2425 break;
411e1bfb 2426 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2427 break;
411e1bfb 2428 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2429 break;
411e1bfb 2430 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2431 break;
411e1bfb 2432 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2433 break;
411e1bfb 2434 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2435 break;
727fc41e
AM
2436 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2437 break;
2438 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2439 break;
411e1bfb 2440 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2441 break;
411e1bfb 2442 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2443 break;
411e1bfb 2444 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2445 break;
411e1bfb 2446 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2447 break;
f9c6b907
AM
2448 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2449 break;
411e1bfb 2450 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2451 break;
f9c6b907
AM
2452 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2453 break;
411e1bfb 2454 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2455 break;
411e1bfb
AM
2456 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2457 break;
2458 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2459 break;
2460 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2461 break;
f9c6b907
AM
2462 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2463 break;
411e1bfb
AM
2464 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2465 break;
f9c6b907
AM
2466 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2467 break;
411e1bfb
AM
2468 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2469 break;
2470 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2471 break;
2472 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2473 break;
2474 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2475 break;
2476 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2477 break;
2478 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2479 break;
2480 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2481 break;
2482 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2483 break;
2484 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2485 break;
2486 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2487 break;
2488 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2489 break;
2490 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2491 break;
2492 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2493 break;
2494 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2495 break;
2496 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2497 break;
2498 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2499 break;
2500 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2501 break;
2502 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2503 break;
2504 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2505 break;
2506 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2507 break;
2508 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2509 break;
2510 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2511 break;
2512 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2513 break;
2514 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2515 break;
2516 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2517 break;
2518 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2519 break;
2520 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2521 break;
2522 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2523 break;
2524 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2525 break;
25f23106
AM
2526 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2527 break;
2528 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2529 break;
2530 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2531 break;
2532 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2533 break;
7ba71655
AM
2534 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2535 break;
a680de9a
PB
2536 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2537 break;
006589cf
AM
2538 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2539 break;
45965137
AM
2540 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2541 break;
411e1bfb
AM
2542 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2543 break;
2544 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2545 break;
2546 }
2547
4ce794b7 2548 return ppc64_elf_howto_table[r];
5bd4f169
AM
2549};
2550
157090f7
AM
2551static reloc_howto_type *
2552ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2553 const char *r_name)
2554{
2555 unsigned int i;
2556
a4b6fadd 2557 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2558 if (ppc64_elf_howto_raw[i].name != NULL
2559 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2560 return &ppc64_elf_howto_raw[i];
2561
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 }
2cdcc330 2595
f3185997 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
2cdcc330
AM
2916 {
2917 bfd_signed_vma refcount;
2918 bfd_vma offset;
2919 struct got_entry *ent;
2920 } got;
927be08e
AM
2921};
2922
2923/* The same for PLT. */
2924struct plt_entry
2925{
2926 struct plt_entry *next;
2927
2928 bfd_vma addend;
2929
2930 union
2cdcc330
AM
2931 {
2932 bfd_signed_vma refcount;
2933 bfd_vma offset;
2934 } plt;
927be08e
AM
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
2cdcc330
AM
2953 union
2954 {
729eabd5
AM
2955 /* A copy of relocs before they are modified for --emit-relocs. */
2956 Elf_Internal_Rela *relocs;
2957
2958 /* Section contents. */
2959 bfd_byte *contents;
2960 } opd;
d77c8a4b
AM
2961
2962 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2963 the reloc to be in the range -32768 to 32767. */
98528052
AM
2964 unsigned int has_small_toc_reloc : 1;
2965
560c8763
AM
2966 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2967 instruction not one we handle. */
2968 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2969};
2970
2971#define ppc64_elf_tdata(bfd) \
2972 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2973
2974#define ppc64_tlsld_got(bfd) \
2975 (&ppc64_elf_tdata (bfd)->tlsld_got)
2976
0c8d6e5c
AM
2977#define is_ppc64_elf(bfd) \
2978 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2979 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2980
e717da7e
AM
2981/* Override the generic function because we store some extras. */
2982
2983static bfd_boolean
2984ppc64_elf_mkobject (bfd *abfd)
2985{
0ffa91dd 2986 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2987 PPC64_ELF_DATA);
e717da7e
AM
2988}
2989
feee612b 2990/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2991 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2992
b34976b6 2993static bfd_boolean
4ce794b7 2994ppc64_elf_object_p (bfd *abfd)
feee612b 2995{
14b57c7c
AM
2996 if (!abfd->arch_info->the_default)
2997 return TRUE;
2998
2999 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
3000 {
3001 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
3002
3003 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
3004 {
3005 /* Relies on arch after 32 bit default being 64 bit default. */
3006 abfd->arch_info = abfd->arch_info->next;
3007 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
3008 }
3009 }
14b57c7c 3010 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
3011}
3012
d37c89e5
AM
3013/* Support for core dump NOTE sections. */
3014
3015static bfd_boolean
3016ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3017{
eea6121a 3018 size_t offset, size;
d37c89e5
AM
3019
3020 if (note->descsz != 504)
3021 return FALSE;
3022
3023 /* pr_cursig */
228e534f 3024 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
3025
3026 /* pr_pid */
228e534f 3027 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
3028
3029 /* pr_reg */
3030 offset = 112;
eea6121a 3031 size = 384;
d37c89e5
AM
3032
3033 /* Make a ".reg/999" section. */
3034 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 3035 size, note->descpos + offset);
d37c89e5
AM
3036}
3037
3038static bfd_boolean
3039ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3040{
3041 if (note->descsz != 136)
3042 return FALSE;
3043
228e534f 3044 elf_tdata (abfd)->core->pid
bc989cdc 3045 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 3046 elf_tdata (abfd)->core->program
d37c89e5 3047 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 3048 elf_tdata (abfd)->core->command
d37c89e5
AM
3049 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3050
3051 return TRUE;
3052}
3053
183e98be
AM
3054static char *
3055ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3056 ...)
3057{
3058 switch (note_type)
3059 {
3060 default:
3061 return NULL;
3062
3063 case NT_PRPSINFO:
3064 {
9ef6d1e3 3065 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
3066 va_list ap;
3067
3068 va_start (ap, note_type);
75cd47ed 3069 memset (data, 0, sizeof (data));
183e98be 3070 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 3071#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 3072 DIAGNOSTIC_PUSH;
be3e27bb 3073 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
3074 -Wstringop-truncation:
3075 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3076 */
95da9854
L
3077 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3078#endif
183e98be 3079 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 3080#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 3081 DIAGNOSTIC_POP;
fe75810f 3082#endif
183e98be
AM
3083 va_end (ap);
3084 return elfcore_write_note (abfd, buf, bufsiz,
3085 "CORE", note_type, data, sizeof (data));
3086 }
3087
3088 case NT_PRSTATUS:
3089 {
3090 char data[504];
3091 va_list ap;
3092 long pid;
3093 int cursig;
3094 const void *greg;
3095
3096 va_start (ap, note_type);
3097 memset (data, 0, 112);
3098 pid = va_arg (ap, long);
3099 bfd_put_32 (abfd, pid, data + 32);
3100 cursig = va_arg (ap, int);
3101 bfd_put_16 (abfd, cursig, data + 12);
3102 greg = va_arg (ap, const void *);
3103 memcpy (data + 112, greg, 384);
3104 memset (data + 496, 0, 8);
3105 va_end (ap);
3106 return elfcore_write_note (abfd, buf, bufsiz,
3107 "CORE", note_type, data, sizeof (data));
3108 }
3109 }
3110}
3111
5d35169e
AM
3112/* Add extra PPC sections. */
3113
2cdcc330 3114static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 3115{
07d6d2b8
AM
3116 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3117 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3118 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
3119 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3120 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3121 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 3122 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3123};
3124
7c8fe5c4
AM
3125enum _ppc64_sec_type {
3126 sec_normal = 0,
3127 sec_opd = 1,
3128 sec_toc = 2
3129};
3130
f0abc2a1
AM
3131struct _ppc64_elf_section_data
3132{
3133 struct bfd_elf_section_data elf;
411e1bfb 3134
f0abc2a1
AM
3135 union
3136 {
51aecdc5
AM
3137 /* An array with one entry for each opd function descriptor,
3138 and some spares since opd entries may be either 16 or 24 bytes. */
3139#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3140 struct _opd_sec_data
3141 {
3142 /* Points to the function code section for local opd entries. */
3143 asection **func_sec;
3144
3145 /* After editing .opd, adjust references to opd local syms. */
3146 long *adjust;
3147 } opd;
7c8fe5c4 3148
3a71aa26
AM
3149 /* An array for toc sections, indexed by offset/8. */
3150 struct _toc_sec_data
3151 {
3152 /* Specifies the relocation symbol index used at a given toc offset. */
3153 unsigned *symndx;
3154
3155 /* And the relocation addend. */
3156 bfd_vma *add;
3157 } toc;
7c8fe5c4
AM
3158 } u;
3159
3160 enum _ppc64_sec_type sec_type:2;
411e1bfb 3161
7c8fe5c4
AM
3162 /* Flag set when small branches are detected. Used to
3163 select suitable defaults for the stub group size. */
3164 unsigned int has_14bit_branch:1;
3e04d765
AM
3165
3166 /* Flag set when PLTCALL relocs are detected. */
3167 unsigned int has_pltcall:1;
f0abc2a1
AM
3168};
3169
3170#define ppc64_elf_section_data(sec) \
411e1bfb 3171 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3172
3173static bfd_boolean
4ce794b7 3174ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3175{
f592407e
AM
3176 if (!sec->used_by_bfd)
3177 {
3178 struct _ppc64_elf_section_data *sdata;
3179 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3180
f592407e
AM
3181 sdata = bfd_zalloc (abfd, amt);
3182 if (sdata == NULL)
3183 return FALSE;
3184 sec->used_by_bfd = sdata;
3185 }
f0abc2a1
AM
3186
3187 return _bfd_elf_new_section_hook (abfd, sec);
3188}
4025353c 3189
74f0fb50 3190static struct _opd_sec_data *
4025353c
AM
3191get_opd_info (asection * sec)
3192{
3193 if (sec != NULL
3194 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3195 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3196 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3197 return NULL;
3198}
90e3cdf2
JJ
3199\f
3200/* Parameters for the qsort hook. */
90e3cdf2 3201static bfd_boolean synthetic_relocatable;
cd285db5 3202static asection *synthetic_opd;
90e3cdf2 3203
699733f6 3204/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3205
3206static int
3207compare_symbols (const void *ap, const void *bp)
3208{
2cdcc330
AM
3209 const asymbol *a = *(const asymbol **) ap;
3210 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 3211
699733f6
AM
3212 /* Section symbols first. */
3213 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3214 return -1;
699733f6 3215 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3216 return 1;
3217
699733f6 3218 /* then .opd symbols. */
cd285db5
AM
3219 if (synthetic_opd != NULL)
3220 {
3221 if (strcmp (a->section->name, ".opd") == 0
3222 && strcmp (b->section->name, ".opd") != 0)
3223 return -1;
3224 if (strcmp (a->section->name, ".opd") != 0
3225 && strcmp (b->section->name, ".opd") == 0)
3226 return 1;
3227 }
90e3cdf2 3228
699733f6 3229 /* then other code symbols. */
2cdcc330
AM
3230 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3231 == (SEC_CODE | SEC_ALLOC))
3232 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3233 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
3234 return -1;
3235
2cdcc330
AM
3236 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3237 != (SEC_CODE | SEC_ALLOC))
3238 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3239 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
3240 return 1;
3241
3242 if (synthetic_relocatable)
3243 {
3244 if (a->section->id < b->section->id)
3245 return -1;
3246
3247 if (a->section->id > b->section->id)
3248 return 1;
3249 }
3250
3251 if (a->value + a->section->vma < b->value + b->section->vma)
3252 return -1;
3253
3254 if (a->value + a->section->vma > b->value + b->section->vma)
3255 return 1;
3256
4d35a0aa
AM
3257 /* For syms with the same value, prefer strong dynamic global function
3258 syms over other syms. */
3259 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3260 return -1;
3261
3262 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3263 return 1;
3264
3265 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3266 return -1;
3267
3268 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3269 return 1;
3270
3271 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3272 return -1;
3273
3274 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3275 return 1;
3276
3277 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3278 return -1;
3279
3280 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3281 return 1;
3282
aaed6f5b 3283 return a > b;
90e3cdf2
JJ
3284}
3285
699733f6 3286/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3287
699733f6 3288static asymbol *
7292b3ac 3289sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3290{
699733f6 3291 long mid;
90e3cdf2 3292
7292b3ac 3293 if (id == (unsigned) -1)
699733f6
AM
3294 {
3295 while (lo < hi)
3296 {
3297 mid = (lo + hi) >> 1;
3298 if (syms[mid]->value + syms[mid]->section->vma < value)
3299 lo = mid + 1;
3300 else if (syms[mid]->value + syms[mid]->section->vma > value)
3301 hi = mid;
3302 else
3303 return syms[mid];
3304 }
3305 }
3306 else
3307 {
3308 while (lo < hi)
3309 {
3310 mid = (lo + hi) >> 1;
3311 if (syms[mid]->section->id < id)
3312 lo = mid + 1;
3313 else if (syms[mid]->section->id > id)
3314 hi = mid;
3315 else if (syms[mid]->value < value)
3316 lo = mid + 1;
3317 else if (syms[mid]->value > value)
3318 hi = mid;
3319 else
3320 return syms[mid];
3321 }
3322 }
3323 return NULL;
90e3cdf2
JJ
3324}
3325
468392fb
AM
3326static bfd_boolean
3327section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3328{
3329 bfd_vma vma = *(bfd_vma *) ptr;
3330 return ((section->flags & SEC_ALLOC) != 0
3331 && section->vma <= vma
3332 && vma < section->vma + section->size);
3333}
3334
699733f6 3335/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3336 entry syms. Also generate @plt symbols for the glink branch table.
3337 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3338
3339static long
a7535cf3
AM
3340ppc64_elf_get_synthetic_symtab (bfd *abfd,
3341 long static_count, asymbol **static_syms,
3342 long dyn_count, asymbol **dyn_syms,
c9727e01 3343 asymbol **ret)
90e3cdf2
JJ
3344{
3345 asymbol *s;
0ccf57bd 3346 size_t i, j, count;
90e3cdf2 3347 char *names;
0ccf57bd 3348 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3349 asection *opd = NULL;
90e3cdf2 3350 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3351 asymbol **syms;
ee67d69a 3352 int abi = abiversion (abfd);
90e3cdf2
JJ
3353
3354 *ret = NULL;
3355
ee67d69a
AM
3356 if (abi < 2)
3357 {
3358 opd = bfd_get_section_by_name (abfd, ".opd");
3359 if (opd == NULL && abi == 1)
3360 return 0;
3361 }
90e3cdf2 3362
a5259595
AM
3363 syms = NULL;
3364 codesecsym = 0;
3365 codesecsymend = 0;
3366 secsymend = 0;
3367 opdsymend = 0;
3368 symcount = 0;
3369 if (opd != NULL)
c9727e01 3370 {
a5259595
AM
3371 symcount = static_count;
3372 if (!relocatable)
3373 symcount += dyn_count;
3374 if (symcount == 0)
3375 return 0;
c9727e01 3376
a5259595
AM
3377 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3378 if (syms == NULL)
3379 return -1;
90e3cdf2 3380
a5259595
AM
3381 if (!relocatable && static_count != 0 && dyn_count != 0)
3382 {
3383 /* Use both symbol tables. */
3384 memcpy (syms, static_syms, static_count * sizeof (*syms));
3385 memcpy (syms + static_count, dyn_syms,
3386 (dyn_count + 1) * sizeof (*syms));
3387 }
3388 else if (!relocatable && static_count == 0)
3389 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3390 else
3391 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3392
0ccf57bd
AM
3393 /* Trim uninteresting symbols. Interesting symbols are section,
3394 function, and notype symbols. */
3395 for (i = 0, j = 0; i < symcount; ++i)
3396 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3397 | BSF_RELC | BSF_SRELC)) == 0)
3398 syms[j++] = syms[i];
3399 symcount = j;
3400
a5259595
AM
3401 synthetic_relocatable = relocatable;
3402 synthetic_opd = opd;
3403 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3404
a5259595
AM
3405 if (!relocatable && symcount > 1)
3406 {
bfa5bd2a
PA
3407 /* Trim duplicate syms, since we may have merged the normal
3408 and dynamic symbols. Actually, we only care about syms
3409 that have different values, so trim any with the same
3410 value. Don't consider ifunc and ifunc resolver symbols
3411 duplicates however, because GDB wants to know whether a
3412 text symbol is an ifunc resolver. */
a5259595 3413 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
3414 {
3415 const asymbol *s0 = syms[i - 1];
3416 const asymbol *s1 = syms[i];
3417
3418 if ((s0->value + s0->section->vma
3419 != s1->value + s1->section->vma)
3420 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3421 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3422 syms[j++] = syms[i];
3423 }
a5259595
AM
3424 symcount = j;
3425 }
699733f6 3426
a5259595
AM
3427 i = 0;
3428 /* Note that here and in compare_symbols we can't compare opd and
3429 sym->section directly. With separate debug info files, the
3430 symbols will be extracted from the debug file while abfd passed
3431 to this function is the real binary. */
0ccf57bd 3432 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
3433 ++i;
3434 codesecsym = i;
3435
3436 for (; i < symcount; ++i)
3437 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3438 | SEC_THREAD_LOCAL))
3439 != (SEC_CODE | SEC_ALLOC))
3440 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3441 break;
3442 codesecsymend = i;
3443
3444 for (; i < symcount; ++i)
3445 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3446 break;
3447 secsymend = i;
3448
3449 for (; i < symcount; ++i)
3450 if (strcmp (syms[i]->section->name, ".opd") != 0)
3451 break;
3452 opdsymend = i;
3453
3454 for (; i < symcount; ++i)
2cdcc330
AM
3455 if (((syms[i]->section->flags
3456 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
3457 != (SEC_CODE | SEC_ALLOC))
3458 break;
3459 symcount = i;
3460 }
c9727e01 3461 count = 0;
90e3cdf2 3462
699733f6 3463 if (relocatable)
90e3cdf2 3464 {
699733f6
AM
3465 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3466 arelent *r;
3467 size_t size;
0ccf57bd 3468 size_t relcount;
90e3cdf2 3469
468392fb
AM
3470 if (opdsymend == secsymend)
3471 goto done;
3472
699733f6 3473 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3474 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3475 if (relcount == 0)
c9727e01 3476 goto done;
90e3cdf2 3477
7356fed5
AM
3478 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3479 {
3480 count = -1;
3481 goto done;
3482 }
3483
699733f6 3484 size = 0;
595da8c5 3485 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3486 {
3487 asymbol *sym;
90e3cdf2 3488
595da8c5 3489 while (r < opd->relocation + relcount
699733f6
AM
3490 && r->address < syms[i]->value + opd->vma)
3491 ++r;
90e3cdf2 3492
595da8c5 3493 if (r == opd->relocation + relcount)
699733f6 3494 break;
90e3cdf2 3495
699733f6
AM
3496 if (r->address != syms[i]->value + opd->vma)
3497 continue;
90e3cdf2 3498
699733f6
AM
3499 if (r->howto->type != R_PPC64_ADDR64)
3500 continue;
90e3cdf2 3501
699733f6
AM
3502 sym = *r->sym_ptr_ptr;
3503 if (!sym_exists_at (syms, opdsymend, symcount,
3504 sym->section->id, sym->value + r->addend))
3505 {
3506 ++count;
3507 size += sizeof (asymbol);
3508 size += strlen (syms[i]->name) + 2;
3509 }
3510 }
90e3cdf2 3511
c4b0b099
AM
3512 if (size == 0)
3513 goto done;
699733f6
AM
3514 s = *ret = bfd_malloc (size);
3515 if (s == NULL)
3516 {
7356fed5 3517 count = -1;
c9727e01 3518 goto done;
699733f6 3519 }
90e3cdf2 3520
699733f6 3521 names = (char *) (s + count);
90e3cdf2 3522
595da8c5 3523 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3524 {
699733f6 3525 asymbol *sym;
90e3cdf2 3526
595da8c5 3527 while (r < opd->relocation + relcount
699733f6
AM
3528 && r->address < syms[i]->value + opd->vma)
3529 ++r;
90e3cdf2 3530
595da8c5 3531 if (r == opd->relocation + relcount)
699733f6
AM
3532 break;
3533
3534 if (r->address != syms[i]->value + opd->vma)
3535 continue;
3536
3537 if (r->howto->type != R_PPC64_ADDR64)
3538 continue;
90e3cdf2 3539
699733f6
AM
3540 sym = *r->sym_ptr_ptr;
3541 if (!sym_exists_at (syms, opdsymend, symcount,
3542 sym->section->id, sym->value + r->addend))
3543 {
3544 size_t len;
3545
3546 *s = *syms[i];
6ba2a415 3547 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3548 s->section = sym->section;
3549 s->value = sym->value + r->addend;
3550 s->name = names;
3551 *names++ = '.';
3552 len = strlen (syms[i]->name);
3553 memcpy (names, syms[i]->name, len + 1);
3554 names += len + 1;
6f610d07
UW
3555 /* Have udata.p point back to the original symbol this
3556 synthetic symbol was derived from. */
3557 s->udata.p = syms[i];
699733f6
AM
3558 s++;
3559 }
3560 }
3561 }
3562 else
90e3cdf2 3563 {
468392fb 3564 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3565 bfd_byte *contents = NULL;
699733f6 3566 size_t size;
0ccf57bd 3567 size_t plt_count = 0;
468392fb
AM
3568 bfd_vma glink_vma = 0, resolv_vma = 0;
3569 asection *dynamic, *glink = NULL, *relplt = NULL;
3570 arelent *p;
90e3cdf2 3571
ee67d69a 3572 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3573 {
c4b0b099
AM
3574 free_contents_and_exit_err:
3575 count = -1;
ee67d69a 3576 free_contents_and_exit:
699733f6 3577 if (contents)
ee67d69a 3578 free (contents);
c9727e01 3579 goto done;
699733f6 3580 }
90e3cdf2 3581
699733f6
AM
3582 size = 0;
3583 for (i = secsymend; i < opdsymend; ++i)
3584 {
3585 bfd_vma ent;
90e3cdf2 3586
5ef11c02
AM
3587 /* Ignore bogus symbols. */
3588 if (syms[i]->value > opd->size - 8)
3589 continue;
3590
699733f6
AM
3591 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3592 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3593 {
3594 ++count;
3595 size += sizeof (asymbol);
3596 size += strlen (syms[i]->name) + 2;
3597 }
3598 }
90e3cdf2 3599
468392fb 3600 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3601 if (dyn_count != 0
3602 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3603 {
3604 bfd_byte *dynbuf, *extdyn, *extdynend;
3605 size_t extdynsize;
3606 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3607
3608 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3609 goto free_contents_and_exit_err;
468392fb
AM
3610
3611 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3612 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3613
3614 extdyn = dynbuf;
3615 extdynend = extdyn + dynamic->size;
3616 for (; extdyn < extdynend; extdyn += extdynsize)
3617 {
3618 Elf_Internal_Dyn dyn;
3619 (*swap_dyn_in) (abfd, extdyn, &dyn);
3620
3621 if (dyn.d_tag == DT_NULL)
3622 break;
3623
3624 if (dyn.d_tag == DT_PPC64_GLINK)
3625 {
9e390558
AM
3626 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3627 See comment in ppc64_elf_finish_dynamic_sections. */
3628 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3629 /* The .glink section usually does not survive the final
3630 link; search for the section (usually .text) where the
3631 glink stubs now reside. */
3632 glink = bfd_sections_find_if (abfd, section_covers_vma,
3633 &glink_vma);
3634 break;
3635 }
3636 }
3637
3638 free (dynbuf);
3639 }
3640
3641 if (glink != NULL)
3642 {
3643 /* Determine __glink trampoline by reading the relative branch
3644 from the first glink stub. */
3645 bfd_byte buf[4];
b9e5796b
AM
3646 unsigned int off = 0;
3647
3648 while (bfd_get_section_contents (abfd, glink, buf,
3649 glink_vma + off - glink->vma, 4))
468392fb
AM
3650 {
3651 unsigned int insn = bfd_get_32 (abfd, buf);
3652 insn ^= B_DOT;
3653 if ((insn & ~0x3fffffc) == 0)
b9e5796b 3654 {
2cdcc330
AM
3655 resolv_vma
3656 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
3657 break;
3658 }
3659 off += 4;
3660 if (off > 4)
3661 break;
468392fb
AM
3662 }
3663
3664 if (resolv_vma)
3665 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3666
066ee829
AM
3667 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3668 if (relplt != NULL)
3669 {
3670 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 3671 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3672 goto free_contents_and_exit_err;
68ffbac6 3673
066ee829
AM
3674 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3675 size += plt_count * sizeof (asymbol);
468392fb 3676
066ee829
AM
3677 p = relplt->relocation;
3678 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3679 {
3680 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3681 if (p->addend != 0)
3682 size += sizeof ("+0x") - 1 + 16;
3683 }
066ee829 3684 }
468392fb
AM
3685 }
3686
c4b0b099
AM
3687 if (size == 0)
3688 goto free_contents_and_exit;
699733f6
AM
3689 s = *ret = bfd_malloc (size);
3690 if (s == NULL)
c4b0b099 3691 goto free_contents_and_exit_err;
90e3cdf2 3692
468392fb 3693 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3694
699733f6 3695 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3696 {
699733f6 3697 bfd_vma ent;
90e3cdf2 3698
5ef11c02
AM
3699 if (syms[i]->value > opd->size - 8)
3700 continue;
3701
699733f6
AM
3702 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3703 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3704 {
0ccf57bd 3705 size_t lo, hi;
699733f6 3706 size_t len;
c9727e01 3707 asection *sec = abfd->sections;
90e3cdf2 3708
699733f6
AM
3709 *s = *syms[i];
3710 lo = codesecsym;
3711 hi = codesecsymend;
3712 while (lo < hi)
3713 {
0ccf57bd 3714 size_t mid = (lo + hi) >> 1;
699733f6
AM
3715 if (syms[mid]->section->vma < ent)
3716 lo = mid + 1;
3717 else if (syms[mid]->section->vma > ent)
3718 hi = mid;
3719 else
c9727e01
AM
3720 {
3721 sec = syms[mid]->section;
3722 break;
3723 }
699733f6
AM
3724 }
3725
c9727e01 3726 if (lo >= hi && lo > codesecsym)
699733f6 3727 sec = syms[lo - 1]->section;
699733f6
AM
3728
3729 for (; sec != NULL; sec = sec->next)
3730 {
3731 if (sec->vma > ent)
3732 break;
63524580
JK
3733 /* SEC_LOAD may not be set if SEC is from a separate debug
3734 info file. */
3735 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3736 break;
3737 if ((sec->flags & SEC_CODE) != 0)
3738 s->section = sec;
3739 }
6ba2a415 3740 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3741 s->value = ent - s->section->vma;
3742 s->name = names;
3743 *names++ = '.';
3744 len = strlen (syms[i]->name);
3745 memcpy (names, syms[i]->name, len + 1);
3746 names += len + 1;
6f610d07
UW
3747 /* Have udata.p point back to the original symbol this
3748 synthetic symbol was derived from. */
3749 s->udata.p = syms[i];
699733f6 3750 s++;
90e3cdf2 3751 }
90e3cdf2 3752 }
699733f6 3753 free (contents);
468392fb
AM
3754
3755 if (glink != NULL && relplt != NULL)
3756 {
3757 if (resolv_vma)
3758 {
3759 /* Add a symbol for the main glink trampoline. */
86a4952b 3760 memset (s, 0, sizeof *s);
468392fb 3761 s->the_bfd = abfd;
6ba2a415 3762 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3763 s->section = glink;
3764 s->value = resolv_vma - glink->vma;
3765 s->name = names;
2cdcc330
AM
3766 memcpy (names, "__glink_PLTresolve",
3767 sizeof ("__glink_PLTresolve"));
468392fb
AM
3768 names += sizeof ("__glink_PLTresolve");
3769 s++;
3770 count++;
3771 }
3772
3773 /* FIXME: It would be very much nicer to put sym@plt on the
3774 stub rather than on the glink branch table entry. The
3775 objdump disassembler would then use a sensible symbol
3776 name on plt calls. The difficulty in doing so is
3777 a) finding the stubs, and,
3778 b) matching stubs against plt entries, and,
3779 c) there can be multiple stubs for a given plt entry.
3780
3781 Solving (a) could be done by code scanning, but older
3782 ppc64 binaries used different stubs to current code.
3783 (b) is the tricky one since you need to known the toc
3784 pointer for at least one function that uses a pic stub to
3785 be able to calculate the plt address referenced.
3786 (c) means gdb would need to set multiple breakpoints (or
3787 find the glink branch itself) when setting breakpoints
3788 for pending shared library loads. */
3789 p = relplt->relocation;
3790 for (i = 0; i < plt_count; i++, p++)
3791 {
3792 size_t len;
3793
3794 *s = **p->sym_ptr_ptr;
3795 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3796 we are defining a symbol, ensure one of them is set. */
3797 if ((s->flags & BSF_LOCAL) == 0)
3798 s->flags |= BSF_GLOBAL;
6ba2a415 3799 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3800 s->section = glink;
3801 s->value = glink_vma - glink->vma;
3802 s->name = names;
3803 s->udata.p = NULL;
3804 len = strlen ((*p->sym_ptr_ptr)->name);
3805 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3806 names += len;
e054468f
AM
3807 if (p->addend != 0)
3808 {
3809 memcpy (names, "+0x", sizeof ("+0x") - 1);
3810 names += sizeof ("+0x") - 1;
3811 bfd_sprintf_vma (abfd, names, p->addend);
3812 names += strlen (names);
3813 }
468392fb
AM
3814 memcpy (names, "@plt", sizeof ("@plt"));
3815 names += sizeof ("@plt");
3816 s++;
b9e5796b
AM
3817 if (abi < 2)
3818 {
3819 glink_vma += 8;
3820 if (i >= 0x8000)
3821 glink_vma += 4;
3822 }
3823 else
468392fb
AM
3824 glink_vma += 4;
3825 }
3826 count += plt_count;
3827 }
90e3cdf2
JJ
3828 }
3829
c9727e01 3830 done:
a7535cf3 3831 free (syms);
90e3cdf2
JJ
3832 return count;
3833}
5bd4f169 3834\f
65f38f15
AM
3835/* The following functions are specific to the ELF linker, while
3836 functions above are used generally. Those named ppc64_elf_* are
3837 called by the main ELF linker code. They appear in this file more
3838 or less in the order in which they are called. eg.
3839 ppc64_elf_check_relocs is called early in the link process,
3840 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3841 called.
3842
3843 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3844 functions have both a function code symbol and a function descriptor
3845 symbol. A call to foo in a relocatable object file looks like:
3846
3847 . .text
3848 . x:
3849 . bl .foo
3850 . nop
3851
3852 The function definition in another object file might be:
3853
3854 . .section .opd
3855 . foo: .quad .foo
3856 . .quad .TOC.@tocbase
3857 . .quad 0
3858 .
3859 . .text
3860 . .foo: blr
3861
3862 When the linker resolves the call during a static link, the branch
3863 unsurprisingly just goes to .foo and the .opd information is unused.
3864 If the function definition is in a shared library, things are a little
3865 different: The call goes via a plt call stub, the opd information gets
3866 copied to the plt, and the linker patches the nop.
3867
3868 . x:
3869 . bl .foo_stub
3870 . ld 2,40(1)
3871 .
3872 .
3873 . .foo_stub:
71a39c98
AM
3874 . std 2,40(1) # in practice, the call stub
3875 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3876 . addi 11,11,Lfoo@toc@l # this is the general idea
3877 . ld 12,0(11)
3878 . ld 2,8(11)
3879 . mtctr 12
3880 . ld 11,16(11)
e86ce104
AM
3881 . bctr
3882 .
3883 . .section .plt
3884 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3885
3886 The "reloc ()" notation is supposed to indicate that the linker emits
3887 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3888 copying.
3889
3890 What are the difficulties here? Well, firstly, the relocations
3891 examined by the linker in check_relocs are against the function code
3892 sym .foo, while the dynamic relocation in the plt is emitted against
3893 the function descriptor symbol, foo. Somewhere along the line, we need
3894 to carefully copy dynamic link information from one symbol to the other.
3895 Secondly, the generic part of the elf linker will make .foo a dynamic
3896 symbol as is normal for most other backends. We need foo dynamic
3897 instead, at least for an application final link. However, when
3898 creating a shared library containing foo, we need to have both symbols
3899 dynamic so that references to .foo are satisfied during the early
3900 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3901 definition from some other object, eg. a static library.
3902
3903 Update: As of August 2004, we support a new convention. Function
3904 calls may use the function descriptor symbol, ie. "bl foo". This
3905 behaves exactly as "bl .foo". */
65f38f15 3906
7c8bbca5
AM
3907/* Of those relocs that might be copied as dynamic relocs, this
3908 function selects those that must be copied when linking a shared
3909 library or PIE, even when the symbol is local. */
65f38f15 3910
1d483afe
AM
3911static int
3912must_be_dyn_reloc (struct bfd_link_info *info,
3913 enum elf_ppc64_reloc_type r_type)
3914{
3915 switch (r_type)
3916 {
3917 default:
7c8bbca5
AM
3918 /* Only relative relocs can be resolved when the object load
3919 address isn't fixed. DTPREL64 is excluded because the
3920 dynamic linker needs to differentiate global dynamic from
3921 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3922 return 1;
3923
3924 case R_PPC64_REL32:
3925 case R_PPC64_REL64:
3926 case R_PPC64_REL30:
3927 return 0;
3928
3929 case R_PPC64_TPREL16:
3930 case R_PPC64_TPREL16_LO:
3931 case R_PPC64_TPREL16_HI:
3932 case R_PPC64_TPREL16_HA:
3933 case R_PPC64_TPREL16_DS:
3934 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3935 case R_PPC64_TPREL16_HIGH:
3936 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3937 case R_PPC64_TPREL16_HIGHER:
3938 case R_PPC64_TPREL16_HIGHERA:
3939 case R_PPC64_TPREL16_HIGHEST:
3940 case R_PPC64_TPREL16_HIGHESTA:
3941 case R_PPC64_TPREL64:
7c8bbca5
AM
3942 /* These relocations are relative but in a shared library the
3943 linker doesn't know the thread pointer base. */
3944 return bfd_link_dll (info);
1d483afe
AM
3945 }
3946}
65f38f15 3947
f4656909
AM
3948/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3949 copying dynamic variables from a shared lib into an app's dynbss
3950 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3951 shared lib. With code that gcc generates, it's vital that this be
3952 enabled; In the PowerPC64 ABI, the address of a function is actually
3953 the address of a function descriptor, which resides in the .opd
3954 section. gcc uses the descriptor directly rather than going via the
3955 GOT as some other ABI's do, which means that initialized function
3956 pointers must reference the descriptor. Thus, a function pointer
3957 initialized to the address of a function in a shared library will
3958 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3959 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3960 presents a problem as a plt entry for that function is also
3961 initialized from the function descriptor symbol and the copy reloc
3962 may not be initialized first. */
a23b6845 3963#define ELIMINATE_COPY_RELOCS 1
f4656909 3964
721956f4 3965/* Section name for stubs is the associated section name plus this
29942be8
NC
3966 string. */
3967#define STUB_SUFFIX ".stub"
721956f4
AM
3968
3969/* Linker stubs.
3970 ppc_stub_long_branch:
3971 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3972 destination, but a 24 bit branch in a stub section will reach.
3973 . b dest
3974
3975 ppc_stub_plt_branch:
3976 Similar to the above, but a 24 bit branch in the stub section won't
3977 reach its destination.
71a39c98
AM
3978 . addis %r11,%r2,xxx@toc@ha
3979 . ld %r12,xxx@toc@l(%r11)
3980 . mtctr %r12
721956f4
AM
3981 . bctr
3982
3983 ppc_stub_plt_call:
2c66dc6c
AM
3984 Used to call a function in a shared library. If it so happens that
3985 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3986 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 3987 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
3988 . addis %r11,%r2,xxx@toc@ha
3989 . ld %r12,xxx+0@toc@l(%r11)
3990 . mtctr %r12
3991 . ld %r2,xxx+8@toc@l(%r11)
3992 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3993 . bctr
ad8e1ba5
AM
3994
3995 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3996 code to adjust the value and save r2 to support multiple toc sections.
3997 A ppc_stub_long_branch with an r2 offset looks like:
3998 . std %r2,40(%r1)
3999 . addis %r2,%r2,off@ha
4000 . addi %r2,%r2,off@l
4001 . b dest
4002
4003 A ppc_stub_plt_branch with an r2 offset looks like:
4004 . std %r2,40(%r1)
71a39c98
AM
4005 . addis %r11,%r2,xxx@toc@ha
4006 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
4007 . addis %r2,%r2,off@ha
4008 . addi %r2,%r2,off@l
71a39c98 4009 . mtctr %r12
ad8e1ba5 4010 . bctr
ac2df442 4011
05d0e962
AM
4012 All of the above stubs are shown as their ELFv1 variants. ELFv2
4013 variants exist too, simpler for plt calls since a new toc pointer
4014 and static chain are not loaded by the stub. In addition, ELFv2
4015 has some more complex stubs to handle calls marked with NOTOC
4016 relocs from functions where r2 is not a valid toc pointer. These
4017 come in two flavours, the ones shown below, and _both variants that
4018 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
4019 one call is from a function where r2 is used as the toc pointer but
4020 needs a toc adjusting stub for small-model multi-toc, and another
4021 call is from a function where r2 is not valid.
4022 ppc_stub_long_branch_notoc:
4023 . mflr %r12
4024 . bcl 20,31,1f
4025 . 1:
4026 . mflr %r11
4027 . mtlr %r12
2cdcc330
AM
4028 . addis %r12,%r11,dest-1b@ha
4029 . addi %r12,%r12,dest-1b@l
05d0e962
AM
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 4085
2cdcc330
AM
4086enum ppc_stub_type
4087{
721956f4
AM
4088 ppc_stub_none,
4089 ppc_stub_long_branch,
ad8e1ba5 4090 ppc_stub_long_branch_r2off,
05d0e962
AM
4091 ppc_stub_long_branch_notoc,
4092 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 4093 ppc_stub_plt_branch,
ad8e1ba5 4094 ppc_stub_plt_branch_r2off,
05d0e962
AM
4095 ppc_stub_plt_branch_notoc,
4096 ppc_stub_plt_branch_both,
794e51c0 4097 ppc_stub_plt_call,
7341d5e2 4098 ppc_stub_plt_call_r2save,
05d0e962
AM
4099 ppc_stub_plt_call_notoc,
4100 ppc_stub_plt_call_both,
a4b6fadd
AM
4101 ppc_stub_global_entry,
4102 ppc_stub_save_res
721956f4
AM
4103};
4104
6f20ed8a
AM
4105/* Information on stub grouping. */
4106struct map_stub
4107{
4108 /* The stub section. */
4109 asection *stub_sec;
4110 /* This is the section to which stubs in the group will be attached. */
4111 asection *link_sec;
a4b6fadd
AM
4112 /* Next group. */
4113 struct map_stub *next;
4114 /* Whether to emit a copy of register save/restore functions in this
4115 group. */
4116 int needs_save_res;
df136d64
AM
4117 /* Current offset within stubs after the insn restoring lr in a
4118 _notoc or _both stub using bcl for pc-relative addressing, or
4119 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
4120 unsigned int lr_restore;
4121 /* Accumulated size of EH info emitted to describe return address
4122 if stubs modify lr. Does not include 17 byte FDE header. */
4123 unsigned int eh_size;
4124 /* Offset in glink_eh_frame to the start of EH info for this group. */
4125 unsigned int eh_base;
6f20ed8a
AM
4126};
4127
2cdcc330
AM
4128struct ppc_stub_hash_entry
4129{
721956f4
AM
4130 /* Base hash table entry structure. */
4131 struct bfd_hash_entry root;
4132
ad8e1ba5
AM
4133 enum ppc_stub_type stub_type;
4134
6f20ed8a
AM
4135 /* Group information. */
4136 struct map_stub *group;
721956f4
AM
4137
4138 /* Offset within stub_sec of the beginning of this stub. */
4139 bfd_vma stub_offset;
4140
4141 /* Given the symbol's value and its section we can determine its final
4142 value when building the stubs (so the stub knows where to jump. */
4143 bfd_vma target_value;
4144 asection *target_section;
4145
721956f4
AM
4146 /* The symbol table entry, if any, that this was derived from. */
4147 struct ppc_link_hash_entry *h;
e054468f 4148 struct plt_entry *plt_ent;
721956f4 4149
2d7ad24e
AM
4150 /* Symbol type. */
4151 unsigned char symtype;
4152
6911b7dc
AM
4153 /* Symbol st_other. */
4154 unsigned char other;
721956f4
AM
4155};
4156
2cdcc330
AM
4157struct ppc_branch_hash_entry
4158{
721956f4
AM
4159 /* Base hash table entry structure. */
4160 struct bfd_hash_entry root;
4161
c456f082 4162 /* Offset within branch lookup table. */
721956f4
AM
4163 unsigned int offset;
4164
4165 /* Generation marker. */
4166 unsigned int iter;
4167};
65f38f15 4168
19e08130
AM
4169/* Used to track dynamic relocations for local symbols. */
4170struct ppc_dyn_relocs
4171{
4172 struct ppc_dyn_relocs *next;
4173
4174 /* The input section of the reloc. */
4175 asection *sec;
4176
4177 /* Total number of relocs copied for the input section. */
4178 unsigned int count : 31;
4179
4180 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4181 unsigned int ifunc : 1;
4182};
4183
65f38f15
AM
4184struct ppc_link_hash_entry
4185{
4186 struct elf_link_hash_entry elf;
4187
2cdcc330
AM
4188 union
4189 {
b3fac117
AM
4190 /* A pointer to the most recently used stub hash entry against this
4191 symbol. */
4192 struct ppc_stub_hash_entry *stub_cache;
4193
4194 /* A pointer to the next symbol starting with a '.' */
4195 struct ppc_link_hash_entry *next_dot_sym;
4196 } u;
721956f4 4197
65f38f15 4198 /* Track dynamic relocs copied for this symbol. */
6061a67d 4199 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4200
721956f4 4201 /* Link between function code and descriptor symbols. */
34814b9f 4202 struct ppc_link_hash_entry *oh;
721956f4 4203
e86ce104
AM
4204 /* Flag function code and descriptor symbols. */
4205 unsigned int is_func:1;
4206 unsigned int is_func_descriptor:1;
908b32fc 4207 unsigned int fake:1;
411e1bfb 4208
c5614fa4
AM
4209 /* Whether global opd/toc sym has been adjusted or not.
4210 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4211 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4212 unsigned int adjust_done:1;
4213
a4b6fadd
AM
4214 /* Set if this is an out-of-line register save/restore function,
4215 with non-standard calling convention. */
4216 unsigned int save_res:1;
4217
8b5f1ed8
AM
4218 /* Set if a duplicate symbol with non-zero localentry is detected,
4219 even when the duplicate symbol does not provide a definition. */
4220 unsigned int non_zero_localentry:1;
4221
411e1bfb 4222 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
4223 Bits are or'd into the mask as the corresponding relocs are
4224 encountered during check_relocs, with TLS_TLS being set when any
4225 of the other TLS bits are set. tls_optimize clears bits when
4226 optimizing to indicate the corresponding GOT entry type is not
4227 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4228 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4229 separate flag rather than setting TPREL just for convenience in
4230 distinguishing the two cases.
4231 These flags are also kept for local symbols. */
4232#define TLS_TLS 1 /* Any TLS reloc. */
4233#define TLS_GD 2 /* GD reloc. */
4234#define TLS_LD 4 /* LD reloc. */
4235#define TLS_TPREL 8 /* TPREL reloc, => IE. */
4236#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4237#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 4238#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 4239#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 4240 unsigned char tls_mask;
37da22e5
AM
4241
4242 /* The above field is also used to mark function symbols. In which
4243 case TLS_TLS will be 0. */
4244#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 4245#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 4246#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
4247};
4248
4249/* ppc64 ELF linker hash table. */
4250
4251struct ppc_link_hash_table
4252{
4253 struct elf_link_hash_table elf;
4254
721956f4
AM
4255 /* The stub hash table. */
4256 struct bfd_hash_table stub_hash_table;
4257
4258 /* Another hash table for plt_branch stubs. */
4259 struct bfd_hash_table branch_hash_table;
4260
3b421ab3
AM
4261 /* Hash table for function prologue tocsave. */
4262 htab_t tocsave_htab;
4263
e7d1c40c
AM
4264 /* Various options and other info passed from the linker. */
4265 struct ppc64_elf_params *params;
721956f4 4266
6f20ed8a
AM
4267 /* The size of sec_info below. */
4268 unsigned int sec_info_arr_size;
4269
4270 /* Per-section array of extra section info. Done this way rather
4271 than as part of ppc64_elf_section_data so we have the info for
4272 non-ppc64 sections. */
4273 struct
4274 {
4275 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4276 bfd_vma toc_off;
6f20ed8a
AM
4277
4278 union
4279 {
4280 /* The section group that this section belongs to. */
4281 struct map_stub *group;
4282 /* A temp section list pointer. */
4283 asection *list;
4284 } u;
4285 } *sec_info;
721956f4 4286
a4b6fadd
AM
4287 /* Linked list of groups. */
4288 struct map_stub *group;
4289
ad8e1ba5
AM
4290 /* Temp used when calculating TOC pointers. */
4291 bfd_vma toc_curr;
bf102f86
AM
4292 bfd *toc_bfd;
4293 asection *toc_first_sec;
ad8e1ba5 4294
b3fac117
AM
4295 /* Used when adding symbols. */
4296 struct ppc_link_hash_entry *dot_syms;
4297
33e44f2e 4298 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4299 asection *glink;
9e390558 4300 asection *global_entry;
82bd7b59 4301 asection *sfpr;
2d7ad24e
AM
4302 asection *pltlocal;
4303 asection *relpltlocal;
4ce794b7
AM
4304 asection *brlt;
4305 asection *relbrlt;
58d180e8 4306 asection *glink_eh_frame;
ec338859 4307
8387904d
AM
4308 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4309 struct ppc_link_hash_entry *tls_get_addr;
4310 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4311
927be08e
AM
4312 /* The size of reliplt used by got entry relocs. */
4313 bfd_size_type got_reli_size;
4314
9b5ecbd0 4315 /* Statistics. */
7341d5e2 4316 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4317
ee75fd95
AM
4318 /* Number of stubs against global syms. */
4319 unsigned long stub_globals;
4320
ee67d69a
AM
4321 /* Set if we're linking code with function descriptors. */
4322 unsigned int opd_abi:1;
4323
4c52953f 4324 /* Support for multiple toc sections. */
33c0ec9d 4325 unsigned int do_multi_toc:1;
4c52953f 4326 unsigned int multi_toc_needed:1;
927be08e 4327 unsigned int second_toc_pass:1;
67f0cbdb 4328 unsigned int do_toc_opt:1;
4c52953f 4329
9a23f96e
AM
4330 /* Set if tls optimization is enabled. */
4331 unsigned int do_tls_opt:1;
4332
3e04d765
AM
4333 /* Set if inline plt calls should be converted to direct calls. */
4334 unsigned int can_convert_all_inline_plt:1;
4335
5d1634d7 4336 /* Set on error. */
99877b66 4337 unsigned int stub_error:1;
721956f4 4338
8c5b4e52
AM
4339 /* Whether func_desc_adjust needs to be run over symbols. */
4340 unsigned int need_func_desc_adj:1;
721956f4 4341
82e66161
AM
4342 /* Whether there exist local gnu indirect function resolvers,
4343 referenced by dynamic relocations. */
4344 unsigned int local_ifunc_resolver:1;
4345 unsigned int maybe_local_ifunc_resolver:1;
4346
f378ab09
AM
4347 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4348 unsigned int has_plt_localentry0:1;
4349
721956f4
AM
4350 /* Incremented every time we size stubs. */
4351 unsigned int stub_iteration;
5d1634d7 4352
87d72d41
AM
4353 /* Small local sym cache. */
4354 struct sym_cache sym_cache;
65f38f15
AM
4355};
4356
4c52953f
AM
4357/* Rename some of the generic section flags to better document how they
4358 are used here. */
b0dddeec
AM
4359
4360/* Nonzero if this section has TLS related relocations. */
4361#define has_tls_reloc sec_flg0
4362
23cedd1d 4363/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
4364#define has_tls_get_addr_call sec_flg1
4365
4366/* Nonzero if this section has any toc or got relocs. */
4367#define has_toc_reloc sec_flg2
4368
4369/* Nonzero if this section has a call to another section that uses
4370 the toc or got. */
d77c8a4b 4371#define makes_toc_func_call sec_flg3
b0dddeec
AM
4372
4373/* Recursion protection when determining above flag. */
d77c8a4b 4374#define call_check_in_progress sec_flg4
70cc837d 4375#define call_check_done sec_flg5
4c52953f 4376
65f38f15
AM
4377/* Get the ppc64 ELF linker hash table from a link_info structure. */
4378
4379#define ppc_hash_table(p) \
4dfe6ac6
NC
4380 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4381 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4382
721956f4
AM
4383#define ppc_stub_hash_lookup(table, string, create, copy) \
4384 ((struct ppc_stub_hash_entry *) \
4385 bfd_hash_lookup ((table), (string), (create), (copy)))
4386
4387#define ppc_branch_hash_lookup(table, string, create, copy) \
4388 ((struct ppc_branch_hash_entry *) \
4389 bfd_hash_lookup ((table), (string), (create), (copy)))
4390
4391/* Create an entry in the stub hash table. */
4392
4393static struct bfd_hash_entry *
4ce794b7
AM
4394stub_hash_newfunc (struct bfd_hash_entry *entry,
4395 struct bfd_hash_table *table,
4396 const char *string)
721956f4
AM
4397{
4398 /* Allocate the structure if it has not already been allocated by a
4399 subclass. */
4400 if (entry == NULL)
4401 {
4402 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4403 if (entry == NULL)
4404 return entry;
4405 }
4406
4407 /* Call the allocation method of the superclass. */
4408 entry = bfd_hash_newfunc (entry, table, string);
4409 if (entry != NULL)
4410 {
4411 struct ppc_stub_hash_entry *eh;
4412
4413 /* Initialize the local fields. */
4414 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4415 eh->stub_type = ppc_stub_none;
6f20ed8a 4416 eh->group = NULL;
721956f4
AM
4417 eh->stub_offset = 0;
4418 eh->target_value = 0;
4419 eh->target_section = NULL;
721956f4 4420 eh->h = NULL;
6911b7dc 4421 eh->plt_ent = NULL;
6911b7dc 4422 eh->other = 0;
721956f4
AM
4423 }
4424
4425 return entry;
4426}
4427
4428/* Create an entry in the branch hash table. */
4429
4430static struct bfd_hash_entry *
4ce794b7
AM
4431branch_hash_newfunc (struct bfd_hash_entry *entry,
4432 struct bfd_hash_table *table,
4433 const char *string)
721956f4
AM
4434{
4435 /* Allocate the structure if it has not already been allocated by a
4436 subclass. */
4437 if (entry == NULL)
4438 {
4439 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4440 if (entry == NULL)
4441 return entry;
4442 }
4443
4444 /* Call the allocation method of the superclass. */
4445 entry = bfd_hash_newfunc (entry, table, string);
4446 if (entry != NULL)
4447 {
4448 struct ppc_branch_hash_entry *eh;
4449
4450 /* Initialize the local fields. */
4451 eh = (struct ppc_branch_hash_entry *) entry;
4452 eh->offset = 0;
4453 eh->iter = 0;
4454 }
4455
4456 return entry;
4457}
4458
65f38f15
AM
4459/* Create an entry in a ppc64 ELF linker hash table. */
4460
4461static struct bfd_hash_entry *
4ce794b7
AM
4462link_hash_newfunc (struct bfd_hash_entry *entry,
4463 struct bfd_hash_table *table,
4464 const char *string)
65f38f15
AM
4465{
4466 /* Allocate the structure if it has not already been allocated by a
4467 subclass. */
4468 if (entry == NULL)
4469 {
4470 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4471 if (entry == NULL)
4472 return entry;
4473 }
4474
4475 /* Call the allocation method of the superclass. */
4476 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4477 if (entry != NULL)
4478 {
4479 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4480
b3fac117 4481 memset (&eh->u.stub_cache, 0,
908b32fc 4482 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4483 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4484
4485 /* When making function calls, old ABI code references function entry
4486 points (dot symbols), while new ABI code references the function
4487 descriptor symbol. We need to make any combination of reference and
4488 definition work together, without breaking archive linking.
4489
4490 For a defined function "foo" and an undefined call to "bar":
4491 An old object defines "foo" and ".foo", references ".bar" (possibly
4492 "bar" too).
4493 A new object defines "foo" and references "bar".
4494
4495 A new object thus has no problem with its undefined symbols being
4496 satisfied by definitions in an old object. On the other hand, the
4497 old object won't have ".bar" satisfied by a new object.
4498
4499 Keep a list of newly added dot-symbols. */
4500
4501 if (string[0] == '.')
4502 {
4503 struct ppc_link_hash_table *htab;
4504
4505 htab = (struct ppc_link_hash_table *) table;
4506 eh->u.next_dot_sym = htab->dot_syms;
4507 htab->dot_syms = eh;
4508 }
65f38f15
AM
4509 }
4510
4511 return entry;
4512}
4513
2cdcc330
AM
4514struct tocsave_entry
4515{
3b421ab3
AM
4516 asection *sec;
4517 bfd_vma offset;
4518};
4519
4520static hashval_t
4521tocsave_htab_hash (const void *p)
4522{
4523 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4524 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4525}
4526
4527static int
4528tocsave_htab_eq (const void *p1, const void *p2)
4529{
4530 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4531 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4532 return e1->sec == e2->sec && e1->offset == e2->offset;
4533}
4534
68faa637
AM
4535/* Destroy a ppc64 ELF linker hash table. */
4536
4537static void
d495ab0d 4538ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4539{
d495ab0d 4540 struct ppc_link_hash_table *htab;
68faa637 4541
d495ab0d 4542 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4543 if (htab->tocsave_htab)
4544 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4545 bfd_hash_table_free (&htab->branch_hash_table);
4546 bfd_hash_table_free (&htab->stub_hash_table);
4547 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4548}
4549
65f38f15
AM
4550/* Create a ppc64 ELF linker hash table. */
4551
4552static struct bfd_link_hash_table *
4ce794b7 4553ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4554{
4555 struct ppc_link_hash_table *htab;
4556 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4557
4ce794b7 4558 htab = bfd_zmalloc (amt);
65f38f15
AM
4559 if (htab == NULL)
4560 return NULL;
4561
66eb6687 4562 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4563 sizeof (struct ppc_link_hash_entry),
4564 PPC64_ELF_DATA))
65f38f15 4565 {
e2d34d7d 4566 free (htab);
65f38f15
AM
4567 return NULL;
4568 }
4569
721956f4 4570 /* Init the stub hash table too. */
66eb6687
AM
4571 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4572 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4573 {
d495ab0d 4574 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4575 return NULL;
4576 }
721956f4
AM
4577
4578 /* And the branch hash table. */
66eb6687
AM
4579 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4580 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4581 {
4582 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4583 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4584 return NULL;
4585 }
721956f4 4586
3b421ab3
AM
4587 htab->tocsave_htab = htab_try_create (1024,
4588 tocsave_htab_hash,
4589 tocsave_htab_eq,
4590 NULL);
4591 if (htab->tocsave_htab == NULL)
2915c55b 4592 {
d495ab0d 4593 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4594 return NULL;
4595 }
d495ab0d 4596 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4597
3254fd24
AM
4598 /* Initializing two fields of the union is just cosmetic. We really
4599 only care about glist, but when compiled on a 32-bit host the
4600 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4601 debugger inspection of these fields look nicer. */
a6aa5195
AM
4602 htab->elf.init_got_refcount.refcount = 0;
4603 htab->elf.init_got_refcount.glist = NULL;
4604 htab->elf.init_plt_refcount.refcount = 0;
4605 htab->elf.init_plt_refcount.glist = NULL;
4606 htab->elf.init_got_offset.offset = 0;
4607 htab->elf.init_got_offset.glist = NULL;
4608 htab->elf.init_plt_offset.offset = 0;
4609 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4610
65f38f15
AM
4611 return &htab->elf.root;
4612}
4613
bfeb4a28
AM
4614/* Create sections for linker generated code. */
4615
4616static bfd_boolean
4617create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4618{
4619 struct ppc_link_hash_table *htab;
4620 flagword flags;
4621
4622 htab = ppc_hash_table (info);
4623
bfeb4a28
AM
4624 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4625 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4626 if (htab->params->save_restore_funcs)
4627 {
4628 /* Create .sfpr for code to save and restore fp regs. */
4629 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4630 flags);
4631 if (htab->sfpr == NULL
2cdcc330 4632 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
4633 return FALSE;
4634 }
4635
4636 if (bfd_link_relocatable (info))
4637 return TRUE;
bfeb4a28
AM
4638
4639 /* Create .glink for lazy dynamic linking support. */
4640 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4641 flags);
4642 if (htab->glink == NULL
2cdcc330 4643 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
4644 return FALSE;
4645
9e390558
AM
4646 /* The part of .glink used by global entry stubs, separate so that
4647 it can be aligned appropriately without affecting htab->glink. */
4648 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4649 flags);
4650 if (htab->global_entry == NULL
2cdcc330 4651 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
4652 return FALSE;
4653
bfeb4a28
AM
4654 if (!info->no_ld_generated_unwind_info)
4655 {
4656 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4657 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4658 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4659 ".eh_frame",
4660 flags);
4661 if (htab->glink_eh_frame == NULL
4662 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4663 return FALSE;
4664 }
4665
4666 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4667 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4668 if (htab->elf.iplt == NULL
2cdcc330 4669 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4670 return FALSE;
4671
4672 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4673 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4674 htab->elf.irelplt
4675 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4676 if (htab->elf.irelplt == NULL
2cdcc330 4677 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4678 return FALSE;
4679
4680 /* Create branch lookup table for plt_branch stubs. */
4681 flags = (SEC_ALLOC | SEC_LOAD
4682 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4683 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4684 flags);
4685 if (htab->brlt == NULL
2cdcc330 4686 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
4687 return FALSE;
4688
2d7ad24e
AM
4689 /* Local plt entries, put in .branch_lt but a separate section for
4690 convenience. */
4691 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4692 flags);
4693 if (htab->pltlocal == NULL
2cdcc330 4694 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
4695 return FALSE;
4696
0e1862bb 4697 if (!bfd_link_pic (info))
bfeb4a28
AM
4698 return TRUE;
4699
4700 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4701 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
4702 htab->relbrlt
4703 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 4704 if (htab->relbrlt == NULL
2cdcc330 4705 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
4706 return FALSE;
4707
2d7ad24e
AM
4708 htab->relpltlocal
4709 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4710 if (htab->relpltlocal == NULL
2cdcc330 4711 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
4712 return FALSE;
4713
bfeb4a28
AM
4714 return TRUE;
4715}
4716
e717da7e
AM
4717/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4718
bfeb4a28 4719bfd_boolean
e7d1c40c
AM
4720ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4721 struct ppc64_elf_params *params)
e717da7e
AM
4722{
4723 struct ppc_link_hash_table *htab;
4724
e7d1c40c 4725 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4726
4727/* Always hook our dynamic sections into the first bfd, which is the
4728 linker created stub bfd. This ensures that the GOT header is at
4729 the start of the output TOC section. */
4730 htab = ppc_hash_table (info);
e7d1c40c
AM
4731 htab->elf.dynobj = params->stub_bfd;
4732 htab->params = params;
bfeb4a28 4733
bfeb4a28 4734 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4735}
4736
721956f4
AM
4737/* Build a name for an entry in the stub hash table. */
4738
4739static char *
4ce794b7
AM
4740ppc_stub_name (const asection *input_section,
4741 const asection *sym_sec,
4742 const struct ppc_link_hash_entry *h,
4743 const Elf_Internal_Rela *rel)
721956f4
AM
4744{
4745 char *stub_name;
bcaa2f82 4746 ssize_t len;
721956f4
AM
4747
4748 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4749 offsets from a sym as a branch target? In fact, we could
4750 probably assume the addend is always zero. */
4751 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4752
4753 if (h)
4754 {
4755 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4756 stub_name = bfd_malloc (len);
46de2a7c
AM
4757 if (stub_name == NULL)
4758 return stub_name;
4759
bcaa2f82
AM
4760 len = sprintf (stub_name, "%08x.%s+%x",
4761 input_section->id & 0xffffffff,
4762 h->elf.root.root.string,
4763 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4764 }
4765 else
4766 {
ad8e1ba5 4767 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4768 stub_name = bfd_malloc (len);
46de2a7c
AM
4769 if (stub_name == NULL)
4770 return stub_name;
4771
bcaa2f82
AM
4772 len = sprintf (stub_name, "%08x.%x:%x+%x",
4773 input_section->id & 0xffffffff,
4774 sym_sec->id & 0xffffffff,
4775 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4776 (int) rel->r_addend & 0xffffffff);
721956f4 4777 }
bcaa2f82 4778 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4779 stub_name[len - 2] = 0;
721956f4
AM
4780 return stub_name;
4781}
4782
4783/* Look up an entry in the stub hash. Stub entries are cached because
4784 creating the stub name takes a bit of time. */
4785
4786static struct ppc_stub_hash_entry *
4ce794b7
AM
4787ppc_get_stub_entry (const asection *input_section,
4788 const asection *sym_sec,
039b3fef 4789 struct ppc_link_hash_entry *h,
4ce794b7
AM
4790 const Elf_Internal_Rela *rel,
4791 struct ppc_link_hash_table *htab)
721956f4
AM
4792{
4793 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4794 struct map_stub *group;
721956f4
AM
4795
4796 /* If this input section is part of a group of sections sharing one
4797 stub section, then use the id of the first section in the group.
4798 Stub names need to include a section id, as there may well be
4799 more than one stub used to reach say, printf, and we need to
4800 distinguish between them. */
6f20ed8a 4801 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4802 if (group == NULL)
4803 return NULL;
721956f4 4804
b3fac117
AM
4805 if (h != NULL && h->u.stub_cache != NULL
4806 && h->u.stub_cache->h == h
6f20ed8a 4807 && h->u.stub_cache->group == group)
721956f4 4808 {
b3fac117 4809 stub_entry = h->u.stub_cache;
721956f4
AM
4810 }
4811 else
4812 {
4813 char *stub_name;
4814
6f20ed8a 4815 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4816 if (stub_name == NULL)
4817 return NULL;
4818
4819 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4820 stub_name, FALSE, FALSE);
721956f4 4821 if (h != NULL)
b3fac117 4822 h->u.stub_cache = stub_entry;
721956f4
AM
4823
4824 free (stub_name);
4825 }
4826
4827 return stub_entry;
4828}
4829
4830/* Add a new stub entry to the stub hash. Not all fields of the new
4831 stub entry are initialised. */
4832
4833static struct ppc_stub_hash_entry *
4ce794b7
AM
4834ppc_add_stub (const char *stub_name,
4835 asection *section,
25f53a85 4836 struct bfd_link_info *info)
721956f4 4837{
25f53a85 4838 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4839 struct map_stub *group;
721956f4
AM
4840 asection *link_sec;
4841 asection *stub_sec;
4842 struct ppc_stub_hash_entry *stub_entry;
4843
6f20ed8a
AM
4844 group = htab->sec_info[section->id].u.group;
4845 link_sec = group->link_sec;
4846 stub_sec = group->stub_sec;
721956f4
AM
4847 if (stub_sec == NULL)
4848 {
6f20ed8a
AM
4849 size_t namelen;
4850 bfd_size_type len;
4851 char *s_name;
721956f4 4852
6f20ed8a
AM
4853 namelen = strlen (link_sec->name);
4854 len = namelen + sizeof (STUB_SUFFIX);
4855 s_name = bfd_alloc (htab->params->stub_bfd, len);
4856 if (s_name == NULL)
4857 return NULL;
721956f4 4858
6f20ed8a
AM
4859 memcpy (s_name, link_sec->name, namelen);
4860 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4861 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4862 if (stub_sec == NULL)
4863 return NULL;
4864 group->stub_sec = stub_sec;
721956f4
AM
4865 }
4866
4867 /* Enter this entry into the linker stub hash table. */
4868 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4869 TRUE, FALSE);
721956f4
AM
4870 if (stub_entry == NULL)
4871 {
695344c0 4872 /* xgettext:c-format */
cf97bcb0
AM
4873 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4874 section->owner, stub_name);
721956f4
AM
4875 return NULL;
4876 }
4877
6f20ed8a 4878 stub_entry->group = group;
721956f4 4879 stub_entry->stub_offset = 0;
721956f4
AM
4880 return stub_entry;
4881}
4882
e717da7e
AM
4883/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4884 not already done. */
65f38f15 4885
b34976b6 4886static bfd_boolean
e717da7e 4887create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4888{
e717da7e
AM
4889 asection *got, *relgot;
4890 flagword flags;
4891 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4892
0c8d6e5c 4893 if (!is_ppc64_elf (abfd))
0ffa91dd 4894 return FALSE;
4dfe6ac6
NC
4895 if (htab == NULL)
4896 return FALSE;
0ffa91dd 4897
33e44f2e
AM
4898 if (!htab->elf.sgot
4899 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4900 return FALSE;
e717da7e
AM
4901
4902 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4903 | SEC_LINKER_CREATED);
4904
c456f082 4905 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4906 if (!got
e717da7e
AM
4907 || !bfd_set_section_alignment (abfd, got, 3))
4908 return FALSE;
65f38f15 4909
c456f082
AM
4910 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4911 flags | SEC_READONLY);
e717da7e 4912 if (!relgot
2cdcc330 4913 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4914 return FALSE;
e717da7e
AM
4915
4916 ppc64_elf_tdata (abfd)->got = got;
4917 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4918 return TRUE;
65f38f15 4919}
5bd4f169 4920
b31867b6
AM
4921/* Follow indirect and warning symbol links. */
4922
4923static inline struct bfd_link_hash_entry *
4924follow_link (struct bfd_link_hash_entry *h)
4925{
4926 while (h->type == bfd_link_hash_indirect
4927 || h->type == bfd_link_hash_warning)
4928 h = h->u.i.link;
4929 return h;
4930}
4931
4932static inline struct elf_link_hash_entry *
4933elf_follow_link (struct elf_link_hash_entry *h)
4934{
4935 return (struct elf_link_hash_entry *) follow_link (&h->root);
4936}
4937
4938static inline struct ppc_link_hash_entry *
4939ppc_follow_link (struct ppc_link_hash_entry *h)
4940{
4941 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4942}
4943
40d16e0b
AM
4944/* Merge PLT info on FROM with that on TO. */
4945
4946static void
4947move_plt_plist (struct ppc_link_hash_entry *from,
4948 struct ppc_link_hash_entry *to)
4949{
4950 if (from->elf.plt.plist != NULL)
4951 {
4952 if (to->elf.plt.plist != NULL)
4953 {
4954 struct plt_entry **entp;
4955 struct plt_entry *ent;
4956
4957 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4958 {
4959 struct plt_entry *dent;
4960
4961 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4962 if (dent->addend == ent->addend)
4963 {
4964 dent->plt.refcount += ent->plt.refcount;
4965 *entp = ent->next;
4966 break;
4967 }
4968 if (dent == NULL)
4969 entp = &ent->next;
4970 }
4971 *entp = to->elf.plt.plist;
4972 }
4973
4974 to->elf.plt.plist = from->elf.plt.plist;
4975 from->elf.plt.plist = NULL;
4976 }
4977}
4978
65f38f15
AM
4979/* Copy the extra info we tack onto an elf_link_hash_entry. */
4980
4981static void
fcfa13d2
AM
4982ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4983 struct elf_link_hash_entry *dir,
4984 struct elf_link_hash_entry *ind)
65f38f15
AM
4985{
4986 struct ppc_link_hash_entry *edir, *eind;
4987
4988 edir = (struct ppc_link_hash_entry *) dir;
4989 eind = (struct ppc_link_hash_entry *) ind;
4990
c79d6685
AM
4991 edir->is_func |= eind->is_func;
4992 edir->is_func_descriptor |= eind->is_func_descriptor;
4993 edir->tls_mask |= eind->tls_mask;
4994 if (eind->oh != NULL)
4995 edir->oh = ppc_follow_link (eind->oh);
4996
474436e6 4997 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4998 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4999 edir->elf.ref_regular |= eind->elf.ref_regular;
5000 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 5001 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
5002 edir->elf.needs_plt |= eind->elf.needs_plt;
5003 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 5004
d311bc8b
AM
5005 /* If we were called to copy over info for a weak sym, don't copy
5006 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
5007 in order to simplify readonly_dynrelocs and save a field in the
5008 symbol hash entry, but that means dyn_relocs can't be used in any
5009 tests about a specific symbol, or affect other symbol flags which
ab2477e1 5010 are then tested. */
d311bc8b 5011 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 5012 return;
d311bc8b 5013
411e1bfb 5014 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 5015 if (eind->dyn_relocs != NULL)
65f38f15 5016 {
bbd7ec4a
AM
5017 if (edir->dyn_relocs != NULL)
5018 {
6061a67d
AM
5019 struct elf_dyn_relocs **pp;
5020 struct elf_dyn_relocs *p;
bbd7ec4a 5021
fcfa13d2 5022 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
5023 list. Merge any entries against the same section. */
5024 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5025 {
6061a67d 5026 struct elf_dyn_relocs *q;
bbd7ec4a
AM
5027
5028 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5029 if (q->sec == p->sec)
5030 {
5031 q->pc_count += p->pc_count;
5032 q->count += p->count;
5033 *pp = p->next;
5034 break;
5035 }
5036 if (q == NULL)
5037 pp = &p->next;
5038 }
5039 *pp = edir->dyn_relocs;
5040 }
5041
65f38f15
AM
5042 edir->dyn_relocs = eind->dyn_relocs;
5043 eind->dyn_relocs = NULL;
5044 }
65f38f15 5045
81848ca0
AM
5046 /* Copy over got entries that we may have already seen to the
5047 symbol which just became indirect. */
411e1bfb
AM
5048 if (eind->elf.got.glist != NULL)
5049 {
5050 if (edir->elf.got.glist != NULL)
5051 {
5052 struct got_entry **entp;
5053 struct got_entry *ent;
5054
5055 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
5056 {
5057 struct got_entry *dent;
5058
5059 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
5060 if (dent->addend == ent->addend
e717da7e 5061 && dent->owner == ent->owner
411e1bfb
AM
5062 && dent->tls_type == ent->tls_type)
5063 {
5064 dent->got.refcount += ent->got.refcount;
5065 *entp = ent->next;
5066 break;
5067 }
5068 if (dent == NULL)
5069 entp = &ent->next;
5070 }
5071 *entp = edir->elf.got.glist;
5072 }
5073
5074 edir->elf.got.glist = eind->elf.got.glist;
5075 eind->elf.got.glist = NULL;
5076 }
5077
5078 /* And plt entries. */
40d16e0b 5079 move_plt_plist (eind, edir);
411e1bfb 5080
fcfa13d2 5081 if (eind->elf.dynindx != -1)
411e1bfb 5082 {
fcfa13d2
AM
5083 if (edir->elf.dynindx != -1)
5084 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
5085 edir->elf.dynstr_index);
411e1bfb
AM
5086 edir->elf.dynindx = eind->elf.dynindx;
5087 edir->elf.dynstr_index = eind->elf.dynstr_index;
5088 eind->elf.dynindx = -1;
5089 eind->elf.dynstr_index = 0;
5090 }
411e1bfb
AM
5091}
5092
8387904d
AM
5093/* Find the function descriptor hash entry from the given function code
5094 hash entry FH. Link the entries via their OH fields. */
5095
5096static struct ppc_link_hash_entry *
b31867b6 5097lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
5098{
5099 struct ppc_link_hash_entry *fdh = fh->oh;
5100
5101 if (fdh == NULL)
5102 {
5103 const char *fd_name = fh->elf.root.root.string + 1;
5104
5105 fdh = (struct ppc_link_hash_entry *)
5106 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
5107 if (fdh == NULL)
5108 return fdh;
5109
5110 fdh->is_func_descriptor = 1;
5111 fdh->oh = fh;
5112 fh->is_func = 1;
5113 fh->oh = fdh;
8387904d
AM
5114 }
5115
8c5b4e52
AM
5116 fdh = ppc_follow_link (fdh);
5117 fdh->is_func_descriptor = 1;
5118 fdh->oh = fh;
5119 return fdh;
8387904d
AM
5120}
5121
8c5b4e52 5122/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
5123
5124static struct ppc_link_hash_entry *
5125make_fdh (struct bfd_link_info *info,
908b32fc 5126 struct ppc_link_hash_entry *fh)
bb700d78 5127{
8c5b4e52
AM
5128 bfd *abfd = fh->elf.root.u.undef.abfd;
5129 struct bfd_link_hash_entry *bh = NULL;
bb700d78 5130 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
5131 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5132 ? BSF_WEAK
5133 : BSF_GLOBAL);
5134
5135 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5136 fh->elf.root.root.string + 1,
5137 flags, bfd_und_section_ptr, 0,
5138 NULL, FALSE, FALSE, &bh))
bb700d78
AM
5139 return NULL;
5140
5141 fdh = (struct ppc_link_hash_entry *) bh;
5142 fdh->elf.non_elf = 0;
908b32fc
AM
5143 fdh->fake = 1;
5144 fdh->is_func_descriptor = 1;
5145 fdh->oh = fh;
5146 fh->is_func = 1;
5147 fh->oh = fdh;
bb700d78
AM
5148 return fdh;
5149}
5150
8387904d
AM
5151/* Fix function descriptor symbols defined in .opd sections to be
5152 function type. */
555cd476
AM
5153
5154static bfd_boolean
c16153ae 5155ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 5156 struct bfd_link_info *info,
555cd476 5157 Elf_Internal_Sym *isym,
6911b7dc 5158 const char **name,
555cd476
AM
5159 flagword *flags ATTRIBUTE_UNUSED,
5160 asection **sec,
b53dfeb2 5161 bfd_vma *value)
555cd476 5162{
b53dfeb2 5163 if (*sec != NULL
f1885d1e 5164 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
5165 {
5166 asection *code_sec;
5167
5168 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5169 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5170 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5171
5172 /* If the symbol is a function defined in .opd, and the function
5173 code is in a discarded group, let it appear to be undefined. */
0e1862bb 5174 if (!bfd_link_relocatable (info)
b53dfeb2
AM
5175 && (*sec)->reloc_count != 0
5176 && opd_entry_value (*sec, *value, &code_sec, NULL,
5177 FALSE) != (bfd_vma) -1
5178 && discarded_section (code_sec))
5179 {
5180 *sec = bfd_und_section_ptr;
5181 isym->st_shndx = SHN_UNDEF;
5182 }
5183 }
dbd1e97e
AM
5184 else if (*sec != NULL
5185 && strcmp ((*sec)->name, ".toc") == 0
5186 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5187 {
5188 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5189 if (htab != NULL)
5190 htab->params->object_in_toc = 1;
5191 }
433817dd 5192
6911b7dc
AM
5193 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5194 {
5195 if (abiversion (ibfd) == 0)
5196 set_abiversion (ibfd, 2);
5197 else if (abiversion (ibfd) == 1)
5198 {
cf97bcb0
AM
5199 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5200 " for ABI version 1"), *name);
6911b7dc
AM
5201 bfd_set_error (bfd_error_bad_value);
5202 return FALSE;
5203 }
5204 }
5205
555cd476
AM
5206 return TRUE;
5207}
5208
6911b7dc
AM
5209/* Merge non-visibility st_other attributes: local entry point. */
5210
5211static void
5212ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5213 const Elf_Internal_Sym *isym,
5214 bfd_boolean definition,
5215 bfd_boolean dynamic)
5216{
f378ab09 5217 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5218 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5219 | ELF_ST_VISIBILITY (h->other));
5220}
5221
8c5b4e52
AM
5222/* Hook called on merging a symbol. We use this to clear "fake" since
5223 we now have a real symbol. */
5224
5225static bfd_boolean
5226ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5227 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5228 asection **psec ATTRIBUTE_UNUSED,
5229 bfd_boolean newdef ATTRIBUTE_UNUSED,
5230 bfd_boolean olddef ATTRIBUTE_UNUSED,
5231 bfd *oldbfd ATTRIBUTE_UNUSED,
5232 const asection *oldsec ATTRIBUTE_UNUSED)
5233{
5234 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5235 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5236 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5237 return TRUE;
5238}
5239
8387904d 5240/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5241 inclusion of a new ABI object archive that defines "bar".
5242 NAME is a symbol defined in an archive. Return a symbol in the hash
5243 table that might be satisfied by the archive symbols. */
8387904d
AM
5244
5245static struct elf_link_hash_entry *
5246ppc64_elf_archive_symbol_lookup (bfd *abfd,
5247 struct bfd_link_info *info,
5248 const char *name)
5249{
5250 struct elf_link_hash_entry *h;
5251 char *dot_name;
5252 size_t len;
5253
5254 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5255 if (h != NULL
5256 /* Don't return this sym if it is a fake function descriptor
5257 created by add_symbol_adjust. */
8c5b4e52 5258 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5259 return h;
5260
5261 if (name[0] == '.')
5262 return h;
5263
5264 len = strlen (name);
5265 dot_name = bfd_alloc (abfd, len + 2);
5266 if (dot_name == NULL)
e99955cd 5267 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5268 dot_name[0] = '.';
5269 memcpy (dot_name + 1, name, len + 1);
5270 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5271 bfd_release (abfd, dot_name);
5272 return h;
5273}
5274
5275/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5276 new ABI object defines "bar". Well, at least, undefined dot symbols
5277 are made weak. This stops later archive searches from including an
5278 object if we already have a function descriptor definition. It also
35b0ce59
AM
5279 prevents the linker complaining about undefined symbols.
5280 We also check and correct mismatched symbol visibility here. The
5281 most restrictive visibility of the function descriptor and the
5282 function entry symbol is used. */
8387904d
AM
5283
5284static bfd_boolean
b3fac117 5285add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5286{
8387904d
AM
5287 struct ppc_link_hash_table *htab;
5288 struct ppc_link_hash_entry *fdh;
5289
b3fac117
AM
5290 if (eh->elf.root.type == bfd_link_hash_warning)
5291 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5292
8400d40d
AM
5293 if (eh->elf.root.type == bfd_link_hash_indirect)
5294 return TRUE;
5295
b3fac117
AM
5296 if (eh->elf.root.root.string[0] != '.')
5297 abort ();
8387904d 5298
b3fac117 5299 htab = ppc_hash_table (info);
4dfe6ac6
NC
5300 if (htab == NULL)
5301 return FALSE;
5302
b31867b6 5303 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5304 if (fdh == NULL
5305 && !bfd_link_relocatable (info)
5306 && (eh->elf.root.type == bfd_link_hash_undefined
5307 || eh->elf.root.type == bfd_link_hash_undefweak)
5308 && eh->elf.ref_regular)
5309 {
5310 /* Make an undefined function descriptor sym, in order to
5311 pull in an --as-needed shared lib. Archives are handled
5312 elsewhere. */
5313 fdh = make_fdh (info, eh);
5314 if (fdh == NULL)
5315 return FALSE;
bb700d78 5316 }
8c5b4e52
AM
5317
5318 if (fdh != NULL)
8387904d 5319 {
35b0ce59
AM
5320 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5321 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5322
5323 /* Make both descriptor and entry symbol have the most
5324 constraining visibility of either symbol. */
35b0ce59
AM
5325 if (entry_vis < descr_vis)
5326 fdh->elf.other += entry_vis - descr_vis;
5327 else if (entry_vis > descr_vis)
5328 eh->elf.other += descr_vis - entry_vis;
5329
8c5b4e52
AM
5330 /* Propagate reference flags from entry symbol to function
5331 descriptor symbol. */
bc4e12de 5332 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5333 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5334 fdh->elf.ref_regular |= eh->elf.ref_regular;
5335 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5336
5337 if (!fdh->elf.forced_local
5338 && fdh->elf.dynindx == -1
5339 && fdh->elf.versioned != versioned_hidden
5340 && (bfd_link_dll (info)
5341 || fdh->elf.def_dynamic
5342 || fdh->elf.ref_dynamic)
5343 && (eh->elf.ref_regular
5344 || eh->elf.def_regular))
5345 {
2cdcc330 5346 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 5347 return FALSE;
35b0ce59 5348 }
8387904d 5349 }
99877b66 5350
8387904d
AM
5351 return TRUE;
5352}
5353
f6c7c3e8
AM
5354/* Set up opd section info and abiversion for IBFD, and process list
5355 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5356
8387904d 5357static bfd_boolean
f6c7c3e8 5358ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5359{
99877b66 5360 struct ppc_link_hash_table *htab;
b3fac117 5361 struct ppc_link_hash_entry **p, *eh;
459609d6 5362 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5363
459609d6 5364 if (opd != NULL && opd->size != 0)
b3fac117 5365 {
b9399fcf
AM
5366 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5367 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5368
459609d6
AM
5369 if (abiversion (ibfd) == 0)
5370 set_abiversion (ibfd, 1);
8a2058b5 5371 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5372 {
695344c0 5373 /* xgettext:c-format */
cf97bcb0
AM
5374 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5375 ibfd, abiversion (ibfd));
459609d6
AM
5376 bfd_set_error (bfd_error_bad_value);
5377 return FALSE;
f6c7c3e8 5378 }
b9399fcf 5379 }
f6c7c3e8 5380
b9399fcf
AM
5381 if (is_ppc64_elf (info->output_bfd))
5382 {
5383 /* For input files without an explicit abiversion in e_flags
5384 we should have flagged any with symbol st_other bits set
5385 as ELFv1 and above flagged those with .opd as ELFv2.
5386 Set the output abiversion if not yet set, and for any input
5387 still ambiguous, take its abiversion from the output.
5388 Differences in ABI are reported later. */
5389 if (abiversion (info->output_bfd) == 0)
5390 set_abiversion (info->output_bfd, abiversion (ibfd));
5391 else if (abiversion (ibfd) == 0)
5392 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5393 }
5394
459609d6
AM
5395 htab = ppc_hash_table (info);
5396 if (htab == NULL)
b9399fcf 5397 return TRUE;
459609d6 5398
b9399fcf
AM
5399 if (opd != NULL && opd->size != 0
5400 && (ibfd->flags & DYNAMIC) == 0
5401 && (opd->flags & SEC_RELOC) != 0
5402 && opd->reloc_count != 0
5403 && !bfd_is_abs_section (opd->output_section)
5404 && info->gc_sections)
5405 {
5406 /* Garbage collection needs some extra help with .opd sections.
5407 We don't want to necessarily keep everything referenced by
5408 relocs in .opd, as that would keep all functions. Instead,
5409 if we reference an .opd symbol (a function descriptor), we
5410 want to keep the function code symbol's section. This is
5411 easy for global symbols, but for local syms we need to keep
5412 information about the associated function section. */
5413 bfd_size_type amt;
5414 asection **opd_sym_map;
5415 Elf_Internal_Shdr *symtab_hdr;
5416 Elf_Internal_Rela *relocs, *rel_end, *rel;
5417
5418 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5419 opd_sym_map = bfd_zalloc (ibfd, amt);
5420 if (opd_sym_map == NULL)
5421 return FALSE;
5422 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5423 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5424 info->keep_memory);
5425 if (relocs == NULL)
5426 return FALSE;
5427 symtab_hdr = &elf_symtab_hdr (ibfd);
5428 rel_end = relocs + opd->reloc_count - 1;
5429 for (rel = relocs; rel < rel_end; rel++)
5430 {
5431 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5432 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5433
5434 if (r_type == R_PPC64_ADDR64
5435 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5436 && r_symndx < symtab_hdr->sh_info)
5437 {
5438 Elf_Internal_Sym *isym;
5439 asection *s;
5440
5441 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5442 if (isym == NULL)
5443 {
5444 if (elf_section_data (opd)->relocs != relocs)
5445 free (relocs);
5446 return FALSE;
5447 }
5448
5449 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5450 if (s != NULL && s != opd)
5451 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5452 }
5453 }
5454 if (elf_section_data (opd)->relocs != relocs)
5455 free (relocs);
5456 }
459609d6
AM
5457
5458 p = &htab->dot_syms;
5459 while ((eh = *p) != NULL)
5460 {
5461 *p = NULL;
5462 if (&eh->elf == htab->elf.hgot)
5463 ;
5464 else if (htab->elf.hgot == NULL
5465 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5466 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5467 else if (abiversion (ibfd) <= 1)
5468 {
5469 htab->need_func_desc_adj = 1;
5470 if (!add_symbol_adjust (eh, info))
5471 return FALSE;
5472 }
459609d6
AM
5473 p = &eh->u.next_dot_sym;
5474 }
b3fac117 5475 return TRUE;
8387904d
AM
5476}
5477
97fed1c9
JJ
5478/* Undo hash table changes when an --as-needed input file is determined
5479 not to be needed. */
5480
5481static bfd_boolean
e5034e59
AM
5482ppc64_elf_notice_as_needed (bfd *ibfd,
5483 struct bfd_link_info *info,
5484 enum notice_asneeded_action act)
97fed1c9 5485{
e5034e59
AM
5486 if (act == notice_not_needed)
5487 {
5488 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5489
e5034e59
AM
5490 if (htab == NULL)
5491 return FALSE;
4dfe6ac6 5492
e5034e59
AM
5493 htab->dot_syms = NULL;
5494 }
5495 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5496}
5497
aa374f67
AM
5498/* If --just-symbols against a final linked binary, then assume we need
5499 toc adjusting stubs when calling functions defined there. */
5500
5501static void
5502ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5503{
5504 if ((sec->flags & SEC_CODE) != 0
5505 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5506 && is_ppc64_elf (sec->owner))
5507 {
2c3f079f
AM
5508 if (abiversion (sec->owner) >= 2
5509 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5510 sec->has_toc_reloc = 1;
5511 }
5512 _bfd_elf_link_just_syms (sec, info);
5513}
5514
e054468f 5515static struct plt_entry **
4ce794b7
AM
5516update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5517 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5518{
5519 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5520 struct plt_entry **local_plt;
f961d9dd 5521 unsigned char *local_got_tls_masks;
411e1bfb
AM
5522
5523 if (local_got_ents == NULL)
5524 {
5525 bfd_size_type size = symtab_hdr->sh_info;
5526
e054468f
AM
5527 size *= (sizeof (*local_got_ents)
5528 + sizeof (*local_plt)
5529 + sizeof (*local_got_tls_masks));
4ce794b7 5530 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5531 if (local_got_ents == NULL)
e054468f 5532 return NULL;
411e1bfb
AM
5533 elf_local_got_ents (abfd) = local_got_ents;
5534 }
5535
37da22e5 5536 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5537 {
5538 struct got_entry *ent;
5539
5540 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5541 if (ent->addend == r_addend
5542 && ent->owner == abfd
5543 && ent->tls_type == tls_type)
411e1bfb
AM
5544 break;
5545 if (ent == NULL)
5546 {
5547 bfd_size_type amt = sizeof (*ent);
4ce794b7 5548 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5549 if (ent == NULL)
5550 return FALSE;
5551 ent->next = local_got_ents[r_symndx];
5552 ent->addend = r_addend;
e717da7e 5553 ent->owner = abfd;
411e1bfb 5554 ent->tls_type = tls_type;
927be08e 5555 ent->is_indirect = FALSE;
411e1bfb
AM
5556 ent->got.refcount = 0;
5557 local_got_ents[r_symndx] = ent;
5558 }
5559 ent->got.refcount += 1;
5560 }
5561
e054468f 5562 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5563 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 5564 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
5565
5566 return local_plt + r_symndx;
65f38f15
AM
5567}
5568
411e1bfb 5569static bfd_boolean
e054468f 5570update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5571{
411e1bfb 5572 struct plt_entry *ent;
1e2f5b6e 5573
e054468f 5574 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5575 if (ent->addend == addend)
5576 break;
5577 if (ent == NULL)
1e2f5b6e 5578 {
411e1bfb 5579 bfd_size_type amt = sizeof (*ent);
4ce794b7 5580 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5581 if (ent == NULL)
5582 return FALSE;
e054468f 5583 ent->next = *plist;
411e1bfb
AM
5584 ent->addend = addend;
5585 ent->plt.refcount = 0;
e054468f 5586 *plist = ent;
1e2f5b6e 5587 }
411e1bfb 5588 ent->plt.refcount += 1;
b34976b6 5589 return TRUE;
1e2f5b6e
AM
5590}
5591
e054468f
AM
5592static bfd_boolean
5593is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5594{
5595 return (r_type == R_PPC64_REL24
05d0e962 5596 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
5597 || r_type == R_PPC64_REL14
5598 || r_type == R_PPC64_REL14_BRTAKEN
5599 || r_type == R_PPC64_REL14_BRNTAKEN
5600 || r_type == R_PPC64_ADDR24
5601 || r_type == R_PPC64_ADDR14
5602 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d
AM
5603 || r_type == R_PPC64_ADDR14_BRNTAKEN
5604 || r_type == R_PPC64_PLTCALL);
5605}
5606
5607/* Relocs on inline plt call sequence insns prior to the call. */
5608
5609static bfd_boolean
5610is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5611{
5612 return (r_type == R_PPC64_PLT16_HA
5613 || r_type == R_PPC64_PLT16_HI
5614 || r_type == R_PPC64_PLT16_LO
5615 || r_type == R_PPC64_PLT16_LO_DS
5616 || r_type == R_PPC64_PLTSEQ);
e054468f
AM
5617}
5618
5bd4f169 5619/* Look through the relocs for a section during the first phase, and
65f38f15 5620 calculate needed space in the global offset table, procedure
5d1634d7 5621 linkage table, and dynamic reloc sections. */
5bd4f169 5622
b34976b6 5623static bfd_boolean
4ce794b7
AM
5624ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5625 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5626{
65f38f15 5627 struct ppc_link_hash_table *htab;
5bd4f169 5628 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5629 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5630 const Elf_Internal_Rela *rel;
5631 const Elf_Internal_Rela *rel_end;
5bd4f169 5632 asection *sreloc;
3a71aa26 5633 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5634 bfd_boolean is_opd;
5bd4f169 5635
0e1862bb 5636 if (bfd_link_relocatable (info))
b34976b6 5637 return TRUE;
5bd4f169 5638
680a3378
AM
5639 /* Don't do anything special with non-loaded, non-alloced sections.
5640 In particular, any relocs in such sections should not affect GOT
5641 and PLT reference counting (ie. we don't allow them to create GOT
5642 or PLT entries), there's no possibility or desire to optimize TLS
5643 relocs, and there's not much point in propagating relocs to shared
5644 libs that the dynamic linker won't relocate. */
5645 if ((sec->flags & SEC_ALLOC) == 0)
5646 return TRUE;
5647
0c8d6e5c 5648 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5649
65f38f15 5650 htab = ppc_hash_table (info);
4dfe6ac6
NC
5651 if (htab == NULL)
5652 return FALSE;
5653
3a71aa26
AM
5654 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5655 FALSE, FALSE, TRUE);
5656 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5657 FALSE, FALSE, TRUE);
0ffa91dd 5658 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5659 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5660 sreloc = NULL;
b9399fcf 5661 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5662 rel_end = relocs + sec->reloc_count;
5663 for (rel = relocs; rel < rel_end; rel++)
5664 {
5665 unsigned long r_symndx;
5666 struct elf_link_hash_entry *h;
04c9666a 5667 enum elf_ppc64_reloc_type r_type;
727fc41e 5668 int tls_type;
7c8fe5c4 5669 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5670 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5671
5672 r_symndx = ELF64_R_SYM (rel->r_info);
5673 if (r_symndx < symtab_hdr->sh_info)
5674 h = NULL;
5675 else
973a3492
L
5676 {
5677 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5678 h = elf_follow_link (h);
1c865ab2
AM
5679
5680 if (h == htab->elf.hgot)
5681 sec->has_toc_reloc = 1;
973a3492 5682 }
5bd4f169 5683
727fc41e 5684 tls_type = 0;
e054468f 5685 ifunc = NULL;
25f23106
AM
5686 if (h != NULL)
5687 {
5688 if (h->type == STT_GNU_IFUNC)
5689 {
5690 h->needs_plt = 1;
5691 ifunc = &h->plt.plist;
5692 }
5693 }
5694 else
5695 {
5696 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5697 abfd, r_symndx);
5698 if (isym == NULL)
5699 return FALSE;
5700
5701 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5702 {
5703 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5
AM
5704 rel->r_addend,
5705 NON_GOT | PLT_IFUNC);
25f23106
AM
5706 if (ifunc == NULL)
5707 return FALSE;
5708 }
5709 }
727fc41e 5710
cbf95972 5711 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5712 switch (r_type)
5bd4f169 5713 {
727fc41e
AM
5714 case R_PPC64_TLSGD:
5715 case R_PPC64_TLSLD:
5716 /* These special tls relocs tie a call to __tls_get_addr with
5717 its parameter symbol. */
37da22e5
AM
5718 if (h != NULL)
5719 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5720 else
5721 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5722 rel->r_addend,
5723 NON_GOT | TLS_TLS | TLS_MARK))
5724 return FALSE;
5725 sec->has_tls_reloc = 1;
727fc41e
AM
5726 break;
5727
411e1bfb
AM
5728 case R_PPC64_GOT_TLSLD16:
5729 case R_PPC64_GOT_TLSLD16_LO:
5730 case R_PPC64_GOT_TLSLD16_HI:
5731 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5732 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5733 goto dogottls;
5734
5735 case R_PPC64_GOT_TLSGD16:
5736 case R_PPC64_GOT_TLSGD16_LO:
5737 case R_PPC64_GOT_TLSGD16_HI:
5738 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5739 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5740 goto dogottls;
5741
5742 case R_PPC64_GOT_TPREL16_DS:
5743 case R_PPC64_GOT_TPREL16_LO_DS:
5744 case R_PPC64_GOT_TPREL16_HI:
5745 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5746 if (bfd_link_dll (info))
411e1bfb
AM
5747 info->flags |= DF_STATIC_TLS;
5748 tls_type = TLS_TLS | TLS_TPREL;
5749 goto dogottls;
5750
5751 case R_PPC64_GOT_DTPREL16_DS:
5752 case R_PPC64_GOT_DTPREL16_LO_DS:
5753 case R_PPC64_GOT_DTPREL16_HI:
5754 case R_PPC64_GOT_DTPREL16_HA:
5755 tls_type = TLS_TLS | TLS_DTPREL;
5756 dogottls:
5757 sec->has_tls_reloc = 1;
1a0670f3 5758 /* Fall through */
411e1bfb 5759
5bd4f169 5760 case R_PPC64_GOT16:
5bd4f169 5761 case R_PPC64_GOT16_DS:
65f38f15
AM
5762 case R_PPC64_GOT16_HA:
5763 case R_PPC64_GOT16_HI:
5764 case R_PPC64_GOT16_LO:
5bd4f169 5765 case R_PPC64_GOT16_LO_DS:
65f38f15 5766 /* This symbol requires a global offset table entry. */
4c52953f 5767 sec->has_toc_reloc = 1;
33c0ec9d
AM
5768 if (r_type == R_PPC64_GOT_TLSLD16
5769 || r_type == R_PPC64_GOT_TLSGD16
5770 || r_type == R_PPC64_GOT_TPREL16_DS
5771 || r_type == R_PPC64_GOT_DTPREL16_DS
5772 || r_type == R_PPC64_GOT16
5773 || r_type == R_PPC64_GOT16_DS)
5774 {
5775 htab->do_multi_toc = 1;
d77c8a4b 5776 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5777 }
5778
e717da7e
AM
5779 if (ppc64_elf_tdata (abfd)->got == NULL
5780 && !create_got_section (abfd, info))
b34976b6 5781 return FALSE;
5bd4f169
AM
5782
5783 if (h != NULL)
5784 {
411e1bfb
AM
5785 struct ppc_link_hash_entry *eh;
5786 struct got_entry *ent;
65f38f15 5787
411e1bfb
AM
5788 eh = (struct ppc_link_hash_entry *) h;
5789 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5790 if (ent->addend == rel->r_addend
e717da7e 5791 && ent->owner == abfd
411e1bfb
AM
5792 && ent->tls_type == tls_type)
5793 break;
5794 if (ent == NULL)
5bd4f169 5795 {
411e1bfb 5796 bfd_size_type amt = sizeof (*ent);
4ce794b7 5797 ent = bfd_alloc (abfd, amt);
411e1bfb 5798 if (ent == NULL)
b34976b6 5799 return FALSE;
411e1bfb
AM
5800 ent->next = eh->elf.got.glist;
5801 ent->addend = rel->r_addend;
e717da7e 5802 ent->owner = abfd;
411e1bfb 5803 ent->tls_type = tls_type;
927be08e 5804 ent->is_indirect = FALSE;
411e1bfb
AM
5805 ent->got.refcount = 0;
5806 eh->elf.got.glist = ent;
5bd4f169 5807 }
411e1bfb 5808 ent->got.refcount += 1;
e7b938ca 5809 eh->tls_mask |= tls_type;
5bd4f169 5810 }
411e1bfb
AM
5811 else
5812 /* This is a global offset table entry for a local symbol. */
5813 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5814 rel->r_addend, tls_type))
5815 return FALSE;
a345bc8d
AM
5816
5817 /* We may also need a plt entry if the symbol turns out to be
5818 an ifunc. */
0e1862bb 5819 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5820 {
5821 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5822 return FALSE;
5823 }
5bd4f169
AM
5824 break;
5825
5bd4f169 5826 case R_PPC64_PLT16_HA:
65f38f15
AM
5827 case R_PPC64_PLT16_HI:
5828 case R_PPC64_PLT16_LO:
08be3224 5829 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
5830 case R_PPC64_PLT32:
5831 case R_PPC64_PLT64:
cbf95972
AM
5832 /* This symbol requires a procedure linkage table entry. */
5833 plt_list = ifunc;
5834 if (h != NULL)
e054468f 5835 {
e054468f
AM
5836 h->needs_plt = 1;
5837 if (h->root.root.string[0] == '.'
5838 && h->root.root.string[1] != '\0')
5839 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 5840 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5841 plt_list = &h->plt.plist;
5842 }
5843 if (plt_list == NULL)
2d7ad24e
AM
5844 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5845 rel->r_addend,
5846 NON_GOT | PLT_KEEP);
cbf95972
AM
5847 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5848 return FALSE;
5bd4f169
AM
5849 break;
5850
5851 /* The following relocations don't need to propagate the
5852 relocation if linking a shared object since they are
5853 section relative. */
5854 case R_PPC64_SECTOFF:
5855 case R_PPC64_SECTOFF_LO:
5856 case R_PPC64_SECTOFF_HI:
5857 case R_PPC64_SECTOFF_HA:
5858 case R_PPC64_SECTOFF_DS:
5859 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5860 case R_PPC64_DTPREL16:
5861 case R_PPC64_DTPREL16_LO:
5862 case R_PPC64_DTPREL16_HI:
5863 case R_PPC64_DTPREL16_HA:
5864 case R_PPC64_DTPREL16_DS:
5865 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5866 case R_PPC64_DTPREL16_HIGH:
5867 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5868 case R_PPC64_DTPREL16_HIGHER:
5869 case R_PPC64_DTPREL16_HIGHERA:
5870 case R_PPC64_DTPREL16_HIGHEST:
5871 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5872 break;
5873
ad8e1ba5 5874 /* Nor do these. */
25f23106
AM
5875 case R_PPC64_REL16:
5876 case R_PPC64_REL16_LO:
5877 case R_PPC64_REL16_HI:
5878 case R_PPC64_REL16_HA:
a680de9a 5879 case R_PPC64_REL16DX_HA:
25f23106
AM
5880 break;
5881
45965137
AM
5882 /* Not supported as a dynamic relocation. */
5883 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5884 if (bfd_link_pic (info))
45965137
AM
5885 {
5886 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5887 ppc_howto_init ();
695344c0 5888 /* xgettext:c-format */
174d0a74 5889 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5890 "in shared libraries and PIEs\n"),
45965137
AM
5891 abfd, sec, rel->r_offset,
5892 ppc64_elf_howto_table[r_type]->name);
5893 bfd_set_error (bfd_error_bad_value);
5894 return FALSE;
5895 }
5896 break;
5897
ad8e1ba5 5898 case R_PPC64_TOC16:
33c0ec9d
AM
5899 case R_PPC64_TOC16_DS:
5900 htab->do_multi_toc = 1;
d77c8a4b 5901 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5902 /* Fall through. */
ad8e1ba5
AM
5903 case R_PPC64_TOC16_LO:
5904 case R_PPC64_TOC16_HI:
5905 case R_PPC64_TOC16_HA:
ad8e1ba5 5906 case R_PPC64_TOC16_LO_DS:
4c52953f 5907 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5908 break;
5909
006589cf
AM
5910 /* Marker reloc. */
5911 case R_PPC64_ENTRY:
5912 break;
5913
5bd4f169
AM
5914 /* This relocation describes the C++ object vtable hierarchy.
5915 Reconstruct it for later use during GC. */
5916 case R_PPC64_GNU_VTINHERIT:
c152c796 5917 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5918 return FALSE;
5bd4f169
AM
5919 break;
5920
5921 /* This relocation describes which C++ vtable entries are actually
5922 used. Record for later use during GC. */
5923 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5924 BFD_ASSERT (h != NULL);
5925 if (h != NULL
5926 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5927 return FALSE;
5bd4f169
AM
5928 break;
5929
721956f4
AM
5930 case R_PPC64_REL14:
5931 case R_PPC64_REL14_BRTAKEN:
5932 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5933 {
5934 asection *dest = NULL;
5935
5936 /* Heuristic: If jumping outside our section, chances are
5937 we are going to need a stub. */
5938 if (h != NULL)
5939 {
5940 /* If the sym is weak it may be overridden later, so
5941 don't assume we know where a weak sym lives. */
5942 if (h->root.type == bfd_link_hash_defined)
5943 dest = h->root.u.def.section;
5944 }
5945 else
87d72d41
AM
5946 {
5947 Elf_Internal_Sym *isym;
5948
5949 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5950 abfd, r_symndx);
5951 if (isym == NULL)
5952 return FALSE;
5953
5954 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5955 }
5956
220c76dd 5957 if (dest != sec)
7c8fe5c4 5958 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5959 }
3e04d765
AM
5960 goto rel24;
5961
5962 case R_PPC64_PLTCALL:
5963 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5964 /* Fall through. */
5965
5d1634d7 5966 case R_PPC64_REL24:
05d0e962 5967 case R_PPC64_REL24_NOTOC:
3e04d765 5968 rel24:
cbf95972
AM
5969 plt_list = ifunc;
5970 if (h != NULL)
5d1634d7 5971 {
e054468f
AM
5972 h->needs_plt = 1;
5973 if (h->root.root.string[0] == '.'
5974 && h->root.root.string[1] != '\0')
5975 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5976
3a71aa26 5977 if (h == tga || h == dottga)
cbf95972
AM
5978 {
5979 sec->has_tls_reloc = 1;
5980 if (rel != relocs
5981 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5982 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5983 /* We have a new-style __tls_get_addr call with
5984 a marker reloc. */
5985 ;
5986 else
5987 /* Mark this section as having an old-style call. */
5988 sec->has_tls_get_addr_call = 1;
5989 }
5990 plt_list = &h->plt.plist;
411e1bfb 5991 }
cbf95972
AM
5992
5993 /* We may need a .plt entry if the function this reloc
5994 refers to is in a shared lib. */
5995 if (plt_list
5996 && !update_plt_info (abfd, plt_list, rel->r_addend))
5997 return FALSE;
411e1bfb
AM
5998 break;
5999
cbf95972
AM
6000 case R_PPC64_ADDR14:
6001 case R_PPC64_ADDR14_BRNTAKEN:
6002 case R_PPC64_ADDR14_BRTAKEN:
6003 case R_PPC64_ADDR24:
6004 goto dodyn;
6005
411e1bfb
AM
6006 case R_PPC64_TPREL64:
6007 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 6008 if (bfd_link_dll (info))
411e1bfb
AM
6009 info->flags |= DF_STATIC_TLS;
6010 goto dotlstoc;
6011
6012 case R_PPC64_DTPMOD64:
6013 if (rel + 1 < rel_end
6014 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
6015 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 6016 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 6017 else
951fd09b 6018 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
6019 goto dotlstoc;
6020
6021 case R_PPC64_DTPREL64:
6022 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
6023 if (rel != relocs
6024 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
6025 && rel[-1].r_offset == rel->r_offset - 8)
6026 /* This is the second reloc of a dtpmod, dtprel pair.
6027 Don't mark with TLS_DTPREL. */
6028 goto dodyn;
6029
6030 dotlstoc:
6031 sec->has_tls_reloc = 1;
6032 if (h != NULL)
6033 {
6034 struct ppc_link_hash_entry *eh;
6035 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6036 eh->tls_mask |= tls_type;
411e1bfb
AM
6037 }
6038 else
6039 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
6040 rel->r_addend, tls_type))
6041 return FALSE;
6042
7c8fe5c4
AM
6043 ppc64_sec = ppc64_elf_section_data (sec);
6044 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 6045 {
3a71aa26
AM
6046 bfd_size_type amt;
6047
e7b938ca 6048 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
6049 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
6050 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
6051 if (ppc64_sec->u.toc.symndx == NULL)
6052 return FALSE;
6053 amt = sec->size * sizeof (bfd_vma) / 8;
6054 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
6055 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 6056 return FALSE;
7c8fe5c4
AM
6057 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
6058 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
6059 }
6060 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
6061 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
6062 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
6063
6064 /* Mark the second slot of a GD or LD entry.
6065 -1 to indicate GD and -2 to indicate LD. */
6066 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 6067 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 6068 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 6069 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
6070 goto dodyn;
6071
6072 case R_PPC64_TPREL16:
6073 case R_PPC64_TPREL16_LO:
6074 case R_PPC64_TPREL16_HI:
6075 case R_PPC64_TPREL16_HA:
6076 case R_PPC64_TPREL16_DS:
6077 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6078 case R_PPC64_TPREL16_HIGH:
6079 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
6080 case R_PPC64_TPREL16_HIGHER:
6081 case R_PPC64_TPREL16_HIGHERA:
6082 case R_PPC64_TPREL16_HIGHEST:
6083 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
6084 if (bfd_link_dll (info))
6085 info->flags |= DF_STATIC_TLS;
6086 goto dodyn;
5d1634d7 6087
e86ce104 6088 case R_PPC64_ADDR64:
b9399fcf 6089 if (is_opd
1e2f5b6e 6090 && rel + 1 < rel_end
4ce794b7 6091 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 6092 {
8387904d 6093 if (h != NULL)
8c5b4e52 6094 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 6095 }
e86ce104
AM
6096 /* Fall through. */
6097
65f38f15
AM
6098 case R_PPC64_ADDR16:
6099 case R_PPC64_ADDR16_DS:
6100 case R_PPC64_ADDR16_HA:
6101 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6102 case R_PPC64_ADDR16_HIGH:
6103 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
6104 case R_PPC64_ADDR16_HIGHER:
6105 case R_PPC64_ADDR16_HIGHERA:
6106 case R_PPC64_ADDR16_HIGHEST:
6107 case R_PPC64_ADDR16_HIGHESTA:
6108 case R_PPC64_ADDR16_LO:
6109 case R_PPC64_ADDR16_LO_DS:
0e1862bb 6110 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
6111 && rel->r_addend == 0)
6112 {
6113 /* We may need a .plt entry if this reloc refers to a
6114 function in a shared lib. */
6115 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
6116 return FALSE;
6117 h->pointer_equality_needed = 1;
6118 }
6119 /* Fall through. */
6120
6121 case R_PPC64_REL30:
6122 case R_PPC64_REL32:
6123 case R_PPC64_REL64:
65f38f15 6124 case R_PPC64_ADDR32:
65f38f15
AM
6125 case R_PPC64_UADDR16:
6126 case R_PPC64_UADDR32:
6127 case R_PPC64_UADDR64:
5bd4f169 6128 case R_PPC64_TOC:
0e1862bb 6129 if (h != NULL && !bfd_link_pic (info))
81848ca0 6130 /* We may need a copy reloc. */
f5385ebf 6131 h->non_got_ref = 1;
81848ca0 6132
41bd81ab 6133 /* Don't propagate .opd relocs. */
b9399fcf 6134 if (NO_OPD_RELOCS && is_opd)
e86ce104 6135 break;
e86ce104 6136
65f38f15
AM
6137 /* If we are creating a shared library, and this is a reloc
6138 against a global symbol, or a non PC relative reloc
6139 against a local symbol, then we need to copy the reloc
6140 into the shared library. However, if we are linking with
6141 -Bsymbolic, we do not need to copy a reloc against a
6142 global symbol which is defined in an object we are
6143 including in the link (i.e., DEF_REGULAR is set). At
6144 this point we have not seen all the input files, so it is
6145 possible that DEF_REGULAR is not set now but will be set
6146 later (it is never cleared). In case of a weak definition,
6147 DEF_REGULAR may be cleared later by a strong definition in
6148 a shared library. We account for that possibility below by
f4656909 6149 storing information in the dyn_relocs field of the hash
65f38f15
AM
6150 table entry. A similar situation occurs when creating
6151 shared libraries and symbol visibility changes render the
6152 symbol local.
6153
6154 If on the other hand, we are creating an executable, we
6155 may need to keep relocations for symbols satisfied by a
6156 dynamic library if we manage to avoid copy relocs for the
6157 symbol. */
411e1bfb 6158 dodyn:
0e1862bb 6159 if ((bfd_link_pic (info)
1d483afe 6160 && (must_be_dyn_reloc (info, r_type)
65f38f15 6161 || (h != NULL
198f1157 6162 && (!SYMBOLIC_BIND (info, h)
65f38f15 6163 || h->root.type == bfd_link_hash_defweak
f5385ebf 6164 || !h->def_regular))))
f4656909 6165 || (ELIMINATE_COPY_RELOCS
0e1862bb 6166 && !bfd_link_pic (info)
65f38f15
AM
6167 && h != NULL
6168 && (h->root.type == bfd_link_hash_defweak
25f23106 6169 || !h->def_regular))
0e1862bb 6170 || (!bfd_link_pic (info)
25f23106 6171 && ifunc != NULL))
5bd4f169 6172 {
65f38f15
AM
6173 /* We must copy these reloc types into the output file.
6174 Create a reloc section in dynobj and make room for
6175 this reloc. */
5bd4f169
AM
6176 if (sreloc == NULL)
6177 {
83bac4b0
NC
6178 sreloc = _bfd_elf_make_dynamic_reloc_section
6179 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 6180
5bd4f169 6181 if (sreloc == NULL)
83bac4b0 6182 return FALSE;
5bd4f169
AM
6183 }
6184
65f38f15
AM
6185 /* If this is a global symbol, we count the number of
6186 relocations we need for this symbol. */
6187 if (h != NULL)
6188 {
19e08130
AM
6189 struct elf_dyn_relocs *p;
6190 struct elf_dyn_relocs **head;
6191
ec338859 6192 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
6193 p = *head;
6194 if (p == NULL || p->sec != sec)
6195 {
6196 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6197 if (p == NULL)
6198 return FALSE;
6199 p->next = *head;
6200 *head = p;
6201 p->sec = sec;
6202 p->count = 0;
6203 p->pc_count = 0;
6204 }
6205 p->count += 1;
6206 if (!must_be_dyn_reloc (info, r_type))
6207 p->pc_count += 1;
65f38f15
AM
6208 }
6209 else
6210 {
ec338859
AM
6211 /* Track dynamic relocs needed for local syms too.
6212 We really need local syms available to do this
6213 easily. Oh well. */
19e08130
AM
6214 struct ppc_dyn_relocs *p;
6215 struct ppc_dyn_relocs **head;
6216 bfd_boolean is_ifunc;
ec338859 6217 asection *s;
6edfbbad 6218 void *vpp;
87d72d41 6219 Elf_Internal_Sym *isym;
6edfbbad 6220
87d72d41
AM
6221 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6222 abfd, r_symndx);
6223 if (isym == NULL)
b34976b6 6224 return FALSE;
ec338859 6225
87d72d41
AM
6226 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6227 if (s == NULL)
6228 s = sec;
6229
6edfbbad 6230 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
6231 head = (struct ppc_dyn_relocs **) vpp;
6232 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6233 p = *head;
6234 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6235 p = p->next;
6236 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6237 {
6238 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6239 if (p == NULL)
6240 return FALSE;
6241 p->next = *head;
6242 *head = p;
6243 p->sec = sec;
6244 p->ifunc = is_ifunc;
6245 p->count = 0;
6246 }
6247 p->count += 1;
ec338859 6248 }
65f38f15 6249 }
5bd4f169 6250 break;
65f38f15
AM
6251
6252 default:
96e0dda4 6253 break;
5bd4f169
AM
6254 }
6255 }
6256
b34976b6 6257 return TRUE;
5bd4f169
AM
6258}
6259
ee67d69a
AM
6260/* Merge backend specific data from an object file to the output
6261 object file when linking. */
6262
6263static bfd_boolean
50e03d47 6264ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6265{
50e03d47 6266 bfd *obfd = info->output_bfd;
ee67d69a
AM
6267 unsigned long iflags, oflags;
6268
6269 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6270 return TRUE;
6271
6272 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6273 return TRUE;
6274
50e03d47 6275 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6276 return FALSE;
6277
6278 iflags = elf_elfheader (ibfd)->e_flags;
6279 oflags = elf_elfheader (obfd)->e_flags;
6280
f6c7c3e8 6281 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6282 {
4eca0228 6283 _bfd_error_handler
695344c0 6284 /* xgettext:c-format */
871b3ab2 6285 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
6286 bfd_set_error (bfd_error_bad_value);
6287 return FALSE;
6288 }
f6c7c3e8 6289 else if (iflags != oflags && iflags != 0)
ee67d69a 6290 {
4eca0228 6291 _bfd_error_handler
695344c0 6292 /* xgettext:c-format */
871b3ab2 6293 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
6294 ibfd, iflags, oflags);
6295 bfd_set_error (bfd_error_bad_value);
6296 return FALSE;
6297 }
6298
4a91d0ba
AM
6299 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
6300 return FALSE;
005d79fd 6301
ee67d69a 6302 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 6303 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6304}
6305
6306static bfd_boolean
6307ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6308{
6309 /* Print normal ELF private data. */
6310 _bfd_elf_print_private_bfd_data (abfd, ptr);
6311
6312 if (elf_elfheader (abfd)->e_flags != 0)
6313 {
6314 FILE *file = ptr;
6315
ee67d69a
AM
6316 fprintf (file, _("private flags = 0x%lx:"),
6317 elf_elfheader (abfd)->e_flags);
6318
6319 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6320 fprintf (file, _(" [abiv%ld]"),
6321 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6322 fputc ('\n', file);
6323 }
6324
6325 return TRUE;
6326}
6327
8387904d 6328/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6329 of the code entry point, and its section, which must be in the same
6330 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6331
6332static bfd_vma
6333opd_entry_value (asection *opd_sec,
6334 bfd_vma offset,
6335 asection **code_sec,
aef36ac1
AM
6336 bfd_vma *code_off,
6337 bfd_boolean in_code_sec)
8387904d
AM
6338{
6339 bfd *opd_bfd = opd_sec->owner;
8860955f 6340 Elf_Internal_Rela *relocs;
8387904d 6341 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6342 bfd_vma val;
8387904d 6343
9f296da3
AM
6344 /* No relocs implies we are linking a --just-symbols object, or looking
6345 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6346 if (opd_sec->reloc_count == 0)
6347 {
729eabd5 6348 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6349
729eabd5
AM
6350 if (contents == NULL)
6351 {
6352 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6353 return (bfd_vma) -1;
6354 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6355 }
ee1e4ede 6356
dbb3fbbb 6357 /* PR 17512: file: 64b9dfbb. */
451dfd38 6358 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6359 return (bfd_vma) -1;
6360
729eabd5 6361 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6362 if (code_sec != NULL)
6363 {
6364 asection *sec, *likely = NULL;
ee1e4ede 6365
aef36ac1 6366 if (in_code_sec)
4b85d634 6367 {
aef36ac1
AM
6368 sec = *code_sec;
6369 if (sec->vma <= val
6370 && val < sec->vma + sec->size)
6371 likely = sec;
6372 else
6373 val = -1;
6374 }
6375 else
6376 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6377 if (sec->vma <= val
6378 && (sec->flags & SEC_LOAD) != 0
6379 && (sec->flags & SEC_ALLOC) != 0)
6380 likely = sec;
6381 if (likely != NULL)
6382 {
6383 *code_sec = likely;
6384 if (code_off != NULL)
6385 *code_off = val - likely->vma;
4b85d634
AM
6386 }
6387 }
aef36ac1 6388 return val;
4b85d634
AM
6389 }
6390
0c8d6e5c 6391 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6392
729eabd5 6393 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6394 if (relocs == NULL)
6395 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6396 /* PR 17512: file: df8e1fd6. */
6397 if (relocs == NULL)
6398 return (bfd_vma) -1;
645ea6a9 6399
8387904d 6400 /* Go find the opd reloc at the sym address. */
8860955f 6401 lo = relocs;
8387904d 6402 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6403 val = (bfd_vma) -1;
8387904d
AM
6404 while (lo < hi)
6405 {
6406 look = lo + (hi - lo) / 2;
6407 if (look->r_offset < offset)
6408 lo = look + 1;
6409 else if (look->r_offset > offset)
6410 hi = look;
6411 else
6412 {
0ffa91dd
NC
6413 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6414
8387904d
AM
6415 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6416 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6417 {
6418 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6419 asection *sec = NULL;
8387904d 6420
b53dfeb2
AM
6421 if (symndx >= symtab_hdr->sh_info
6422 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6423 {
6424 struct elf_link_hash_entry **sym_hashes;
6425 struct elf_link_hash_entry *rh;
6426
6427 sym_hashes = elf_sym_hashes (opd_bfd);
6428 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6429 if (rh != NULL)
6430 {
6431 rh = elf_follow_link (rh);
bb854a36
AM
6432 if (rh->root.type != bfd_link_hash_defined
6433 && rh->root.type != bfd_link_hash_defweak)
6434 break;
6435 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6436 {
bb854a36
AM
6437 val = rh->root.u.def.value;
6438 sec = rh->root.u.def.section;
b53dfeb2
AM
6439 }
6440 }
6441 }
6442
6443 if (sec == NULL)
6444 {
6445 Elf_Internal_Sym *sym;
6446
6447 if (symndx < symtab_hdr->sh_info)
6448 {
6449 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6450 if (sym == NULL)
6451 {
6452 size_t symcnt = symtab_hdr->sh_info;
6453 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6454 symcnt, 0,
6455 NULL, NULL, NULL);
6456 if (sym == NULL)
6457 break;
6458 symtab_hdr->contents = (bfd_byte *) sym;
6459 }
6460 sym += symndx;
128205bb
AM
6461 }
6462 else
6463 {
b53dfeb2
AM
6464 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6465 1, symndx,
6466 NULL, NULL, NULL);
128205bb
AM
6467 if (sym == NULL)
6468 break;
128205bb 6469 }
b53dfeb2
AM
6470 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6471 if (sec == NULL)
6472 break;
6473 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6474 val = sym->st_value;
8387904d 6475 }
b53dfeb2 6476
8387904d
AM
6477 val += look->r_addend;
6478 if (code_off != NULL)
6479 *code_off = val;
6480 if (code_sec != NULL)
aef36ac1
AM
6481 {
6482 if (in_code_sec && *code_sec != sec)
6483 return -1;
6484 else
6485 *code_sec = sec;
6486 }
b53dfeb2 6487 if (sec->output_section != NULL)
8387904d 6488 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6489 }
6490 break;
6491 }
6492 }
645ea6a9 6493
645ea6a9 6494 return val;
8387904d
AM
6495}
6496
aef36ac1
AM
6497/* If the ELF symbol SYM might be a function in SEC, return the
6498 function size and set *CODE_OFF to the function's entry point,
6499 otherwise return zero. */
9f296da3 6500
aef36ac1
AM
6501static bfd_size_type
6502ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6503 bfd_vma *code_off)
9f296da3 6504{
aef36ac1
AM
6505 bfd_size_type size;
6506
6507 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6508 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6509 return 0;
6510
6511 size = 0;
6512 if (!(sym->flags & BSF_SYNTHETIC))
6513 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6514
6515 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6516 {
b07bca4e
AM
6517 struct _opd_sec_data *opd = get_opd_info (sym->section);
6518 bfd_vma symval = sym->value;
6519
6520 if (opd != NULL
6521 && opd->adjust != NULL
6522 && elf_section_data (sym->section)->relocs != NULL)
6523 {
6524 /* opd_entry_value will use cached relocs that have been
6525 adjusted, but with raw symbols. That means both local
6526 and global symbols need adjusting. */
6527 long adjust = opd->adjust[OPD_NDX (symval)];
6528 if (adjust == -1)
6529 return 0;
6530 symval += adjust;
6531 }
6532
6533 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6534 &sec, code_off, TRUE) == (bfd_vma) -1)
6535 return 0;
6536 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6537 symbol. This size has nothing to do with the code size of the
6538 function, which is what we're supposed to return, but the
6539 code size isn't available without looking up the dot-sym.
6540 However, doing that would be a waste of time particularly
6541 since elf_find_function will look at the dot-sym anyway.
6542 Now, elf_find_function will keep the largest size of any
6543 function sym found at the code address of interest, so return
6544 1 here to avoid it incorrectly caching a larger function size
6545 for a small function. This does mean we return the wrong
6546 size for a new-ABI function of size 24, but all that does is
6547 disable caching for such functions. */
6548 if (size == 24)
6549 size = 1;
9f296da3 6550 }
aef36ac1
AM
6551 else
6552 {
6553 if (sym->section != sec)
6554 return 0;
6555 *code_off = sym->value;
6556 }
6557 if (size == 0)
6558 size = 1;
6559 return size;
9f296da3
AM
6560}
6561
f378ab09
AM
6562/* Return true if symbol is a strong function defined in an ELFv2
6563 object with st_other localentry bits of zero, ie. its local entry
6564 point coincides with its global entry point. */
6565
6566static bfd_boolean
6567is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6568{
6569 return (h != NULL
6570 && h->type == STT_FUNC
6571 && h->root.type == bfd_link_hash_defined
6572 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6573 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6574 && is_ppc64_elf (h->root.u.def.section->owner)
6575 && abiversion (h->root.u.def.section->owner) >= 2);
6576}
6577
854b41e7
AM
6578/* Return true if symbol is defined in a regular object file. */
6579
6580static bfd_boolean
6581is_static_defined (struct elf_link_hash_entry *h)
6582{
6583 return ((h->root.type == bfd_link_hash_defined
6584 || h->root.type == bfd_link_hash_defweak)
6585 && h->root.u.def.section != NULL
6586 && h->root.u.def.section->output_section != NULL);
6587}
6588
b31867b6
AM
6589/* If FDH is a function descriptor symbol, return the associated code
6590 entry symbol if it is defined. Return NULL otherwise. */
6591
6592static struct ppc_link_hash_entry *
6593defined_code_entry (struct ppc_link_hash_entry *fdh)
6594{
6595 if (fdh->is_func_descriptor)
6596 {
6597 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6598 if (fh->elf.root.type == bfd_link_hash_defined
6599 || fh->elf.root.type == bfd_link_hash_defweak)
6600 return fh;
6601 }
6602 return NULL;
6603}
6604
6605/* If FH is a function code entry symbol, return the associated
6606 function descriptor symbol if it is defined. Return NULL otherwise. */
6607
6608static struct ppc_link_hash_entry *
6609defined_func_desc (struct ppc_link_hash_entry *fh)
6610{
6611 if (fh->oh != NULL
6612 && fh->oh->is_func_descriptor)
6613 {
6614 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6615 if (fdh->elf.root.type == bfd_link_hash_defined
6616 || fdh->elf.root.type == bfd_link_hash_defweak)
6617 return fdh;
6618 }
6619 return NULL;
6620}
6621
8c5b4e52
AM
6622static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6623
6624/* Garbage collect sections, after first dealing with dot-symbols. */
6625
6626static bfd_boolean
6627ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6628{
6629 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6630
6631 if (htab != NULL && htab->need_func_desc_adj)
6632 {
6633 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6634 htab->need_func_desc_adj = 0;
6635 }
6636 return bfd_elf_gc_sections (abfd, info);
6637}
6638
74f0fb50
AM
6639/* Mark all our entry sym sections, both opd and code section. */
6640
6641static void
6642ppc64_elf_gc_keep (struct bfd_link_info *info)
6643{
6644 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6645 struct bfd_sym_chain *sym;
6646
4dfe6ac6
NC
6647 if (htab == NULL)
6648 return;
6649
74f0fb50
AM
6650 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6651 {
b31867b6 6652 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6653 asection *sec;
6654
6655 eh = (struct ppc_link_hash_entry *)
b31867b6 6656 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6657 if (eh == NULL)
6658 continue;
6659 if (eh->elf.root.type != bfd_link_hash_defined
6660 && eh->elf.root.type != bfd_link_hash_defweak)
6661 continue;
6662
b31867b6
AM
6663 fh = defined_code_entry (eh);
6664 if (fh != NULL)
74f0fb50 6665 {
b31867b6 6666 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6667 sec->flags |= SEC_KEEP;
6668 }
6669 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6670 && opd_entry_value (eh->elf.root.u.def.section,
6671 eh->elf.root.u.def.value,
aef36ac1 6672 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6673 sec->flags |= SEC_KEEP;
6674
6675 sec = eh->elf.root.u.def.section;
6676 sec->flags |= SEC_KEEP;
6677 }
6678}
6679
64d03ab5
AM
6680/* Mark sections containing dynamically referenced symbols. When
6681 building shared libraries, we must assume that any visible symbol is
6682 referenced. */
6683
6684static bfd_boolean
6685ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6686{
6687 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6688 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6689 struct ppc_link_hash_entry *fdh;
b407645f 6690 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6691
64d03ab5 6692 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6693 fdh = defined_func_desc (eh);
6694 if (fdh != NULL)
6695 eh = fdh;
64d03ab5
AM
6696
6697 if ((eh->elf.root.type == bfd_link_hash_defined
6698 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6699 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6700 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6701 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6702 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6703 && (!bfd_link_executable (info)
e278ae05 6704 || info->gc_keep_exported
b407645f
AM
6705 || info->export_dynamic
6706 || (eh->elf.dynamic
6707 && d != NULL
2cdcc330
AM
6708 && (*d->match) (&d->head, NULL,
6709 eh->elf.root.root.string)))
e278ae05 6710 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6711 || !bfd_hide_sym_by_version (info->version_info,
6712 eh->elf.root.root.string)))))
64d03ab5
AM
6713 {
6714 asection *code_sec;
b31867b6 6715 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6716
6717 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6718
6719 /* Function descriptor syms cause the associated
6720 function code sym section to be marked. */
b31867b6
AM
6721 fh = defined_code_entry (eh);
6722 if (fh != NULL)
6723 {
6724 code_sec = fh->elf.root.u.def.section;
6725 code_sec->flags |= SEC_KEEP;
6726 }
64d03ab5
AM
6727 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6728 && opd_entry_value (eh->elf.root.u.def.section,
6729 eh->elf.root.u.def.value,
aef36ac1 6730 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6731 code_sec->flags |= SEC_KEEP;
6732 }
6733
6734 return TRUE;
6735}
6736
5bd4f169
AM
6737/* Return the section that should be marked against GC for a given
6738 relocation. */
6739
6740static asection *
4ce794b7 6741ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6742 struct bfd_link_info *info,
4ce794b7
AM
6743 Elf_Internal_Rela *rel,
6744 struct elf_link_hash_entry *h,
6745 Elf_Internal_Sym *sym)
5bd4f169 6746{
ccfa59ea
AM
6747 asection *rsec;
6748
ccfa59ea
AM
6749 /* Syms return NULL if we're marking .opd, so we avoid marking all
6750 function sections, as all functions are referenced in .opd. */
6751 rsec = NULL;
6752 if (get_opd_info (sec) != NULL)
6753 return rsec;
1e2f5b6e 6754
5bd4f169
AM
6755 if (h != NULL)
6756 {
04c9666a 6757 enum elf_ppc64_reloc_type r_type;
b31867b6 6758 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6759
4ce794b7 6760 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6761 switch (r_type)
5bd4f169
AM
6762 {
6763 case R_PPC64_GNU_VTINHERIT:
6764 case R_PPC64_GNU_VTENTRY:
6765 break;
6766
6767 default:
6768 switch (h->root.type)
6769 {
6770 case bfd_link_hash_defined:
6771 case bfd_link_hash_defweak:
ccfa59ea 6772 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6773 fdh = defined_func_desc (eh);
6774 if (fdh != NULL)
8c5b4e52
AM
6775 {
6776 /* -mcall-aixdesc code references the dot-symbol on
6777 a call reloc. Mark the function descriptor too
6778 against garbage collection. */
6779 fdh->elf.mark = 1;
60d67dc8
AM
6780 if (fdh->elf.is_weakalias)
6781 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6782 eh = fdh;
6783 }
1e2f5b6e
AM
6784
6785 /* Function descriptor syms cause the associated
6786 function code sym section to be marked. */
b31867b6
AM
6787 fh = defined_code_entry (eh);
6788 if (fh != NULL)
ccfa59ea
AM
6789 {
6790 /* They also mark their opd section. */
74f0fb50 6791 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6792
b31867b6 6793 rsec = fh->elf.root.u.def.section;
ccfa59ea 6794 }
8387904d
AM
6795 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6796 && opd_entry_value (eh->elf.root.u.def.section,
6797 eh->elf.root.u.def.value,
aef36ac1 6798 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6799 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6800 else
1e2f5b6e
AM
6801 rsec = h->root.u.def.section;
6802 break;
5bd4f169
AM
6803
6804 case bfd_link_hash_common:
1e2f5b6e
AM
6805 rsec = h->root.u.c.p->section;
6806 break;
5bd4f169
AM
6807
6808 default:
fb34365b 6809 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6810 }
6811 }
6812 }
6813 else
6814 {
74f0fb50 6815 struct _opd_sec_data *opd;
1e2f5b6e
AM
6816
6817 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6818 opd = get_opd_info (rsec);
6819 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6820 {
74f0fb50 6821 rsec->gc_mark = 1;
ccfa59ea 6822
51aecdc5 6823 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6824 }
5bd4f169
AM
6825 }
6826
1e2f5b6e 6827 return rsec;
5bd4f169
AM
6828}
6829
deb0e272
AM
6830/* The maximum size of .sfpr. */
6831#define SFPR_MAX (218*4)
6832
6833struct sfpr_def_parms
6834{
699733f6
AM
6835 const char name[12];
6836 unsigned char lo, hi;
2cdcc330
AM
6837 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6838 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
6839};
6840
a4b6fadd
AM
6841/* Auto-generate _save*, _rest* functions in .sfpr.
6842 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6843 instead. */
deb0e272 6844
4dfe6ac6 6845static bfd_boolean
a4b6fadd
AM
6846sfpr_define (struct bfd_link_info *info,
6847 const struct sfpr_def_parms *parm,
6848 asection *stub_sec)
deb0e272
AM
6849{
6850 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6851 unsigned int i;
6852 size_t len = strlen (parm->name);
6853 bfd_boolean writing = FALSE;
699733f6 6854 char sym[16];
deb0e272 6855
4dfe6ac6
NC
6856 if (htab == NULL)
6857 return FALSE;
6858
deb0e272
AM
6859 memcpy (sym, parm->name, len);
6860 sym[len + 2] = 0;
6861
6862 for (i = parm->lo; i <= parm->hi; i++)
6863 {
a4b6fadd 6864 struct ppc_link_hash_entry *h;
deb0e272
AM
6865
6866 sym[len + 0] = i / 10 + '0';
6867 sym[len + 1] = i % 10 + '0';
a4b6fadd 6868 h = (struct ppc_link_hash_entry *)
b32547cd 6869 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6870 if (stub_sec != NULL)
deb0e272 6871 {
a4b6fadd
AM
6872 if (h != NULL
6873 && h->elf.root.type == bfd_link_hash_defined
6874 && h->elf.root.u.def.section == htab->sfpr)
6875 {
6876 struct elf_link_hash_entry *s;
6877 char buf[32];
6878 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6879 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6880 if (s == NULL)
6881 return FALSE;
6882 if (s->root.type == bfd_link_hash_new
6883 || (s->root.type = bfd_link_hash_defined
6884 && s->root.u.def.section == stub_sec))
6885 {
6886 s->root.type = bfd_link_hash_defined;
6887 s->root.u.def.section = stub_sec;
7dda8d3c 6888 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6889 + h->elf.root.u.def.value);
6890 s->ref_regular = 1;
6891 s->def_regular = 1;
6892 s->ref_regular_nonweak = 1;
6893 s->forced_local = 1;
6894 s->non_elf = 0;
6895 s->root.linker_def = 1;
6896 }
6897 }
6898 continue;
6899 }
6900 if (h != NULL)
6901 {
6902 h->save_res = 1;
6903 if (!h->elf.def_regular)
deb0e272 6904 {
a4b6fadd
AM
6905 h->elf.root.type = bfd_link_hash_defined;
6906 h->elf.root.u.def.section = htab->sfpr;
6907 h->elf.root.u.def.value = htab->sfpr->size;
6908 h->elf.type = STT_FUNC;
6909 h->elf.def_regular = 1;
b32547cd 6910 h->elf.non_elf = 0;
a4b6fadd
AM
6911 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6912 writing = TRUE;
deb0e272 6913 if (htab->sfpr->contents == NULL)
a4b6fadd 6914 {
2cdcc330
AM
6915 htab->sfpr->contents
6916 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
6917 if (htab->sfpr->contents == NULL)
6918 return FALSE;
6919 }
deb0e272
AM
6920 }
6921 }
6922 if (writing)
6923 {
6924 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6925 if (i != parm->hi)
6926 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6927 else
6928 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6929 htab->sfpr->size = p - htab->sfpr->contents;
6930 }
6931 }
6932
6933 return TRUE;
6934}
6935
6936static bfd_byte *
6937savegpr0 (bfd *abfd, bfd_byte *p, int r)
6938{
6939 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6940 return p + 4;
6941}
6942
6943static bfd_byte *
6944savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6945{
6946 p = savegpr0 (abfd, p, r);
a078d95a 6947 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6948 p = p + 4;
6949 bfd_put_32 (abfd, BLR, p);
6950 return p + 4;
6951}
6952
6953static bfd_byte *
6954restgpr0 (bfd *abfd, bfd_byte *p, int r)
6955{
6956 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6957 return p + 4;
6958}
6959
6960static bfd_byte *
6961restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6962{
a078d95a 6963 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6964 p = p + 4;
6965 p = restgpr0 (abfd, p, r);
6966 bfd_put_32 (abfd, MTLR_R0, p);
6967 p = p + 4;
6968 if (r == 29)
6969 {
6970 p = restgpr0 (abfd, p, 30);
6971 p = restgpr0 (abfd, p, 31);
6972 }
6973 bfd_put_32 (abfd, BLR, p);
6974 return p + 4;
6975}
6976
6977static bfd_byte *
6978savegpr1 (bfd *abfd, bfd_byte *p, int r)
6979{
6980 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6981 return p + 4;
6982}
6983
6984static bfd_byte *
6985savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6986{
6987 p = savegpr1 (abfd, p, r);
6988 bfd_put_32 (abfd, BLR, p);
6989 return p + 4;
6990}
6991
6992static bfd_byte *
6993restgpr1 (bfd *abfd, bfd_byte *p, int r)
6994{
6995 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6996 return p + 4;
6997}
6998
6999static bfd_byte *
7000restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
7001{
7002 p = restgpr1 (abfd, p, r);
7003 bfd_put_32 (abfd, BLR, p);
7004 return p + 4;
7005}
7006
7007static bfd_byte *
7008savefpr (bfd *abfd, bfd_byte *p, int r)
7009{
7010 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7011 return p + 4;
7012}
7013
7014static bfd_byte *
7015savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
7016{
7017 p = savefpr (abfd, p, r);
a078d95a 7018 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
7019 p = p + 4;
7020 bfd_put_32 (abfd, BLR, p);
7021 return p + 4;
7022}
7023
7024static bfd_byte *
7025restfpr (bfd *abfd, bfd_byte *p, int r)
7026{
7027 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7028 return p + 4;
7029}
7030
7031static bfd_byte *
7032restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
7033{
a078d95a 7034 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
7035 p = p + 4;
7036 p = restfpr (abfd, p, r);
7037 bfd_put_32 (abfd, MTLR_R0, p);
7038 p = p + 4;
7039 if (r == 29)
7040 {
7041 p = restfpr (abfd, p, 30);
7042 p = restfpr (abfd, p, 31);
7043 }
7044 bfd_put_32 (abfd, BLR, p);
7045 return p + 4;
7046}
7047
7048static bfd_byte *
7049savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
7050{
7051 p = savefpr (abfd, p, r);
7052 bfd_put_32 (abfd, BLR, p);
7053 return p + 4;
7054}
7055
7056static bfd_byte *
7057restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
7058{
7059 p = restfpr (abfd, p, r);
7060 bfd_put_32 (abfd, BLR, p);
7061 return p + 4;
7062}
7063
7064static bfd_byte *
7065savevr (bfd *abfd, bfd_byte *p, int r)
7066{
7067 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7068 p = p + 4;
7069 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
7070 return p + 4;
7071}
7072
7073static bfd_byte *
7074savevr_tail (bfd *abfd, bfd_byte *p, int r)
7075{
7076 p = savevr (abfd, p, r);
7077 bfd_put_32 (abfd, BLR, p);
7078 return p + 4;
7079}
7080
7081static bfd_byte *
7082restvr (bfd *abfd, bfd_byte *p, int r)
7083{
7084 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7085 p = p + 4;
7086 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
7087 return p + 4;
7088}
7089
7090static bfd_byte *
7091restvr_tail (bfd *abfd, bfd_byte *p, int r)
7092{
7093 p = restvr (abfd, p, r);
7094 bfd_put_32 (abfd, BLR, p);
7095 return p + 4;
7096}
7097
e86ce104
AM
7098/* Called via elf_link_hash_traverse to transfer dynamic linking
7099 information on function code symbol entries to their corresponding
7100 function descriptor symbol entries. */
deb0e272 7101
b34976b6 7102static bfd_boolean
4ce794b7 7103func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 7104{
e86ce104 7105 struct bfd_link_info *info;
65f38f15 7106 struct ppc_link_hash_table *htab;
50bc7936
AM
7107 struct ppc_link_hash_entry *fh;
7108 struct ppc_link_hash_entry *fdh;
7109 bfd_boolean force_local;
5bd4f169 7110
50bc7936
AM
7111 fh = (struct ppc_link_hash_entry *) h;
7112 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 7113 return TRUE;
e86ce104 7114
8c5b4e52
AM
7115 if (!fh->is_func)
7116 return TRUE;
7117
7118 if (fh->elf.root.root.string[0] != '.'
7119 || fh->elf.root.root.string[1] == '\0')
7120 return TRUE;
7121
4ce794b7 7122 info = inf;
65f38f15 7123 htab = ppc_hash_table (info);
4dfe6ac6
NC
7124 if (htab == NULL)
7125 return FALSE;
5bd4f169 7126
8c5b4e52
AM
7127 /* Find the corresponding function descriptor symbol. */
7128 fdh = lookup_fdh (fh, htab);
7129
c09bdfe5
AM
7130 /* Resolve undefined references to dot-symbols as the value
7131 in the function descriptor, if we have one in a regular object.
7132 This is to satisfy cases like ".quad .foo". Calls to functions
7133 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7134 if ((fh->elf.root.type == bfd_link_hash_undefined
7135 || fh->elf.root.type == bfd_link_hash_undefweak)
7136 && (fdh->elf.root.type == bfd_link_hash_defined
7137 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7138 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7139 && opd_entry_value (fdh->elf.root.u.def.section,
7140 fdh->elf.root.u.def.value,
c09bdfe5 7141 &fh->elf.root.u.def.section,
aef36ac1 7142 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7143 {
b31867b6 7144 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7145 fh->elf.forced_local = 1;
b31867b6
AM
7146 fh->elf.def_regular = fdh->elf.def_regular;
7147 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7148 }
7149
8c5b4e52
AM
7150 if (!fh->elf.dynamic)
7151 {
7152 struct plt_entry *ent;
5bd4f169 7153
8c5b4e52
AM
7154 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7155 if (ent->plt.refcount > 0)
7156 break;
7157 if (ent == NULL)
7158 return TRUE;
7159 }
5bd4f169 7160
8c5b4e52 7161 /* Create a descriptor as undefined if necessary. */
50bc7936 7162 if (fdh == NULL
0e1862bb 7163 && !bfd_link_executable (info)
50bc7936
AM
7164 && (fh->elf.root.type == bfd_link_hash_undefined
7165 || fh->elf.root.type == bfd_link_hash_undefweak))
7166 {
908b32fc 7167 fdh = make_fdh (info, fh);
bb700d78
AM
7168 if (fdh == NULL)
7169 return FALSE;
50bc7936 7170 }
648cca2c 7171
8c5b4e52 7172 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7173 if (fdh != NULL
7174 && fdh->fake
8c5b4e52
AM
7175 && (fh->elf.root.type == bfd_link_hash_defined
7176 || fh->elf.root.type == bfd_link_hash_defweak))
7177 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7178
8c5b4e52
AM
7179 /* Transfer dynamic linking information to the function descriptor. */
7180 if (fdh != NULL)
7181 {
f5385ebf
AM
7182 fdh->elf.ref_regular |= fh->elf.ref_regular;
7183 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7184 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7185 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7186 fdh->elf.dynamic |= fh->elf.dynamic;
7187 fdh->elf.needs_plt |= (fh->elf.needs_plt
7188 || fh->elf.type == STT_FUNC
7189 || fh->elf.type == STT_GNU_IFUNC);
7190 move_plt_plist (fh, fdh);
7191
7192 if (!fdh->elf.forced_local
7193 && fh->elf.dynindx != -1)
7194 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7195 return FALSE;
e86ce104
AM
7196 }
7197
50bc7936
AM
7198 /* Now that the info is on the function descriptor, clear the
7199 function code sym info. Any function code syms for which we
7200 don't have a definition in a regular file, we force local.
7201 This prevents a shared library from exporting syms that have
7202 been imported from another library. Function code syms that
7203 are really in the library we must leave global to prevent the
7204 linker dragging in a definition from a static library. */
93f3fa99
AM
7205 force_local = (!fh->elf.def_regular
7206 || fdh == NULL
7207 || !fdh->elf.def_regular
7208 || fdh->elf.forced_local);
50bc7936
AM
7209 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7210
b34976b6 7211 return TRUE;
e86ce104 7212}
40b8271b 7213
a4b6fadd
AM
7214static const struct sfpr_def_parms save_res_funcs[] =
7215 {
7216 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7217 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7218 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7219 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7220 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7221 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7222 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7223 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7224 { "._savef", 14, 31, savefpr, savefpr1_tail },
7225 { "._restf", 14, 31, restfpr, restfpr1_tail },
7226 { "_savevr_", 20, 31, savevr, savevr_tail },
7227 { "_restvr_", 20, 31, restvr, restvr_tail }
7228 };
7229
e86ce104 7230/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7231 this hook to a) provide some gcc support functions, and b) transfer
7232 dynamic linking information gathered so far on function code symbol
7233 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7234
b34976b6 7235static bfd_boolean
4ce794b7
AM
7236ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7237 struct bfd_link_info *info)
e86ce104
AM
7238{
7239 struct ppc_link_hash_table *htab;
7240
7241 htab = ppc_hash_table (info);
4dfe6ac6
NC
7242 if (htab == NULL)
7243 return FALSE;
7244
b32547cd
AM
7245 /* Provide any missing _save* and _rest* functions. */
7246 if (htab->sfpr != NULL)
7247 {
7248 unsigned int i;
7249
7250 htab->sfpr->size = 0;
7251 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7252 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7253 return FALSE;
7254 if (htab->sfpr->size == 0)
7255 htab->sfpr->flags |= SEC_EXCLUDE;
7256 }
7257
7258 if (bfd_link_relocatable (info))
7259 return TRUE;
7260
7261 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7262 {
7263 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7264 /* Make .TOC. defined so as to prevent it being made dynamic.
7265 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7266 if (!htab->elf.hgot->def_regular
7267 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7268 {
7269 htab->elf.hgot->root.type = bfd_link_hash_defined;
7270 htab->elf.hgot->root.u.def.value = 0;
7271 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7272 htab->elf.hgot->def_regular = 1;
7273 htab->elf.hgot->root.linker_def = 1;
7274 }
dba6fa9b 7275 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
7276 htab->elf.hgot->other
7277 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 7278 }
c66bb0ee 7279
8c5b4e52
AM
7280 if (htab->need_func_desc_adj)
7281 {
7282 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7283 htab->need_func_desc_adj = 0;
7284 }
805fc799 7285
b34976b6 7286 return TRUE;
e86ce104
AM
7287}
7288
98bbb1b8 7289/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7290
98bbb1b8 7291static asection *
a345bc8d
AM
7292readonly_dynrelocs (struct elf_link_hash_entry *h)
7293{
7294 struct ppc_link_hash_entry *eh;
7295 struct elf_dyn_relocs *p;
7296
7297 eh = (struct ppc_link_hash_entry *) h;
7298 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7299 {
7300 asection *s = p->sec->output_section;
7301
7302 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7303 return p->sec;
a345bc8d 7304 }
98bbb1b8 7305 return NULL;
a345bc8d
AM
7306}
7307
d311bc8b 7308/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7309 aliases, that apply to read-only sections. Cannot be used after
7310 size_dynamic_sections. */
d311bc8b
AM
7311
7312static bfd_boolean
7313alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7314{
7315 struct ppc_link_hash_entry *eh;
7316
7317 eh = (struct ppc_link_hash_entry *) h;
7318 do
7319 {
7320 if (readonly_dynrelocs (&eh->elf))
7321 return TRUE;
ab2477e1 7322 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
7323 }
7324 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
7325
7326 return FALSE;
7327}
8a2058b5 7328
8a9e8e72
AM
7329/* Return whether EH has pc-relative dynamic relocs. */
7330
7331static bfd_boolean
7332pc_dynrelocs (struct ppc_link_hash_entry *eh)
7333{
7334 struct elf_dyn_relocs *p;
7335
7336 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7337 if (p->pc_count != 0)
7338 return TRUE;
7339 return FALSE;
7340}
7341
8a2058b5
AM
7342/* Return true if a global entry stub will be created for H. Valid
7343 for ELFv2 before plt entries have been allocated. */
7344
7345static bfd_boolean
7346global_entry_stub (struct elf_link_hash_entry *h)
7347{
7348 struct plt_entry *pent;
7349
7350 if (!h->pointer_equality_needed
7351 || h->def_regular)
7352 return FALSE;
7353
7354 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7355 if (pent->plt.refcount > 0
7356 && pent->addend == 0)
7357 return TRUE;
7358
7359 return FALSE;
7360}
7361
e86ce104
AM
7362/* Adjust a symbol defined by a dynamic object and referenced by a
7363 regular object. The current definition is in some section of the
7364 dynamic object, but we're not including those sections. We have to
7365 change the definition to something the rest of the link can
7366 understand. */
7367
b34976b6 7368static bfd_boolean
4ce794b7
AM
7369ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7370 struct elf_link_hash_entry *h)
e86ce104
AM
7371{
7372 struct ppc_link_hash_table *htab;
5474d94f 7373 asection *s, *srel;
e86ce104
AM
7374
7375 htab = ppc_hash_table (info);
4dfe6ac6
NC
7376 if (htab == NULL)
7377 return FALSE;
e86ce104
AM
7378
7379 /* Deal with function syms. */
7380 if (h->type == STT_FUNC
e054468f 7381 || h->type == STT_GNU_IFUNC
f5385ebf 7382 || h->needs_plt)
e86ce104 7383 {
529fe20e
AM
7384 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7385 || SYMBOL_CALLS_LOCAL (info, h)
7386 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7387 /* Discard dyn_relocs when non-pic if we've decided that a
7388 function symbol is local and not an ifunc. We keep dynamic
7389 relocs for ifuncs when local rather than always emitting a
7390 plt call stub for them and defining the symbol on the call
7391 stub. We can't do that for ELFv1 anyway (a function symbol
7392 is defined on a descriptor, not code) and it can be faster at
7393 run-time due to not needing to bounce through a stub. The
7394 dyn_relocs for ifuncs will be applied even in a static
7395 executable. */
7396 if (!bfd_link_pic (info)
7397 && h->type != STT_GNU_IFUNC
7398 && local)
7399 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7400
e86ce104
AM
7401 /* Clear procedure linkage table information for any symbol that
7402 won't need a .plt entry. */
411e1bfb
AM
7403 struct plt_entry *ent;
7404 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7405 if (ent->plt.refcount > 0)
7406 break;
8387904d 7407 if (ent == NULL
2d7ad24e
AM
7408 || (h->type != STT_GNU_IFUNC
7409 && local
3e04d765
AM
7410 && (htab->can_convert_all_inline_plt
7411 || (((struct ppc_link_hash_entry *) h)->tls_mask
7412 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 7413 {
411e1bfb 7414 h->plt.plist = NULL;
f5385ebf 7415 h->needs_plt = 0;
d1eca1e4 7416 h->pointer_equality_needed = 0;
40b8271b 7417 }
8a2058b5 7418 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7419 {
d1eca1e4
AM
7420 /* Taking a function's address in a read/write section
7421 doesn't require us to define the function symbol in the
7422 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7423 be used instead. The reason we prefer a few more dynamic
7424 relocs is that calling via a global entry stub costs a
7425 few more instructions, and pointer_equality_needed causes
7426 extra work in ld.so when resolving these symbols. */
529fe20e 7427 if (global_entry_stub (h))
d1eca1e4 7428 {
ab2477e1 7429 if (!readonly_dynrelocs (h))
529fe20e
AM
7430 {
7431 h->pointer_equality_needed = 0;
04383fd1
AM
7432 /* If we haven't seen a branch reloc and the symbol
7433 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
7434 if (!h->needs_plt)
7435 h->plt.plist = NULL;
7436 }
7437 else if (!bfd_link_pic (info))
7438 /* We are going to be defining the function symbol on the
7439 plt stub, so no dyn_relocs needed when non-pic. */
7440 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7441 }
7442
3988aed5
AM
7443 /* ELFv2 function symbols can't have copy relocs. */
7444 return TRUE;
7445 }
7446 else if (!h->needs_plt
ab2477e1 7447 && !readonly_dynrelocs (h))
3988aed5 7448 {
04383fd1
AM
7449 /* If we haven't seen a branch reloc and the symbol isn't an
7450 ifunc then we don't need a plt entry. */
3988aed5
AM
7451 h->plt.plist = NULL;
7452 h->pointer_equality_needed = 0;
a345bc8d
AM
7453 return TRUE;
7454 }
5bd4f169 7455 }
bbd7ec4a 7456 else
411e1bfb 7457 h->plt.plist = NULL;
5bd4f169
AM
7458
7459 /* If this is a weak symbol, and there is a real definition, the
7460 processor independent code will have arranged for us to see the
7461 real definition first, and we can just use the same value. */
60d67dc8 7462 if (h->is_weakalias)
5bd4f169 7463 {
60d67dc8
AM
7464 struct elf_link_hash_entry *def = weakdef (h);
7465 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7466 h->root.u.def.section = def->root.u.def.section;
7467 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7468 if (def->root.u.def.section == htab->elf.sdynbss
7469 || def->root.u.def.section == htab->elf.sdynrelro)
7470 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7471 return TRUE;
5bd4f169
AM
7472 }
7473
5bd4f169
AM
7474 /* If we are creating a shared library, we must presume that the
7475 only references to the symbol are via the global offset table.
7476 For such cases we need not do anything here; the relocations will
7477 be handled correctly by relocate_section. */
0e1862bb 7478 if (bfd_link_pic (info))
b34976b6 7479 return TRUE;
5bd4f169 7480
65f38f15
AM
7481 /* If there are no references to this symbol that do not use the
7482 GOT, we don't need to generate a copy reloc. */
f5385ebf 7483 if (!h->non_got_ref)
b34976b6 7484 return TRUE;
65f38f15 7485
b186458a 7486 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7487 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7488
d93d1c80
AM
7489 /* If -z nocopyreloc was given, don't generate them either. */
7490 || info->nocopyreloc
a127494f 7491
dce2246a 7492 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7493 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7494 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7495
d93d1c80
AM
7496 /* Protected variables do not work with .dynbss. The copy in
7497 .dynbss won't be used by the shared library with the protected
7498 definition for the variable. Text relocations are preferable
7499 to an incorrect program. */
7500 || h->protected_def)
529fe20e 7501 return TRUE;
a127494f 7502
5d35169e 7503 if (h->plt.plist != NULL)
97b639ba
AM
7504 {
7505 /* We should never get here, but unfortunately there are versions
7506 of gcc out there that improperly (for this ABI) put initialized
7507 function pointers, vtable refs and suchlike in read-only
7508 sections. Allow them to proceed, but warn that this might
7509 break at runtime. */
25f53a85 7510 info->callbacks->einfo
c1c8c1ef 7511 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 7512 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7513 h->root.root.string);
7514 }
5d35169e
AM
7515
7516 /* This is a reference to a symbol defined by a dynamic object which
7517 is not a function. */
7518
5bd4f169
AM
7519 /* We must allocate the symbol in our .dynbss section, which will
7520 become part of the .bss section of the executable. There will be
7521 an entry for this symbol in the .dynsym section. The dynamic
7522 object will contain position independent code, so all references
7523 from the dynamic object to this symbol will go through the global
7524 offset table. The dynamic linker will use the .dynsym entry to
7525 determine the address it must put in the global offset table, so
7526 both the dynamic object and the regular object will refer to the
7527 same memory location for the variable. */
5474d94f
AM
7528 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7529 {
7530 s = htab->elf.sdynrelro;
7531 srel = htab->elf.sreldynrelro;
7532 }
7533 else
7534 {
7535 s = htab->elf.sdynbss;
7536 srel = htab->elf.srelbss;
7537 }
1d7e9d18 7538 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7539 {
4a7e5234
AM
7540 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7541 linker to copy the initial value out of the dynamic object
7542 and into the runtime process image. */
5474d94f 7543 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7544 h->needs_copy = 1;
5bd4f169
AM
7545 }
7546
529fe20e
AM
7547 /* We no longer want dyn_relocs. */
7548 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7549 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7550}
7551
e86ce104
AM
7552/* If given a function descriptor symbol, hide both the function code
7553 sym and the descriptor. */
7554static void
4ce794b7
AM
7555ppc64_elf_hide_symbol (struct bfd_link_info *info,
7556 struct elf_link_hash_entry *h,
7557 bfd_boolean force_local)
e86ce104 7558{
34814b9f 7559 struct ppc_link_hash_entry *eh;
e86ce104
AM
7560 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7561
34814b9f
AM
7562 eh = (struct ppc_link_hash_entry *) h;
7563 if (eh->is_func_descriptor)
e86ce104 7564 {
34814b9f 7565 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7566
721956f4 7567 if (fh == NULL)
d1329ca3
AM
7568 {
7569 const char *p, *q;
b8ac2841 7570 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7571 char save;
7572
7573 /* We aren't supposed to use alloca in BFD because on
7574 systems which do not have alloca the version in libiberty
7575 calls xmalloc, which might cause the program to crash
7576 when it runs out of memory. This function doesn't have a
7577 return status, so there's no way to gracefully return an
7578 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7579 accessed; It's either a string in an ELF string table,
7580 or allocated in an objalloc structure. */
d1329ca3 7581
34814b9f 7582 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7583 save = *p;
7584 *(char *) p = '.';
34814b9f 7585 fh = (struct ppc_link_hash_entry *)
b8ac2841 7586 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7587 *(char *) p = save;
7588
7589 /* Unfortunately, if it so happens that the string we were
7590 looking for was allocated immediately before this string,
7591 then we overwrote the string terminator. That's the only
7592 reason the lookup should fail. */
7593 if (fh == NULL)
7594 {
34814b9f
AM
7595 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7596 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7597 --q, --p;
34814b9f
AM
7598 if (q < eh->elf.root.root.string && *p == '.')
7599 fh = (struct ppc_link_hash_entry *)
b8ac2841 7600 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7601 }
7602 if (fh != NULL)
7603 {
34814b9f
AM
7604 eh->oh = fh;
7605 fh->oh = eh;
d1329ca3
AM
7606 }
7607 }
e86ce104 7608 if (fh != NULL)
34814b9f 7609 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7610 }
7611}
7612
411e1bfb 7613static bfd_boolean
8843416a
AM
7614get_sym_h (struct elf_link_hash_entry **hp,
7615 Elf_Internal_Sym **symp,
7616 asection **symsecp,
f961d9dd 7617 unsigned char **tls_maskp,
8843416a
AM
7618 Elf_Internal_Sym **locsymsp,
7619 unsigned long r_symndx,
7620 bfd *ibfd)
411e1bfb 7621{
0ffa91dd 7622 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7623
7624 if (r_symndx >= symtab_hdr->sh_info)
7625 {
7626 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7627 struct elf_link_hash_entry *h;
7628
7629 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7630 h = elf_follow_link (h);
411e1bfb
AM
7631
7632 if (hp != NULL)
7633 *hp = h;
7634
7635 if (symp != NULL)
7636 *symp = NULL;
7637
7638 if (symsecp != NULL)
7639 {
7640 asection *symsec = NULL;
7641 if (h->root.type == bfd_link_hash_defined
7642 || h->root.type == bfd_link_hash_defweak)
7643 symsec = h->root.u.def.section;
7644 *symsecp = symsec;
7645 }
7646
e7b938ca 7647 if (tls_maskp != NULL)
411e1bfb
AM
7648 {
7649 struct ppc_link_hash_entry *eh;
7650
7651 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7652 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7653 }
7654 }
7655 else
7656 {
7657 Elf_Internal_Sym *sym;
7658 Elf_Internal_Sym *locsyms = *locsymsp;
7659
7660 if (locsyms == NULL)
7661 {
7662 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7663 if (locsyms == NULL)
7664 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7665 symtab_hdr->sh_info,
7666 0, NULL, NULL, NULL);
7667 if (locsyms == NULL)
7668 return FALSE;
7669 *locsymsp = locsyms;
7670 }
7671 sym = locsyms + r_symndx;
7672
7673 if (hp != NULL)
7674 *hp = NULL;
7675
7676 if (symp != NULL)
7677 *symp = sym;
7678
7679 if (symsecp != NULL)
cb33740c 7680 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7681
e7b938ca 7682 if (tls_maskp != NULL)
411e1bfb
AM
7683 {
7684 struct got_entry **lgot_ents;
f961d9dd 7685 unsigned char *tls_mask;
411e1bfb 7686
e7b938ca 7687 tls_mask = NULL;
411e1bfb
AM
7688 lgot_ents = elf_local_got_ents (ibfd);
7689 if (lgot_ents != NULL)
7690 {
e054468f
AM
7691 struct plt_entry **local_plt = (struct plt_entry **)
7692 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7693 unsigned char *lgot_masks = (unsigned char *)
e054468f 7694 (local_plt + symtab_hdr->sh_info);
e7b938ca 7695 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7696 }
e7b938ca 7697 *tls_maskp = tls_mask;
411e1bfb
AM
7698 }
7699 }
7700 return TRUE;
7701}
7702
e7b938ca 7703/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7704 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7705 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7706
7707static int
f961d9dd 7708get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7709 unsigned long *toc_symndx,
7710 bfd_vma *toc_addend,
0d4792f7 7711 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7712 const Elf_Internal_Rela *rel,
7713 bfd *ibfd)
411e1bfb
AM
7714{
7715 unsigned long r_symndx;
0d4792f7 7716 int next_r;
411e1bfb
AM
7717 struct elf_link_hash_entry *h;
7718 Elf_Internal_Sym *sym;
7719 asection *sec;
7720 bfd_vma off;
7721
7722 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7723 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7724 return 0;
411e1bfb 7725
37da22e5
AM
7726 if ((*tls_maskp != NULL
7727 && (**tls_maskp & TLS_TLS) != 0
7728 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7729 || sec == NULL
6bee8834 7730 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7731 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7732 return 1;
411e1bfb
AM
7733
7734 /* Look inside a TOC section too. */
7735 if (h != NULL)
7736 {
7737 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7738 off = h->root.u.def.value;
7739 }
7740 else
7741 off = sym->st_value;
7742 off += rel->r_addend;
7743 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7744 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7745 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7746 if (toc_symndx != NULL)
7747 *toc_symndx = r_symndx;
3a71aa26
AM
7748 if (toc_addend != NULL)
7749 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7750 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7751 return 0;
854b41e7 7752 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7753 && (next_r == -1 || next_r == -2))
7754 return 1 - next_r;
951fd09b 7755 return 1;
411e1bfb
AM
7756}
7757
3b421ab3
AM
7758/* Find (or create) an entry in the tocsave hash table. */
7759
7760static struct tocsave_entry *
7761tocsave_find (struct ppc_link_hash_table *htab,
7762 enum insert_option insert,
7763 Elf_Internal_Sym **local_syms,
7764 const Elf_Internal_Rela *irela,
7765 bfd *ibfd)
7766{
7767 unsigned long r_indx;
7768 struct elf_link_hash_entry *h;
7769 Elf_Internal_Sym *sym;
7770 struct tocsave_entry ent, *p;
7771 hashval_t hash;
7772 struct tocsave_entry **slot;
7773
7774 r_indx = ELF64_R_SYM (irela->r_info);
7775 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7776 return NULL;
7777 if (ent.sec == NULL || ent.sec->output_section == NULL)
7778 {
4eca0228 7779 _bfd_error_handler
871b3ab2 7780 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7781 return NULL;
7782 }
7783
7784 if (h != NULL)
7785 ent.offset = h->root.u.def.value;
7786 else
7787 ent.offset = sym->st_value;
7788 ent.offset += irela->r_addend;
7789
7790 hash = tocsave_htab_hash (&ent);
7791 slot = ((struct tocsave_entry **)
7792 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7793 if (slot == NULL)
7794 return NULL;
7795
7796 if (*slot == NULL)
7797 {
7798 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7799 if (p == NULL)
7800 return NULL;
7801 *p = ent;
7802 *slot = p;
7803 }
7804 return *slot;
7805}
7806
754021d0 7807/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7808 code for the old ABI, these will already have been done. */
754021d0
AM
7809
7810static bfd_boolean
7811adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7812{
7813 struct ppc_link_hash_entry *eh;
7814 asection *sym_sec;
74f0fb50 7815 struct _opd_sec_data *opd;
754021d0
AM
7816
7817 if (h->root.type == bfd_link_hash_indirect)
7818 return TRUE;
7819
754021d0
AM
7820 if (h->root.type != bfd_link_hash_defined
7821 && h->root.type != bfd_link_hash_defweak)
7822 return TRUE;
7823
7824 eh = (struct ppc_link_hash_entry *) h;
7825 if (eh->adjust_done)
7826 return TRUE;
7827
7828 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7829 opd = get_opd_info (sym_sec);
7830 if (opd != NULL && opd->adjust != NULL)
754021d0 7831 {
51aecdc5 7832 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7833 if (adjust == -1)
7834 {
7835 /* This entry has been deleted. */
b3fac117 7836 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7837 if (dsec == NULL)
7838 {
7839 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7840 if (discarded_section (dsec))
81688140 7841 {
b3fac117 7842 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7843 break;
7844 }
7845 }
4025353c 7846 eh->elf.root.u.def.value = 0;
81688140 7847 eh->elf.root.u.def.section = dsec;
4025353c
AM
7848 }
7849 else
7850 eh->elf.root.u.def.value += adjust;
754021d0
AM
7851 eh->adjust_done = 1;
7852 }
7853 return TRUE;
7854}
7855
8c1d1bb8 7856/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7857 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7858 have already been determined. */
7859
7860static bfd_boolean
7861dec_dynrel_count (bfd_vma r_info,
7862 asection *sec,
7863 struct bfd_link_info *info,
7864 Elf_Internal_Sym **local_syms,
7865 struct elf_link_hash_entry *h,
19e08130 7866 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7867{
7868 enum elf_ppc64_reloc_type r_type;
19e08130 7869 asection *sym_sec = NULL;
8c1d1bb8
AM
7870
7871 /* Can this reloc be dynamic? This switch, and later tests here
7872 should be kept in sync with the code in check_relocs. */
7873 r_type = ELF64_R_TYPE (r_info);
7874 switch (r_type)
7875 {
7876 default:
7877 return TRUE;
7878
7879 case R_PPC64_TPREL16:
7880 case R_PPC64_TPREL16_LO:
7881 case R_PPC64_TPREL16_HI:
7882 case R_PPC64_TPREL16_HA:
7883 case R_PPC64_TPREL16_DS:
7884 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7885 case R_PPC64_TPREL16_HIGH:
7886 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7887 case R_PPC64_TPREL16_HIGHER:
7888 case R_PPC64_TPREL16_HIGHERA:
7889 case R_PPC64_TPREL16_HIGHEST:
7890 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7891 case R_PPC64_TPREL64:
7892 case R_PPC64_DTPMOD64:
7893 case R_PPC64_DTPREL64:
7894 case R_PPC64_ADDR64:
7895 case R_PPC64_REL30:
7896 case R_PPC64_REL32:
7897 case R_PPC64_REL64:
7898 case R_PPC64_ADDR14:
7899 case R_PPC64_ADDR14_BRNTAKEN:
7900 case R_PPC64_ADDR14_BRTAKEN:
7901 case R_PPC64_ADDR16:
7902 case R_PPC64_ADDR16_DS:
7903 case R_PPC64_ADDR16_HA:
7904 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7905 case R_PPC64_ADDR16_HIGH:
7906 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7907 case R_PPC64_ADDR16_HIGHER:
7908 case R_PPC64_ADDR16_HIGHERA:
7909 case R_PPC64_ADDR16_HIGHEST:
7910 case R_PPC64_ADDR16_HIGHESTA:
7911 case R_PPC64_ADDR16_LO:
7912 case R_PPC64_ADDR16_LO_DS:
7913 case R_PPC64_ADDR24:
7914 case R_PPC64_ADDR32:
7915 case R_PPC64_UADDR16:
7916 case R_PPC64_UADDR32:
7917 case R_PPC64_UADDR64:
7918 case R_PPC64_TOC:
7919 break;
7920 }
7921
7922 if (local_syms != NULL)
7923 {
7924 unsigned long r_symndx;
8c1d1bb8
AM
7925 bfd *ibfd = sec->owner;
7926
7927 r_symndx = ELF64_R_SYM (r_info);
7928 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7929 return FALSE;
7930 }
7931
0e1862bb 7932 if ((bfd_link_pic (info)
1d483afe 7933 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7934 || (h != NULL
198f1157 7935 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7936 || h->root.type == bfd_link_hash_defweak
7937 || !h->def_regular))))
7938 || (ELIMINATE_COPY_RELOCS
0e1862bb 7939 && !bfd_link_pic (info)
8c1d1bb8
AM
7940 && h != NULL
7941 && (h->root.type == bfd_link_hash_defweak
7942 || !h->def_regular)))
7943 ;
7944 else
7945 return TRUE;
7946
7947 if (h != NULL)
6edfbbad 7948 {
19e08130
AM
7949 struct elf_dyn_relocs *p;
7950 struct elf_dyn_relocs **pp;
7951 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7952
7953 /* elf_gc_sweep may have already removed all dyn relocs associated
7954 with local syms for a given section. Also, symbol flags are
7955 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7956 report a dynreloc miscount. */
7957 if (*pp == NULL && info->gc_sections)
7958 return TRUE;
7959
7960 while ((p = *pp) != NULL)
60124e18 7961 {
19e08130
AM
7962 if (p->sec == sec)
7963 {
7964 if (!must_be_dyn_reloc (info, r_type))
7965 p->pc_count -= 1;
7966 p->count -= 1;
7967 if (p->count == 0)
7968 *pp = p->next;
7969 return TRUE;
7970 }
7971 pp = &p->next;
60124e18 7972 }
6edfbbad 7973 }
19e08130
AM
7974 else
7975 {
7976 struct ppc_dyn_relocs *p;
7977 struct ppc_dyn_relocs **pp;
7978 void *vpp;
7979 bfd_boolean is_ifunc;
8c1d1bb8 7980
19e08130
AM
7981 if (local_syms == NULL)
7982 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7983 if (sym_sec == NULL)
7984 sym_sec = sec;
c57da1a7 7985
19e08130
AM
7986 vpp = &elf_section_data (sym_sec)->local_dynrel;
7987 pp = (struct ppc_dyn_relocs **) vpp;
7988
7989 if (*pp == NULL && info->gc_sections)
7990 return TRUE;
7991
7992 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7993 while ((p = *pp) != NULL)
8c1d1bb8 7994 {
19e08130
AM
7995 if (p->sec == sec && p->ifunc == is_ifunc)
7996 {
7997 p->count -= 1;
7998 if (p->count == 0)
7999 *pp = p->next;
8000 return TRUE;
8001 }
8002 pp = &p->next;
8c1d1bb8 8003 }
8c1d1bb8
AM
8004 }
8005
695344c0 8006 /* xgettext:c-format */
cf97bcb0
AM
8007 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
8008 sec->owner, sec);
8c1d1bb8
AM
8009 bfd_set_error (bfd_error_bad_value);
8010 return FALSE;
8011}
8012
754021d0
AM
8013/* Remove unused Official Procedure Descriptor entries. Currently we
8014 only remove those associated with functions in discarded link-once
8015 sections, or weakly defined functions that have been overridden. It
8016 would be possible to remove many more entries for statically linked
8017 applications. */
8018
b34976b6 8019bfd_boolean
e7d1c40c 8020ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
8021{
8022 bfd *ibfd;
754021d0 8023 bfd_boolean some_edited = FALSE;
3f764659 8024 asection *need_pad = NULL;
e7d1c40c
AM
8025 struct ppc_link_hash_table *htab;
8026
8027 htab = ppc_hash_table (info);
8028 if (htab == NULL)
8029 return FALSE;
1e2f5b6e 8030
c72f2fb2 8031 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
8032 {
8033 asection *sec;
8034 Elf_Internal_Rela *relstart, *rel, *relend;
8035 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 8036 Elf_Internal_Sym *local_syms;
74f0fb50 8037 struct _opd_sec_data *opd;
51aecdc5 8038 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 8039 bfd_size_type cnt_16b = 0;
1e2f5b6e 8040
854b41e7
AM
8041 if (!is_ppc64_elf (ibfd))
8042 continue;
8043
1e2f5b6e 8044 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 8045 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
8046 continue;
8047
dbaa2011 8048 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
8049 continue;
8050
1e2f5b6e
AM
8051 if (sec->output_section == bfd_abs_section_ptr)
8052 continue;
8053
8054 /* Look through the section relocs. */
8055 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
8056 continue;
8057
6cdc0ccc 8058 local_syms = NULL;
0ffa91dd 8059 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
8060
8061 /* Read the relocations. */
4ce794b7 8062 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 8063 info->keep_memory);
1e2f5b6e 8064 if (relstart == NULL)
b34976b6 8065 return FALSE;
1e2f5b6e
AM
8066
8067 /* First run through the relocs to check they are sane, and to
8068 determine whether we need to edit this opd section. */
b34976b6 8069 need_edit = FALSE;
51aecdc5 8070 broken = FALSE;
3f764659 8071 need_pad = sec;
1e2f5b6e 8072 relend = relstart + sec->reloc_count;
50bc7936 8073 for (rel = relstart; rel < relend; )
1e2f5b6e 8074 {
04c9666a 8075 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
8076 unsigned long r_symndx;
8077 asection *sym_sec;
8078 struct elf_link_hash_entry *h;
8079 Elf_Internal_Sym *sym;
51aecdc5 8080 bfd_vma offset;
1e2f5b6e 8081
51aecdc5 8082 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
8083 only interested in the reloc pointing to a function entry
8084 point. */
51aecdc5
AM
8085 offset = rel->r_offset;
8086 if (rel + 1 == relend
8087 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
8088 {
8089 /* If someone messes with .opd alignment then after a
8090 "ld -r" we might have padding in the middle of .opd.
8091 Also, there's nothing to prevent someone putting
8092 something silly in .opd with the assembler. No .opd
b34976b6 8093 optimization for them! */
3f764659 8094 broken_opd:
4eca0228 8095 _bfd_error_handler
871b3ab2 8096 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 8097 broken = TRUE;
1e2f5b6e
AM
8098 break;
8099 }
8100
50bc7936
AM
8101 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
8102 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
8103 {
4eca0228 8104 _bfd_error_handler
695344c0 8105 /* xgettext:c-format */
871b3ab2 8106 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 8107 ibfd, r_type);
51aecdc5 8108 broken = TRUE;
50bc7936
AM
8109 break;
8110 }
8111
1e2f5b6e 8112 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
8113 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8114 r_symndx, ibfd))
50bc7936 8115 goto error_ret;
1e2f5b6e
AM
8116
8117 if (sym_sec == NULL || sym_sec->owner == NULL)
8118 {
411e1bfb
AM
8119 const char *sym_name;
8120 if (h != NULL)
8121 sym_name = h->root.root.string;
8122 else
26c61ae5
L
8123 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8124 sym_sec);
411e1bfb 8125
4eca0228 8126 _bfd_error_handler
695344c0 8127 /* xgettext:c-format */
871b3ab2 8128 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 8129 ibfd, sym_name);
51aecdc5 8130 broken = TRUE;
1e2f5b6e
AM
8131 break;
8132 }
8133
51020317
AM
8134 /* opd entries are always for functions defined in the
8135 current input bfd. If the symbol isn't defined in the
8136 input bfd, then we won't be using the function in this
8137 bfd; It must be defined in a linkonce section in another
8138 bfd, or is weak. It's also possible that we are
8139 discarding the function due to a linker script /DISCARD/,
8140 which we test for via the output_section. */
8141 if (sym_sec->owner != ibfd
8142 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 8143 need_edit = TRUE;
1e2f5b6e 8144
50bc7936 8145 rel += 2;
51aecdc5
AM
8146 if (rel + 1 == relend
8147 || (rel + 2 < relend
8148 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8149 ++rel;
8150
8151 if (rel == relend)
3f764659
JJ
8152 {
8153 if (sec->size == offset + 24)
8154 {
8155 need_pad = NULL;
8156 break;
8157 }
51aecdc5 8158 if (sec->size == offset + 16)
3f764659
JJ
8159 {
8160 cnt_16b++;
8161 break;
8162 }
8163 goto broken_opd;
8164 }
3f764659
JJ
8165 else if (rel + 1 < relend
8166 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8167 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8168 {
51aecdc5
AM
8169 if (rel[0].r_offset == offset + 16)
8170 cnt_16b++;
8171 else if (rel[0].r_offset != offset + 24)
8172 goto broken_opd;
3f764659
JJ
8173 }
8174 else
8175 goto broken_opd;
1e2f5b6e
AM
8176 }
8177
e7d1c40c 8178 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8179
51aecdc5 8180 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8181 {
8182 Elf_Internal_Rela *write_rel;
d4730f92 8183 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8184 bfd_byte *rptr, *wptr;
983bddc8 8185 bfd_byte *new_contents;
74f0fb50
AM
8186 bfd_size_type amt;
8187
983bddc8 8188 new_contents = NULL;
51aecdc5 8189 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8190 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8191 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8192 if (opd->adjust == NULL)
8193 return FALSE;
1e2f5b6e
AM
8194
8195 /* This seems a waste of time as input .opd sections are all
8196 zeros as generated by gcc, but I suppose there's no reason
8197 this will always be so. We might start putting something in
8198 the third word of .opd entries. */
8199 if ((sec->flags & SEC_IN_MEMORY) == 0)
8200 {
eea6121a
AM
8201 bfd_byte *loc;
8202 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8203 {
eea6121a
AM
8204 if (loc != NULL)
8205 free (loc);
50bc7936 8206 error_ret:
6cdc0ccc
AM
8207 if (local_syms != NULL
8208 && symtab_hdr->contents != (unsigned char *) local_syms)
8209 free (local_syms);
6cdc0ccc
AM
8210 if (elf_section_data (sec)->relocs != relstart)
8211 free (relstart);
b34976b6 8212 return FALSE;
6cdc0ccc 8213 }
1e2f5b6e
AM
8214 sec->contents = loc;
8215 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8216 }
8217
8218 elf_section_data (sec)->relocs = relstart;
8219
3f764659 8220 new_contents = sec->contents;
3f764659
JJ
8221 if (add_aux_fields)
8222 {
8223 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8224 if (new_contents == NULL)
8225 return FALSE;
51aecdc5 8226 need_pad = NULL;
3f764659 8227 }
b4f4e59f
AM
8228 wptr = new_contents;
8229 rptr = sec->contents;
1e2f5b6e 8230 write_rel = relstart;
51aecdc5 8231 for (rel = relstart; rel < relend; )
1e2f5b6e 8232 {
50bc7936
AM
8233 unsigned long r_symndx;
8234 asection *sym_sec;
8235 struct elf_link_hash_entry *h;
51aecdc5 8236 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8237 Elf_Internal_Sym *sym;
51aecdc5
AM
8238 long opd_ent_size;
8239 Elf_Internal_Rela *next_rel;
8240 bfd_boolean skip;
50bc7936
AM
8241
8242 r_symndx = ELF64_R_SYM (rel->r_info);
8243 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8244 r_symndx, ibfd))
50bc7936
AM
8245 goto error_ret;
8246
51aecdc5
AM
8247 next_rel = rel + 2;
8248 if (next_rel + 1 == relend
8249 || (next_rel + 2 < relend
8250 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8251 ++next_rel;
8252
8253 /* See if the .opd entry is full 24 byte or
8254 16 byte (with fd_aux entry overlapped with next
8255 fd_func). */
8256 opd_ent_size = 24;
8257 if (next_rel == relend)
1e2f5b6e 8258 {
51aecdc5 8259 if (sec->size == rel->r_offset + 16)
3f764659 8260 opd_ent_size = 16;
51aecdc5
AM
8261 }
8262 else if (next_rel->r_offset == rel->r_offset + 16)
8263 opd_ent_size = 16;
3f764659 8264
51aecdc5
AM
8265 if (h != NULL
8266 && h->root.root.string[0] == '.')
8267 {
8c5b4e52
AM
8268 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8269 if (fdh != NULL)
8270 {
8271 fdh = ppc_follow_link (fdh);
8272 if (fdh->elf.root.type != bfd_link_hash_defined
8273 && fdh->elf.root.type != bfd_link_hash_defweak)
8274 fdh = NULL;
8275 }
51aecdc5 8276 }
1e2f5b6e 8277
51aecdc5
AM
8278 skip = (sym_sec->owner != ibfd
8279 || sym_sec->output_section == bfd_abs_section_ptr);
8280 if (skip)
8281 {
8282 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8283 {
51aecdc5
AM
8284 /* Arrange for the function descriptor sym
8285 to be dropped. */
8286 fdh->elf.root.u.def.value = 0;
8287 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8288 }
51aecdc5 8289 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8290
0e1862bb 8291 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8292 rel = next_rel;
8293 else
8294 while (1)
8295 {
8296 if (!dec_dynrel_count (rel->r_info, sec, info,
8297 NULL, h, sym))
8298 goto error_ret;
754021d0 8299
51aecdc5
AM
8300 if (++rel == next_rel)
8301 break;
1e2f5b6e 8302
51aecdc5
AM
8303 r_symndx = ELF64_R_SYM (rel->r_info);
8304 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8305 r_symndx, ibfd))
8306 goto error_ret;
8307 }
50bc7936
AM
8308 }
8309 else
1e2f5b6e 8310 {
51aecdc5
AM
8311 /* We'll be keeping this opd entry. */
8312 long adjust;
8313
8314 if (fdh != NULL)
8315 {
8316 /* Redefine the function descriptor symbol to
8317 this location in the opd section. It is
8318 necessary to update the value here rather
8319 than using an array of adjustments as we do
8320 for local symbols, because various places
8321 in the generic ELF code use the value
8322 stored in u.def.value. */
8323 fdh->elf.root.u.def.value = wptr - new_contents;
8324 fdh->adjust_done = 1;
8325 }
8326
8327 /* Local syms are a bit tricky. We could
8328 tweak them as they can be cached, but
8329 we'd need to look through the local syms
8330 for the function descriptor sym which we
8331 don't have at the moment. So keep an
8332 array of adjustments. */
8333 adjust = (wptr - new_contents) - (rptr - sec->contents);
8334 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8335
8336 if (wptr != rptr)
8337 memcpy (wptr, rptr, opd_ent_size);
8338 wptr += opd_ent_size;
8339 if (add_aux_fields && opd_ent_size == 16)
8340 {
8341 memset (wptr, '\0', 8);
8342 wptr += 8;
8343 }
8344
50bc7936 8345 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8346 new opd entries. */
8347 for ( ; rel != next_rel; ++rel)
8348 {
8349 rel->r_offset += adjust;
8350 if (write_rel != rel)
8351 memcpy (write_rel, rel, sizeof (*rel));
8352 ++write_rel;
8353 }
1e2f5b6e 8354 }
51aecdc5
AM
8355
8356 rptr += opd_ent_size;
1e2f5b6e
AM
8357 }
8358
3f764659 8359 sec->size = wptr - new_contents;
1e2f5b6e 8360 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8361 if (add_aux_fields)
8362 {
8363 free (sec->contents);
8364 sec->contents = new_contents;
8365 }
8366
05bf9422 8367 /* Fudge the header size too, as this is used later in
cdcf6e38 8368 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8369 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8370 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8371 some_edited = TRUE;
1e2f5b6e 8372 }
6cdc0ccc 8373 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8374 free (relstart);
6cdc0ccc 8375
411e1bfb
AM
8376 if (local_syms != NULL
8377 && symtab_hdr->contents != (unsigned char *) local_syms)
8378 {
8379 if (!info->keep_memory)
8380 free (local_syms);
8381 else
8382 symtab_hdr->contents = (unsigned char *) local_syms;
8383 }
8384 }
8385
754021d0
AM
8386 if (some_edited)
8387 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8388
3f764659
JJ
8389 /* If we are doing a final link and the last .opd entry is just 16 byte
8390 long, add a 8 byte padding after it. */
0e1862bb 8391 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8392 {
8393 bfd_byte *p;
8394
8395 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8396 {
8397 BFD_ASSERT (need_pad->size > 0);
8398
8399 p = bfd_malloc (need_pad->size + 8);
8400 if (p == NULL)
8401 return FALSE;
699733f6 8402
2cdcc330
AM
8403 if (!bfd_get_section_contents (need_pad->owner, need_pad,
8404 p, 0, need_pad->size))
3f764659
JJ
8405 return FALSE;
8406
8407 need_pad->contents = p;
8408 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8409 }
8410 else
8411 {
8412 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8413 if (p == NULL)
8414 return FALSE;
8415
8416 need_pad->contents = p;
8417 }
8418
8419 memset (need_pad->contents + need_pad->size, 0, 8);
8420 need_pad->size += 8;
8421 }
8422
411e1bfb
AM
8423 return TRUE;
8424}
8425
3e04d765
AM
8426/* Analyze inline PLT call relocations to see whether calls to locally
8427 defined functions can be converted to direct calls. */
8428
8429bfd_boolean
8430ppc64_elf_inline_plt (struct bfd_link_info *info)
8431{
8432 struct ppc_link_hash_table *htab;
8433 bfd *ibfd;
8434 asection *sec;
8435 bfd_vma low_vma, high_vma, limit;
8436
8437 htab = ppc_hash_table (info);
8438 if (htab == NULL)
8439 return FALSE;
8440
8441 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8442 reduced somewhat to cater for possible stubs that might be added
8443 between the call and its destination. */
8444 if (htab->params->group_size < 0)
8445 {
8446 limit = -htab->params->group_size;
8447 if (limit == 1)
8448 limit = 0x1e00000;
8449 }
8450 else
8451 {
8452 limit = htab->params->group_size;
8453 if (limit == 1)
8454 limit = 0x1c00000;
8455 }
8456
8457 low_vma = -1;
8458 high_vma = 0;
8459 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8460 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8461 {
8462 if (low_vma > sec->vma)
8463 low_vma = sec->vma;
8464 if (high_vma < sec->vma + sec->size)
8465 high_vma = sec->vma + sec->size;
8466 }
8467
8468 /* If a "bl" can reach anywhere in local code sections, then we can
8469 convert all inline PLT sequences to direct calls when the symbol
8470 is local. */
8471 if (high_vma - low_vma < limit)
8472 {
8473 htab->can_convert_all_inline_plt = 1;
8474 return TRUE;
8475 }
8476
8477 /* Otherwise, go looking through relocs for cases where a direct
8478 call won't reach. Mark the symbol on any such reloc to disable
8479 the optimization and keep the PLT entry as it seems likely that
8480 this will be better than creating trampolines. Note that this
8481 will disable the optimization for all inline PLT calls to a
8482 particular symbol, not just those that won't reach. The
8483 difficulty in doing a more precise optimization is that the
8484 linker needs to make a decision depending on whether a
8485 particular R_PPC64_PLTCALL insn can be turned into a direct
8486 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8487 the sequence, and there is nothing that ties those relocs
8488 together except their symbol. */
8489
8490 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8491 {
8492 Elf_Internal_Shdr *symtab_hdr;
8493 Elf_Internal_Sym *local_syms;
8494
8495 if (!is_ppc64_elf (ibfd))
8496 continue;
8497
8498 local_syms = NULL;
8499 symtab_hdr = &elf_symtab_hdr (ibfd);
8500
8501 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8502 if (ppc64_elf_section_data (sec)->has_pltcall
8503 && !bfd_is_abs_section (sec->output_section))
8504 {
8505 Elf_Internal_Rela *relstart, *rel, *relend;
8506
8507 /* Read the relocations. */
8508 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8509 info->keep_memory);
8510 if (relstart == NULL)
8511 return FALSE;
8512
8513 relend = relstart + sec->reloc_count;
8514 for (rel = relstart; rel < relend; )
8515 {
8516 enum elf_ppc64_reloc_type r_type;
8517 unsigned long r_symndx;
8518 asection *sym_sec;
8519 struct elf_link_hash_entry *h;
8520 Elf_Internal_Sym *sym;
8521 unsigned char *tls_maskp;
8522
8523 r_type = ELF64_R_TYPE (rel->r_info);
8524 if (r_type != R_PPC64_PLTCALL)
8525 continue;
8526
8527 r_symndx = ELF64_R_SYM (rel->r_info);
8528 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8529 r_symndx, ibfd))
8530 {
8531 if (elf_section_data (sec)->relocs != relstart)
8532 free (relstart);
8533 if (local_syms != NULL
2cdcc330 8534 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
8535 free (local_syms);
8536 return FALSE;
8537 }
8538
8539 if (sym_sec != NULL && sym_sec->output_section != NULL)
8540 {
8541 bfd_vma from, to;
8542 if (h != NULL)
8543 to = h->root.u.def.value;
8544 else
8545 to = sym->st_value;
8546 to += (rel->r_addend
8547 + sym_sec->output_offset
8548 + sym_sec->output_section->vma);
8549 from = (rel->r_offset
8550 + sec->output_offset
8551 + sec->output_section->vma);
8552 if (to - from + limit < 2 * limit)
8553 *tls_maskp &= ~PLT_KEEP;
8554 }
8555 }
8556 if (elf_section_data (sec)->relocs != relstart)
8557 free (relstart);
8558 }
8559
8560 if (local_syms != NULL
8561 && symtab_hdr->contents != (unsigned char *) local_syms)
8562 {
8563 if (!info->keep_memory)
8564 free (local_syms);
8565 else
8566 symtab_hdr->contents = (unsigned char *) local_syms;
8567 }
8568 }
8569
8570 return TRUE;
8571}
8572
e1918d23 8573/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8574
e1918d23 8575asection *
e7d1c40c 8576ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8577{
411e1bfb
AM
8578 struct ppc_link_hash_table *htab;
8579
411e1bfb 8580 htab = ppc_hash_table (info);
4dfe6ac6
NC
8581 if (htab == NULL)
8582 return NULL;
8583
ee67d69a
AM
8584 if (abiversion (info->output_bfd) == 1)
8585 htab->opd_abi = 1;
8586
e7d1c40c 8587 if (htab->params->no_multi_toc)
33c0ec9d
AM
8588 htab->do_multi_toc = 0;
8589 else if (!htab->do_multi_toc)
e7d1c40c 8590 htab->params->no_multi_toc = 1;
33c0ec9d 8591
8b5f1ed8
AM
8592 /* Default to --no-plt-localentry, as this option can cause problems
8593 with symbol interposition. For example, glibc libpthread.so and
8594 libc.so duplicate many pthread symbols, with a fallback
8595 implementation in libc.so. In some cases the fallback does more
8596 work than the pthread implementation. __pthread_condattr_destroy
8597 is one such symbol: the libpthread.so implementation is
8598 localentry:0 while the libc.so implementation is localentry:8.
8599 An app that "cleverly" uses dlopen to only load necessary
8600 libraries at runtime may omit loading libpthread.so when not
8601 running multi-threaded, which then results in the libc.so
8602 fallback symbols being used and ld.so complaining. Now there
8603 are workarounds in ld (see non_zero_localentry) to detect the
8604 pthread situation, but that may not be the only case where
8605 --plt-localentry can cause trouble. */
f378ab09 8606 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8607 htab->params->plt_localentry0 = 0;
d44c746a
AM
8608 if (htab->params->plt_localentry0
8609 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8610 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
8611 _bfd_error_handler
8612 (_("warning: --plt-localentry is especially dangerous without "
8613 "ld.so support to detect ABI violations"));
f378ab09 8614
3a71aa26
AM
8615 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8616 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8617 FALSE, FALSE, TRUE));
a7f2871e
AM
8618 /* Move dynamic linking info to the function descriptor sym. */
8619 if (htab->tls_get_addr != NULL)
8620 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8621 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8622 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8623 FALSE, FALSE, TRUE));
7c9cf415 8624 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8625 {
8626 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8627
8628 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8629 FALSE, FALSE, TRUE);
8630 if (opt != NULL)
8631 func_desc_adjust (opt, info);
8632 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8633 FALSE, FALSE, TRUE);
8634 if (opt_fd != NULL
8635 && (opt_fd->root.type == bfd_link_hash_defined
8636 || opt_fd->root.type == bfd_link_hash_defweak))
8637 {
8638 /* If glibc supports an optimized __tls_get_addr call stub,
8639 signalled by the presence of __tls_get_addr_opt, and we'll
8640 be calling __tls_get_addr via a plt call stub, then
8641 make __tls_get_addr point to __tls_get_addr_opt. */
8642 tga_fd = &htab->tls_get_addr_fd->elf;
8643 if (htab->elf.dynamic_sections_created
8644 && tga_fd != NULL
8645 && (tga_fd->type == STT_FUNC
8646 || tga_fd->needs_plt)
8647 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8648 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8649 {
8650 struct plt_entry *ent;
8651
8652 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8653 if (ent->plt.refcount > 0)
8654 break;
8655 if (ent != NULL)
8656 {
8657 tga_fd->root.type = bfd_link_hash_indirect;
8658 tga_fd->root.u.i.link = &opt_fd->root;
8659 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8660 opt_fd->mark = 1;
a7f2871e
AM
8661 if (opt_fd->dynindx != -1)
8662 {
8663 /* Use __tls_get_addr_opt in dynamic relocations. */
8664 opt_fd->dynindx = -1;
8665 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8666 opt_fd->dynstr_index);
8667 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8668 return NULL;
a7f2871e 8669 }
2cdcc330
AM
8670 htab->tls_get_addr_fd
8671 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
8672 tga = &htab->tls_get_addr->elf;
8673 if (opt != NULL && tga != NULL)
8674 {
8675 tga->root.type = bfd_link_hash_indirect;
8676 tga->root.u.i.link = &opt->root;
8677 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8678 opt->mark = 1;
a7f2871e
AM
8679 _bfd_elf_link_hash_hide_symbol (info, opt,
8680 tga->forced_local);
8681 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8682 }
8683 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8684 htab->tls_get_addr_fd->is_func_descriptor = 1;
8685 if (htab->tls_get_addr != NULL)
8686 {
8687 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8688 htab->tls_get_addr->is_func = 1;
8689 }
8690 }
8691 }
8692 }
7c9cf415
AM
8693 else if (htab->params->tls_get_addr_opt < 0)
8694 htab->params->tls_get_addr_opt = 0;
a7f2871e 8695 }
33c0ec9d 8696 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8697}
8387904d 8698
3a71aa26
AM
8699/* Return TRUE iff REL is a branch reloc with a global symbol matching
8700 HASH1 or HASH2. */
8387904d 8701
3a71aa26
AM
8702static bfd_boolean
8703branch_reloc_hash_match (const bfd *ibfd,
8704 const Elf_Internal_Rela *rel,
8705 const struct ppc_link_hash_entry *hash1,
8706 const struct ppc_link_hash_entry *hash2)
8707{
8708 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8709 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8710 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8711
e054468f 8712 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8713 {
3a71aa26
AM
8714 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8715 struct elf_link_hash_entry *h;
8387904d 8716
3a71aa26 8717 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8718 h = elf_follow_link (h);
3a71aa26
AM
8719 if (h == &hash1->elf || h == &hash2->elf)
8720 return TRUE;
a48ebf4d 8721 }
3a71aa26 8722 return FALSE;
951fd09b 8723}
411e1bfb 8724
951fd09b
AM
8725/* Run through all the TLS relocs looking for optimization
8726 opportunities. The linker has been hacked (see ppc64elf.em) to do
8727 a preliminary section layout so that we know the TLS segment
8728 offsets. We can't optimize earlier because some optimizations need
8729 to know the tp offset, and we need to optimize before allocating
8730 dynamic relocations. */
8731
8732bfd_boolean
33c0ec9d 8733ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8734{
8735 bfd *ibfd;
8736 asection *sec;
8737 struct ppc_link_hash_table *htab;
663a1470 8738 unsigned char *toc_ref;
102890f0 8739 int pass;
951fd09b 8740
3cbc1e5e 8741 if (!bfd_link_executable (info))
411e1bfb
AM
8742 return TRUE;
8743
951fd09b 8744 htab = ppc_hash_table (info);
4dfe6ac6
NC
8745 if (htab == NULL)
8746 return FALSE;
8747
663a1470
AM
8748 /* Make two passes over the relocs. On the first pass, mark toc
8749 entries involved with tls relocs, and check that tls relocs
8750 involved in setting up a tls_get_addr call are indeed followed by
8751 such a call. If they are not, we can't do any tls optimization.
8752 On the second pass twiddle tls_mask flags to notify
8753 relocate_section that optimization can be done, and adjust got
8754 and plt refcounts. */
8755 toc_ref = NULL;
8756 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8757 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8758 {
8759 Elf_Internal_Sym *locsyms = NULL;
8760 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8761
102890f0
AM
8762 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8763 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8764 {
8765 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8766 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8767
102890f0
AM
8768 /* Read the relocations. */
8769 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8770 info->keep_memory);
8771 if (relstart == NULL)
2915c55b
JK
8772 {
8773 free (toc_ref);
8774 return FALSE;
8775 }
411e1bfb 8776
102890f0
AM
8777 relend = relstart + sec->reloc_count;
8778 for (rel = relstart; rel < relend; rel++)
8779 {
8780 enum elf_ppc64_reloc_type r_type;
8781 unsigned long r_symndx;
8782 struct elf_link_hash_entry *h;
8783 Elf_Internal_Sym *sym;
8784 asection *sym_sec;
f961d9dd
AM
8785 unsigned char *tls_mask;
8786 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8787 bfd_vma value;
8788 bfd_boolean ok_tprel, is_local;
8789 long toc_ref_index = 0;
8790 int expecting_tls_get_addr = 0;
663a1470 8791 bfd_boolean ret = FALSE;
411e1bfb 8792
102890f0
AM
8793 r_symndx = ELF64_R_SYM (rel->r_info);
8794 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8795 r_symndx, ibfd))
8796 {
8797 err_free_rel:
8798 if (elf_section_data (sec)->relocs != relstart)
8799 free (relstart);
8800 if (toc_ref != NULL)
8801 free (toc_ref);
8802 if (locsyms != NULL
0ffa91dd 8803 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8804 != (unsigned char *) locsyms))
8805 free (locsyms);
663a1470 8806 return ret;
102890f0 8807 }
411e1bfb 8808
102890f0
AM
8809 if (h != NULL)
8810 {
766bc656
AM
8811 if (h->root.type == bfd_link_hash_defined
8812 || h->root.type == bfd_link_hash_defweak)
8813 value = h->root.u.def.value;
8814 else if (h->root.type == bfd_link_hash_undefweak)
8815 value = 0;
8816 else
663a1470
AM
8817 {
8818 found_tls_get_addr_arg = 0;
8819 continue;
8820 }
102890f0
AM
8821 }
8822 else
8823 /* Symbols referenced by TLS relocs must be of type
8824 STT_TLS. So no need for .opd local sym adjust. */
8825 value = sym->st_value;
8826
8827 ok_tprel = FALSE;
8828 is_local = FALSE;
8829 if (h == NULL
8830 || !h->def_dynamic)
8831 {
8832 is_local = TRUE;
766bc656
AM
8833 if (h != NULL
8834 && h->root.type == bfd_link_hash_undefweak)
8835 ok_tprel = TRUE;
c27b8c2a
AM
8836 else if (sym_sec != NULL
8837 && sym_sec->output_section != NULL)
766bc656
AM
8838 {
8839 value += sym_sec->output_offset;
8840 value += sym_sec->output_section->vma;
8841 value -= htab->elf.tls_sec->vma;
8842 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8843 < (bfd_vma) 1 << 32);
8844 }
102890f0 8845 }
951fd09b 8846
102890f0 8847 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8848 /* If this section has old-style __tls_get_addr calls
8849 without marker relocs, then check that each
8850 __tls_get_addr call reloc is preceded by a reloc
8851 that conceivably belongs to the __tls_get_addr arg
8852 setup insn. If we don't find matching arg setup
8853 relocs, don't do any tls optimization. */
8854 if (pass == 0
8855 && sec->has_tls_get_addr_call
8856 && h != NULL
8857 && (h == &htab->tls_get_addr->elf
8858 || h == &htab->tls_get_addr_fd->elf)
8859 && !found_tls_get_addr_arg
8860 && is_branch_reloc (r_type))
8861 {
25f53a85 8862 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8863 "TLS optimization disabled\n"),
8864 ibfd, sec, rel->r_offset);
8865 ret = TRUE;
8866 goto err_free_rel;
8867 }
8868
8869 found_tls_get_addr_arg = 0;
102890f0
AM
8870 switch (r_type)
8871 {
8872 case R_PPC64_GOT_TLSLD16:
8873 case R_PPC64_GOT_TLSLD16_LO:
8874 expecting_tls_get_addr = 1;
663a1470 8875 found_tls_get_addr_arg = 1;
1a0670f3 8876 /* Fall through. */
102890f0
AM
8877
8878 case R_PPC64_GOT_TLSLD16_HI:
8879 case R_PPC64_GOT_TLSLD16_HA:
8880 /* These relocs should never be against a symbol
8881 defined in a shared lib. Leave them alone if
8882 that turns out to be the case. */
8883 if (!is_local)
8884 continue;
411e1bfb 8885
102890f0 8886 /* LD -> LE */
411e1bfb 8887 tls_set = 0;
102890f0
AM
8888 tls_clear = TLS_LD;
8889 tls_type = TLS_TLS | TLS_LD;
8890 break;
411e1bfb 8891
102890f0
AM
8892 case R_PPC64_GOT_TLSGD16:
8893 case R_PPC64_GOT_TLSGD16_LO:
8894 expecting_tls_get_addr = 1;
663a1470 8895 found_tls_get_addr_arg = 1;
1a0670f3 8896 /* Fall through. */
102890f0
AM
8897
8898 case R_PPC64_GOT_TLSGD16_HI:
8899 case R_PPC64_GOT_TLSGD16_HA:
8900 if (ok_tprel)
8901 /* GD -> LE */
411e1bfb 8902 tls_set = 0;
102890f0
AM
8903 else
8904 /* GD -> IE */
8905 tls_set = TLS_TLS | TLS_TPRELGD;
8906 tls_clear = TLS_GD;
8907 tls_type = TLS_TLS | TLS_GD;
8908 break;
8909
8910 case R_PPC64_GOT_TPREL16_DS:
8911 case R_PPC64_GOT_TPREL16_LO_DS:
8912 case R_PPC64_GOT_TPREL16_HI:
8913 case R_PPC64_GOT_TPREL16_HA:
8914 if (ok_tprel)
8915 {
8916 /* IE -> LE */
8917 tls_set = 0;
8918 tls_clear = TLS_TPREL;
8919 tls_type = TLS_TLS | TLS_TPREL;
8920 break;
8921 }
411e1bfb
AM
8922 continue;
8923
727fc41e
AM
8924 case R_PPC64_TLSGD:
8925 case R_PPC64_TLSLD:
23cedd1d
AM
8926 if (rel + 1 < relend
8927 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8928 {
8929 if (pass != 0
2cdcc330
AM
8930 && (ELF64_R_TYPE (rel[1].r_info)
8931 != R_PPC64_PLTSEQ))
23cedd1d
AM
8932 {
8933 r_symndx = ELF64_R_SYM (rel[1].r_info);
8934 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8935 r_symndx, ibfd))
23cedd1d
AM
8936 goto err_free_rel;
8937 if (h != NULL)
8938 {
8939 struct plt_entry *ent = NULL;
8940
8941 for (ent = h->plt.plist;
8942 ent != NULL;
8943 ent = ent->next)
8944 if (ent->addend == rel[1].r_addend)
8945 break;
8946
8947 if (ent != NULL
8948 && ent->plt.refcount > 0)
8949 ent->plt.refcount -= 1;
8950 }
8951 }
8952 continue;
8953 }
663a1470 8954 found_tls_get_addr_arg = 1;
1a0670f3 8955 /* Fall through. */
663a1470
AM
8956
8957 case R_PPC64_TLS:
8958 case R_PPC64_TOC16:
8959 case R_PPC64_TOC16_LO:
102890f0
AM
8960 if (sym_sec == NULL || sym_sec != toc)
8961 continue;
8962
8963 /* Mark this toc entry as referenced by a TLS
8964 code sequence. We can do that now in the
8965 case of R_PPC64_TLS, and after checking for
8966 tls_get_addr for the TOC16 relocs. */
8967 if (toc_ref == NULL)
2cdcc330
AM
8968 toc_ref
8969 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8970 if (toc_ref == NULL)
8971 goto err_free_rel;
8972
102890f0
AM
8973 if (h != NULL)
8974 value = h->root.u.def.value;
8975 else
8976 value = sym->st_value;
8977 value += rel->r_addend;
73242275
AM
8978 if (value % 8 != 0)
8979 continue;
8980 BFD_ASSERT (value < toc->size
8981 && toc->output_offset % 8 == 0);
663a1470 8982 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8983 if (r_type == R_PPC64_TLS
8984 || r_type == R_PPC64_TLSGD
8985 || r_type == R_PPC64_TLSLD)
102890f0
AM
8986 {
8987 toc_ref[toc_ref_index] = 1;
8988 continue;
8989 }
8990
8991 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8992 continue;
8993
8994 tls_set = 0;
8995 tls_clear = 0;
8996 expecting_tls_get_addr = 2;
8997 break;
8998
8999 case R_PPC64_TPREL64:
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 (ok_tprel)
9006 {
9007 /* IE -> LE */
9008 tls_set = TLS_EXPLICIT;
9009 tls_clear = TLS_TPREL;
9010 break;
9011 }
9012 continue;
9013
9014 case R_PPC64_DTPMOD64:
9015 if (pass == 0
9016 || sec != toc
9017 || toc_ref == NULL
663a1470 9018 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
9019 continue;
9020 if (rel + 1 < relend
9021 && (rel[1].r_info
9022 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
9023 && rel[1].r_offset == rel->r_offset + 8)
9024 {
9025 if (ok_tprel)
9026 /* GD -> LE */
9027 tls_set = TLS_EXPLICIT | TLS_GD;
9028 else
9029 /* GD -> IE */
9030 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
9031 tls_clear = TLS_GD;
9032 }
9033 else
9034 {
9035 if (!is_local)
9036 continue;
9037
9038 /* LD -> LE */
9039 tls_set = TLS_EXPLICIT;
9040 tls_clear = TLS_LD;
9041 }
9042 break;
9043
9044 default:
9045 continue;
9046 }
9047
9048 if (pass == 0)
9049 {
727fc41e
AM
9050 if (!expecting_tls_get_addr
9051 || !sec->has_tls_get_addr_call)
102890f0
AM
9052 continue;
9053
3a71aa26
AM
9054 if (rel + 1 < relend
9055 && branch_reloc_hash_match (ibfd, rel + 1,
9056 htab->tls_get_addr,
9057 htab->tls_get_addr_fd))
102890f0 9058 {
3a71aa26 9059 if (expecting_tls_get_addr == 2)
102890f0 9060 {
3a71aa26 9061 /* Check for toc tls entries. */
f961d9dd 9062 unsigned char *toc_tls;
3a71aa26
AM
9063 int retval;
9064
9065 retval = get_tls_mask (&toc_tls, NULL, NULL,
9066 &locsyms,
9067 rel, ibfd);
9068 if (retval == 0)
9069 goto err_free_rel;
663a1470
AM
9070 if (toc_tls != NULL)
9071 {
37da22e5
AM
9072 if ((*toc_tls & TLS_TLS) != 0
9073 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
9074 found_tls_get_addr_arg = 1;
9075 if (retval > 1)
9076 toc_ref[toc_ref_index] = 1;
9077 }
102890f0 9078 }
3a71aa26 9079 continue;
102890f0
AM
9080 }
9081
102890f0
AM
9082 /* Uh oh, we didn't find the expected call. We
9083 could just mark this symbol to exclude it
9084 from tls optimization but it's safer to skip
663a1470 9085 the entire optimization. */
695344c0 9086 /* xgettext:c-format */
25f53a85 9087 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
9088 "TLS optimization disabled\n"),
9089 ibfd, sec, rel->r_offset);
9090 ret = TRUE;
9091 goto err_free_rel;
102890f0
AM
9092 }
9093
37da22e5
AM
9094 /* If we don't have old-style __tls_get_addr calls
9095 without TLSGD/TLSLD marker relocs, and we haven't
9096 found a new-style __tls_get_addr call with a
9097 marker for this symbol, then we either have a
9098 broken object file or an -mlongcall style
9099 indirect call to __tls_get_addr without a marker.
9100 Disable optimization in this case. */
9101 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
9102 && (tls_set & TLS_EXPLICIT) == 0
9103 && !sec->has_tls_get_addr_call
9104 && ((*tls_mask & (TLS_TLS | TLS_MARK))
9105 != (TLS_TLS | TLS_MARK)))
9106 continue;
9107
23cedd1d 9108 if (expecting_tls_get_addr)
102890f0 9109 {
23cedd1d
AM
9110 struct plt_entry *ent = NULL;
9111
9112 if (htab->tls_get_addr != NULL)
9113 for (ent = htab->tls_get_addr->elf.plt.plist;
9114 ent != NULL;
9115 ent = ent->next)
9116 if (ent->addend == 0)
102890f0 9117 break;
411e1bfb 9118
23cedd1d
AM
9119 if (ent == NULL && htab->tls_get_addr_fd != NULL)
9120 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
9121 ent != NULL;
9122 ent = ent->next)
9123 if (ent->addend == 0)
102890f0 9124 break;
23cedd1d
AM
9125
9126 if (ent != NULL
9127 && ent->plt.refcount > 0)
9128 ent->plt.refcount -= 1;
102890f0 9129 }
411e1bfb 9130
102890f0 9131 if (tls_clear == 0)
30038c59
AM
9132 continue;
9133
102890f0
AM
9134 if ((tls_set & TLS_EXPLICIT) == 0)
9135 {
9136 struct got_entry *ent;
411e1bfb 9137
102890f0
AM
9138 /* Adjust got entry for this reloc. */
9139 if (h != NULL)
9140 ent = h->got.glist;
9141 else
9142 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 9143
102890f0
AM
9144 for (; ent != NULL; ent = ent->next)
9145 if (ent->addend == rel->r_addend
9146 && ent->owner == ibfd
9147 && ent->tls_type == tls_type)
9148 break;
9149 if (ent == NULL)
9150 abort ();
411e1bfb 9151
102890f0
AM
9152 if (tls_set == 0)
9153 {
9154 /* We managed to get rid of a got entry. */
9155 if (ent->got.refcount > 0)
9156 ent->got.refcount -= 1;
9157 }
9158 }
9159 else
9160 {
9161 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9162 we'll lose one or two dyn relocs. */
9163 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 9164 NULL, h, sym))
102890f0 9165 return FALSE;
411e1bfb 9166
102890f0
AM
9167 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9168 {
9169 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 9170 NULL, h, sym))
102890f0
AM
9171 return FALSE;
9172 }
9173 }
411e1bfb 9174
102890f0
AM
9175 *tls_mask |= tls_set;
9176 *tls_mask &= ~tls_clear;
9177 }
8c1d1bb8 9178
102890f0
AM
9179 if (elf_section_data (sec)->relocs != relstart)
9180 free (relstart);
9181 }
411e1bfb 9182
663a1470
AM
9183 if (locsyms != NULL
9184 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9185 {
9186 if (!info->keep_memory)
9187 free (locsyms);
9188 else
9189 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9190 }
9191 }
411e1bfb 9192
663a1470
AM
9193 if (toc_ref != NULL)
9194 free (toc_ref);
9a23f96e 9195 htab->do_tls_opt = 1;
b34976b6 9196 return TRUE;
1e2f5b6e 9197}
b34976b6 9198
c5614fa4
AM
9199/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9200 the values of any global symbols in a toc section that has been
9201 edited. Globals in toc sections should be a rarity, so this function
9202 sets a flag if any are found in toc sections other than the one just
de194d85 9203 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
9204
9205struct adjust_toc_info
9206{
9207 asection *toc;
9208 unsigned long *skip;
9209 bfd_boolean global_toc_syms;
9210};
9211
ba761f19
AM
9212enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9213
c5614fa4
AM
9214static bfd_boolean
9215adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9216{
9217 struct ppc_link_hash_entry *eh;
9218 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 9219 unsigned long i;
c5614fa4 9220
c5614fa4
AM
9221 if (h->root.type != bfd_link_hash_defined
9222 && h->root.type != bfd_link_hash_defweak)
9223 return TRUE;
9224
9225 eh = (struct ppc_link_hash_entry *) h;
9226 if (eh->adjust_done)
9227 return TRUE;
9228
9229 if (eh->elf.root.u.def.section == toc_inf->toc)
9230 {
854b41e7
AM
9231 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9232 i = toc_inf->toc->rawsize >> 3;
c5614fa4 9233 else
854b41e7
AM
9234 i = eh->elf.root.u.def.value >> 3;
9235
ba761f19 9236 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 9237 {
4eca0228 9238 _bfd_error_handler
854b41e7
AM
9239 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9240 do
9241 ++i;
ba761f19 9242 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 9243 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 9244 }
854b41e7
AM
9245
9246 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
9247 eh->adjust_done = 1;
9248 }
9249 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9250 toc_inf->global_toc_syms = TRUE;
9251
9252 return TRUE;
9253}
9254
39eeab25
AM
9255/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9256 on a _LO variety toc/got reloc. */
560c8763
AM
9257
9258static bfd_boolean
39eeab25 9259ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 9260{
39eeab25
AM
9261 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9262 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
9263 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9264 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9265 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9266 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9267 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9268 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9269 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9270 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9271 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9272 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9273 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9274 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9275 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
9276 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9277 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9278 /* Exclude lfqu by testing reloc. If relocs are ever
9279 defined for the reduced D field in psq_lu then those
9280 will need testing too. */
9281 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9282 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9283 && (insn & 1) == 0)
9284 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9285 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9286 /* Exclude stfqu. psq_stu as above for psq_lu. */
9287 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9288 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9289 && (insn & 1) == 0));
560c8763
AM
9290}
9291
c5614fa4
AM
9292/* Examine all relocs referencing .toc sections in order to remove
9293 unused .toc entries. */
9294
9295bfd_boolean
33c0ec9d 9296ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
9297{
9298 bfd *ibfd;
9299 struct adjust_toc_info toc_inf;
67f0cbdb 9300 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 9301
67f0cbdb 9302 htab->do_toc_opt = 1;
c5614fa4 9303 toc_inf.global_toc_syms = TRUE;
c72f2fb2 9304 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
9305 {
9306 asection *toc, *sec;
9307 Elf_Internal_Shdr *symtab_hdr;
9308 Elf_Internal_Sym *local_syms;
425b145b 9309 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
9310 unsigned long *skip, *drop;
9311 unsigned char *used;
9312 unsigned char *keep, last, some_unused;
9313
854b41e7
AM
9314 if (!is_ppc64_elf (ibfd))
9315 continue;
9316
c5614fa4
AM
9317 toc = bfd_get_section_by_name (ibfd, ".toc");
9318 if (toc == NULL
92b7a70f 9319 || toc->size == 0
dbaa2011
AM
9320 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9321 || discarded_section (toc))
c5614fa4
AM
9322 continue;
9323
425b145b 9324 toc_relocs = NULL;
c5614fa4 9325 local_syms = NULL;
0ffa91dd 9326 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
9327
9328 /* Look at sections dropped from the final link. */
9329 skip = NULL;
9330 relstart = NULL;
9331 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9332 {
9333 if (sec->reloc_count == 0
dbaa2011 9334 || !discarded_section (sec)
c5614fa4
AM
9335 || get_opd_info (sec)
9336 || (sec->flags & SEC_ALLOC) == 0
9337 || (sec->flags & SEC_DEBUGGING) != 0)
9338 continue;
9339
9340 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9341 if (relstart == NULL)
9342 goto error_ret;
9343
9344 /* Run through the relocs to see which toc entries might be
9345 unused. */
9346 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9347 {
9348 enum elf_ppc64_reloc_type r_type;
9349 unsigned long r_symndx;
9350 asection *sym_sec;
9351 struct elf_link_hash_entry *h;
9352 Elf_Internal_Sym *sym;
9353 bfd_vma val;
9354
9355 r_type = ELF64_R_TYPE (rel->r_info);
9356 switch (r_type)
9357 {
9358 default:
9359 continue;
9360
9361 case R_PPC64_TOC16:
9362 case R_PPC64_TOC16_LO:
9363 case R_PPC64_TOC16_HI:
9364 case R_PPC64_TOC16_HA:
9365 case R_PPC64_TOC16_DS:
9366 case R_PPC64_TOC16_LO_DS:
9367 break;
9368 }
9369
9370 r_symndx = ELF64_R_SYM (rel->r_info);
9371 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9372 r_symndx, ibfd))
9373 goto error_ret;
9374
9375 if (sym_sec != toc)
9376 continue;
9377
9378 if (h != NULL)
9379 val = h->root.u.def.value;
9380 else
9381 val = sym->st_value;
9382 val += rel->r_addend;
9383
9384 if (val >= toc->size)
9385 continue;
9386
9387 /* Anything in the toc ought to be aligned to 8 bytes.
9388 If not, don't mark as unused. */
9389 if (val & 7)
9390 continue;
9391
9392 if (skip == NULL)
9393 {
854b41e7 9394 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9395 if (skip == NULL)
9396 goto error_ret;
9397 }
9398
ba761f19 9399 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9400 }
9401
9402 if (elf_section_data (sec)->relocs != relstart)
9403 free (relstart);
9404 }
9405
ba761f19
AM
9406 /* For largetoc loads of address constants, we can convert
9407 . addis rx,2,addr@got@ha
9408 . ld ry,addr@got@l(rx)
9409 to
9410 . addis rx,2,addr@toc@ha
9411 . addi ry,rx,addr@toc@l
9412 when addr is within 2G of the toc pointer. This then means
9413 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9414
ba761f19
AM
9415 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9416 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9417 && toc->reloc_count != 0)
9418 {
9419 /* Read toc relocs. */
425b145b
AM
9420 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9421 info->keep_memory);
9422 if (toc_relocs == NULL)
ba761f19
AM
9423 goto error_ret;
9424
425b145b 9425 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9426 {
9427 enum elf_ppc64_reloc_type r_type;
9428 unsigned long r_symndx;
9429 asection *sym_sec;
9430 struct elf_link_hash_entry *h;
9431 Elf_Internal_Sym *sym;
9432 bfd_vma val, addr;
9433
9434 r_type = ELF64_R_TYPE (rel->r_info);
9435 if (r_type != R_PPC64_ADDR64)
9436 continue;
9437
9438 r_symndx = ELF64_R_SYM (rel->r_info);
9439 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9440 r_symndx, ibfd))
9441 goto error_ret;
9442
425b145b 9443 if (sym_sec == NULL
c27b8c2a 9444 || sym_sec->output_section == NULL
dbaa2011 9445 || discarded_section (sym_sec))
425b145b
AM
9446 continue;
9447
afe397ea 9448 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9449 continue;
9450
9451 if (h != NULL)
bddc25c9
AM
9452 {
9453 if (h->type == STT_GNU_IFUNC)
9454 continue;
9455 val = h->root.u.def.value;
9456 }
ba761f19 9457 else
bddc25c9
AM
9458 {
9459 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9460 continue;
9461 val = sym->st_value;
9462 }
ba761f19
AM
9463 val += rel->r_addend;
9464 val += sym_sec->output_section->vma + sym_sec->output_offset;
9465
9466 /* We don't yet know the exact toc pointer value, but we
9467 know it will be somewhere in the toc section. Don't
9468 optimize if the difference from any possible toc
9469 pointer is outside [ff..f80008000, 7fff7fff]. */
9470 addr = toc->output_section->vma + TOC_BASE_OFF;
9471 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9472 continue;
9473
9474 addr = toc->output_section->vma + toc->output_section->rawsize;
9475 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9476 continue;
9477
9478 if (skip == NULL)
9479 {
9480 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9481 if (skip == NULL)
9482 goto error_ret;
9483 }
9484
9485 skip[rel->r_offset >> 3]
425b145b 9486 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9487 }
ba761f19
AM
9488 }
9489
c5614fa4
AM
9490 if (skip == NULL)
9491 continue;
9492
9493 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9494 if (used == NULL)
9495 {
9496 error_ret:
9497 if (local_syms != NULL
9498 && symtab_hdr->contents != (unsigned char *) local_syms)
9499 free (local_syms);
9500 if (sec != NULL
9501 && relstart != NULL
9502 && elf_section_data (sec)->relocs != relstart)
9503 free (relstart);
425b145b
AM
9504 if (toc_relocs != NULL
9505 && elf_section_data (toc)->relocs != toc_relocs)
9506 free (toc_relocs);
c5614fa4
AM
9507 if (skip != NULL)
9508 free (skip);
9509 return FALSE;
9510 }
9511
30038c59
AM
9512 /* Now check all kept sections that might reference the toc.
9513 Check the toc itself last. */
9514 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9515 : ibfd->sections);
c5614fa4 9516 sec != NULL;
c5614fa4 9517 sec = (sec == toc ? NULL
c5614fa4 9518 : sec->next == NULL ? toc
30038c59 9519 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9520 : sec->next))
9521 {
9522 int repeat;
9523
9524 if (sec->reloc_count == 0
dbaa2011 9525 || discarded_section (sec)
c5614fa4
AM
9526 || get_opd_info (sec)
9527 || (sec->flags & SEC_ALLOC) == 0
9528 || (sec->flags & SEC_DEBUGGING) != 0)
9529 continue;
9530
854b41e7
AM
9531 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9532 info->keep_memory);
c5614fa4 9533 if (relstart == NULL)
2915c55b
JK
9534 {
9535 free (used);
9536 goto error_ret;
9537 }
c5614fa4
AM
9538
9539 /* Mark toc entries referenced as used. */
c5614fa4 9540 do
d4f1ee75
AM
9541 {
9542 repeat = 0;
9543 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9544 {
9545 enum elf_ppc64_reloc_type r_type;
9546 unsigned long r_symndx;
9547 asection *sym_sec;
9548 struct elf_link_hash_entry *h;
9549 Elf_Internal_Sym *sym;
9550 bfd_vma val;
9551 enum {no_check, check_lo, check_ha} insn_check;
98528052 9552
d4f1ee75
AM
9553 r_type = ELF64_R_TYPE (rel->r_info);
9554 switch (r_type)
9555 {
9556 default:
9557 insn_check = no_check;
9558 break;
98528052 9559
d4f1ee75
AM
9560 case R_PPC64_GOT_TLSLD16_HA:
9561 case R_PPC64_GOT_TLSGD16_HA:
9562 case R_PPC64_GOT_TPREL16_HA:
9563 case R_PPC64_GOT_DTPREL16_HA:
9564 case R_PPC64_GOT16_HA:
9565 case R_PPC64_TOC16_HA:
9566 insn_check = check_ha;
9567 break;
98528052 9568
d4f1ee75
AM
9569 case R_PPC64_GOT_TLSLD16_LO:
9570 case R_PPC64_GOT_TLSGD16_LO:
9571 case R_PPC64_GOT_TPREL16_LO_DS:
9572 case R_PPC64_GOT_DTPREL16_LO_DS:
9573 case R_PPC64_GOT16_LO:
9574 case R_PPC64_GOT16_LO_DS:
9575 case R_PPC64_TOC16_LO:
9576 case R_PPC64_TOC16_LO_DS:
9577 insn_check = check_lo;
9578 break;
9579 }
560c8763 9580
d4f1ee75
AM
9581 if (insn_check != no_check)
9582 {
9583 bfd_vma off = rel->r_offset & ~3;
9584 unsigned char buf[4];
9585 unsigned int insn;
c5614fa4 9586
d4f1ee75
AM
9587 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9588 {
9589 free (used);
9590 goto error_ret;
9591 }
9592 insn = bfd_get_32 (ibfd, buf);
9593 if (insn_check == check_lo
39eeab25 9594 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9595 : ((insn & ((0x3f << 26) | 0x1f << 16))
9596 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9597 {
9598 char str[12];
9599
9600 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9601 sprintf (str, "%#08x", insn);
9602 info->callbacks->einfo
695344c0 9603 /* xgettext:c-format */
174d0a74 9604 (_("%H: toc optimization is not supported for"
cf97bcb0 9605 " %s instruction\n"),
d4f1ee75
AM
9606 ibfd, sec, rel->r_offset & ~3, str);
9607 }
9608 }
c5614fa4 9609
d4f1ee75
AM
9610 switch (r_type)
9611 {
9612 case R_PPC64_TOC16:
9613 case R_PPC64_TOC16_LO:
9614 case R_PPC64_TOC16_HI:
9615 case R_PPC64_TOC16_HA:
9616 case R_PPC64_TOC16_DS:
9617 case R_PPC64_TOC16_LO_DS:
9618 /* In case we're taking addresses of toc entries. */
9619 case R_PPC64_ADDR64:
9620 break;
c5614fa4 9621
d4f1ee75
AM
9622 default:
9623 continue;
9624 }
c5614fa4 9625
d4f1ee75
AM
9626 r_symndx = ELF64_R_SYM (rel->r_info);
9627 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9628 r_symndx, ibfd))
9629 {
9630 free (used);
9631 goto error_ret;
9632 }
c5614fa4 9633
d4f1ee75
AM
9634 if (sym_sec != toc)
9635 continue;
c5614fa4 9636
d4f1ee75
AM
9637 if (h != NULL)
9638 val = h->root.u.def.value;
9639 else
9640 val = sym->st_value;
9641 val += rel->r_addend;
ba761f19 9642
d4f1ee75
AM
9643 if (val >= toc->size)
9644 continue;
ba761f19 9645
d4f1ee75
AM
9646 if ((skip[val >> 3] & can_optimize) != 0)
9647 {
9648 bfd_vma off;
9649 unsigned char opc;
9650
9651 switch (r_type)
9652 {
9653 case R_PPC64_TOC16_HA:
ba761f19 9654 break;
ba761f19 9655
d4f1ee75
AM
9656 case R_PPC64_TOC16_LO_DS:
9657 off = rel->r_offset;
9658 off += (bfd_big_endian (ibfd) ? -2 : 3);
9659 if (!bfd_get_section_contents (ibfd, sec, &opc,
9660 off, 1))
9661 {
9662 free (used);
9663 goto error_ret;
9664 }
9665 if ((opc & (0x3f << 2)) == (58u << 2))
9666 break;
1a0670f3 9667 /* Fall through. */
ba761f19 9668
d4f1ee75
AM
9669 default:
9670 /* Wrong sort of reloc, or not a ld. We may
9671 as well clear ref_from_discarded too. */
9672 skip[val >> 3] = 0;
9673 }
9674 }
9675
9676 if (sec != toc)
9677 used[val >> 3] = 1;
9678 /* For the toc section, we only mark as used if this
9679 entry itself isn't unused. */
9680 else if ((used[rel->r_offset >> 3]
9681 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9682 && !used[val >> 3])
9683 {
9684 /* Do all the relocs again, to catch reference
9685 chains. */
9686 repeat = 1;
9687 used[val >> 3] = 1;
9688 }
9689 }
9690 }
c5614fa4 9691 while (repeat);
854b41e7
AM
9692
9693 if (elf_section_data (sec)->relocs != relstart)
9694 free (relstart);
c5614fa4
AM
9695 }
9696
9697 /* Merge the used and skip arrays. Assume that TOC
9698 doublewords not appearing as either used or unused belong
de194d85 9699 to an entry more than one doubleword in size. */
c5614fa4
AM
9700 for (drop = skip, keep = used, last = 0, some_unused = 0;
9701 drop < skip + (toc->size + 7) / 8;
9702 ++drop, ++keep)
9703 {
9704 if (*keep)
9705 {
ba761f19
AM
9706 *drop &= ~ref_from_discarded;
9707 if ((*drop & can_optimize) != 0)
9708 some_unused = 1;
c5614fa4
AM
9709 last = 0;
9710 }
b140b010 9711 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9712 {
9713 some_unused = 1;
ba761f19 9714 last = ref_from_discarded;
c5614fa4
AM
9715 }
9716 else
9717 *drop = last;
9718 }
9719
9720 free (used);
9721
9722 if (some_unused)
9723 {
9724 bfd_byte *contents, *src;
9725 unsigned long off;
d62b3684 9726 Elf_Internal_Sym *sym;
ba761f19 9727 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9728
9729 /* Shuffle the toc contents, and at the same time convert the
9730 skip array from booleans into offsets. */
9731 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9732 goto error_ret;
9733
9734 elf_section_data (toc)->this_hdr.contents = contents;
9735
9736 for (src = contents, off = 0, drop = skip;
9737 src < contents + toc->size;
9738 src += 8, ++drop)
9739 {
ba761f19
AM
9740 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9741 off += 8;
c5614fa4
AM
9742 else if (off != 0)
9743 {
9744 *drop = off;
9745 memcpy (src - off, src, 8);
9746 }
9747 }
854b41e7 9748 *drop = off;
c5614fa4
AM
9749 toc->rawsize = toc->size;
9750 toc->size = src - contents - off;
9751
ba761f19
AM
9752 /* Adjust addends for relocs against the toc section sym,
9753 and optimize any accesses we can. */
c5614fa4
AM
9754 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9755 {
9756 if (sec->reloc_count == 0
dbaa2011 9757 || discarded_section (sec))
c5614fa4
AM
9758 continue;
9759
9760 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9761 info->keep_memory);
c5614fa4
AM
9762 if (relstart == NULL)
9763 goto error_ret;
9764
9765 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9766 {
9767 enum elf_ppc64_reloc_type r_type;
9768 unsigned long r_symndx;
9769 asection *sym_sec;
9770 struct elf_link_hash_entry *h;
854b41e7 9771 bfd_vma val;
c5614fa4
AM
9772
9773 r_type = ELF64_R_TYPE (rel->r_info);
9774 switch (r_type)
9775 {
9776 default:
9777 continue;
9778
9779 case R_PPC64_TOC16:
9780 case R_PPC64_TOC16_LO:
9781 case R_PPC64_TOC16_HI:
9782 case R_PPC64_TOC16_HA:
9783 case R_PPC64_TOC16_DS:
9784 case R_PPC64_TOC16_LO_DS:
9785 case R_PPC64_ADDR64:
9786 break;
9787 }
9788
9789 r_symndx = ELF64_R_SYM (rel->r_info);
9790 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9791 r_symndx, ibfd))
9792 goto error_ret;
9793
ba761f19 9794 if (sym_sec != toc)
c5614fa4
AM
9795 continue;
9796
ba761f19
AM
9797 if (h != NULL)
9798 val = h->root.u.def.value;
9799 else
9800 {
9801 val = sym->st_value;
9802 if (val != 0)
9803 local_toc_syms = TRUE;
9804 }
9805
9806 val += rel->r_addend;
854b41e7
AM
9807
9808 if (val > toc->rawsize)
9809 val = toc->rawsize;
ba761f19
AM
9810 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9811 continue;
9812 else if ((skip[val >> 3] & can_optimize) != 0)
9813 {
9814 Elf_Internal_Rela *tocrel
425b145b 9815 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9816 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9817
9818 switch (r_type)
9819 {
9820 case R_PPC64_TOC16_HA:
9821 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9822 break;
9823
9824 case R_PPC64_TOC16_LO_DS:
9825 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9826 break;
9827
9828 default:
28942f62
AM
9829 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9830 ppc_howto_init ();
b140b010 9831 info->callbacks->einfo
695344c0 9832 /* xgettext:c-format */
174d0a74 9833 (_("%H: %s references "
b140b010
AM
9834 "optimized away TOC entry\n"),
9835 ibfd, sec, rel->r_offset,
9836 ppc64_elf_howto_table[r_type]->name);
9837 bfd_set_error (bfd_error_bad_value);
9838 goto error_ret;
ba761f19
AM
9839 }
9840 rel->r_addend = tocrel->r_addend;
9841 elf_section_data (sec)->relocs = relstart;
9842 continue;
9843 }
9844
9845 if (h != NULL || sym->st_value != 0)
9846 continue;
854b41e7
AM
9847
9848 rel->r_addend -= skip[val >> 3];
9849 elf_section_data (sec)->relocs = relstart;
c5614fa4 9850 }
854b41e7
AM
9851
9852 if (elf_section_data (sec)->relocs != relstart)
9853 free (relstart);
c5614fa4
AM
9854 }
9855
9856 /* We shouldn't have local or global symbols defined in the TOC,
9857 but handle them anyway. */
df22d223
AM
9858 if (local_syms != NULL)
9859 for (sym = local_syms;
9860 sym < local_syms + symtab_hdr->sh_info;
9861 ++sym)
9862 if (sym->st_value != 0
9863 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9864 {
9865 unsigned long i;
854b41e7 9866
df22d223
AM
9867 if (sym->st_value > toc->rawsize)
9868 i = toc->rawsize >> 3;
9869 else
9870 i = sym->st_value >> 3;
854b41e7 9871
df22d223
AM
9872 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9873 {
9874 if (local_toc_syms)
4eca0228 9875 _bfd_error_handler
df22d223
AM
9876 (_("%s defined on removed toc entry"),
9877 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9878 do
9879 ++i;
9880 while ((skip[i] & (ref_from_discarded | can_optimize)));
9881 sym->st_value = (bfd_vma) i << 3;
9882 }
d62b3684 9883
df22d223
AM
9884 sym->st_value -= skip[i];
9885 symtab_hdr->contents = (unsigned char *) local_syms;
9886 }
c5614fa4 9887
854b41e7 9888 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9889 if (toc_inf.global_toc_syms)
9890 {
9891 toc_inf.toc = toc;
9892 toc_inf.skip = skip;
9893 toc_inf.global_toc_syms = FALSE;
9894 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9895 &toc_inf);
9896 }
854b41e7
AM
9897
9898 if (toc->reloc_count != 0)
9899 {
d4730f92 9900 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9901 Elf_Internal_Rela *wrel;
9902 bfd_size_type sz;
9903
854b41e7 9904 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9905 if (toc_relocs == NULL)
9906 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9907 info->keep_memory);
9908 if (toc_relocs == NULL)
9909 goto error_ret;
9910
425b145b
AM
9911 wrel = toc_relocs;
9912 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9913 if ((skip[rel->r_offset >> 3]
9914 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9915 {
9916 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9917 wrel->r_info = rel->r_info;
9918 wrel->r_addend = rel->r_addend;
9919 ++wrel;
9920 }
9921 else if (!dec_dynrel_count (rel->r_info, toc, info,
9922 &local_syms, NULL, NULL))
9923 goto error_ret;
9924
425b145b
AM
9925 elf_section_data (toc)->relocs = toc_relocs;
9926 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9927 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9928 sz = rel_hdr->sh_entsize;
9929 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9930 }
c5614fa4 9931 }
28be611c
AM
9932 else if (toc_relocs != NULL
9933 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9934 free (toc_relocs);
c5614fa4
AM
9935
9936 if (local_syms != NULL
9937 && symtab_hdr->contents != (unsigned char *) local_syms)
9938 {
9939 if (!info->keep_memory)
9940 free (local_syms);
9941 else
9942 symtab_hdr->contents = (unsigned char *) local_syms;
9943 }
9944 free (skip);
9945 }
9946
9947 return TRUE;
9948}
9949
1bbe0902
AM
9950/* Return true iff input section I references the TOC using
9951 instructions limited to +/-32k offsets. */
9952
9953bfd_boolean
9954ppc64_elf_has_small_toc_reloc (asection *i)
9955{
9956 return (is_ppc64_elf (i->owner)
9957 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9958}
9959
927be08e
AM
9960/* Allocate space for one GOT entry. */
9961
9962static void
9963allocate_got (struct elf_link_hash_entry *h,
9964 struct bfd_link_info *info,
9965 struct got_entry *gent)
9966{
9967 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9968 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9969 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9970 ? 16 : 8);
9971 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9972 ? 2 : 1) * sizeof (Elf64_External_Rela);
9973 asection *got = ppc64_elf_tdata (gent->owner)->got;
9974
9975 gent->got.offset = got->size;
9976 got->size += entsize;
9977
19e08130 9978 if (h->type == STT_GNU_IFUNC)
927be08e 9979 {
33e44f2e 9980 htab->elf.irelplt->size += rentsize;
19e08130 9981 htab->got_reli_size += rentsize;
927be08e 9982 }
f15d0b54
AM
9983 else if (((bfd_link_pic (info)
9984 && !((gent->tls_type & TLS_TPREL) != 0
9985 && bfd_link_executable (info)
9986 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9987 || (htab->elf.dynamic_sections_created
9988 && h->dynindx != -1
9989 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9990 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9991 {
19e08130 9992 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9993 relgot->size += rentsize;
927be08e
AM
9994 }
9995}
9996
7865406b
AM
9997/* This function merges got entries in the same toc group. */
9998
9999static void
10000merge_got_entries (struct got_entry **pent)
10001{
10002 struct got_entry *ent, *ent2;
10003
10004 for (ent = *pent; ent != NULL; ent = ent->next)
10005 if (!ent->is_indirect)
10006 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
10007 if (!ent2->is_indirect
10008 && ent2->addend == ent->addend
10009 && ent2->tls_type == ent->tls_type
10010 && elf_gp (ent2->owner) == elf_gp (ent->owner))
10011 {
10012 ent2->is_indirect = TRUE;
10013 ent2->got.ent = ent;
10014 }
10015}
10016
46434633 10017/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
10018
10019static bfd_boolean
46434633
AM
10020ensure_undef_dynamic (struct bfd_link_info *info,
10021 struct elf_link_hash_entry *h)
f0158f44
AM
10022{
10023 struct elf_link_hash_table *htab = elf_hash_table (info);
10024
10025 if (htab->dynamic_sections_created
46434633
AM
10026 && ((info->dynamic_undefined_weak != 0
10027 && h->root.type == bfd_link_hash_undefweak)
10028 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
10029 && h->dynindx == -1
10030 && !h->forced_local
10031 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
10032 return bfd_elf_link_record_dynamic_symbol (info, h);
10033 return TRUE;
10034}
10035
65f38f15
AM
10036/* Allocate space in .plt, .got and associated reloc sections for
10037 dynamic relocs. */
5bd4f169 10038
b34976b6 10039static bfd_boolean
4ce794b7 10040allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 10041{
65f38f15
AM
10042 struct bfd_link_info *info;
10043 struct ppc_link_hash_table *htab;
5bd4f169 10044 asection *s;
65f38f15 10045 struct ppc_link_hash_entry *eh;
0b8bcf0d 10046 struct got_entry **pgent, *gent;
5bd4f169 10047
e92d460e 10048 if (h->root.type == bfd_link_hash_indirect)
b34976b6 10049 return TRUE;
5bd4f169 10050
65f38f15
AM
10051 info = (struct bfd_link_info *) inf;
10052 htab = ppc_hash_table (info);
4dfe6ac6
NC
10053 if (htab == NULL)
10054 return FALSE;
5bd4f169 10055
951fd09b
AM
10056 eh = (struct ppc_link_hash_entry *) h;
10057 /* Run through the TLS GD got entries first if we're changing them
10058 to TPREL. */
37da22e5 10059 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
10060 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10061 if (gent->got.refcount > 0
10062 && (gent->tls_type & TLS_GD) != 0)
10063 {
10064 /* This was a GD entry that has been converted to TPREL. If
10065 there happens to be a TPREL entry we can use that one. */
10066 struct got_entry *ent;
10067 for (ent = h->got.glist; ent != NULL; ent = ent->next)
10068 if (ent->got.refcount > 0
10069 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
10070 && ent->addend == gent->addend
10071 && ent->owner == gent->owner)
951fd09b
AM
10072 {
10073 gent->got.refcount = 0;
10074 break;
10075 }
10076
10077 /* If not, then we'll be using our own TPREL entry. */
10078 if (gent->got.refcount != 0)
10079 gent->tls_type = TLS_TLS | TLS_TPREL;
10080 }
10081
7865406b
AM
10082 /* Remove any list entry that won't generate a word in the GOT before
10083 we call merge_got_entries. Otherwise we risk merging to empty
10084 entries. */
0b8bcf0d
AM
10085 pgent = &h->got.glist;
10086 while ((gent = *pgent) != NULL)
411e1bfb 10087 if (gent->got.refcount > 0)
7865406b
AM
10088 {
10089 if ((gent->tls_type & TLS_LD) != 0
10090 && !h->def_dynamic)
10091 {
10092 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
10093 *pgent = gent->next;
10094 }
10095 else
10096 pgent = &gent->next;
10097 }
10098 else
10099 *pgent = gent->next;
10100
10101 if (!htab->do_multi_toc)
10102 merge_got_entries (&h->got.glist);
10103
10104 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10105 if (!gent->is_indirect)
411e1bfb 10106 {
46434633
AM
10107 /* Make sure this symbol is output as a dynamic symbol. */
10108 if (!ensure_undef_dynamic (info, h))
f0158f44 10109 return FALSE;
65f38f15 10110
0c8d6e5c 10111 if (!is_ppc64_elf (gent->owner))
927be08e 10112 abort ();
0ffa91dd 10113
927be08e 10114 allocate_got (h, info, gent);
411e1bfb 10115 }
65f38f15 10116
954b63d4
AM
10117 /* If no dynamic sections we can't have dynamic relocs, except for
10118 IFUNCs which are handled even in static executables. */
8a2058b5
AM
10119 if (!htab->elf.dynamic_sections_created
10120 && h->type != STT_GNU_IFUNC)
10121 eh->dyn_relocs = NULL;
10122
529fe20e
AM
10123 /* Discard relocs on undefined symbols that must be local. */
10124 else if (h->root.type == bfd_link_hash_undefined
10125 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10126 eh->dyn_relocs = NULL;
10127
954b63d4
AM
10128 /* Also discard relocs on undefined weak syms with non-default
10129 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 10130 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
10131 eh->dyn_relocs = NULL;
10132
8a2058b5 10133 if (eh->dyn_relocs != NULL)
65f38f15 10134 {
8a2058b5
AM
10135 struct elf_dyn_relocs *p, **pp;
10136
57e7d118
AM
10137 /* In the shared -Bsymbolic case, discard space allocated for
10138 dynamic pc-relative relocs against symbols which turn out to
10139 be defined in regular objects. For the normal shared case,
10140 discard space for relocs that have become local due to symbol
10141 visibility changes. */
10142
10143 if (bfd_link_pic (info))
65f38f15 10144 {
57e7d118
AM
10145 /* Relocs that use pc_count are those that appear on a call
10146 insn, or certain REL relocs (see must_be_dyn_reloc) that
10147 can be generated via assembly. We want calls to
10148 protected symbols to resolve directly to the function
10149 rather than going via the plt. If people want function
10150 pointer comparisons to work as expected then they should
10151 avoid writing weird assembly. */
10152 if (SYMBOL_CALLS_LOCAL (info, h))
10153 {
57e7d118
AM
10154 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10155 {
10156 p->count -= p->pc_count;
10157 p->pc_count = 0;
10158 if (p->count == 0)
10159 *pp = p->next;
10160 else
10161 pp = &p->next;
10162 }
10163 }
65f38f15 10164
954b63d4 10165 if (eh->dyn_relocs != NULL)
5bd4f169 10166 {
46434633
AM
10167 /* Make sure this symbol is output as a dynamic symbol. */
10168 if (!ensure_undef_dynamic (info, h))
f0158f44 10169 return FALSE;
5bd4f169 10170 }
65f38f15 10171 }
529fe20e 10172 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 10173 {
8a2058b5 10174 /* For the non-pic case, discard space for relocs against
57e7d118
AM
10175 symbols which turn out to need copy relocs or are not
10176 dynamic. */
529fe20e
AM
10177 if (h->dynamic_adjusted
10178 && !h->def_regular
10179 && !ELF_COMMON_DEF_P (h))
f0158f44 10180 {
46434633
AM
10181 /* Make sure this symbol is output as a dynamic symbol. */
10182 if (!ensure_undef_dynamic (info, h))
f0158f44 10183 return FALSE;
dfbb6ac9 10184
f0158f44
AM
10185 if (h->dynindx == -1)
10186 eh->dyn_relocs = NULL;
10187 }
10188 else
8a2058b5 10189 eh->dyn_relocs = NULL;
57e7d118
AM
10190 }
10191
10192 /* Finally, allocate space. */
10193 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10194 {
10195 asection *sreloc = elf_section_data (p->sec)->sreloc;
10196 if (eh->elf.type == STT_GNU_IFUNC)
10197 sreloc = htab->elf.irelplt;
10198 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 10199 }
65f38f15 10200 }
57e7d118 10201
2d7ad24e
AM
10202 /* We might need a PLT entry when the symbol
10203 a) is dynamic, or
10204 b) is an ifunc, or
10205 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10206 d) has plt16 relocs and we are linking statically. */
10207 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10208 || h->type == STT_GNU_IFUNC
10209 || (h->needs_plt && h->dynamic_adjusted)
10210 || (h->needs_plt
10211 && h->def_regular
10212 && !htab->elf.dynamic_sections_created
3e04d765 10213 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
10214 && (((struct ppc_link_hash_entry *) h)->tls_mask
10215 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10216 {
57e7d118
AM
10217 struct plt_entry *pent;
10218 bfd_boolean doneone = FALSE;
10219 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10220 if (pent->plt.refcount > 0)
10221 {
10222 if (!htab->elf.dynamic_sections_created
10223 || h->dynindx == -1)
10224 {
2d7ad24e
AM
10225 if (h->type == STT_GNU_IFUNC)
10226 {
10227 s = htab->elf.iplt;
10228 pent->plt.offset = s->size;
10229 s->size += PLT_ENTRY_SIZE (htab);
10230 s = htab->elf.irelplt;
10231 }
10232 else
10233 {
10234 s = htab->pltlocal;
10235 pent->plt.offset = s->size;
10236 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10237 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10238 }
57e7d118
AM
10239 }
10240 else
10241 {
10242 /* If this is the first .plt entry, make room for the special
10243 first entry. */
10244 s = htab->elf.splt;
10245 if (s->size == 0)
10246 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10247
57e7d118 10248 pent->plt.offset = s->size;
65f38f15 10249
57e7d118
AM
10250 /* Make room for this entry. */
10251 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10252
57e7d118
AM
10253 /* Make room for the .glink code. */
10254 s = htab->glink;
10255 if (s->size == 0)
9e390558 10256 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10257 if (htab->opd_abi)
10258 {
10259 /* We need bigger stubs past index 32767. */
9e390558 10260 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10261 s->size += 4;
10262 s->size += 2*4;
10263 }
10264 else
10265 s->size += 4;
65f38f15 10266
57e7d118
AM
10267 /* We also need to make an entry in the .rela.plt section. */
10268 s = htab->elf.srelplt;
10269 }
2d7ad24e
AM
10270 if (s != NULL)
10271 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
10272 doneone = TRUE;
10273 }
10274 else
10275 pent->plt.offset = (bfd_vma) -1;
10276 if (!doneone)
10277 {
10278 h->plt.plist = NULL;
10279 h->needs_plt = 0;
10280 }
65f38f15 10281 }
57e7d118 10282 else
65f38f15 10283 {
57e7d118
AM
10284 h->plt.plist = NULL;
10285 h->needs_plt = 0;
65f38f15
AM
10286 }
10287
b34976b6 10288 return TRUE;
65f38f15
AM
10289}
10290
9e390558
AM
10291#define PPC_LO(v) ((v) & 0xffff)
10292#define PPC_HI(v) (((v) >> 16) & 0xffff)
10293#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10294
a345bc8d
AM
10295/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10296 to set up space for global entry stubs. These are put in glink,
10297 after the branch table. */
65f38f15 10298
b34976b6 10299static bfd_boolean
a345bc8d 10300size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10301{
a345bc8d
AM
10302 struct bfd_link_info *info;
10303 struct ppc_link_hash_table *htab;
10304 struct plt_entry *pent;
9e390558 10305 asection *s, *plt;
65f38f15 10306
a345bc8d
AM
10307 if (h->root.type == bfd_link_hash_indirect)
10308 return TRUE;
65f38f15 10309
a345bc8d
AM
10310 if (!h->pointer_equality_needed)
10311 return TRUE;
65f38f15 10312
a345bc8d
AM
10313 if (h->def_regular)
10314 return TRUE;
65f38f15 10315
a345bc8d
AM
10316 info = inf;
10317 htab = ppc_hash_table (info);
10318 if (htab == NULL)
10319 return FALSE;
10320
9e390558
AM
10321 s = htab->global_entry;
10322 plt = htab->elf.splt;
a345bc8d
AM
10323 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10324 if (pent->plt.offset != (bfd_vma) -1
10325 && pent->addend == 0)
10326 {
afe397ea
AM
10327 /* For ELFv2, if this symbol is not defined in a regular file
10328 and we are not generating a shared library or pie, then we
10329 need to define the symbol in the executable on a call stub.
10330 This is to avoid text relocations. */
9e390558
AM
10331 bfd_vma off, stub_align, stub_off, stub_size;
10332 unsigned int align_power;
10333
10334 stub_size = 16;
10335 stub_off = s->size;
10336 if (htab->params->plt_stub_align >= 0)
10337 align_power = htab->params->plt_stub_align;
10338 else
10339 align_power = -htab->params->plt_stub_align;
10340 /* Setting section alignment is delayed until we know it is
10341 non-empty. Otherwise the .text output section will be
10342 aligned at least to plt_stub_align even when no global
10343 entry stubs are needed. */
10344 if (s->alignment_power < align_power)
10345 s->alignment_power = align_power;
10346 stub_align = (bfd_vma) 1 << align_power;
10347 if (htab->params->plt_stub_align >= 0
10348 || ((((stub_off + stub_size - 1) & -stub_align)
10349 - (stub_off & -stub_align))
10350 > ((stub_size - 1) & -stub_align)))
10351 stub_off = (stub_off + stub_align - 1) & -stub_align;
10352 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10353 off -= stub_off + s->output_offset + s->output_section->vma;
10354 /* Note that for --plt-stub-align negative we have a possible
10355 dependency between stub offset and size. Break that
10356 dependency by assuming the max stub size when calculating
10357 the stub offset. */
10358 if (PPC_HA (off) == 0)
10359 stub_size -= 4;
8a2058b5 10360 h->root.type = bfd_link_hash_defined;
afe397ea 10361 h->root.u.def.section = s;
9e390558
AM
10362 h->root.u.def.value = stub_off;
10363 s->size = stub_off + stub_size;
a345bc8d
AM
10364 break;
10365 }
10366 return TRUE;
10367}
10368
10369/* Set DF_TEXTREL if we find any dynamic relocs that apply to
10370 read-only sections. */
10371
10372static bfd_boolean
98bbb1b8 10373maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 10374{
98bbb1b8
AM
10375 asection *sec;
10376
a345bc8d
AM
10377 if (h->root.type == bfd_link_hash_indirect)
10378 return TRUE;
10379
98bbb1b8
AM
10380 sec = readonly_dynrelocs (h);
10381 if (sec != NULL)
a345bc8d 10382 {
98bbb1b8
AM
10383 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10384
10385 info->flags |= DF_TEXTREL;
2cdcc330
AM
10386 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
10387 " in read-only section `%pA'\n"),
10388 sec->owner, h->root.root.string, sec);
a345bc8d
AM
10389
10390 /* Not an error, just cut short the traversal. */
10391 return FALSE;
65f38f15 10392 }
b34976b6 10393 return TRUE;
65f38f15
AM
10394}
10395
10396/* Set the sizes of the dynamic sections. */
10397
b34976b6 10398static bfd_boolean
ee67d69a 10399ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10400 struct bfd_link_info *info)
65f38f15
AM
10401{
10402 struct ppc_link_hash_table *htab;
10403 bfd *dynobj;
10404 asection *s;
b34976b6 10405 bfd_boolean relocs;
65f38f15 10406 bfd *ibfd;
7865406b 10407 struct got_entry *first_tlsld;
65f38f15
AM
10408
10409 htab = ppc_hash_table (info);
4dfe6ac6
NC
10410 if (htab == NULL)
10411 return FALSE;
10412
65f38f15
AM
10413 dynobj = htab->elf.dynobj;
10414 if (dynobj == NULL)
10415 abort ();
10416
10417 if (htab->elf.dynamic_sections_created)
10418 {
10419 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10420 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10421 {
3d4d4302 10422 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10423 if (s == NULL)
10424 abort ();
eea6121a 10425 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10426 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10427 }
10428 }
10429
10430 /* Set up .got offsets for local syms, and space for local dynamic
10431 relocs. */
c72f2fb2 10432 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10433 {
411e1bfb
AM
10434 struct got_entry **lgot_ents;
10435 struct got_entry **end_lgot_ents;
e054468f
AM
10436 struct plt_entry **local_plt;
10437 struct plt_entry **end_local_plt;
f961d9dd 10438 unsigned char *lgot_masks;
65f38f15
AM
10439 bfd_size_type locsymcount;
10440 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10441
0c8d6e5c 10442 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10443 continue;
10444
10445 for (s = ibfd->sections; s != NULL; s = s->next)
10446 {
19e08130 10447 struct ppc_dyn_relocs *p;
65f38f15 10448
6edfbbad 10449 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10450 {
ec338859
AM
10451 if (!bfd_is_abs_section (p->sec)
10452 && bfd_is_abs_section (p->sec->output_section))
10453 {
10454 /* Input section has been discarded, either because
10455 it is a copy of a linkonce section or due to
10456 linker script /DISCARD/, so we'll be discarding
10457 the relocs too. */
10458 }
248866a8 10459 else if (p->count != 0)
ec338859 10460 {
19e08130
AM
10461 asection *srel = elf_section_data (p->sec)->sreloc;
10462 if (p->ifunc)
33e44f2e 10463 srel = htab->elf.irelplt;
eea6121a 10464 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10465 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10466 info->flags |= DF_TEXTREL;
ec338859 10467 }
65f38f15
AM
10468 }
10469 }
10470
411e1bfb
AM
10471 lgot_ents = elf_local_got_ents (ibfd);
10472 if (!lgot_ents)
65f38f15
AM
10473 continue;
10474
0ffa91dd 10475 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10476 locsymcount = symtab_hdr->sh_info;
411e1bfb 10477 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10478 local_plt = (struct plt_entry **) end_lgot_ents;
10479 end_local_plt = local_plt + locsymcount;
f961d9dd 10480 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10481 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10482 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10483 {
0b8bcf0d 10484 struct got_entry **pent, *ent;
411e1bfb 10485
0b8bcf0d
AM
10486 pent = lgot_ents;
10487 while ((ent = *pent) != NULL)
411e1bfb
AM
10488 if (ent->got.refcount > 0)
10489 {
e7b938ca 10490 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10491 {
927be08e 10492 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10493 *pent = ent->next;
411e1bfb
AM
10494 }
10495 else
10496 {
19e08130
AM
10497 unsigned int ent_size = 8;
10498 unsigned int rel_size = sizeof (Elf64_External_Rela);
10499
eea6121a 10500 ent->got.offset = s->size;
e7b938ca 10501 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10502 {
19e08130
AM
10503 ent_size *= 2;
10504 rel_size *= 2;
10505 }
10506 s->size += ent_size;
37da22e5 10507 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10508 {
33e44f2e 10509 htab->elf.irelplt->size += rel_size;
19e08130
AM
10510 htab->got_reli_size += rel_size;
10511 }
f15d0b54
AM
10512 else if (bfd_link_pic (info)
10513 && !((ent->tls_type & TLS_TPREL) != 0
10514 && bfd_link_executable (info)))
19e08130
AM
10515 {
10516 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10517 srel->size += rel_size;
927be08e 10518 }
0b8bcf0d 10519 pent = &ent->next;
411e1bfb
AM
10520 }
10521 }
10522 else
0b8bcf0d 10523 *pent = ent->next;
65f38f15 10524 }
e054468f 10525
2d7ad24e
AM
10526 /* Allocate space for plt calls to local syms. */
10527 lgot_masks = (unsigned char *) end_local_plt;
10528 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10529 {
10530 struct plt_entry *ent;
10531
10532 for (ent = *local_plt; ent != NULL; ent = ent->next)
10533 if (ent->plt.refcount > 0)
10534 {
2d7ad24e
AM
10535 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10536 {
10537 s = htab->elf.iplt;
10538 ent->plt.offset = s->size;
10539 s->size += PLT_ENTRY_SIZE (htab);
10540 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10541 }
3e04d765
AM
10542 else if (htab->can_convert_all_inline_plt
10543 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10544 ent->plt.offset = (bfd_vma) -1;
10545 else
10546 {
10547 s = htab->pltlocal;
10548 ent->plt.offset = s->size;
10549 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10550 if (bfd_link_pic (info))
10551 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10552 }
e054468f
AM
10553 }
10554 else
10555 ent->plt.offset = (bfd_vma) -1;
10556 }
65f38f15
AM
10557 }
10558
10559 /* Allocate global sym .plt and .got entries, and space for global
10560 sym dynamic relocs. */
4ce794b7 10561 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10562
0e1862bb 10563 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10564 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10565
7865406b 10566 first_tlsld = NULL;
c72f2fb2 10567 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10568 {
7865406b
AM
10569 struct got_entry *ent;
10570
0c8d6e5c 10571 if (!is_ppc64_elf (ibfd))
102890f0
AM
10572 continue;
10573
7865406b
AM
10574 ent = ppc64_tlsld_got (ibfd);
10575 if (ent->got.refcount > 0)
102890f0 10576 {
7865406b 10577 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10578 {
7865406b
AM
10579 ent->is_indirect = TRUE;
10580 ent->got.ent = first_tlsld;
10581 }
10582 else
10583 {
10584 if (first_tlsld == NULL)
10585 first_tlsld = ent;
10586 s = ppc64_elf_tdata (ibfd)->got;
10587 ent->got.offset = s->size;
10588 ent->owner = ibfd;
10589 s->size += 16;
0e1862bb 10590 if (bfd_link_pic (info))
7865406b
AM
10591 {
10592 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10593 srel->size += sizeof (Elf64_External_Rela);
10594 }
102890f0
AM
10595 }
10596 }
10597 else
7865406b 10598 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10599 }
10600
65f38f15
AM
10601 /* We now have determined the sizes of the various dynamic sections.
10602 Allocate memory for them. */
b34976b6 10603 relocs = FALSE;
65f38f15
AM
10604 for (s = dynobj->sections; s != NULL; s = s->next)
10605 {
10606 if ((s->flags & SEC_LINKER_CREATED) == 0)
10607 continue;
10608
4ce794b7 10609 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10610 /* These haven't been allocated yet; don't strip. */
10611 continue;
33e44f2e
AM
10612 else if (s == htab->elf.sgot
10613 || s == htab->elf.splt
10614 || s == htab->elf.iplt
2d7ad24e 10615 || s == htab->pltlocal
c456f082 10616 || s == htab->glink
9e390558 10617 || s == htab->global_entry
5474d94f
AM
10618 || s == htab->elf.sdynbss
10619 || s == htab->elf.sdynrelro)
65f38f15
AM
10620 {
10621 /* Strip this section if we don't need it; see the
10622 comment below. */
5bd4f169 10623 }
58d180e8
AM
10624 else if (s == htab->glink_eh_frame)
10625 {
10626 if (!bfd_is_abs_section (s->output_section))
10627 /* Not sized yet. */
10628 continue;
10629 }
70cc837d 10630 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10631 {
c456f082 10632 if (s->size != 0)
5bd4f169 10633 {
33e44f2e 10634 if (s != htab->elf.srelplt)
b34976b6 10635 relocs = TRUE;
5bd4f169
AM
10636
10637 /* We use the reloc_count field as a counter if we need
10638 to copy relocs into the output file. */
10639 s->reloc_count = 0;
10640 }
10641 }
65f38f15 10642 else
5bd4f169
AM
10643 {
10644 /* It's not one of our sections, so don't allocate space. */
10645 continue;
10646 }
10647
eea6121a 10648 if (s->size == 0)
5bd4f169 10649 {
c456f082
AM
10650 /* If we don't need this section, strip it from the
10651 output file. This is mostly to handle .rela.bss and
10652 .rela.plt. We must create both sections in
10653 create_dynamic_sections, because they must be created
10654 before the linker maps input sections to output
10655 sections. The linker does that before
10656 adjust_dynamic_symbol is called, and it is that
10657 function which decides whether anything needs to go
10658 into these sections. */
8423293d 10659 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10660 continue;
10661 }
10662
06bcf541
AM
10663 if (bfd_is_abs_section (s->output_section))
10664 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10665 s->name);
10666
c456f082 10667 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10668 continue;
10669
65f38f15
AM
10670 /* Allocate memory for the section contents. We use bfd_zalloc
10671 here in case unused entries are not reclaimed before the
10672 section's contents are written out. This should not happen,
411e1bfb
AM
10673 but this way if it does we get a R_PPC64_NONE reloc in .rela
10674 sections instead of garbage.
10675 We also rely on the section contents being zero when writing
5474d94f 10676 the GOT and .dynrelro. */
eea6121a 10677 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10678 if (s->contents == NULL)
b34976b6 10679 return FALSE;
5bd4f169
AM
10680 }
10681
c72f2fb2 10682 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10683 {
0c8d6e5c 10684 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10685 continue;
10686
e717da7e 10687 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10688 if (s != NULL && s != htab->elf.sgot)
e717da7e 10689 {
eea6121a 10690 if (s->size == 0)
8423293d 10691 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10692 else
10693 {
eea6121a 10694 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10695 if (s->contents == NULL)
10696 return FALSE;
10697 }
10698 }
10699 s = ppc64_elf_tdata (ibfd)->relgot;
10700 if (s != NULL)
10701 {
eea6121a 10702 if (s->size == 0)
8423293d 10703 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10704 else
10705 {
eea6121a 10706 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10707 if (s->contents == NULL)
10708 return FALSE;
10709 relocs = TRUE;
10710 s->reloc_count = 0;
10711 }
10712 }
10713 }
10714
e86ce104 10715 if (htab->elf.dynamic_sections_created)
5bd4f169 10716 {
e8910a83
AM
10717 bfd_boolean tls_opt;
10718
5bd4f169
AM
10719 /* Add some entries to the .dynamic section. We fill in the
10720 values later, in ppc64_elf_finish_dynamic_sections, but we
10721 must add the entries now so that we get the correct size for
10722 the .dynamic section. The DT_DEBUG entry is filled in by the
10723 dynamic linker and used by the debugger. */
dc810e39 10724#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10725 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10726
0e1862bb 10727 if (bfd_link_executable (info))
5bd4f169 10728 {
dc810e39 10729 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10730 return FALSE;
5bd4f169
AM
10731 }
10732
33e44f2e 10733 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10734 {
dc810e39
AM
10735 if (!add_dynamic_entry (DT_PLTGOT, 0)
10736 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10737 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10738 || !add_dynamic_entry (DT_JMPREL, 0)
10739 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10740 return FALSE;
5bd4f169
AM
10741 }
10742
ee67d69a 10743 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10744 {
10745 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10746 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10747 return FALSE;
19397422
AM
10748 }
10749
7c9cf415 10750 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10751 && htab->tls_get_addr_fd != NULL
10752 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10753 if (tls_opt || !htab->opd_abi)
10754 {
10755 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10756 return FALSE;
10757 }
a7f2871e 10758
5bd4f169
AM
10759 if (relocs)
10760 {
dc810e39
AM
10761 if (!add_dynamic_entry (DT_RELA, 0)
10762 || !add_dynamic_entry (DT_RELASZ, 0)
10763 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10764 return FALSE;
5bd4f169 10765
65f38f15
AM
10766 /* If any dynamic relocs apply to a read-only section,
10767 then we need a DT_TEXTREL entry. */
248866a8 10768 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10769 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10770
65f38f15 10771 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10772 {
65f38f15 10773 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10774 return FALSE;
5bd4f169 10775 }
5bd4f169 10776 }
5bd4f169 10777 }
65f38f15 10778#undef add_dynamic_entry
5bd4f169 10779
b34976b6 10780 return TRUE;
5bd4f169
AM
10781}
10782
a345bc8d
AM
10783/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10784
10785static bfd_boolean
10786ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10787{
10788 if (h->plt.plist != NULL
10789 && !h->def_regular
10790 && !h->pointer_equality_needed)
10791 return FALSE;
10792
10793 return _bfd_elf_hash_symbol (h);
10794}
10795
721956f4 10796/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10797
4ce794b7
AM
10798static inline enum ppc_stub_type
10799ppc_type_of_stub (asection *input_sec,
10800 const Elf_Internal_Rela *rel,
10801 struct ppc_link_hash_entry **hash,
e054468f 10802 struct plt_entry **plt_ent,
6911b7dc
AM
10803 bfd_vma destination,
10804 unsigned long local_off)
5bd4f169 10805{
721956f4
AM
10806 struct ppc_link_hash_entry *h = *hash;
10807 bfd_vma location;
10808 bfd_vma branch_offset;
10809 bfd_vma max_branch_offset;
4ce794b7 10810 enum elf_ppc64_reloc_type r_type;
5bd4f169 10811
721956f4
AM
10812 if (h != NULL)
10813 {
e054468f 10814 struct plt_entry *ent;
7fe2b9a6 10815 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10816 if (h->oh != NULL
10817 && h->oh->is_func_descriptor)
7b8f6675
AM
10818 {
10819 fdh = ppc_follow_link (h->oh);
10820 *hash = fdh;
10821 }
8387904d 10822
e054468f
AM
10823 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10824 if (ent->addend == rel->r_addend
10825 && ent->plt.offset != (bfd_vma) -1)
10826 {
e054468f
AM
10827 *plt_ent = ent;
10828 return ppc_stub_plt_call;
10829 }
5bd4f169 10830
7fe2b9a6
AM
10831 /* Here, we know we don't have a plt entry. If we don't have a
10832 either a defined function descriptor or a defined entry symbol
10833 in a regular object file, then it is pointless trying to make
10834 any other type of stub. */
854b41e7
AM
10835 if (!is_static_defined (&fdh->elf)
10836 && !is_static_defined (&h->elf))
721956f4 10837 return ppc_stub_none;
5d1634d7 10838 }
e054468f
AM
10839 else if (elf_local_got_ents (input_sec->owner) != NULL)
10840 {
10841 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10842 struct plt_entry **local_plt = (struct plt_entry **)
10843 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10844 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10845
10846 if (local_plt[r_symndx] != NULL)
10847 {
10848 struct plt_entry *ent;
10849
10850 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10851 if (ent->addend == rel->r_addend
10852 && ent->plt.offset != (bfd_vma) -1)
10853 {
10854 *plt_ent = ent;
10855 return ppc_stub_plt_call;
10856 }
10857 }
10858 }
5d1634d7 10859
721956f4
AM
10860 /* Determine where the call point is. */
10861 location = (input_sec->output_offset
10862 + input_sec->output_section->vma
10863 + rel->r_offset);
5d1634d7 10864
721956f4
AM
10865 branch_offset = destination - location;
10866 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10867
721956f4
AM
10868 /* Determine if a long branch stub is needed. */
10869 max_branch_offset = 1 << 25;
23cedd1d
AM
10870 if (r_type == R_PPC64_REL14
10871 || r_type == R_PPC64_REL14_BRTAKEN
10872 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10873 max_branch_offset = 1 << 15;
5d1634d7 10874
6911b7dc 10875 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10876 /* We need a stub. Figure out whether a long_branch or plt_branch
10877 is needed later. */
10878 return ppc_stub_long_branch;
5d1634d7 10879
721956f4 10880 return ppc_stub_none;
5d1634d7
AM
10881}
10882
05d0e962
AM
10883/* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
10884 loads r12 from r11+r12 (LOAD true).
10885 . lis %r12,xxx-1b@highest
10886 . ori %r12,xxx-1b@higher
10887 . sldi %r12,%r12,32
10888 . oris %r12,%r12,xxx-1b@hi
10889 . ori %r12,%r12,xxx-1b@l
10890 . add %r12,%r11,%r12 */
10891
10892static bfd_byte *
10893build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10894{
10895 if (off + 0x8000 < 0x10000)
10896 {
10897 if (load)
10898 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10899 else
10900 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10901 p += 4;
10902 }
10903 else if (off + 0x80008000ULL < 0x100000000ULL)
10904 {
10905 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10906 p += 4;
10907 if (load)
10908 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10909 else
10910 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10911 p += 4;
10912 }
10913 else
10914 {
10915 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10916 {
10917 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10918 p += 4;
10919 }
10920 else
10921 {
10922 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10923 p += 4;
10924 if (((off >> 32) & 0xffff) != 0)
10925 {
10926 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10927 p += 4;
10928 }
10929 }
10930 if (((off >> 32) & 0xffffffffULL) != 0)
10931 {
10932 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10933 p += 4;
10934 }
10935 if (PPC_HI (off) != 0)
10936 {
10937 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10938 p += 4;
10939 }
10940 if (PPC_LO (off) != 0)
10941 {
10942 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10943 p += 4;
10944 }
10945 if (load)
10946 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10947 else
10948 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10949 p += 4;
10950 }
10951 return p;
10952}
10953
10954static unsigned int
10955size_offset (bfd_vma off)
10956{
10957 unsigned int size;
10958 if (off + 0x8000 < 0x10000)
10959 size = 4;
10960 else if (off + 0x80008000ULL < 0x100000000ULL)
10961 size = 8;
10962 else
10963 {
10964 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10965 size = 4;
10966 else
10967 {
10968 size = 4;
10969 if (((off >> 32) & 0xffff) != 0)
10970 size += 4;
10971 }
10972 if (((off >> 32) & 0xffffffffULL) != 0)
10973 size += 4;
10974 if (PPC_HI (off) != 0)
10975 size += 4;
10976 if (PPC_LO (off) != 0)
10977 size += 4;
10978 size += 4;
10979 }
10980 return size;
10981}
10982
df136d64
AM
10983/* Emit .eh_frame opcode to advance pc by DELTA. */
10984
10985static bfd_byte *
10986eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10987{
10988 delta /= 4;
10989 if (delta < 64)
10990 *eh++ = DW_CFA_advance_loc + delta;
10991 else if (delta < 256)
10992 {
10993 *eh++ = DW_CFA_advance_loc1;
10994 *eh++ = delta;
10995 }
10996 else if (delta < 65536)
10997 {
10998 *eh++ = DW_CFA_advance_loc2;
10999 bfd_put_16 (abfd, delta, eh);
11000 eh += 2;
11001 }
11002 else
11003 {
11004 *eh++ = DW_CFA_advance_loc4;
11005 bfd_put_32 (abfd, delta, eh);
11006 eh += 4;
11007 }
11008 return eh;
11009}
11010
11011/* Size of required .eh_frame opcode to advance pc by DELTA. */
11012
11013static unsigned int
11014eh_advance_size (unsigned int delta)
11015{
11016 if (delta < 64 * 4)
11017 /* DW_CFA_advance_loc+[1..63]. */
11018 return 1;
11019 if (delta < 256 * 4)
11020 /* DW_CFA_advance_loc1, byte. */
11021 return 2;
11022 if (delta < 65536 * 4)
11023 /* DW_CFA_advance_loc2, 2 bytes. */
11024 return 3;
11025 /* DW_CFA_advance_loc4, 4 bytes. */
11026 return 5;
11027}
11028
794e51c0
AM
11029/* With power7 weakly ordered memory model, it is possible for ld.so
11030 to update a plt entry in one thread and have another thread see a
11031 stale zero toc entry. To avoid this we need some sort of acquire
11032 barrier in the call stub. One solution is to make the load of the
11033 toc word seem to appear to depend on the load of the function entry
11034 word. Another solution is to test for r2 being zero, and branch to
11035 the appropriate glink entry if so.
11036
11037 . fake dep barrier compare
71a39c98
AM
11038 . ld 12,xxx(2) ld 12,xxx(2)
11039 . mtctr 12 mtctr 12
11040 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
11041 . add 2,2,11 cmpldi 2,0
11042 . ld 2,xxx+8(2) bnectr+
11043 . bctr b <glink_entry>
11044
11045 The solution involving the compare turns out to be faster, so
11046 that's what we use unless the branch won't reach. */
11047
11048#define ALWAYS_USE_FAKE_DEP 0
11049#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 11050
794e51c0
AM
11051static inline unsigned int
11052plt_stub_size (struct ppc_link_hash_table *htab,
11053 struct ppc_stub_hash_entry *stub_entry,
11054 bfd_vma off)
11055{
05d0e962 11056 unsigned size;
b9e5796b 11057
05d0e962
AM
11058 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11059 {
11060 size = 24 + size_offset (off);
11061 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
11062 size += 4;
11063 return size;
11064 }
11065
11066 size = 12;
b9e5796b
AM
11067 if (ALWAYS_EMIT_R2SAVE
11068 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11069 size += 4;
11070 if (PPC_HA (off) != 0)
794e51c0 11071 size += 4;
b9e5796b
AM
11072 if (htab->opd_abi)
11073 {
11074 size += 4;
e7d1c40c 11075 if (htab->params->plt_static_chain)
b9e5796b 11076 size += 4;
bd4d2eaa
AM
11077 if (htab->params->plt_thread_safe
11078 && htab->elf.dynamic_sections_created
11079 && stub_entry->h != NULL
11080 && stub_entry->h->elf.dynindx != -1)
b9e5796b 11081 size += 8;
e7d1c40c 11082 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
11083 size += 4;
11084 }
794e51c0
AM
11085 if (stub_entry->h != NULL
11086 && (stub_entry->h == htab->tls_get_addr_fd
11087 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11088 && htab->params->tls_get_addr_opt)
f378ab09
AM
11089 {
11090 size += 7 * 4;
e81b4c93 11091 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 11092 size += 6 * 4;
f378ab09 11093 }
794e51c0
AM
11094 return size;
11095}
11096
2420fff6
AM
11097/* Depending on the sign of plt_stub_align:
11098 If positive, return the padding to align to a 2**plt_stub_align
11099 boundary.
11100 If negative, if this stub would cross fewer 2**plt_stub_align
11101 boundaries if we align, then return the padding needed to do so. */
11102
794e51c0
AM
11103static inline unsigned int
11104plt_stub_pad (struct ppc_link_hash_table *htab,
11105 struct ppc_stub_hash_entry *stub_entry,
11106 bfd_vma plt_off)
11107{
2420fff6 11108 int stub_align;
1aa42141 11109 unsigned stub_size;
6f20ed8a 11110 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 11111
2420fff6
AM
11112 if (htab->params->plt_stub_align >= 0)
11113 {
11114 stub_align = 1 << htab->params->plt_stub_align;
11115 if ((stub_off & (stub_align - 1)) != 0)
11116 return stub_align - (stub_off & (stub_align - 1));
11117 return 0;
11118 }
11119
11120 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 11121 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 11122 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 11123 > ((stub_size - 1) & -stub_align))
794e51c0
AM
11124 return stub_align - (stub_off & (stub_align - 1));
11125 return 0;
11126}
11127
11128/* Build a .plt call stub. */
11129
11130static inline bfd_byte *
11131build_plt_stub (struct ppc_link_hash_table *htab,
11132 struct ppc_stub_hash_entry *stub_entry,
11133 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11134{
e7d1c40c 11135 bfd *obfd = htab->params->stub_bfd;
b9e5796b 11136 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 11137 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
11138 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
11139 && htab->elf.dynamic_sections_created
11140 && stub_entry->h != NULL
11141 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
11142 bfd_boolean use_fake_dep = plt_thread_safe;
11143 bfd_vma cmp_branch_off = 0;
11144
11145 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11146 && plt_load_toc
794e51c0 11147 && plt_thread_safe
bd4d2eaa
AM
11148 && !((stub_entry->h == htab->tls_get_addr_fd
11149 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11150 && htab->params->tls_get_addr_opt))
794e51c0
AM
11151 {
11152 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11153 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11154 / PLT_ENTRY_SIZE (htab));
9e390558 11155 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11156 bfd_vma to, from;
11157
68d62958
AM
11158 if (pltindex > 32768)
11159 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11160 to = (glinkoff
11161 + htab->glink->output_offset
11162 + htab->glink->output_section->vma);
6f20ed8a 11163 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
11164 + 4 * (ALWAYS_EMIT_R2SAVE
11165 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11166 + 4 * (PPC_HA (offset) != 0)
11167 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11168 != PPC_HA (offset))
11169 + 4 * (plt_static_chain != 0)
11170 + 20
6f20ed8a
AM
11171 + stub_entry->group->stub_sec->output_offset
11172 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11173 cmp_branch_off = to - from;
11174 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11175 }
11176
ac2df442
AM
11177 if (PPC_HA (offset) != 0)
11178 {
176a0d42
AM
11179 if (r != NULL)
11180 {
794e51c0
AM
11181 if (ALWAYS_EMIT_R2SAVE
11182 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11183 r[0].r_offset += 4;
176a0d42 11184 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11185 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11186 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11187 r[1].r_addend = r[0].r_addend;
b9e5796b 11188 if (plt_load_toc)
176a0d42 11189 {
b9e5796b 11190 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11191 {
b9e5796b
AM
11192 r[2].r_offset = r[1].r_offset + 4;
11193 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11194 r[2].r_addend = r[0].r_addend;
11195 }
11196 else
11197 {
11198 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11199 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11200 r[2].r_addend = r[0].r_addend + 8;
11201 if (plt_static_chain)
11202 {
11203 r[3].r_offset = r[2].r_offset + 4;
11204 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11205 r[3].r_addend = r[0].r_addend + 16;
11206 }
c7131b65 11207 }
176a0d42
AM
11208 }
11209 }
794e51c0
AM
11210 if (ALWAYS_EMIT_R2SAVE
11211 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11212 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11213 if (plt_load_toc)
11214 {
11215 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11216 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11217 }
11218 else
11219 {
11220 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11221 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11222 }
b9e5796b
AM
11223 if (plt_load_toc
11224 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11225 {
71a39c98 11226 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11227 offset = 0;
11228 }
71a39c98 11229 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11230 if (plt_load_toc)
794e51c0 11231 {
b9e5796b
AM
11232 if (use_fake_dep)
11233 {
11234 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11235 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11236 }
11237 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11238 if (plt_static_chain)
11239 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11240 }
ac2df442
AM
11241 }
11242 else
11243 {
176a0d42
AM
11244 if (r != NULL)
11245 {
794e51c0
AM
11246 if (ALWAYS_EMIT_R2SAVE
11247 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11248 r[0].r_offset += 4;
176a0d42 11249 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11250 if (plt_load_toc)
176a0d42 11251 {
b9e5796b 11252 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11253 {
b9e5796b
AM
11254 r[1].r_offset = r[0].r_offset + 4;
11255 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11256 r[1].r_addend = r[0].r_addend;
11257 }
11258 else
11259 {
11260 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11261 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11262 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11263 if (plt_static_chain)
11264 {
11265 r[2].r_offset = r[1].r_offset + 4;
11266 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11267 r[2].r_addend = r[0].r_addend + 8;
11268 }
c7131b65 11269 }
176a0d42
AM
11270 }
11271 }
794e51c0
AM
11272 if (ALWAYS_EMIT_R2SAVE
11273 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11274 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11275 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11276 if (plt_load_toc
11277 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11278 {
11279 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11280 offset = 0;
11281 }
71a39c98 11282 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11283 if (plt_load_toc)
794e51c0 11284 {
b9e5796b
AM
11285 if (use_fake_dep)
11286 {
11287 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11288 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11289 }
11290 if (plt_static_chain)
11291 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11292 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11293 }
ac2df442 11294 }
b9e5796b 11295 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11296 {
11297 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11298 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11299 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11300 }
11301 else
407aa07c 11302 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11303 return p;
11304}
11305
a7f2871e
AM
11306/* Build a special .plt call stub for __tls_get_addr. */
11307
11308#define LD_R11_0R3 0xe9630000
11309#define LD_R12_0R3 0xe9830000
11310#define MR_R0_R3 0x7c601b78
11311#define CMPDI_R11_0 0x2c2b0000
11312#define ADD_R3_R12_R13 0x7c6c6a14
11313#define BEQLR 0x4d820020
11314#define MR_R3_R0 0x7c030378
a7f2871e
AM
11315#define STD_R11_0R1 0xf9610000
11316#define BCTRL 0x4e800421
11317#define LD_R11_0R1 0xe9610000
a7f2871e
AM
11318#define MTLR_R11 0x7d6803a6
11319
11320static inline bfd_byte *
794e51c0
AM
11321build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11322 struct ppc_stub_hash_entry *stub_entry,
11323 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 11324{
e7d1c40c 11325 bfd *obfd = htab->params->stub_bfd;
df136d64 11326 bfd_byte *loc = p;
794e51c0 11327
a7f2871e
AM
11328 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11329 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11330 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11331 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11332 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11333 bfd_put_32 (obfd, BEQLR, p), p += 4;
11334 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
11335 if (r != NULL)
11336 r[0].r_offset += 7 * 4;
e81b4c93 11337 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
11338 return build_plt_stub (htab, stub_entry, p, offset, r);
11339
a7f2871e 11340 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 11341 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11342
11343 if (r != NULL)
f378ab09 11344 r[0].r_offset += 2 * 4;
794e51c0 11345 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 11346 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 11347
a078d95a 11348 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 11349 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11350 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11351 bfd_put_32 (obfd, BLR, p), p += 4;
11352
df136d64
AM
11353 if (htab->glink_eh_frame != NULL
11354 && htab->glink_eh_frame->size != 0)
11355 {
11356 bfd_byte *base, *eh;
11357 unsigned int lr_used, delta;
11358
11359 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11360 eh = base + stub_entry->group->eh_size;
11361 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11362 delta = lr_used - stub_entry->group->lr_restore;
11363 stub_entry->group->lr_restore = lr_used + 16;
11364 eh = eh_advance (htab->elf.dynobj, eh, delta);
11365 *eh++ = DW_CFA_offset_extended_sf;
11366 *eh++ = 65;
11367 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11368 *eh++ = DW_CFA_advance_loc + 4;
11369 *eh++ = DW_CFA_restore_extended;
11370 *eh++ = 65;
11371 stub_entry->group->eh_size = eh - base;
11372 }
a7f2871e
AM
11373 return p;
11374}
11375
176a0d42
AM
11376static Elf_Internal_Rela *
11377get_relocs (asection *sec, int count)
11378{
11379 Elf_Internal_Rela *relocs;
11380 struct bfd_elf_section_data *elfsec_data;
11381
11382 elfsec_data = elf_section_data (sec);
11383 relocs = elfsec_data->relocs;
11384 if (relocs == NULL)
11385 {
11386 bfd_size_type relsize;
11387 relsize = sec->reloc_count * sizeof (*relocs);
11388 relocs = bfd_alloc (sec->owner, relsize);
11389 if (relocs == NULL)
11390 return NULL;
11391 elfsec_data->relocs = relocs;
d4730f92
BS
11392 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11393 sizeof (Elf_Internal_Shdr));
11394 if (elfsec_data->rela.hdr == NULL)
11395 return NULL;
11396 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11397 * sizeof (Elf64_External_Rela));
11398 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11399 sec->reloc_count = 0;
11400 }
11401 relocs += sec->reloc_count;
11402 sec->reloc_count += count;
11403 return relocs;
11404}
11405
aa374f67 11406static bfd_vma
25f53a85 11407get_r2off (struct bfd_link_info *info,
aa374f67
AM
11408 struct ppc_stub_hash_entry *stub_entry)
11409{
25f53a85 11410 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11411 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11412
11413 if (r2off == 0)
11414 {
11415 /* Support linking -R objects. Get the toc pointer from the
11416 opd entry. */
11417 char buf[8];
b9e5796b
AM
11418 if (!htab->opd_abi)
11419 return r2off;
aa374f67
AM
11420 asection *opd = stub_entry->h->elf.root.u.def.section;
11421 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11422
11423 if (strcmp (opd->name, ".opd") != 0
11424 || opd->reloc_count != 0)
11425 {
2cdcc330
AM
11426 info->callbacks->einfo
11427 (_("%P: cannot find opd entry toc for `%pT'\n"),
11428 stub_entry->h->elf.root.root.string);
aa374f67 11429 bfd_set_error (bfd_error_bad_value);
a7c49797 11430 return (bfd_vma) -1;
aa374f67
AM
11431 }
11432 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11433 return (bfd_vma) -1;
aa374f67 11434 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11435 r2off -= elf_gp (info->output_bfd);
aa374f67 11436 }
6f20ed8a 11437 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11438 return r2off;
11439}
11440
b34976b6 11441static bfd_boolean
4ce794b7 11442ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11443{
721956f4
AM
11444 struct ppc_stub_hash_entry *stub_entry;
11445 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11446 struct bfd_link_info *info;
11447 struct ppc_link_hash_table *htab;
721956f4
AM
11448 bfd_byte *loc;
11449 bfd_byte *p;
1aa42141 11450 bfd_vma targ, off;
176a0d42 11451 Elf_Internal_Rela *r;
e054468f 11452 asection *plt;
5d1634d7 11453
721956f4
AM
11454 /* Massage our args to the form they really have. */
11455 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11456 info = in_arg;
5d1634d7 11457
5d1634d7 11458 htab = ppc_hash_table (info);
4dfe6ac6
NC
11459 if (htab == NULL)
11460 return FALSE;
5d1634d7 11461
1aa42141 11462 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11463 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11464
4ce794b7 11465 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11466 switch (stub_entry->stub_type)
5d1634d7 11467 {
721956f4 11468 case ppc_stub_long_branch:
ad8e1ba5 11469 case ppc_stub_long_branch_r2off:
721956f4 11470 /* Branches are relative. This is where we are going to. */
1aa42141 11471 targ = (stub_entry->target_value
6911b7dc
AM
11472 + stub_entry->target_section->output_offset
11473 + stub_entry->target_section->output_section->vma);
1aa42141 11474 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11475
721956f4 11476 /* And this is where we are coming from. */
1aa42141
AM
11477 off = (stub_entry->stub_offset
11478 + stub_entry->group->stub_sec->output_offset
11479 + stub_entry->group->stub_sec->output_section->vma);
11480 off = targ - off;
e86ce104 11481
9e390558 11482 p = loc;
ac2df442 11483 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11484 {
25f53a85 11485 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11486
a7c49797 11487 if (r2off == (bfd_vma) -1)
aa374f67
AM
11488 {
11489 htab->stub_error = TRUE;
11490 return FALSE;
11491 }
9e390558
AM
11492 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11493 p += 4;
ac2df442
AM
11494 if (PPC_HA (r2off) != 0)
11495 {
e7d1c40c 11496 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11497 ADDIS_R2_R2 | PPC_HA (r2off), p);
11498 p += 4;
a7c49797
AM
11499 }
11500 if (PPC_LO (r2off) != 0)
11501 {
11502 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11503 ADDI_R2_R2 | PPC_LO (r2off), p);
11504 p += 4;
ac2df442 11505 }
9e390558 11506 off -= p - loc;
ad8e1ba5 11507 }
9e390558
AM
11508 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11509 p += 4;
ad8e1ba5 11510
5c3dead3
AM
11511 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11512 {
cf97bcb0
AM
11513 _bfd_error_handler
11514 (_("long branch stub `%s' offset overflow"),
bc30df16 11515 stub_entry->root.string);
5c3dead3
AM
11516 htab->stub_error = TRUE;
11517 return FALSE;
11518 }
ee75fd95
AM
11519
11520 if (info->emitrelocations)
11521 {
6f20ed8a 11522 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11523 if (r == NULL)
11524 return FALSE;
9e390558 11525 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11526 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11527 r->r_addend = targ;
ee75fd95
AM
11528 if (stub_entry->h != NULL)
11529 {
11530 struct elf_link_hash_entry **hashes;
11531 unsigned long symndx;
11532 struct ppc_link_hash_entry *h;
11533
e7d1c40c 11534 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
11535 if (hashes == NULL)
11536 {
11537 bfd_size_type hsize;
11538
11539 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 11540 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
11541 if (hashes == NULL)
11542 return FALSE;
e7d1c40c 11543 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
11544 htab->stub_globals = 1;
11545 }
11546 symndx = htab->stub_globals++;
11547 h = stub_entry->h;
11548 hashes[symndx] = &h->elf;
11549 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11550 if (h->oh != NULL && h->oh->is_func)
b31867b6 11551 h = ppc_follow_link (h->oh);
ee75fd95
AM
11552 if (h->elf.root.u.def.section != stub_entry->target_section)
11553 /* H is an opd symbol. The addend must be zero. */
11554 r->r_addend = 0;
11555 else
11556 {
11557 off = (h->elf.root.u.def.value
11558 + h->elf.root.u.def.section->output_offset
11559 + h->elf.root.u.def.section->output_section->vma);
11560 r->r_addend -= off;
11561 }
11562 }
11563 }
721956f4 11564 break;
e86ce104 11565
721956f4 11566 case ppc_stub_plt_branch:
ad8e1ba5 11567 case ppc_stub_plt_branch_r2off:
721956f4
AM
11568 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11569 stub_entry->root.string + 9,
b34976b6 11570 FALSE, FALSE);
721956f4
AM
11571 if (br_entry == NULL)
11572 {
cf97bcb0
AM
11573 _bfd_error_handler (_("can't find branch stub `%s'"),
11574 stub_entry->root.string);
b34976b6
AM
11575 htab->stub_error = TRUE;
11576 return FALSE;
721956f4
AM
11577 }
11578
1aa42141 11579 targ = (stub_entry->target_value
176a0d42
AM
11580 + stub_entry->target_section->output_offset
11581 + stub_entry->target_section->output_section->vma);
6911b7dc 11582 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11583 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11584
1aa42141 11585 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11586 htab->brlt->contents + br_entry->offset);
721956f4 11587
f94498ff 11588 if (br_entry->iter == htab->stub_iteration)
721956f4 11589 {
f94498ff 11590 br_entry->iter = 0;
84f5d08e 11591
f94498ff 11592 if (htab->relbrlt != NULL)
84f5d08e 11593 {
f94498ff
AM
11594 /* Create a reloc for the branch lookup table entry. */
11595 Elf_Internal_Rela rela;
11596 bfd_byte *rl;
11597
11598 rela.r_offset = (br_entry->offset
11599 + htab->brlt->output_offset
11600 + htab->brlt->output_section->vma);
11601 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11602 rela.r_addend = targ;
f94498ff
AM
11603
11604 rl = htab->relbrlt->contents;
11605 rl += (htab->relbrlt->reloc_count++
11606 * sizeof (Elf64_External_Rela));
11607 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11608 }
11609 else if (info->emitrelocations)
11610 {
176a0d42
AM
11611 r = get_relocs (htab->brlt, 1);
11612 if (r == NULL)
11613 return FALSE;
11614 /* brlt, being SEC_LINKER_CREATED does not go through the
11615 normal reloc processing. Symbols and offsets are not
11616 translated from input file to output file form, so
11617 set up the offset per the output file. */
f94498ff
AM
11618 r->r_offset = (br_entry->offset
11619 + htab->brlt->output_offset
11620 + htab->brlt->output_section->vma);
11621 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11622 r->r_addend = targ;
84f5d08e 11623 }
84f5d08e 11624 }
721956f4 11625
1aa42141 11626 targ = (br_entry->offset
176a0d42
AM
11627 + htab->brlt->output_offset
11628 + htab->brlt->output_section->vma);
11629
1aa42141
AM
11630 off = (elf_gp (info->output_bfd)
11631 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11632 off = targ - off;
721956f4 11633
ad8e1ba5 11634 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11635 {
25f53a85 11636 info->callbacks->einfo
c1c8c1ef 11637 (_("%P: linkage table error against `%pT'\n"),
721956f4 11638 stub_entry->root.string);
5d1634d7 11639 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11640 htab->stub_error = TRUE;
11641 return FALSE;
5d1634d7 11642 }
41bd81ab 11643
176a0d42
AM
11644 if (info->emitrelocations)
11645 {
6f20ed8a 11646 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11647 if (r == NULL)
11648 return FALSE;
6f20ed8a 11649 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11650 if (bfd_big_endian (info->output_bfd))
11651 r[0].r_offset += 2;
00f412ee 11652 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11653 r[0].r_offset += 4;
11654 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11655 r[0].r_addend = targ;
176a0d42
AM
11656 if (PPC_HA (off) != 0)
11657 {
11658 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11659 r[1].r_offset = r[0].r_offset + 4;
11660 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11661 r[1].r_addend = r[0].r_addend;
11662 }
11663 }
11664
9e390558 11665 p = loc;
00f412ee 11666 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11667 {
176a0d42 11668 if (PPC_HA (off) != 0)
ac2df442 11669 {
e7d1c40c 11670 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11671 ADDIS_R12_R2 | PPC_HA (off), p);
11672 p += 4;
e7d1c40c 11673 bfd_put_32 (htab->params->stub_bfd,
9e390558 11674 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11675 }
11676 else
9e390558
AM
11677 bfd_put_32 (htab->params->stub_bfd,
11678 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11679 }
11680 else
11681 {
25f53a85 11682 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11683
a7c49797 11684 if (r2off == (bfd_vma) -1)
aa374f67
AM
11685 {
11686 htab->stub_error = TRUE;
11687 return FALSE;
11688 }
ad8e1ba5 11689
9e390558
AM
11690 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11691 p += 4;
176a0d42 11692 if (PPC_HA (off) != 0)
ac2df442 11693 {
e7d1c40c 11694 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11695 ADDIS_R12_R2 | PPC_HA (off), p);
11696 p += 4;
e7d1c40c 11697 bfd_put_32 (htab->params->stub_bfd,
9e390558 11698 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11699 }
11700 else
9e390558 11701 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11702
11703 if (PPC_HA (r2off) != 0)
11704 {
9e390558 11705 p += 4;
e7d1c40c 11706 bfd_put_32 (htab->params->stub_bfd,
9e390558 11707 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11708 }
11709 if (PPC_LO (r2off) != 0)
11710 {
9e390558 11711 p += 4;
e7d1c40c 11712 bfd_put_32 (htab->params->stub_bfd,
9e390558 11713 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11714 }
ad8e1ba5 11715 }
9e390558
AM
11716 p += 4;
11717 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11718 p += 4;
407aa07c
AM
11719 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11720 p += 4;
721956f4 11721 break;
5d1634d7 11722
05d0e962
AM
11723 case ppc_stub_long_branch_notoc:
11724 case ppc_stub_long_branch_both:
11725 case ppc_stub_plt_branch_notoc:
11726 case ppc_stub_plt_branch_both:
11727 case ppc_stub_plt_call_notoc:
11728 case ppc_stub_plt_call_both:
11729 p = loc;
11730 off = (8 + stub_entry->stub_offset
11731 + stub_entry->group->stub_sec->output_offset
11732 + stub_entry->group->stub_sec->output_section->vma);
11733 if (stub_entry->stub_type == ppc_stub_long_branch_both
11734 || stub_entry->stub_type == ppc_stub_plt_branch_both
11735 || stub_entry->stub_type == ppc_stub_plt_call_both)
11736 {
11737 off += 4;
11738 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11739 p += 4;
11740 }
11741 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11742 {
11743 targ = stub_entry->plt_ent->plt.offset & ~1;
11744 if (targ >= (bfd_vma) -2)
11745 abort ();
11746
11747 plt = htab->elf.splt;
11748 if (!htab->elf.dynamic_sections_created
11749 || stub_entry->h == NULL
11750 || stub_entry->h->elf.dynindx == -1)
11751 {
11752 if (stub_entry->symtype == STT_GNU_IFUNC)
11753 plt = htab->elf.iplt;
11754 else
11755 plt = htab->pltlocal;
11756 }
11757 targ += plt->output_offset + plt->output_section->vma;
11758 }
11759 else
11760 targ = (stub_entry->target_value
11761 + stub_entry->target_section->output_offset
11762 + stub_entry->target_section->output_section->vma);
11763 off = targ - off;
11764 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
11765 p += 4;
11766 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
11767 p += 4;
11768 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
11769 p += 4;
11770 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
11771 p += 4;
11772 p = build_offset (htab->params->stub_bfd, p, off,
11773 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11774 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
11775 {
11776 off += 8;
11777 bfd_put_32 (htab->params->stub_bfd,
11778 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11779 }
11780 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
11781 {
11782 off += 12;
11783 bfd_put_32 (htab->params->stub_bfd,
11784 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11785 }
11786 else
11787 {
11788 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11789 p += 4;
11790 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11791 }
11792 p += 4;
df136d64
AM
11793
11794 if (htab->glink_eh_frame != NULL
11795 && htab->glink_eh_frame->size != 0)
11796 {
11797 bfd_byte *base, *eh;
11798 unsigned int lr_used, delta;
11799
11800 base = (htab->glink_eh_frame->contents
11801 + stub_entry->group->eh_base + 17);
11802 eh = base + stub_entry->group->eh_size;
11803 lr_used = stub_entry->stub_offset + 8;
11804 if (stub_entry->stub_type == ppc_stub_long_branch_both
11805 || stub_entry->stub_type == ppc_stub_plt_branch_both
11806 || stub_entry->stub_type == ppc_stub_plt_call_both)
11807 lr_used += 4;
11808 delta = lr_used - stub_entry->group->lr_restore;
11809 stub_entry->group->lr_restore = lr_used + 8;
11810 eh = eh_advance (htab->elf.dynobj, eh, delta);
11811 *eh++ = DW_CFA_register;
11812 *eh++ = 65;
11813 *eh++ = 12;
11814 *eh++ = DW_CFA_advance_loc + 2;
11815 *eh++ = DW_CFA_restore_extended;
11816 *eh++ = 65;
11817 stub_entry->group->eh_size = eh - base;
11818 }
05d0e962
AM
11819 break;
11820
721956f4 11821 case ppc_stub_plt_call:
794e51c0 11822 case ppc_stub_plt_call_r2save:
e054468f 11823 if (stub_entry->h != NULL
b31867b6
AM
11824 && stub_entry->h->is_func_descriptor
11825 && stub_entry->h->oh != NULL)
c862ae31 11826 {
b31867b6
AM
11827 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11828
11829 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11830 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11831 if (fh->elf.root.type == bfd_link_hash_undefined
11832 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11833 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11834 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11835 }
11836
721956f4 11837 /* Now build the stub. */
1aa42141
AM
11838 targ = stub_entry->plt_ent->plt.offset & ~1;
11839 if (targ >= (bfd_vma) -2)
721956f4
AM
11840 abort ();
11841
33e44f2e 11842 plt = htab->elf.splt;
25f23106
AM
11843 if (!htab->elf.dynamic_sections_created
11844 || stub_entry->h == NULL
11845 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11846 {
11847 if (stub_entry->symtype == STT_GNU_IFUNC)
11848 plt = htab->elf.iplt;
11849 else
11850 plt = htab->pltlocal;
11851 }
1aa42141 11852 targ += plt->output_offset + plt->output_section->vma;
e054468f 11853
1aa42141
AM
11854 off = (elf_gp (info->output_bfd)
11855 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11856 off = targ - off;
721956f4 11857
ad8e1ba5 11858 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11859 {
25f53a85 11860 info->callbacks->einfo
695344c0 11861 /* xgettext:c-format */
c1c8c1ef 11862 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11863 stub_entry->h != NULL
11864 ? stub_entry->h->elf.root.root.string
11865 : "<local sym>");
721956f4 11866 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11867 htab->stub_error = TRUE;
11868 return FALSE;
721956f4
AM
11869 }
11870
176a0d42
AM
11871 r = NULL;
11872 if (info->emitrelocations)
11873 {
6f20ed8a 11874 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11875 ((PPC_HA (off) != 0)
11876 + (htab->opd_abi
e7d1c40c 11877 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11878 && PPC_HA (off + 16) == PPC_HA (off))
11879 : 1)));
176a0d42
AM
11880 if (r == NULL)
11881 return FALSE;
6f20ed8a 11882 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11883 if (bfd_big_endian (info->output_bfd))
11884 r[0].r_offset += 2;
1aa42141 11885 r[0].r_addend = targ;
176a0d42 11886 }
a7f2871e
AM
11887 if (stub_entry->h != NULL
11888 && (stub_entry->h == htab->tls_get_addr_fd
11889 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11890 && htab->params->tls_get_addr_opt)
794e51c0 11891 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11892 else
794e51c0 11893 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11894 break;
11895
a4b6fadd
AM
11896 case ppc_stub_save_res:
11897 return TRUE;
11898
721956f4
AM
11899 default:
11900 BFD_FAIL ();
b34976b6 11901 return FALSE;
721956f4
AM
11902 }
11903
1aa42141 11904 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11905
e7d1c40c 11906 if (htab->params->emit_stub_syms)
97b639ba
AM
11907 {
11908 struct elf_link_hash_entry *h;
ee75fd95
AM
11909 size_t len1, len2;
11910 char *name;
11911 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11912 "long_branch",
11913 "long_branch",
3f6ff479
AM
11914 "long_branch",
11915 "plt_branch",
ee75fd95 11916 "plt_branch",
05d0e962
AM
11917 "plt_branch",
11918 "plt_branch",
11919 "plt_call",
11920 "plt_call",
794e51c0 11921 "plt_call",
ee75fd95
AM
11922 "plt_call" };
11923
11924 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11925 len2 = strlen (stub_entry->root.string);
11926 name = bfd_malloc (len1 + len2 + 2);
11927 if (name == NULL)
11928 return FALSE;
11929 memcpy (name, stub_entry->root.string, 9);
11930 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11931 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11932 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11933 if (h == NULL)
11934 return FALSE;
11935 if (h->root.type == bfd_link_hash_new)
11936 {
11937 h->root.type = bfd_link_hash_defined;
6f20ed8a 11938 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11939 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11940 h->ref_regular = 1;
11941 h->def_regular = 1;
11942 h->ref_regular_nonweak = 1;
11943 h->forced_local = 1;
11944 h->non_elf = 0;
2ec55de3 11945 h->root.linker_def = 1;
97b639ba
AM
11946 }
11947 }
11948
b34976b6 11949 return TRUE;
721956f4
AM
11950}
11951
11952/* As above, but don't actually build the stub. Just bump offset so
11953 we know stub section sizes, and select plt_branch stubs where
11954 long_branch stubs won't do. */
11955
b34976b6 11956static bfd_boolean
4ce794b7 11957ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11958{
11959 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11960 struct bfd_link_info *info;
721956f4 11961 struct ppc_link_hash_table *htab;
1aa42141 11962 bfd_vma targ, off;
721956f4
AM
11963 int size;
11964
11965 /* Massage our args to the form they really have. */
11966 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11967 info = in_arg;
11968
11969 htab = ppc_hash_table (info);
4dfe6ac6
NC
11970 if (htab == NULL)
11971 return FALSE;
721956f4 11972
1aa42141
AM
11973 /* Make a note of the offset within the stubs for this entry. */
11974 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11975
a4b6fadd
AM
11976 if (stub_entry->h != NULL
11977 && stub_entry->h->save_res
11978 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11979 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11980 {
11981 /* Don't make stubs to out-of-line register save/restore
11982 functions. Instead, emit copies of the functions. */
11983 stub_entry->group->needs_save_res = 1;
11984 stub_entry->stub_type = ppc_stub_save_res;
11985 return TRUE;
11986 }
11987
05d0e962
AM
11988 if (stub_entry->stub_type >= ppc_stub_plt_call
11989 && stub_entry->stub_type <= ppc_stub_plt_call_both)
721956f4 11990 {
e054468f 11991 asection *plt;
1aa42141
AM
11992 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11993 if (targ >= (bfd_vma) -2)
411e1bfb 11994 abort ();
33e44f2e 11995 plt = htab->elf.splt;
25f23106
AM
11996 if (!htab->elf.dynamic_sections_created
11997 || stub_entry->h == NULL
11998 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11999 {
12000 if (stub_entry->symtype == STT_GNU_IFUNC)
12001 plt = htab->elf.iplt;
12002 else
12003 plt = htab->pltlocal;
12004 }
1aa42141
AM
12005 targ += plt->output_offset + plt->output_section->vma;
12006
05d0e962
AM
12007 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12008 {
12009 off = (8 + stub_entry->stub_offset
12010 + stub_entry->group->stub_sec->output_offset
12011 + stub_entry->group->stub_sec->output_section->vma);
12012 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
12013 off += 4;
12014 }
12015 else
12016 off = (elf_gp (info->output_bfd)
12017 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
1aa42141
AM
12018
12019 if (htab->params->plt_stub_align != 0)
12020 {
05d0e962 12021 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
1aa42141
AM
12022
12023 stub_entry->group->stub_sec->size += pad;
12024 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
05d0e962
AM
12025 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12026 off += pad;
1aa42141 12027 }
721956f4 12028
05d0e962 12029 off = targ - off;
794e51c0 12030 size = plt_stub_size (htab, stub_entry, off);
1aa42141 12031
df136d64
AM
12032 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12033 {
12034 /* After the bcl, lr has been modified so we need to emit
12035 .eh_frame info saying the return address is in r12. */
12036 unsigned int lr_used = stub_entry->stub_offset + 8;
12037 unsigned int delta;
12038 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
12039 lr_used += 4;
12040 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12041 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12042 DW_CFA_restore_extended 65. */
12043 delta = lr_used - stub_entry->group->lr_restore;
12044 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12045 stub_entry->group->lr_restore = lr_used + 8;
12046 }
12047 else
176a0d42 12048 {
05d0e962
AM
12049 if (stub_entry->h != NULL
12050 && (stub_entry->h == htab->tls_get_addr_fd
12051 || stub_entry->h == htab->tls_get_addr)
12052 && htab->params->tls_get_addr_opt
e81b4c93 12053 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
df136d64
AM
12054 {
12055 /* After the bctrl, lr has been modified so we need to
12056 emit .eh_frame info saying the return address is
12057 on the stack. In fact we put the EH info specifying
12058 that the return address is on the stack *at* the
12059 call rather than after it, because the EH info for a
12060 call needs to be specified by that point.
12061 See libgcc/unwind-dw2.c execute_cfa_program. */
12062 unsigned int lr_used = stub_entry->stub_offset + size - 20;
12063 unsigned int delta;
12064 /* The eh_frame info will consist of a DW_CFA_advance_loc
12065 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12066 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12067 delta = lr_used - stub_entry->group->lr_restore;
12068 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12069 stub_entry->group->lr_restore = size - 4;
12070 }
05d0e962
AM
12071
12072 if (info->emitrelocations)
12073 {
12074 stub_entry->group->stub_sec->reloc_count
12075 += ((PPC_HA (off) != 0)
12076 + (htab->opd_abi
12077 ? 2 + (htab->params->plt_static_chain
12078 && PPC_HA (off + 16) == PPC_HA (off))
12079 : 1));
12080 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12081 }
176a0d42 12082 }
721956f4
AM
12083 }
12084 else
12085 {
ad8e1ba5
AM
12086 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
12087 variants. */
ac2df442 12088 bfd_vma r2off = 0;
6911b7dc 12089 bfd_vma local_off = 0;
ac2df442 12090
1aa42141
AM
12091 targ = (stub_entry->target_value
12092 + stub_entry->target_section->output_offset
12093 + stub_entry->target_section->output_section->vma);
12094 off = (stub_entry->stub_offset
12095 + stub_entry->group->stub_sec->output_offset
12096 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12097
ad8e1ba5
AM
12098 /* Reset the stub type from the plt variant in case we now
12099 can reach with a shorter stub. */
12100 if (stub_entry->stub_type >= ppc_stub_plt_branch)
12101 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12102
12103 size = 4;
12104 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12105 {
25f53a85 12106 r2off = get_r2off (info, stub_entry);
a7c49797 12107 if (r2off == (bfd_vma) -1)
aa374f67
AM
12108 {
12109 htab->stub_error = TRUE;
12110 return FALSE;
12111 }
a7c49797 12112 size = 8;
ac2df442 12113 if (PPC_HA (r2off) != 0)
a7c49797
AM
12114 size += 4;
12115 if (PPC_LO (r2off) != 0)
12116 size += 4;
1aa42141 12117 off += size - 4;
ad8e1ba5 12118 }
05d0e962
AM
12119 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
12120 {
12121 size = 20 + size_offset (targ - (off + 8));
12122 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
12123 size += 4;
12124 off += size - 4;
12125 }
1aa42141 12126 off = targ - off;
ad8e1ba5 12127
05d0e962 12128 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
721956f4 12129 {
df136d64
AM
12130 /* After the bcl, lr has been modified so we need to emit
12131 .eh_frame info saying the return address is in r12. */
12132 unsigned int lr_used = stub_entry->stub_offset + 8;
12133 unsigned int delta;
12134 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
12135 lr_used += 4;
12136 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12137 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12138 DW_CFA_restore_extended 65. */
12139 delta = lr_used - stub_entry->group->lr_restore;
12140 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12141 stub_entry->group->lr_restore = lr_used + 8;
12142
05d0e962 12143 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12144 {
05d0e962
AM
12145 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12146 - ppc_stub_long_branch_notoc);
12147 size += 4;
721956f4 12148 }
05d0e962
AM
12149 }
12150 else
12151 {
12152 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 12153
05d0e962
AM
12154 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12155 Do the same for -R objects without function descriptors. */
12156 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12157 && r2off == 0
12158 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12159 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
721956f4 12160 {
05d0e962 12161 struct ppc_branch_hash_entry *br_entry;
63bc6f6c 12162
05d0e962
AM
12163 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12164 stub_entry->root.string + 9,
12165 TRUE, FALSE);
12166 if (br_entry == NULL)
84f5d08e 12167 {
05d0e962
AM
12168 _bfd_error_handler (_("can't build branch stub `%s'"),
12169 stub_entry->root.string);
12170 htab->stub_error = TRUE;
12171 return FALSE;
84f5d08e 12172 }
ad8e1ba5 12173
05d0e962
AM
12174 if (br_entry->iter != htab->stub_iteration)
12175 {
12176 br_entry->iter = htab->stub_iteration;
12177 br_entry->offset = htab->brlt->size;
12178 htab->brlt->size += 8;
12179
12180 if (htab->relbrlt != NULL)
12181 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12182 else if (info->emitrelocations)
12183 {
12184 htab->brlt->reloc_count += 1;
12185 htab->brlt->flags |= SEC_RELOC;
12186 }
12187 }
ac2df442 12188
05d0e962
AM
12189 targ = (br_entry->offset
12190 + htab->brlt->output_offset
12191 + htab->brlt->output_section->vma);
12192 off = (elf_gp (info->output_bfd)
12193 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12194 off = targ - off;
176a0d42 12195
05d0e962
AM
12196 if (info->emitrelocations)
12197 {
12198 stub_entry->group->stub_sec->reloc_count
12199 += 1 + (PPC_HA (off) != 0);
12200 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12201 }
12202
12203 stub_entry->stub_type
12204 += ppc_stub_plt_branch - ppc_stub_long_branch;
12205 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12206 {
12207 size = 12;
12208 if (PPC_HA (off) != 0)
12209 size = 16;
12210 }
12211 else
12212 {
12213 size = 16;
12214 if (PPC_HA (off) != 0)
12215 size += 4;
12216
12217 if (PPC_HA (r2off) != 0)
12218 size += 4;
12219 if (PPC_LO (r2off) != 0)
12220 size += 4;
12221 }
ac2df442 12222 }
05d0e962 12223 else if (info->emitrelocations)
ac2df442 12224 {
05d0e962
AM
12225 stub_entry->group->stub_sec->reloc_count += 1;
12226 stub_entry->group->stub_sec->flags |= SEC_RELOC;
ac2df442 12227 }
721956f4
AM
12228 }
12229 }
12230
6f20ed8a 12231 stub_entry->group->stub_sec->size += size;
b34976b6 12232 return TRUE;
721956f4
AM
12233}
12234
12235/* Set up various things so that we can make a list of input sections
12236 for each output section included in the link. Returns -1 on error,
cedb70c5 12237 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12238
12239int
e7d1c40c 12240ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12241{
6f20ed8a 12242 unsigned int id;
721956f4
AM
12243 bfd_size_type amt;
12244 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12245
4dfe6ac6
NC
12246 if (htab == NULL)
12247 return -1;
4c52953f 12248
7cf7fcc8 12249 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12250 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12251 htab->sec_info = bfd_zmalloc (amt);
12252 if (htab->sec_info == NULL)
721956f4
AM
12253 return -1;
12254
3d6f9012
AM
12255 /* Set toc_off for com, und, abs and ind sections. */
12256 for (id = 0; id < 3; id++)
6f20ed8a 12257 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12258
721956f4
AM
12259 return 1;
12260}
12261
927be08e
AM
12262/* Set up for first pass at multitoc partitioning. */
12263
12264void
12265ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12266{
12267 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12268
1c865ab2 12269 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12270 htab->toc_bfd = NULL;
12271 htab->toc_first_sec = NULL;
12272}
12273
e717da7e
AM
12274/* The linker repeatedly calls this function for each TOC input section
12275 and linker generated GOT section. Group input bfds such that the toc
927be08e 12276 within a group is less than 64k in size. */
ad8e1ba5 12277
927be08e 12278bfd_boolean
4ce794b7 12279ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12280{
12281 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12282 bfd_vma addr, off, limit;
ad8e1ba5 12283
4dfe6ac6
NC
12284 if (htab == NULL)
12285 return FALSE;
12286
927be08e 12287 if (!htab->second_toc_pass)
4c52953f 12288 {
927be08e 12289 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
12290 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12291
12292 if (new_bfd)
bf102f86
AM
12293 {
12294 htab->toc_bfd = isec->owner;
12295 htab->toc_first_sec = isec;
12296 }
927be08e 12297
bf102f86
AM
12298 addr = isec->output_offset + isec->output_section->vma;
12299 off = addr - htab->toc_curr;
d77c8a4b
AM
12300 limit = 0x80008000;
12301 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12302 limit = 0x10000;
12303 if (off + isec->size > limit)
bf102f86
AM
12304 {
12305 addr = (htab->toc_first_sec->output_offset
12306 + htab->toc_first_sec->output_section->vma);
12307 htab->toc_curr = addr;
a27e685f 12308 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12309 }
99877b66 12310
927be08e
AM
12311 /* toc_curr is the base address of this toc group. Set elf_gp
12312 for the input section to be the offset relative to the
12313 output toc base plus 0x8000. Making the input elf_gp an
12314 offset allows us to move the toc as a whole without
12315 recalculating input elf_gp. */
06bcf541 12316 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12317 off += TOC_BASE_OFF;
12318
12319 /* Die if someone uses a linker script that doesn't keep input
12320 file .toc and .got together. */
a4fd3de5
AM
12321 if (new_bfd
12322 && elf_gp (isec->owner) != 0
927be08e
AM
12323 && elf_gp (isec->owner) != off)
12324 return FALSE;
12325
12326 elf_gp (isec->owner) = off;
12327 return TRUE;
4c52953f 12328 }
927be08e
AM
12329
12330 /* During the second pass toc_first_sec points to the start of
12331 a toc group, and toc_curr is used to track the old elf_gp.
12332 We use toc_bfd to ensure we only look at each bfd once. */
12333 if (htab->toc_bfd == isec->owner)
12334 return TRUE;
12335 htab->toc_bfd = isec->owner;
12336
12337 if (htab->toc_first_sec == NULL
12338 || htab->toc_curr != elf_gp (isec->owner))
12339 {
12340 htab->toc_curr = elf_gp (isec->owner);
12341 htab->toc_first_sec = isec;
12342 }
12343 addr = (htab->toc_first_sec->output_offset
12344 + htab->toc_first_sec->output_section->vma);
06bcf541 12345 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12346 elf_gp (isec->owner) = off;
12347
12348 return TRUE;
ad8e1ba5
AM
12349}
12350
927be08e
AM
12351/* Called via elf_link_hash_traverse to merge GOT entries for global
12352 symbol H. */
12353
12354static bfd_boolean
12355merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12356{
12357 if (h->root.type == bfd_link_hash_indirect)
12358 return TRUE;
12359
927be08e
AM
12360 merge_got_entries (&h->got.glist);
12361
12362 return TRUE;
12363}
12364
12365/* Called via elf_link_hash_traverse to allocate GOT entries for global
12366 symbol H. */
12367
12368static bfd_boolean
12369reallocate_got (struct elf_link_hash_entry *h, void *inf)
12370{
12371 struct got_entry *gent;
12372
12373 if (h->root.type == bfd_link_hash_indirect)
12374 return TRUE;
12375
927be08e
AM
12376 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12377 if (!gent->is_indirect)
12378 allocate_got (h, (struct bfd_link_info *) inf, gent);
12379 return TRUE;
12380}
12381
12382/* Called on the first multitoc pass after the last call to
12383 ppc64_elf_next_toc_section. This function removes duplicate GOT
12384 entries. */
12385
12386bfd_boolean
12387ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12388{
12389 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12390 struct bfd *ibfd, *ibfd2;
12391 bfd_boolean done_something;
12392
12393 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12394
7865406b
AM
12395 if (!htab->do_multi_toc)
12396 return FALSE;
12397
d0fae19d 12398 /* Merge global sym got entries within a toc group. */
927be08e
AM
12399 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12400
12401 /* And tlsld_got. */
c72f2fb2 12402 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12403 {
12404 struct got_entry *ent, *ent2;
12405
12406 if (!is_ppc64_elf (ibfd))
12407 continue;
12408
12409 ent = ppc64_tlsld_got (ibfd);
12410 if (!ent->is_indirect
12411 && ent->got.offset != (bfd_vma) -1)
12412 {
c72f2fb2 12413 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12414 {
12415 if (!is_ppc64_elf (ibfd2))
12416 continue;
12417
12418 ent2 = ppc64_tlsld_got (ibfd2);
12419 if (!ent2->is_indirect
12420 && ent2->got.offset != (bfd_vma) -1
12421 && elf_gp (ibfd2) == elf_gp (ibfd))
12422 {
12423 ent2->is_indirect = TRUE;
12424 ent2->got.ent = ent;
12425 }
12426 }
12427 }
12428 }
12429
12430 /* Zap sizes of got sections. */
33e44f2e
AM
12431 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12432 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12433 htab->got_reli_size = 0;
12434
c72f2fb2 12435 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12436 {
12437 asection *got, *relgot;
12438
12439 if (!is_ppc64_elf (ibfd))
12440 continue;
12441
12442 got = ppc64_elf_tdata (ibfd)->got;
12443 if (got != NULL)
12444 {
12445 got->rawsize = got->size;
12446 got->size = 0;
12447 relgot = ppc64_elf_tdata (ibfd)->relgot;
12448 relgot->rawsize = relgot->size;
12449 relgot->size = 0;
12450 }
12451 }
12452
12453 /* Now reallocate the got, local syms first. We don't need to
12454 allocate section contents again since we never increase size. */
c72f2fb2 12455 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12456 {
12457 struct got_entry **lgot_ents;
12458 struct got_entry **end_lgot_ents;
12459 struct plt_entry **local_plt;
12460 struct plt_entry **end_local_plt;
f961d9dd 12461 unsigned char *lgot_masks;
927be08e
AM
12462 bfd_size_type locsymcount;
12463 Elf_Internal_Shdr *symtab_hdr;
19e08130 12464 asection *s;
927be08e
AM
12465
12466 if (!is_ppc64_elf (ibfd))
12467 continue;
12468
12469 lgot_ents = elf_local_got_ents (ibfd);
12470 if (!lgot_ents)
12471 continue;
12472
12473 symtab_hdr = &elf_symtab_hdr (ibfd);
12474 locsymcount = symtab_hdr->sh_info;
12475 end_lgot_ents = lgot_ents + locsymcount;
12476 local_plt = (struct plt_entry **) end_lgot_ents;
12477 end_local_plt = local_plt + locsymcount;
f961d9dd 12478 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12479 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12480 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12481 {
12482 struct got_entry *ent;
12483
12484 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12485 {
19e08130
AM
12486 unsigned int ent_size = 8;
12487 unsigned int rel_size = sizeof (Elf64_External_Rela);
12488
d0fae19d
AM
12489 ent->got.offset = s->size;
12490 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12491 {
19e08130
AM
12492 ent_size *= 2;
12493 rel_size *= 2;
12494 }
12495 s->size += ent_size;
37da22e5 12496 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12497 {
33e44f2e 12498 htab->elf.irelplt->size += rel_size;
19e08130
AM
12499 htab->got_reli_size += rel_size;
12500 }
6a3858a6
AM
12501 else if (bfd_link_pic (info)
12502 && !((ent->tls_type & TLS_TPREL) != 0
12503 && bfd_link_executable (info)))
19e08130
AM
12504 {
12505 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12506 srel->size += rel_size;
d0fae19d
AM
12507 }
12508 }
927be08e
AM
12509 }
12510 }
12511
12512 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12513
c72f2fb2 12514 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12515 {
12516 struct got_entry *ent;
12517
12518 if (!is_ppc64_elf (ibfd))
12519 continue;
12520
12521 ent = ppc64_tlsld_got (ibfd);
12522 if (!ent->is_indirect
12523 && ent->got.offset != (bfd_vma) -1)
12524 {
12525 asection *s = ppc64_elf_tdata (ibfd)->got;
12526 ent->got.offset = s->size;
12527 s->size += 16;
0e1862bb 12528 if (bfd_link_pic (info))
927be08e
AM
12529 {
12530 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12531 srel->size += sizeof (Elf64_External_Rela);
12532 }
12533 }
12534 }
12535
33e44f2e 12536 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12537 if (!done_something)
c72f2fb2 12538 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12539 {
12540 asection *got;
12541
12542 if (!is_ppc64_elf (ibfd))
12543 continue;
12544
12545 got = ppc64_elf_tdata (ibfd)->got;
12546 if (got != NULL)
12547 {
12548 done_something = got->rawsize != got->size;
12549 if (done_something)
12550 break;
12551 }
12552 }
12553
12554 if (done_something)
e7d1c40c 12555 (*htab->params->layout_sections_again) ();
927be08e
AM
12556
12557 /* Set up for second pass over toc sections to recalculate elf_gp
12558 on input sections. */
12559 htab->toc_bfd = NULL;
12560 htab->toc_first_sec = NULL;
12561 htab->second_toc_pass = TRUE;
12562 return done_something;
12563}
12564
12565/* Called after second pass of multitoc partitioning. */
12566
12567void
12568ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12569{
12570 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12571
12572 /* After the second pass, toc_curr tracks the TOC offset used
12573 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12574 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12575}
12576
9b5ecbd0
AM
12577/* No toc references were found in ISEC. If the code in ISEC makes no
12578 calls, then there's no need to use toc adjusting stubs when branching
12579 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12580 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12581 needed, and 2 if a cyclical call-graph was found but no other reason
12582 for a stub was detected. If called from the top level, a return of
12583 2 means the same as a return of 0. */
9b5ecbd0
AM
12584
12585static int
4ce794b7 12586toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12587{
9b5ecbd0 12588 int ret;
70cc837d
AM
12589
12590 /* Mark this section as checked. */
12591 isec->call_check_done = 1;
9b5ecbd0 12592
772119ce
AM
12593 /* We know none of our code bearing sections will need toc stubs. */
12594 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12595 return 0;
12596
eea6121a 12597 if (isec->size == 0)
082c50f8
AM
12598 return 0;
12599
4c52953f
AM
12600 if (isec->output_section == NULL)
12601 return 0;
12602
4c52953f 12603 ret = 0;
70cc837d 12604 if (isec->reloc_count != 0)
9b5ecbd0 12605 {
70cc837d
AM
12606 Elf_Internal_Rela *relstart, *rel;
12607 Elf_Internal_Sym *local_syms;
12608 struct ppc_link_hash_table *htab;
2917689a 12609
70cc837d
AM
12610 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12611 info->keep_memory);
12612 if (relstart == NULL)
12613 return -1;
90aecf7a 12614
70cc837d
AM
12615 /* Look for branches to outside of this section. */
12616 local_syms = NULL;
12617 htab = ppc_hash_table (info);
12618 if (htab == NULL)
12619 return -1;
4c52953f 12620
70cc837d 12621 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12622 {
70cc837d
AM
12623 enum elf_ppc64_reloc_type r_type;
12624 unsigned long r_symndx;
12625 struct elf_link_hash_entry *h;
12626 struct ppc_link_hash_entry *eh;
12627 Elf_Internal_Sym *sym;
12628 asection *sym_sec;
12629 struct _opd_sec_data *opd;
12630 bfd_vma sym_value;
12631 bfd_vma dest;
12632
12633 r_type = ELF64_R_TYPE (rel->r_info);
12634 if (r_type != R_PPC64_REL24
05d0e962 12635 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12636 && r_type != R_PPC64_REL14
12637 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d
AM
12638 && r_type != R_PPC64_REL14_BRNTAKEN
12639 && r_type != R_PPC64_PLTCALL)
70cc837d 12640 continue;
4c52953f 12641
70cc837d
AM
12642 r_symndx = ELF64_R_SYM (rel->r_info);
12643 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12644 isec->owner))
4c52953f 12645 {
70cc837d
AM
12646 ret = -1;
12647 break;
12648 }
4c52953f 12649
70cc837d
AM
12650 /* Calls to dynamic lib functions go through a plt call stub
12651 that uses r2. */
12652 eh = (struct ppc_link_hash_entry *) h;
12653 if (eh != NULL
12654 && (eh->elf.plt.plist != NULL
12655 || (eh->oh != NULL
12656 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12657 {
12658 ret = 1;
12659 break;
4c52953f
AM
12660 }
12661
70cc837d
AM
12662 if (sym_sec == NULL)
12663 /* Ignore other undefined symbols. */
4c52953f 12664 continue;
4c52953f 12665
70cc837d
AM
12666 /* Assume branches to other sections not included in the
12667 link need stubs too, to cover -R and absolute syms. */
12668 if (sym_sec->output_section == NULL)
12669 {
12670 ret = 1;
12671 break;
12672 }
4c52953f 12673
70cc837d
AM
12674 if (h == NULL)
12675 sym_value = sym->st_value;
12676 else
12677 {
12678 if (h->root.type != bfd_link_hash_defined
12679 && h->root.type != bfd_link_hash_defweak)
12680 abort ();
12681 sym_value = h->root.u.def.value;
12682 }
12683 sym_value += rel->r_addend;
4c52953f 12684
70cc837d
AM
12685 /* If this branch reloc uses an opd sym, find the code section. */
12686 opd = get_opd_info (sym_sec);
12687 if (opd != NULL)
12688 {
12689 if (h == NULL && opd->adjust != NULL)
12690 {
12691 long adjust;
4c52953f 12692
92a9c616 12693 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12694 if (adjust == -1)
12695 /* Assume deleted functions won't ever be called. */
12696 continue;
12697 sym_value += adjust;
12698 }
4c52953f 12699
aef36ac1
AM
12700 dest = opd_entry_value (sym_sec, sym_value,
12701 &sym_sec, NULL, FALSE);
70cc837d
AM
12702 if (dest == (bfd_vma) -1)
12703 continue;
12704 }
12705 else
12706 dest = (sym_value
12707 + sym_sec->output_offset
12708 + sym_sec->output_section->vma);
4c52953f 12709
70cc837d
AM
12710 /* Ignore branch to self. */
12711 if (sym_sec == isec)
12712 continue;
4c52953f 12713
70cc837d
AM
12714 /* If the called function uses the toc, we need a stub. */
12715 if (sym_sec->has_toc_reloc
12716 || sym_sec->makes_toc_func_call)
4c52953f 12717 {
70cc837d 12718 ret = 1;
4c52953f
AM
12719 break;
12720 }
70cc837d
AM
12721
12722 /* Assume any branch that needs a long branch stub might in fact
12723 need a plt_branch stub. A plt_branch stub uses r2. */
12724 else if (dest - (isec->output_offset
12725 + isec->output_section->vma
6911b7dc
AM
12726 + rel->r_offset) + (1 << 25)
12727 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12728 ? h->other
12729 : sym->st_other))
4c52953f 12730 {
70cc837d
AM
12731 ret = 1;
12732 break;
12733 }
12734
12735 /* If calling back to a section in the process of being
12736 tested, we can't say for sure that no toc adjusting stubs
12737 are needed, so don't return zero. */
12738 else if (sym_sec->call_check_in_progress)
12739 ret = 2;
12740
12741 /* Branches to another section that itself doesn't have any TOC
12742 references are OK. Recursively call ourselves to check. */
12743 else if (!sym_sec->call_check_done)
12744 {
12745 int recur;
12746
12747 /* Mark current section as indeterminate, so that other
12748 sections that call back to current won't be marked as
12749 known. */
12750 isec->call_check_in_progress = 1;
12751 recur = toc_adjusting_stub_needed (info, sym_sec);
12752 isec->call_check_in_progress = 0;
12753
4c52953f
AM
12754 if (recur != 0)
12755 {
70cc837d
AM
12756 ret = recur;
12757 if (recur != 2)
12758 break;
4c52953f
AM
12759 }
12760 }
4c52953f 12761 }
70cc837d
AM
12762
12763 if (local_syms != NULL
12764 && (elf_symtab_hdr (isec->owner).contents
12765 != (unsigned char *) local_syms))
12766 free (local_syms);
12767 if (elf_section_data (isec)->relocs != relstart)
12768 free (relstart);
9b5ecbd0
AM
12769 }
12770
70cc837d
AM
12771 if ((ret & 1) == 0
12772 && isec->map_head.s != NULL
12773 && (strcmp (isec->output_section->name, ".init") == 0
12774 || strcmp (isec->output_section->name, ".fini") == 0))
12775 {
12776 if (isec->map_head.s->has_toc_reloc
12777 || isec->map_head.s->makes_toc_func_call)
12778 ret = 1;
12779 else if (!isec->map_head.s->call_check_done)
12780 {
12781 int recur;
12782 isec->call_check_in_progress = 1;
12783 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12784 isec->call_check_in_progress = 0;
12785 if (recur != 0)
12786 ret = recur;
12787 }
12788 }
12789
12790 if (ret == 1)
12791 isec->makes_toc_func_call = 1;
4c52953f 12792
9b5ecbd0
AM
12793 return ret;
12794}
12795
721956f4
AM
12796/* The linker repeatedly calls this function for each input section,
12797 in the order that input sections are linked into output sections.
12798 Build lists of input sections to determine groupings between which
12799 we may insert linker stubs. */
12800
9b5ecbd0 12801bfd_boolean
4ce794b7 12802ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12803{
12804 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12805
4dfe6ac6
NC
12806 if (htab == NULL)
12807 return FALSE;
12808
734b6cf9 12809 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12810 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12811 {
3d6f9012
AM
12812 /* This happens to make the list in reverse order,
12813 which is what we want. */
6f20ed8a
AM
12814 htab->sec_info[isec->id].u.list
12815 = htab->sec_info[isec->output_section->id].u.list;
12816 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12817 }
ad8e1ba5 12818
4c52953f 12819 if (htab->multi_toc_needed)
9b5ecbd0 12820 {
8b974ba3
AM
12821 /* Analyse sections that aren't already flagged as needing a
12822 valid toc pointer. Exclude .fixup for the linux kernel.
12823 .fixup contains branches, but only back to the function that
12824 hit an exception. */
12825 if (!(isec->has_toc_reloc
12826 || (isec->flags & SEC_CODE) == 0
12827 || strcmp (isec->name, ".fixup") == 0
12828 || isec->call_check_done))
12829 {
12830 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12831 return FALSE;
8b974ba3
AM
12832 }
12833 /* Make all sections use the TOC assigned for this object file.
12834 This will be wrong for pasted sections; We fix that in
12835 check_pasted_section(). */
12836 if (elf_gp (isec->owner) != 0)
12837 htab->toc_curr = elf_gp (isec->owner);
12838 }
12839
6f20ed8a 12840 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12841 return TRUE;
721956f4
AM
12842}
12843
70cc837d
AM
12844/* Check that all .init and .fini sections use the same toc, if they
12845 have toc relocs. */
12846
12847static bfd_boolean
12848check_pasted_section (struct bfd_link_info *info, const char *name)
12849{
12850 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12851
12852 if (o != NULL)
12853 {
12854 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12855 bfd_vma toc_off = 0;
12856 asection *i;
12857
12858 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12859 if (i->has_toc_reloc)
12860 {
12861 if (toc_off == 0)
6f20ed8a
AM
12862 toc_off = htab->sec_info[i->id].toc_off;
12863 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12864 return FALSE;
12865 }
6683a28d
AM
12866
12867 if (toc_off == 0)
12868 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12869 if (i->makes_toc_func_call)
12870 {
6f20ed8a 12871 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12872 break;
12873 }
12874
70cc837d
AM
12875 /* Make sure the whole pasted function uses the same toc offset. */
12876 if (toc_off != 0)
12877 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12878 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12879 }
12880 return TRUE;
12881}
12882
12883bfd_boolean
12884ppc64_elf_check_init_fini (struct bfd_link_info *info)
12885{
12886 return (check_pasted_section (info, ".init")
12887 & check_pasted_section (info, ".fini"));
12888}
12889
721956f4
AM
12890/* See whether we can group stub sections together. Grouping stub
12891 sections may result in fewer stubs. More importantly, we need to
12892 put all .init* and .fini* stubs at the beginning of the .init or
12893 .fini output sections respectively, because glibc splits the
12894 _init and _fini functions into multiple parts. Putting a stub in
12895 the middle of a function is not a good idea. */
12896
6f20ed8a
AM
12897static bfd_boolean
12898group_sections (struct bfd_link_info *info,
4ce794b7
AM
12899 bfd_size_type stub_group_size,
12900 bfd_boolean stubs_always_before_branch)
721956f4 12901{
6f20ed8a
AM
12902 struct ppc_link_hash_table *htab;
12903 asection *osec;
7c8fe5c4
AM
12904 bfd_boolean suppress_size_errors;
12905
6f20ed8a
AM
12906 htab = ppc_hash_table (info);
12907 if (htab == NULL)
12908 return FALSE;
12909
7c8fe5c4 12910 suppress_size_errors = FALSE;
7c8fe5c4
AM
12911 if (stub_group_size == 1)
12912 {
12913 /* Default values. */
12914 if (stubs_always_before_branch)
09f92717 12915 stub_group_size = 0x1e00000;
7c8fe5c4 12916 else
09f92717 12917 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12918 suppress_size_errors = TRUE;
12919 }
12920
6f20ed8a 12921 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12922 {
6f20ed8a
AM
12923 asection *tail;
12924
12925 if (osec->id >= htab->sec_info_arr_size)
12926 continue;
12927
12928 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12929 while (tail != NULL)
721956f4 12930 {
734b6cf9
AM
12931 asection *curr;
12932 asection *prev;
12933 bfd_size_type total;
12934 bfd_boolean big_sec;
12935 bfd_vma curr_toc;
6f20ed8a 12936 struct map_stub *group;
09f92717 12937 bfd_size_type group_size;
734b6cf9
AM
12938
12939 curr = tail;
eea6121a 12940 total = tail->size;
09f92717
AM
12941 group_size = (ppc64_elf_section_data (tail) != NULL
12942 && ppc64_elf_section_data (tail)->has_14bit_branch
12943 ? stub_group_size >> 10 : stub_group_size);
12944
12945 big_sec = total > group_size;
7c8fe5c4 12946 if (big_sec && !suppress_size_errors)
695344c0 12947 /* xgettext:c-format */
871b3ab2 12948 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12949 tail->owner, tail);
6f20ed8a 12950 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12951
6f20ed8a 12952 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12953 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12954 < (ppc64_elf_section_data (prev) != NULL
12955 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12956 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12957 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12958 curr = prev;
12959
12960 /* OK, the size from the start of CURR to the end is less
09f92717 12961 than group_size and thus can be handled by one stub
734b6cf9 12962 section. (or the tail section is itself larger than
09f92717
AM
12963 group_size, in which case we may be toast.) We should
12964 really be keeping track of the total size of stubs added
12965 here, as stubs contribute to the final output section
12966 size. That's a little tricky, and this way will only
12967 break if stubs added make the total size more than 2^25,
12968 ie. for the default stub_group_size, if stubs total more
12969 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12970 group = bfd_alloc (curr->owner, sizeof (*group));
12971 if (group == NULL)
12972 return FALSE;
12973 group->link_sec = curr;
12974 group->stub_sec = NULL;
a4b6fadd 12975 group->needs_save_res = 0;
df136d64
AM
12976 group->lr_restore = 0;
12977 group->eh_size = 0;
12978 group->eh_base = 0;
a4b6fadd
AM
12979 group->next = htab->group;
12980 htab->group = group;
734b6cf9 12981 do
721956f4 12982 {
6f20ed8a 12983 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12984 /* Set up this stub group. */
6f20ed8a 12985 htab->sec_info[tail->id].u.group = group;
721956f4 12986 }
734b6cf9
AM
12987 while (tail != curr && (tail = prev) != NULL);
12988
09f92717 12989 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12990 bytes before the stub section can be handled by it too.
12991 Don't do this if we have a really large section after the
12992 stubs, as adding more stubs increases the chance that
12993 branches may not reach into the stub section. */
12994 if (!stubs_always_before_branch && !big_sec)
12995 {
12996 total = 0;
12997 while (prev != NULL
12998 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12999 < (ppc64_elf_section_data (prev) != NULL
13000 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13001 ? (group_size = stub_group_size >> 10)
13002 : group_size))
6f20ed8a 13003 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13004 {
13005 tail = prev;
6f20ed8a
AM
13006 prev = htab->sec_info[tail->id].u.list;
13007 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13008 }
13009 }
13010 tail = prev;
721956f4
AM
13011 }
13012 }
6f20ed8a 13013 return TRUE;
721956f4
AM
13014}
13015
58d180e8
AM
13016static const unsigned char glink_eh_frame_cie[] =
13017{
13018 0, 0, 0, 16, /* length. */
13019 0, 0, 0, 0, /* id. */
13020 1, /* CIE version. */
13021 'z', 'R', 0, /* Augmentation string. */
13022 4, /* Code alignment. */
13023 0x78, /* Data alignment. */
13024 65, /* RA reg. */
13025 1, /* Augmentation size. */
13026 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13027 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13028};
13029
d969d15f
AM
13030/* Stripping output sections is normally done before dynamic section
13031 symbols have been allocated. This function is called later, and
13032 handles cases like htab->brlt which is mapped to its own output
13033 section. */
13034
13035static void
13036maybe_strip_output (struct bfd_link_info *info, asection *isec)
13037{
13038 if (isec->size == 0
13039 && isec->output_section->size == 0
53d8967a 13040 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13041 && !bfd_section_removed_from_list (info->output_bfd,
13042 isec->output_section)
13043 && elf_section_data (isec->output_section)->dynindx == 0)
13044 {
13045 isec->output_section->flags |= SEC_EXCLUDE;
13046 bfd_section_list_remove (info->output_bfd, isec->output_section);
13047 info->output_bfd->section_count--;
13048 }
13049}
13050
721956f4
AM
13051/* Determine and set the size of the stub section for a final link.
13052
13053 The basic idea here is to examine all the relocations looking for
13054 PC-relative calls to a target that is unreachable with a "bl"
13055 instruction. */
13056
b34976b6 13057bfd_boolean
e7d1c40c 13058ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13059{
13060 bfd_size_type stub_group_size;
b34976b6 13061 bfd_boolean stubs_always_before_branch;
721956f4
AM
13062 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13063
4dfe6ac6
NC
13064 if (htab == NULL)
13065 return FALSE;
13066
0e1862bb 13067 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13068 htab->params->plt_thread_safe = 1;
b9e5796b 13069 if (!htab->opd_abi)
e7d1c40c
AM
13070 htab->params->plt_thread_safe = 0;
13071 else if (htab->params->plt_thread_safe == -1)
794e51c0 13072 {
e2458743 13073 static const char *const thread_starter[] =
794e51c0
AM
13074 {
13075 "pthread_create",
13076 /* libstdc++ */
13077 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13078 /* librt */
13079 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13080 "mq_notify", "create_timer",
13081 /* libanl */
13082 "getaddrinfo_a",
13083 /* libgomp */
2300b5a1 13084 "GOMP_parallel",
794e51c0 13085 "GOMP_parallel_start",
2300b5a1 13086 "GOMP_parallel_loop_static",
794e51c0 13087 "GOMP_parallel_loop_static_start",
2300b5a1 13088 "GOMP_parallel_loop_dynamic",
794e51c0 13089 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13090 "GOMP_parallel_loop_guided",
794e51c0 13091 "GOMP_parallel_loop_guided_start",
2300b5a1 13092 "GOMP_parallel_loop_runtime",
794e51c0 13093 "GOMP_parallel_loop_runtime_start",
2300b5a1 13094 "GOMP_parallel_sections",
68ffbac6 13095 "GOMP_parallel_sections_start",
f9dffbf0
AM
13096 /* libgo */
13097 "__go_go",
794e51c0
AM
13098 };
13099 unsigned i;
13100
a4b6fadd 13101 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13102 {
13103 struct elf_link_hash_entry *h;
13104 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13105 FALSE, FALSE, TRUE);
e7d1c40c
AM
13106 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13107 if (htab->params->plt_thread_safe)
794e51c0
AM
13108 break;
13109 }
13110 }
e7d1c40c
AM
13111 stubs_always_before_branch = htab->params->group_size < 0;
13112 if (htab->params->group_size < 0)
13113 stub_group_size = -htab->params->group_size;
721956f4 13114 else
e7d1c40c 13115 stub_group_size = htab->params->group_size;
721956f4 13116
6f20ed8a
AM
13117 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13118 return FALSE;
721956f4 13119
c9301e31
AM
13120#define STUB_SHRINK_ITER 20
13121 /* Loop until no stubs added. After iteration 20 of this loop we may
13122 exit on a stub section shrinking. This is to break out of a
13123 pathological case where adding stubs on one iteration decreases
13124 section gaps (perhaps due to alignment), which then requires
13125 fewer or smaller stubs on the next iteration. */
13126
721956f4
AM
13127 while (1)
13128 {
13129 bfd *input_bfd;
13130 unsigned int bfd_indx;
a4b6fadd 13131 struct map_stub *group;
721956f4
AM
13132
13133 htab->stub_iteration += 1;
721956f4
AM
13134
13135 for (input_bfd = info->input_bfds, bfd_indx = 0;
13136 input_bfd != NULL;
c72f2fb2 13137 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13138 {
13139 Elf_Internal_Shdr *symtab_hdr;
13140 asection *section;
6cdc0ccc 13141 Elf_Internal_Sym *local_syms = NULL;
721956f4 13142
0c8d6e5c 13143 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13144 continue;
13145
721956f4 13146 /* We'll need the symbol table in a second. */
0ffa91dd 13147 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13148 if (symtab_hdr->sh_info == 0)
13149 continue;
13150
721956f4
AM
13151 /* Walk over each section attached to the input bfd. */
13152 for (section = input_bfd->sections;
13153 section != NULL;
13154 section = section->next)
13155 {
721956f4 13156 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
13157
13158 /* If there aren't any relocs, then there's nothing more
13159 to do. */
13160 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13161 || (section->flags & SEC_ALLOC) == 0
13162 || (section->flags & SEC_LOAD) == 0
13163 || (section->flags & SEC_CODE) == 0
721956f4
AM
13164 || section->reloc_count == 0)
13165 continue;
13166
13167 /* If this section is a link-once section that will be
13168 discarded, then don't create any stubs. */
13169 if (section->output_section == NULL
927be08e 13170 || section->output_section->owner != info->output_bfd)
721956f4
AM
13171 continue;
13172
1e2f5b6e
AM
13173 /* Get the relocs. */
13174 internal_relocs
4ce794b7 13175 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13176 info->keep_memory);
721956f4 13177 if (internal_relocs == NULL)
1e2f5b6e 13178 goto error_ret_free_local;
721956f4
AM
13179
13180 /* Now examine each relocation. */
13181 irela = internal_relocs;
13182 irelaend = irela + section->reloc_count;
13183 for (; irela < irelaend; irela++)
13184 {
4ce794b7
AM
13185 enum elf_ppc64_reloc_type r_type;
13186 unsigned int r_indx;
721956f4
AM
13187 enum ppc_stub_type stub_type;
13188 struct ppc_stub_hash_entry *stub_entry;
8387904d 13189 asection *sym_sec, *code_sec;
e054468f 13190 bfd_vma sym_value, code_value;
721956f4 13191 bfd_vma destination;
6911b7dc 13192 unsigned long local_off;
8843416a 13193 bfd_boolean ok_dest;
721956f4 13194 struct ppc_link_hash_entry *hash;
8387904d 13195 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13196 struct elf_link_hash_entry *h;
13197 Elf_Internal_Sym *sym;
721956f4
AM
13198 char *stub_name;
13199 const asection *id_sec;
74f0fb50 13200 struct _opd_sec_data *opd;
e054468f 13201 struct plt_entry *plt_ent;
721956f4
AM
13202
13203 r_type = ELF64_R_TYPE (irela->r_info);
13204 r_indx = ELF64_R_SYM (irela->r_info);
13205
4ce794b7 13206 if (r_type >= R_PPC64_max)
721956f4
AM
13207 {
13208 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13209 goto error_ret_free_internal;
721956f4
AM
13210 }
13211
13212 /* Only look for stubs on branch instructions. */
4ce794b7 13213 if (r_type != R_PPC64_REL24
05d0e962 13214 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
13215 && r_type != R_PPC64_REL14
13216 && r_type != R_PPC64_REL14_BRTAKEN
13217 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
13218 continue;
13219
13220 /* Now determine the call target, its name, value,
13221 section. */
411e1bfb
AM
13222 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13223 r_indx, input_bfd))
13224 goto error_ret_free_internal;
13225 hash = (struct ppc_link_hash_entry *) h;
13226
8843416a 13227 ok_dest = FALSE;
8387904d 13228 fdh = NULL;
7fe2b9a6 13229 sym_value = 0;
411e1bfb 13230 if (hash == NULL)
721956f4 13231 {
411e1bfb 13232 sym_value = sym->st_value;
c27b8c2a
AM
13233 if (sym_sec != NULL
13234 && sym_sec->output_section != NULL)
13235 ok_dest = TRUE;
721956f4 13236 }
7fe2b9a6
AM
13237 else if (hash->elf.root.type == bfd_link_hash_defined
13238 || hash->elf.root.type == bfd_link_hash_defweak)
13239 {
13240 sym_value = hash->elf.root.u.def.value;
13241 if (sym_sec->output_section != NULL)
13242 ok_dest = TRUE;
13243 }
13244 else if (hash->elf.root.type == bfd_link_hash_undefweak
13245 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13246 {
99877b66 13247 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13248 use the func descriptor sym instead if it is
13249 defined. */
ceb1f1ef 13250 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13251 && hash->oh != NULL)
8387904d 13252 {
8c5b4e52 13253 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13254 if (fdh->elf.root.type == bfd_link_hash_defined
13255 || fdh->elf.root.type == bfd_link_hash_defweak)
13256 {
13257 sym_sec = fdh->elf.root.u.def.section;
13258 sym_value = fdh->elf.root.u.def.value;
13259 if (sym_sec->output_section != NULL)
13260 ok_dest = TRUE;
13261 }
99877b66
AM
13262 else
13263 fdh = NULL;
8387904d 13264 }
7fe2b9a6
AM
13265 }
13266 else
13267 {
13268 bfd_set_error (bfd_error_bad_value);
13269 goto error_ret_free_internal;
721956f4
AM
13270 }
13271
8843416a 13272 destination = 0;
6911b7dc 13273 local_off = 0;
8843416a
AM
13274 if (ok_dest)
13275 {
13276 sym_value += irela->r_addend;
13277 destination = (sym_value
13278 + sym_sec->output_offset
13279 + sym_sec->output_section->vma);
6911b7dc
AM
13280 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13281 ? hash->elf.other
13282 : sym->st_other);
8843416a
AM
13283 }
13284
8387904d 13285 code_sec = sym_sec;
e054468f 13286 code_value = sym_value;
74f0fb50
AM
13287 opd = get_opd_info (sym_sec);
13288 if (opd != NULL)
8387904d
AM
13289 {
13290 bfd_vma dest;
13291
74f0fb50 13292 if (hash == NULL && opd->adjust != NULL)
8387904d 13293 {
51aecdc5 13294 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13295 if (adjust == -1)
13296 continue;
e054468f 13297 code_value += adjust;
8387904d
AM
13298 sym_value += adjust;
13299 }
13300 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 13301 &code_sec, &code_value, FALSE);
8387904d
AM
13302 if (dest != (bfd_vma) -1)
13303 {
13304 destination = dest;
13305 if (fdh != NULL)
13306 {
13307 /* Fixup old ABI sym to point at code
13308 entry. */
99877b66 13309 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13310 hash->elf.root.u.def.section = code_sec;
e054468f 13311 hash->elf.root.u.def.value = code_value;
8387904d
AM
13312 }
13313 }
13314 }
13315
721956f4 13316 /* Determine what (if any) linker stub is needed. */
e054468f 13317 plt_ent = NULL;
721956f4 13318 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13319 &plt_ent, destination,
13320 local_off);
ad8e1ba5 13321
05d0e962
AM
13322 if (r_type == R_PPC64_REL24_NOTOC)
13323 {
13324 if (stub_type == ppc_stub_plt_call)
13325 stub_type = ppc_stub_plt_call_notoc;
13326 else if (stub_type == ppc_stub_long_branch
13327 || (code_sec != NULL
13328 && code_sec->output_section != NULL
13329 && (((hash ? hash->elf.other : sym->st_other)
13330 & STO_PPC64_LOCAL_MASK)
13331 != 1 << STO_PPC64_LOCAL_BIT)))
13332 stub_type = ppc_stub_long_branch_notoc;
13333 }
13334 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13335 {
13336 /* Check whether we need a TOC adjusting stub.
13337 Since the linker pastes together pieces from
13338 different object files when creating the
13339 _init and _fini functions, it may be that a
13340 call to what looks like a local sym is in
13341 fact a call needing a TOC adjustment. */
33cb30a1
AM
13342 if ((code_sec != NULL
13343 && code_sec->output_section != NULL
13344 && (htab->sec_info[code_sec->id].toc_off
13345 != htab->sec_info[section->id].toc_off)
13346 && (code_sec->has_toc_reloc
13347 || code_sec->makes_toc_func_call))
13348 || (((hash ? hash->elf.other : sym->st_other)
13349 & STO_PPC64_LOCAL_MASK)
13350 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13351 stub_type = ppc_stub_long_branch_r2off;
13352 }
13353
721956f4
AM
13354 if (stub_type == ppc_stub_none)
13355 continue;
13356
411e1bfb
AM
13357 /* __tls_get_addr calls might be eliminated. */
13358 if (stub_type != ppc_stub_plt_call
05d0e962 13359 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13360 && hash != NULL
8387904d
AM
13361 && (hash == htab->tls_get_addr
13362 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
13363 && section->has_tls_reloc
13364 && irela != internal_relocs)
13365 {
13366 /* Get tls info. */
f961d9dd 13367 unsigned char *tls_mask;
411e1bfb 13368
3a71aa26 13369 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13370 irela - 1, input_bfd))
13371 goto error_ret_free_internal;
37da22e5 13372 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
13373 continue;
13374 }
13375
f378ab09 13376 if (stub_type == ppc_stub_plt_call)
794e51c0 13377 {
6e1816be
AM
13378 if (!htab->opd_abi
13379 && htab->params->plt_localentry0 != 0
13380 && is_elfv2_localentry0 (&hash->elf))
13381 htab->has_plt_localentry0 = 1;
13382 else if (irela + 1 < irelaend
13383 && irela[1].r_offset == irela->r_offset + 4
13384 && (ELF64_R_TYPE (irela[1].r_info)
13385 == R_PPC64_TOCSAVE))
f378ab09
AM
13386 {
13387 if (!tocsave_find (htab, INSERT,
13388 &local_syms, irela + 1, input_bfd))
13389 goto error_ret_free_internal;
13390 }
f378ab09
AM
13391 else
13392 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13393 }
3b421ab3 13394
721956f4 13395 /* Support for grouping stub sections. */
6f20ed8a 13396 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13397
13398 /* Get the name of this stub. */
13399 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13400 if (!stub_name)
13401 goto error_ret_free_internal;
13402
13403 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13404 stub_name, FALSE, FALSE);
721956f4
AM
13405 if (stub_entry != NULL)
13406 {
05d0e962
AM
13407 enum ppc_stub_type old_type;
13408 /* A stub has already been created, but it may
13409 not be the required type. We shouldn't be
13410 transitioning from plt_call to long_branch
13411 stubs or vice versa, but we might be
13412 upgrading from plt_call to plt_call_r2save or
13413 from long_branch to long_branch_r2off. */
721956f4 13414 free (stub_name);
05d0e962
AM
13415 old_type = stub_entry->stub_type;
13416 switch (old_type)
13417 {
13418 default:
13419 abort ();
13420
13421 case ppc_stub_save_res:
13422 continue;
13423
13424 case ppc_stub_plt_call:
13425 case ppc_stub_plt_call_r2save:
13426 case ppc_stub_plt_call_notoc:
13427 case ppc_stub_plt_call_both:
13428 if (stub_type == ppc_stub_plt_call)
13429 continue;
13430 else if (stub_type == ppc_stub_plt_call_r2save)
13431 {
13432 if (old_type == ppc_stub_plt_call_notoc)
13433 stub_type = ppc_stub_plt_call_both;
13434 }
13435 else if (stub_type == ppc_stub_plt_call_notoc)
13436 {
13437 if (old_type == ppc_stub_plt_call_r2save)
13438 stub_type = ppc_stub_plt_call_both;
13439 }
13440 else
13441 abort ();
13442 break;
13443
13444 case ppc_stub_plt_branch:
13445 case ppc_stub_plt_branch_r2off:
13446 case ppc_stub_plt_branch_notoc:
13447 case ppc_stub_plt_branch_both:
13448 old_type += (ppc_stub_long_branch
13449 - ppc_stub_plt_branch);
13450 /* Fall through. */
13451 case ppc_stub_long_branch:
13452 case ppc_stub_long_branch_r2off:
13453 case ppc_stub_long_branch_notoc:
13454 case ppc_stub_long_branch_both:
13455 if (stub_type == ppc_stub_long_branch)
13456 continue;
13457 else if (stub_type == ppc_stub_long_branch_r2off)
13458 {
13459 if (old_type == ppc_stub_long_branch_notoc)
13460 stub_type = ppc_stub_long_branch_both;
13461 }
13462 else if (stub_type == ppc_stub_long_branch_notoc)
13463 {
13464 if (old_type == ppc_stub_long_branch_r2off)
13465 stub_type = ppc_stub_long_branch_both;
13466 }
13467 else
13468 abort ();
13469 break;
13470 }
13471 if (old_type < stub_type)
794e51c0 13472 stub_entry->stub_type = stub_type;
721956f4
AM
13473 continue;
13474 }
13475
25f53a85 13476 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13477 if (stub_entry == NULL)
13478 {
13479 free (stub_name);
6cdc0ccc
AM
13480 error_ret_free_internal:
13481 if (elf_section_data (section)->relocs == NULL)
13482 free (internal_relocs);
13483 error_ret_free_local:
13484 if (local_syms != NULL
13485 && (symtab_hdr->contents
13486 != (unsigned char *) local_syms))
13487 free (local_syms);
b34976b6 13488 return FALSE;
721956f4
AM
13489 }
13490
ad8e1ba5 13491 stub_entry->stub_type = stub_type;
05d0e962
AM
13492 if (stub_type >= ppc_stub_plt_call
13493 && stub_type <= ppc_stub_plt_call_both)
e054468f 13494 {
05d0e962
AM
13495 stub_entry->target_value = sym_value;
13496 stub_entry->target_section = sym_sec;
e054468f
AM
13497 }
13498 else
13499 {
05d0e962
AM
13500 stub_entry->target_value = code_value;
13501 stub_entry->target_section = code_sec;
e054468f 13502 }
721956f4 13503 stub_entry->h = hash;
e054468f 13504 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13505 stub_entry->symtype
13506 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13507 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
13508
13509 if (stub_entry->h != NULL)
13510 htab->stub_globals += 1;
721956f4
AM
13511 }
13512
13513 /* We're done with the internal relocs, free them. */
6cdc0ccc 13514 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13515 free (internal_relocs);
721956f4 13516 }
6cdc0ccc
AM
13517
13518 if (local_syms != NULL
13519 && symtab_hdr->contents != (unsigned char *) local_syms)
13520 {
13521 if (!info->keep_memory)
13522 free (local_syms);
13523 else
13524 symtab_hdr->contents = (unsigned char *) local_syms;
13525 }
721956f4
AM
13526 }
13527
5c3dead3 13528 /* We may have added some stubs. Find out the new size of the
721956f4 13529 stub sections. */
d4aaa2a0 13530 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13531 {
13532 group->lr_restore = 0;
13533 group->eh_size = 0;
13534 if (group->stub_sec != NULL)
13535 {
13536 asection *stub_sec = group->stub_sec;
13537
13538 if (htab->stub_iteration <= STUB_SHRINK_ITER
13539 || stub_sec->rawsize < stub_sec->size)
13540 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13541 stub_sec->rawsize = stub_sec->size;
13542 stub_sec->size = 0;
13543 stub_sec->reloc_count = 0;
13544 stub_sec->flags &= ~SEC_RELOC;
13545 }
13546 }
eea6121a 13547
ba21f564
AM
13548 if (htab->stub_iteration <= STUB_SHRINK_ITER
13549 || htab->brlt->rawsize < htab->brlt->size)
13550 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13551 htab->brlt->size = 0;
84f5d08e
AM
13552 htab->brlt->reloc_count = 0;
13553 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13554 if (htab->relbrlt != NULL)
eea6121a 13555 htab->relbrlt->size = 0;
721956f4 13556
63bc6f6c 13557 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13558
a4b6fadd
AM
13559 for (group = htab->group; group != NULL; group = group->next)
13560 if (group->needs_save_res)
13561 group->stub_sec->size += htab->sfpr->size;
13562
176a0d42
AM
13563 if (info->emitrelocations
13564 && htab->glink != NULL && htab->glink->size != 0)
13565 {
13566 htab->glink->reloc_count = 1;
13567 htab->glink->flags |= SEC_RELOC;
13568 }
13569
58d180e8
AM
13570 if (htab->glink_eh_frame != NULL
13571 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13572 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13573 {
2e0ce1c8 13574 size_t size = 0, align = 4;
58d180e8 13575
d4aaa2a0 13576 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13577 if (group->eh_size != 0)
13578 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13579 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13580 size += (24 + align - 1) & -align;
58d180e8 13581 if (size != 0)
2e0ce1c8
AM
13582 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13583 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13584 size = (size + align - 1) & -align;
58d180e8
AM
13585 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13586 htab->glink_eh_frame->size = size;
13587 }
13588
e7d1c40c 13589 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13590 for (group = htab->group; group != NULL; group = group->next)
13591 if (group->stub_sec != NULL)
691d2e9a
AM
13592 {
13593 int align = abs (htab->params->plt_stub_align);
13594 group->stub_sec->size
13595 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13596 }
d4aaa2a0
AM
13597
13598 for (group = htab->group; group != NULL; group = group->next)
13599 if (group->stub_sec != NULL
13600 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13601 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13602 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13603 break;
13604
d4aaa2a0 13605 if (group == NULL
ba21f564
AM
13606 && (htab->brlt->rawsize == htab->brlt->size
13607 || (htab->stub_iteration > STUB_SHRINK_ITER
13608 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13609 && (htab->glink_eh_frame == NULL
13610 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13611 break;
13612
721956f4 13613 /* Ask the linker to do its stuff. */
e7d1c40c 13614 (*htab->params->layout_sections_again) ();
721956f4
AM
13615 }
13616
da44f4e5
AM
13617 if (htab->glink_eh_frame != NULL
13618 && htab->glink_eh_frame->size != 0)
13619 {
13620 bfd_vma val;
13621 bfd_byte *p, *last_fde;
13622 size_t last_fde_len, size, align, pad;
d4aaa2a0 13623 struct map_stub *group;
da44f4e5 13624
df136d64
AM
13625 /* It is necessary to at least have a rough outline of the
13626 linker generated CIEs and FDEs written before
13627 bfd_elf_discard_info is run, in order for these FDEs to be
13628 indexed in .eh_frame_hdr. */
da44f4e5
AM
13629 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13630 if (p == NULL)
13631 return FALSE;
13632 htab->glink_eh_frame->contents = p;
13633 last_fde = p;
2e0ce1c8 13634 align = 4;
da44f4e5
AM
13635
13636 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13637 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13638 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13639 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13640 p += last_fde_len + 4;
da44f4e5 13641
d4aaa2a0 13642 for (group = htab->group; group != NULL; group = group->next)
df136d64 13643 if (group->eh_size != 0)
da44f4e5 13644 {
df136d64 13645 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13646 last_fde = p;
df136d64 13647 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13648 /* FDE length. */
2e0ce1c8 13649 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13650 p += 4;
13651 /* CIE pointer. */
13652 val = p - htab->glink_eh_frame->contents;
13653 bfd_put_32 (htab->elf.dynobj, val, p);
13654 p += 4;
13655 /* Offset to stub section, written later. */
13656 p += 4;
13657 /* stub section size. */
d4aaa2a0 13658 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13659 p += 4;
13660 /* Augmentation. */
13661 p += 1;
df136d64
AM
13662 /* Make sure we don't have all nops. This is enough for
13663 elf-eh-frame.c to detect the last non-nop opcode. */
13664 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13665 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13666 }
13667 if (htab->glink != NULL && htab->glink->size != 0)
13668 {
13669 last_fde = p;
2e0ce1c8 13670 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13671 /* FDE length. */
2e0ce1c8 13672 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13673 p += 4;
13674 /* CIE pointer. */
13675 val = p - htab->glink_eh_frame->contents;
13676 bfd_put_32 (htab->elf.dynobj, val, p);
13677 p += 4;
13678 /* Offset to .glink, written later. */
13679 p += 4;
13680 /* .glink size. */
13681 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13682 p += 4;
13683 /* Augmentation. */
13684 p += 1;
13685
13686 *p++ = DW_CFA_advance_loc + 1;
13687 *p++ = DW_CFA_register;
13688 *p++ = 65;
9f08fa5c 13689 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13690 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13691 *p++ = DW_CFA_restore_extended;
13692 *p++ = 65;
2e0ce1c8 13693 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13694 }
13695 /* Subsume any padding into the last FDE if user .eh_frame
13696 sections are aligned more than glink_eh_frame. Otherwise any
13697 zero padding will be seen as a terminator. */
2e0ce1c8 13698 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13699 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13700 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13701 htab->glink_eh_frame->size = size + pad;
13702 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13703 }
13704
d969d15f
AM
13705 maybe_strip_output (info, htab->brlt);
13706 if (htab->glink_eh_frame != NULL)
13707 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13708
b34976b6 13709 return TRUE;
721956f4
AM
13710}
13711
13712/* Called after we have determined section placement. If sections
805fc799 13713 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13714
805fc799 13715bfd_vma
1c865ab2 13716ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13717{
805fc799 13718 asection *s;
a27e685f 13719 bfd_vma TOCstart, adjust;
721956f4 13720
43417696
AM
13721 if (info != NULL)
13722 {
13723 struct elf_link_hash_entry *h;
13724 struct elf_link_hash_table *htab = elf_hash_table (info);
13725
13726 if (is_elf_hash_table (htab)
13727 && htab->hgot != NULL)
13728 h = htab->hgot;
13729 else
13730 {
13731 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13732 if (is_elf_hash_table (htab))
13733 htab->hgot = h;
13734 }
13735 if (h != NULL
13736 && h->root.type == bfd_link_hash_defined
13737 && !h->root.linker_def
13738 && (!is_elf_hash_table (htab)
13739 || h->def_regular))
13740 {
13741 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13742 + h->root.u.def.section->output_offset
13743 + h->root.u.def.section->output_section->vma);
13744 _bfd_set_gp_value (obfd, TOCstart);
13745 return TOCstart;
13746 }
13747 }
13748
805fc799
AM
13749 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13750 order. The TOC starts where the first of these sections starts. */
13751 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13752 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13753 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13754 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13755 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13756 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13757 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13758 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13759 {
13760 /* This may happen for
13761 o references to TOC base (SYM@toc / TOC[tc0]) without a
13762 .toc directive
13763 o bad linker script
13764 o --gc-sections and empty TOC sections
13765
13766 FIXME: Warn user? */
13767
13768 /* Look for a likely section. We probably won't even be
13769 using TOCstart. */
13770 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13771 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13772 | SEC_EXCLUDE))
805fc799
AM
13773 == (SEC_ALLOC | SEC_SMALL_DATA))
13774 break;
721956f4 13775 if (s == NULL)
805fc799 13776 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13777 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13778 == (SEC_ALLOC | SEC_SMALL_DATA))
13779 break;
721956f4 13780 if (s == NULL)
805fc799 13781 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13782 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13783 == SEC_ALLOC)
805fc799 13784 break;
721956f4 13785 if (s == NULL)
805fc799 13786 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13787 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13788 break;
13789 }
721956f4 13790
805fc799
AM
13791 TOCstart = 0;
13792 if (s != NULL)
13793 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13794
a27e685f
AM
13795 /* Force alignment. */
13796 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13797 TOCstart -= adjust;
1c865ab2
AM
13798 _bfd_set_gp_value (obfd, TOCstart);
13799
810d4e75 13800 if (info != NULL && s != NULL)
1c865ab2
AM
13801 {
13802 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13803
810d4e75
AM
13804 if (htab != NULL)
13805 {
13806 if (htab->elf.hgot != NULL)
13807 {
a27e685f 13808 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13809 htab->elf.hgot->root.u.def.section = s;
13810 }
13811 }
13812 else
1c865ab2 13813 {
810d4e75
AM
13814 struct bfd_link_hash_entry *bh = NULL;
13815 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13816 s, TOC_BASE_OFF - adjust,
13817 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13818 }
13819 }
805fc799 13820 return TOCstart;
721956f4
AM
13821}
13822
a345bc8d 13823/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13824 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13825
13826static bfd_boolean
49c09209 13827build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13828{
13829 struct bfd_link_info *info;
13830 struct ppc_link_hash_table *htab;
49c09209 13831 struct plt_entry *ent;
a345bc8d
AM
13832 asection *s;
13833
13834 if (h->root.type == bfd_link_hash_indirect)
13835 return TRUE;
13836
49c09209
AM
13837 info = inf;
13838 htab = ppc_hash_table (info);
13839 if (htab == NULL)
13840 return FALSE;
13841
13842 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13843 if (ent->plt.offset != (bfd_vma) -1)
13844 {
13845 /* This symbol has an entry in the procedure linkage
13846 table. Set it up. */
13847 Elf_Internal_Rela rela;
2d7ad24e 13848 asection *plt, *relplt;
49c09209
AM
13849 bfd_byte *loc;
13850
13851 if (!htab->elf.dynamic_sections_created
13852 || h->dynindx == -1)
13853 {
13854 if (!(h->def_regular
13855 && (h->root.type == bfd_link_hash_defined
13856 || h->root.type == bfd_link_hash_defweak)))
13857 continue;
2d7ad24e
AM
13858 if (h->type == STT_GNU_IFUNC)
13859 {
13860 plt = htab->elf.iplt;
13861 relplt = htab->elf.irelplt;
13862 htab->local_ifunc_resolver = 1;
13863 if (htab->opd_abi)
13864 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13865 else
13866 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13867 }
49c09209 13868 else
2d7ad24e
AM
13869 {
13870 plt = htab->pltlocal;
13871 if (bfd_link_pic (info))
13872 {
13873 relplt = htab->relpltlocal;
13874 if (htab->opd_abi)
13875 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13876 else
13877 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13878 }
13879 else
13880 relplt = NULL;
13881 }
49c09209
AM
13882 rela.r_addend = (h->root.u.def.value
13883 + h->root.u.def.section->output_offset
13884 + h->root.u.def.section->output_section->vma
13885 + ent->addend);
2d7ad24e
AM
13886
13887 if (relplt == NULL)
13888 {
13889 loc = plt->contents + ent->plt.offset;
13890 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13891 if (htab->opd_abi)
13892 {
13893 bfd_vma toc = elf_gp (info->output_bfd);
13894 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13895 bfd_put_64 (info->output_bfd, toc, loc + 8);
13896 }
13897 }
13898 else
13899 {
13900 rela.r_offset = (plt->output_section->vma
13901 + plt->output_offset
13902 + ent->plt.offset);
13903 loc = relplt->contents + (relplt->reloc_count++
13904 * sizeof (Elf64_External_Rela));
13905 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13906 }
49c09209
AM
13907 }
13908 else
13909 {
13910 rela.r_offset = (htab->elf.splt->output_section->vma
13911 + htab->elf.splt->output_offset
13912 + ent->plt.offset);
13913 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13914 rela.r_addend = ent->addend;
13915 loc = (htab->elf.srelplt->contents
13916 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13917 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13918 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13919 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13920 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13921 }
49c09209
AM
13922 }
13923
a345bc8d
AM
13924 if (!h->pointer_equality_needed)
13925 return TRUE;
13926
13927 if (h->def_regular)
13928 return TRUE;
13929
9e390558 13930 s = htab->global_entry;
49c09209
AM
13931 if (s == NULL || s->size == 0)
13932 return TRUE;
13933
13934 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13935 if (ent->plt.offset != (bfd_vma) -1
13936 && ent->addend == 0)
a345bc8d
AM
13937 {
13938 bfd_byte *p;
13939 asection *plt;
13940 bfd_vma off;
13941
a345bc8d 13942 p = s->contents + h->root.u.def.value;
33e44f2e 13943 plt = htab->elf.splt;
a345bc8d
AM
13944 if (!htab->elf.dynamic_sections_created
13945 || h->dynindx == -1)
2d7ad24e
AM
13946 {
13947 if (h->type == STT_GNU_IFUNC)
13948 plt = htab->elf.iplt;
13949 else
13950 plt = htab->pltlocal;
13951 }
49c09209 13952 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13953 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13954
13955 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13956 {
13957 info->callbacks->einfo
c1c8c1ef 13958 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13959 h->root.root.string);
13960 bfd_set_error (bfd_error_bad_value);
13961 htab->stub_error = TRUE;
13962 }
13963
7341d5e2
AM
13964 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13965 if (htab->params->emit_stub_syms)
13966 {
13967 size_t len = strlen (h->root.root.string);
13968 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13969
13970 if (name == NULL)
13971 return FALSE;
13972
13973 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13974 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13975 if (h == NULL)
13976 return FALSE;
13977 if (h->root.type == bfd_link_hash_new)
13978 {
13979 h->root.type = bfd_link_hash_defined;
13980 h->root.u.def.section = s;
13981 h->root.u.def.value = p - s->contents;
13982 h->ref_regular = 1;
13983 h->def_regular = 1;
13984 h->ref_regular_nonweak = 1;
13985 h->forced_local = 1;
13986 h->non_elf = 0;
2ec55de3 13987 h->root.linker_def = 1;
7341d5e2
AM
13988 }
13989 }
13990
a345bc8d
AM
13991 if (PPC_HA (off) != 0)
13992 {
13993 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13994 p += 4;
13995 }
13996 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13997 p += 4;
13998 bfd_put_32 (s->owner, MTCTR_R12, p);
13999 p += 4;
407aa07c 14000 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14001 break;
14002 }
14003 return TRUE;
14004}
14005
49c09209
AM
14006/* Write PLT relocs for locals. */
14007
14008static bfd_boolean
14009write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14010{
14011 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14012 bfd *ibfd;
14013
14014 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14015 {
14016 struct got_entry **lgot_ents, **end_lgot_ents;
14017 struct plt_entry **local_plt, **lplt, **end_local_plt;
14018 Elf_Internal_Shdr *symtab_hdr;
14019 bfd_size_type locsymcount;
14020 Elf_Internal_Sym *local_syms = NULL;
14021 struct plt_entry *ent;
14022
14023 if (!is_ppc64_elf (ibfd))
14024 continue;
14025
14026 lgot_ents = elf_local_got_ents (ibfd);
14027 if (!lgot_ents)
14028 continue;
14029
14030 symtab_hdr = &elf_symtab_hdr (ibfd);
14031 locsymcount = symtab_hdr->sh_info;
14032 end_lgot_ents = lgot_ents + locsymcount;
14033 local_plt = (struct plt_entry **) end_lgot_ents;
14034 end_local_plt = local_plt + locsymcount;
14035 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14036 for (ent = *lplt; ent != NULL; ent = ent->next)
14037 if (ent->plt.offset != (bfd_vma) -1)
14038 {
14039 Elf_Internal_Sym *sym;
14040 asection *sym_sec;
14041 asection *plt, *relplt;
14042 bfd_byte *loc;
14043 bfd_vma val;
49c09209
AM
14044
14045 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14046 lplt - local_plt, ibfd))
14047 {
14048 if (local_syms != NULL
14049 && symtab_hdr->contents != (unsigned char *) local_syms)
14050 free (local_syms);
14051 return FALSE;
14052 }
14053
14054 val = sym->st_value + ent->addend;
14055 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
14056 if (sym_sec != NULL && sym_sec->output_section != NULL)
14057 val += sym_sec->output_offset + sym_sec->output_section->vma;
14058
2d7ad24e
AM
14059 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14060 {
14061 htab->local_ifunc_resolver = 1;
14062 plt = htab->elf.iplt;
14063 relplt = htab->elf.irelplt;
14064 }
14065 else
14066 {
14067 plt = htab->pltlocal;
14068 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14069 }
49c09209 14070
2d7ad24e
AM
14071 if (relplt == NULL)
14072 {
14073 loc = plt->contents + ent->plt.offset;
14074 bfd_put_64 (info->output_bfd, val, loc);
14075 if (htab->opd_abi)
14076 {
14077 bfd_vma toc = elf_gp (ibfd);
14078 bfd_put_64 (info->output_bfd, toc, loc + 8);
14079 }
14080 }
49c09209 14081 else
2d7ad24e
AM
14082 {
14083 Elf_Internal_Rela rela;
14084 rela.r_offset = (ent->plt.offset
14085 + plt->output_offset
14086 + plt->output_section->vma);
14087 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14088 {
14089 if (htab->opd_abi)
14090 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14091 else
14092 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14093 }
14094 else
14095 {
14096 if (htab->opd_abi)
14097 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14098 else
14099 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14100 }
14101 rela.r_addend = val;
14102 loc = relplt->contents + (relplt->reloc_count++
14103 * sizeof (Elf64_External_Rela));
14104 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14105 }
49c09209
AM
14106 }
14107
14108 if (local_syms != NULL
14109 && symtab_hdr->contents != (unsigned char *) local_syms)
14110 {
14111 if (!info->keep_memory)
14112 free (local_syms);
14113 else
14114 symtab_hdr->contents = (unsigned char *) local_syms;
14115 }
14116 }
14117 return TRUE;
14118}
14119
721956f4
AM
14120/* Build all the stubs associated with the current output file.
14121 The stubs are kept in a hash table attached to the main linker
14122 hash table. This function is called via gldelf64ppc_finish. */
14123
b34976b6 14124bfd_boolean
e7d1c40c 14125ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14126 char **stats)
5d1634d7
AM
14127{
14128 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14129 struct map_stub *group;
721956f4 14130 asection *stub_sec;
5d1634d7 14131 bfd_byte *p;
e717da7e 14132 int stub_sec_count = 0;
5d1634d7 14133
4dfe6ac6
NC
14134 if (htab == NULL)
14135 return FALSE;
14136
eea6121a 14137 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14138 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14139 {
14140 group->eh_size = 0;
14141 group->lr_restore = 0;
14142 if ((stub_sec = group->stub_sec) != NULL
14143 && stub_sec->size != 0)
14144 {
14145 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14146 stub_sec->size);
14147 if (stub_sec->contents == NULL)
14148 return FALSE;
14149 stub_sec->size = 0;
14150 }
14151 }
5d1634d7 14152
23eb7e01 14153 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14154 {
9f951329 14155 unsigned int indx;
ad8e1ba5 14156 bfd_vma plt0;
9f951329 14157
721956f4 14158 /* Build the .glink plt call stub. */
e7d1c40c 14159 if (htab->params->emit_stub_syms)
97b639ba
AM
14160 {
14161 struct elf_link_hash_entry *h;
468392fb
AM
14162 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14163 TRUE, FALSE, FALSE);
97b639ba
AM
14164 if (h == NULL)
14165 return FALSE;
14166 if (h->root.type == bfd_link_hash_new)
14167 {
14168 h->root.type = bfd_link_hash_defined;
14169 h->root.u.def.section = htab->glink;
ee4bf8d2 14170 h->root.u.def.value = 8;
f5385ebf
AM
14171 h->ref_regular = 1;
14172 h->def_regular = 1;
14173 h->ref_regular_nonweak = 1;
14174 h->forced_local = 1;
14175 h->non_elf = 0;
2ec55de3 14176 h->root.linker_def = 1;
97b639ba
AM
14177 }
14178 }
33e44f2e
AM
14179 plt0 = (htab->elf.splt->output_section->vma
14180 + htab->elf.splt->output_offset
14181 - 16);
176a0d42
AM
14182 if (info->emitrelocations)
14183 {
14184 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14185 if (r == NULL)
14186 return FALSE;
14187 r->r_offset = (htab->glink->output_offset
14188 + htab->glink->output_section->vma);
14189 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14190 r->r_addend = plt0;
14191 }
4ce794b7 14192 p = htab->glink->contents;
176a0d42 14193 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14194 bfd_put_64 (htab->glink->owner, plt0, p);
14195 p += 8;
b9e5796b
AM
14196 if (htab->opd_abi)
14197 {
14198 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14199 p += 4;
14200 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14201 p += 4;
14202 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14203 p += 4;
14204 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14205 p += 4;
14206 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14207 p += 4;
14208 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14209 p += 4;
14210 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14211 p += 4;
14212 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14213 p += 4;
14214 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14215 p += 4;
14216 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14217 p += 4;
14218 }
14219 else
14220 {
14221 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14222 p += 4;
14223 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14224 p += 4;
14225 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14226 p += 4;
f378ab09
AM
14227 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14228 p += 4;
b9e5796b
AM
14229 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14230 p += 4;
14231 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14232 p += 4;
14233 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14234 p += 4;
14235 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14236 p += 4;
14237 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
14238 p += 4;
14239 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14240 p += 4;
14241 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14242 p += 4;
14243 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14244 p += 4;
14245 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14246 p += 4;
14247 }
407aa07c
AM
14248 bfd_put_32 (htab->glink->owner, BCTR, p);
14249 p += 4;
c75bc4f7 14250 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14251
9f951329
AM
14252 /* Build the .glink lazy link call stubs. */
14253 indx = 0;
9e390558 14254 while (p < htab->glink->contents + htab->glink->size)
9f951329 14255 {
b9e5796b 14256 if (htab->opd_abi)
9f951329 14257 {
b9e5796b
AM
14258 if (indx < 0x8000)
14259 {
14260 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14261 p += 4;
14262 }
14263 else
14264 {
14265 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14266 p += 4;
14267 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14268 p);
14269 p += 4;
14270 }
9f951329 14271 }
4ce794b7 14272 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14273 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14274 indx++;
9f951329
AM
14275 p += 4;
14276 }
5d1634d7 14277 }
5d1634d7 14278
49c09209
AM
14279 /* Build .glink global entry stubs, and PLT relocs for globals. */
14280 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14281
14282 if (!write_plt_relocs_for_local_syms (info))
14283 return FALSE;
9e390558 14284
7341d5e2 14285 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 14286 {
4ce794b7 14287 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 14288 htab->brlt->size);
4ce794b7 14289 if (htab->brlt->contents == NULL)
b34976b6 14290 return FALSE;
721956f4 14291 }
ee75fd95 14292 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
14293 {
14294 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 14295 htab->relbrlt->size);
63bc6f6c
AM
14296 if (htab->relbrlt->contents == NULL)
14297 return FALSE;
14298 }
5d1634d7 14299
721956f4
AM
14300 /* Build the stubs as directed by the stub hash table. */
14301 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 14302
a4b6fadd
AM
14303 for (group = htab->group; group != NULL; group = group->next)
14304 if (group->needs_save_res)
7dda8d3c 14305 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 14306
aa8a7074
AM
14307 if (htab->relbrlt != NULL)
14308 htab->relbrlt->reloc_count = 0;
14309
e7d1c40c 14310 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14311 for (group = htab->group; group != NULL; group = group->next)
14312 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
14313 {
14314 int align = abs (htab->params->plt_stub_align);
14315 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14316 }
794e51c0 14317
7dda8d3c
AM
14318 for (group = htab->group; group != NULL; group = group->next)
14319 if (group->needs_save_res)
14320 {
14321 stub_sec = group->stub_sec;
14322 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14323 htab->sfpr->contents, htab->sfpr->size);
14324 if (htab->params->emit_stub_syms)
14325 {
14326 unsigned int i;
14327
14328 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14329 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14330 return FALSE;
14331 }
14332 }
14333
df136d64
AM
14334 if (htab->glink_eh_frame != NULL
14335 && htab->glink_eh_frame->size != 0)
14336 {
14337 bfd_vma val;
14338 size_t align = 4;
14339
14340 p = htab->glink_eh_frame->contents;
14341 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14342
14343 for (group = htab->group; group != NULL; group = group->next)
14344 if (group->eh_size != 0)
14345 {
14346 /* Offset to stub section. */
14347 val = (group->stub_sec->output_section->vma
14348 + group->stub_sec->output_offset);
14349 val -= (htab->glink_eh_frame->output_section->vma
14350 + htab->glink_eh_frame->output_offset
14351 + (p + 8 - htab->glink_eh_frame->contents));
14352 if (val + 0x80000000 > 0xffffffff)
14353 {
14354 _bfd_error_handler
14355 (_("%s offset too large for .eh_frame sdata4 encoding"),
14356 group->stub_sec->name);
14357 return FALSE;
14358 }
14359 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14360 p += (group->eh_size + 17 + 3) & -4;
14361 }
14362 if (htab->glink != NULL && htab->glink->size != 0)
14363 {
14364 /* Offset to .glink. */
14365 val = (htab->glink->output_section->vma
14366 + htab->glink->output_offset
14367 + 8);
14368 val -= (htab->glink_eh_frame->output_section->vma
14369 + htab->glink_eh_frame->output_offset
14370 + (p + 8 - htab->glink_eh_frame->contents));
14371 if (val + 0x80000000 > 0xffffffff)
14372 {
14373 _bfd_error_handler
14374 (_("%s offset too large for .eh_frame sdata4 encoding"),
14375 htab->glink->name);
14376 return FALSE;
14377 }
14378 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14379 p += (24 + align - 1) & -align;
14380 }
14381 }
14382
d4aaa2a0
AM
14383 for (group = htab->group; group != NULL; group = group->next)
14384 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14385 {
14386 stub_sec_count += 1;
c9301e31
AM
14387 if (stub_sec->rawsize != stub_sec->size
14388 && (htab->stub_iteration <= STUB_SHRINK_ITER
14389 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14390 break;
14391 }
5d1634d7 14392
25516cc5 14393 if (group != NULL)
5d1634d7 14394 {
b34976b6 14395 htab->stub_error = TRUE;
cf97bcb0 14396 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14397 }
721956f4 14398
d2a300cf
AM
14399 if (htab->stub_error)
14400 return FALSE;
14401
14402 if (stats != NULL)
14403 {
db84fff3 14404 size_t len;
d2a300cf
AM
14405 *stats = bfd_malloc (500);
14406 if (*stats == NULL)
14407 return FALSE;
14408
db84fff3
AM
14409 len = sprintf (*stats,
14410 ngettext ("linker stubs in %u group\n",
14411 "linker stubs in %u groups\n",
14412 stub_sec_count),
14413 stub_sec_count);
05d0e962
AM
14414 sprintf (*stats + len, _(" branch %lu\n"
14415 " branch toc adj %lu\n"
14416 " branch notoc %lu\n"
14417 " branch both %lu\n"
14418 " long branch %lu\n"
14419 " long toc adj %lu\n"
14420 " long notoc %lu\n"
14421 " long both %lu\n"
14422 " plt call %lu\n"
14423 " plt call save %lu\n"
14424 " plt call notoc %lu\n"
14425 " plt call both %lu\n"
14426 " global entry %lu"),
4ce794b7
AM
14427 htab->stub_count[ppc_stub_long_branch - 1],
14428 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14429 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14430 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14431 htab->stub_count[ppc_stub_plt_branch - 1],
14432 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14433 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14434 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14435 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14436 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14437 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14438 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14439 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14440 }
14441 return TRUE;
5bd4f169
AM
14442}
14443
60124e18
AM
14444/* What to do when ld finds relocations against symbols defined in
14445 discarded sections. */
14446
14447static unsigned int
14448ppc64_elf_action_discarded (asection *sec)
14449{
14450 if (strcmp (".opd", sec->name) == 0)
14451 return 0;
14452
14453 if (strcmp (".toc", sec->name) == 0)
14454 return 0;
14455
bce50a28
JJ
14456 if (strcmp (".toc1", sec->name) == 0)
14457 return 0;
14458
60124e18
AM
14459 return _bfd_elf_default_action_discarded (sec);
14460}
14461
5bd4f169
AM
14462/* The RELOCATE_SECTION function is called by the ELF backend linker
14463 to handle the relocations for a section.
14464
14465 The relocs are always passed as Rela structures; if the section
14466 actually uses Rel structures, the r_addend field will always be
14467 zero.
14468
14469 This function is responsible for adjust the section contents as
14470 necessary, and (if using Rela relocs and generating a
1049f94e 14471 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14472 necessary.
14473
14474 This function does not have to worry about setting the reloc
14475 address or the reloc symbol index.
14476
14477 LOCAL_SYMS is a pointer to the swapped in local symbols.
14478
14479 LOCAL_SECTIONS is an array giving the section in the input file
14480 corresponding to the st_shndx field of each local symbol.
14481
14482 The global hash table entry for the global symbols can be found
14483 via elf_sym_hashes (input_bfd).
14484
1049f94e 14485 When generating relocatable output, this function must handle
5bd4f169
AM
14486 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14487 going to be the section symbol corresponding to the output
14488 section, which means that the addend must be adjusted
14489 accordingly. */
14490
b34976b6 14491static bfd_boolean
4ce794b7
AM
14492ppc64_elf_relocate_section (bfd *output_bfd,
14493 struct bfd_link_info *info,
14494 bfd *input_bfd,
14495 asection *input_section,
14496 bfd_byte *contents,
14497 Elf_Internal_Rela *relocs,
14498 Elf_Internal_Sym *local_syms,
14499 asection **local_sections)
5bd4f169 14500{
65f38f15 14501 struct ppc_link_hash_table *htab;
5bd4f169
AM
14502 Elf_Internal_Shdr *symtab_hdr;
14503 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14504 Elf_Internal_Rela *rel;
c316a17c 14505 Elf_Internal_Rela *wrel;
5bd4f169 14506 Elf_Internal_Rela *relend;
411e1bfb
AM
14507 Elf_Internal_Rela outrel;
14508 bfd_byte *loc;
411e1bfb 14509 struct got_entry **local_got_ents;
5bd4f169 14510 bfd_vma TOCstart;
b34976b6
AM
14511 bfd_boolean ret = TRUE;
14512 bfd_boolean is_opd;
794e51c0
AM
14513 /* Assume 'at' branch hints. */
14514 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 14515 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14516
65f38f15 14517 /* Initialize howto table if needed. */
5bd4f169 14518 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14519 ppc_howto_init ();
14520
65f38f15 14521 htab = ppc_hash_table (info);
4dfe6ac6
NC
14522 if (htab == NULL)
14523 return FALSE;
ee75fd95
AM
14524
14525 /* Don't relocate stub sections. */
e7d1c40c 14526 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14527 return TRUE;
14528
0c8d6e5c 14529 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 14530
411e1bfb 14531 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14532 TOCstart = elf_gp (output_bfd);
0ffa91dd 14533 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14534 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14535 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14536
c316a17c 14537 rel = wrel = relocs;
5bd4f169 14538 relend = relocs + input_section->reloc_count;
c316a17c 14539 for (; rel < relend; wrel++, rel++)
5bd4f169 14540 {
04c9666a 14541 enum elf_ppc64_reloc_type r_type;
31c76678 14542 bfd_vma addend;
5bd4f169
AM
14543 bfd_reloc_status_type r;
14544 Elf_Internal_Sym *sym;
14545 asection *sec;
039b3fef
AM
14546 struct elf_link_hash_entry *h_elf;
14547 struct ppc_link_hash_entry *h;
14548 struct ppc_link_hash_entry *fdh;
5bd4f169 14549 const char *sym_name;
0d4792f7 14550 unsigned long r_symndx, toc_symndx;
3a71aa26 14551 bfd_vma toc_addend;
f961d9dd
AM
14552 unsigned char tls_mask, tls_gd, tls_type;
14553 unsigned char sym_type;
5bd4f169 14554 bfd_vma relocation;
23cedd1d 14555 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14556 bfd_boolean warned;
bc30df16 14557 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14558 unsigned int insn;
e11840f9 14559 unsigned int mask;
721956f4
AM
14560 struct ppc_stub_hash_entry *stub_entry;
14561 bfd_vma max_br_offset;
14562 bfd_vma from;
c316a17c 14563 Elf_Internal_Rela orig_rel;
b80eed39
AM
14564 reloc_howto_type *howto;
14565 struct reloc_howto_struct alt_howto;
5bd4f169 14566
c316a17c
AM
14567 again:
14568 orig_rel = *rel;
14569
4ce794b7 14570 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14571 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14572
14573 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14574 symbol of the previous ADDR64 reloc. The symbol gives us the
14575 proper TOC base to use. */
14576 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14577 && wrel != relocs
14578 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14579 && is_opd)
c316a17c 14580 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14581
4ce794b7
AM
14582 sym = NULL;
14583 sec = NULL;
039b3fef 14584 h_elf = NULL;
4ce794b7 14585 sym_name = NULL;
b34976b6
AM
14586 unresolved_reloc = FALSE;
14587 warned = FALSE;
65f38f15 14588
0b13192e 14589 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14590 {
14591 /* It's a local symbol. */
74f0fb50 14592 struct _opd_sec_data *opd;
4025353c 14593
5bd4f169
AM
14594 sym = local_syms + r_symndx;
14595 sec = local_sections[r_symndx];
26c61ae5 14596 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14597 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14598 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14599 opd = get_opd_info (sec);
14600 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14601 {
51aecdc5
AM
14602 long adjust = opd->adjust[OPD_NDX (sym->st_value
14603 + rel->r_addend)];
4025353c
AM
14604 if (adjust == -1)
14605 relocation = 0;
14606 else
4cc603a5
AM
14607 {
14608 /* If this is a relocation against the opd section sym
14609 and we have edited .opd, adjust the reloc addend so
14610 that ld -r and ld --emit-relocs output is correct.
14611 If it is a reloc against some other .opd symbol,
14612 then the symbol value will be adjusted later. */
14613 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14614 rel->r_addend += adjust;
14615 else
14616 relocation += adjust;
14617 }
1e2f5b6e 14618 }
5bd4f169
AM
14619 }
14620 else
14621 {
62d887d4
L
14622 bfd_boolean ignored;
14623
b2a8e766
AM
14624 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14625 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14626 h_elf, sec, relocation,
62d887d4 14627 unresolved_reloc, warned, ignored);
039b3fef
AM
14628 sym_name = h_elf->root.root.string;
14629 sym_type = h_elf->type;
b69fdb4e
AM
14630 if (sec != NULL
14631 && sec->owner == output_bfd
14632 && strcmp (sec->name, ".opd") == 0)
14633 {
14634 /* This is a symbol defined in a linker script. All
14635 such are defined in output sections, even those
14636 defined by simple assignment from a symbol defined in
14637 an input section. Transfer the symbol to an
14638 appropriate input .opd section, so that a branch to
14639 this symbol will be mapped to the location specified
14640 by the opd entry. */
14641 struct bfd_link_order *lo;
14642 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14643 if (lo->type == bfd_indirect_link_order)
14644 {
14645 asection *isec = lo->u.indirect.section;
14646 if (h_elf->root.u.def.value >= isec->output_offset
14647 && h_elf->root.u.def.value < (isec->output_offset
14648 + isec->size))
14649 {
14650 h_elf->root.u.def.value -= isec->output_offset;
14651 h_elf->root.u.def.section = isec;
14652 sec = isec;
14653 break;
14654 }
14655 }
14656 }
5bd4f169 14657 }
039b3fef 14658 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14659
dbaa2011 14660 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14661 {
14662 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14663 input_bfd, input_section,
14664 contents + rel->r_offset);
14665 wrel->r_offset = rel->r_offset;
14666 wrel->r_info = 0;
14667 wrel->r_addend = 0;
14668
14669 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14670 symbols defined in discarded sections. Not done for
c316a17c
AM
14671 non-debug to preserve relocs in .eh_frame which the
14672 eh_frame editing code expects to be present. */
14673 if (bfd_link_relocatable (info)
14674 && (input_section->flags & SEC_DEBUGGING))
14675 wrel--;
14676
14677 continue;
14678 }
ab96bf03 14679
0e1862bb 14680 if (bfd_link_relocatable (info))
c316a17c 14681 goto copy_reloc;
ab96bf03 14682
f40da81b
AM
14683 if (h != NULL && &h->elf == htab->elf.hgot)
14684 {
6f20ed8a 14685 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14686 sec = bfd_abs_section_ptr;
14687 unresolved_reloc = FALSE;
14688 }
14689
951fd09b
AM
14690 /* TLS optimizations. Replace instruction sequences and relocs
14691 based on information we collected in tls_optimize. We edit
14692 RELOCS so that --emit-relocs will output something sensible
14693 for the final instruction stream. */
14694 tls_mask = 0;
14695 tls_gd = 0;
0d4792f7 14696 toc_symndx = 0;
727fc41e
AM
14697 if (h != NULL)
14698 tls_mask = h->tls_mask;
14699 else if (local_got_ents != NULL)
411e1bfb 14700 {
e054468f
AM
14701 struct plt_entry **local_plt = (struct plt_entry **)
14702 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14703 unsigned char *lgot_masks = (unsigned char *)
e054468f 14704 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14705 tls_mask = lgot_masks[r_symndx];
14706 }
37da22e5 14707 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14708 && (r_type == R_PPC64_TLS
14709 || r_type == R_PPC64_TLSGD
14710 || r_type == R_PPC64_TLSLD))
14711 {
14712 /* Check for toc tls entries. */
f961d9dd 14713 unsigned char *toc_tls;
0d4792f7 14714
727fc41e
AM
14715 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14716 &local_syms, rel, input_bfd))
14717 return FALSE;
0d4792f7 14718
727fc41e
AM
14719 if (toc_tls)
14720 tls_mask = *toc_tls;
0d4792f7
AM
14721 }
14722
14723 /* Check that tls relocs are used with tls syms, and non-tls
14724 relocs are used with non-tls syms. */
cf35638d 14725 if (r_symndx != STN_UNDEF
0d4792f7
AM
14726 && r_type != R_PPC64_NONE
14727 && (h == NULL
039b3fef
AM
14728 || h->elf.root.type == bfd_link_hash_defined
14729 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
14730 && (IS_PPC64_TLS_RELOC (r_type)
14731 != (sym_type == STT_TLS
14732 || (sym_type == STT_SECTION
14733 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 14734 {
37da22e5 14735 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14736 && (r_type == R_PPC64_TLS
14737 || r_type == R_PPC64_TLSGD
14738 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14739 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14740 ;
14741 else
25f53a85 14742 info->callbacks->einfo
1d483afe 14743 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14744 /* xgettext:c-format */
c1c8c1ef 14745 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14746 /* xgettext:c-format */
c1c8c1ef 14747 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14748 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14749 ppc64_elf_howto_table[r_type]->name,
14750 sym_name);
411e1bfb
AM
14751 }
14752
14753 /* Ensure reloc mapping code below stays sane. */
14754 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14755 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14756 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14757 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14758 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14759 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14760 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14761 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14762 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14763 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14764 abort ();
0d4792f7 14765
411e1bfb
AM
14766 switch (r_type)
14767 {
14768 default:
411e1bfb
AM
14769 break;
14770
ba761f19 14771 case R_PPC64_LO_DS_OPT:
95f0d0d2 14772 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14773 if ((insn & (0x3f << 26)) != 58u << 26)
14774 abort ();
14775 insn += (14u << 26) - (58u << 26);
95f0d0d2 14776 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14777 r_type = R_PPC64_TOC16_LO;
14778 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14779 break;
14780
411e1bfb
AM
14781 case R_PPC64_TOC16:
14782 case R_PPC64_TOC16_LO:
14783 case R_PPC64_TOC16_DS:
14784 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14785 {
14786 /* Check for toc tls entries. */
f961d9dd 14787 unsigned char *toc_tls;
951fd09b 14788 int retval;
411e1bfb 14789
3a71aa26
AM
14790 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14791 &local_syms, rel, input_bfd);
951fd09b 14792 if (retval == 0)
411e1bfb
AM
14793 return FALSE;
14794
14795 if (toc_tls)
14796 {
951fd09b 14797 tls_mask = *toc_tls;
411e1bfb
AM
14798 if (r_type == R_PPC64_TOC16_DS
14799 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14800 {
37da22e5 14801 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14802 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14803 goto toctprel;
14804 }
411e1bfb 14805 else
951fd09b
AM
14806 {
14807 /* If we found a GD reloc pair, then we might be
14808 doing a GD->IE transition. */
14809 if (retval == 2)
14810 {
14811 tls_gd = TLS_TPRELGD;
37da22e5
AM
14812 if ((tls_mask & TLS_TLS) != 0
14813 && (tls_mask & TLS_GD) == 0)
102890f0 14814 goto tls_ldgd_opt;
951fd09b
AM
14815 }
14816 else if (retval == 3)
14817 {
37da22e5
AM
14818 if ((tls_mask & TLS_TLS) != 0
14819 && (tls_mask & TLS_LD) == 0)
102890f0 14820 goto tls_ldgd_opt;
951fd09b
AM
14821 }
14822 }
411e1bfb
AM
14823 }
14824 }
14825 break;
14826
9d6ded02
AM
14827 case R_PPC64_GOT_TPREL16_HI:
14828 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14829 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14830 && (tls_mask & TLS_TPREL) == 0)
14831 {
14832 rel->r_offset -= d_offset;
95f0d0d2 14833 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14834 r_type = R_PPC64_NONE;
14835 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14836 }
14837 break;
14838
411e1bfb
AM
14839 case R_PPC64_GOT_TPREL16_DS:
14840 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14841 if ((tls_mask & TLS_TLS) != 0
951fd09b 14842 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14843 {
81407a69 14844 toctprel:
95f0d0d2 14845 insn = bfd_get_32 (input_bfd,
c316a17c 14846 contents + rel->r_offset - d_offset);
411e1bfb
AM
14847 insn &= 31 << 21;
14848 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14849 bfd_put_32 (input_bfd, insn,
c316a17c 14850 contents + rel->r_offset - d_offset);
411e1bfb 14851 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14852 if (toc_symndx != 0)
14853 {
14854 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14855 rel->r_addend = toc_addend;
0d4792f7
AM
14856 /* We changed the symbol. Start over in order to
14857 get h, sym, sec etc. right. */
c316a17c 14858 goto again;
0d4792f7
AM
14859 }
14860 else
14861 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14862 }
14863 break;
14864
14865 case R_PPC64_TLS:
37da22e5 14866 if ((tls_mask & TLS_TLS) != 0
951fd09b 14867 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14868 {
95f0d0d2 14869 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
14870 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14871 if (insn == 0)
411e1bfb 14872 abort ();
95f0d0d2 14873 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 14874 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
14875 PPC64_TPREL16_LO which is at low-order half-word. */
14876 rel->r_offset += d_offset;
0d4792f7
AM
14877 r_type = R_PPC64_TPREL16_LO;
14878 if (toc_symndx != 0)
14879 {
14880 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14881 rel->r_addend = toc_addend;
0d4792f7
AM
14882 /* We changed the symbol. Start over in order to
14883 get h, sym, sec etc. right. */
c316a17c 14884 goto again;
0d4792f7
AM
14885 }
14886 else
14887 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14888 }
14889 break;
14890
411e1bfb
AM
14891 case R_PPC64_GOT_TLSGD16_HI:
14892 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14893 tls_gd = TLS_TPRELGD;
37da22e5 14894 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14895 goto tls_gdld_hi;
14896 break;
14897
411e1bfb
AM
14898 case R_PPC64_GOT_TLSLD16_HI:
14899 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14900 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14901 {
951fd09b
AM
14902 tls_gdld_hi:
14903 if ((tls_mask & tls_gd) != 0)
14904 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14905 + R_PPC64_GOT_TPREL16_DS);
14906 else
411e1bfb 14907 {
4fe5ca5b 14908 rel->r_offset -= d_offset;
95f0d0d2 14909 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14910 r_type = R_PPC64_NONE;
411e1bfb 14911 }
951fd09b 14912 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14913 }
14914 break;
14915
951fd09b
AM
14916 case R_PPC64_GOT_TLSGD16:
14917 case R_PPC64_GOT_TLSGD16_LO:
14918 tls_gd = TLS_TPRELGD;
37da22e5 14919 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14920 goto tls_ldgd_opt;
951fd09b 14921 break;
411e1bfb 14922
951fd09b
AM
14923 case R_PPC64_GOT_TLSLD16:
14924 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14925 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14926 {
b9f04fe0 14927 unsigned int insn1, insn2;
102890f0
AM
14928 bfd_vma offset;
14929
14930 tls_ldgd_opt:
727fc41e
AM
14931 offset = (bfd_vma) -1;
14932 /* If not using the newer R_PPC64_TLSGD/LD to mark
14933 __tls_get_addr calls, we must trust that the call
14934 stays with its arg setup insns, ie. that the next
14935 reloc is the __tls_get_addr call associated with
14936 the current reloc. Edit both insns. */
14937 if (input_section->has_tls_get_addr_call
14938 && rel + 1 < relend
14939 && branch_reloc_hash_match (input_bfd, rel + 1,
14940 htab->tls_get_addr,
14941 htab->tls_get_addr_fd))
14942 offset = rel[1].r_offset;
b86ac8e3
AM
14943 /* We read the low GOT_TLS (or TOC16) insn because we
14944 need to keep the destination reg. It may be
14945 something other than the usual r3, and moved to r3
14946 before the call by intervening code. */
95f0d0d2 14947 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14948 contents + rel->r_offset - d_offset);
102890f0 14949 if ((tls_mask & tls_gd) != 0)
411e1bfb 14950 {
102890f0 14951 /* IE */
b86ac8e3 14952 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14953 insn1 |= 58 << 26; /* ld */
14954 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14955 if (offset != (bfd_vma) -1)
f58d5a2d 14956 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
14957 if ((tls_mask & TLS_EXPLICIT) == 0)
14958 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14959 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 14960 else
102890f0
AM
14961 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14962 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14963 }
14964 else
14965 {
14966 /* LE */
b86ac8e3
AM
14967 insn1 &= 0x1f << 21;
14968 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14969 insn2 = 0x38630000; /* addi 3,3,0 */
14970 if (tls_gd == 0)
951fd09b 14971 {
102890f0 14972 /* Was an LD reloc. */
1d483afe
AM
14973 if (toc_symndx)
14974 sec = local_sections[toc_symndx];
14975 for (r_symndx = 0;
14976 r_symndx < symtab_hdr->sh_info;
14977 r_symndx++)
14978 if (local_sections[r_symndx] == sec)
14979 break;
14980 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14981 r_symndx = STN_UNDEF;
102890f0 14982 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14983 if (r_symndx != STN_UNDEF)
1d483afe
AM
14984 rel->r_addend -= (local_syms[r_symndx].st_value
14985 + sec->output_offset
14986 + sec->output_section->vma);
951fd09b 14987 }
102890f0 14988 else if (toc_symndx != 0)
3a71aa26
AM
14989 {
14990 r_symndx = toc_symndx;
14991 rel->r_addend = toc_addend;
14992 }
102890f0
AM
14993 r_type = R_PPC64_TPREL16_HA;
14994 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14995 if (offset != (bfd_vma) -1)
14996 {
14997 rel[1].r_info = ELF64_R_INFO (r_symndx,
14998 R_PPC64_TPREL16_LO);
14999 rel[1].r_offset = offset + d_offset;
15000 rel[1].r_addend = rel->r_addend;
15001 }
102890f0 15002 }
95f0d0d2 15003 bfd_put_32 (input_bfd, insn1,
3a71aa26 15004 contents + rel->r_offset - d_offset);
727fc41e 15005 if (offset != (bfd_vma) -1)
b9f04fe0 15006 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
15007 if ((tls_mask & tls_gd) == 0
15008 && (tls_gd == 0 || toc_symndx != 0))
15009 {
15010 /* We changed the symbol. Start over in order
15011 to get h, sym, sec etc. right. */
c316a17c 15012 goto again;
727fc41e
AM
15013 }
15014 }
15015 break;
15016
15017 case R_PPC64_TLSGD:
37da22e5 15018 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 15019 && rel + 1 < relend)
727fc41e 15020 {
b9f04fe0 15021 unsigned int insn2;
727fc41e
AM
15022 bfd_vma offset = rel->r_offset;
15023
23cedd1d
AM
15024 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
15025 {
15026 bfd_put_32 (output_bfd, NOP, contents + offset);
15027 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15028 break;
15029 }
15030
15031 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15032 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15033
727fc41e
AM
15034 if ((tls_mask & TLS_TPRELGD) != 0)
15035 {
15036 /* IE */
15037 r_type = R_PPC64_NONE;
15038 insn2 = 0x7c636a14; /* add 3,3,13 */
15039 }
15040 else
15041 {
15042 /* LE */
15043 if (toc_symndx != 0)
15044 {
15045 r_symndx = toc_symndx;
15046 rel->r_addend = toc_addend;
15047 }
15048 r_type = R_PPC64_TPREL16_LO;
15049 rel->r_offset = offset + d_offset;
15050 insn2 = 0x38630000; /* addi 3,3,0 */
15051 }
15052 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15053 /* Zap the reloc on the _tls_get_addr call too. */
15054 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15055 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15056 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 15057 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 15058 goto again;
411e1bfb 15059 }
411e1bfb
AM
15060 break;
15061
727fc41e 15062 case R_PPC64_TLSLD:
37da22e5 15063 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 15064 && rel + 1 < relend)
727fc41e 15065 {
b9f04fe0 15066 unsigned int insn2;
727fc41e
AM
15067 bfd_vma offset = rel->r_offset;
15068
23cedd1d
AM
15069 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
15070 {
15071 bfd_put_32 (output_bfd, NOP, contents + offset);
15072 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15073 break;
15074 }
15075
15076 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15077 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15078
727fc41e
AM
15079 if (toc_symndx)
15080 sec = local_sections[toc_symndx];
15081 for (r_symndx = 0;
15082 r_symndx < symtab_hdr->sh_info;
15083 r_symndx++)
15084 if (local_sections[r_symndx] == sec)
15085 break;
15086 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 15087 r_symndx = STN_UNDEF;
727fc41e 15088 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 15089 if (r_symndx != STN_UNDEF)
727fc41e
AM
15090 rel->r_addend -= (local_syms[r_symndx].st_value
15091 + sec->output_offset
15092 + sec->output_section->vma);
15093
15094 r_type = R_PPC64_TPREL16_LO;
15095 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15096 rel->r_offset = offset + d_offset;
15097 /* Zap the reloc on the _tls_get_addr call too. */
15098 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15099 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 15100 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 15101 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 15102 goto again;
727fc41e
AM
15103 }
15104 break;
15105
411e1bfb 15106 case R_PPC64_DTPMOD64:
951fd09b
AM
15107 if (rel + 1 < relend
15108 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15109 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 15110 {
951fd09b
AM
15111 if ((tls_mask & TLS_GD) == 0)
15112 {
15113 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
15114 if ((tls_mask & TLS_TPRELGD) != 0)
15115 r_type = R_PPC64_TPREL64;
15116 else
15117 {
4ce794b7 15118 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
15119 r_type = R_PPC64_NONE;
15120 }
15121 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15122 }
15123 }
15124 else
15125 {
15126 if ((tls_mask & TLS_LD) == 0)
411e1bfb 15127 {
4ce794b7 15128 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 15129 r_type = R_PPC64_NONE;
951fd09b 15130 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 15131 }
411e1bfb
AM
15132 }
15133 break;
15134
15135 case R_PPC64_TPREL64:
951fd09b 15136 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
15137 {
15138 r_type = R_PPC64_NONE;
15139 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15140 }
15141 break;
52a82034 15142
006589cf
AM
15143 case R_PPC64_ENTRY:
15144 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15145 if (!bfd_link_pic (info)
15146 && !info->traditional_format
15147 && relocation + 0x80008000 <= 0xffffffff)
15148 {
15149 unsigned int insn1, insn2;
15150
15151 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15152 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15153 if ((insn1 & ~0xfffc) == LD_R2_0R12
15154 && insn2 == ADD_R2_R2_R12)
15155 {
95f0d0d2 15156 bfd_put_32 (input_bfd,
006589cf
AM
15157 LIS_R2 + PPC_HA (relocation),
15158 contents + rel->r_offset);
95f0d0d2 15159 bfd_put_32 (input_bfd,
006589cf
AM
15160 ADDI_R2_R2 + PPC_LO (relocation),
15161 contents + rel->r_offset + 4);
15162 }
15163 }
15164 else
15165 {
15166 relocation -= (rel->r_offset
15167 + input_section->output_offset
15168 + input_section->output_section->vma);
15169 if (relocation + 0x80008000 <= 0xffffffff)
15170 {
15171 unsigned int insn1, insn2;
15172
15173 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15174 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15175 if ((insn1 & ~0xfffc) == LD_R2_0R12
15176 && insn2 == ADD_R2_R2_R12)
15177 {
95f0d0d2 15178 bfd_put_32 (input_bfd,
006589cf
AM
15179 ADDIS_R2_R12 + PPC_HA (relocation),
15180 contents + rel->r_offset);
95f0d0d2 15181 bfd_put_32 (input_bfd,
006589cf
AM
15182 ADDI_R2_R2 + PPC_LO (relocation),
15183 contents + rel->r_offset + 4);
15184 }
15185 }
15186 }
15187 break;
15188
52a82034
AM
15189 case R_PPC64_REL16_HA:
15190 /* If we are generating a non-PIC executable, edit
15191 . 0: addis 2,12,.TOC.-0b@ha
15192 . addi 2,2,.TOC.-0b@l
15193 used by ELFv2 global entry points to set up r2, to
15194 . lis 2,.TOC.@ha
15195 . addi 2,2,.TOC.@l
15196 if .TOC. is in range. */
0e1862bb 15197 if (!bfd_link_pic (info)
810d4e75 15198 && !info->traditional_format
006589cf 15199 && !htab->opd_abi
4f038ee5 15200 && rel->r_addend == d_offset
52a82034
AM
15201 && h != NULL && &h->elf == htab->elf.hgot
15202 && rel + 1 < relend
15203 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15204 && rel[1].r_offset == rel->r_offset + 4
15205 && rel[1].r_addend == rel->r_addend + 4
15206 && relocation + 0x80008000 <= 0xffffffff)
15207 {
15208 unsigned int insn1, insn2;
15209 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
15210 insn1 = bfd_get_32 (input_bfd, contents + offset);
15211 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15212 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15213 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15214 {
15215 r_type = R_PPC64_ADDR16_HA;
15216 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15217 rel->r_addend -= d_offset;
15218 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15219 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15220 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15221 }
15222 }
15223 break;
411e1bfb
AM
15224 }
15225
15226 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15227 insn = 0;
b25116a9
AM
15228 max_br_offset = 1 << 25;
15229 addend = rel->r_addend;
bc30df16 15230 reloc_dest = DEST_NORMAL;
65f38f15 15231 switch (r_type)
5bd4f169
AM
15232 {
15233 default:
65f38f15 15234 break;
5bd4f169 15235
3b421ab3
AM
15236 case R_PPC64_TOCSAVE:
15237 if (relocation + addend == (rel->r_offset
15238 + input_section->output_offset
15239 + input_section->output_section->vma)
15240 && tocsave_find (htab, NO_INSERT,
15241 &local_syms, rel, input_bfd))
15242 {
15243 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15244 if (insn == NOP
15245 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15246 bfd_put_32 (input_bfd,
15247 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15248 contents + rel->r_offset);
15249 }
15250 break;
15251
65f38f15
AM
15252 /* Branch taken prediction relocations. */
15253 case R_PPC64_ADDR14_BRTAKEN:
15254 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15255 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15256 /* Fall through. */
65f38f15 15257
86c76c7b 15258 /* Branch not taken prediction relocations. */
65f38f15
AM
15259 case R_PPC64_ADDR14_BRNTAKEN:
15260 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15261 insn |= bfd_get_32 (input_bfd,
411e1bfb 15262 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15263 /* Fall through. */
86c76c7b 15264
b25116a9
AM
15265 case R_PPC64_REL14:
15266 max_br_offset = 1 << 15;
1a0670f3 15267 /* Fall through. */
5bd4f169 15268
65f38f15 15269 case R_PPC64_REL24:
05d0e962 15270 case R_PPC64_REL24_NOTOC:
23cedd1d 15271 case R_PPC64_PLTCALL:
ad8e1ba5
AM
15272 /* Calls to functions with a different TOC, such as calls to
15273 shared objects, need to alter the TOC pointer. This is
15274 done using a linkage stub. A REL24 branching to these
15275 linkage stubs needs to be followed by a nop, as the nop
15276 will be replaced with an instruction to restore the TOC
15277 base pointer. */
8387904d 15278 fdh = h;
b31867b6
AM
15279 if (h != NULL
15280 && h->oh != NULL
15281 && h->oh->is_func_descriptor)
15282 fdh = ppc_follow_link (h->oh);
31c76678
DK
15283 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15284 htab);
23cedd1d
AM
15285 if (r_type == R_PPC64_PLTCALL
15286 && stub_entry != NULL
05d0e962
AM
15287 && stub_entry->stub_type >= ppc_stub_plt_call
15288 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15289 stub_entry = NULL;
15290
6abec6bc 15291 if (stub_entry != NULL
ad8e1ba5 15292 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 15293 || stub_entry->stub_type == ppc_stub_plt_call_r2save
05d0e962 15294 || stub_entry->stub_type == ppc_stub_plt_call_both
ad8e1ba5 15295 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15296 || stub_entry->stub_type == ppc_stub_plt_branch_both
15297 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15298 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15299 {
b25116a9 15300 bfd_boolean can_plt_call = FALSE;
721956f4 15301
6e1816be
AM
15302 if (stub_entry->stub_type == ppc_stub_plt_call
15303 && !htab->opd_abi
15304 && htab->params->plt_localentry0 != 0
15305 && is_elfv2_localentry0 (&h->elf))
15306 {
15307 /* The function doesn't use or change r2. */
15308 can_plt_call = TRUE;
15309 }
05d0e962
AM
15310 else if (r_type == R_PPC64_REL24_NOTOC)
15311 {
15312 /* NOTOC calls don't need to restore r2. */
15313 can_plt_call = TRUE;
15314 }
6e1816be 15315
f378ab09 15316 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15317 branch and link followed by a nop. The nop is
15318 replaced by an insn to restore r2. */
6e1816be 15319 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15320 {
ba8ca3e7
AM
15321 unsigned long br;
15322
15323 br = bfd_get_32 (input_bfd,
15324 contents + rel->r_offset);
15325 if ((br & 1) != 0)
41bd81ab 15326 {
ba8ca3e7
AM
15327 unsigned long nop;
15328
15329 nop = bfd_get_32 (input_bfd,
15330 contents + rel->r_offset + 4);
23cedd1d
AM
15331 if (nop == LD_R2_0R1 + STK_TOC (htab))
15332 can_plt_call = TRUE;
15333 else if (nop == NOP
15334 || nop == CROR_151515
15335 || nop == CROR_313131)
a7f2871e 15336 {
ba8ca3e7
AM
15337 if (h != NULL
15338 && (h == htab->tls_get_addr_fd
15339 || h == htab->tls_get_addr)
7c9cf415 15340 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15341 {
15342 /* Special stub used, leave nop alone. */
15343 }
15344 else
a078d95a
AM
15345 bfd_put_32 (input_bfd,
15346 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15347 contents + rel->r_offset + 4);
15348 can_plt_call = TRUE;
a7f2871e 15349 }
41bd81ab 15350 }
5bd4f169 15351 }
721956f4 15352
ba8ca3e7 15353 if (!can_plt_call && h != NULL)
721956f4 15354 {
ba8ca3e7
AM
15355 const char *name = h->elf.root.root.string;
15356
15357 if (*name == '.')
15358 ++name;
15359
15360 if (strncmp (name, "__libc_start_main", 17) == 0
15361 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15362 {
ba8ca3e7
AM
15363 /* Allow crt1 branch to go via a toc adjusting
15364 stub. Other calls that never return could do
15365 the same, if we could detect such. */
b25116a9 15366 can_plt_call = TRUE;
6ab189d5 15367 }
ba8ca3e7
AM
15368 }
15369
15370 if (!can_plt_call)
15371 {
15372 /* g++ as of 20130507 emits self-calls without a
15373 following nop. This is arguably wrong since we
15374 have conflicting information. On the one hand a
15375 global symbol and on the other a local call
15376 sequence, but don't error for this special case.
15377 It isn't possible to cheaply verify we have
15378 exactly such a call. Allow all calls to the same
15379 section. */
15380 asection *code_sec = sec;
15381
15382 if (get_opd_info (sec) != NULL)
ad8e1ba5 15383 {
ba8ca3e7
AM
15384 bfd_vma off = (relocation + addend
15385 - sec->output_section->vma
15386 - sec->output_offset);
bc30df16 15387
ba8ca3e7 15388 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15389 }
ba8ca3e7
AM
15390 if (code_sec == input_section)
15391 can_plt_call = TRUE;
15392 }
15393
15394 if (!can_plt_call)
15395 {
05d0e962
AM
15396 if (stub_entry->stub_type >= ppc_stub_plt_call
15397 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15398 info->callbacks->einfo
695344c0 15399 /* xgettext:c-format */
c1c8c1ef 15400 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
15401 "recompile with -fPIC\n"),
15402 input_bfd, input_section, rel->r_offset, sym_name);
15403 else
15404 info->callbacks->einfo
695344c0 15405 /* xgettext:c-format */
c1c8c1ef 15406 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
15407 "(-mcmodel=small toc adjust stub)\n"),
15408 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15409
15410 bfd_set_error (bfd_error_bad_value);
15411 ret = FALSE;
721956f4
AM
15412 }
15413
b25116a9 15414 if (can_plt_call
05d0e962
AM
15415 && stub_entry->stub_type >= ppc_stub_plt_call
15416 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15417 unresolved_reloc = FALSE;
15418 }
15419
6abec6bc
AM
15420 if ((stub_entry == NULL
15421 || stub_entry->stub_type == ppc_stub_long_branch
15422 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15423 && get_opd_info (sec) != NULL)
15424 {
15425 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15426 bfd_vma off = (relocation + addend
15427 - sec->output_section->vma
15428 - sec->output_offset);
aef36ac1 15429 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15430 if (dest != (bfd_vma) -1)
15431 {
15432 relocation = dest;
15433 addend = 0;
bc30df16 15434 reloc_dest = DEST_OPD;
8387904d
AM
15435 }
15436 }
15437
b25116a9
AM
15438 /* If the branch is out of reach we ought to have a long
15439 branch stub. */
15440 from = (rel->r_offset
15441 + input_section->output_offset
15442 + input_section->output_section->vma);
15443
6911b7dc
AM
15444 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15445 ? fdh->elf.other
15446 : sym->st_other);
15447
6abec6bc
AM
15448 if (stub_entry != NULL
15449 && (stub_entry->stub_type == ppc_stub_long_branch
15450 || stub_entry->stub_type == ppc_stub_plt_branch)
15451 && (r_type == R_PPC64_ADDR14_BRTAKEN
15452 || r_type == R_PPC64_ADDR14_BRNTAKEN
15453 || (relocation + addend - from + max_br_offset
15454 < 2 * max_br_offset)))
15455 /* Don't use the stub if this branch is in range. */
15456 stub_entry = NULL;
b25116a9 15457
05d0e962
AM
15458 if (stub_entry != NULL
15459 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15460 || stub_entry->stub_type == ppc_stub_long_branch_both
15461 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15462 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15463 && (r_type != R_PPC64_REL24_NOTOC
15464 || ((fdh ? fdh->elf.other : sym->st_other)
15465 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
15466 && (relocation + addend - from + max_br_offset
15467 < 2 * max_br_offset))
15468 stub_entry = NULL;
15469
15470 if (stub_entry != NULL
15471 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15472 || stub_entry->stub_type == ppc_stub_long_branch_both
15473 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15474 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15475 && r_type == R_PPC64_REL24_NOTOC
15476 && (relocation + addend - from + max_br_offset
15477 < 2 * max_br_offset))
15478 stub_entry = NULL;
15479
b25116a9
AM
15480 if (stub_entry != NULL)
15481 {
15482 /* Munge up the value and addend so that we call the stub
15483 rather than the procedure directly. */
a4b6fadd
AM
15484 asection *stub_sec = stub_entry->group->stub_sec;
15485
15486 if (stub_entry->stub_type == ppc_stub_save_res)
15487 relocation += (stub_sec->output_offset
15488 + stub_sec->output_section->vma
15489 + stub_sec->size - htab->sfpr->size
15490 - htab->sfpr->output_offset
15491 - htab->sfpr->output_section->vma);
15492 else
15493 relocation = (stub_entry->stub_offset
15494 + stub_sec->output_offset
15495 + stub_sec->output_section->vma);
b25116a9 15496 addend = 0;
bc30df16 15497 reloc_dest = DEST_STUB;
3b421ab3 15498
05d0e962
AM
15499 if (((stub_entry->stub_type == ppc_stub_plt_call
15500 && ALWAYS_EMIT_R2SAVE)
15501 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15502 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
15503 && !(h != NULL
15504 && (h == htab->tls_get_addr_fd
15505 || h == htab->tls_get_addr)
15506 && htab->params->tls_get_addr_opt)
3b421ab3
AM
15507 && rel + 1 < relend
15508 && rel[1].r_offset == rel->r_offset + 4
15509 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15510 relocation += 4;
05d0e962
AM
15511 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15512 || stub_entry->stub_type == ppc_stub_plt_branch_both
15513 || stub_entry->stub_type == ppc_stub_plt_call_both)
15514 && r_type == R_PPC64_REL24_NOTOC)
15515 relocation += 4;
b25116a9
AM
15516 }
15517
15518 if (insn != 0)
15519 {
794e51c0 15520 if (is_isa_v2)
721956f4 15521 {
b25116a9
AM
15522 /* Set 'a' bit. This is 0b00010 in BO field for branch
15523 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15524 for branch on CTR insns (BO == 1a00t or 1a01t). */
15525 if ((insn & (0x14 << 21)) == (0x04 << 21))
15526 insn |= 0x02 << 21;
15527 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15528 insn |= 0x08 << 21;
15529 else
15530 break;
15531 }
15532 else
15533 {
15534 /* Invert 'y' bit if not the default. */
4cc603a5 15535 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15536 insn ^= 0x01 << 21;
721956f4 15537 }
b25116a9 15538
95f0d0d2 15539 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15540 }
e86ce104 15541
06da1e8e
AM
15542 /* NOP out calls to undefined weak functions.
15543 We can thus call a weak function without first
15544 checking whether the function is defined. */
b25116a9 15545 else if (h != NULL
039b3fef 15546 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15547 && h->elf.dynindx == -1
05d0e962
AM
15548 && (r_type == R_PPC64_REL24
15549 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15550 && relocation == 0
4cc603a5 15551 && addend == 0)
e86ce104 15552 {
95f0d0d2 15553 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15554 goto copy_reloc;
e86ce104 15555 }
65f38f15
AM
15556 break;
15557 }
5bd4f169 15558
65f38f15 15559 /* Set `addend'. */
411e1bfb 15560 tls_type = 0;
23cedd1d 15561 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15562 switch (r_type)
15563 {
15564 default:
cf97bcb0
AM
15565 /* xgettext:c-format */
15566 _bfd_error_handler (_("%pB: %s unsupported"),
15567 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15568
65f38f15 15569 bfd_set_error (bfd_error_bad_value);
b34976b6 15570 ret = FALSE;
c316a17c 15571 goto copy_reloc;
5bd4f169 15572
65f38f15 15573 case R_PPC64_NONE:
411e1bfb 15574 case R_PPC64_TLS:
727fc41e
AM
15575 case R_PPC64_TLSGD:
15576 case R_PPC64_TLSLD:
3b421ab3 15577 case R_PPC64_TOCSAVE:
04c9666a
AM
15578 case R_PPC64_GNU_VTINHERIT:
15579 case R_PPC64_GNU_VTENTRY:
006589cf 15580 case R_PPC64_ENTRY:
c316a17c 15581 goto copy_reloc;
5bd4f169
AM
15582
15583 /* GOT16 relocations. Like an ADDR16 using the symbol's
15584 address in the GOT as relocation value instead of the
411e1bfb 15585 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15586 symbol and put the symbol value there. */
411e1bfb
AM
15587 case R_PPC64_GOT_TLSGD16:
15588 case R_PPC64_GOT_TLSGD16_LO:
15589 case R_PPC64_GOT_TLSGD16_HI:
15590 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 15591 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15592 goto dogot;
15593
15594 case R_PPC64_GOT_TLSLD16:
15595 case R_PPC64_GOT_TLSLD16_LO:
15596 case R_PPC64_GOT_TLSLD16_HI:
15597 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 15598 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15599 goto dogot;
15600
15601 case R_PPC64_GOT_TPREL16_DS:
15602 case R_PPC64_GOT_TPREL16_LO_DS:
15603 case R_PPC64_GOT_TPREL16_HI:
15604 case R_PPC64_GOT_TPREL16_HA:
15605 tls_type = TLS_TLS | TLS_TPREL;
15606 goto dogot;
15607
15608 case R_PPC64_GOT_DTPREL16_DS:
15609 case R_PPC64_GOT_DTPREL16_LO_DS:
15610 case R_PPC64_GOT_DTPREL16_HI:
15611 case R_PPC64_GOT_DTPREL16_HA:
15612 tls_type = TLS_TLS | TLS_DTPREL;
15613 goto dogot;
15614
65f38f15
AM
15615 case R_PPC64_GOT16:
15616 case R_PPC64_GOT16_LO:
15617 case R_PPC64_GOT16_HI:
15618 case R_PPC64_GOT16_HA:
15619 case R_PPC64_GOT16_DS:
15620 case R_PPC64_GOT16_LO_DS:
411e1bfb 15621 dogot:
5bd4f169
AM
15622 {
15623 /* Relocation is to the entry for this symbol in the global
15624 offset table. */
e717da7e 15625 asection *got;
d881513a 15626 bfd_vma *offp;
5bd4f169 15627 bfd_vma off;
d881513a 15628 unsigned long indx = 0;
927be08e 15629 struct got_entry *ent;
65f38f15 15630
d881513a
AM
15631 if (tls_type == (TLS_TLS | TLS_LD)
15632 && (h == NULL
f5385ebf 15633 || !h->elf.def_dynamic))
927be08e 15634 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15635 else
5bd4f169 15636 {
d881513a
AM
15637 if (h != NULL)
15638 {
f0158f44
AM
15639 if (!htab->elf.dynamic_sections_created
15640 || h->elf.dynindx == -1
15641 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15642 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15643 /* This is actually a static link, or it is a
15644 -Bsymbolic link and the symbol is defined
15645 locally, or the symbol was forced to be local
15646 because of a version file. */
15647 ;
15648 else
15649 {
039b3fef 15650 indx = h->elf.dynindx;
d881513a
AM
15651 unresolved_reloc = FALSE;
15652 }
039b3fef 15653 ent = h->elf.got.glist;
d881513a 15654 }
411e1bfb 15655 else
5bd4f169 15656 {
d881513a
AM
15657 if (local_got_ents == NULL)
15658 abort ();
15659 ent = local_got_ents[r_symndx];
5bd4f169 15660 }
d881513a
AM
15661
15662 for (; ent != NULL; ent = ent->next)
31c76678 15663 if (ent->addend == orig_rel.r_addend
e717da7e 15664 && ent->owner == input_bfd
d881513a
AM
15665 && ent->tls_type == tls_type)
15666 break;
5bd4f169 15667 }
411e1bfb 15668
927be08e
AM
15669 if (ent == NULL)
15670 abort ();
15671 if (ent->is_indirect)
15672 ent = ent->got.ent;
15673 offp = &ent->got.offset;
15674 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15675 if (got == NULL)
15676 abort ();
15677
411e1bfb
AM
15678 /* The offset must always be a multiple of 8. We use the
15679 least significant bit to record whether we have already
15680 processed this entry. */
d881513a 15681 off = *offp;
411e1bfb
AM
15682 if ((off & 1) != 0)
15683 off &= ~1;
5bd4f169
AM
15684 else
15685 {
411e1bfb
AM
15686 /* Generate relocs for the dynamic linker, except in
15687 the case of TLSLD where we'll use one entry per
15688 module. */
25f23106
AM
15689 asection *relgot;
15690 bfd_boolean ifunc;
e717da7e 15691
d881513a 15692 *offp = off | 1;
25f23106
AM
15693 relgot = NULL;
15694 ifunc = (h != NULL
15695 ? h->elf.type == STT_GNU_IFUNC
15696 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15697 if (ifunc)
82e66161
AM
15698 {
15699 relgot = htab->elf.irelplt;
15700 if (indx == 0)
15701 htab->local_ifunc_resolver = 1;
15702 else if (is_static_defined (&h->elf))
15703 htab->maybe_local_ifunc_resolver = 1;
15704 }
f0158f44
AM
15705 else if (indx != 0
15706 || (bfd_link_pic (info)
15707 && (h == NULL
21d68fcd 15708 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15709 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15710 && !h->elf.def_dynamic))
15711 && !(tls_type == (TLS_TLS | TLS_TPREL)
15712 && bfd_link_executable (info)
15713 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15714 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15715 if (relgot != NULL)
5bd4f169 15716 {
e717da7e
AM
15717 outrel.r_offset = (got->output_section->vma
15718 + got->output_offset
411e1bfb 15719 + off);
4cc603a5 15720 outrel.r_addend = addend;
d881513a 15721 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15722 {
411e1bfb 15723 outrel.r_addend = 0;
e515b051 15724 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15725 if (tls_type == (TLS_TLS | TLS_GD))
15726 {
e717da7e
AM
15727 loc = relgot->contents;
15728 loc += (relgot->reloc_count++
d881513a
AM
15729 * sizeof (Elf64_External_Rela));
15730 bfd_elf64_swap_reloca_out (output_bfd,
15731 &outrel, loc);
e515b051 15732 outrel.r_offset += 8;
4cc603a5 15733 outrel.r_addend = addend;
d881513a
AM
15734 outrel.r_info
15735 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15736 }
411e1bfb 15737 }
951fd09b 15738 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15739 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15740 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15741 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15742 else if (indx != 0)
15743 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15744 else
81407a69 15745 {
25f23106
AM
15746 if (ifunc)
15747 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15748 else
15749 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15750
15751 /* Write the .got section contents for the sake
15752 of prelink. */
e717da7e 15753 loc = got->contents + off;
23fbd6fa
JJ
15754 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15755 loc);
81407a69 15756 }
81407a69
AM
15757
15758 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15759 {
15760 outrel.r_addend += relocation;
15761 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15762 {
15763 if (htab->elf.tls_sec == NULL)
15764 outrel.r_addend = 0;
15765 else
15766 outrel.r_addend -= htab->elf.tls_sec->vma;
15767 }
e515b051 15768 }
e717da7e
AM
15769 loc = relgot->contents;
15770 loc += (relgot->reloc_count++
411e1bfb
AM
15771 * sizeof (Elf64_External_Rela));
15772 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15773 }
15774
ad8e1ba5 15775 /* Init the .got section contents here if we're not
81407a69 15776 emitting a reloc. */
d881513a 15777 else
411e1bfb 15778 {
4cc603a5 15779 relocation += addend;
f0158f44 15780 if (tls_type != 0)
411e1bfb 15781 {
989f9879
AM
15782 if (htab->elf.tls_sec == NULL)
15783 relocation = 0;
15784 else
15785 {
f0158f44
AM
15786 if (tls_type & TLS_LD)
15787 relocation = 0;
15788 else
15789 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15790 if (tls_type & TLS_TPREL)
989f9879
AM
15791 relocation += DTP_OFFSET - TP_OFFSET;
15792 }
5bd4f169 15793
f0158f44 15794 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15795 {
15796 bfd_put_64 (output_bfd, relocation,
e717da7e 15797 got->contents + off + 8);
676ee2b5 15798 relocation = 1;
7b609f53 15799 }
411e1bfb
AM
15800 }
15801 bfd_put_64 (output_bfd, relocation,
e717da7e 15802 got->contents + off);
5bd4f169
AM
15803 }
15804 }
15805
65f38f15
AM
15806 if (off >= (bfd_vma) -2)
15807 abort ();
15808
bf102f86 15809 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 15810 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15811 }
65f38f15
AM
15812 break;
15813
15814 case R_PPC64_PLT16_HA:
15815 case R_PPC64_PLT16_HI:
15816 case R_PPC64_PLT16_LO:
08be3224 15817 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
15818 case R_PPC64_PLT32:
15819 case R_PPC64_PLT64:
23cedd1d
AM
15820 case R_PPC64_PLTSEQ:
15821 case R_PPC64_PLTCALL:
65f38f15
AM
15822 /* Relocation is to the entry for this symbol in the
15823 procedure linkage table. */
23cedd1d 15824 unresolved_reloc = TRUE;
cbf95972
AM
15825 {
15826 struct plt_entry **plt_list = NULL;
15827 if (h != NULL)
15828 plt_list = &h->elf.plt.plist;
15829 else if (local_got_ents != NULL)
15830 {
15831 struct plt_entry **local_plt = (struct plt_entry **)
15832 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15833 plt_list = local_plt + r_symndx;
cbf95972
AM
15834 }
15835 if (plt_list)
15836 {
15837 struct plt_entry *ent;
65f38f15 15838
cbf95972
AM
15839 for (ent = *plt_list; ent != NULL; ent = ent->next)
15840 if (ent->plt.offset != (bfd_vma) -1
15841 && ent->addend == orig_rel.r_addend)
15842 {
15843 asection *plt;
08be3224 15844 bfd_vma got;
cbf95972
AM
15845
15846 plt = htab->elf.splt;
15847 if (!htab->elf.dynamic_sections_created
15848 || h == NULL
15849 || h->elf.dynindx == -1)
2d7ad24e
AM
15850 {
15851 if (h != NULL
15852 ? h->elf.type == STT_GNU_IFUNC
15853 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15854 plt = htab->elf.iplt;
15855 else
15856 plt = htab->pltlocal;
15857 }
15858 relocation = (plt->output_section->vma
15859 + plt->output_offset
15860 + ent->plt.offset);
08be3224 15861 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15862 || r_type == R_PPC64_PLT16_HI
15863 || r_type == R_PPC64_PLT16_LO
15864 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15865 {
15866 got = (elf_gp (output_bfd)
15867 + htab->sec_info[input_section->id].toc_off);
15868 relocation -= got;
15869 }
cbf95972
AM
15870 addend = 0;
15871 unresolved_reloc = FALSE;
15872 break;
15873 }
15874 }
15875 }
65f38f15 15876 break;
5bd4f169 15877
0b13192e
AM
15878 case R_PPC64_TOC:
15879 /* Relocation value is TOC base. */
15880 relocation = TOCstart;
cf35638d 15881 if (r_symndx == STN_UNDEF)
6f20ed8a 15882 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15883 else if (unresolved_reloc)
15884 ;
6f20ed8a
AM
15885 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15886 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15887 else
15888 unresolved_reloc = TRUE;
ab96bf03 15889 goto dodyn;
0b13192e 15890
5bd4f169
AM
15891 /* TOC16 relocs. We want the offset relative to the TOC base,
15892 which is the address of the start of the TOC plus 0x8000.
15893 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15894 in this order. */
65f38f15
AM
15895 case R_PPC64_TOC16:
15896 case R_PPC64_TOC16_LO:
15897 case R_PPC64_TOC16_HI:
15898 case R_PPC64_TOC16_DS:
15899 case R_PPC64_TOC16_LO_DS:
15900 case R_PPC64_TOC16_HA:
6f20ed8a 15901 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15902 break;
15903
15904 /* Relocate against the beginning of the section. */
65f38f15
AM
15905 case R_PPC64_SECTOFF:
15906 case R_PPC64_SECTOFF_LO:
15907 case R_PPC64_SECTOFF_HI:
15908 case R_PPC64_SECTOFF_DS:
15909 case R_PPC64_SECTOFF_LO_DS:
15910 case R_PPC64_SECTOFF_HA:
4ce794b7 15911 if (sec != NULL)
65f38f15 15912 addend -= sec->output_section->vma;
5bd4f169
AM
15913 break;
15914
25f23106
AM
15915 case R_PPC64_REL16:
15916 case R_PPC64_REL16_LO:
15917 case R_PPC64_REL16_HI:
15918 case R_PPC64_REL16_HA:
a680de9a 15919 case R_PPC64_REL16DX_HA:
25f23106
AM
15920 break;
15921
721956f4
AM
15922 case R_PPC64_REL14:
15923 case R_PPC64_REL14_BRNTAKEN:
15924 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15925 case R_PPC64_REL24:
05d0e962 15926 case R_PPC64_REL24_NOTOC:
5d1634d7
AM
15927 break;
15928
411e1bfb
AM
15929 case R_PPC64_TPREL16:
15930 case R_PPC64_TPREL16_LO:
15931 case R_PPC64_TPREL16_HI:
15932 case R_PPC64_TPREL16_HA:
15933 case R_PPC64_TPREL16_DS:
15934 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15935 case R_PPC64_TPREL16_HIGH:
15936 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15937 case R_PPC64_TPREL16_HIGHER:
15938 case R_PPC64_TPREL16_HIGHERA:
15939 case R_PPC64_TPREL16_HIGHEST:
15940 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
15941 if (h != NULL
15942 && h->elf.root.type == bfd_link_hash_undefweak
15943 && h->elf.dynindx == -1)
15944 {
15945 /* Make this relocation against an undefined weak symbol
15946 resolve to zero. This is really just a tweak, since
15947 code using weak externs ought to check that they are
15948 defined before using them. */
15949 bfd_byte *p = contents + rel->r_offset - d_offset;
15950
95f0d0d2 15951 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15952 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15953 if (insn != 0)
95f0d0d2 15954 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15955 break;
15956 }
989f9879
AM
15957 if (htab->elf.tls_sec != NULL)
15958 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15959 /* The TPREL16 relocs shouldn't really be used in shared
15960 libs or with non-local symbols as that will result in
15961 DT_TEXTREL being set, but support them anyway. */
15962 goto dodyn;
411e1bfb
AM
15963
15964 case R_PPC64_DTPREL16:
15965 case R_PPC64_DTPREL16_LO:
15966 case R_PPC64_DTPREL16_HI:
15967 case R_PPC64_DTPREL16_HA:
15968 case R_PPC64_DTPREL16_DS:
15969 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15970 case R_PPC64_DTPREL16_HIGH:
15971 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15972 case R_PPC64_DTPREL16_HIGHER:
15973 case R_PPC64_DTPREL16_HIGHERA:
15974 case R_PPC64_DTPREL16_HIGHEST:
15975 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15976 if (htab->elf.tls_sec != NULL)
15977 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15978 break;
15979
45965137
AM
15980 case R_PPC64_ADDR64_LOCAL:
15981 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15982 ? h->elf.other
15983 : sym->st_other);
15984 break;
15985
e515b051
AM
15986 case R_PPC64_DTPMOD64:
15987 relocation = 1;
15988 addend = 0;
15989 goto dodyn;
15990
411e1bfb 15991 case R_PPC64_TPREL64:
989f9879
AM
15992 if (htab->elf.tls_sec != NULL)
15993 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15994 goto dodyn;
15995
15996 case R_PPC64_DTPREL64:
989f9879
AM
15997 if (htab->elf.tls_sec != NULL)
15998 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15999 /* Fall through. */
411e1bfb 16000
65f38f15
AM
16001 /* Relocations that may need to be propagated if this is a
16002 dynamic object. */
04c9666a 16003 case R_PPC64_REL30:
65f38f15
AM
16004 case R_PPC64_REL32:
16005 case R_PPC64_REL64:
16006 case R_PPC64_ADDR14:
16007 case R_PPC64_ADDR14_BRNTAKEN:
16008 case R_PPC64_ADDR14_BRTAKEN:
16009 case R_PPC64_ADDR16:
16010 case R_PPC64_ADDR16_DS:
16011 case R_PPC64_ADDR16_HA:
16012 case R_PPC64_ADDR16_HI:
f9c6b907
AM
16013 case R_PPC64_ADDR16_HIGH:
16014 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16015 case R_PPC64_ADDR16_HIGHER:
16016 case R_PPC64_ADDR16_HIGHERA:
16017 case R_PPC64_ADDR16_HIGHEST:
16018 case R_PPC64_ADDR16_HIGHESTA:
16019 case R_PPC64_ADDR16_LO:
16020 case R_PPC64_ADDR16_LO_DS:
16021 case R_PPC64_ADDR24:
65f38f15
AM
16022 case R_PPC64_ADDR32:
16023 case R_PPC64_ADDR64:
16024 case R_PPC64_UADDR16:
16025 case R_PPC64_UADDR32:
16026 case R_PPC64_UADDR64:
411e1bfb 16027 dodyn:
5d1634d7 16028 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
16029 break;
16030
41bd81ab
AM
16031 if (NO_OPD_RELOCS && is_opd)
16032 break;
16033
8a9e8e72 16034 if (bfd_link_pic (info)
b1b07054
AM
16035 ? ((h == NULL
16036 || h->dyn_relocs != NULL)
16037 && ((h != NULL && pc_dynrelocs (h))
16038 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
16039 : (h != NULL
16040 ? h->dyn_relocs != NULL
d311bc8b 16041 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 16042 {
b34976b6 16043 bfd_boolean skip, relocate;
65f38f15 16044 asection *sreloc;
1cf1f670 16045 bfd_vma out_off;
82e66161 16046 long indx = 0;
65f38f15
AM
16047
16048 /* When generating a dynamic object, these relocations
16049 are copied into the output file to be resolved at run
16050 time. */
16051
b34976b6
AM
16052 skip = FALSE;
16053 relocate = FALSE;
65f38f15 16054
1cf1f670
AM
16055 out_off = _bfd_elf_section_offset (output_bfd, info,
16056 input_section, rel->r_offset);
16057 if (out_off == (bfd_vma) -1)
b34976b6 16058 skip = TRUE;
1cf1f670 16059 else if (out_off == (bfd_vma) -2)
b34976b6 16060 skip = TRUE, relocate = TRUE;
1cf1f670
AM
16061 out_off += (input_section->output_section->vma
16062 + input_section->output_offset);
16063 outrel.r_offset = out_off;
411e1bfb 16064 outrel.r_addend = rel->r_addend;
65f38f15 16065
1cf1f670
AM
16066 /* Optimize unaligned reloc use. */
16067 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16068 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16069 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16070 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16071 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16072 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16073 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16074 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16075 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16076
65f38f15 16077 if (skip)
0bb2d96a 16078 memset (&outrel, 0, sizeof outrel);
afe397ea 16079 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16080 && !is_opd
16081 && r_type != R_PPC64_TOC)
14acf4dc 16082 {
82e66161
AM
16083 indx = h->elf.dynindx;
16084 BFD_ASSERT (indx != -1);
16085 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16086 }
65f38f15
AM
16087 else
16088 {
41bd81ab
AM
16089 /* This symbol is local, or marked to become local,
16090 or this is an opd section reloc which must point
16091 at a local function. */
65f38f15 16092 outrel.r_addend += relocation;
e86ce104 16093 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16094 {
3fad3c7c 16095 if (is_opd && h != NULL)
afbe61cf
AM
16096 {
16097 /* Lie about opd entries. This case occurs
16098 when building shared libraries and we
16099 reference a function in another shared
3fad3c7c
AM
16100 lib. The same thing happens for a weak
16101 definition in an application that's
16102 overridden by a strong definition in a
16103 shared lib. (I believe this is a generic
16104 bug in binutils handling of weak syms.)
16105 In these cases we won't use the opd
1e2f5b6e 16106 entry in this lib. */
b34976b6 16107 unresolved_reloc = FALSE;
afbe61cf 16108 }
25f23106
AM
16109 if (!is_opd
16110 && r_type == R_PPC64_ADDR64
16111 && (h != NULL
16112 ? h->elf.type == STT_GNU_IFUNC
16113 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16114 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16115 else
16116 {
16117 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16118
25f23106
AM
16119 /* We need to relocate .opd contents for ld.so.
16120 Prelink also wants simple and consistent rules
16121 for relocs. This make all RELATIVE relocs have
16122 *r_offset equal to r_addend. */
16123 relocate = TRUE;
16124 }
65f38f15
AM
16125 }
16126 else
16127 {
25f23106
AM
16128 if (h != NULL
16129 ? h->elf.type == STT_GNU_IFUNC
16130 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16131 {
25f53a85 16132 info->callbacks->einfo
695344c0 16133 /* xgettext:c-format */
174d0a74 16134 (_("%H: %s for indirect "
c1c8c1ef 16135 "function `%pT' unsupported\n"),
25f53a85 16136 input_bfd, input_section, rel->r_offset,
25f23106
AM
16137 ppc64_elf_howto_table[r_type]->name,
16138 sym_name);
16139 ret = FALSE;
16140 }
cf35638d 16141 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16142 ;
16143 else if (sec == NULL || sec->owner == NULL)
16144 {
16145 bfd_set_error (bfd_error_bad_value);
b34976b6 16146 return FALSE;
65f38f15
AM
16147 }
16148 else
16149 {
16150 asection *osec;
16151
16152 osec = sec->output_section;
16153 indx = elf_section_data (osec)->dynindx;
16154
74541ad4
AM
16155 if (indx == 0)
16156 {
16157 if ((osec->flags & SEC_READONLY) == 0
16158 && htab->elf.data_index_section != NULL)
16159 osec = htab->elf.data_index_section;
16160 else
16161 osec = htab->elf.text_index_section;
16162 indx = elf_section_data (osec)->dynindx;
16163 }
16164 BFD_ASSERT (indx != 0);
16165
65f38f15
AM
16166 /* We are turning this relocation into one
16167 against a section symbol, so subtract out
16168 the output section's address but not the
16169 offset of the input section in the output
16170 section. */
16171 outrel.r_addend -= osec->vma;
16172 }
16173
16174 outrel.r_info = ELF64_R_INFO (indx, r_type);
16175 }
16176 }
16177
16178 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16179 if (h != NULL
16180 ? h->elf.type == STT_GNU_IFUNC
16181 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16182 {
16183 sreloc = htab->elf.irelplt;
16184 if (indx == 0)
16185 htab->local_ifunc_resolver = 1;
16186 else if (is_static_defined (&h->elf))
16187 htab->maybe_local_ifunc_resolver = 1;
16188 }
65f38f15
AM
16189 if (sreloc == NULL)
16190 abort ();
16191
dfbb6ac9
AM
16192 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16193 >= sreloc->size)
16194 abort ();
947216bf
AM
16195 loc = sreloc->contents;
16196 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16197 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16198
16199 /* If this reloc is against an external symbol, it will
16200 be computed at runtime, so there's no need to do
81407a69
AM
16201 anything now. However, for the sake of prelink ensure
16202 that the section contents are a known value. */
2cdcc330 16203 if (!relocate)
81407a69
AM
16204 {
16205 unresolved_reloc = FALSE;
16206 /* The value chosen here is quite arbitrary as ld.so
16207 ignores section contents except for the special
16208 case of .opd where the contents might be accessed
16209 before relocation. Choose zero, as that won't
16210 cause reloc overflow. */
16211 relocation = 0;
16212 addend = 0;
16213 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16214 to improve backward compatibility with older
16215 versions of ld. */
16216 if (r_type == R_PPC64_ADDR64)
16217 addend = outrel.r_addend;
16218 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16219 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16220 addend = outrel.r_offset;
81407a69 16221 }
65f38f15 16222 }
5bd4f169
AM
16223 break;
16224
65f38f15
AM
16225 case R_PPC64_COPY:
16226 case R_PPC64_GLOB_DAT:
16227 case R_PPC64_JMP_SLOT:
25f23106 16228 case R_PPC64_JMP_IREL:
65f38f15
AM
16229 case R_PPC64_RELATIVE:
16230 /* We shouldn't ever see these dynamic relocs in relocatable
16231 files. */
ae9a127f 16232 /* Fall through. */
65f38f15
AM
16233
16234 case R_PPC64_PLTGOT16:
16235 case R_PPC64_PLTGOT16_DS:
16236 case R_PPC64_PLTGOT16_HA:
16237 case R_PPC64_PLTGOT16_HI:
16238 case R_PPC64_PLTGOT16_LO:
16239 case R_PPC64_PLTGOT16_LO_DS:
16240 case R_PPC64_PLTREL32:
16241 case R_PPC64_PLTREL64:
16242 /* These ones haven't been implemented yet. */
16243
25f53a85 16244 info->callbacks->einfo
695344c0 16245 /* xgettext:c-format */
c1c8c1ef 16246 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16247 input_bfd,
4ce794b7 16248 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16249
16250 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16251 ret = FALSE;
c316a17c 16252 goto copy_reloc;
65f38f15 16253 }
5bd4f169 16254
67f0cbdb
AM
16255 /* Multi-instruction sequences that access the TOC can be
16256 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16257 to nop; addi rb,r2,x; */
67f0cbdb
AM
16258 switch (r_type)
16259 {
16260 default:
16261 break;
16262
16263 case R_PPC64_GOT_TLSLD16_HI:
16264 case R_PPC64_GOT_TLSGD16_HI:
16265 case R_PPC64_GOT_TPREL16_HI:
16266 case R_PPC64_GOT_DTPREL16_HI:
16267 case R_PPC64_GOT16_HI:
16268 case R_PPC64_TOC16_HI:
16269 /* These relocs would only be useful if building up an
16270 offset to later add to r2, perhaps in an indexed
16271 addressing mode instruction. Don't try to optimize.
16272 Unfortunately, the possibility of someone building up an
16273 offset like this or even with the HA relocs, means that
16274 we need to check the high insn when optimizing the low
16275 insn. */
16276 break;
16277
23cedd1d
AM
16278 case R_PPC64_PLTCALL:
16279 if (unresolved_reloc)
16280 {
16281 /* No plt entry. Make this into a direct call. */
16282 bfd_byte *p = contents + rel->r_offset;
16283 insn = bfd_get_32 (input_bfd, p);
16284 insn &= 1;
16285 bfd_put_32 (input_bfd, B_DOT | insn, p);
16286 bfd_put_32 (input_bfd, NOP, p + 4);
16287 unresolved_reloc = save_unresolved_reloc;
16288 r_type = R_PPC64_REL24;
16289 }
16290 break;
16291
16292 case R_PPC64_PLTSEQ:
16293 if (unresolved_reloc)
16294 {
16295 unresolved_reloc = FALSE;
16296 goto nop_it;
16297 }
16298 break;
16299
16300 case R_PPC64_PLT16_HA:
16301 if (unresolved_reloc)
16302 {
16303 unresolved_reloc = FALSE;
16304 goto nop_it;
16305 }
16306 /* Fall through. */
67f0cbdb
AM
16307 case R_PPC64_GOT_TLSLD16_HA:
16308 case R_PPC64_GOT_TLSGD16_HA:
16309 case R_PPC64_GOT_TPREL16_HA:
16310 case R_PPC64_GOT_DTPREL16_HA:
16311 case R_PPC64_GOT16_HA:
16312 case R_PPC64_TOC16_HA:
98528052 16313 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16314 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16315 {
23cedd1d
AM
16316 bfd_byte *p;
16317 nop_it:
16318 p = contents + (rel->r_offset & ~3);
98528052 16319 bfd_put_32 (input_bfd, NOP, p);
d830549d 16320 goto copy_reloc;
98528052 16321 }
67f0cbdb
AM
16322 break;
16323
23cedd1d
AM
16324 case R_PPC64_PLT16_LO:
16325 case R_PPC64_PLT16_LO_DS:
16326 if (unresolved_reloc)
16327 {
16328 unresolved_reloc = FALSE;
16329 goto nop_it;
16330 }
16331 /* Fall through. */
67f0cbdb
AM
16332 case R_PPC64_GOT_TLSLD16_LO:
16333 case R_PPC64_GOT_TLSGD16_LO:
16334 case R_PPC64_GOT_TPREL16_LO_DS:
16335 case R_PPC64_GOT_DTPREL16_LO_DS:
16336 case R_PPC64_GOT16_LO:
16337 case R_PPC64_GOT16_LO_DS:
16338 case R_PPC64_TOC16_LO:
16339 case R_PPC64_TOC16_LO_DS:
98528052 16340 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16341 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16342 {
16343 bfd_byte *p = contents + (rel->r_offset & ~3);
16344 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16345 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16346 {
16347 /* Transform addic to addi when we change reg. */
16348 insn &= ~((0x3f << 26) | (0x1f << 16));
16349 insn |= (14u << 26) | (2 << 16);
16350 }
16351 else
67f0cbdb 16352 {
98528052
AM
16353 insn &= ~(0x1f << 16);
16354 insn |= 2 << 16;
67f0cbdb 16355 }
560c8763 16356 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16357 }
16358 break;
9a23f96e
AM
16359
16360 case R_PPC64_TPREL16_HA:
16361 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16362 {
16363 bfd_byte *p = contents + (rel->r_offset & ~3);
16364 insn = bfd_get_32 (input_bfd, p);
16365 if ((insn & ((0x3f << 26) | 0x1f << 16))
16366 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16367 /* xgettext:c-format */
16368 info->callbacks->minfo
16369 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16370 input_bfd, input_section, rel->r_offset,
16371 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16372 else
d830549d
AM
16373 {
16374 bfd_put_32 (input_bfd, NOP, p);
16375 goto copy_reloc;
16376 }
9a23f96e
AM
16377 }
16378 break;
16379
16380 case R_PPC64_TPREL16_LO:
16381 case R_PPC64_TPREL16_LO_DS:
16382 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16383 {
16384 bfd_byte *p = contents + (rel->r_offset & ~3);
16385 insn = bfd_get_32 (input_bfd, p);
16386 insn &= ~(0x1f << 16);
16387 insn |= 13 << 16;
16388 bfd_put_32 (input_bfd, insn, p);
16389 }
16390 break;
67f0cbdb
AM
16391 }
16392
65f38f15
AM
16393 /* Do any further special processing. */
16394 switch (r_type)
16395 {
16396 default:
16397 break;
16398
25f23106 16399 case R_PPC64_REL16_HA:
a680de9a 16400 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16401 case R_PPC64_ADDR16_HA:
16402 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16403 case R_PPC64_ADDR16_HIGHERA:
16404 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16405 case R_PPC64_TOC16_HA:
16406 case R_PPC64_SECTOFF_HA:
411e1bfb 16407 case R_PPC64_TPREL16_HA:
f9c6b907 16408 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16409 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16410 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16411 case R_PPC64_DTPREL16_HA:
16412 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16413 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16414 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16415 /* It's just possible that this symbol is a weak symbol
16416 that's not actually defined anywhere. In that case,
16417 'sec' would be NULL, and we should leave the symbol
16418 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16419 if (sec == NULL)
16420 break;
1a0670f3 16421 /* Fall through. */
5c5f6e17
AM
16422
16423 case R_PPC64_GOT16_HA:
16424 case R_PPC64_PLTGOT16_HA:
16425 case R_PPC64_PLT16_HA:
16426 case R_PPC64_GOT_TLSGD16_HA:
16427 case R_PPC64_GOT_TLSLD16_HA:
16428 case R_PPC64_GOT_TPREL16_HA:
16429 case R_PPC64_GOT_DTPREL16_HA:
16430 /* Add 0x10000 if sign bit in 0:15 is set.
16431 Bits 0:15 are not used. */
16432 addend += 0x8000;
65f38f15
AM
16433 break;
16434
16435 case R_PPC64_ADDR16_DS:
16436 case R_PPC64_ADDR16_LO_DS:
16437 case R_PPC64_GOT16_DS:
16438 case R_PPC64_GOT16_LO_DS:
16439 case R_PPC64_PLT16_LO_DS:
16440 case R_PPC64_SECTOFF_DS:
16441 case R_PPC64_SECTOFF_LO_DS:
16442 case R_PPC64_TOC16_DS:
16443 case R_PPC64_TOC16_LO_DS:
16444 case R_PPC64_PLTGOT16_DS:
16445 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16446 case R_PPC64_GOT_TPREL16_DS:
16447 case R_PPC64_GOT_TPREL16_LO_DS:
16448 case R_PPC64_GOT_DTPREL16_DS:
16449 case R_PPC64_GOT_DTPREL16_LO_DS:
16450 case R_PPC64_TPREL16_DS:
16451 case R_PPC64_TPREL16_LO_DS:
16452 case R_PPC64_DTPREL16_DS:
16453 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16454 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16455 mask = 3;
a680de9a
PB
16456 /* If this reloc is against an lq, lxv, or stxv insn, then
16457 the value must be a multiple of 16. This is somewhat of
16458 a hack, but the "correct" way to do this by defining _DQ
16459 forms of all the _DS relocs bloats all reloc switches in
16460 this file. It doesn't make much sense to use these
16461 relocs in data, so testing the insn should be safe. */
16462 if ((insn & (0x3f << 26)) == (56u << 26)
16463 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16464 mask = 15;
a680de9a
PB
16465 relocation += addend;
16466 addend = insn & (mask ^ 3);
16467 if ((relocation & mask) != 0)
65f38f15 16468 {
a680de9a 16469 relocation ^= relocation & mask;
25f53a85 16470 info->callbacks->einfo
695344c0 16471 /* xgettext:c-format */
174d0a74 16472 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16473 input_bfd, input_section, rel->r_offset,
d830549d 16474 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16475 mask + 1);
65f38f15 16476 bfd_set_error (bfd_error_bad_value);
b34976b6 16477 ret = FALSE;
c316a17c 16478 goto copy_reloc;
65f38f15
AM
16479 }
16480 break;
5bd4f169
AM
16481 }
16482
239e1f3a
AM
16483 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16484 because such sections are not SEC_ALLOC and thus ld.so will
16485 not process them. */
d830549d 16486 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16487 if (unresolved_reloc
239e1f3a 16488 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16489 && h->elf.def_dynamic)
16490 && _bfd_elf_section_offset (output_bfd, info, input_section,
16491 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16492 {
25f53a85 16493 info->callbacks->einfo
695344c0 16494 /* xgettext:c-format */
c1c8c1ef 16495 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16496 input_bfd, input_section, rel->r_offset,
b80eed39 16497 howto->name,
039b3fef 16498 h->elf.root.root.string);
b34976b6 16499 ret = FALSE;
9c07fe7c 16500 }
5bd4f169 16501
b80eed39
AM
16502 /* 16-bit fields in insns mostly have signed values, but a
16503 few insns have 16-bit unsigned values. Really, we should
16504 have different reloc types. */
16505 if (howto->complain_on_overflow != complain_overflow_dont
16506 && howto->dst_mask == 0xffff
16507 && (input_section->flags & SEC_CODE) != 0)
16508 {
16509 enum complain_overflow complain = complain_overflow_signed;
16510
16511 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16512 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16513 complain = complain_overflow_bitfield;
16514 else if (howto->rightshift == 0
16515 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16516 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16517 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16518 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16519 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16520 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16521 complain = complain_overflow_unsigned;
16522 if (howto->complain_on_overflow != complain)
16523 {
16524 alt_howto = *howto;
16525 alt_howto.complain_on_overflow = complain;
16526 howto = &alt_howto;
16527 }
16528 }
16529
a680de9a
PB
16530 if (r_type == R_PPC64_REL16DX_HA)
16531 {
16532 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
16533 if (rel->r_offset + 4 > input_section->size)
16534 r = bfd_reloc_outofrange;
16535 else
16536 {
16537 relocation += addend;
16538 relocation -= (rel->r_offset
16539 + input_section->output_offset
16540 + input_section->output_section->vma);
3de43e7b 16541 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16542 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16543 insn &= ~0x1fffc1;
3de43e7b 16544 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16545 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16546 r = bfd_reloc_ok;
3de43e7b 16547 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16548 r = bfd_reloc_overflow;
16549 }
16550 }
16551 else
16552 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
16553 rel->r_offset, relocation, addend);
5bd4f169 16554
ef60b7ff 16555 if (r != bfd_reloc_ok)
5bd4f169 16556 {
bc30df16 16557 char *more_info = NULL;
b80eed39 16558 const char *reloc_name = howto->name;
bc30df16
AM
16559
16560 if (reloc_dest != DEST_NORMAL)
16561 {
16562 more_info = bfd_malloc (strlen (reloc_name) + 8);
16563 if (more_info != NULL)
16564 {
16565 strcpy (more_info, reloc_name);
16566 strcat (more_info, (reloc_dest == DEST_OPD
16567 ? " (OPD)" : " (stub)"));
16568 reloc_name = more_info;
16569 }
16570 }
16571
cd27b276 16572 if (r == bfd_reloc_overflow)
5bd4f169 16573 {
8131c122
AM
16574 /* On code like "if (foo) foo();" don't report overflow
16575 on a branch to zero when foo is undefined. */
16576 if (!warned
16577 && (reloc_dest == DEST_STUB
16578 || !(h != NULL
16579 && (h->elf.root.type == bfd_link_hash_undefweak
16580 || h->elf.root.type == bfd_link_hash_undefined)
16581 && is_branch_reloc (r_type))))
1a72702b
AM
16582 info->callbacks->reloc_overflow (info, &h->elf.root,
16583 sym_name, reloc_name,
16584 orig_rel.r_addend,
16585 input_bfd, input_section,
16586 rel->r_offset);
ef60b7ff
AM
16587 }
16588 else
16589 {
25f53a85 16590 info->callbacks->einfo
695344c0 16591 /* xgettext:c-format */
c1c8c1ef 16592 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16593 input_bfd, input_section, rel->r_offset,
bc30df16 16594 reloc_name, sym_name, (int) r);
b34976b6 16595 ret = FALSE;
ef60b7ff 16596 }
bc30df16
AM
16597 if (more_info != NULL)
16598 free (more_info);
5bd4f169 16599 }
c316a17c
AM
16600 copy_reloc:
16601 if (wrel != rel)
16602 *wrel = *rel;
16603 }
16604
16605 if (wrel != rel)
16606 {
16607 Elf_Internal_Shdr *rel_hdr;
16608 size_t deleted = rel - wrel;
16609
16610 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16611 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16612 if (rel_hdr->sh_size == 0)
16613 {
16614 /* It is too late to remove an empty reloc section. Leave
16615 one NONE reloc.
16616 ??? What is wrong with an empty section??? */
16617 rel_hdr->sh_size = rel_hdr->sh_entsize;
16618 deleted -= 1;
16619 }
16620 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16621 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16622 input_section->reloc_count -= deleted;
5bd4f169
AM
16623 }
16624
645ea6a9
AM
16625 /* If we're emitting relocations, then shortly after this function
16626 returns, reloc offsets and addends for this section will be
16627 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16628 file rather than the input. Save a copy of the relocs for
16629 opd_entry_value. */
0e1862bb 16630 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16631 {
16632 bfd_size_type amt;
16633 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16634 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16635 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16636 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16637 if (rel == NULL)
16638 return FALSE;
16639 memcpy (rel, relocs, amt);
16640 }
5bd4f169
AM
16641 return ret;
16642}
16643
754021d0
AM
16644/* Adjust the value of any local symbols in opd sections. */
16645
6e0b88f1 16646static int
754021d0
AM
16647ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16648 const char *name ATTRIBUTE_UNUSED,
16649 Elf_Internal_Sym *elfsym,
16650 asection *input_sec,
16651 struct elf_link_hash_entry *h)
16652{
74f0fb50
AM
16653 struct _opd_sec_data *opd;
16654 long adjust;
754021d0
AM
16655 bfd_vma value;
16656
4025353c 16657 if (h != NULL)
6e0b88f1 16658 return 1;
4025353c 16659
74f0fb50
AM
16660 opd = get_opd_info (input_sec);
16661 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16662 return 1;
754021d0
AM
16663
16664 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16665 if (!bfd_link_relocatable (info))
754021d0
AM
16666 value -= input_sec->output_section->vma;
16667
51aecdc5 16668 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16669 if (adjust == -1)
6e0b88f1
AM
16670 return 2;
16671
16672 elfsym->st_value += adjust;
16673 return 1;
754021d0
AM
16674}
16675
5bd4f169
AM
16676/* Finish up dynamic symbol handling. We set the contents of various
16677 dynamic sections here. */
16678
b34976b6 16679static bfd_boolean
4ce794b7
AM
16680ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16681 struct bfd_link_info *info,
16682 struct elf_link_hash_entry *h,
4aef7643 16683 Elf_Internal_Sym *sym)
5bd4f169 16684{
65f38f15 16685 struct ppc_link_hash_table *htab;
8387904d 16686 struct plt_entry *ent;
5bd4f169 16687
65f38f15 16688 htab = ppc_hash_table (info);
4dfe6ac6
NC
16689 if (htab == NULL)
16690 return FALSE;
5bd4f169 16691
49c09209
AM
16692 if (!htab->opd_abi && !h->def_regular)
16693 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16694 if (ent->plt.offset != (bfd_vma) -1)
16695 {
16696 /* Mark the symbol as undefined, rather than as
16697 defined in glink. Leave the value if there were
16698 any relocations where pointer equality matters
16699 (this is a clue for the dynamic linker, to make
16700 function pointer comparisons work between an
16701 application and shared library), otherwise set it
16702 to zero. */
16703 sym->st_shndx = SHN_UNDEF;
16704 if (!h->pointer_equality_needed)
16705 sym->st_value = 0;
16706 else if (!h->ref_regular_nonweak)
16707 {
16708 /* This breaks function pointer comparisons, but
16709 that is better than breaking tests for a NULL
16710 function pointer. */
16711 sym->st_value = 0;
16712 }
16713 break;
16714 }
5bd4f169 16715
f5385ebf 16716 if (h->needs_copy)
5bd4f169 16717 {
65f38f15 16718 /* This symbol needs a copy reloc. Set it up. */
49c09209 16719 Elf_Internal_Rela rela;
5474d94f 16720 asection *srel;
49c09209 16721 bfd_byte *loc;
5bd4f169 16722
65f38f15
AM
16723 if (h->dynindx == -1
16724 || (h->root.type != bfd_link_hash_defined
16725 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16726 || htab->elf.srelbss == NULL
16727 || htab->elf.sreldynrelro == NULL)
65f38f15 16728 abort ();
5bd4f169
AM
16729
16730 rela.r_offset = (h->root.u.def.value
16731 + h->root.u.def.section->output_section->vma
16732 + h->root.u.def.section->output_offset);
16733 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16734 rela.r_addend = 0;
afbf7e8e 16735 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16736 srel = htab->elf.sreldynrelro;
16737 else
16738 srel = htab->elf.srelbss;
16739 loc = srel->contents;
16740 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16741 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16742 }
16743
b34976b6 16744 return TRUE;
5bd4f169
AM
16745}
16746
65f38f15
AM
16747/* Used to decide how to sort relocs in an optimal manner for the
16748 dynamic linker, before writing them out. */
16749
16750static enum elf_reloc_type_class
7e612e98
AM
16751ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16752 const asection *rel_sec,
16753 const Elf_Internal_Rela *rela)
65f38f15 16754{
04c9666a 16755 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16756 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16757
33e44f2e 16758 if (rel_sec == htab->elf.irelplt)
7e612e98 16759 return reloc_class_ifunc;
a33d1f77 16760
4ce794b7 16761 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16762 switch (r_type)
65f38f15
AM
16763 {
16764 case R_PPC64_RELATIVE:
16765 return reloc_class_relative;
16766 case R_PPC64_JMP_SLOT:
16767 return reloc_class_plt;
16768 case R_PPC64_COPY:
16769 return reloc_class_copy;
16770 default:
16771 return reloc_class_normal;
16772 }
16773}
16774
5bd4f169
AM
16775/* Finish up the dynamic sections. */
16776
b34976b6 16777static bfd_boolean
4ce794b7
AM
16778ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16779 struct bfd_link_info *info)
5bd4f169 16780{
65f38f15
AM
16781 struct ppc_link_hash_table *htab;
16782 bfd *dynobj;
5bd4f169 16783 asection *sdyn;
5bd4f169 16784
65f38f15 16785 htab = ppc_hash_table (info);
4dfe6ac6
NC
16786 if (htab == NULL)
16787 return FALSE;
16788
65f38f15 16789 dynobj = htab->elf.dynobj;
3d4d4302 16790 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16791
65f38f15 16792 if (htab->elf.dynamic_sections_created)
5bd4f169 16793 {
5bd4f169
AM
16794 Elf64_External_Dyn *dyncon, *dynconend;
16795
33e44f2e 16796 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16797 abort ();
5bd4f169
AM
16798
16799 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16800 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16801 for (; dyncon < dynconend; dyncon++)
16802 {
16803 Elf_Internal_Dyn dyn;
19397422 16804 asection *s;
5bd4f169
AM
16805
16806 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16807
16808 switch (dyn.d_tag)
16809 {
65f38f15
AM
16810 default:
16811 continue;
5bd4f169 16812
5d1634d7 16813 case DT_PPC64_GLINK:
4ce794b7 16814 s = htab->glink;
6348e046 16815 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16816 /* We stupidly defined DT_PPC64_GLINK to be the start
16817 of glink rather than the first entry point, which is
16818 what ld.so needs, and now have a bigger stub to
16819 support automatic multiple TOCs. */
9e390558 16820 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16821 break;
16822
19397422
AM
16823 case DT_PPC64_OPD:
16824 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16825 if (s == NULL)
16826 continue;
16827 dyn.d_un.d_ptr = s->vma;
19397422
AM
16828 break;
16829
e8910a83
AM
16830 case DT_PPC64_OPT:
16831 if (htab->do_multi_toc && htab->multi_toc_needed)
16832 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16833 if (htab->has_plt_localentry0)
16834 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16835 break;
16836
19397422
AM
16837 case DT_PPC64_OPDSZ:
16838 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16839 if (s == NULL)
16840 continue;
eea6121a 16841 dyn.d_un.d_val = s->size;
19397422
AM
16842 break;
16843
65f38f15 16844 case DT_PLTGOT:
33e44f2e 16845 s = htab->elf.splt;
6348e046 16846 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16847 break;
16848
16849 case DT_JMPREL:
33e44f2e 16850 s = htab->elf.srelplt;
6348e046 16851 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16852 break;
5bd4f169 16853
65f38f15 16854 case DT_PLTRELSZ:
33e44f2e 16855 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16856 break;
82e66161
AM
16857
16858 case DT_TEXTREL:
16859 if (htab->local_ifunc_resolver)
16860 info->callbacks->einfo
16861 (_("%X%P: text relocations and GNU indirect "
16862 "functions will result in a segfault at runtime\n"));
16863 else if (htab->maybe_local_ifunc_resolver)
16864 info->callbacks->einfo
16865 (_("%P: warning: text relocations and GNU indirect "
16866 "functions may result in a segfault at runtime\n"));
16867 continue;
5bd4f169 16868 }
5bd4f169 16869
65f38f15 16870 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16871 }
5bd4f169
AM
16872 }
16873
6528b6eb
AM
16874 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16875 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16876 {
16877 /* Fill in the first entry in the global offset table.
16878 We use it to hold the link-time TOCbase. */
16879 bfd_put_64 (output_bfd,
60ee0d4a 16880 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16881 htab->elf.sgot->contents);
5d1634d7
AM
16882
16883 /* Set .got entry size. */
2cdcc330
AM
16884 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16885 = 8;
5d1634d7
AM
16886 }
16887
6528b6eb
AM
16888 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16889 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16890 {
16891 /* Set .plt entry size. */
33e44f2e 16892 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16893 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16894 }
16895
84f5d08e
AM
16896 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16897 brlt ourselves if emitrelocations. */
16898 if (htab->brlt != NULL
16899 && htab->brlt->reloc_count != 0
16900 && !_bfd_elf_link_output_relocs (output_bfd,
16901 htab->brlt,
d4730f92 16902 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16903 elf_section_data (htab->brlt)->relocs,
16904 NULL))
16905 return FALSE;
16906
176a0d42
AM
16907 if (htab->glink != NULL
16908 && htab->glink->reloc_count != 0
16909 && !_bfd_elf_link_output_relocs (output_bfd,
16910 htab->glink,
d4730f92 16911 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16912 elf_section_data (htab->glink)->relocs,
16913 NULL))
16914 return FALSE;
16915
da44f4e5 16916
df136d64
AM
16917 if (htab->glink_eh_frame != NULL
16918 && htab->glink_eh_frame->size != 0
16919 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16920 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16921 htab->glink_eh_frame,
16922 htab->glink_eh_frame->contents))
16923 return FALSE;
58d180e8 16924
e717da7e 16925 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16926 since we didn't add them to DYNOBJ. We know dynobj is the first
16927 bfd. */
c72f2fb2 16928 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16929 {
16930 asection *s;
7b53ace3 16931
0c8d6e5c 16932 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16933 continue;
16934
e717da7e
AM
16935 s = ppc64_elf_tdata (dynobj)->got;
16936 if (s != NULL
eea6121a 16937 && s->size != 0
e717da7e
AM
16938 && s->output_section != bfd_abs_section_ptr
16939 && !bfd_set_section_contents (output_bfd, s->output_section,
16940 s->contents, s->output_offset,
eea6121a 16941 s->size))
e717da7e
AM
16942 return FALSE;
16943 s = ppc64_elf_tdata (dynobj)->relgot;
16944 if (s != NULL
eea6121a 16945 && s->size != 0
e717da7e
AM
16946 && s->output_section != bfd_abs_section_ptr
16947 && !bfd_set_section_contents (output_bfd, s->output_section,
16948 s->contents, s->output_offset,
eea6121a 16949 s->size))
e717da7e
AM
16950 return FALSE;
16951 }
f6c52c13 16952
b34976b6 16953 return TRUE;
5bd4f169
AM
16954}
16955
5bd4f169 16956#include "elf64-target.h"
7b8e7dad
AM
16957
16958/* FreeBSD support */
16959
16960#undef TARGET_LITTLE_SYM
16961#undef TARGET_LITTLE_NAME
16962
16963#undef TARGET_BIG_SYM
6d00b590 16964#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16965#undef TARGET_BIG_NAME
16966#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16967
16968#undef ELF_OSABI
16969#define ELF_OSABI ELFOSABI_FREEBSD
16970
16971#undef elf64_bed
16972#define elf64_bed elf64_powerpc_fbsd_bed
16973
16974#include "elf64-target.h"
This page took 3.92743 seconds and 4 git commands to generate.