Automatic date update in version.in
[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
04c6a44c 67#define ELF_COMMONPAGESIZE 0x10000
ad8e1ba5
AM
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
ad8e1ba5 74#define elf_backend_got_header_size 8
5474d94f 75#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
76#define elf_backend_can_gc_sections 1
77#define elf_backend_can_refcount 1
78#define elf_backend_rela_normal 1
64f52338 79#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 80#define elf_backend_default_execstack 0
ad8e1ba5 81
e717da7e 82#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 83#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 84#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
85#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
86#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
87#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
88#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 89#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 90#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 91#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
92
93#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
94#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
95#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 96#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 97#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 98#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 99#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 100#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 101#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 102#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 103#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 104#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 105#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 106#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 107#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
108#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
109#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 110#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
111#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
112#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 113#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 114#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 115#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
116#define elf_backend_relocate_section ppc64_elf_relocate_section
117#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
118#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
119#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 120#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 121#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 122#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 123#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 124#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 125
5bd4f169
AM
126/* The name of the dynamic interpreter. This is put in the .interp
127 section. */
128#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
129
130/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 131#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
132
133/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 134#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 135
a078d95a
AM
136/* Offsets to some stack save slots. */
137#define STK_LR 16
138#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 139/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
140 CR save slot. Used only by optimised __tls_get_addr call stub,
141 relying on __tls_get_addr_opt not saving CR.. */
142#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
143
5bd4f169 144/* TOC base pointers offset from start of TOC. */
411e1bfb 145#define TOC_BASE_OFF 0x8000
a27e685f
AM
146/* TOC base alignment. */
147#define TOC_BASE_ALIGN 256
411e1bfb
AM
148
149/* Offset of tp and dtp pointers from start of TLS block. */
150#define TP_OFFSET 0x7000
151#define DTP_OFFSET 0x8000
5bd4f169 152
ad8e1ba5
AM
153/* .plt call stub instructions. The normal stub is like this, but
154 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 155 insert an addi to adjust r11. */
a078d95a 156#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
157#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
158#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
159#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
160#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
161#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
162#define BCTR 0x4e800420 /* bctr */
163
07d6d2b8
AM
164#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
165#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
166#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
167
168#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
169#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
170#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
171#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
172#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
173#define BNECTR 0x4ca20420 /* bnectr+ */
174#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 175
71a39c98 176#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 177#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 178#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 179
07d6d2b8
AM
180#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
181#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
182#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 183
07d6d2b8 184#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
006589cf
AM
185#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
186#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d 187#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
07d6d2b8 188#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
a345bc8d 189
9e390558
AM
190/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
191#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 192 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
193 /* 0: */
194 /* .quad plt0-1f */
195 /* __glink: */
196#define MFLR_R12 0x7d8802a6 /* mflr %12 */
197#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
198 /* 1: */
199#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 200 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 201#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
202#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
203 /* ld %12,0(%11) */
204 /* ld %2,8(%11) */
205 /* mtctr %12 */
206 /* ld %11,16(%11) */
ee4bf8d2 207 /* bctr */
b9e5796b
AM
208#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
209#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
210#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
211#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
212#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
213
214/* Pad with this. */
215#define NOP 0x60000000
216
721956f4
AM
217/* Some other nops. */
218#define CROR_151515 0x4def7b82
219#define CROR_313131 0x4ffffb82
220
cedb70c5 221/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
222#define LI_R0_0 0x38000000 /* li %r0,0 */
223#define B_DOT 0x48000000 /* b . */
224
225/* After that, we need two instructions to load the index, followed by
226 a branch. */
227#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 228#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 229
deb0e272
AM
230/* Instructions used by the save and restore reg functions. */
231#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
232#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
233#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
234#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
235#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
236#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
237#define LI_R12_0 0x39800000 /* li %r12,0 */
238#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
239#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
240#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
241#define BLR 0x4e800020 /* blr */
242
41bd81ab
AM
243/* Since .opd is an array of descriptors and each entry will end up
244 with identical R_PPC64_RELATIVE relocs, there is really no need to
245 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 246 relocate .opd without reloc entries. */
41bd81ab
AM
247#ifndef NO_OPD_RELOCS
248#define NO_OPD_RELOCS 0
249#endif
810d4e75 250
a4b6fadd
AM
251#ifndef ARRAY_SIZE
252#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
253#endif
254
810d4e75
AM
255static inline int
256abiversion (bfd *abfd)
257{
258 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
259}
260
261static inline void
262set_abiversion (bfd *abfd, int ver)
263{
264 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
265 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
266}
5bd4f169 267\f
f5e87a1d 268#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 269
5bd4f169 270/* Relocation HOWTO's. */
04c9666a 271static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
272
273static reloc_howto_type ppc64_elf_howto_raw[] = {
274 /* This reloc does nothing. */
275 HOWTO (R_PPC64_NONE, /* type */
276 0, /* rightshift */
6346d5ca
AM
277 3, /* size (0 = byte, 1 = short, 2 = long) */
278 0, /* bitsize */
b34976b6 279 FALSE, /* pc_relative */
5bd4f169 280 0, /* bitpos */
f5e87a1d 281 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
282 bfd_elf_generic_reloc, /* special_function */
283 "R_PPC64_NONE", /* name */
b34976b6 284 FALSE, /* partial_inplace */
d006db6c 285 0, /* src_mask */
5bd4f169 286 0, /* dst_mask */
b34976b6 287 FALSE), /* pcrel_offset */
5bd4f169
AM
288
289 /* A standard 32 bit relocation. */
290 HOWTO (R_PPC64_ADDR32, /* type */
291 0, /* rightshift */
292 2, /* size (0 = byte, 1 = short, 2 = long) */
293 32, /* bitsize */
b34976b6 294 FALSE, /* pc_relative */
5bd4f169
AM
295 0, /* bitpos */
296 complain_overflow_bitfield, /* complain_on_overflow */
297 bfd_elf_generic_reloc, /* special_function */
298 "R_PPC64_ADDR32", /* name */
b34976b6 299 FALSE, /* partial_inplace */
5bd4f169
AM
300 0, /* src_mask */
301 0xffffffff, /* dst_mask */
b34976b6 302 FALSE), /* pcrel_offset */
5bd4f169
AM
303
304 /* An absolute 26 bit branch; the lower two bits must be zero.
305 FIXME: we don't check that, we just clear them. */
306 HOWTO (R_PPC64_ADDR24, /* type */
307 0, /* rightshift */
308 2, /* size (0 = byte, 1 = short, 2 = long) */
309 26, /* bitsize */
b34976b6 310 FALSE, /* pc_relative */
5bd4f169
AM
311 0, /* bitpos */
312 complain_overflow_bitfield, /* complain_on_overflow */
313 bfd_elf_generic_reloc, /* special_function */
314 "R_PPC64_ADDR24", /* name */
b34976b6 315 FALSE, /* partial_inplace */
d006db6c 316 0, /* src_mask */
f5e87a1d 317 0x03fffffc, /* dst_mask */
b34976b6 318 FALSE), /* pcrel_offset */
5bd4f169
AM
319
320 /* A standard 16 bit relocation. */
321 HOWTO (R_PPC64_ADDR16, /* type */
322 0, /* rightshift */
323 1, /* size (0 = byte, 1 = short, 2 = long) */
324 16, /* bitsize */
b34976b6 325 FALSE, /* pc_relative */
5bd4f169
AM
326 0, /* bitpos */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_PPC64_ADDR16", /* name */
b34976b6 330 FALSE, /* partial_inplace */
5bd4f169
AM
331 0, /* src_mask */
332 0xffff, /* dst_mask */
b34976b6 333 FALSE), /* pcrel_offset */
5bd4f169
AM
334
335 /* A 16 bit relocation without overflow. */
336 HOWTO (R_PPC64_ADDR16_LO, /* type */
337 0, /* rightshift */
338 1, /* size (0 = byte, 1 = short, 2 = long) */
339 16, /* bitsize */
b34976b6 340 FALSE, /* pc_relative */
5bd4f169
AM
341 0, /* bitpos */
342 complain_overflow_dont,/* complain_on_overflow */
343 bfd_elf_generic_reloc, /* special_function */
344 "R_PPC64_ADDR16_LO", /* name */
b34976b6 345 FALSE, /* partial_inplace */
5bd4f169
AM
346 0, /* src_mask */
347 0xffff, /* dst_mask */
b34976b6 348 FALSE), /* pcrel_offset */
5bd4f169
AM
349
350 /* Bits 16-31 of an address. */
351 HOWTO (R_PPC64_ADDR16_HI, /* type */
352 16, /* rightshift */
353 1, /* size (0 = byte, 1 = short, 2 = long) */
354 16, /* bitsize */
b34976b6 355 FALSE, /* pc_relative */
5bd4f169 356 0, /* bitpos */
f9c6b907 357 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
358 bfd_elf_generic_reloc, /* special_function */
359 "R_PPC64_ADDR16_HI", /* name */
b34976b6 360 FALSE, /* partial_inplace */
5bd4f169
AM
361 0, /* src_mask */
362 0xffff, /* dst_mask */
b34976b6 363 FALSE), /* pcrel_offset */
5bd4f169
AM
364
365 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
366 bits, treated as a signed number, is negative. */
367 HOWTO (R_PPC64_ADDR16_HA, /* type */
368 16, /* rightshift */
369 1, /* size (0 = byte, 1 = short, 2 = long) */
370 16, /* bitsize */
b34976b6 371 FALSE, /* pc_relative */
5bd4f169 372 0, /* bitpos */
f9c6b907 373 complain_overflow_signed, /* complain_on_overflow */
805fc799 374 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 375 "R_PPC64_ADDR16_HA", /* name */
b34976b6 376 FALSE, /* partial_inplace */
5bd4f169
AM
377 0, /* src_mask */
378 0xffff, /* dst_mask */
b34976b6 379 FALSE), /* pcrel_offset */
5bd4f169
AM
380
381 /* An absolute 16 bit branch; the lower two bits must be zero.
382 FIXME: we don't check that, we just clear them. */
383 HOWTO (R_PPC64_ADDR14, /* type */
384 0, /* rightshift */
385 2, /* size (0 = byte, 1 = short, 2 = long) */
386 16, /* bitsize */
b34976b6 387 FALSE, /* pc_relative */
5bd4f169 388 0, /* bitpos */
b80eed39 389 complain_overflow_signed, /* complain_on_overflow */
2441e016 390 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 391 "R_PPC64_ADDR14", /* name */
b34976b6 392 FALSE, /* partial_inplace */
d006db6c 393 0, /* src_mask */
f5e87a1d 394 0x0000fffc, /* dst_mask */
b34976b6 395 FALSE), /* pcrel_offset */
5bd4f169
AM
396
397 /* An absolute 16 bit branch, for which bit 10 should be set to
398 indicate that the branch is expected to be taken. The lower two
399 bits must be zero. */
400 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
401 0, /* rightshift */
402 2, /* size (0 = byte, 1 = short, 2 = long) */
403 16, /* bitsize */
b34976b6 404 FALSE, /* pc_relative */
5bd4f169 405 0, /* bitpos */
b80eed39 406 complain_overflow_signed, /* complain_on_overflow */
805fc799 407 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 408 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 409 FALSE, /* partial_inplace */
d006db6c 410 0, /* src_mask */
f5e87a1d 411 0x0000fffc, /* dst_mask */
b34976b6 412 FALSE), /* pcrel_offset */
5bd4f169
AM
413
414 /* An absolute 16 bit branch, for which bit 10 should be set to
415 indicate that the branch is not expected to be taken. The lower
416 two bits must be zero. */
417 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
418 0, /* rightshift */
419 2, /* size (0 = byte, 1 = short, 2 = long) */
420 16, /* bitsize */
b34976b6 421 FALSE, /* pc_relative */
5bd4f169 422 0, /* bitpos */
b80eed39 423 complain_overflow_signed, /* complain_on_overflow */
805fc799 424 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 425 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 426 FALSE, /* partial_inplace */
d006db6c 427 0, /* src_mask */
f5e87a1d 428 0x0000fffc, /* dst_mask */
b34976b6 429 FALSE), /* pcrel_offset */
5bd4f169
AM
430
431 /* A relative 26 bit branch; the lower two bits must be zero. */
432 HOWTO (R_PPC64_REL24, /* type */
433 0, /* rightshift */
434 2, /* size (0 = byte, 1 = short, 2 = long) */
435 26, /* bitsize */
b34976b6 436 TRUE, /* pc_relative */
5bd4f169
AM
437 0, /* bitpos */
438 complain_overflow_signed, /* complain_on_overflow */
2441e016 439 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 440 "R_PPC64_REL24", /* name */
b34976b6 441 FALSE, /* partial_inplace */
d006db6c 442 0, /* src_mask */
f5e87a1d 443 0x03fffffc, /* dst_mask */
b34976b6 444 TRUE), /* pcrel_offset */
5bd4f169
AM
445
446 /* A relative 16 bit branch; the lower two bits must be zero. */
447 HOWTO (R_PPC64_REL14, /* type */
448 0, /* rightshift */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
450 16, /* bitsize */
b34976b6 451 TRUE, /* pc_relative */
5bd4f169
AM
452 0, /* bitpos */
453 complain_overflow_signed, /* complain_on_overflow */
2441e016 454 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 455 "R_PPC64_REL14", /* name */
b34976b6 456 FALSE, /* partial_inplace */
d006db6c 457 0, /* src_mask */
f5e87a1d 458 0x0000fffc, /* dst_mask */
b34976b6 459 TRUE), /* pcrel_offset */
5bd4f169
AM
460
461 /* A relative 16 bit branch. Bit 10 should be set to indicate that
462 the branch is expected to be taken. The lower two bits must be
463 zero. */
464 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
465 0, /* rightshift */
466 2, /* size (0 = byte, 1 = short, 2 = long) */
467 16, /* bitsize */
b34976b6 468 TRUE, /* pc_relative */
5bd4f169
AM
469 0, /* bitpos */
470 complain_overflow_signed, /* complain_on_overflow */
805fc799 471 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 472 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 473 FALSE, /* partial_inplace */
d006db6c 474 0, /* src_mask */
f5e87a1d 475 0x0000fffc, /* dst_mask */
b34976b6 476 TRUE), /* pcrel_offset */
5bd4f169
AM
477
478 /* A relative 16 bit branch. Bit 10 should be set to indicate that
479 the branch is not expected to be taken. The lower two bits must
480 be zero. */
481 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
482 0, /* rightshift */
483 2, /* size (0 = byte, 1 = short, 2 = long) */
484 16, /* bitsize */
b34976b6 485 TRUE, /* pc_relative */
5bd4f169
AM
486 0, /* bitpos */
487 complain_overflow_signed, /* complain_on_overflow */
805fc799 488 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 489 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 490 FALSE, /* partial_inplace */
d006db6c 491 0, /* src_mask */
f5e87a1d 492 0x0000fffc, /* dst_mask */
b34976b6 493 TRUE), /* pcrel_offset */
5bd4f169
AM
494
495 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
496 symbol. */
497 HOWTO (R_PPC64_GOT16, /* type */
498 0, /* rightshift */
499 1, /* size (0 = byte, 1 = short, 2 = long) */
500 16, /* bitsize */
b34976b6 501 FALSE, /* pc_relative */
5bd4f169
AM
502 0, /* bitpos */
503 complain_overflow_signed, /* complain_on_overflow */
805fc799 504 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 505 "R_PPC64_GOT16", /* name */
b34976b6 506 FALSE, /* partial_inplace */
5bd4f169
AM
507 0, /* src_mask */
508 0xffff, /* dst_mask */
b34976b6 509 FALSE), /* pcrel_offset */
5bd4f169
AM
510
511 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
512 the symbol. */
513 HOWTO (R_PPC64_GOT16_LO, /* type */
514 0, /* rightshift */
515 1, /* size (0 = byte, 1 = short, 2 = long) */
516 16, /* bitsize */
b34976b6 517 FALSE, /* pc_relative */
5bd4f169
AM
518 0, /* bitpos */
519 complain_overflow_dont, /* complain_on_overflow */
805fc799 520 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 521 "R_PPC64_GOT16_LO", /* name */
b34976b6 522 FALSE, /* partial_inplace */
5bd4f169
AM
523 0, /* src_mask */
524 0xffff, /* dst_mask */
b34976b6 525 FALSE), /* pcrel_offset */
5bd4f169
AM
526
527 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
528 the symbol. */
529 HOWTO (R_PPC64_GOT16_HI, /* type */
530 16, /* rightshift */
531 1, /* size (0 = byte, 1 = short, 2 = long) */
532 16, /* bitsize */
b34976b6 533 FALSE, /* pc_relative */
5bd4f169 534 0, /* bitpos */
f9c6b907 535 complain_overflow_signed,/* complain_on_overflow */
805fc799 536 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 537 "R_PPC64_GOT16_HI", /* name */
b34976b6 538 FALSE, /* partial_inplace */
5bd4f169
AM
539 0, /* src_mask */
540 0xffff, /* dst_mask */
b34976b6 541 FALSE), /* pcrel_offset */
5bd4f169
AM
542
543 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
544 the symbol. */
545 HOWTO (R_PPC64_GOT16_HA, /* type */
546 16, /* rightshift */
547 1, /* size (0 = byte, 1 = short, 2 = long) */
548 16, /* bitsize */
b34976b6 549 FALSE, /* pc_relative */
5bd4f169 550 0, /* bitpos */
f9c6b907 551 complain_overflow_signed,/* complain_on_overflow */
805fc799 552 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 553 "R_PPC64_GOT16_HA", /* name */
b34976b6 554 FALSE, /* partial_inplace */
5bd4f169
AM
555 0, /* src_mask */
556 0xffff, /* dst_mask */
b34976b6 557 FALSE), /* pcrel_offset */
5bd4f169
AM
558
559 /* This is used only by the dynamic linker. The symbol should exist
560 both in the object being run and in some shared library. The
561 dynamic linker copies the data addressed by the symbol from the
562 shared library into the object, because the object being
563 run has to have the data at some particular address. */
564 HOWTO (R_PPC64_COPY, /* type */
565 0, /* rightshift */
f5e87a1d
AM
566 0, /* this one is variable size */
567 0, /* bitsize */
b34976b6 568 FALSE, /* pc_relative */
5bd4f169 569 0, /* bitpos */
f5e87a1d
AM
570 complain_overflow_dont, /* complain_on_overflow */
571 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 572 "R_PPC64_COPY", /* name */
b34976b6 573 FALSE, /* partial_inplace */
5bd4f169
AM
574 0, /* src_mask */
575 0, /* dst_mask */
b34976b6 576 FALSE), /* pcrel_offset */
5bd4f169
AM
577
578 /* Like R_PPC64_ADDR64, but used when setting global offset table
579 entries. */
580 HOWTO (R_PPC64_GLOB_DAT, /* type */
581 0, /* rightshift */
582 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
583 64, /* bitsize */
b34976b6 584 FALSE, /* pc_relative */
5bd4f169
AM
585 0, /* bitpos */
586 complain_overflow_dont, /* complain_on_overflow */
805fc799 587 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 588 "R_PPC64_GLOB_DAT", /* name */
b34976b6 589 FALSE, /* partial_inplace */
5bd4f169 590 0, /* src_mask */
f5e87a1d 591 ONES (64), /* dst_mask */
b34976b6 592 FALSE), /* pcrel_offset */
5bd4f169
AM
593
594 /* Created by the link editor. Marks a procedure linkage table
595 entry for a symbol. */
596 HOWTO (R_PPC64_JMP_SLOT, /* type */
597 0, /* rightshift */
598 0, /* size (0 = byte, 1 = short, 2 = long) */
599 0, /* bitsize */
b34976b6 600 FALSE, /* pc_relative */
5bd4f169
AM
601 0, /* bitpos */
602 complain_overflow_dont, /* complain_on_overflow */
805fc799 603 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 604 "R_PPC64_JMP_SLOT", /* name */
b34976b6 605 FALSE, /* partial_inplace */
5bd4f169
AM
606 0, /* src_mask */
607 0, /* dst_mask */
b34976b6 608 FALSE), /* pcrel_offset */
5bd4f169
AM
609
610 /* Used only by the dynamic linker. When the object is run, this
611 doubleword64 is set to the load address of the object, plus the
612 addend. */
613 HOWTO (R_PPC64_RELATIVE, /* type */
614 0, /* rightshift */
615 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
616 64, /* bitsize */
b34976b6 617 FALSE, /* pc_relative */
5bd4f169
AM
618 0, /* bitpos */
619 complain_overflow_dont, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_PPC64_RELATIVE", /* name */
b34976b6 622 FALSE, /* partial_inplace */
5bd4f169 623 0, /* src_mask */
f5e87a1d 624 ONES (64), /* dst_mask */
b34976b6 625 FALSE), /* pcrel_offset */
5bd4f169
AM
626
627 /* Like R_PPC64_ADDR32, but may be unaligned. */
628 HOWTO (R_PPC64_UADDR32, /* type */
629 0, /* rightshift */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
631 32, /* bitsize */
b34976b6 632 FALSE, /* pc_relative */
5bd4f169
AM
633 0, /* bitpos */
634 complain_overflow_bitfield, /* complain_on_overflow */
635 bfd_elf_generic_reloc, /* special_function */
636 "R_PPC64_UADDR32", /* name */
b34976b6 637 FALSE, /* partial_inplace */
5bd4f169
AM
638 0, /* src_mask */
639 0xffffffff, /* dst_mask */
b34976b6 640 FALSE), /* pcrel_offset */
5bd4f169
AM
641
642 /* Like R_PPC64_ADDR16, but may be unaligned. */
643 HOWTO (R_PPC64_UADDR16, /* type */
644 0, /* rightshift */
645 1, /* size (0 = byte, 1 = short, 2 = long) */
646 16, /* bitsize */
b34976b6 647 FALSE, /* pc_relative */
5bd4f169
AM
648 0, /* bitpos */
649 complain_overflow_bitfield, /* complain_on_overflow */
650 bfd_elf_generic_reloc, /* special_function */
651 "R_PPC64_UADDR16", /* name */
b34976b6 652 FALSE, /* partial_inplace */
5bd4f169
AM
653 0, /* src_mask */
654 0xffff, /* dst_mask */
b34976b6 655 FALSE), /* pcrel_offset */
5bd4f169
AM
656
657 /* 32-bit PC relative. */
658 HOWTO (R_PPC64_REL32, /* type */
659 0, /* rightshift */
660 2, /* size (0 = byte, 1 = short, 2 = long) */
661 32, /* bitsize */
b34976b6 662 TRUE, /* pc_relative */
5bd4f169 663 0, /* bitpos */
5bd4f169
AM
664 complain_overflow_signed, /* complain_on_overflow */
665 bfd_elf_generic_reloc, /* special_function */
666 "R_PPC64_REL32", /* name */
b34976b6 667 FALSE, /* partial_inplace */
5bd4f169
AM
668 0, /* src_mask */
669 0xffffffff, /* dst_mask */
b34976b6 670 TRUE), /* pcrel_offset */
5bd4f169 671
10ed1bba 672 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
673 HOWTO (R_PPC64_PLT32, /* type */
674 0, /* rightshift */
675 2, /* size (0 = byte, 1 = short, 2 = long) */
676 32, /* bitsize */
b34976b6 677 FALSE, /* pc_relative */
5bd4f169
AM
678 0, /* bitpos */
679 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 680 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 681 "R_PPC64_PLT32", /* name */
b34976b6 682 FALSE, /* partial_inplace */
5bd4f169 683 0, /* src_mask */
f5e87a1d 684 0xffffffff, /* dst_mask */
b34976b6 685 FALSE), /* pcrel_offset */
5bd4f169
AM
686
687 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
688 FIXME: R_PPC64_PLTREL32 not supported. */
689 HOWTO (R_PPC64_PLTREL32, /* type */
690 0, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 32, /* bitsize */
b34976b6 693 TRUE, /* pc_relative */
5bd4f169
AM
694 0, /* bitpos */
695 complain_overflow_signed, /* complain_on_overflow */
3ce51288 696 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 697 "R_PPC64_PLTREL32", /* name */
b34976b6 698 FALSE, /* partial_inplace */
5bd4f169 699 0, /* src_mask */
f5e87a1d 700 0xffffffff, /* dst_mask */
b34976b6 701 TRUE), /* pcrel_offset */
5bd4f169
AM
702
703 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
704 the symbol. */
705 HOWTO (R_PPC64_PLT16_LO, /* type */
706 0, /* rightshift */
707 1, /* size (0 = byte, 1 = short, 2 = long) */
708 16, /* bitsize */
b34976b6 709 FALSE, /* pc_relative */
5bd4f169
AM
710 0, /* bitpos */
711 complain_overflow_dont, /* complain_on_overflow */
805fc799 712 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 713 "R_PPC64_PLT16_LO", /* name */
b34976b6 714 FALSE, /* partial_inplace */
5bd4f169
AM
715 0, /* src_mask */
716 0xffff, /* dst_mask */
b34976b6 717 FALSE), /* pcrel_offset */
5bd4f169
AM
718
719 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
720 the symbol. */
721 HOWTO (R_PPC64_PLT16_HI, /* type */
722 16, /* rightshift */
723 1, /* size (0 = byte, 1 = short, 2 = long) */
724 16, /* bitsize */
b34976b6 725 FALSE, /* pc_relative */
5bd4f169 726 0, /* bitpos */
f9c6b907 727 complain_overflow_signed, /* complain_on_overflow */
805fc799 728 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 729 "R_PPC64_PLT16_HI", /* name */
b34976b6 730 FALSE, /* partial_inplace */
5bd4f169
AM
731 0, /* src_mask */
732 0xffff, /* dst_mask */
b34976b6 733 FALSE), /* pcrel_offset */
5bd4f169
AM
734
735 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
736 the symbol. */
737 HOWTO (R_PPC64_PLT16_HA, /* type */
738 16, /* rightshift */
739 1, /* size (0 = byte, 1 = short, 2 = long) */
740 16, /* bitsize */
b34976b6 741 FALSE, /* pc_relative */
5bd4f169 742 0, /* bitpos */
f9c6b907 743 complain_overflow_signed, /* complain_on_overflow */
805fc799 744 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 745 "R_PPC64_PLT16_HA", /* name */
b34976b6 746 FALSE, /* partial_inplace */
5bd4f169
AM
747 0, /* src_mask */
748 0xffff, /* dst_mask */
b34976b6 749 FALSE), /* pcrel_offset */
5bd4f169 750
c061c2d8 751 /* 16-bit section relative relocation. */
5bd4f169
AM
752 HOWTO (R_PPC64_SECTOFF, /* type */
753 0, /* rightshift */
c061c2d8
AM
754 1, /* size (0 = byte, 1 = short, 2 = long) */
755 16, /* bitsize */
b34976b6 756 FALSE, /* pc_relative */
5bd4f169 757 0, /* bitpos */
b80eed39 758 complain_overflow_signed, /* complain_on_overflow */
805fc799 759 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 760 "R_PPC64_SECTOFF", /* name */
b34976b6 761 FALSE, /* partial_inplace */
5bd4f169 762 0, /* src_mask */
c061c2d8 763 0xffff, /* dst_mask */
b34976b6 764 FALSE), /* pcrel_offset */
5bd4f169 765
c061c2d8 766 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
767 HOWTO (R_PPC64_SECTOFF_LO, /* type */
768 0, /* rightshift */
769 1, /* size (0 = byte, 1 = short, 2 = long) */
770 16, /* bitsize */
b34976b6 771 FALSE, /* pc_relative */
5bd4f169
AM
772 0, /* bitpos */
773 complain_overflow_dont, /* complain_on_overflow */
805fc799 774 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 775 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 776 FALSE, /* partial_inplace */
5bd4f169
AM
777 0, /* src_mask */
778 0xffff, /* dst_mask */
b34976b6 779 FALSE), /* pcrel_offset */
5bd4f169
AM
780
781 /* 16-bit upper half section relative relocation. */
782 HOWTO (R_PPC64_SECTOFF_HI, /* type */
783 16, /* rightshift */
784 1, /* size (0 = byte, 1 = short, 2 = long) */
785 16, /* bitsize */
b34976b6 786 FALSE, /* pc_relative */
5bd4f169 787 0, /* bitpos */
f9c6b907 788 complain_overflow_signed, /* complain_on_overflow */
805fc799 789 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 790 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 791 FALSE, /* partial_inplace */
5bd4f169
AM
792 0, /* src_mask */
793 0xffff, /* dst_mask */
b34976b6 794 FALSE), /* pcrel_offset */
5bd4f169
AM
795
796 /* 16-bit upper half adjusted section relative relocation. */
797 HOWTO (R_PPC64_SECTOFF_HA, /* type */
798 16, /* rightshift */
799 1, /* size (0 = byte, 1 = short, 2 = long) */
800 16, /* bitsize */
b34976b6 801 FALSE, /* pc_relative */
5bd4f169 802 0, /* bitpos */
f9c6b907 803 complain_overflow_signed, /* complain_on_overflow */
805fc799 804 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 805 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 806 FALSE, /* partial_inplace */
5bd4f169
AM
807 0, /* src_mask */
808 0xffff, /* dst_mask */
b34976b6 809 FALSE), /* pcrel_offset */
5bd4f169 810
04c9666a
AM
811 /* Like R_PPC64_REL24 without touching the two least significant bits. */
812 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
813 2, /* rightshift */
814 2, /* size (0 = byte, 1 = short, 2 = long) */
815 30, /* bitsize */
b34976b6 816 TRUE, /* pc_relative */
5bd4f169
AM
817 0, /* bitpos */
818 complain_overflow_dont, /* complain_on_overflow */
819 bfd_elf_generic_reloc, /* special_function */
04c9666a 820 "R_PPC64_REL30", /* name */
b34976b6 821 FALSE, /* partial_inplace */
d006db6c 822 0, /* src_mask */
5bd4f169 823 0xfffffffc, /* dst_mask */
b34976b6 824 TRUE), /* pcrel_offset */
5bd4f169
AM
825
826 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
827
828 /* A standard 64-bit relocation. */
829 HOWTO (R_PPC64_ADDR64, /* type */
830 0, /* rightshift */
831 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
832 64, /* bitsize */
b34976b6 833 FALSE, /* pc_relative */
5bd4f169
AM
834 0, /* bitpos */
835 complain_overflow_dont, /* complain_on_overflow */
836 bfd_elf_generic_reloc, /* special_function */
837 "R_PPC64_ADDR64", /* name */
b34976b6 838 FALSE, /* partial_inplace */
5bd4f169 839 0, /* src_mask */
f5e87a1d 840 ONES (64), /* dst_mask */
b34976b6 841 FALSE), /* pcrel_offset */
5bd4f169
AM
842
843 /* The bits 32-47 of an address. */
844 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
845 32, /* rightshift */
846 1, /* size (0 = byte, 1 = short, 2 = long) */
847 16, /* bitsize */
b34976b6 848 FALSE, /* pc_relative */
5bd4f169
AM
849 0, /* bitpos */
850 complain_overflow_dont, /* complain_on_overflow */
851 bfd_elf_generic_reloc, /* special_function */
852 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 853 FALSE, /* partial_inplace */
5bd4f169
AM
854 0, /* src_mask */
855 0xffff, /* dst_mask */
b34976b6 856 FALSE), /* pcrel_offset */
5bd4f169
AM
857
858 /* The bits 32-47 of an address, plus 1 if the contents of the low
859 16 bits, treated as a signed number, is negative. */
860 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
861 32, /* rightshift */
862 1, /* size (0 = byte, 1 = short, 2 = long) */
863 16, /* bitsize */
b34976b6 864 FALSE, /* pc_relative */
5bd4f169
AM
865 0, /* bitpos */
866 complain_overflow_dont, /* complain_on_overflow */
805fc799 867 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 868 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 869 FALSE, /* partial_inplace */
5bd4f169
AM
870 0, /* src_mask */
871 0xffff, /* dst_mask */
b34976b6 872 FALSE), /* pcrel_offset */
5bd4f169
AM
873
874 /* The bits 48-63 of an address. */
875 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
876 48, /* rightshift */
877 1, /* size (0 = byte, 1 = short, 2 = long) */
878 16, /* bitsize */
b34976b6 879 FALSE, /* pc_relative */
5bd4f169
AM
880 0, /* bitpos */
881 complain_overflow_dont, /* complain_on_overflow */
882 bfd_elf_generic_reloc, /* special_function */
883 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 884 FALSE, /* partial_inplace */
5bd4f169
AM
885 0, /* src_mask */
886 0xffff, /* dst_mask */
b34976b6 887 FALSE), /* pcrel_offset */
5bd4f169
AM
888
889 /* The bits 48-63 of an address, plus 1 if the contents of the low
890 16 bits, treated as a signed number, is negative. */
891 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
892 48, /* rightshift */
893 1, /* size (0 = byte, 1 = short, 2 = long) */
894 16, /* bitsize */
b34976b6 895 FALSE, /* pc_relative */
5bd4f169
AM
896 0, /* bitpos */
897 complain_overflow_dont, /* complain_on_overflow */
805fc799 898 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 899 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 900 FALSE, /* partial_inplace */
5bd4f169
AM
901 0, /* src_mask */
902 0xffff, /* dst_mask */
b34976b6 903 FALSE), /* pcrel_offset */
5bd4f169
AM
904
905 /* Like ADDR64, but may be unaligned. */
906 HOWTO (R_PPC64_UADDR64, /* type */
907 0, /* rightshift */
908 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
909 64, /* bitsize */
b34976b6 910 FALSE, /* pc_relative */
5bd4f169
AM
911 0, /* bitpos */
912 complain_overflow_dont, /* complain_on_overflow */
913 bfd_elf_generic_reloc, /* special_function */
914 "R_PPC64_UADDR64", /* name */
b34976b6 915 FALSE, /* partial_inplace */
5bd4f169 916 0, /* src_mask */
f5e87a1d 917 ONES (64), /* dst_mask */
b34976b6 918 FALSE), /* pcrel_offset */
5bd4f169
AM
919
920 /* 64-bit relative relocation. */
921 HOWTO (R_PPC64_REL64, /* type */
922 0, /* rightshift */
923 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
924 64, /* bitsize */
b34976b6 925 TRUE, /* pc_relative */
5bd4f169
AM
926 0, /* bitpos */
927 complain_overflow_dont, /* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_PPC64_REL64", /* name */
b34976b6 930 FALSE, /* partial_inplace */
5bd4f169 931 0, /* src_mask */
f5e87a1d 932 ONES (64), /* dst_mask */
b34976b6 933 TRUE), /* pcrel_offset */
5bd4f169 934
cedb70c5 935 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
936 HOWTO (R_PPC64_PLT64, /* type */
937 0, /* rightshift */
938 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
939 64, /* bitsize */
b34976b6 940 FALSE, /* pc_relative */
5bd4f169
AM
941 0, /* bitpos */
942 complain_overflow_dont, /* complain_on_overflow */
805fc799 943 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 944 "R_PPC64_PLT64", /* name */
b34976b6 945 FALSE, /* partial_inplace */
5bd4f169 946 0, /* src_mask */
f5e87a1d 947 ONES (64), /* dst_mask */
b34976b6 948 FALSE), /* pcrel_offset */
5bd4f169
AM
949
950 /* 64-bit PC relative relocation to the symbol's procedure linkage
951 table. */
952 /* FIXME: R_PPC64_PLTREL64 not supported. */
953 HOWTO (R_PPC64_PLTREL64, /* type */
954 0, /* rightshift */
955 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
956 64, /* bitsize */
b34976b6 957 TRUE, /* pc_relative */
5bd4f169
AM
958 0, /* bitpos */
959 complain_overflow_dont, /* complain_on_overflow */
805fc799 960 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 961 "R_PPC64_PLTREL64", /* name */
b34976b6 962 FALSE, /* partial_inplace */
5bd4f169 963 0, /* src_mask */
f5e87a1d 964 ONES (64), /* dst_mask */
b34976b6 965 TRUE), /* pcrel_offset */
5bd4f169
AM
966
967 /* 16 bit TOC-relative relocation. */
968
969 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
970 HOWTO (R_PPC64_TOC16, /* type */
971 0, /* rightshift */
972 1, /* size (0 = byte, 1 = short, 2 = long) */
973 16, /* bitsize */
b34976b6 974 FALSE, /* pc_relative */
5bd4f169
AM
975 0, /* bitpos */
976 complain_overflow_signed, /* complain_on_overflow */
805fc799 977 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 978 "R_PPC64_TOC16", /* name */
b34976b6 979 FALSE, /* partial_inplace */
5bd4f169
AM
980 0, /* src_mask */
981 0xffff, /* dst_mask */
b34976b6 982 FALSE), /* pcrel_offset */
5bd4f169
AM
983
984 /* 16 bit TOC-relative relocation without overflow. */
985
986 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
987 HOWTO (R_PPC64_TOC16_LO, /* type */
988 0, /* rightshift */
989 1, /* size (0 = byte, 1 = short, 2 = long) */
990 16, /* bitsize */
b34976b6 991 FALSE, /* pc_relative */
5bd4f169
AM
992 0, /* bitpos */
993 complain_overflow_dont, /* complain_on_overflow */
805fc799 994 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 995 "R_PPC64_TOC16_LO", /* name */
b34976b6 996 FALSE, /* partial_inplace */
5bd4f169
AM
997 0, /* src_mask */
998 0xffff, /* dst_mask */
b34976b6 999 FALSE), /* pcrel_offset */
5bd4f169
AM
1000
1001 /* 16 bit TOC-relative relocation, high 16 bits. */
1002
1003 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1004 HOWTO (R_PPC64_TOC16_HI, /* type */
1005 16, /* rightshift */
1006 1, /* size (0 = byte, 1 = short, 2 = long) */
1007 16, /* bitsize */
b34976b6 1008 FALSE, /* pc_relative */
5bd4f169 1009 0, /* bitpos */
f9c6b907 1010 complain_overflow_signed, /* complain_on_overflow */
805fc799 1011 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1012 "R_PPC64_TOC16_HI", /* name */
b34976b6 1013 FALSE, /* partial_inplace */
5bd4f169
AM
1014 0, /* src_mask */
1015 0xffff, /* dst_mask */
b34976b6 1016 FALSE), /* pcrel_offset */
5bd4f169
AM
1017
1018 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1019 contents of the low 16 bits, treated as a signed number, is
1020 negative. */
1021
1022 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1023 HOWTO (R_PPC64_TOC16_HA, /* type */
1024 16, /* rightshift */
1025 1, /* size (0 = byte, 1 = short, 2 = long) */
1026 16, /* bitsize */
b34976b6 1027 FALSE, /* pc_relative */
5bd4f169 1028 0, /* bitpos */
f9c6b907 1029 complain_overflow_signed, /* complain_on_overflow */
805fc799 1030 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1031 "R_PPC64_TOC16_HA", /* name */
b34976b6 1032 FALSE, /* partial_inplace */
5bd4f169
AM
1033 0, /* src_mask */
1034 0xffff, /* dst_mask */
b34976b6 1035 FALSE), /* pcrel_offset */
5bd4f169
AM
1036
1037 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1038
1039 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1040 HOWTO (R_PPC64_TOC, /* type */
1041 0, /* rightshift */
1042 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1043 64, /* bitsize */
b34976b6 1044 FALSE, /* pc_relative */
5bd4f169 1045 0, /* bitpos */
b80eed39 1046 complain_overflow_dont, /* complain_on_overflow */
805fc799 1047 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1048 "R_PPC64_TOC", /* name */
b34976b6 1049 FALSE, /* partial_inplace */
5bd4f169 1050 0, /* src_mask */
f5e87a1d 1051 ONES (64), /* dst_mask */
b34976b6 1052 FALSE), /* pcrel_offset */
5bd4f169
AM
1053
1054 /* Like R_PPC64_GOT16, but also informs the link editor that the
1055 value to relocate may (!) refer to a PLT entry which the link
1056 editor (a) may replace with the symbol value. If the link editor
1057 is unable to fully resolve the symbol, it may (b) create a PLT
1058 entry and store the address to the new PLT entry in the GOT.
1059 This permits lazy resolution of function symbols at run time.
1060 The link editor may also skip all of this and just (c) emit a
1061 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1062 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1063 HOWTO (R_PPC64_PLTGOT16, /* type */
1064 0, /* rightshift */
1065 1, /* size (0 = byte, 1 = short, 2 = long) */
1066 16, /* bitsize */
b34976b6 1067 FALSE, /* pc_relative */
5bd4f169
AM
1068 0, /* bitpos */
1069 complain_overflow_signed, /* complain_on_overflow */
805fc799 1070 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1071 "R_PPC64_PLTGOT16", /* name */
1072 FALSE, /* partial_inplace */
1073 0, /* src_mask */
1074 0xffff, /* dst_mask */
1075 FALSE), /* pcrel_offset */
1076
1077 /* Like R_PPC64_PLTGOT16, but without overflow. */
1078 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1079 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1080 0, /* rightshift */
1081 1, /* size (0 = byte, 1 = short, 2 = long) */
1082 16, /* bitsize */
1083 FALSE, /* pc_relative */
1084 0, /* bitpos */
1085 complain_overflow_dont, /* complain_on_overflow */
1086 ppc64_elf_unhandled_reloc, /* special_function */
1087 "R_PPC64_PLTGOT16_LO", /* name */
1088 FALSE, /* partial_inplace */
1089 0, /* src_mask */
1090 0xffff, /* dst_mask */
1091 FALSE), /* pcrel_offset */
1092
1093 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1094 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1095 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1096 16, /* rightshift */
1097 1, /* size (0 = byte, 1 = short, 2 = long) */
1098 16, /* bitsize */
1099 FALSE, /* pc_relative */
1100 0, /* bitpos */
f9c6b907 1101 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1102 ppc64_elf_unhandled_reloc, /* special_function */
1103 "R_PPC64_PLTGOT16_HI", /* name */
1104 FALSE, /* partial_inplace */
1105 0, /* src_mask */
1106 0xffff, /* dst_mask */
1107 FALSE), /* pcrel_offset */
1108
1109 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1110 1 if the contents of the low 16 bits, treated as a signed number,
1111 is negative. */
1112 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1113 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1114 16, /* rightshift */
1115 1, /* size (0 = byte, 1 = short, 2 = long) */
1116 16, /* bitsize */
1117 FALSE, /* pc_relative */
1118 0, /* bitpos */
f9c6b907 1119 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1120 ppc64_elf_unhandled_reloc, /* special_function */
1121 "R_PPC64_PLTGOT16_HA", /* name */
1122 FALSE, /* partial_inplace */
1123 0, /* src_mask */
1124 0xffff, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1126
1127 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1128 HOWTO (R_PPC64_ADDR16_DS, /* type */
1129 0, /* rightshift */
1130 1, /* size (0 = byte, 1 = short, 2 = long) */
1131 16, /* bitsize */
1132 FALSE, /* pc_relative */
1133 0, /* bitpos */
b80eed39 1134 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1135 bfd_elf_generic_reloc, /* special_function */
1136 "R_PPC64_ADDR16_DS", /* name */
1137 FALSE, /* partial_inplace */
1138 0, /* src_mask */
1139 0xfffc, /* dst_mask */
1140 FALSE), /* pcrel_offset */
1141
1142 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1143 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1144 0, /* rightshift */
1145 1, /* size (0 = byte, 1 = short, 2 = long) */
1146 16, /* bitsize */
1147 FALSE, /* pc_relative */
1148 0, /* bitpos */
1149 complain_overflow_dont,/* complain_on_overflow */
1150 bfd_elf_generic_reloc, /* special_function */
1151 "R_PPC64_ADDR16_LO_DS",/* name */
1152 FALSE, /* partial_inplace */
1153 0, /* src_mask */
1154 0xfffc, /* dst_mask */
1155 FALSE), /* pcrel_offset */
1156
1157 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1158 HOWTO (R_PPC64_GOT16_DS, /* type */
1159 0, /* rightshift */
1160 1, /* size (0 = byte, 1 = short, 2 = long) */
1161 16, /* bitsize */
1162 FALSE, /* pc_relative */
1163 0, /* bitpos */
1164 complain_overflow_signed, /* complain_on_overflow */
1165 ppc64_elf_unhandled_reloc, /* special_function */
1166 "R_PPC64_GOT16_DS", /* name */
1167 FALSE, /* partial_inplace */
1168 0, /* src_mask */
1169 0xfffc, /* dst_mask */
1170 FALSE), /* pcrel_offset */
1171
1172 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1173 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1174 0, /* rightshift */
1175 1, /* size (0 = byte, 1 = short, 2 = long) */
1176 16, /* bitsize */
1177 FALSE, /* pc_relative */
1178 0, /* bitpos */
1179 complain_overflow_dont, /* complain_on_overflow */
1180 ppc64_elf_unhandled_reloc, /* special_function */
1181 "R_PPC64_GOT16_LO_DS", /* name */
1182 FALSE, /* partial_inplace */
1183 0, /* src_mask */
1184 0xfffc, /* dst_mask */
1185 FALSE), /* pcrel_offset */
1186
1187 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1188 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1189 0, /* rightshift */
1190 1, /* size (0 = byte, 1 = short, 2 = long) */
1191 16, /* bitsize */
1192 FALSE, /* pc_relative */
1193 0, /* bitpos */
1194 complain_overflow_dont, /* complain_on_overflow */
1195 ppc64_elf_unhandled_reloc, /* special_function */
1196 "R_PPC64_PLT16_LO_DS", /* name */
1197 FALSE, /* partial_inplace */
1198 0, /* src_mask */
1199 0xfffc, /* dst_mask */
1200 FALSE), /* pcrel_offset */
1201
1202 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1203 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1204 0, /* rightshift */
1205 1, /* size (0 = byte, 1 = short, 2 = long) */
1206 16, /* bitsize */
1207 FALSE, /* pc_relative */
1208 0, /* bitpos */
b80eed39 1209 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1210 ppc64_elf_sectoff_reloc, /* special_function */
1211 "R_PPC64_SECTOFF_DS", /* name */
1212 FALSE, /* partial_inplace */
1213 0, /* src_mask */
1214 0xfffc, /* dst_mask */
1215 FALSE), /* pcrel_offset */
1216
1217 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1218 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1219 0, /* rightshift */
1220 1, /* size (0 = byte, 1 = short, 2 = long) */
1221 16, /* bitsize */
1222 FALSE, /* pc_relative */
1223 0, /* bitpos */
1224 complain_overflow_dont, /* complain_on_overflow */
1225 ppc64_elf_sectoff_reloc, /* special_function */
1226 "R_PPC64_SECTOFF_LO_DS",/* name */
1227 FALSE, /* partial_inplace */
1228 0, /* src_mask */
1229 0xfffc, /* dst_mask */
1230 FALSE), /* pcrel_offset */
1231
1232 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1233 HOWTO (R_PPC64_TOC16_DS, /* type */
1234 0, /* rightshift */
1235 1, /* size (0 = byte, 1 = short, 2 = long) */
1236 16, /* bitsize */
1237 FALSE, /* pc_relative */
1238 0, /* bitpos */
1239 complain_overflow_signed, /* complain_on_overflow */
1240 ppc64_elf_toc_reloc, /* special_function */
1241 "R_PPC64_TOC16_DS", /* name */
1242 FALSE, /* partial_inplace */
1243 0, /* src_mask */
1244 0xfffc, /* dst_mask */
1245 FALSE), /* pcrel_offset */
1246
1247 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1248 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1249 0, /* rightshift */
1250 1, /* size (0 = byte, 1 = short, 2 = long) */
1251 16, /* bitsize */
1252 FALSE, /* pc_relative */
1253 0, /* bitpos */
1254 complain_overflow_dont, /* complain_on_overflow */
1255 ppc64_elf_toc_reloc, /* special_function */
1256 "R_PPC64_TOC16_LO_DS", /* name */
1257 FALSE, /* partial_inplace */
1258 0, /* src_mask */
1259 0xfffc, /* dst_mask */
1260 FALSE), /* pcrel_offset */
1261
1262 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1263 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1264 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1265 0, /* rightshift */
1266 1, /* size (0 = byte, 1 = short, 2 = long) */
1267 16, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_signed, /* complain_on_overflow */
1271 ppc64_elf_unhandled_reloc, /* special_function */
1272 "R_PPC64_PLTGOT16_DS", /* name */
1273 FALSE, /* partial_inplace */
1274 0, /* src_mask */
1275 0xfffc, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1277
1278 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1279 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1280 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1281 0, /* rightshift */
1282 1, /* size (0 = byte, 1 = short, 2 = long) */
1283 16, /* bitsize */
1284 FALSE, /* pc_relative */
1285 0, /* bitpos */
1286 complain_overflow_dont, /* complain_on_overflow */
1287 ppc64_elf_unhandled_reloc, /* special_function */
1288 "R_PPC64_PLTGOT16_LO_DS",/* name */
1289 FALSE, /* partial_inplace */
1290 0, /* src_mask */
1291 0xfffc, /* dst_mask */
1292 FALSE), /* pcrel_offset */
1293
727fc41e 1294 /* Marker relocs for TLS. */
411e1bfb
AM
1295 HOWTO (R_PPC64_TLS,
1296 0, /* rightshift */
1297 2, /* size (0 = byte, 1 = short, 2 = long) */
1298 32, /* bitsize */
1299 FALSE, /* pc_relative */
1300 0, /* bitpos */
1301 complain_overflow_dont, /* complain_on_overflow */
1302 bfd_elf_generic_reloc, /* special_function */
1303 "R_PPC64_TLS", /* name */
1304 FALSE, /* partial_inplace */
1305 0, /* src_mask */
1306 0, /* dst_mask */
1307 FALSE), /* pcrel_offset */
1308
727fc41e
AM
1309 HOWTO (R_PPC64_TLSGD,
1310 0, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 32, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_dont, /* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_PPC64_TLSGD", /* name */
1318 FALSE, /* partial_inplace */
1319 0, /* src_mask */
1320 0, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1322
1323 HOWTO (R_PPC64_TLSLD,
1324 0, /* rightshift */
1325 2, /* size (0 = byte, 1 = short, 2 = long) */
1326 32, /* bitsize */
1327 FALSE, /* pc_relative */
1328 0, /* bitpos */
1329 complain_overflow_dont, /* complain_on_overflow */
1330 bfd_elf_generic_reloc, /* special_function */
1331 "R_PPC64_TLSLD", /* name */
1332 FALSE, /* partial_inplace */
1333 0, /* src_mask */
1334 0, /* dst_mask */
1335 FALSE), /* pcrel_offset */
1336
3b421ab3
AM
1337 HOWTO (R_PPC64_TOCSAVE,
1338 0, /* rightshift */
1339 2, /* size (0 = byte, 1 = short, 2 = long) */
1340 32, /* bitsize */
1341 FALSE, /* pc_relative */
1342 0, /* bitpos */
1343 complain_overflow_dont, /* complain_on_overflow */
1344 bfd_elf_generic_reloc, /* special_function */
1345 "R_PPC64_TOCSAVE", /* name */
1346 FALSE, /* partial_inplace */
1347 0, /* src_mask */
1348 0, /* dst_mask */
1349 FALSE), /* pcrel_offset */
1350
411e1bfb
AM
1351 /* Computes the load module index of the load module that contains the
1352 definition of its TLS sym. */
1353 HOWTO (R_PPC64_DTPMOD64,
1354 0, /* rightshift */
1355 4, /* size (0 = byte, 1 = short, 2 = long) */
1356 64, /* bitsize */
1357 FALSE, /* pc_relative */
1358 0, /* bitpos */
1359 complain_overflow_dont, /* complain_on_overflow */
1360 ppc64_elf_unhandled_reloc, /* special_function */
1361 "R_PPC64_DTPMOD64", /* name */
1362 FALSE, /* partial_inplace */
1363 0, /* src_mask */
1364 ONES (64), /* dst_mask */
1365 FALSE), /* pcrel_offset */
1366
1367 /* Computes a dtv-relative displacement, the difference between the value
1368 of sym+add and the base address of the thread-local storage block that
1369 contains the definition of sym, minus 0x8000. */
1370 HOWTO (R_PPC64_DTPREL64,
1371 0, /* rightshift */
1372 4, /* size (0 = byte, 1 = short, 2 = long) */
1373 64, /* bitsize */
1374 FALSE, /* pc_relative */
1375 0, /* bitpos */
1376 complain_overflow_dont, /* complain_on_overflow */
1377 ppc64_elf_unhandled_reloc, /* special_function */
1378 "R_PPC64_DTPREL64", /* name */
1379 FALSE, /* partial_inplace */
1380 0, /* src_mask */
1381 ONES (64), /* dst_mask */
1382 FALSE), /* pcrel_offset */
1383
1384 /* A 16 bit dtprel reloc. */
1385 HOWTO (R_PPC64_DTPREL16,
1386 0, /* rightshift */
1387 1, /* size (0 = byte, 1 = short, 2 = long) */
1388 16, /* bitsize */
1389 FALSE, /* pc_relative */
1390 0, /* bitpos */
1391 complain_overflow_signed, /* complain_on_overflow */
1392 ppc64_elf_unhandled_reloc, /* special_function */
1393 "R_PPC64_DTPREL16", /* name */
1394 FALSE, /* partial_inplace */
1395 0, /* src_mask */
1396 0xffff, /* dst_mask */
1397 FALSE), /* pcrel_offset */
1398
1399 /* Like DTPREL16, but no overflow. */
1400 HOWTO (R_PPC64_DTPREL16_LO,
1401 0, /* rightshift */
1402 1, /* size (0 = byte, 1 = short, 2 = long) */
1403 16, /* bitsize */
1404 FALSE, /* pc_relative */
1405 0, /* bitpos */
1406 complain_overflow_dont, /* complain_on_overflow */
1407 ppc64_elf_unhandled_reloc, /* special_function */
1408 "R_PPC64_DTPREL16_LO", /* name */
1409 FALSE, /* partial_inplace */
1410 0, /* src_mask */
1411 0xffff, /* dst_mask */
1412 FALSE), /* pcrel_offset */
1413
1414 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1415 HOWTO (R_PPC64_DTPREL16_HI,
1416 16, /* rightshift */
1417 1, /* size (0 = byte, 1 = short, 2 = long) */
1418 16, /* bitsize */
1419 FALSE, /* pc_relative */
1420 0, /* bitpos */
f9c6b907 1421 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1422 ppc64_elf_unhandled_reloc, /* special_function */
1423 "R_PPC64_DTPREL16_HI", /* name */
1424 FALSE, /* partial_inplace */
1425 0, /* src_mask */
1426 0xffff, /* dst_mask */
1427 FALSE), /* pcrel_offset */
1428
1429 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1430 HOWTO (R_PPC64_DTPREL16_HA,
1431 16, /* rightshift */
1432 1, /* size (0 = byte, 1 = short, 2 = long) */
1433 16, /* bitsize */
1434 FALSE, /* pc_relative */
1435 0, /* bitpos */
f9c6b907 1436 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1437 ppc64_elf_unhandled_reloc, /* special_function */
1438 "R_PPC64_DTPREL16_HA", /* name */
1439 FALSE, /* partial_inplace */
1440 0, /* src_mask */
1441 0xffff, /* dst_mask */
1442 FALSE), /* pcrel_offset */
1443
1444 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1445 HOWTO (R_PPC64_DTPREL16_HIGHER,
1446 32, /* rightshift */
1447 1, /* size (0 = byte, 1 = short, 2 = long) */
1448 16, /* bitsize */
1449 FALSE, /* pc_relative */
1450 0, /* bitpos */
1451 complain_overflow_dont, /* complain_on_overflow */
1452 ppc64_elf_unhandled_reloc, /* special_function */
1453 "R_PPC64_DTPREL16_HIGHER", /* name */
1454 FALSE, /* partial_inplace */
1455 0, /* src_mask */
1456 0xffff, /* dst_mask */
1457 FALSE), /* pcrel_offset */
1458
1459 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1460 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1461 32, /* rightshift */
1462 1, /* size (0 = byte, 1 = short, 2 = long) */
1463 16, /* bitsize */
1464 FALSE, /* pc_relative */
1465 0, /* bitpos */
1466 complain_overflow_dont, /* complain_on_overflow */
1467 ppc64_elf_unhandled_reloc, /* special_function */
1468 "R_PPC64_DTPREL16_HIGHERA", /* name */
1469 FALSE, /* partial_inplace */
1470 0, /* src_mask */
1471 0xffff, /* dst_mask */
1472 FALSE), /* pcrel_offset */
1473
1474 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1475 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1476 48, /* rightshift */
1477 1, /* size (0 = byte, 1 = short, 2 = long) */
1478 16, /* bitsize */
1479 FALSE, /* pc_relative */
1480 0, /* bitpos */
1481 complain_overflow_dont, /* complain_on_overflow */
1482 ppc64_elf_unhandled_reloc, /* special_function */
1483 "R_PPC64_DTPREL16_HIGHEST", /* name */
1484 FALSE, /* partial_inplace */
1485 0, /* src_mask */
1486 0xffff, /* dst_mask */
1487 FALSE), /* pcrel_offset */
1488
1489 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1490 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1491 48, /* rightshift */
1492 1, /* size (0 = byte, 1 = short, 2 = long) */
1493 16, /* bitsize */
1494 FALSE, /* pc_relative */
1495 0, /* bitpos */
1496 complain_overflow_dont, /* complain_on_overflow */
1497 ppc64_elf_unhandled_reloc, /* special_function */
1498 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1499 FALSE, /* partial_inplace */
1500 0, /* src_mask */
1501 0xffff, /* dst_mask */
1502 FALSE), /* pcrel_offset */
1503
1504 /* Like DTPREL16, but for insns with a DS field. */
1505 HOWTO (R_PPC64_DTPREL16_DS,
1506 0, /* rightshift */
1507 1, /* size (0 = byte, 1 = short, 2 = long) */
1508 16, /* bitsize */
1509 FALSE, /* pc_relative */
1510 0, /* bitpos */
1511 complain_overflow_signed, /* complain_on_overflow */
1512 ppc64_elf_unhandled_reloc, /* special_function */
1513 "R_PPC64_DTPREL16_DS", /* name */
1514 FALSE, /* partial_inplace */
1515 0, /* src_mask */
1516 0xfffc, /* dst_mask */
1517 FALSE), /* pcrel_offset */
1518
1519 /* Like DTPREL16_DS, but no overflow. */
1520 HOWTO (R_PPC64_DTPREL16_LO_DS,
1521 0, /* rightshift */
1522 1, /* size (0 = byte, 1 = short, 2 = long) */
1523 16, /* bitsize */
1524 FALSE, /* pc_relative */
1525 0, /* bitpos */
1526 complain_overflow_dont, /* complain_on_overflow */
1527 ppc64_elf_unhandled_reloc, /* special_function */
1528 "R_PPC64_DTPREL16_LO_DS", /* name */
1529 FALSE, /* partial_inplace */
1530 0, /* src_mask */
1531 0xfffc, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1533
1534 /* Computes a tp-relative displacement, the difference between the value of
1535 sym+add and the value of the thread pointer (r13). */
1536 HOWTO (R_PPC64_TPREL64,
1537 0, /* rightshift */
1538 4, /* size (0 = byte, 1 = short, 2 = long) */
1539 64, /* bitsize */
1540 FALSE, /* pc_relative */
1541 0, /* bitpos */
1542 complain_overflow_dont, /* complain_on_overflow */
1543 ppc64_elf_unhandled_reloc, /* special_function */
1544 "R_PPC64_TPREL64", /* name */
1545 FALSE, /* partial_inplace */
1546 0, /* src_mask */
1547 ONES (64), /* dst_mask */
1548 FALSE), /* pcrel_offset */
1549
1550 /* A 16 bit tprel reloc. */
1551 HOWTO (R_PPC64_TPREL16,
1552 0, /* rightshift */
1553 1, /* size (0 = byte, 1 = short, 2 = long) */
1554 16, /* bitsize */
1555 FALSE, /* pc_relative */
1556 0, /* bitpos */
1557 complain_overflow_signed, /* complain_on_overflow */
1558 ppc64_elf_unhandled_reloc, /* special_function */
1559 "R_PPC64_TPREL16", /* name */
1560 FALSE, /* partial_inplace */
1561 0, /* src_mask */
1562 0xffff, /* dst_mask */
1563 FALSE), /* pcrel_offset */
1564
1565 /* Like TPREL16, but no overflow. */
1566 HOWTO (R_PPC64_TPREL16_LO,
1567 0, /* rightshift */
1568 1, /* size (0 = byte, 1 = short, 2 = long) */
1569 16, /* bitsize */
1570 FALSE, /* pc_relative */
1571 0, /* bitpos */
1572 complain_overflow_dont, /* complain_on_overflow */
1573 ppc64_elf_unhandled_reloc, /* special_function */
1574 "R_PPC64_TPREL16_LO", /* name */
1575 FALSE, /* partial_inplace */
1576 0, /* src_mask */
1577 0xffff, /* dst_mask */
1578 FALSE), /* pcrel_offset */
1579
1580 /* Like TPREL16_LO, but next higher group of 16 bits. */
1581 HOWTO (R_PPC64_TPREL16_HI,
1582 16, /* rightshift */
1583 1, /* size (0 = byte, 1 = short, 2 = long) */
1584 16, /* bitsize */
1585 FALSE, /* pc_relative */
1586 0, /* bitpos */
f9c6b907 1587 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1588 ppc64_elf_unhandled_reloc, /* special_function */
1589 "R_PPC64_TPREL16_HI", /* name */
1590 FALSE, /* partial_inplace */
1591 0, /* src_mask */
1592 0xffff, /* dst_mask */
1593 FALSE), /* pcrel_offset */
1594
1595 /* Like TPREL16_HI, but adjust for low 16 bits. */
1596 HOWTO (R_PPC64_TPREL16_HA,
1597 16, /* rightshift */
1598 1, /* size (0 = byte, 1 = short, 2 = long) */
1599 16, /* bitsize */
1600 FALSE, /* pc_relative */
1601 0, /* bitpos */
f9c6b907 1602 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1603 ppc64_elf_unhandled_reloc, /* special_function */
1604 "R_PPC64_TPREL16_HA", /* name */
1605 FALSE, /* partial_inplace */
1606 0, /* src_mask */
1607 0xffff, /* dst_mask */
1608 FALSE), /* pcrel_offset */
1609
1610 /* Like TPREL16_HI, but next higher group of 16 bits. */
1611 HOWTO (R_PPC64_TPREL16_HIGHER,
1612 32, /* rightshift */
1613 1, /* size (0 = byte, 1 = short, 2 = long) */
1614 16, /* bitsize */
1615 FALSE, /* pc_relative */
1616 0, /* bitpos */
1617 complain_overflow_dont, /* complain_on_overflow */
1618 ppc64_elf_unhandled_reloc, /* special_function */
1619 "R_PPC64_TPREL16_HIGHER", /* name */
1620 FALSE, /* partial_inplace */
1621 0, /* src_mask */
1622 0xffff, /* dst_mask */
1623 FALSE), /* pcrel_offset */
1624
1625 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1626 HOWTO (R_PPC64_TPREL16_HIGHERA,
1627 32, /* rightshift */
1628 1, /* size (0 = byte, 1 = short, 2 = long) */
1629 16, /* bitsize */
1630 FALSE, /* pc_relative */
1631 0, /* bitpos */
1632 complain_overflow_dont, /* complain_on_overflow */
1633 ppc64_elf_unhandled_reloc, /* special_function */
1634 "R_PPC64_TPREL16_HIGHERA", /* name */
1635 FALSE, /* partial_inplace */
1636 0, /* src_mask */
1637 0xffff, /* dst_mask */
1638 FALSE), /* pcrel_offset */
1639
1640 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1641 HOWTO (R_PPC64_TPREL16_HIGHEST,
1642 48, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 16, /* bitsize */
1645 FALSE, /* pc_relative */
1646 0, /* bitpos */
1647 complain_overflow_dont, /* complain_on_overflow */
1648 ppc64_elf_unhandled_reloc, /* special_function */
1649 "R_PPC64_TPREL16_HIGHEST", /* name */
1650 FALSE, /* partial_inplace */
1651 0, /* src_mask */
1652 0xffff, /* dst_mask */
1653 FALSE), /* pcrel_offset */
1654
1655 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1656 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1657 48, /* rightshift */
1658 1, /* size (0 = byte, 1 = short, 2 = long) */
1659 16, /* bitsize */
1660 FALSE, /* pc_relative */
1661 0, /* bitpos */
1662 complain_overflow_dont, /* complain_on_overflow */
1663 ppc64_elf_unhandled_reloc, /* special_function */
1664 "R_PPC64_TPREL16_HIGHESTA", /* name */
1665 FALSE, /* partial_inplace */
1666 0, /* src_mask */
1667 0xffff, /* dst_mask */
1668 FALSE), /* pcrel_offset */
1669
1670 /* Like TPREL16, but for insns with a DS field. */
1671 HOWTO (R_PPC64_TPREL16_DS,
1672 0, /* rightshift */
1673 1, /* size (0 = byte, 1 = short, 2 = long) */
1674 16, /* bitsize */
1675 FALSE, /* pc_relative */
1676 0, /* bitpos */
1677 complain_overflow_signed, /* complain_on_overflow */
1678 ppc64_elf_unhandled_reloc, /* special_function */
1679 "R_PPC64_TPREL16_DS", /* name */
1680 FALSE, /* partial_inplace */
1681 0, /* src_mask */
1682 0xfffc, /* dst_mask */
1683 FALSE), /* pcrel_offset */
1684
1685 /* Like TPREL16_DS, but no overflow. */
1686 HOWTO (R_PPC64_TPREL16_LO_DS,
1687 0, /* rightshift */
1688 1, /* size (0 = byte, 1 = short, 2 = long) */
1689 16, /* bitsize */
1690 FALSE, /* pc_relative */
1691 0, /* bitpos */
1692 complain_overflow_dont, /* complain_on_overflow */
1693 ppc64_elf_unhandled_reloc, /* special_function */
1694 "R_PPC64_TPREL16_LO_DS", /* name */
1695 FALSE, /* partial_inplace */
1696 0, /* src_mask */
1697 0xfffc, /* dst_mask */
1698 FALSE), /* pcrel_offset */
1699
1700 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1701 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1702 to the first entry relative to the TOC base (r2). */
1703 HOWTO (R_PPC64_GOT_TLSGD16,
1704 0, /* rightshift */
1705 1, /* size (0 = byte, 1 = short, 2 = long) */
1706 16, /* bitsize */
1707 FALSE, /* pc_relative */
1708 0, /* bitpos */
1709 complain_overflow_signed, /* complain_on_overflow */
1710 ppc64_elf_unhandled_reloc, /* special_function */
1711 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1712 FALSE, /* partial_inplace */
5bd4f169
AM
1713 0, /* src_mask */
1714 0xffff, /* dst_mask */
b34976b6 1715 FALSE), /* pcrel_offset */
5bd4f169 1716
411e1bfb
AM
1717 /* Like GOT_TLSGD16, but no overflow. */
1718 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1719 0, /* rightshift */
1720 1, /* size (0 = byte, 1 = short, 2 = long) */
1721 16, /* bitsize */
b34976b6 1722 FALSE, /* pc_relative */
5bd4f169
AM
1723 0, /* bitpos */
1724 complain_overflow_dont, /* complain_on_overflow */
805fc799 1725 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1726 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1727 FALSE, /* partial_inplace */
5bd4f169
AM
1728 0, /* src_mask */
1729 0xffff, /* dst_mask */
b34976b6 1730 FALSE), /* pcrel_offset */
5bd4f169 1731
411e1bfb
AM
1732 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1733 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1734 16, /* rightshift */
1735 1, /* size (0 = byte, 1 = short, 2 = long) */
1736 16, /* bitsize */
b34976b6 1737 FALSE, /* pc_relative */
5bd4f169 1738 0, /* bitpos */
f9c6b907 1739 complain_overflow_signed, /* complain_on_overflow */
805fc799 1740 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1741 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1742 FALSE, /* partial_inplace */
5bd4f169
AM
1743 0, /* src_mask */
1744 0xffff, /* dst_mask */
b34976b6 1745 FALSE), /* pcrel_offset */
5bd4f169 1746
411e1bfb
AM
1747 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1748 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1749 16, /* rightshift */
1750 1, /* size (0 = byte, 1 = short, 2 = long) */
1751 16, /* bitsize */
b34976b6 1752 FALSE, /* pc_relative */
5bd4f169 1753 0, /* bitpos */
f9c6b907 1754 complain_overflow_signed, /* complain_on_overflow */
805fc799 1755 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1756 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1757 FALSE, /* partial_inplace */
5bd4f169
AM
1758 0, /* src_mask */
1759 0xffff, /* dst_mask */
b34976b6 1760 FALSE), /* pcrel_offset */
5bd4f169 1761
411e1bfb
AM
1762 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1763 with values (sym+add)@dtpmod and zero, and computes the offset to the
1764 first entry relative to the TOC base (r2). */
1765 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1766 0, /* rightshift */
1767 1, /* size (0 = byte, 1 = short, 2 = long) */
1768 16, /* bitsize */
b34976b6 1769 FALSE, /* pc_relative */
5bd4f169 1770 0, /* bitpos */
411e1bfb
AM
1771 complain_overflow_signed, /* complain_on_overflow */
1772 ppc64_elf_unhandled_reloc, /* special_function */
1773 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1774 FALSE, /* partial_inplace */
d006db6c 1775 0, /* src_mask */
411e1bfb 1776 0xffff, /* dst_mask */
b34976b6 1777 FALSE), /* pcrel_offset */
5bd4f169 1778
411e1bfb
AM
1779 /* Like GOT_TLSLD16, but no overflow. */
1780 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1781 0, /* rightshift */
1782 1, /* size (0 = byte, 1 = short, 2 = long) */
1783 16, /* bitsize */
b34976b6 1784 FALSE, /* pc_relative */
5bd4f169 1785 0, /* bitpos */
411e1bfb
AM
1786 complain_overflow_dont, /* complain_on_overflow */
1787 ppc64_elf_unhandled_reloc, /* special_function */
1788 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1789 FALSE, /* partial_inplace */
d006db6c 1790 0, /* src_mask */
411e1bfb 1791 0xffff, /* dst_mask */
b34976b6 1792 FALSE), /* pcrel_offset */
5bd4f169 1793
411e1bfb
AM
1794 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1795 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1796 16, /* rightshift */
5bd4f169
AM
1797 1, /* size (0 = byte, 1 = short, 2 = long) */
1798 16, /* bitsize */
b34976b6 1799 FALSE, /* pc_relative */
5bd4f169 1800 0, /* bitpos */
f9c6b907 1801 complain_overflow_signed, /* complain_on_overflow */
805fc799 1802 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1803 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1804 FALSE, /* partial_inplace */
d006db6c 1805 0, /* src_mask */
411e1bfb 1806 0xffff, /* dst_mask */
b34976b6 1807 FALSE), /* pcrel_offset */
5bd4f169 1808
411e1bfb
AM
1809 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1810 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1811 16, /* rightshift */
5bd4f169
AM
1812 1, /* size (0 = byte, 1 = short, 2 = long) */
1813 16, /* bitsize */
b34976b6 1814 FALSE, /* pc_relative */
5bd4f169 1815 0, /* bitpos */
f9c6b907 1816 complain_overflow_signed, /* complain_on_overflow */
805fc799 1817 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1818 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1819 FALSE, /* partial_inplace */
d006db6c 1820 0, /* src_mask */
411e1bfb 1821 0xffff, /* dst_mask */
b34976b6 1822 FALSE), /* pcrel_offset */
5bd4f169 1823
411e1bfb
AM
1824 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1825 the offset to the entry relative to the TOC base (r2). */
1826 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1827 0, /* rightshift */
1828 1, /* size (0 = byte, 1 = short, 2 = long) */
1829 16, /* bitsize */
b34976b6 1830 FALSE, /* pc_relative */
5bd4f169 1831 0, /* bitpos */
411e1bfb 1832 complain_overflow_signed, /* complain_on_overflow */
805fc799 1833 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1834 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1835 FALSE, /* partial_inplace */
d006db6c 1836 0, /* src_mask */
5bd4f169 1837 0xfffc, /* dst_mask */
b34976b6 1838 FALSE), /* pcrel_offset */
5bd4f169 1839
411e1bfb
AM
1840 /* Like GOT_DTPREL16_DS, but no overflow. */
1841 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1842 0, /* rightshift */
c061c2d8
AM
1843 1, /* size (0 = byte, 1 = short, 2 = long) */
1844 16, /* bitsize */
b34976b6 1845 FALSE, /* pc_relative */
5bd4f169 1846 0, /* bitpos */
411e1bfb
AM
1847 complain_overflow_dont, /* complain_on_overflow */
1848 ppc64_elf_unhandled_reloc, /* special_function */
1849 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1850 FALSE, /* partial_inplace */
d006db6c 1851 0, /* src_mask */
c061c2d8 1852 0xfffc, /* dst_mask */
b34976b6 1853 FALSE), /* pcrel_offset */
5bd4f169 1854
411e1bfb
AM
1855 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1856 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1857 16, /* rightshift */
5bd4f169
AM
1858 1, /* size (0 = byte, 1 = short, 2 = long) */
1859 16, /* bitsize */
b34976b6 1860 FALSE, /* pc_relative */
5bd4f169 1861 0, /* bitpos */
f9c6b907 1862 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1863 ppc64_elf_unhandled_reloc, /* special_function */
1864 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1865 FALSE, /* partial_inplace */
d006db6c 1866 0, /* src_mask */
411e1bfb 1867 0xffff, /* dst_mask */
b34976b6 1868 FALSE), /* pcrel_offset */
5bd4f169 1869
411e1bfb
AM
1870 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1871 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1872 16, /* rightshift */
1873 1, /* size (0 = byte, 1 = short, 2 = long) */
1874 16, /* bitsize */
1875 FALSE, /* pc_relative */
1876 0, /* bitpos */
f9c6b907 1877 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1878 ppc64_elf_unhandled_reloc, /* special_function */
1879 "R_PPC64_GOT_DTPREL16_HA", /* name */
1880 FALSE, /* partial_inplace */
1881 0, /* src_mask */
1882 0xffff, /* dst_mask */
1883 FALSE), /* pcrel_offset */
1884
1885 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1886 offset to the entry relative to the TOC base (r2). */
1887 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1888 0, /* rightshift */
1889 1, /* size (0 = byte, 1 = short, 2 = long) */
1890 16, /* bitsize */
b34976b6 1891 FALSE, /* pc_relative */
5bd4f169
AM
1892 0, /* bitpos */
1893 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1894 ppc64_elf_unhandled_reloc, /* special_function */
1895 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1896 FALSE, /* partial_inplace */
d006db6c 1897 0, /* src_mask */
ad8e1ba5 1898 0xfffc, /* dst_mask */
b34976b6 1899 FALSE), /* pcrel_offset */
5bd4f169 1900
411e1bfb
AM
1901 /* Like GOT_TPREL16_DS, but no overflow. */
1902 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1903 0, /* rightshift */
1904 1, /* size (0 = byte, 1 = short, 2 = long) */
1905 16, /* bitsize */
b34976b6 1906 FALSE, /* pc_relative */
5bd4f169
AM
1907 0, /* bitpos */
1908 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1909 ppc64_elf_unhandled_reloc, /* special_function */
1910 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1911 FALSE, /* partial_inplace */
d006db6c 1912 0, /* src_mask */
ad8e1ba5 1913 0xfffc, /* dst_mask */
b34976b6 1914 FALSE), /* pcrel_offset */
5bd4f169 1915
411e1bfb
AM
1916 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1917 HOWTO (R_PPC64_GOT_TPREL16_HI,
1918 16, /* rightshift */
5bd4f169
AM
1919 1, /* size (0 = byte, 1 = short, 2 = long) */
1920 16, /* bitsize */
b34976b6 1921 FALSE, /* pc_relative */
5bd4f169 1922 0, /* bitpos */
f9c6b907 1923 complain_overflow_signed, /* complain_on_overflow */
805fc799 1924 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1925 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1926 FALSE, /* partial_inplace */
d006db6c 1927 0, /* src_mask */
411e1bfb 1928 0xffff, /* dst_mask */
b34976b6 1929 FALSE), /* pcrel_offset */
5bd4f169 1930
411e1bfb
AM
1931 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1932 HOWTO (R_PPC64_GOT_TPREL16_HA,
1933 16, /* rightshift */
5bd4f169
AM
1934 1, /* size (0 = byte, 1 = short, 2 = long) */
1935 16, /* bitsize */
b34976b6 1936 FALSE, /* pc_relative */
5bd4f169 1937 0, /* bitpos */
f9c6b907 1938 complain_overflow_signed, /* complain_on_overflow */
805fc799 1939 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1940 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1941 FALSE, /* partial_inplace */
d006db6c 1942 0, /* src_mask */
411e1bfb 1943 0xffff, /* dst_mask */
b34976b6 1944 FALSE), /* pcrel_offset */
5bd4f169 1945
25f23106
AM
1946 HOWTO (R_PPC64_JMP_IREL, /* type */
1947 0, /* rightshift */
1948 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1949 0, /* bitsize */
1950 FALSE, /* pc_relative */
1951 0, /* bitpos */
1952 complain_overflow_dont, /* complain_on_overflow */
1953 ppc64_elf_unhandled_reloc, /* special_function */
1954 "R_PPC64_JMP_IREL", /* name */
1955 FALSE, /* partial_inplace */
1956 0, /* src_mask */
1957 0, /* dst_mask */
1958 FALSE), /* pcrel_offset */
1959
e054468f
AM
1960 HOWTO (R_PPC64_IRELATIVE, /* type */
1961 0, /* rightshift */
1962 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1963 64, /* bitsize */
1964 FALSE, /* pc_relative */
1965 0, /* bitpos */
1966 complain_overflow_dont, /* complain_on_overflow */
1967 bfd_elf_generic_reloc, /* special_function */
1968 "R_PPC64_IRELATIVE", /* name */
1969 FALSE, /* partial_inplace */
1970 0, /* src_mask */
1971 ONES (64), /* dst_mask */
1972 FALSE), /* pcrel_offset */
1973
25f23106
AM
1974 /* A 16 bit relative relocation. */
1975 HOWTO (R_PPC64_REL16, /* type */
1976 0, /* rightshift */
1977 1, /* size (0 = byte, 1 = short, 2 = long) */
1978 16, /* bitsize */
1979 TRUE, /* pc_relative */
1980 0, /* bitpos */
b80eed39 1981 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1982 bfd_elf_generic_reloc, /* special_function */
1983 "R_PPC64_REL16", /* name */
1984 FALSE, /* partial_inplace */
1985 0, /* src_mask */
1986 0xffff, /* dst_mask */
1987 TRUE), /* pcrel_offset */
1988
1989 /* A 16 bit relative relocation without overflow. */
1990 HOWTO (R_PPC64_REL16_LO, /* type */
1991 0, /* rightshift */
1992 1, /* size (0 = byte, 1 = short, 2 = long) */
1993 16, /* bitsize */
1994 TRUE, /* pc_relative */
1995 0, /* bitpos */
1996 complain_overflow_dont,/* complain_on_overflow */
1997 bfd_elf_generic_reloc, /* special_function */
1998 "R_PPC64_REL16_LO", /* name */
1999 FALSE, /* partial_inplace */
2000 0, /* src_mask */
2001 0xffff, /* dst_mask */
2002 TRUE), /* pcrel_offset */
2003
2004 /* The high order 16 bits of a relative address. */
2005 HOWTO (R_PPC64_REL16_HI, /* type */
2006 16, /* rightshift */
2007 1, /* size (0 = byte, 1 = short, 2 = long) */
2008 16, /* bitsize */
2009 TRUE, /* pc_relative */
2010 0, /* bitpos */
f9c6b907 2011 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2012 bfd_elf_generic_reloc, /* special_function */
2013 "R_PPC64_REL16_HI", /* name */
2014 FALSE, /* partial_inplace */
2015 0, /* src_mask */
2016 0xffff, /* dst_mask */
2017 TRUE), /* pcrel_offset */
2018
2019 /* The high order 16 bits of a relative address, plus 1 if the contents of
2020 the low 16 bits, treated as a signed number, is negative. */
2021 HOWTO (R_PPC64_REL16_HA, /* type */
2022 16, /* rightshift */
2023 1, /* size (0 = byte, 1 = short, 2 = long) */
2024 16, /* bitsize */
2025 TRUE, /* pc_relative */
2026 0, /* bitpos */
f9c6b907 2027 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2028 ppc64_elf_ha_reloc, /* special_function */
2029 "R_PPC64_REL16_HA", /* name */
2030 FALSE, /* partial_inplace */
2031 0, /* src_mask */
2032 0xffff, /* dst_mask */
2033 TRUE), /* pcrel_offset */
2034
a680de9a
PB
2035 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2036 HOWTO (R_PPC64_REL16DX_HA, /* type */
2037 16, /* rightshift */
2038 2, /* size (0 = byte, 1 = short, 2 = long) */
2039 16, /* bitsize */
2040 TRUE, /* pc_relative */
2041 0, /* bitpos */
2042 complain_overflow_signed, /* complain_on_overflow */
2043 ppc64_elf_ha_reloc, /* special_function */
2044 "R_PPC64_REL16DX_HA", /* name */
2045 FALSE, /* partial_inplace */
2046 0, /* src_mask */
2047 0x1fffc1, /* dst_mask */
2048 TRUE), /* pcrel_offset */
2049
7ba71655
AM
2050 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2051 HOWTO (R_PPC64_16DX_HA, /* type */
2052 16, /* rightshift */
2053 2, /* size (0 = byte, 1 = short, 2 = long) */
2054 16, /* bitsize */
2055 FALSE, /* pc_relative */
2056 0, /* bitpos */
2057 complain_overflow_signed, /* complain_on_overflow */
2058 ppc64_elf_ha_reloc, /* special_function */
2059 "R_PPC64_16DX_HA", /* name */
2060 FALSE, /* partial_inplace */
2061 0, /* src_mask */
2062 0x1fffc1, /* dst_mask */
2063 FALSE), /* pcrel_offset */
2064
f9c6b907
AM
2065 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2066 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2067 16, /* rightshift */
2068 1, /* size (0 = byte, 1 = short, 2 = long) */
2069 16, /* bitsize */
2070 FALSE, /* pc_relative */
2071 0, /* bitpos */
2072 complain_overflow_dont, /* complain_on_overflow */
2073 bfd_elf_generic_reloc, /* special_function */
2074 "R_PPC64_ADDR16_HIGH", /* name */
2075 FALSE, /* partial_inplace */
2076 0, /* src_mask */
2077 0xffff, /* dst_mask */
2078 FALSE), /* pcrel_offset */
2079
2080 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2081 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2082 16, /* rightshift */
2083 1, /* size (0 = byte, 1 = short, 2 = long) */
2084 16, /* bitsize */
2085 FALSE, /* pc_relative */
2086 0, /* bitpos */
2087 complain_overflow_dont, /* complain_on_overflow */
2088 ppc64_elf_ha_reloc, /* special_function */
2089 "R_PPC64_ADDR16_HIGHA", /* name */
2090 FALSE, /* partial_inplace */
2091 0, /* src_mask */
2092 0xffff, /* dst_mask */
2093 FALSE), /* pcrel_offset */
2094
2095 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2096 HOWTO (R_PPC64_DTPREL16_HIGH,
2097 16, /* rightshift */
2098 1, /* size (0 = byte, 1 = short, 2 = long) */
2099 16, /* bitsize */
2100 FALSE, /* pc_relative */
2101 0, /* bitpos */
2102 complain_overflow_dont, /* complain_on_overflow */
2103 ppc64_elf_unhandled_reloc, /* special_function */
2104 "R_PPC64_DTPREL16_HIGH", /* name */
2105 FALSE, /* partial_inplace */
2106 0, /* src_mask */
2107 0xffff, /* dst_mask */
2108 FALSE), /* pcrel_offset */
2109
2110 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2111 HOWTO (R_PPC64_DTPREL16_HIGHA,
2112 16, /* rightshift */
2113 1, /* size (0 = byte, 1 = short, 2 = long) */
2114 16, /* bitsize */
2115 FALSE, /* pc_relative */
2116 0, /* bitpos */
2117 complain_overflow_dont, /* complain_on_overflow */
2118 ppc64_elf_unhandled_reloc, /* special_function */
2119 "R_PPC64_DTPREL16_HIGHA", /* name */
2120 FALSE, /* partial_inplace */
2121 0, /* src_mask */
2122 0xffff, /* dst_mask */
2123 FALSE), /* pcrel_offset */
2124
2125 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2126 HOWTO (R_PPC64_TPREL16_HIGH,
2127 16, /* rightshift */
2128 1, /* size (0 = byte, 1 = short, 2 = long) */
2129 16, /* bitsize */
2130 FALSE, /* pc_relative */
2131 0, /* bitpos */
2132 complain_overflow_dont, /* complain_on_overflow */
2133 ppc64_elf_unhandled_reloc, /* special_function */
2134 "R_PPC64_TPREL16_HIGH", /* name */
2135 FALSE, /* partial_inplace */
2136 0, /* src_mask */
2137 0xffff, /* dst_mask */
2138 FALSE), /* pcrel_offset */
2139
2140 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2141 HOWTO (R_PPC64_TPREL16_HIGHA,
2142 16, /* rightshift */
2143 1, /* size (0 = byte, 1 = short, 2 = long) */
2144 16, /* bitsize */
2145 FALSE, /* pc_relative */
2146 0, /* bitpos */
2147 complain_overflow_dont, /* complain_on_overflow */
2148 ppc64_elf_unhandled_reloc, /* special_function */
2149 "R_PPC64_TPREL16_HIGHA", /* name */
2150 FALSE, /* partial_inplace */
2151 0, /* src_mask */
2152 0xffff, /* dst_mask */
2153 FALSE), /* pcrel_offset */
2154
006589cf
AM
2155 /* Marker reloc on ELFv2 large-model function entry. */
2156 HOWTO (R_PPC64_ENTRY,
2157 0, /* rightshift */
2158 2, /* size (0 = byte, 1 = short, 2 = long) */
2159 32, /* bitsize */
2160 FALSE, /* pc_relative */
2161 0, /* bitpos */
2162 complain_overflow_dont, /* complain_on_overflow */
2163 bfd_elf_generic_reloc, /* special_function */
2164 "R_PPC64_ENTRY", /* name */
2165 FALSE, /* partial_inplace */
2166 0, /* src_mask */
2167 0, /* dst_mask */
2168 FALSE), /* pcrel_offset */
2169
45965137
AM
2170 /* Like ADDR64, but use local entry point of function. */
2171 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2172 0, /* rightshift */
2173 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2174 64, /* bitsize */
2175 FALSE, /* pc_relative */
2176 0, /* bitpos */
2177 complain_overflow_dont, /* complain_on_overflow */
2178 bfd_elf_generic_reloc, /* special_function */
2179 "R_PPC64_ADDR64_LOCAL", /* name */
2180 FALSE, /* partial_inplace */
2181 0, /* src_mask */
2182 ONES (64), /* dst_mask */
2183 FALSE), /* pcrel_offset */
2184
5bd4f169
AM
2185 /* GNU extension to record C++ vtable hierarchy. */
2186 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2187 0, /* rightshift */
2188 0, /* size (0 = byte, 1 = short, 2 = long) */
2189 0, /* bitsize */
b34976b6 2190 FALSE, /* pc_relative */
5bd4f169
AM
2191 0, /* bitpos */
2192 complain_overflow_dont, /* complain_on_overflow */
2193 NULL, /* special_function */
2194 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2195 FALSE, /* partial_inplace */
5bd4f169
AM
2196 0, /* src_mask */
2197 0, /* dst_mask */
b34976b6 2198 FALSE), /* pcrel_offset */
5bd4f169
AM
2199
2200 /* GNU extension to record C++ vtable member usage. */
2201 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2202 0, /* rightshift */
2203 0, /* size (0 = byte, 1 = short, 2 = long) */
2204 0, /* bitsize */
b34976b6 2205 FALSE, /* pc_relative */
5bd4f169
AM
2206 0, /* bitpos */
2207 complain_overflow_dont, /* complain_on_overflow */
2208 NULL, /* special_function */
2209 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2210 FALSE, /* partial_inplace */
5bd4f169
AM
2211 0, /* src_mask */
2212 0, /* dst_mask */
b34976b6 2213 FALSE), /* pcrel_offset */
5bd4f169
AM
2214};
2215
2216\f
2217/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2218 be done. */
2219
2220static void
4ce794b7 2221ppc_howto_init (void)
5bd4f169
AM
2222{
2223 unsigned int i, type;
2224
a4b6fadd 2225 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2226 {
2227 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2228 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2229 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2230 }
2231}
2232
2233static reloc_howto_type *
4ce794b7
AM
2234ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2235 bfd_reloc_code_real_type code)
5bd4f169 2236{
411e1bfb 2237 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2238
2239 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2240 /* Initialize howto table if needed. */
2241 ppc_howto_init ();
2242
4ce794b7 2243 switch (code)
5bd4f169
AM
2244 {
2245 default:
4ce794b7 2246 return NULL;
5bd4f169 2247
411e1bfb
AM
2248 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2249 break;
2250 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2251 break;
2252 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2253 break;
2254 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2255 break;
2256 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2257 break;
2258 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2259 break;
f9c6b907
AM
2260 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2261 break;
411e1bfb 2262 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2263 break;
f9c6b907
AM
2264 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2265 break;
411e1bfb 2266 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2267 break;
411e1bfb 2268 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2269 break;
411e1bfb 2270 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2271 break;
411e1bfb 2272 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2273 break;
411e1bfb 2274 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2275 break;
411e1bfb 2276 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2277 break;
411e1bfb 2278 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2279 break;
411e1bfb 2280 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2281 break;
411e1bfb 2282 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2283 break;
411e1bfb 2284 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2285 break;
411e1bfb 2286 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2287 break;
411e1bfb 2288 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2289 break;
411e1bfb 2290 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2291 break;
411e1bfb 2292 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2293 break;
411e1bfb 2294 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2295 break;
411e1bfb 2296 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2297 break;
411e1bfb 2298 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2299 break;
411e1bfb 2300 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2301 break;
411e1bfb 2302 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2303 break;
411e1bfb 2304 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2305 break;
411e1bfb 2306 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2307 break;
411e1bfb 2308 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2309 break;
411e1bfb 2310 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2311 break;
411e1bfb 2312 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2313 break;
411e1bfb 2314 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2315 break;
411e1bfb 2316 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2317 break;
411e1bfb 2318 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2319 break;
411e1bfb 2320 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2321 break;
411e1bfb 2322 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2323 break;
411e1bfb 2324 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2325 break;
411e1bfb 2326 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2327 break;
411e1bfb 2328 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2329 break;
411e1bfb 2330 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2331 break;
411e1bfb 2332 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2333 break;
411e1bfb 2334 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2335 break;
411e1bfb 2336 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2337 break;
411e1bfb 2338 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2339 break;
411e1bfb 2340 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2341 break;
411e1bfb 2342 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2343 break;
411e1bfb 2344 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2345 break;
411e1bfb 2346 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2347 break;
411e1bfb 2348 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2349 break;
411e1bfb 2350 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2351 break;
411e1bfb 2352 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2353 break;
411e1bfb 2354 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2355 break;
411e1bfb 2356 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2357 break;
411e1bfb 2358 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2359 break;
411e1bfb 2360 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2361 break;
411e1bfb 2362 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2363 break;
411e1bfb 2364 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2365 break;
411e1bfb 2366 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2367 break;
411e1bfb 2368 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2369 break;
411e1bfb 2370 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2371 break;
727fc41e
AM
2372 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2373 break;
2374 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2375 break;
411e1bfb 2376 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2377 break;
411e1bfb 2378 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2379 break;
411e1bfb 2380 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2381 break;
411e1bfb 2382 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2383 break;
f9c6b907
AM
2384 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2385 break;
411e1bfb 2386 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2387 break;
f9c6b907
AM
2388 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2389 break;
411e1bfb 2390 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2391 break;
411e1bfb
AM
2392 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2393 break;
2394 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2395 break;
2396 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2397 break;
f9c6b907
AM
2398 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2399 break;
411e1bfb
AM
2400 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2401 break;
f9c6b907
AM
2402 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2403 break;
411e1bfb
AM
2404 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2409 break;
2410 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2411 break;
2412 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2413 break;
2414 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2415 break;
2416 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2417 break;
2418 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2419 break;
2420 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2421 break;
2422 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2423 break;
2424 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2425 break;
2426 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2427 break;
2428 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2429 break;
2430 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2431 break;
2432 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2433 break;
2434 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2435 break;
2436 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2437 break;
2438 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2439 break;
2440 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2441 break;
2442 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2443 break;
2444 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2445 break;
2446 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2447 break;
2448 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2449 break;
2450 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2451 break;
2452 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2453 break;
2454 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2455 break;
2456 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2457 break;
2458 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2459 break;
2460 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2461 break;
25f23106
AM
2462 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2463 break;
2464 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2465 break;
2466 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2467 break;
2468 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2469 break;
7ba71655
AM
2470 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2471 break;
a680de9a
PB
2472 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2473 break;
006589cf
AM
2474 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2475 break;
45965137
AM
2476 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2477 break;
411e1bfb
AM
2478 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2479 break;
2480 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2481 break;
2482 }
2483
4ce794b7 2484 return ppc64_elf_howto_table[r];
5bd4f169
AM
2485};
2486
157090f7
AM
2487static reloc_howto_type *
2488ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2489 const char *r_name)
2490{
2491 unsigned int i;
2492
a4b6fadd 2493 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2494 if (ppc64_elf_howto_raw[i].name != NULL
2495 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2496 return &ppc64_elf_howto_raw[i];
2497
2498 return NULL;
2499}
2500
5bd4f169
AM
2501/* Set the howto pointer for a PowerPC ELF reloc. */
2502
2503static void
4aef7643 2504ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 2505 Elf_Internal_Rela *dst)
5bd4f169 2506{
65f38f15
AM
2507 unsigned int type;
2508
ef60b7ff 2509 /* Initialize howto table if needed. */
5bd4f169 2510 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2511 ppc_howto_init ();
2512
65f38f15 2513 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2514 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2515 {
695344c0 2516 /* xgettext:c-format */
0aa13fee 2517 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 2518 abfd, type);
3ec2b351 2519 type = R_PPC64_NONE;
d0fb9a8d 2520 }
65f38f15 2521 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2522}
2523
04c9666a 2524/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2525
2526static bfd_reloc_status_type
4ce794b7
AM
2527ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2528 void *data, asection *input_section,
2529 bfd *output_bfd, char **error_message)
5bd4f169 2530{
a680de9a
PB
2531 enum elf_ppc64_reloc_type r_type;
2532 long insn;
2533 bfd_size_type octets;
3de43e7b 2534 bfd_vma value;
a680de9a 2535
805fc799
AM
2536 /* If this is a relocatable link (output_bfd test tells us), just
2537 call the generic function. Any adjustment will be done at final
2538 link time. */
2539 if (output_bfd != NULL)
cedb70c5 2540 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2541 input_section, output_bfd, error_message);
2542
2543 /* Adjust the addend for sign extension of the low 16 bits.
2544 We won't actually be using the low 16 bits, so trashing them
2545 doesn't matter. */
2546 reloc_entry->addend += 0x8000;
a680de9a
PB
2547 r_type = reloc_entry->howto->type;
2548 if (r_type != R_PPC64_REL16DX_HA)
2549 return bfd_reloc_continue;
2550
2551 value = 0;
2552 if (!bfd_is_com_section (symbol->section))
2553 value = symbol->value;
2554 value += (reloc_entry->addend
2555 + symbol->section->output_offset
2556 + symbol->section->output_section->vma);
2557 value -= (reloc_entry->address
2558 + input_section->output_offset
2559 + input_section->output_section->vma);
3de43e7b 2560 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
2561
2562 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2563 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2564 insn &= ~0x1fffc1;
3de43e7b 2565 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 2566 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 2567 if (value + 0x8000 > 0xffff)
a680de9a
PB
2568 return bfd_reloc_overflow;
2569 return bfd_reloc_ok;
805fc799 2570}
5bd4f169 2571
2441e016
AM
2572static bfd_reloc_status_type
2573ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2574 void *data, asection *input_section,
2575 bfd *output_bfd, char **error_message)
2576{
2577 if (output_bfd != NULL)
2578 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2579 input_section, output_bfd, error_message);
2580
699733f6
AM
2581 if (strcmp (symbol->section->name, ".opd") == 0
2582 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2583 {
2584 bfd_vma dest = opd_entry_value (symbol->section,
2585 symbol->value + reloc_entry->addend,
aef36ac1 2586 NULL, NULL, FALSE);
2441e016
AM
2587 if (dest != (bfd_vma) -1)
2588 reloc_entry->addend = dest - (symbol->value
2589 + symbol->section->output_section->vma
2590 + symbol->section->output_offset);
2591 }
810d4e75
AM
2592 else
2593 {
2594 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2595
2596 if (symbol->section->owner != abfd
9f284bf9 2597 && symbol->section->owner != NULL
810d4e75
AM
2598 && abiversion (symbol->section->owner) >= 2)
2599 {
2600 unsigned int i;
2601
2602 for (i = 0; i < symbol->section->owner->symcount; ++i)
2603 {
2604 asymbol *symdef = symbol->section->owner->outsymbols[i];
2605
2606 if (strcmp (symdef->name, symbol->name) == 0)
2607 {
2608 elfsym = (elf_symbol_type *) symdef;
2609 break;
2610 }
2611 }
2612 }
2613 reloc_entry->addend
2614 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2615 }
2441e016
AM
2616 return bfd_reloc_continue;
2617}
2618
805fc799 2619static bfd_reloc_status_type
4ce794b7
AM
2620ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2621 void *data, asection *input_section,
2622 bfd *output_bfd, char **error_message)
805fc799
AM
2623{
2624 long insn;
04c9666a 2625 enum elf_ppc64_reloc_type r_type;
805fc799 2626 bfd_size_type octets;
794e51c0
AM
2627 /* Assume 'at' branch hints. */
2628 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2629
2630 /* If this is a relocatable link (output_bfd test tells us), just
2631 call the generic function. Any adjustment will be done at final
2632 link time. */
5bd4f169 2633 if (output_bfd != NULL)
cedb70c5 2634 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2635 input_section, output_bfd, error_message);
2636
2637 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2638 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2639 insn &= ~(0x01 << 21);
4ce794b7 2640 r_type = reloc_entry->howto->type;
805fc799
AM
2641 if (r_type == R_PPC64_ADDR14_BRTAKEN
2642 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2643 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2644
794e51c0 2645 if (is_isa_v2)
5bd4f169 2646 {
805fc799
AM
2647 /* Set 'a' bit. This is 0b00010 in BO field for branch
2648 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2649 for branch on CTR insns (BO == 1a00t or 1a01t). */
2650 if ((insn & (0x14 << 21)) == (0x04 << 21))
2651 insn |= 0x02 << 21;
2652 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2653 insn |= 0x08 << 21;
2654 else
2441e016 2655 goto out;
5bd4f169 2656 }
805fc799
AM
2657 else
2658 {
2659 bfd_vma target = 0;
2660 bfd_vma from;
5bd4f169 2661
805fc799
AM
2662 if (!bfd_is_com_section (symbol->section))
2663 target = symbol->value;
2664 target += symbol->section->output_section->vma;
2665 target += symbol->section->output_offset;
2666 target += reloc_entry->addend;
5bd4f169 2667
805fc799
AM
2668 from = (reloc_entry->address
2669 + input_section->output_offset
2670 + input_section->output_section->vma);
5bd4f169 2671
805fc799
AM
2672 /* Invert 'y' bit if not the default. */
2673 if ((bfd_signed_vma) (target - from) < 0)
2674 insn ^= 0x01 << 21;
2675 }
4ce794b7 2676 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2677 out:
2678 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2679 input_section, output_bfd, error_message);
805fc799 2680}
5bd4f169 2681
805fc799 2682static bfd_reloc_status_type
4ce794b7
AM
2683ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2684 void *data, asection *input_section,
2685 bfd *output_bfd, char **error_message)
805fc799
AM
2686{
2687 /* If this is a relocatable link (output_bfd test tells us), just
2688 call the generic function. Any adjustment will be done at final
2689 link time. */
2690 if (output_bfd != NULL)
cedb70c5 2691 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2692 input_section, output_bfd, error_message);
5bd4f169 2693
805fc799
AM
2694 /* Subtract the symbol section base address. */
2695 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2696 return bfd_reloc_continue;
2697}
2698
805fc799 2699static bfd_reloc_status_type
4ce794b7
AM
2700ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2701 void *data, asection *input_section,
2702 bfd *output_bfd, char **error_message)
805fc799
AM
2703{
2704 /* If this is a relocatable link (output_bfd test tells us), just
2705 call the generic function. Any adjustment will be done at final
2706 link time. */
2707 if (output_bfd != NULL)
cedb70c5 2708 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2709 input_section, output_bfd, error_message);
2710
2711 /* Subtract the symbol section base address. */
2712 reloc_entry->addend -= symbol->section->output_section->vma;
2713
2714 /* Adjust the addend for sign extension of the low 16 bits. */
2715 reloc_entry->addend += 0x8000;
2716 return bfd_reloc_continue;
2717}
2718
2719static bfd_reloc_status_type
4ce794b7
AM
2720ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2721 void *data, asection *input_section,
2722 bfd *output_bfd, char **error_message)
805fc799
AM
2723{
2724 bfd_vma TOCstart;
2725
2726 /* If this is a relocatable link (output_bfd test tells us), just
2727 call the generic function. Any adjustment will be done at final
2728 link time. */
2729 if (output_bfd != NULL)
cedb70c5 2730 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2731 input_section, output_bfd, error_message);
2732
2733 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2734 if (TOCstart == 0)
1c865ab2 2735 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2736
2737 /* Subtract the TOC base address. */
2738 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2739 return bfd_reloc_continue;
2740}
2741
2742static bfd_reloc_status_type
4ce794b7
AM
2743ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2744 void *data, asection *input_section,
2745 bfd *output_bfd, char **error_message)
805fc799
AM
2746{
2747 bfd_vma TOCstart;
2748
2749 /* If this is a relocatable link (output_bfd test tells us), just
2750 call the generic function. Any adjustment will be done at final
2751 link time. */
2752 if (output_bfd != NULL)
cedb70c5 2753 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2754 input_section, output_bfd, error_message);
2755
2756 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2757 if (TOCstart == 0)
1c865ab2 2758 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2759
2760 /* Subtract the TOC base address. */
2761 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2762
2763 /* Adjust the addend for sign extension of the low 16 bits. */
2764 reloc_entry->addend += 0x8000;
2765 return bfd_reloc_continue;
2766}
2767
2768static bfd_reloc_status_type
4ce794b7
AM
2769ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2770 void *data, asection *input_section,
2771 bfd *output_bfd, char **error_message)
805fc799
AM
2772{
2773 bfd_vma TOCstart;
2774 bfd_size_type octets;
2775
2776 /* If this is a relocatable link (output_bfd test tells us), just
2777 call the generic function. Any adjustment will be done at final
2778 link time. */
2779 if (output_bfd != NULL)
cedb70c5 2780 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2781 input_section, output_bfd, error_message);
2782
2783 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2784 if (TOCstart == 0)
1c865ab2 2785 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2786
2787 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2788 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2789 return bfd_reloc_ok;
2790}
2791
2792static bfd_reloc_status_type
4ce794b7
AM
2793ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2794 void *data, asection *input_section,
2795 bfd *output_bfd, char **error_message)
805fc799
AM
2796{
2797 /* If this is a relocatable link (output_bfd test tells us), just
2798 call the generic function. Any adjustment will be done at final
2799 link time. */
2800 if (output_bfd != NULL)
cedb70c5 2801 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2802 input_section, output_bfd, error_message);
2803
2804 if (error_message != NULL)
2805 {
2806 static char buf[60];
2807 sprintf (buf, "generic linker can't handle %s",
2808 reloc_entry->howto->name);
2809 *error_message = buf;
2810 }
2811 return bfd_reloc_dangerous;
2812}
2813
927be08e
AM
2814/* Track GOT entries needed for a given symbol. We might need more
2815 than one got entry per symbol. */
2816struct got_entry
2817{
2818 struct got_entry *next;
2819
2820 /* The symbol addend that we'll be placing in the GOT. */
2821 bfd_vma addend;
2822
2823 /* Unlike other ELF targets, we use separate GOT entries for the same
2824 symbol referenced from different input files. This is to support
2825 automatic multiple TOC/GOT sections, where the TOC base can vary
2826 from one input file to another. After partitioning into TOC groups
2827 we merge entries within the group.
2828
2829 Point to the BFD owning this GOT entry. */
2830 bfd *owner;
2831
2832 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2833 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2834 unsigned char tls_type;
927be08e
AM
2835
2836 /* Non-zero if got.ent points to real entry. */
f961d9dd 2837 unsigned char is_indirect;
927be08e
AM
2838
2839 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2840 union
2841 {
2842 bfd_signed_vma refcount;
2843 bfd_vma offset;
2844 struct got_entry *ent;
2845 } got;
2846};
2847
2848/* The same for PLT. */
2849struct plt_entry
2850{
2851 struct plt_entry *next;
2852
2853 bfd_vma addend;
2854
2855 union
2856 {
2857 bfd_signed_vma refcount;
2858 bfd_vma offset;
2859 } plt;
2860};
2861
e717da7e
AM
2862struct ppc64_elf_obj_tdata
2863{
2864 struct elf_obj_tdata elf;
2865
2866 /* Shortcuts to dynamic linker sections. */
2867 asection *got;
2868 asection *relgot;
2869
b3fac117
AM
2870 /* Used during garbage collection. We attach global symbols defined
2871 on removed .opd entries to this section so that the sym is removed. */
2872 asection *deleted_section;
81688140 2873
927be08e 2874 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2875 sections means we potentially need one of these for each input bfd. */
927be08e 2876 struct got_entry tlsld_got;
8860955f 2877
729eabd5
AM
2878 union {
2879 /* A copy of relocs before they are modified for --emit-relocs. */
2880 Elf_Internal_Rela *relocs;
2881
2882 /* Section contents. */
2883 bfd_byte *contents;
2884 } opd;
d77c8a4b
AM
2885
2886 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2887 the reloc to be in the range -32768 to 32767. */
98528052
AM
2888 unsigned int has_small_toc_reloc : 1;
2889
560c8763
AM
2890 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2891 instruction not one we handle. */
2892 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2893};
2894
2895#define ppc64_elf_tdata(bfd) \
2896 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2897
2898#define ppc64_tlsld_got(bfd) \
2899 (&ppc64_elf_tdata (bfd)->tlsld_got)
2900
0c8d6e5c
AM
2901#define is_ppc64_elf(bfd) \
2902 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2903 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2904
e717da7e
AM
2905/* Override the generic function because we store some extras. */
2906
2907static bfd_boolean
2908ppc64_elf_mkobject (bfd *abfd)
2909{
0ffa91dd 2910 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2911 PPC64_ELF_DATA);
e717da7e
AM
2912}
2913
feee612b 2914/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2915 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2916
b34976b6 2917static bfd_boolean
4ce794b7 2918ppc64_elf_object_p (bfd *abfd)
feee612b 2919{
14b57c7c
AM
2920 if (!abfd->arch_info->the_default)
2921 return TRUE;
2922
2923 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2924 {
2925 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2926
2927 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2928 {
2929 /* Relies on arch after 32 bit default being 64 bit default. */
2930 abfd->arch_info = abfd->arch_info->next;
2931 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2932 }
2933 }
14b57c7c 2934 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2935}
2936
d37c89e5
AM
2937/* Support for core dump NOTE sections. */
2938
2939static bfd_boolean
2940ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2941{
eea6121a 2942 size_t offset, size;
d37c89e5
AM
2943
2944 if (note->descsz != 504)
2945 return FALSE;
2946
2947 /* pr_cursig */
228e534f 2948 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2949
2950 /* pr_pid */
228e534f 2951 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2952
2953 /* pr_reg */
2954 offset = 112;
eea6121a 2955 size = 384;
d37c89e5
AM
2956
2957 /* Make a ".reg/999" section. */
2958 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2959 size, note->descpos + offset);
d37c89e5
AM
2960}
2961
2962static bfd_boolean
2963ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2964{
2965 if (note->descsz != 136)
2966 return FALSE;
2967
228e534f 2968 elf_tdata (abfd)->core->pid
bc989cdc 2969 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2970 elf_tdata (abfd)->core->program
d37c89e5 2971 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2972 elf_tdata (abfd)->core->command
d37c89e5
AM
2973 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2974
2975 return TRUE;
2976}
2977
183e98be
AM
2978static char *
2979ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2980 ...)
2981{
2982 switch (note_type)
2983 {
2984 default:
2985 return NULL;
2986
2987 case NT_PRPSINFO:
2988 {
2989 char data[136];
2990 va_list ap;
2991
2992 va_start (ap, note_type);
75cd47ed 2993 memset (data, 0, sizeof (data));
183e98be
AM
2994 strncpy (data + 40, va_arg (ap, const char *), 16);
2995 strncpy (data + 56, va_arg (ap, const char *), 80);
2996 va_end (ap);
2997 return elfcore_write_note (abfd, buf, bufsiz,
2998 "CORE", note_type, data, sizeof (data));
2999 }
3000
3001 case NT_PRSTATUS:
3002 {
3003 char data[504];
3004 va_list ap;
3005 long pid;
3006 int cursig;
3007 const void *greg;
3008
3009 va_start (ap, note_type);
3010 memset (data, 0, 112);
3011 pid = va_arg (ap, long);
3012 bfd_put_32 (abfd, pid, data + 32);
3013 cursig = va_arg (ap, int);
3014 bfd_put_16 (abfd, cursig, data + 12);
3015 greg = va_arg (ap, const void *);
3016 memcpy (data + 112, greg, 384);
3017 memset (data + 496, 0, 8);
3018 va_end (ap);
3019 return elfcore_write_note (abfd, buf, bufsiz,
3020 "CORE", note_type, data, sizeof (data));
3021 }
3022 }
3023}
3024
5d35169e
AM
3025/* Add extra PPC sections. */
3026
b35d266b 3027static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3028{
07d6d2b8
AM
3029 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3030 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3031 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
3032 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3033 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3034 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 3035 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3036};
3037
7c8fe5c4
AM
3038enum _ppc64_sec_type {
3039 sec_normal = 0,
3040 sec_opd = 1,
3041 sec_toc = 2
3042};
3043
f0abc2a1
AM
3044struct _ppc64_elf_section_data
3045{
3046 struct bfd_elf_section_data elf;
411e1bfb 3047
f0abc2a1
AM
3048 union
3049 {
51aecdc5
AM
3050 /* An array with one entry for each opd function descriptor,
3051 and some spares since opd entries may be either 16 or 24 bytes. */
3052#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3053 struct _opd_sec_data
3054 {
3055 /* Points to the function code section for local opd entries. */
3056 asection **func_sec;
3057
3058 /* After editing .opd, adjust references to opd local syms. */
3059 long *adjust;
3060 } opd;
7c8fe5c4 3061
3a71aa26
AM
3062 /* An array for toc sections, indexed by offset/8. */
3063 struct _toc_sec_data
3064 {
3065 /* Specifies the relocation symbol index used at a given toc offset. */
3066 unsigned *symndx;
3067
3068 /* And the relocation addend. */
3069 bfd_vma *add;
3070 } toc;
7c8fe5c4
AM
3071 } u;
3072
3073 enum _ppc64_sec_type sec_type:2;
411e1bfb 3074
7c8fe5c4
AM
3075 /* Flag set when small branches are detected. Used to
3076 select suitable defaults for the stub group size. */
3077 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3078};
3079
3080#define ppc64_elf_section_data(sec) \
411e1bfb 3081 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3082
3083static bfd_boolean
4ce794b7 3084ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3085{
f592407e
AM
3086 if (!sec->used_by_bfd)
3087 {
3088 struct _ppc64_elf_section_data *sdata;
3089 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3090
f592407e
AM
3091 sdata = bfd_zalloc (abfd, amt);
3092 if (sdata == NULL)
3093 return FALSE;
3094 sec->used_by_bfd = sdata;
3095 }
f0abc2a1
AM
3096
3097 return _bfd_elf_new_section_hook (abfd, sec);
3098}
4025353c 3099
74f0fb50 3100static struct _opd_sec_data *
4025353c
AM
3101get_opd_info (asection * sec)
3102{
3103 if (sec != NULL
3104 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3105 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3106 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3107 return NULL;
3108}
90e3cdf2
JJ
3109\f
3110/* Parameters for the qsort hook. */
90e3cdf2 3111static bfd_boolean synthetic_relocatable;
cd285db5 3112static asection *synthetic_opd;
90e3cdf2 3113
699733f6 3114/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3115
3116static int
3117compare_symbols (const void *ap, const void *bp)
3118{
3119 const asymbol *a = * (const asymbol **) ap;
3120 const asymbol *b = * (const asymbol **) bp;
3121
699733f6
AM
3122 /* Section symbols first. */
3123 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3124 return -1;
699733f6 3125 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3126 return 1;
3127
699733f6 3128 /* then .opd symbols. */
cd285db5
AM
3129 if (synthetic_opd != NULL)
3130 {
3131 if (strcmp (a->section->name, ".opd") == 0
3132 && strcmp (b->section->name, ".opd") != 0)
3133 return -1;
3134 if (strcmp (a->section->name, ".opd") != 0
3135 && strcmp (b->section->name, ".opd") == 0)
3136 return 1;
3137 }
90e3cdf2 3138
699733f6 3139 /* then other code symbols. */
90e3cdf2
JJ
3140 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3141 == (SEC_CODE | SEC_ALLOC)
3142 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3143 != (SEC_CODE | SEC_ALLOC))
3144 return -1;
3145
3146 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3147 != (SEC_CODE | SEC_ALLOC)
3148 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3149 == (SEC_CODE | SEC_ALLOC))
3150 return 1;
3151
3152 if (synthetic_relocatable)
3153 {
3154 if (a->section->id < b->section->id)
3155 return -1;
3156
3157 if (a->section->id > b->section->id)
3158 return 1;
3159 }
3160
3161 if (a->value + a->section->vma < b->value + b->section->vma)
3162 return -1;
3163
3164 if (a->value + a->section->vma > b->value + b->section->vma)
3165 return 1;
3166
4d35a0aa
AM
3167 /* For syms with the same value, prefer strong dynamic global function
3168 syms over other syms. */
3169 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3170 return -1;
3171
3172 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3173 return 1;
3174
3175 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3176 return -1;
3177
3178 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3179 return 1;
3180
3181 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3182 return -1;
3183
3184 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3185 return 1;
3186
3187 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3188 return -1;
3189
3190 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3191 return 1;
3192
aaed6f5b 3193 return a > b;
90e3cdf2
JJ
3194}
3195
699733f6 3196/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3197
699733f6 3198static asymbol *
7292b3ac 3199sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3200{
699733f6 3201 long mid;
90e3cdf2 3202
7292b3ac 3203 if (id == (unsigned) -1)
699733f6
AM
3204 {
3205 while (lo < hi)
3206 {
3207 mid = (lo + hi) >> 1;
3208 if (syms[mid]->value + syms[mid]->section->vma < value)
3209 lo = mid + 1;
3210 else if (syms[mid]->value + syms[mid]->section->vma > value)
3211 hi = mid;
3212 else
3213 return syms[mid];
3214 }
3215 }
3216 else
3217 {
3218 while (lo < hi)
3219 {
3220 mid = (lo + hi) >> 1;
3221 if (syms[mid]->section->id < id)
3222 lo = mid + 1;
3223 else if (syms[mid]->section->id > id)
3224 hi = mid;
3225 else if (syms[mid]->value < value)
3226 lo = mid + 1;
3227 else if (syms[mid]->value > value)
3228 hi = mid;
3229 else
3230 return syms[mid];
3231 }
3232 }
3233 return NULL;
90e3cdf2
JJ
3234}
3235
468392fb
AM
3236static bfd_boolean
3237section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3238{
3239 bfd_vma vma = *(bfd_vma *) ptr;
3240 return ((section->flags & SEC_ALLOC) != 0
3241 && section->vma <= vma
3242 && vma < section->vma + section->size);
3243}
3244
699733f6 3245/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3246 entry syms. Also generate @plt symbols for the glink branch table.
3247 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3248
3249static long
a7535cf3
AM
3250ppc64_elf_get_synthetic_symtab (bfd *abfd,
3251 long static_count, asymbol **static_syms,
3252 long dyn_count, asymbol **dyn_syms,
c9727e01 3253 asymbol **ret)
90e3cdf2
JJ
3254{
3255 asymbol *s;
699733f6
AM
3256 long i;
3257 long count;
90e3cdf2 3258 char *names;
a7535cf3 3259 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3260 asection *opd = NULL;
90e3cdf2 3261 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3262 asymbol **syms;
ee67d69a 3263 int abi = abiversion (abfd);
90e3cdf2
JJ
3264
3265 *ret = NULL;
3266
ee67d69a
AM
3267 if (abi < 2)
3268 {
3269 opd = bfd_get_section_by_name (abfd, ".opd");
3270 if (opd == NULL && abi == 1)
3271 return 0;
3272 }
90e3cdf2 3273
a5259595
AM
3274 syms = NULL;
3275 codesecsym = 0;
3276 codesecsymend = 0;
3277 secsymend = 0;
3278 opdsymend = 0;
3279 symcount = 0;
3280 if (opd != NULL)
c9727e01 3281 {
a5259595
AM
3282 symcount = static_count;
3283 if (!relocatable)
3284 symcount += dyn_count;
3285 if (symcount == 0)
3286 return 0;
c9727e01 3287
a5259595
AM
3288 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3289 if (syms == NULL)
3290 return -1;
90e3cdf2 3291
a5259595
AM
3292 if (!relocatable && static_count != 0 && dyn_count != 0)
3293 {
3294 /* Use both symbol tables. */
3295 memcpy (syms, static_syms, static_count * sizeof (*syms));
3296 memcpy (syms + static_count, dyn_syms,
3297 (dyn_count + 1) * sizeof (*syms));
3298 }
3299 else if (!relocatable && static_count == 0)
3300 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3301 else
3302 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3303
a5259595
AM
3304 synthetic_relocatable = relocatable;
3305 synthetic_opd = opd;
3306 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3307
a5259595
AM
3308 if (!relocatable && symcount > 1)
3309 {
3310 long j;
3311 /* Trim duplicate syms, since we may have merged the normal and
3312 dynamic symbols. Actually, we only care about syms that have
3313 different values, so trim any with the same value. */
3314 for (i = 1, j = 1; i < symcount; ++i)
3315 if (syms[i - 1]->value + syms[i - 1]->section->vma
3316 != syms[i]->value + syms[i]->section->vma)
3317 syms[j++] = syms[i];
3318 symcount = j;
3319 }
699733f6 3320
a5259595
AM
3321 i = 0;
3322 /* Note that here and in compare_symbols we can't compare opd and
3323 sym->section directly. With separate debug info files, the
3324 symbols will be extracted from the debug file while abfd passed
3325 to this function is the real binary. */
3326 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3327 ++i;
3328 codesecsym = i;
3329
3330 for (; i < symcount; ++i)
3331 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3332 | SEC_THREAD_LOCAL))
3333 != (SEC_CODE | SEC_ALLOC))
3334 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3335 break;
3336 codesecsymend = i;
3337
3338 for (; i < symcount; ++i)
3339 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3340 break;
3341 secsymend = i;
3342
3343 for (; i < symcount; ++i)
3344 if (strcmp (syms[i]->section->name, ".opd") != 0)
3345 break;
3346 opdsymend = i;
3347
3348 for (; i < symcount; ++i)
3349 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3350 != (SEC_CODE | SEC_ALLOC))
3351 break;
3352 symcount = i;
3353 }
c9727e01 3354 count = 0;
90e3cdf2 3355
699733f6 3356 if (relocatable)
90e3cdf2 3357 {
699733f6
AM
3358 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3359 arelent *r;
3360 size_t size;
3361 long relcount;
90e3cdf2 3362
468392fb
AM
3363 if (opdsymend == secsymend)
3364 goto done;
3365
699733f6 3366 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3367 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3368 if (relcount == 0)
c9727e01 3369 goto done;
90e3cdf2 3370
7356fed5
AM
3371 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3372 {
3373 count = -1;
3374 goto done;
3375 }
3376
699733f6 3377 size = 0;
595da8c5 3378 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3379 {
3380 asymbol *sym;
90e3cdf2 3381
595da8c5 3382 while (r < opd->relocation + relcount
699733f6
AM
3383 && r->address < syms[i]->value + opd->vma)
3384 ++r;
90e3cdf2 3385
595da8c5 3386 if (r == opd->relocation + relcount)
699733f6 3387 break;
90e3cdf2 3388
699733f6
AM
3389 if (r->address != syms[i]->value + opd->vma)
3390 continue;
90e3cdf2 3391
699733f6
AM
3392 if (r->howto->type != R_PPC64_ADDR64)
3393 continue;
90e3cdf2 3394
699733f6
AM
3395 sym = *r->sym_ptr_ptr;
3396 if (!sym_exists_at (syms, opdsymend, symcount,
3397 sym->section->id, sym->value + r->addend))
3398 {
3399 ++count;
3400 size += sizeof (asymbol);
3401 size += strlen (syms[i]->name) + 2;
3402 }
3403 }
90e3cdf2 3404
c4b0b099
AM
3405 if (size == 0)
3406 goto done;
699733f6
AM
3407 s = *ret = bfd_malloc (size);
3408 if (s == NULL)
3409 {
7356fed5 3410 count = -1;
c9727e01 3411 goto done;
699733f6 3412 }
90e3cdf2 3413
699733f6 3414 names = (char *) (s + count);
90e3cdf2 3415
595da8c5 3416 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3417 {
699733f6 3418 asymbol *sym;
90e3cdf2 3419
595da8c5 3420 while (r < opd->relocation + relcount
699733f6
AM
3421 && r->address < syms[i]->value + opd->vma)
3422 ++r;
90e3cdf2 3423
595da8c5 3424 if (r == opd->relocation + relcount)
699733f6
AM
3425 break;
3426
3427 if (r->address != syms[i]->value + opd->vma)
3428 continue;
3429
3430 if (r->howto->type != R_PPC64_ADDR64)
3431 continue;
90e3cdf2 3432
699733f6
AM
3433 sym = *r->sym_ptr_ptr;
3434 if (!sym_exists_at (syms, opdsymend, symcount,
3435 sym->section->id, sym->value + r->addend))
3436 {
3437 size_t len;
3438
3439 *s = *syms[i];
6ba2a415 3440 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3441 s->section = sym->section;
3442 s->value = sym->value + r->addend;
3443 s->name = names;
3444 *names++ = '.';
3445 len = strlen (syms[i]->name);
3446 memcpy (names, syms[i]->name, len + 1);
3447 names += len + 1;
6f610d07
UW
3448 /* Have udata.p point back to the original symbol this
3449 synthetic symbol was derived from. */
3450 s->udata.p = syms[i];
699733f6
AM
3451 s++;
3452 }
3453 }
3454 }
3455 else
90e3cdf2 3456 {
468392fb 3457 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3458 bfd_byte *contents = NULL;
699733f6 3459 size_t size;
468392fb
AM
3460 long plt_count = 0;
3461 bfd_vma glink_vma = 0, resolv_vma = 0;
3462 asection *dynamic, *glink = NULL, *relplt = NULL;
3463 arelent *p;
90e3cdf2 3464
ee67d69a 3465 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3466 {
c4b0b099
AM
3467 free_contents_and_exit_err:
3468 count = -1;
ee67d69a 3469 free_contents_and_exit:
699733f6 3470 if (contents)
ee67d69a 3471 free (contents);
c9727e01 3472 goto done;
699733f6 3473 }
90e3cdf2 3474
699733f6
AM
3475 size = 0;
3476 for (i = secsymend; i < opdsymend; ++i)
3477 {
3478 bfd_vma ent;
90e3cdf2 3479
5ef11c02
AM
3480 /* Ignore bogus symbols. */
3481 if (syms[i]->value > opd->size - 8)
3482 continue;
3483
699733f6
AM
3484 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3485 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3486 {
3487 ++count;
3488 size += sizeof (asymbol);
3489 size += strlen (syms[i]->name) + 2;
3490 }
3491 }
90e3cdf2 3492
468392fb 3493 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3494 if (dyn_count != 0
3495 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3496 {
3497 bfd_byte *dynbuf, *extdyn, *extdynend;
3498 size_t extdynsize;
3499 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3500
3501 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3502 goto free_contents_and_exit_err;
468392fb
AM
3503
3504 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3505 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3506
3507 extdyn = dynbuf;
3508 extdynend = extdyn + dynamic->size;
3509 for (; extdyn < extdynend; extdyn += extdynsize)
3510 {
3511 Elf_Internal_Dyn dyn;
3512 (*swap_dyn_in) (abfd, extdyn, &dyn);
3513
3514 if (dyn.d_tag == DT_NULL)
3515 break;
3516
3517 if (dyn.d_tag == DT_PPC64_GLINK)
3518 {
9e390558
AM
3519 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3520 See comment in ppc64_elf_finish_dynamic_sections. */
3521 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3522 /* The .glink section usually does not survive the final
3523 link; search for the section (usually .text) where the
3524 glink stubs now reside. */
3525 glink = bfd_sections_find_if (abfd, section_covers_vma,
3526 &glink_vma);
3527 break;
3528 }
3529 }
3530
3531 free (dynbuf);
3532 }
3533
3534 if (glink != NULL)
3535 {
3536 /* Determine __glink trampoline by reading the relative branch
3537 from the first glink stub. */
3538 bfd_byte buf[4];
b9e5796b
AM
3539 unsigned int off = 0;
3540
3541 while (bfd_get_section_contents (abfd, glink, buf,
3542 glink_vma + off - glink->vma, 4))
468392fb
AM
3543 {
3544 unsigned int insn = bfd_get_32 (abfd, buf);
3545 insn ^= B_DOT;
3546 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3547 {
3548 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3549 break;
3550 }
3551 off += 4;
3552 if (off > 4)
3553 break;
468392fb
AM
3554 }
3555
3556 if (resolv_vma)
3557 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3558
066ee829
AM
3559 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3560 if (relplt != NULL)
3561 {
3562 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3563 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3564 goto free_contents_and_exit_err;
68ffbac6 3565
066ee829
AM
3566 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3567 size += plt_count * sizeof (asymbol);
468392fb 3568
066ee829
AM
3569 p = relplt->relocation;
3570 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3571 {
3572 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3573 if (p->addend != 0)
3574 size += sizeof ("+0x") - 1 + 16;
3575 }
066ee829 3576 }
468392fb
AM
3577 }
3578
c4b0b099
AM
3579 if (size == 0)
3580 goto free_contents_and_exit;
699733f6
AM
3581 s = *ret = bfd_malloc (size);
3582 if (s == NULL)
c4b0b099 3583 goto free_contents_and_exit_err;
90e3cdf2 3584
468392fb 3585 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3586
699733f6 3587 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3588 {
699733f6 3589 bfd_vma ent;
90e3cdf2 3590
5ef11c02
AM
3591 if (syms[i]->value > opd->size - 8)
3592 continue;
3593
699733f6
AM
3594 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3595 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3596 {
c9727e01 3597 long lo, hi;
699733f6 3598 size_t len;
c9727e01 3599 asection *sec = abfd->sections;
90e3cdf2 3600
699733f6
AM
3601 *s = *syms[i];
3602 lo = codesecsym;
3603 hi = codesecsymend;
3604 while (lo < hi)
3605 {
c9727e01 3606 long mid = (lo + hi) >> 1;
699733f6
AM
3607 if (syms[mid]->section->vma < ent)
3608 lo = mid + 1;
3609 else if (syms[mid]->section->vma > ent)
3610 hi = mid;
3611 else
c9727e01
AM
3612 {
3613 sec = syms[mid]->section;
3614 break;
3615 }
699733f6
AM
3616 }
3617
c9727e01 3618 if (lo >= hi && lo > codesecsym)
699733f6 3619 sec = syms[lo - 1]->section;
699733f6
AM
3620
3621 for (; sec != NULL; sec = sec->next)
3622 {
3623 if (sec->vma > ent)
3624 break;
63524580
JK
3625 /* SEC_LOAD may not be set if SEC is from a separate debug
3626 info file. */
3627 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3628 break;
3629 if ((sec->flags & SEC_CODE) != 0)
3630 s->section = sec;
3631 }
6ba2a415 3632 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3633 s->value = ent - s->section->vma;
3634 s->name = names;
3635 *names++ = '.';
3636 len = strlen (syms[i]->name);
3637 memcpy (names, syms[i]->name, len + 1);
3638 names += len + 1;
6f610d07
UW
3639 /* Have udata.p point back to the original symbol this
3640 synthetic symbol was derived from. */
3641 s->udata.p = syms[i];
699733f6 3642 s++;
90e3cdf2 3643 }
90e3cdf2 3644 }
699733f6 3645 free (contents);
468392fb
AM
3646
3647 if (glink != NULL && relplt != NULL)
3648 {
3649 if (resolv_vma)
3650 {
3651 /* Add a symbol for the main glink trampoline. */
86a4952b 3652 memset (s, 0, sizeof *s);
468392fb 3653 s->the_bfd = abfd;
6ba2a415 3654 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3655 s->section = glink;
3656 s->value = resolv_vma - glink->vma;
3657 s->name = names;
3658 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3659 names += sizeof ("__glink_PLTresolve");
3660 s++;
3661 count++;
3662 }
3663
3664 /* FIXME: It would be very much nicer to put sym@plt on the
3665 stub rather than on the glink branch table entry. The
3666 objdump disassembler would then use a sensible symbol
3667 name on plt calls. The difficulty in doing so is
3668 a) finding the stubs, and,
3669 b) matching stubs against plt entries, and,
3670 c) there can be multiple stubs for a given plt entry.
3671
3672 Solving (a) could be done by code scanning, but older
3673 ppc64 binaries used different stubs to current code.
3674 (b) is the tricky one since you need to known the toc
3675 pointer for at least one function that uses a pic stub to
3676 be able to calculate the plt address referenced.
3677 (c) means gdb would need to set multiple breakpoints (or
3678 find the glink branch itself) when setting breakpoints
3679 for pending shared library loads. */
3680 p = relplt->relocation;
3681 for (i = 0; i < plt_count; i++, p++)
3682 {
3683 size_t len;
3684
3685 *s = **p->sym_ptr_ptr;
3686 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3687 we are defining a symbol, ensure one of them is set. */
3688 if ((s->flags & BSF_LOCAL) == 0)
3689 s->flags |= BSF_GLOBAL;
6ba2a415 3690 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3691 s->section = glink;
3692 s->value = glink_vma - glink->vma;
3693 s->name = names;
3694 s->udata.p = NULL;
3695 len = strlen ((*p->sym_ptr_ptr)->name);
3696 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3697 names += len;
e054468f
AM
3698 if (p->addend != 0)
3699 {
3700 memcpy (names, "+0x", sizeof ("+0x") - 1);
3701 names += sizeof ("+0x") - 1;
3702 bfd_sprintf_vma (abfd, names, p->addend);
3703 names += strlen (names);
3704 }
468392fb
AM
3705 memcpy (names, "@plt", sizeof ("@plt"));
3706 names += sizeof ("@plt");
3707 s++;
b9e5796b
AM
3708 if (abi < 2)
3709 {
3710 glink_vma += 8;
3711 if (i >= 0x8000)
3712 glink_vma += 4;
3713 }
3714 else
468392fb
AM
3715 glink_vma += 4;
3716 }
3717 count += plt_count;
3718 }
90e3cdf2
JJ
3719 }
3720
c9727e01 3721 done:
a7535cf3 3722 free (syms);
90e3cdf2
JJ
3723 return count;
3724}
5bd4f169 3725\f
65f38f15
AM
3726/* The following functions are specific to the ELF linker, while
3727 functions above are used generally. Those named ppc64_elf_* are
3728 called by the main ELF linker code. They appear in this file more
3729 or less in the order in which they are called. eg.
3730 ppc64_elf_check_relocs is called early in the link process,
3731 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3732 called.
3733
3734 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3735 functions have both a function code symbol and a function descriptor
3736 symbol. A call to foo in a relocatable object file looks like:
3737
3738 . .text
3739 . x:
3740 . bl .foo
3741 . nop
3742
3743 The function definition in another object file might be:
3744
3745 . .section .opd
3746 . foo: .quad .foo
3747 . .quad .TOC.@tocbase
3748 . .quad 0
3749 .
3750 . .text
3751 . .foo: blr
3752
3753 When the linker resolves the call during a static link, the branch
3754 unsurprisingly just goes to .foo and the .opd information is unused.
3755 If the function definition is in a shared library, things are a little
3756 different: The call goes via a plt call stub, the opd information gets
3757 copied to the plt, and the linker patches the nop.
3758
3759 . x:
3760 . bl .foo_stub
3761 . ld 2,40(1)
3762 .
3763 .
3764 . .foo_stub:
71a39c98
AM
3765 . std 2,40(1) # in practice, the call stub
3766 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3767 . addi 11,11,Lfoo@toc@l # this is the general idea
3768 . ld 12,0(11)
3769 . ld 2,8(11)
3770 . mtctr 12
3771 . ld 11,16(11)
e86ce104
AM
3772 . bctr
3773 .
3774 . .section .plt
3775 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3776
3777 The "reloc ()" notation is supposed to indicate that the linker emits
3778 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3779 copying.
3780
3781 What are the difficulties here? Well, firstly, the relocations
3782 examined by the linker in check_relocs are against the function code
3783 sym .foo, while the dynamic relocation in the plt is emitted against
3784 the function descriptor symbol, foo. Somewhere along the line, we need
3785 to carefully copy dynamic link information from one symbol to the other.
3786 Secondly, the generic part of the elf linker will make .foo a dynamic
3787 symbol as is normal for most other backends. We need foo dynamic
3788 instead, at least for an application final link. However, when
3789 creating a shared library containing foo, we need to have both symbols
3790 dynamic so that references to .foo are satisfied during the early
3791 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3792 definition from some other object, eg. a static library.
3793
3794 Update: As of August 2004, we support a new convention. Function
3795 calls may use the function descriptor symbol, ie. "bl foo". This
3796 behaves exactly as "bl .foo". */
65f38f15 3797
7c8bbca5
AM
3798/* Of those relocs that might be copied as dynamic relocs, this
3799 function selects those that must be copied when linking a shared
3800 library or PIE, even when the symbol is local. */
65f38f15 3801
1d483afe
AM
3802static int
3803must_be_dyn_reloc (struct bfd_link_info *info,
3804 enum elf_ppc64_reloc_type r_type)
3805{
3806 switch (r_type)
3807 {
3808 default:
7c8bbca5
AM
3809 /* Only relative relocs can be resolved when the object load
3810 address isn't fixed. DTPREL64 is excluded because the
3811 dynamic linker needs to differentiate global dynamic from
3812 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3813 return 1;
3814
3815 case R_PPC64_REL32:
3816 case R_PPC64_REL64:
3817 case R_PPC64_REL30:
3818 return 0;
3819
3820 case R_PPC64_TPREL16:
3821 case R_PPC64_TPREL16_LO:
3822 case R_PPC64_TPREL16_HI:
3823 case R_PPC64_TPREL16_HA:
3824 case R_PPC64_TPREL16_DS:
3825 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3826 case R_PPC64_TPREL16_HIGH:
3827 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3828 case R_PPC64_TPREL16_HIGHER:
3829 case R_PPC64_TPREL16_HIGHERA:
3830 case R_PPC64_TPREL16_HIGHEST:
3831 case R_PPC64_TPREL16_HIGHESTA:
3832 case R_PPC64_TPREL64:
7c8bbca5
AM
3833 /* These relocations are relative but in a shared library the
3834 linker doesn't know the thread pointer base. */
3835 return bfd_link_dll (info);
1d483afe
AM
3836 }
3837}
65f38f15 3838
f4656909
AM
3839/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3840 copying dynamic variables from a shared lib into an app's dynbss
3841 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3842 shared lib. With code that gcc generates, it's vital that this be
3843 enabled; In the PowerPC64 ABI, the address of a function is actually
3844 the address of a function descriptor, which resides in the .opd
3845 section. gcc uses the descriptor directly rather than going via the
3846 GOT as some other ABI's do, which means that initialized function
3847 pointers must reference the descriptor. Thus, a function pointer
3848 initialized to the address of a function in a shared library will
3849 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3850 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3851 presents a problem as a plt entry for that function is also
3852 initialized from the function descriptor symbol and the copy reloc
3853 may not be initialized first. */
a23b6845 3854#define ELIMINATE_COPY_RELOCS 1
f4656909 3855
721956f4 3856/* Section name for stubs is the associated section name plus this
29942be8
NC
3857 string. */
3858#define STUB_SUFFIX ".stub"
721956f4
AM
3859
3860/* Linker stubs.
3861 ppc_stub_long_branch:
3862 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3863 destination, but a 24 bit branch in a stub section will reach.
3864 . b dest
3865
3866 ppc_stub_plt_branch:
3867 Similar to the above, but a 24 bit branch in the stub section won't
3868 reach its destination.
71a39c98
AM
3869 . addis %r11,%r2,xxx@toc@ha
3870 . ld %r12,xxx@toc@l(%r11)
3871 . mtctr %r12
721956f4
AM
3872 . bctr
3873
3874 ppc_stub_plt_call:
2c66dc6c
AM
3875 Used to call a function in a shared library. If it so happens that
3876 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3877 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3878 . std %r2,40(%r1)
71a39c98
AM
3879 . addis %r11,%r2,xxx@toc@ha
3880 . ld %r12,xxx+0@toc@l(%r11)
3881 . mtctr %r12
3882 . ld %r2,xxx+8@toc@l(%r11)
3883 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3884 . bctr
ad8e1ba5
AM
3885
3886 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3887 code to adjust the value and save r2 to support multiple toc sections.
3888 A ppc_stub_long_branch with an r2 offset looks like:
3889 . std %r2,40(%r1)
3890 . addis %r2,%r2,off@ha
3891 . addi %r2,%r2,off@l
3892 . b dest
3893
3894 A ppc_stub_plt_branch with an r2 offset looks like:
3895 . std %r2,40(%r1)
71a39c98
AM
3896 . addis %r11,%r2,xxx@toc@ha
3897 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3898 . addis %r2,%r2,off@ha
3899 . addi %r2,%r2,off@l
71a39c98 3900 . mtctr %r12
ad8e1ba5 3901 . bctr
ac2df442
AM
3902
3903 In cases where the "addis" instruction would add zero, the "addis" is
3904 omitted and following instructions modified slightly in some cases.
721956f4
AM
3905*/
3906
3907enum ppc_stub_type {
3908 ppc_stub_none,
3909 ppc_stub_long_branch,
ad8e1ba5 3910 ppc_stub_long_branch_r2off,
721956f4 3911 ppc_stub_plt_branch,
ad8e1ba5 3912 ppc_stub_plt_branch_r2off,
794e51c0 3913 ppc_stub_plt_call,
7341d5e2 3914 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3915 ppc_stub_global_entry,
3916 ppc_stub_save_res
721956f4
AM
3917};
3918
6f20ed8a
AM
3919/* Information on stub grouping. */
3920struct map_stub
3921{
3922 /* The stub section. */
3923 asection *stub_sec;
3924 /* This is the section to which stubs in the group will be attached. */
3925 asection *link_sec;
a4b6fadd
AM
3926 /* Next group. */
3927 struct map_stub *next;
3928 /* Whether to emit a copy of register save/restore functions in this
3929 group. */
3930 int needs_save_res;
d4aaa2a0
AM
3931 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3932 or -1u if no such stub with bctrl exists. */
3933 unsigned int tls_get_addr_opt_bctrl;
6f20ed8a
AM
3934};
3935
721956f4
AM
3936struct ppc_stub_hash_entry {
3937
3938 /* Base hash table entry structure. */
3939 struct bfd_hash_entry root;
3940
ad8e1ba5
AM
3941 enum ppc_stub_type stub_type;
3942
6f20ed8a
AM
3943 /* Group information. */
3944 struct map_stub *group;
721956f4
AM
3945
3946 /* Offset within stub_sec of the beginning of this stub. */
3947 bfd_vma stub_offset;
3948
3949 /* Given the symbol's value and its section we can determine its final
3950 value when building the stubs (so the stub knows where to jump. */
3951 bfd_vma target_value;
3952 asection *target_section;
3953
721956f4
AM
3954 /* The symbol table entry, if any, that this was derived from. */
3955 struct ppc_link_hash_entry *h;
e054468f 3956 struct plt_entry *plt_ent;
721956f4 3957
6911b7dc
AM
3958 /* Symbol st_other. */
3959 unsigned char other;
721956f4
AM
3960};
3961
3962struct ppc_branch_hash_entry {
3963
3964 /* Base hash table entry structure. */
3965 struct bfd_hash_entry root;
3966
c456f082 3967 /* Offset within branch lookup table. */
721956f4
AM
3968 unsigned int offset;
3969
3970 /* Generation marker. */
3971 unsigned int iter;
3972};
65f38f15 3973
19e08130
AM
3974/* Used to track dynamic relocations for local symbols. */
3975struct ppc_dyn_relocs
3976{
3977 struct ppc_dyn_relocs *next;
3978
3979 /* The input section of the reloc. */
3980 asection *sec;
3981
3982 /* Total number of relocs copied for the input section. */
3983 unsigned int count : 31;
3984
3985 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3986 unsigned int ifunc : 1;
3987};
3988
65f38f15
AM
3989struct ppc_link_hash_entry
3990{
3991 struct elf_link_hash_entry elf;
3992
b3fac117
AM
3993 union {
3994 /* A pointer to the most recently used stub hash entry against this
3995 symbol. */
3996 struct ppc_stub_hash_entry *stub_cache;
3997
3998 /* A pointer to the next symbol starting with a '.' */
3999 struct ppc_link_hash_entry *next_dot_sym;
4000 } u;
721956f4 4001
65f38f15 4002 /* Track dynamic relocs copied for this symbol. */
6061a67d 4003 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4004
721956f4 4005 /* Link between function code and descriptor symbols. */
34814b9f 4006 struct ppc_link_hash_entry *oh;
721956f4 4007
e86ce104
AM
4008 /* Flag function code and descriptor symbols. */
4009 unsigned int is_func:1;
4010 unsigned int is_func_descriptor:1;
908b32fc 4011 unsigned int fake:1;
411e1bfb 4012
c5614fa4
AM
4013 /* Whether global opd/toc sym has been adjusted or not.
4014 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4015 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4016 unsigned int adjust_done:1;
4017
a4b6fadd
AM
4018 /* Set if this is an out-of-line register save/restore function,
4019 with non-standard calling convention. */
4020 unsigned int save_res:1;
4021
8b5f1ed8
AM
4022 /* Set if a duplicate symbol with non-zero localentry is detected,
4023 even when the duplicate symbol does not provide a definition. */
4024 unsigned int non_zero_localentry:1;
4025
411e1bfb 4026 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
4027 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4028 corresponding relocs are encountered during check_relocs.
4029 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4030 indicate the corresponding GOT entry type is not needed.
4031 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4032 a TPREL one. We use a separate flag rather than setting TPREL
4033 just for convenience in distinguishing the two cases. */
4034#define TLS_GD 1 /* GD reloc. */
4035#define TLS_LD 2 /* LD reloc. */
4036#define TLS_TPREL 4 /* TPREL reloc, => IE. */
4037#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4038#define TLS_TLS 16 /* Any TLS reloc. */
4039#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4040#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4041#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4042 unsigned char tls_mask;
65f38f15
AM
4043};
4044
4045/* ppc64 ELF linker hash table. */
4046
4047struct ppc_link_hash_table
4048{
4049 struct elf_link_hash_table elf;
4050
721956f4
AM
4051 /* The stub hash table. */
4052 struct bfd_hash_table stub_hash_table;
4053
4054 /* Another hash table for plt_branch stubs. */
4055 struct bfd_hash_table branch_hash_table;
4056
3b421ab3
AM
4057 /* Hash table for function prologue tocsave. */
4058 htab_t tocsave_htab;
4059
e7d1c40c
AM
4060 /* Various options and other info passed from the linker. */
4061 struct ppc64_elf_params *params;
721956f4 4062
6f20ed8a
AM
4063 /* The size of sec_info below. */
4064 unsigned int sec_info_arr_size;
4065
4066 /* Per-section array of extra section info. Done this way rather
4067 than as part of ppc64_elf_section_data so we have the info for
4068 non-ppc64 sections. */
4069 struct
4070 {
4071 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4072 bfd_vma toc_off;
6f20ed8a
AM
4073
4074 union
4075 {
4076 /* The section group that this section belongs to. */
4077 struct map_stub *group;
4078 /* A temp section list pointer. */
4079 asection *list;
4080 } u;
4081 } *sec_info;
721956f4 4082
a4b6fadd
AM
4083 /* Linked list of groups. */
4084 struct map_stub *group;
4085
ad8e1ba5
AM
4086 /* Temp used when calculating TOC pointers. */
4087 bfd_vma toc_curr;
bf102f86
AM
4088 bfd *toc_bfd;
4089 asection *toc_first_sec;
ad8e1ba5 4090
b3fac117
AM
4091 /* Used when adding symbols. */
4092 struct ppc_link_hash_entry *dot_syms;
4093
33e44f2e 4094 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4095 asection *glink;
9e390558 4096 asection *global_entry;
82bd7b59 4097 asection *sfpr;
4ce794b7
AM
4098 asection *brlt;
4099 asection *relbrlt;
58d180e8 4100 asection *glink_eh_frame;
ec338859 4101
8387904d
AM
4102 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4103 struct ppc_link_hash_entry *tls_get_addr;
4104 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4105
927be08e
AM
4106 /* The size of reliplt used by got entry relocs. */
4107 bfd_size_type got_reli_size;
4108
9b5ecbd0 4109 /* Statistics. */
7341d5e2 4110 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4111
ee75fd95
AM
4112 /* Number of stubs against global syms. */
4113 unsigned long stub_globals;
4114
ee67d69a
AM
4115 /* Set if we're linking code with function descriptors. */
4116 unsigned int opd_abi:1;
4117
4c52953f 4118 /* Support for multiple toc sections. */
33c0ec9d 4119 unsigned int do_multi_toc:1;
4c52953f 4120 unsigned int multi_toc_needed:1;
927be08e 4121 unsigned int second_toc_pass:1;
67f0cbdb 4122 unsigned int do_toc_opt:1;
4c52953f 4123
9a23f96e
AM
4124 /* Set if tls optimization is enabled. */
4125 unsigned int do_tls_opt:1;
4126
5d1634d7 4127 /* Set on error. */
99877b66 4128 unsigned int stub_error:1;
721956f4 4129
8c5b4e52
AM
4130 /* Whether func_desc_adjust needs to be run over symbols. */
4131 unsigned int need_func_desc_adj:1;
721956f4 4132
82e66161
AM
4133 /* Whether there exist local gnu indirect function resolvers,
4134 referenced by dynamic relocations. */
4135 unsigned int local_ifunc_resolver:1;
4136 unsigned int maybe_local_ifunc_resolver:1;
4137
f378ab09
AM
4138 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4139 unsigned int has_plt_localentry0:1;
4140
721956f4
AM
4141 /* Incremented every time we size stubs. */
4142 unsigned int stub_iteration;
5d1634d7 4143
87d72d41
AM
4144 /* Small local sym cache. */
4145 struct sym_cache sym_cache;
65f38f15
AM
4146};
4147
4c52953f
AM
4148/* Rename some of the generic section flags to better document how they
4149 are used here. */
b0dddeec
AM
4150
4151/* Nonzero if this section has TLS related relocations. */
4152#define has_tls_reloc sec_flg0
4153
4154/* Nonzero if this section has a call to __tls_get_addr. */
4155#define has_tls_get_addr_call sec_flg1
4156
4157/* Nonzero if this section has any toc or got relocs. */
4158#define has_toc_reloc sec_flg2
4159
4160/* Nonzero if this section has a call to another section that uses
4161 the toc or got. */
d77c8a4b 4162#define makes_toc_func_call sec_flg3
b0dddeec
AM
4163
4164/* Recursion protection when determining above flag. */
d77c8a4b 4165#define call_check_in_progress sec_flg4
70cc837d 4166#define call_check_done sec_flg5
4c52953f 4167
65f38f15
AM
4168/* Get the ppc64 ELF linker hash table from a link_info structure. */
4169
4170#define ppc_hash_table(p) \
4dfe6ac6
NC
4171 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4172 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4173
721956f4
AM
4174#define ppc_stub_hash_lookup(table, string, create, copy) \
4175 ((struct ppc_stub_hash_entry *) \
4176 bfd_hash_lookup ((table), (string), (create), (copy)))
4177
4178#define ppc_branch_hash_lookup(table, string, create, copy) \
4179 ((struct ppc_branch_hash_entry *) \
4180 bfd_hash_lookup ((table), (string), (create), (copy)))
4181
4182/* Create an entry in the stub hash table. */
4183
4184static struct bfd_hash_entry *
4ce794b7
AM
4185stub_hash_newfunc (struct bfd_hash_entry *entry,
4186 struct bfd_hash_table *table,
4187 const char *string)
721956f4
AM
4188{
4189 /* Allocate the structure if it has not already been allocated by a
4190 subclass. */
4191 if (entry == NULL)
4192 {
4193 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4194 if (entry == NULL)
4195 return entry;
4196 }
4197
4198 /* Call the allocation method of the superclass. */
4199 entry = bfd_hash_newfunc (entry, table, string);
4200 if (entry != NULL)
4201 {
4202 struct ppc_stub_hash_entry *eh;
4203
4204 /* Initialize the local fields. */
4205 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4206 eh->stub_type = ppc_stub_none;
6f20ed8a 4207 eh->group = NULL;
721956f4
AM
4208 eh->stub_offset = 0;
4209 eh->target_value = 0;
4210 eh->target_section = NULL;
721956f4 4211 eh->h = NULL;
6911b7dc 4212 eh->plt_ent = NULL;
6911b7dc 4213 eh->other = 0;
721956f4
AM
4214 }
4215
4216 return entry;
4217}
4218
4219/* Create an entry in the branch hash table. */
4220
4221static struct bfd_hash_entry *
4ce794b7
AM
4222branch_hash_newfunc (struct bfd_hash_entry *entry,
4223 struct bfd_hash_table *table,
4224 const char *string)
721956f4
AM
4225{
4226 /* Allocate the structure if it has not already been allocated by a
4227 subclass. */
4228 if (entry == NULL)
4229 {
4230 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4231 if (entry == NULL)
4232 return entry;
4233 }
4234
4235 /* Call the allocation method of the superclass. */
4236 entry = bfd_hash_newfunc (entry, table, string);
4237 if (entry != NULL)
4238 {
4239 struct ppc_branch_hash_entry *eh;
4240
4241 /* Initialize the local fields. */
4242 eh = (struct ppc_branch_hash_entry *) entry;
4243 eh->offset = 0;
4244 eh->iter = 0;
4245 }
4246
4247 return entry;
4248}
4249
65f38f15
AM
4250/* Create an entry in a ppc64 ELF linker hash table. */
4251
4252static struct bfd_hash_entry *
4ce794b7
AM
4253link_hash_newfunc (struct bfd_hash_entry *entry,
4254 struct bfd_hash_table *table,
4255 const char *string)
65f38f15
AM
4256{
4257 /* Allocate the structure if it has not already been allocated by a
4258 subclass. */
4259 if (entry == NULL)
4260 {
4261 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4262 if (entry == NULL)
4263 return entry;
4264 }
4265
4266 /* Call the allocation method of the superclass. */
4267 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4268 if (entry != NULL)
4269 {
4270 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4271
b3fac117 4272 memset (&eh->u.stub_cache, 0,
908b32fc 4273 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4274 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4275
4276 /* When making function calls, old ABI code references function entry
4277 points (dot symbols), while new ABI code references the function
4278 descriptor symbol. We need to make any combination of reference and
4279 definition work together, without breaking archive linking.
4280
4281 For a defined function "foo" and an undefined call to "bar":
4282 An old object defines "foo" and ".foo", references ".bar" (possibly
4283 "bar" too).
4284 A new object defines "foo" and references "bar".
4285
4286 A new object thus has no problem with its undefined symbols being
4287 satisfied by definitions in an old object. On the other hand, the
4288 old object won't have ".bar" satisfied by a new object.
4289
4290 Keep a list of newly added dot-symbols. */
4291
4292 if (string[0] == '.')
4293 {
4294 struct ppc_link_hash_table *htab;
4295
4296 htab = (struct ppc_link_hash_table *) table;
4297 eh->u.next_dot_sym = htab->dot_syms;
4298 htab->dot_syms = eh;
4299 }
65f38f15
AM
4300 }
4301
4302 return entry;
4303}
4304
3b421ab3
AM
4305struct tocsave_entry {
4306 asection *sec;
4307 bfd_vma offset;
4308};
4309
4310static hashval_t
4311tocsave_htab_hash (const void *p)
4312{
4313 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4314 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4315}
4316
4317static int
4318tocsave_htab_eq (const void *p1, const void *p2)
4319{
4320 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4321 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4322 return e1->sec == e2->sec && e1->offset == e2->offset;
4323}
4324
68faa637
AM
4325/* Destroy a ppc64 ELF linker hash table. */
4326
4327static void
d495ab0d 4328ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4329{
d495ab0d 4330 struct ppc_link_hash_table *htab;
68faa637 4331
d495ab0d 4332 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4333 if (htab->tocsave_htab)
4334 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4335 bfd_hash_table_free (&htab->branch_hash_table);
4336 bfd_hash_table_free (&htab->stub_hash_table);
4337 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4338}
4339
65f38f15
AM
4340/* Create a ppc64 ELF linker hash table. */
4341
4342static struct bfd_link_hash_table *
4ce794b7 4343ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4344{
4345 struct ppc_link_hash_table *htab;
4346 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4347
4ce794b7 4348 htab = bfd_zmalloc (amt);
65f38f15
AM
4349 if (htab == NULL)
4350 return NULL;
4351
66eb6687 4352 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4353 sizeof (struct ppc_link_hash_entry),
4354 PPC64_ELF_DATA))
65f38f15 4355 {
e2d34d7d 4356 free (htab);
65f38f15
AM
4357 return NULL;
4358 }
4359
721956f4 4360 /* Init the stub hash table too. */
66eb6687
AM
4361 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4362 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4363 {
d495ab0d 4364 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4365 return NULL;
4366 }
721956f4
AM
4367
4368 /* And the branch hash table. */
66eb6687
AM
4369 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4370 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4371 {
4372 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4373 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4374 return NULL;
4375 }
721956f4 4376
3b421ab3
AM
4377 htab->tocsave_htab = htab_try_create (1024,
4378 tocsave_htab_hash,
4379 tocsave_htab_eq,
4380 NULL);
4381 if (htab->tocsave_htab == NULL)
2915c55b 4382 {
d495ab0d 4383 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4384 return NULL;
4385 }
d495ab0d 4386 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4387
3254fd24
AM
4388 /* Initializing two fields of the union is just cosmetic. We really
4389 only care about glist, but when compiled on a 32-bit host the
4390 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4391 debugger inspection of these fields look nicer. */
a6aa5195
AM
4392 htab->elf.init_got_refcount.refcount = 0;
4393 htab->elf.init_got_refcount.glist = NULL;
4394 htab->elf.init_plt_refcount.refcount = 0;
4395 htab->elf.init_plt_refcount.glist = NULL;
4396 htab->elf.init_got_offset.offset = 0;
4397 htab->elf.init_got_offset.glist = NULL;
4398 htab->elf.init_plt_offset.offset = 0;
4399 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4400
65f38f15
AM
4401 return &htab->elf.root;
4402}
4403
bfeb4a28
AM
4404/* Create sections for linker generated code. */
4405
4406static bfd_boolean
4407create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4408{
4409 struct ppc_link_hash_table *htab;
4410 flagword flags;
4411
4412 htab = ppc_hash_table (info);
4413
bfeb4a28
AM
4414 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4415 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4416 if (htab->params->save_restore_funcs)
4417 {
4418 /* Create .sfpr for code to save and restore fp regs. */
4419 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4420 flags);
4421 if (htab->sfpr == NULL
4422 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4423 return FALSE;
4424 }
4425
4426 if (bfd_link_relocatable (info))
4427 return TRUE;
bfeb4a28
AM
4428
4429 /* Create .glink for lazy dynamic linking support. */
4430 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4431 flags);
4432 if (htab->glink == NULL
4433 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4434 return FALSE;
4435
9e390558
AM
4436 /* The part of .glink used by global entry stubs, separate so that
4437 it can be aligned appropriately without affecting htab->glink. */
4438 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4439 flags);
4440 if (htab->global_entry == NULL
4441 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4442 return FALSE;
4443
bfeb4a28
AM
4444 if (!info->no_ld_generated_unwind_info)
4445 {
4446 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4447 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4448 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4449 ".eh_frame",
4450 flags);
4451 if (htab->glink_eh_frame == NULL
4452 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4453 return FALSE;
4454 }
4455
4456 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4457 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4458 if (htab->elf.iplt == NULL
4459 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4460 return FALSE;
4461
4462 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4463 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4464 htab->elf.irelplt
4465 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4466 if (htab->elf.irelplt == NULL
4467 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4468 return FALSE;
4469
4470 /* Create branch lookup table for plt_branch stubs. */
4471 flags = (SEC_ALLOC | SEC_LOAD
4472 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4473 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4474 flags);
4475 if (htab->brlt == NULL
4476 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4477 return FALSE;
4478
0e1862bb 4479 if (!bfd_link_pic (info))
bfeb4a28
AM
4480 return TRUE;
4481
4482 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4483 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4484 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4485 ".rela.branch_lt",
4486 flags);
4487 if (htab->relbrlt == NULL
4488 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4489 return FALSE;
4490
4491 return TRUE;
4492}
4493
e717da7e
AM
4494/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4495
bfeb4a28 4496bfd_boolean
e7d1c40c
AM
4497ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4498 struct ppc64_elf_params *params)
e717da7e
AM
4499{
4500 struct ppc_link_hash_table *htab;
4501
e7d1c40c 4502 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4503
4504/* Always hook our dynamic sections into the first bfd, which is the
4505 linker created stub bfd. This ensures that the GOT header is at
4506 the start of the output TOC section. */
4507 htab = ppc_hash_table (info);
e7d1c40c
AM
4508 htab->elf.dynobj = params->stub_bfd;
4509 htab->params = params;
bfeb4a28 4510
bfeb4a28 4511 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4512}
4513
721956f4
AM
4514/* Build a name for an entry in the stub hash table. */
4515
4516static char *
4ce794b7
AM
4517ppc_stub_name (const asection *input_section,
4518 const asection *sym_sec,
4519 const struct ppc_link_hash_entry *h,
4520 const Elf_Internal_Rela *rel)
721956f4
AM
4521{
4522 char *stub_name;
bcaa2f82 4523 ssize_t len;
721956f4
AM
4524
4525 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4526 offsets from a sym as a branch target? In fact, we could
4527 probably assume the addend is always zero. */
4528 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4529
4530 if (h)
4531 {
4532 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4533 stub_name = bfd_malloc (len);
46de2a7c
AM
4534 if (stub_name == NULL)
4535 return stub_name;
4536
bcaa2f82
AM
4537 len = sprintf (stub_name, "%08x.%s+%x",
4538 input_section->id & 0xffffffff,
4539 h->elf.root.root.string,
4540 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4541 }
4542 else
4543 {
ad8e1ba5 4544 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4545 stub_name = bfd_malloc (len);
46de2a7c
AM
4546 if (stub_name == NULL)
4547 return stub_name;
4548
bcaa2f82
AM
4549 len = sprintf (stub_name, "%08x.%x:%x+%x",
4550 input_section->id & 0xffffffff,
4551 sym_sec->id & 0xffffffff,
4552 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4553 (int) rel->r_addend & 0xffffffff);
721956f4 4554 }
bcaa2f82 4555 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4556 stub_name[len - 2] = 0;
721956f4
AM
4557 return stub_name;
4558}
4559
4560/* Look up an entry in the stub hash. Stub entries are cached because
4561 creating the stub name takes a bit of time. */
4562
4563static struct ppc_stub_hash_entry *
4ce794b7
AM
4564ppc_get_stub_entry (const asection *input_section,
4565 const asection *sym_sec,
039b3fef 4566 struct ppc_link_hash_entry *h,
4ce794b7
AM
4567 const Elf_Internal_Rela *rel,
4568 struct ppc_link_hash_table *htab)
721956f4
AM
4569{
4570 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4571 struct map_stub *group;
721956f4
AM
4572
4573 /* If this input section is part of a group of sections sharing one
4574 stub section, then use the id of the first section in the group.
4575 Stub names need to include a section id, as there may well be
4576 more than one stub used to reach say, printf, and we need to
4577 distinguish between them. */
6f20ed8a 4578 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4579 if (group == NULL)
4580 return NULL;
721956f4 4581
b3fac117
AM
4582 if (h != NULL && h->u.stub_cache != NULL
4583 && h->u.stub_cache->h == h
6f20ed8a 4584 && h->u.stub_cache->group == group)
721956f4 4585 {
b3fac117 4586 stub_entry = h->u.stub_cache;
721956f4
AM
4587 }
4588 else
4589 {
4590 char *stub_name;
4591
6f20ed8a 4592 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4593 if (stub_name == NULL)
4594 return NULL;
4595
4596 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4597 stub_name, FALSE, FALSE);
721956f4 4598 if (h != NULL)
b3fac117 4599 h->u.stub_cache = stub_entry;
721956f4
AM
4600
4601 free (stub_name);
4602 }
4603
4604 return stub_entry;
4605}
4606
4607/* Add a new stub entry to the stub hash. Not all fields of the new
4608 stub entry are initialised. */
4609
4610static struct ppc_stub_hash_entry *
4ce794b7
AM
4611ppc_add_stub (const char *stub_name,
4612 asection *section,
25f53a85 4613 struct bfd_link_info *info)
721956f4 4614{
25f53a85 4615 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4616 struct map_stub *group;
721956f4
AM
4617 asection *link_sec;
4618 asection *stub_sec;
4619 struct ppc_stub_hash_entry *stub_entry;
4620
6f20ed8a
AM
4621 group = htab->sec_info[section->id].u.group;
4622 link_sec = group->link_sec;
4623 stub_sec = group->stub_sec;
721956f4
AM
4624 if (stub_sec == NULL)
4625 {
6f20ed8a
AM
4626 size_t namelen;
4627 bfd_size_type len;
4628 char *s_name;
721956f4 4629
6f20ed8a
AM
4630 namelen = strlen (link_sec->name);
4631 len = namelen + sizeof (STUB_SUFFIX);
4632 s_name = bfd_alloc (htab->params->stub_bfd, len);
4633 if (s_name == NULL)
4634 return NULL;
721956f4 4635
6f20ed8a
AM
4636 memcpy (s_name, link_sec->name, namelen);
4637 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4638 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4639 if (stub_sec == NULL)
4640 return NULL;
4641 group->stub_sec = stub_sec;
721956f4
AM
4642 }
4643
4644 /* Enter this entry into the linker stub hash table. */
4645 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4646 TRUE, FALSE);
721956f4
AM
4647 if (stub_entry == NULL)
4648 {
695344c0 4649 /* xgettext:c-format */
cf97bcb0
AM
4650 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4651 section->owner, stub_name);
721956f4
AM
4652 return NULL;
4653 }
4654
6f20ed8a 4655 stub_entry->group = group;
721956f4 4656 stub_entry->stub_offset = 0;
721956f4
AM
4657 return stub_entry;
4658}
4659
e717da7e
AM
4660/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4661 not already done. */
65f38f15 4662
b34976b6 4663static bfd_boolean
e717da7e 4664create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4665{
e717da7e
AM
4666 asection *got, *relgot;
4667 flagword flags;
4668 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4669
0c8d6e5c 4670 if (!is_ppc64_elf (abfd))
0ffa91dd 4671 return FALSE;
4dfe6ac6
NC
4672 if (htab == NULL)
4673 return FALSE;
0ffa91dd 4674
33e44f2e
AM
4675 if (!htab->elf.sgot
4676 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4677 return FALSE;
e717da7e
AM
4678
4679 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4680 | SEC_LINKER_CREATED);
4681
c456f082 4682 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4683 if (!got
e717da7e
AM
4684 || !bfd_set_section_alignment (abfd, got, 3))
4685 return FALSE;
65f38f15 4686
c456f082
AM
4687 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4688 flags | SEC_READONLY);
e717da7e 4689 if (!relgot
e717da7e 4690 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4691 return FALSE;
e717da7e
AM
4692
4693 ppc64_elf_tdata (abfd)->got = got;
4694 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4695 return TRUE;
65f38f15 4696}
5bd4f169 4697
b31867b6
AM
4698/* Follow indirect and warning symbol links. */
4699
4700static inline struct bfd_link_hash_entry *
4701follow_link (struct bfd_link_hash_entry *h)
4702{
4703 while (h->type == bfd_link_hash_indirect
4704 || h->type == bfd_link_hash_warning)
4705 h = h->u.i.link;
4706 return h;
4707}
4708
4709static inline struct elf_link_hash_entry *
4710elf_follow_link (struct elf_link_hash_entry *h)
4711{
4712 return (struct elf_link_hash_entry *) follow_link (&h->root);
4713}
4714
4715static inline struct ppc_link_hash_entry *
4716ppc_follow_link (struct ppc_link_hash_entry *h)
4717{
4718 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4719}
4720
40d16e0b
AM
4721/* Merge PLT info on FROM with that on TO. */
4722
4723static void
4724move_plt_plist (struct ppc_link_hash_entry *from,
4725 struct ppc_link_hash_entry *to)
4726{
4727 if (from->elf.plt.plist != NULL)
4728 {
4729 if (to->elf.plt.plist != NULL)
4730 {
4731 struct plt_entry **entp;
4732 struct plt_entry *ent;
4733
4734 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4735 {
4736 struct plt_entry *dent;
4737
4738 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4739 if (dent->addend == ent->addend)
4740 {
4741 dent->plt.refcount += ent->plt.refcount;
4742 *entp = ent->next;
4743 break;
4744 }
4745 if (dent == NULL)
4746 entp = &ent->next;
4747 }
4748 *entp = to->elf.plt.plist;
4749 }
4750
4751 to->elf.plt.plist = from->elf.plt.plist;
4752 from->elf.plt.plist = NULL;
4753 }
4754}
4755
65f38f15
AM
4756/* Copy the extra info we tack onto an elf_link_hash_entry. */
4757
4758static void
fcfa13d2
AM
4759ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4760 struct elf_link_hash_entry *dir,
4761 struct elf_link_hash_entry *ind)
65f38f15
AM
4762{
4763 struct ppc_link_hash_entry *edir, *eind;
4764
4765 edir = (struct ppc_link_hash_entry *) dir;
4766 eind = (struct ppc_link_hash_entry *) ind;
4767
c79d6685
AM
4768 edir->is_func |= eind->is_func;
4769 edir->is_func_descriptor |= eind->is_func_descriptor;
4770 edir->tls_mask |= eind->tls_mask;
4771 if (eind->oh != NULL)
4772 edir->oh = ppc_follow_link (eind->oh);
4773
474436e6 4774 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4775 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4776 edir->elf.ref_regular |= eind->elf.ref_regular;
4777 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4778 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4779 edir->elf.needs_plt |= eind->elf.needs_plt;
4780 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4781
d311bc8b
AM
4782 /* If we were called to copy over info for a weak sym, don't copy
4783 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4784 in order to simplify readonly_dynrelocs and save a field in the
4785 symbol hash entry, but that means dyn_relocs can't be used in any
4786 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4787 are then tested. */
d311bc8b 4788 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4789 return;
d311bc8b 4790
411e1bfb 4791 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4792 if (eind->dyn_relocs != NULL)
65f38f15 4793 {
bbd7ec4a
AM
4794 if (edir->dyn_relocs != NULL)
4795 {
6061a67d
AM
4796 struct elf_dyn_relocs **pp;
4797 struct elf_dyn_relocs *p;
bbd7ec4a 4798
fcfa13d2 4799 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4800 list. Merge any entries against the same section. */
4801 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4802 {
6061a67d 4803 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4804
4805 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4806 if (q->sec == p->sec)
4807 {
4808 q->pc_count += p->pc_count;
4809 q->count += p->count;
4810 *pp = p->next;
4811 break;
4812 }
4813 if (q == NULL)
4814 pp = &p->next;
4815 }
4816 *pp = edir->dyn_relocs;
4817 }
4818
65f38f15
AM
4819 edir->dyn_relocs = eind->dyn_relocs;
4820 eind->dyn_relocs = NULL;
4821 }
65f38f15 4822
81848ca0
AM
4823 /* Copy over got entries that we may have already seen to the
4824 symbol which just became indirect. */
411e1bfb
AM
4825 if (eind->elf.got.glist != NULL)
4826 {
4827 if (edir->elf.got.glist != NULL)
4828 {
4829 struct got_entry **entp;
4830 struct got_entry *ent;
4831
4832 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4833 {
4834 struct got_entry *dent;
4835
4836 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4837 if (dent->addend == ent->addend
e717da7e 4838 && dent->owner == ent->owner
411e1bfb
AM
4839 && dent->tls_type == ent->tls_type)
4840 {
4841 dent->got.refcount += ent->got.refcount;
4842 *entp = ent->next;
4843 break;
4844 }
4845 if (dent == NULL)
4846 entp = &ent->next;
4847 }
4848 *entp = edir->elf.got.glist;
4849 }
4850
4851 edir->elf.got.glist = eind->elf.got.glist;
4852 eind->elf.got.glist = NULL;
4853 }
4854
4855 /* And plt entries. */
40d16e0b 4856 move_plt_plist (eind, edir);
411e1bfb 4857
fcfa13d2 4858 if (eind->elf.dynindx != -1)
411e1bfb 4859 {
fcfa13d2
AM
4860 if (edir->elf.dynindx != -1)
4861 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4862 edir->elf.dynstr_index);
411e1bfb
AM
4863 edir->elf.dynindx = eind->elf.dynindx;
4864 edir->elf.dynstr_index = eind->elf.dynstr_index;
4865 eind->elf.dynindx = -1;
4866 eind->elf.dynstr_index = 0;
4867 }
411e1bfb
AM
4868}
4869
8387904d
AM
4870/* Find the function descriptor hash entry from the given function code
4871 hash entry FH. Link the entries via their OH fields. */
4872
4873static struct ppc_link_hash_entry *
b31867b6 4874lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4875{
4876 struct ppc_link_hash_entry *fdh = fh->oh;
4877
4878 if (fdh == NULL)
4879 {
4880 const char *fd_name = fh->elf.root.root.string + 1;
4881
4882 fdh = (struct ppc_link_hash_entry *)
4883 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4884 if (fdh == NULL)
4885 return fdh;
4886
4887 fdh->is_func_descriptor = 1;
4888 fdh->oh = fh;
4889 fh->is_func = 1;
4890 fh->oh = fdh;
8387904d
AM
4891 }
4892
8c5b4e52
AM
4893 fdh = ppc_follow_link (fdh);
4894 fdh->is_func_descriptor = 1;
4895 fdh->oh = fh;
4896 return fdh;
8387904d
AM
4897}
4898
8c5b4e52 4899/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4900
4901static struct ppc_link_hash_entry *
4902make_fdh (struct bfd_link_info *info,
908b32fc 4903 struct ppc_link_hash_entry *fh)
bb700d78 4904{
8c5b4e52
AM
4905 bfd *abfd = fh->elf.root.u.undef.abfd;
4906 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4907 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4908 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4909 ? BSF_WEAK
4910 : BSF_GLOBAL);
4911
4912 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4913 fh->elf.root.root.string + 1,
4914 flags, bfd_und_section_ptr, 0,
4915 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4916 return NULL;
4917
4918 fdh = (struct ppc_link_hash_entry *) bh;
4919 fdh->elf.non_elf = 0;
908b32fc
AM
4920 fdh->fake = 1;
4921 fdh->is_func_descriptor = 1;
4922 fdh->oh = fh;
4923 fh->is_func = 1;
4924 fh->oh = fdh;
bb700d78
AM
4925 return fdh;
4926}
4927
8387904d
AM
4928/* Fix function descriptor symbols defined in .opd sections to be
4929 function type. */
555cd476
AM
4930
4931static bfd_boolean
c16153ae 4932ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4933 struct bfd_link_info *info,
555cd476 4934 Elf_Internal_Sym *isym,
6911b7dc 4935 const char **name,
555cd476
AM
4936 flagword *flags ATTRIBUTE_UNUSED,
4937 asection **sec,
b53dfeb2 4938 bfd_vma *value)
555cd476 4939{
a43942db 4940 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4941 && (ibfd->flags & DYNAMIC) == 0
4942 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4943 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4944
b53dfeb2 4945 if (*sec != NULL
f1885d1e 4946 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4947 {
4948 asection *code_sec;
4949
4950 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4951 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4952 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4953
4954 /* If the symbol is a function defined in .opd, and the function
4955 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4956 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4957 && (*sec)->reloc_count != 0
4958 && opd_entry_value (*sec, *value, &code_sec, NULL,
4959 FALSE) != (bfd_vma) -1
4960 && discarded_section (code_sec))
4961 {
4962 *sec = bfd_und_section_ptr;
4963 isym->st_shndx = SHN_UNDEF;
4964 }
4965 }
dbd1e97e
AM
4966 else if (*sec != NULL
4967 && strcmp ((*sec)->name, ".toc") == 0
4968 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4969 {
4970 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4971 if (htab != NULL)
4972 htab->params->object_in_toc = 1;
4973 }
433817dd 4974
6911b7dc
AM
4975 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4976 {
4977 if (abiversion (ibfd) == 0)
4978 set_abiversion (ibfd, 2);
4979 else if (abiversion (ibfd) == 1)
4980 {
cf97bcb0
AM
4981 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4982 " for ABI version 1"), *name);
6911b7dc
AM
4983 bfd_set_error (bfd_error_bad_value);
4984 return FALSE;
4985 }
4986 }
4987
555cd476
AM
4988 return TRUE;
4989}
4990
6911b7dc
AM
4991/* Merge non-visibility st_other attributes: local entry point. */
4992
4993static void
4994ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4995 const Elf_Internal_Sym *isym,
4996 bfd_boolean definition,
4997 bfd_boolean dynamic)
4998{
f378ab09 4999 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5000 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5001 | ELF_ST_VISIBILITY (h->other));
5002}
5003
8c5b4e52
AM
5004/* Hook called on merging a symbol. We use this to clear "fake" since
5005 we now have a real symbol. */
5006
5007static bfd_boolean
5008ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5009 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5010 asection **psec ATTRIBUTE_UNUSED,
5011 bfd_boolean newdef ATTRIBUTE_UNUSED,
5012 bfd_boolean olddef ATTRIBUTE_UNUSED,
5013 bfd *oldbfd ATTRIBUTE_UNUSED,
5014 const asection *oldsec ATTRIBUTE_UNUSED)
5015{
5016 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5017 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5018 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5019 return TRUE;
5020}
5021
8387904d 5022/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5023 inclusion of a new ABI object archive that defines "bar".
5024 NAME is a symbol defined in an archive. Return a symbol in the hash
5025 table that might be satisfied by the archive symbols. */
8387904d
AM
5026
5027static struct elf_link_hash_entry *
5028ppc64_elf_archive_symbol_lookup (bfd *abfd,
5029 struct bfd_link_info *info,
5030 const char *name)
5031{
5032 struct elf_link_hash_entry *h;
5033 char *dot_name;
5034 size_t len;
5035
5036 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5037 if (h != NULL
5038 /* Don't return this sym if it is a fake function descriptor
5039 created by add_symbol_adjust. */
8c5b4e52 5040 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5041 return h;
5042
5043 if (name[0] == '.')
5044 return h;
5045
5046 len = strlen (name);
5047 dot_name = bfd_alloc (abfd, len + 2);
5048 if (dot_name == NULL)
e99955cd 5049 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5050 dot_name[0] = '.';
5051 memcpy (dot_name + 1, name, len + 1);
5052 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5053 bfd_release (abfd, dot_name);
5054 return h;
5055}
5056
5057/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5058 new ABI object defines "bar". Well, at least, undefined dot symbols
5059 are made weak. This stops later archive searches from including an
5060 object if we already have a function descriptor definition. It also
35b0ce59
AM
5061 prevents the linker complaining about undefined symbols.
5062 We also check and correct mismatched symbol visibility here. The
5063 most restrictive visibility of the function descriptor and the
5064 function entry symbol is used. */
8387904d
AM
5065
5066static bfd_boolean
b3fac117 5067add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5068{
8387904d
AM
5069 struct ppc_link_hash_table *htab;
5070 struct ppc_link_hash_entry *fdh;
5071
b3fac117
AM
5072 if (eh->elf.root.type == bfd_link_hash_warning)
5073 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5074
8400d40d
AM
5075 if (eh->elf.root.type == bfd_link_hash_indirect)
5076 return TRUE;
5077
b3fac117
AM
5078 if (eh->elf.root.root.string[0] != '.')
5079 abort ();
8387904d 5080
b3fac117 5081 htab = ppc_hash_table (info);
4dfe6ac6
NC
5082 if (htab == NULL)
5083 return FALSE;
5084
b31867b6 5085 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5086 if (fdh == NULL
5087 && !bfd_link_relocatable (info)
5088 && (eh->elf.root.type == bfd_link_hash_undefined
5089 || eh->elf.root.type == bfd_link_hash_undefweak)
5090 && eh->elf.ref_regular)
5091 {
5092 /* Make an undefined function descriptor sym, in order to
5093 pull in an --as-needed shared lib. Archives are handled
5094 elsewhere. */
5095 fdh = make_fdh (info, eh);
5096 if (fdh == NULL)
5097 return FALSE;
bb700d78 5098 }
8c5b4e52
AM
5099
5100 if (fdh != NULL)
8387904d 5101 {
35b0ce59
AM
5102 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5103 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5104
5105 /* Make both descriptor and entry symbol have the most
5106 constraining visibility of either symbol. */
35b0ce59
AM
5107 if (entry_vis < descr_vis)
5108 fdh->elf.other += entry_vis - descr_vis;
5109 else if (entry_vis > descr_vis)
5110 eh->elf.other += descr_vis - entry_vis;
5111
8c5b4e52
AM
5112 /* Propagate reference flags from entry symbol to function
5113 descriptor symbol. */
bc4e12de 5114 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5115 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5116 fdh->elf.ref_regular |= eh->elf.ref_regular;
5117 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5118
5119 if (!fdh->elf.forced_local
5120 && fdh->elf.dynindx == -1
5121 && fdh->elf.versioned != versioned_hidden
5122 && (bfd_link_dll (info)
5123 || fdh->elf.def_dynamic
5124 || fdh->elf.ref_dynamic)
5125 && (eh->elf.ref_regular
5126 || eh->elf.def_regular))
5127 {
5128 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5129 return FALSE;
35b0ce59 5130 }
8387904d 5131 }
99877b66 5132
8387904d
AM
5133 return TRUE;
5134}
5135
f6c7c3e8
AM
5136/* Set up opd section info and abiversion for IBFD, and process list
5137 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5138
8387904d 5139static bfd_boolean
f6c7c3e8 5140ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5141{
99877b66 5142 struct ppc_link_hash_table *htab;
b3fac117 5143 struct ppc_link_hash_entry **p, *eh;
459609d6 5144 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5145
459609d6 5146 if (opd != NULL && opd->size != 0)
b3fac117 5147 {
b9399fcf
AM
5148 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5149 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5150
459609d6
AM
5151 if (abiversion (ibfd) == 0)
5152 set_abiversion (ibfd, 1);
8a2058b5 5153 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5154 {
695344c0 5155 /* xgettext:c-format */
cf97bcb0
AM
5156 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5157 ibfd, abiversion (ibfd));
459609d6
AM
5158 bfd_set_error (bfd_error_bad_value);
5159 return FALSE;
f6c7c3e8 5160 }
b9399fcf 5161 }
f6c7c3e8 5162
b9399fcf
AM
5163 if (is_ppc64_elf (info->output_bfd))
5164 {
5165 /* For input files without an explicit abiversion in e_flags
5166 we should have flagged any with symbol st_other bits set
5167 as ELFv1 and above flagged those with .opd as ELFv2.
5168 Set the output abiversion if not yet set, and for any input
5169 still ambiguous, take its abiversion from the output.
5170 Differences in ABI are reported later. */
5171 if (abiversion (info->output_bfd) == 0)
5172 set_abiversion (info->output_bfd, abiversion (ibfd));
5173 else if (abiversion (ibfd) == 0)
5174 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5175 }
5176
459609d6
AM
5177 htab = ppc_hash_table (info);
5178 if (htab == NULL)
b9399fcf 5179 return TRUE;
459609d6 5180
b9399fcf
AM
5181 if (opd != NULL && opd->size != 0
5182 && (ibfd->flags & DYNAMIC) == 0
5183 && (opd->flags & SEC_RELOC) != 0
5184 && opd->reloc_count != 0
5185 && !bfd_is_abs_section (opd->output_section)
5186 && info->gc_sections)
5187 {
5188 /* Garbage collection needs some extra help with .opd sections.
5189 We don't want to necessarily keep everything referenced by
5190 relocs in .opd, as that would keep all functions. Instead,
5191 if we reference an .opd symbol (a function descriptor), we
5192 want to keep the function code symbol's section. This is
5193 easy for global symbols, but for local syms we need to keep
5194 information about the associated function section. */
5195 bfd_size_type amt;
5196 asection **opd_sym_map;
5197 Elf_Internal_Shdr *symtab_hdr;
5198 Elf_Internal_Rela *relocs, *rel_end, *rel;
5199
5200 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5201 opd_sym_map = bfd_zalloc (ibfd, amt);
5202 if (opd_sym_map == NULL)
5203 return FALSE;
5204 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5205 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5206 info->keep_memory);
5207 if (relocs == NULL)
5208 return FALSE;
5209 symtab_hdr = &elf_symtab_hdr (ibfd);
5210 rel_end = relocs + opd->reloc_count - 1;
5211 for (rel = relocs; rel < rel_end; rel++)
5212 {
5213 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5214 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5215
5216 if (r_type == R_PPC64_ADDR64
5217 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5218 && r_symndx < symtab_hdr->sh_info)
5219 {
5220 Elf_Internal_Sym *isym;
5221 asection *s;
5222
5223 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5224 if (isym == NULL)
5225 {
5226 if (elf_section_data (opd)->relocs != relocs)
5227 free (relocs);
5228 return FALSE;
5229 }
5230
5231 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5232 if (s != NULL && s != opd)
5233 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5234 }
5235 }
5236 if (elf_section_data (opd)->relocs != relocs)
5237 free (relocs);
5238 }
459609d6
AM
5239
5240 p = &htab->dot_syms;
5241 while ((eh = *p) != NULL)
5242 {
5243 *p = NULL;
5244 if (&eh->elf == htab->elf.hgot)
5245 ;
5246 else if (htab->elf.hgot == NULL
5247 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5248 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5249 else if (abiversion (ibfd) <= 1)
5250 {
5251 htab->need_func_desc_adj = 1;
5252 if (!add_symbol_adjust (eh, info))
5253 return FALSE;
5254 }
459609d6
AM
5255 p = &eh->u.next_dot_sym;
5256 }
b3fac117 5257 return TRUE;
8387904d
AM
5258}
5259
97fed1c9
JJ
5260/* Undo hash table changes when an --as-needed input file is determined
5261 not to be needed. */
5262
5263static bfd_boolean
e5034e59
AM
5264ppc64_elf_notice_as_needed (bfd *ibfd,
5265 struct bfd_link_info *info,
5266 enum notice_asneeded_action act)
97fed1c9 5267{
e5034e59
AM
5268 if (act == notice_not_needed)
5269 {
5270 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5271
e5034e59
AM
5272 if (htab == NULL)
5273 return FALSE;
4dfe6ac6 5274
e5034e59
AM
5275 htab->dot_syms = NULL;
5276 }
5277 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5278}
5279
aa374f67
AM
5280/* If --just-symbols against a final linked binary, then assume we need
5281 toc adjusting stubs when calling functions defined there. */
5282
5283static void
5284ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5285{
5286 if ((sec->flags & SEC_CODE) != 0
5287 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5288 && is_ppc64_elf (sec->owner))
5289 {
2c3f079f
AM
5290 if (abiversion (sec->owner) >= 2
5291 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5292 sec->has_toc_reloc = 1;
5293 }
5294 _bfd_elf_link_just_syms (sec, info);
5295}
5296
e054468f 5297static struct plt_entry **
4ce794b7
AM
5298update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5299 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5300{
5301 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5302 struct plt_entry **local_plt;
f961d9dd 5303 unsigned char *local_got_tls_masks;
411e1bfb
AM
5304
5305 if (local_got_ents == NULL)
5306 {
5307 bfd_size_type size = symtab_hdr->sh_info;
5308
e054468f
AM
5309 size *= (sizeof (*local_got_ents)
5310 + sizeof (*local_plt)
5311 + sizeof (*local_got_tls_masks));
4ce794b7 5312 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5313 if (local_got_ents == NULL)
e054468f 5314 return NULL;
411e1bfb
AM
5315 elf_local_got_ents (abfd) = local_got_ents;
5316 }
5317
e054468f 5318 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5319 {
5320 struct got_entry *ent;
5321
5322 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5323 if (ent->addend == r_addend
5324 && ent->owner == abfd
5325 && ent->tls_type == tls_type)
411e1bfb
AM
5326 break;
5327 if (ent == NULL)
5328 {
5329 bfd_size_type amt = sizeof (*ent);
4ce794b7 5330 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5331 if (ent == NULL)
5332 return FALSE;
5333 ent->next = local_got_ents[r_symndx];
5334 ent->addend = r_addend;
e717da7e 5335 ent->owner = abfd;
411e1bfb 5336 ent->tls_type = tls_type;
927be08e 5337 ent->is_indirect = FALSE;
411e1bfb
AM
5338 ent->got.refcount = 0;
5339 local_got_ents[r_symndx] = ent;
5340 }
5341 ent->got.refcount += 1;
5342 }
5343
e054468f 5344 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5345 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5346 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5347
5348 return local_plt + r_symndx;
65f38f15
AM
5349}
5350
411e1bfb 5351static bfd_boolean
e054468f 5352update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5353{
411e1bfb 5354 struct plt_entry *ent;
1e2f5b6e 5355
e054468f 5356 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5357 if (ent->addend == addend)
5358 break;
5359 if (ent == NULL)
1e2f5b6e 5360 {
411e1bfb 5361 bfd_size_type amt = sizeof (*ent);
4ce794b7 5362 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5363 if (ent == NULL)
5364 return FALSE;
e054468f 5365 ent->next = *plist;
411e1bfb
AM
5366 ent->addend = addend;
5367 ent->plt.refcount = 0;
e054468f 5368 *plist = ent;
1e2f5b6e 5369 }
411e1bfb 5370 ent->plt.refcount += 1;
b34976b6 5371 return TRUE;
1e2f5b6e
AM
5372}
5373
e054468f
AM
5374static bfd_boolean
5375is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5376{
5377 return (r_type == R_PPC64_REL24
5378 || r_type == R_PPC64_REL14
5379 || r_type == R_PPC64_REL14_BRTAKEN
5380 || r_type == R_PPC64_REL14_BRNTAKEN
5381 || r_type == R_PPC64_ADDR24
5382 || r_type == R_PPC64_ADDR14
5383 || r_type == R_PPC64_ADDR14_BRTAKEN
5384 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5385}
5386
5bd4f169 5387/* Look through the relocs for a section during the first phase, and
65f38f15 5388 calculate needed space in the global offset table, procedure
5d1634d7 5389 linkage table, and dynamic reloc sections. */
5bd4f169 5390
b34976b6 5391static bfd_boolean
4ce794b7
AM
5392ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5393 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5394{
65f38f15 5395 struct ppc_link_hash_table *htab;
5bd4f169 5396 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5397 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5398 const Elf_Internal_Rela *rel;
5399 const Elf_Internal_Rela *rel_end;
5bd4f169 5400 asection *sreloc;
3a71aa26 5401 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5402 bfd_boolean is_opd;
5bd4f169 5403
0e1862bb 5404 if (bfd_link_relocatable (info))
b34976b6 5405 return TRUE;
5bd4f169 5406
680a3378
AM
5407 /* Don't do anything special with non-loaded, non-alloced sections.
5408 In particular, any relocs in such sections should not affect GOT
5409 and PLT reference counting (ie. we don't allow them to create GOT
5410 or PLT entries), there's no possibility or desire to optimize TLS
5411 relocs, and there's not much point in propagating relocs to shared
5412 libs that the dynamic linker won't relocate. */
5413 if ((sec->flags & SEC_ALLOC) == 0)
5414 return TRUE;
5415
0c8d6e5c 5416 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5417
65f38f15 5418 htab = ppc_hash_table (info);
4dfe6ac6
NC
5419 if (htab == NULL)
5420 return FALSE;
5421
3a71aa26
AM
5422 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5423 FALSE, FALSE, TRUE);
5424 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5425 FALSE, FALSE, TRUE);
0ffa91dd 5426 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5427 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5428 sreloc = NULL;
b9399fcf 5429 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5430 rel_end = relocs + sec->reloc_count;
5431 for (rel = relocs; rel < rel_end; rel++)
5432 {
5433 unsigned long r_symndx;
5434 struct elf_link_hash_entry *h;
04c9666a 5435 enum elf_ppc64_reloc_type r_type;
727fc41e 5436 int tls_type;
7c8fe5c4 5437 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5438 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5439
5440 r_symndx = ELF64_R_SYM (rel->r_info);
5441 if (r_symndx < symtab_hdr->sh_info)
5442 h = NULL;
5443 else
973a3492
L
5444 {
5445 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5446 h = elf_follow_link (h);
1c865ab2
AM
5447
5448 if (h == htab->elf.hgot)
5449 sec->has_toc_reloc = 1;
973a3492 5450 }
5bd4f169 5451
727fc41e 5452 tls_type = 0;
e054468f 5453 ifunc = NULL;
25f23106
AM
5454 if (h != NULL)
5455 {
5456 if (h->type == STT_GNU_IFUNC)
5457 {
5458 h->needs_plt = 1;
5459 ifunc = &h->plt.plist;
5460 }
5461 }
5462 else
5463 {
5464 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5465 abfd, r_symndx);
5466 if (isym == NULL)
5467 return FALSE;
5468
5469 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5470 {
5471 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5472 rel->r_addend, PLT_IFUNC);
5473 if (ifunc == NULL)
5474 return FALSE;
5475 }
5476 }
727fc41e 5477
cbf95972 5478 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5479 switch (r_type)
5bd4f169 5480 {
727fc41e
AM
5481 case R_PPC64_TLSGD:
5482 case R_PPC64_TLSLD:
5483 /* These special tls relocs tie a call to __tls_get_addr with
5484 its parameter symbol. */
5485 break;
5486
411e1bfb
AM
5487 case R_PPC64_GOT_TLSLD16:
5488 case R_PPC64_GOT_TLSLD16_LO:
5489 case R_PPC64_GOT_TLSLD16_HI:
5490 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5491 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5492 goto dogottls;
5493
5494 case R_PPC64_GOT_TLSGD16:
5495 case R_PPC64_GOT_TLSGD16_LO:
5496 case R_PPC64_GOT_TLSGD16_HI:
5497 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5498 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5499 goto dogottls;
5500
5501 case R_PPC64_GOT_TPREL16_DS:
5502 case R_PPC64_GOT_TPREL16_LO_DS:
5503 case R_PPC64_GOT_TPREL16_HI:
5504 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5505 if (bfd_link_dll (info))
411e1bfb
AM
5506 info->flags |= DF_STATIC_TLS;
5507 tls_type = TLS_TLS | TLS_TPREL;
5508 goto dogottls;
5509
5510 case R_PPC64_GOT_DTPREL16_DS:
5511 case R_PPC64_GOT_DTPREL16_LO_DS:
5512 case R_PPC64_GOT_DTPREL16_HI:
5513 case R_PPC64_GOT_DTPREL16_HA:
5514 tls_type = TLS_TLS | TLS_DTPREL;
5515 dogottls:
5516 sec->has_tls_reloc = 1;
1a0670f3 5517 /* Fall through */
411e1bfb 5518
5bd4f169 5519 case R_PPC64_GOT16:
5bd4f169 5520 case R_PPC64_GOT16_DS:
65f38f15
AM
5521 case R_PPC64_GOT16_HA:
5522 case R_PPC64_GOT16_HI:
5523 case R_PPC64_GOT16_LO:
5bd4f169 5524 case R_PPC64_GOT16_LO_DS:
65f38f15 5525 /* This symbol requires a global offset table entry. */
4c52953f 5526 sec->has_toc_reloc = 1;
33c0ec9d
AM
5527 if (r_type == R_PPC64_GOT_TLSLD16
5528 || r_type == R_PPC64_GOT_TLSGD16
5529 || r_type == R_PPC64_GOT_TPREL16_DS
5530 || r_type == R_PPC64_GOT_DTPREL16_DS
5531 || r_type == R_PPC64_GOT16
5532 || r_type == R_PPC64_GOT16_DS)
5533 {
5534 htab->do_multi_toc = 1;
d77c8a4b 5535 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5536 }
5537
e717da7e
AM
5538 if (ppc64_elf_tdata (abfd)->got == NULL
5539 && !create_got_section (abfd, info))
b34976b6 5540 return FALSE;
5bd4f169
AM
5541
5542 if (h != NULL)
5543 {
411e1bfb
AM
5544 struct ppc_link_hash_entry *eh;
5545 struct got_entry *ent;
65f38f15 5546
411e1bfb
AM
5547 eh = (struct ppc_link_hash_entry *) h;
5548 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5549 if (ent->addend == rel->r_addend
e717da7e 5550 && ent->owner == abfd
411e1bfb
AM
5551 && ent->tls_type == tls_type)
5552 break;
5553 if (ent == NULL)
5bd4f169 5554 {
411e1bfb 5555 bfd_size_type amt = sizeof (*ent);
4ce794b7 5556 ent = bfd_alloc (abfd, amt);
411e1bfb 5557 if (ent == NULL)
b34976b6 5558 return FALSE;
411e1bfb
AM
5559 ent->next = eh->elf.got.glist;
5560 ent->addend = rel->r_addend;
e717da7e 5561 ent->owner = abfd;
411e1bfb 5562 ent->tls_type = tls_type;
927be08e 5563 ent->is_indirect = FALSE;
411e1bfb
AM
5564 ent->got.refcount = 0;
5565 eh->elf.got.glist = ent;
5bd4f169 5566 }
411e1bfb 5567 ent->got.refcount += 1;
e7b938ca 5568 eh->tls_mask |= tls_type;
5bd4f169 5569 }
411e1bfb
AM
5570 else
5571 /* This is a global offset table entry for a local symbol. */
5572 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5573 rel->r_addend, tls_type))
5574 return FALSE;
a345bc8d
AM
5575
5576 /* We may also need a plt entry if the symbol turns out to be
5577 an ifunc. */
0e1862bb 5578 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5579 {
5580 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5581 return FALSE;
5582 }
5bd4f169
AM
5583 break;
5584
5bd4f169 5585 case R_PPC64_PLT16_HA:
65f38f15
AM
5586 case R_PPC64_PLT16_HI:
5587 case R_PPC64_PLT16_LO:
5588 case R_PPC64_PLT32:
5589 case R_PPC64_PLT64:
cbf95972
AM
5590 /* This symbol requires a procedure linkage table entry. */
5591 plt_list = ifunc;
5592 if (h != NULL)
e054468f 5593 {
e054468f
AM
5594 h->needs_plt = 1;
5595 if (h->root.root.string[0] == '.'
5596 && h->root.root.string[1] != '\0')
5597 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5598 plt_list = &h->plt.plist;
5599 }
5600 if (plt_list == NULL)
5601 {
5602 /* It does not make sense to have a procedure linkage
5603 table entry for a non-ifunc local symbol. */
5604 info->callbacks->einfo
695344c0 5605 /* xgettext:c-format */
174d0a74 5606 (_("%H: %s reloc against local symbol\n"),
cbf95972
AM
5607 abfd, sec, rel->r_offset,
5608 ppc64_elf_howto_table[r_type]->name);
5609 bfd_set_error (bfd_error_bad_value);
5610 return FALSE;
e054468f 5611 }
cbf95972
AM
5612 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5613 return FALSE;
5bd4f169
AM
5614 break;
5615
5616 /* The following relocations don't need to propagate the
5617 relocation if linking a shared object since they are
5618 section relative. */
5619 case R_PPC64_SECTOFF:
5620 case R_PPC64_SECTOFF_LO:
5621 case R_PPC64_SECTOFF_HI:
5622 case R_PPC64_SECTOFF_HA:
5623 case R_PPC64_SECTOFF_DS:
5624 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5625 case R_PPC64_DTPREL16:
5626 case R_PPC64_DTPREL16_LO:
5627 case R_PPC64_DTPREL16_HI:
5628 case R_PPC64_DTPREL16_HA:
5629 case R_PPC64_DTPREL16_DS:
5630 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5631 case R_PPC64_DTPREL16_HIGH:
5632 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5633 case R_PPC64_DTPREL16_HIGHER:
5634 case R_PPC64_DTPREL16_HIGHERA:
5635 case R_PPC64_DTPREL16_HIGHEST:
5636 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5637 break;
5638
ad8e1ba5 5639 /* Nor do these. */
25f23106
AM
5640 case R_PPC64_REL16:
5641 case R_PPC64_REL16_LO:
5642 case R_PPC64_REL16_HI:
5643 case R_PPC64_REL16_HA:
a680de9a 5644 case R_PPC64_REL16DX_HA:
25f23106
AM
5645 break;
5646
45965137
AM
5647 /* Not supported as a dynamic relocation. */
5648 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5649 if (bfd_link_pic (info))
45965137
AM
5650 {
5651 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5652 ppc_howto_init ();
695344c0 5653 /* xgettext:c-format */
174d0a74 5654 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5655 "in shared libraries and PIEs\n"),
45965137
AM
5656 abfd, sec, rel->r_offset,
5657 ppc64_elf_howto_table[r_type]->name);
5658 bfd_set_error (bfd_error_bad_value);
5659 return FALSE;
5660 }
5661 break;
5662
ad8e1ba5 5663 case R_PPC64_TOC16:
33c0ec9d
AM
5664 case R_PPC64_TOC16_DS:
5665 htab->do_multi_toc = 1;
d77c8a4b 5666 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5667 /* Fall through. */
ad8e1ba5
AM
5668 case R_PPC64_TOC16_LO:
5669 case R_PPC64_TOC16_HI:
5670 case R_PPC64_TOC16_HA:
ad8e1ba5 5671 case R_PPC64_TOC16_LO_DS:
4c52953f 5672 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5673 break;
5674
006589cf
AM
5675 /* Marker reloc. */
5676 case R_PPC64_ENTRY:
5677 break;
5678
5bd4f169
AM
5679 /* This relocation describes the C++ object vtable hierarchy.
5680 Reconstruct it for later use during GC. */
5681 case R_PPC64_GNU_VTINHERIT:
c152c796 5682 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5683 return FALSE;
5bd4f169
AM
5684 break;
5685
5686 /* This relocation describes which C++ vtable entries are actually
5687 used. Record for later use during GC. */
5688 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5689 BFD_ASSERT (h != NULL);
5690 if (h != NULL
5691 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5692 return FALSE;
5bd4f169
AM
5693 break;
5694
721956f4
AM
5695 case R_PPC64_REL14:
5696 case R_PPC64_REL14_BRTAKEN:
5697 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5698 {
5699 asection *dest = NULL;
5700
5701 /* Heuristic: If jumping outside our section, chances are
5702 we are going to need a stub. */
5703 if (h != NULL)
5704 {
5705 /* If the sym is weak it may be overridden later, so
5706 don't assume we know where a weak sym lives. */
5707 if (h->root.type == bfd_link_hash_defined)
5708 dest = h->root.u.def.section;
5709 }
5710 else
87d72d41
AM
5711 {
5712 Elf_Internal_Sym *isym;
5713
5714 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5715 abfd, r_symndx);
5716 if (isym == NULL)
5717 return FALSE;
5718
5719 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5720 }
5721
220c76dd 5722 if (dest != sec)
7c8fe5c4 5723 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5724 }
721956f4
AM
5725 /* Fall through. */
5726
5d1634d7 5727 case R_PPC64_REL24:
cbf95972
AM
5728 plt_list = ifunc;
5729 if (h != NULL)
5d1634d7 5730 {
e054468f
AM
5731 h->needs_plt = 1;
5732 if (h->root.root.string[0] == '.'
5733 && h->root.root.string[1] != '\0')
5734 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5735
3a71aa26 5736 if (h == tga || h == dottga)
cbf95972
AM
5737 {
5738 sec->has_tls_reloc = 1;
5739 if (rel != relocs
5740 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5741 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5742 /* We have a new-style __tls_get_addr call with
5743 a marker reloc. */
5744 ;
5745 else
5746 /* Mark this section as having an old-style call. */
5747 sec->has_tls_get_addr_call = 1;
5748 }
5749 plt_list = &h->plt.plist;
411e1bfb 5750 }
cbf95972
AM
5751
5752 /* We may need a .plt entry if the function this reloc
5753 refers to is in a shared lib. */
5754 if (plt_list
5755 && !update_plt_info (abfd, plt_list, rel->r_addend))
5756 return FALSE;
411e1bfb
AM
5757 break;
5758
cbf95972
AM
5759 case R_PPC64_ADDR14:
5760 case R_PPC64_ADDR14_BRNTAKEN:
5761 case R_PPC64_ADDR14_BRTAKEN:
5762 case R_PPC64_ADDR24:
5763 goto dodyn;
5764
411e1bfb
AM
5765 case R_PPC64_TPREL64:
5766 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5767 if (bfd_link_dll (info))
411e1bfb
AM
5768 info->flags |= DF_STATIC_TLS;
5769 goto dotlstoc;
5770
5771 case R_PPC64_DTPMOD64:
5772 if (rel + 1 < rel_end
5773 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5774 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5775 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5776 else
951fd09b 5777 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5778 goto dotlstoc;
5779
5780 case R_PPC64_DTPREL64:
5781 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5782 if (rel != relocs
5783 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5784 && rel[-1].r_offset == rel->r_offset - 8)
5785 /* This is the second reloc of a dtpmod, dtprel pair.
5786 Don't mark with TLS_DTPREL. */
5787 goto dodyn;
5788
5789 dotlstoc:
5790 sec->has_tls_reloc = 1;
5791 if (h != NULL)
5792 {
5793 struct ppc_link_hash_entry *eh;
5794 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5795 eh->tls_mask |= tls_type;
411e1bfb
AM
5796 }
5797 else
5798 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5799 rel->r_addend, tls_type))
5800 return FALSE;
5801
7c8fe5c4
AM
5802 ppc64_sec = ppc64_elf_section_data (sec);
5803 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5804 {
3a71aa26
AM
5805 bfd_size_type amt;
5806
e7b938ca 5807 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5808 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5809 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5810 if (ppc64_sec->u.toc.symndx == NULL)
5811 return FALSE;
5812 amt = sec->size * sizeof (bfd_vma) / 8;
5813 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5814 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5815 return FALSE;
7c8fe5c4
AM
5816 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5817 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5818 }
5819 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5820 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5821 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5822
5823 /* Mark the second slot of a GD or LD entry.
5824 -1 to indicate GD and -2 to indicate LD. */
5825 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5826 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5827 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5828 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5829 goto dodyn;
5830
5831 case R_PPC64_TPREL16:
5832 case R_PPC64_TPREL16_LO:
5833 case R_PPC64_TPREL16_HI:
5834 case R_PPC64_TPREL16_HA:
5835 case R_PPC64_TPREL16_DS:
5836 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5837 case R_PPC64_TPREL16_HIGH:
5838 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5839 case R_PPC64_TPREL16_HIGHER:
5840 case R_PPC64_TPREL16_HIGHERA:
5841 case R_PPC64_TPREL16_HIGHEST:
5842 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
5843 if (bfd_link_dll (info))
5844 info->flags |= DF_STATIC_TLS;
5845 goto dodyn;
5d1634d7 5846
e86ce104 5847 case R_PPC64_ADDR64:
b9399fcf 5848 if (is_opd
1e2f5b6e 5849 && rel + 1 < rel_end
4ce794b7 5850 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5851 {
8387904d 5852 if (h != NULL)
8c5b4e52 5853 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5854 }
e86ce104
AM
5855 /* Fall through. */
5856
65f38f15
AM
5857 case R_PPC64_ADDR16:
5858 case R_PPC64_ADDR16_DS:
5859 case R_PPC64_ADDR16_HA:
5860 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5861 case R_PPC64_ADDR16_HIGH:
5862 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5863 case R_PPC64_ADDR16_HIGHER:
5864 case R_PPC64_ADDR16_HIGHERA:
5865 case R_PPC64_ADDR16_HIGHEST:
5866 case R_PPC64_ADDR16_HIGHESTA:
5867 case R_PPC64_ADDR16_LO:
5868 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5869 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5870 && rel->r_addend == 0)
5871 {
5872 /* We may need a .plt entry if this reloc refers to a
5873 function in a shared lib. */
5874 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5875 return FALSE;
5876 h->pointer_equality_needed = 1;
5877 }
5878 /* Fall through. */
5879
5880 case R_PPC64_REL30:
5881 case R_PPC64_REL32:
5882 case R_PPC64_REL64:
65f38f15 5883 case R_PPC64_ADDR32:
65f38f15
AM
5884 case R_PPC64_UADDR16:
5885 case R_PPC64_UADDR32:
5886 case R_PPC64_UADDR64:
5bd4f169 5887 case R_PPC64_TOC:
0e1862bb 5888 if (h != NULL && !bfd_link_pic (info))
81848ca0 5889 /* We may need a copy reloc. */
f5385ebf 5890 h->non_got_ref = 1;
81848ca0 5891
41bd81ab 5892 /* Don't propagate .opd relocs. */
b9399fcf 5893 if (NO_OPD_RELOCS && is_opd)
e86ce104 5894 break;
e86ce104 5895
65f38f15
AM
5896 /* If we are creating a shared library, and this is a reloc
5897 against a global symbol, or a non PC relative reloc
5898 against a local symbol, then we need to copy the reloc
5899 into the shared library. However, if we are linking with
5900 -Bsymbolic, we do not need to copy a reloc against a
5901 global symbol which is defined in an object we are
5902 including in the link (i.e., DEF_REGULAR is set). At
5903 this point we have not seen all the input files, so it is
5904 possible that DEF_REGULAR is not set now but will be set
5905 later (it is never cleared). In case of a weak definition,
5906 DEF_REGULAR may be cleared later by a strong definition in
5907 a shared library. We account for that possibility below by
f4656909 5908 storing information in the dyn_relocs field of the hash
65f38f15
AM
5909 table entry. A similar situation occurs when creating
5910 shared libraries and symbol visibility changes render the
5911 symbol local.
5912
5913 If on the other hand, we are creating an executable, we
5914 may need to keep relocations for symbols satisfied by a
5915 dynamic library if we manage to avoid copy relocs for the
5916 symbol. */
411e1bfb 5917 dodyn:
0e1862bb 5918 if ((bfd_link_pic (info)
1d483afe 5919 && (must_be_dyn_reloc (info, r_type)
65f38f15 5920 || (h != NULL
198f1157 5921 && (!SYMBOLIC_BIND (info, h)
65f38f15 5922 || h->root.type == bfd_link_hash_defweak
f5385ebf 5923 || !h->def_regular))))
f4656909 5924 || (ELIMINATE_COPY_RELOCS
0e1862bb 5925 && !bfd_link_pic (info)
65f38f15
AM
5926 && h != NULL
5927 && (h->root.type == bfd_link_hash_defweak
25f23106 5928 || !h->def_regular))
0e1862bb 5929 || (!bfd_link_pic (info)
25f23106 5930 && ifunc != NULL))
5bd4f169 5931 {
65f38f15
AM
5932 /* We must copy these reloc types into the output file.
5933 Create a reloc section in dynobj and make room for
5934 this reloc. */
5bd4f169
AM
5935 if (sreloc == NULL)
5936 {
83bac4b0
NC
5937 sreloc = _bfd_elf_make_dynamic_reloc_section
5938 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5939
5bd4f169 5940 if (sreloc == NULL)
83bac4b0 5941 return FALSE;
5bd4f169
AM
5942 }
5943
65f38f15
AM
5944 /* If this is a global symbol, we count the number of
5945 relocations we need for this symbol. */
5946 if (h != NULL)
5947 {
19e08130
AM
5948 struct elf_dyn_relocs *p;
5949 struct elf_dyn_relocs **head;
5950
ec338859 5951 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5952 p = *head;
5953 if (p == NULL || p->sec != sec)
5954 {
5955 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5956 if (p == NULL)
5957 return FALSE;
5958 p->next = *head;
5959 *head = p;
5960 p->sec = sec;
5961 p->count = 0;
5962 p->pc_count = 0;
5963 }
5964 p->count += 1;
5965 if (!must_be_dyn_reloc (info, r_type))
5966 p->pc_count += 1;
65f38f15
AM
5967 }
5968 else
5969 {
ec338859
AM
5970 /* Track dynamic relocs needed for local syms too.
5971 We really need local syms available to do this
5972 easily. Oh well. */
19e08130
AM
5973 struct ppc_dyn_relocs *p;
5974 struct ppc_dyn_relocs **head;
5975 bfd_boolean is_ifunc;
ec338859 5976 asection *s;
6edfbbad 5977 void *vpp;
87d72d41 5978 Elf_Internal_Sym *isym;
6edfbbad 5979
87d72d41
AM
5980 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5981 abfd, r_symndx);
5982 if (isym == NULL)
b34976b6 5983 return FALSE;
ec338859 5984
87d72d41
AM
5985 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5986 if (s == NULL)
5987 s = sec;
5988
6edfbbad 5989 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5990 head = (struct ppc_dyn_relocs **) vpp;
5991 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5992 p = *head;
5993 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5994 p = p->next;
5995 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5996 {
5997 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5998 if (p == NULL)
5999 return FALSE;
6000 p->next = *head;
6001 *head = p;
6002 p->sec = sec;
6003 p->ifunc = is_ifunc;
6004 p->count = 0;
6005 }
6006 p->count += 1;
ec338859 6007 }
65f38f15 6008 }
5bd4f169 6009 break;
65f38f15
AM
6010
6011 default:
96e0dda4 6012 break;
5bd4f169
AM
6013 }
6014 }
6015
b34976b6 6016 return TRUE;
5bd4f169
AM
6017}
6018
ee67d69a
AM
6019/* Merge backend specific data from an object file to the output
6020 object file when linking. */
6021
6022static bfd_boolean
50e03d47 6023ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6024{
50e03d47 6025 bfd *obfd = info->output_bfd;
ee67d69a
AM
6026 unsigned long iflags, oflags;
6027
6028 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6029 return TRUE;
6030
6031 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6032 return TRUE;
6033
50e03d47 6034 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6035 return FALSE;
6036
6037 iflags = elf_elfheader (ibfd)->e_flags;
6038 oflags = elf_elfheader (obfd)->e_flags;
6039
f6c7c3e8 6040 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6041 {
4eca0228 6042 _bfd_error_handler
695344c0 6043 /* xgettext:c-format */
871b3ab2 6044 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
6045 bfd_set_error (bfd_error_bad_value);
6046 return FALSE;
6047 }
f6c7c3e8 6048 else if (iflags != oflags && iflags != 0)
ee67d69a 6049 {
4eca0228 6050 _bfd_error_handler
695344c0 6051 /* xgettext:c-format */
871b3ab2 6052 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
6053 ibfd, iflags, oflags);
6054 bfd_set_error (bfd_error_bad_value);
6055 return FALSE;
6056 }
6057
50e03d47 6058 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6059
ee67d69a 6060 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6061 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6062
6063 return TRUE;
6064}
6065
6066static bfd_boolean
6067ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6068{
6069 /* Print normal ELF private data. */
6070 _bfd_elf_print_private_bfd_data (abfd, ptr);
6071
6072 if (elf_elfheader (abfd)->e_flags != 0)
6073 {
6074 FILE *file = ptr;
6075
ee67d69a
AM
6076 fprintf (file, _("private flags = 0x%lx:"),
6077 elf_elfheader (abfd)->e_flags);
6078
6079 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6080 fprintf (file, _(" [abiv%ld]"),
6081 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6082 fputc ('\n', file);
6083 }
6084
6085 return TRUE;
6086}
6087
8387904d 6088/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6089 of the code entry point, and its section, which must be in the same
6090 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6091
6092static bfd_vma
6093opd_entry_value (asection *opd_sec,
6094 bfd_vma offset,
6095 asection **code_sec,
aef36ac1
AM
6096 bfd_vma *code_off,
6097 bfd_boolean in_code_sec)
8387904d
AM
6098{
6099 bfd *opd_bfd = opd_sec->owner;
8860955f 6100 Elf_Internal_Rela *relocs;
8387904d 6101 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6102 bfd_vma val;
8387904d 6103
9f296da3
AM
6104 /* No relocs implies we are linking a --just-symbols object, or looking
6105 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6106 if (opd_sec->reloc_count == 0)
6107 {
729eabd5 6108 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6109
729eabd5
AM
6110 if (contents == NULL)
6111 {
6112 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6113 return (bfd_vma) -1;
6114 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6115 }
ee1e4ede 6116
dbb3fbbb 6117 /* PR 17512: file: 64b9dfbb. */
451dfd38 6118 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6119 return (bfd_vma) -1;
6120
729eabd5 6121 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6122 if (code_sec != NULL)
6123 {
6124 asection *sec, *likely = NULL;
ee1e4ede 6125
aef36ac1 6126 if (in_code_sec)
4b85d634 6127 {
aef36ac1
AM
6128 sec = *code_sec;
6129 if (sec->vma <= val
6130 && val < sec->vma + sec->size)
6131 likely = sec;
6132 else
6133 val = -1;
6134 }
6135 else
6136 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6137 if (sec->vma <= val
6138 && (sec->flags & SEC_LOAD) != 0
6139 && (sec->flags & SEC_ALLOC) != 0)
6140 likely = sec;
6141 if (likely != NULL)
6142 {
6143 *code_sec = likely;
6144 if (code_off != NULL)
6145 *code_off = val - likely->vma;
4b85d634
AM
6146 }
6147 }
aef36ac1 6148 return val;
4b85d634
AM
6149 }
6150
0c8d6e5c 6151 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6152
729eabd5 6153 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6154 if (relocs == NULL)
6155 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6156 /* PR 17512: file: df8e1fd6. */
6157 if (relocs == NULL)
6158 return (bfd_vma) -1;
645ea6a9 6159
8387904d 6160 /* Go find the opd reloc at the sym address. */
8860955f 6161 lo = relocs;
8387904d 6162 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6163 val = (bfd_vma) -1;
8387904d
AM
6164 while (lo < hi)
6165 {
6166 look = lo + (hi - lo) / 2;
6167 if (look->r_offset < offset)
6168 lo = look + 1;
6169 else if (look->r_offset > offset)
6170 hi = look;
6171 else
6172 {
0ffa91dd
NC
6173 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6174
8387904d
AM
6175 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6176 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6177 {
6178 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6179 asection *sec = NULL;
8387904d 6180
b53dfeb2
AM
6181 if (symndx >= symtab_hdr->sh_info
6182 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6183 {
6184 struct elf_link_hash_entry **sym_hashes;
6185 struct elf_link_hash_entry *rh;
6186
6187 sym_hashes = elf_sym_hashes (opd_bfd);
6188 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6189 if (rh != NULL)
6190 {
6191 rh = elf_follow_link (rh);
bb854a36
AM
6192 if (rh->root.type != bfd_link_hash_defined
6193 && rh->root.type != bfd_link_hash_defweak)
6194 break;
6195 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6196 {
bb854a36
AM
6197 val = rh->root.u.def.value;
6198 sec = rh->root.u.def.section;
b53dfeb2
AM
6199 }
6200 }
6201 }
6202
6203 if (sec == NULL)
6204 {
6205 Elf_Internal_Sym *sym;
6206
6207 if (symndx < symtab_hdr->sh_info)
6208 {
6209 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6210 if (sym == NULL)
6211 {
6212 size_t symcnt = symtab_hdr->sh_info;
6213 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6214 symcnt, 0,
6215 NULL, NULL, NULL);
6216 if (sym == NULL)
6217 break;
6218 symtab_hdr->contents = (bfd_byte *) sym;
6219 }
6220 sym += symndx;
128205bb
AM
6221 }
6222 else
6223 {
b53dfeb2
AM
6224 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6225 1, symndx,
6226 NULL, NULL, NULL);
128205bb
AM
6227 if (sym == NULL)
6228 break;
128205bb 6229 }
b53dfeb2
AM
6230 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6231 if (sec == NULL)
6232 break;
6233 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6234 val = sym->st_value;
8387904d 6235 }
b53dfeb2 6236
8387904d
AM
6237 val += look->r_addend;
6238 if (code_off != NULL)
6239 *code_off = val;
6240 if (code_sec != NULL)
aef36ac1
AM
6241 {
6242 if (in_code_sec && *code_sec != sec)
6243 return -1;
6244 else
6245 *code_sec = sec;
6246 }
b53dfeb2 6247 if (sec->output_section != NULL)
8387904d 6248 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6249 }
6250 break;
6251 }
6252 }
645ea6a9 6253
645ea6a9 6254 return val;
8387904d
AM
6255}
6256
aef36ac1
AM
6257/* If the ELF symbol SYM might be a function in SEC, return the
6258 function size and set *CODE_OFF to the function's entry point,
6259 otherwise return zero. */
9f296da3 6260
aef36ac1
AM
6261static bfd_size_type
6262ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6263 bfd_vma *code_off)
9f296da3 6264{
aef36ac1
AM
6265 bfd_size_type size;
6266
6267 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6268 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6269 return 0;
6270
6271 size = 0;
6272 if (!(sym->flags & BSF_SYNTHETIC))
6273 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6274
6275 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6276 {
b07bca4e
AM
6277 struct _opd_sec_data *opd = get_opd_info (sym->section);
6278 bfd_vma symval = sym->value;
6279
6280 if (opd != NULL
6281 && opd->adjust != NULL
6282 && elf_section_data (sym->section)->relocs != NULL)
6283 {
6284 /* opd_entry_value will use cached relocs that have been
6285 adjusted, but with raw symbols. That means both local
6286 and global symbols need adjusting. */
6287 long adjust = opd->adjust[OPD_NDX (symval)];
6288 if (adjust == -1)
6289 return 0;
6290 symval += adjust;
6291 }
6292
6293 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6294 &sec, code_off, TRUE) == (bfd_vma) -1)
6295 return 0;
6296 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6297 symbol. This size has nothing to do with the code size of the
6298 function, which is what we're supposed to return, but the
6299 code size isn't available without looking up the dot-sym.
6300 However, doing that would be a waste of time particularly
6301 since elf_find_function will look at the dot-sym anyway.
6302 Now, elf_find_function will keep the largest size of any
6303 function sym found at the code address of interest, so return
6304 1 here to avoid it incorrectly caching a larger function size
6305 for a small function. This does mean we return the wrong
6306 size for a new-ABI function of size 24, but all that does is
6307 disable caching for such functions. */
6308 if (size == 24)
6309 size = 1;
9f296da3 6310 }
aef36ac1
AM
6311 else
6312 {
6313 if (sym->section != sec)
6314 return 0;
6315 *code_off = sym->value;
6316 }
6317 if (size == 0)
6318 size = 1;
6319 return size;
9f296da3
AM
6320}
6321
f378ab09
AM
6322/* Return true if symbol is a strong function defined in an ELFv2
6323 object with st_other localentry bits of zero, ie. its local entry
6324 point coincides with its global entry point. */
6325
6326static bfd_boolean
6327is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6328{
6329 return (h != NULL
6330 && h->type == STT_FUNC
6331 && h->root.type == bfd_link_hash_defined
6332 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6333 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6334 && is_ppc64_elf (h->root.u.def.section->owner)
6335 && abiversion (h->root.u.def.section->owner) >= 2);
6336}
6337
854b41e7
AM
6338/* Return true if symbol is defined in a regular object file. */
6339
6340static bfd_boolean
6341is_static_defined (struct elf_link_hash_entry *h)
6342{
6343 return ((h->root.type == bfd_link_hash_defined
6344 || h->root.type == bfd_link_hash_defweak)
6345 && h->root.u.def.section != NULL
6346 && h->root.u.def.section->output_section != NULL);
6347}
6348
b31867b6
AM
6349/* If FDH is a function descriptor symbol, return the associated code
6350 entry symbol if it is defined. Return NULL otherwise. */
6351
6352static struct ppc_link_hash_entry *
6353defined_code_entry (struct ppc_link_hash_entry *fdh)
6354{
6355 if (fdh->is_func_descriptor)
6356 {
6357 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6358 if (fh->elf.root.type == bfd_link_hash_defined
6359 || fh->elf.root.type == bfd_link_hash_defweak)
6360 return fh;
6361 }
6362 return NULL;
6363}
6364
6365/* If FH is a function code entry symbol, return the associated
6366 function descriptor symbol if it is defined. Return NULL otherwise. */
6367
6368static struct ppc_link_hash_entry *
6369defined_func_desc (struct ppc_link_hash_entry *fh)
6370{
6371 if (fh->oh != NULL
6372 && fh->oh->is_func_descriptor)
6373 {
6374 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6375 if (fdh->elf.root.type == bfd_link_hash_defined
6376 || fdh->elf.root.type == bfd_link_hash_defweak)
6377 return fdh;
6378 }
6379 return NULL;
6380}
6381
8c5b4e52
AM
6382static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6383
6384/* Garbage collect sections, after first dealing with dot-symbols. */
6385
6386static bfd_boolean
6387ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6388{
6389 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6390
6391 if (htab != NULL && htab->need_func_desc_adj)
6392 {
6393 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6394 htab->need_func_desc_adj = 0;
6395 }
6396 return bfd_elf_gc_sections (abfd, info);
6397}
6398
74f0fb50
AM
6399/* Mark all our entry sym sections, both opd and code section. */
6400
6401static void
6402ppc64_elf_gc_keep (struct bfd_link_info *info)
6403{
6404 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6405 struct bfd_sym_chain *sym;
6406
4dfe6ac6
NC
6407 if (htab == NULL)
6408 return;
6409
74f0fb50
AM
6410 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6411 {
b31867b6 6412 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6413 asection *sec;
6414
6415 eh = (struct ppc_link_hash_entry *)
b31867b6 6416 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6417 if (eh == NULL)
6418 continue;
6419 if (eh->elf.root.type != bfd_link_hash_defined
6420 && eh->elf.root.type != bfd_link_hash_defweak)
6421 continue;
6422
b31867b6
AM
6423 fh = defined_code_entry (eh);
6424 if (fh != NULL)
74f0fb50 6425 {
b31867b6 6426 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6427 sec->flags |= SEC_KEEP;
6428 }
6429 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6430 && opd_entry_value (eh->elf.root.u.def.section,
6431 eh->elf.root.u.def.value,
aef36ac1 6432 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6433 sec->flags |= SEC_KEEP;
6434
6435 sec = eh->elf.root.u.def.section;
6436 sec->flags |= SEC_KEEP;
6437 }
6438}
6439
64d03ab5
AM
6440/* Mark sections containing dynamically referenced symbols. When
6441 building shared libraries, we must assume that any visible symbol is
6442 referenced. */
6443
6444static bfd_boolean
6445ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6446{
6447 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6448 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6449 struct ppc_link_hash_entry *fdh;
b407645f 6450 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6451
64d03ab5 6452 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6453 fdh = defined_func_desc (eh);
6454 if (fdh != NULL)
6455 eh = fdh;
64d03ab5
AM
6456
6457 if ((eh->elf.root.type == bfd_link_hash_defined
6458 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6459 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6460 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6461 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6462 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6463 && (!bfd_link_executable (info)
e278ae05 6464 || info->gc_keep_exported
b407645f
AM
6465 || info->export_dynamic
6466 || (eh->elf.dynamic
6467 && d != NULL
6468 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6469 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6470 || !bfd_hide_sym_by_version (info->version_info,
6471 eh->elf.root.root.string)))))
64d03ab5
AM
6472 {
6473 asection *code_sec;
b31867b6 6474 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6475
6476 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6477
6478 /* Function descriptor syms cause the associated
6479 function code sym section to be marked. */
b31867b6
AM
6480 fh = defined_code_entry (eh);
6481 if (fh != NULL)
6482 {
6483 code_sec = fh->elf.root.u.def.section;
6484 code_sec->flags |= SEC_KEEP;
6485 }
64d03ab5
AM
6486 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6487 && opd_entry_value (eh->elf.root.u.def.section,
6488 eh->elf.root.u.def.value,
aef36ac1 6489 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6490 code_sec->flags |= SEC_KEEP;
6491 }
6492
6493 return TRUE;
6494}
6495
5bd4f169
AM
6496/* Return the section that should be marked against GC for a given
6497 relocation. */
6498
6499static asection *
4ce794b7 6500ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6501 struct bfd_link_info *info,
4ce794b7
AM
6502 Elf_Internal_Rela *rel,
6503 struct elf_link_hash_entry *h,
6504 Elf_Internal_Sym *sym)
5bd4f169 6505{
ccfa59ea
AM
6506 asection *rsec;
6507
ccfa59ea
AM
6508 /* Syms return NULL if we're marking .opd, so we avoid marking all
6509 function sections, as all functions are referenced in .opd. */
6510 rsec = NULL;
6511 if (get_opd_info (sec) != NULL)
6512 return rsec;
1e2f5b6e 6513
5bd4f169
AM
6514 if (h != NULL)
6515 {
04c9666a 6516 enum elf_ppc64_reloc_type r_type;
b31867b6 6517 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6518
4ce794b7 6519 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6520 switch (r_type)
5bd4f169
AM
6521 {
6522 case R_PPC64_GNU_VTINHERIT:
6523 case R_PPC64_GNU_VTENTRY:
6524 break;
6525
6526 default:
6527 switch (h->root.type)
6528 {
6529 case bfd_link_hash_defined:
6530 case bfd_link_hash_defweak:
ccfa59ea 6531 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6532 fdh = defined_func_desc (eh);
6533 if (fdh != NULL)
8c5b4e52
AM
6534 {
6535 /* -mcall-aixdesc code references the dot-symbol on
6536 a call reloc. Mark the function descriptor too
6537 against garbage collection. */
6538 fdh->elf.mark = 1;
60d67dc8
AM
6539 if (fdh->elf.is_weakalias)
6540 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6541 eh = fdh;
6542 }
1e2f5b6e
AM
6543
6544 /* Function descriptor syms cause the associated
6545 function code sym section to be marked. */
b31867b6
AM
6546 fh = defined_code_entry (eh);
6547 if (fh != NULL)
ccfa59ea
AM
6548 {
6549 /* They also mark their opd section. */
74f0fb50 6550 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6551
b31867b6 6552 rsec = fh->elf.root.u.def.section;
ccfa59ea 6553 }
8387904d
AM
6554 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6555 && opd_entry_value (eh->elf.root.u.def.section,
6556 eh->elf.root.u.def.value,
aef36ac1 6557 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6558 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6559 else
1e2f5b6e
AM
6560 rsec = h->root.u.def.section;
6561 break;
5bd4f169
AM
6562
6563 case bfd_link_hash_common:
1e2f5b6e
AM
6564 rsec = h->root.u.c.p->section;
6565 break;
5bd4f169
AM
6566
6567 default:
fb34365b 6568 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6569 }
6570 }
6571 }
6572 else
6573 {
74f0fb50 6574 struct _opd_sec_data *opd;
1e2f5b6e
AM
6575
6576 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6577 opd = get_opd_info (rsec);
6578 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6579 {
74f0fb50 6580 rsec->gc_mark = 1;
ccfa59ea 6581
51aecdc5 6582 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6583 }
5bd4f169
AM
6584 }
6585
1e2f5b6e 6586 return rsec;
5bd4f169
AM
6587}
6588
deb0e272
AM
6589/* The maximum size of .sfpr. */
6590#define SFPR_MAX (218*4)
6591
6592struct sfpr_def_parms
6593{
699733f6
AM
6594 const char name[12];
6595 unsigned char lo, hi;
deb0e272
AM
6596 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6597 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6598};
6599
a4b6fadd
AM
6600/* Auto-generate _save*, _rest* functions in .sfpr.
6601 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6602 instead. */
deb0e272 6603
4dfe6ac6 6604static bfd_boolean
a4b6fadd
AM
6605sfpr_define (struct bfd_link_info *info,
6606 const struct sfpr_def_parms *parm,
6607 asection *stub_sec)
deb0e272
AM
6608{
6609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6610 unsigned int i;
6611 size_t len = strlen (parm->name);
6612 bfd_boolean writing = FALSE;
699733f6 6613 char sym[16];
deb0e272 6614
4dfe6ac6
NC
6615 if (htab == NULL)
6616 return FALSE;
6617
deb0e272
AM
6618 memcpy (sym, parm->name, len);
6619 sym[len + 2] = 0;
6620
6621 for (i = parm->lo; i <= parm->hi; i++)
6622 {
a4b6fadd 6623 struct ppc_link_hash_entry *h;
deb0e272
AM
6624
6625 sym[len + 0] = i / 10 + '0';
6626 sym[len + 1] = i % 10 + '0';
a4b6fadd 6627 h = (struct ppc_link_hash_entry *)
b32547cd 6628 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6629 if (stub_sec != NULL)
deb0e272 6630 {
a4b6fadd
AM
6631 if (h != NULL
6632 && h->elf.root.type == bfd_link_hash_defined
6633 && h->elf.root.u.def.section == htab->sfpr)
6634 {
6635 struct elf_link_hash_entry *s;
6636 char buf[32];
6637 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6638 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6639 if (s == NULL)
6640 return FALSE;
6641 if (s->root.type == bfd_link_hash_new
6642 || (s->root.type = bfd_link_hash_defined
6643 && s->root.u.def.section == stub_sec))
6644 {
6645 s->root.type = bfd_link_hash_defined;
6646 s->root.u.def.section = stub_sec;
6647 s->root.u.def.value = (stub_sec->size
6648 + h->elf.root.u.def.value);
6649 s->ref_regular = 1;
6650 s->def_regular = 1;
6651 s->ref_regular_nonweak = 1;
6652 s->forced_local = 1;
6653 s->non_elf = 0;
6654 s->root.linker_def = 1;
6655 }
6656 }
6657 continue;
6658 }
6659 if (h != NULL)
6660 {
6661 h->save_res = 1;
6662 if (!h->elf.def_regular)
deb0e272 6663 {
a4b6fadd
AM
6664 h->elf.root.type = bfd_link_hash_defined;
6665 h->elf.root.u.def.section = htab->sfpr;
6666 h->elf.root.u.def.value = htab->sfpr->size;
6667 h->elf.type = STT_FUNC;
6668 h->elf.def_regular = 1;
b32547cd 6669 h->elf.non_elf = 0;
a4b6fadd
AM
6670 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6671 writing = TRUE;
deb0e272 6672 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6673 {
6674 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6675 if (htab->sfpr->contents == NULL)
6676 return FALSE;
6677 }
deb0e272
AM
6678 }
6679 }
6680 if (writing)
6681 {
6682 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6683 if (i != parm->hi)
6684 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6685 else
6686 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6687 htab->sfpr->size = p - htab->sfpr->contents;
6688 }
6689 }
6690
6691 return TRUE;
6692}
6693
6694static bfd_byte *
6695savegpr0 (bfd *abfd, bfd_byte *p, int r)
6696{
6697 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6698 return p + 4;
6699}
6700
6701static bfd_byte *
6702savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6703{
6704 p = savegpr0 (abfd, p, r);
a078d95a 6705 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6706 p = p + 4;
6707 bfd_put_32 (abfd, BLR, p);
6708 return p + 4;
6709}
6710
6711static bfd_byte *
6712restgpr0 (bfd *abfd, bfd_byte *p, int r)
6713{
6714 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6715 return p + 4;
6716}
6717
6718static bfd_byte *
6719restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6720{
a078d95a 6721 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6722 p = p + 4;
6723 p = restgpr0 (abfd, p, r);
6724 bfd_put_32 (abfd, MTLR_R0, p);
6725 p = p + 4;
6726 if (r == 29)
6727 {
6728 p = restgpr0 (abfd, p, 30);
6729 p = restgpr0 (abfd, p, 31);
6730 }
6731 bfd_put_32 (abfd, BLR, p);
6732 return p + 4;
6733}
6734
6735static bfd_byte *
6736savegpr1 (bfd *abfd, bfd_byte *p, int r)
6737{
6738 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6739 return p + 4;
6740}
6741
6742static bfd_byte *
6743savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6744{
6745 p = savegpr1 (abfd, p, r);
6746 bfd_put_32 (abfd, BLR, p);
6747 return p + 4;
6748}
6749
6750static bfd_byte *
6751restgpr1 (bfd *abfd, bfd_byte *p, int r)
6752{
6753 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6754 return p + 4;
6755}
6756
6757static bfd_byte *
6758restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6759{
6760 p = restgpr1 (abfd, p, r);
6761 bfd_put_32 (abfd, BLR, p);
6762 return p + 4;
6763}
6764
6765static bfd_byte *
6766savefpr (bfd *abfd, bfd_byte *p, int r)
6767{
6768 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6769 return p + 4;
6770}
6771
6772static bfd_byte *
6773savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6774{
6775 p = savefpr (abfd, p, r);
a078d95a 6776 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6777 p = p + 4;
6778 bfd_put_32 (abfd, BLR, p);
6779 return p + 4;
6780}
6781
6782static bfd_byte *
6783restfpr (bfd *abfd, bfd_byte *p, int r)
6784{
6785 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6786 return p + 4;
6787}
6788
6789static bfd_byte *
6790restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6791{
a078d95a 6792 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6793 p = p + 4;
6794 p = restfpr (abfd, p, r);
6795 bfd_put_32 (abfd, MTLR_R0, p);
6796 p = p + 4;
6797 if (r == 29)
6798 {
6799 p = restfpr (abfd, p, 30);
6800 p = restfpr (abfd, p, 31);
6801 }
6802 bfd_put_32 (abfd, BLR, p);
6803 return p + 4;
6804}
6805
6806static bfd_byte *
6807savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6808{
6809 p = savefpr (abfd, p, r);
6810 bfd_put_32 (abfd, BLR, p);
6811 return p + 4;
6812}
6813
6814static bfd_byte *
6815restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6816{
6817 p = restfpr (abfd, p, r);
6818 bfd_put_32 (abfd, BLR, p);
6819 return p + 4;
6820}
6821
6822static bfd_byte *
6823savevr (bfd *abfd, bfd_byte *p, int r)
6824{
6825 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6826 p = p + 4;
6827 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6828 return p + 4;
6829}
6830
6831static bfd_byte *
6832savevr_tail (bfd *abfd, bfd_byte *p, int r)
6833{
6834 p = savevr (abfd, p, r);
6835 bfd_put_32 (abfd, BLR, p);
6836 return p + 4;
6837}
6838
6839static bfd_byte *
6840restvr (bfd *abfd, bfd_byte *p, int r)
6841{
6842 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6843 p = p + 4;
6844 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6845 return p + 4;
6846}
6847
6848static bfd_byte *
6849restvr_tail (bfd *abfd, bfd_byte *p, int r)
6850{
6851 p = restvr (abfd, p, r);
6852 bfd_put_32 (abfd, BLR, p);
6853 return p + 4;
6854}
6855
e86ce104
AM
6856/* Called via elf_link_hash_traverse to transfer dynamic linking
6857 information on function code symbol entries to their corresponding
6858 function descriptor symbol entries. */
deb0e272 6859
b34976b6 6860static bfd_boolean
4ce794b7 6861func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6862{
e86ce104 6863 struct bfd_link_info *info;
65f38f15 6864 struct ppc_link_hash_table *htab;
50bc7936
AM
6865 struct ppc_link_hash_entry *fh;
6866 struct ppc_link_hash_entry *fdh;
6867 bfd_boolean force_local;
5bd4f169 6868
50bc7936
AM
6869 fh = (struct ppc_link_hash_entry *) h;
6870 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6871 return TRUE;
e86ce104 6872
8c5b4e52
AM
6873 if (!fh->is_func)
6874 return TRUE;
6875
6876 if (fh->elf.root.root.string[0] != '.'
6877 || fh->elf.root.root.string[1] == '\0')
6878 return TRUE;
6879
4ce794b7 6880 info = inf;
65f38f15 6881 htab = ppc_hash_table (info);
4dfe6ac6
NC
6882 if (htab == NULL)
6883 return FALSE;
5bd4f169 6884
8c5b4e52
AM
6885 /* Find the corresponding function descriptor symbol. */
6886 fdh = lookup_fdh (fh, htab);
6887
c09bdfe5
AM
6888 /* Resolve undefined references to dot-symbols as the value
6889 in the function descriptor, if we have one in a regular object.
6890 This is to satisfy cases like ".quad .foo". Calls to functions
6891 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6892 if ((fh->elf.root.type == bfd_link_hash_undefined
6893 || fh->elf.root.type == bfd_link_hash_undefweak)
6894 && (fdh->elf.root.type == bfd_link_hash_defined
6895 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6896 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6897 && opd_entry_value (fdh->elf.root.u.def.section,
6898 fdh->elf.root.u.def.value,
c09bdfe5 6899 &fh->elf.root.u.def.section,
aef36ac1 6900 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6901 {
b31867b6 6902 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6903 fh->elf.forced_local = 1;
b31867b6
AM
6904 fh->elf.def_regular = fdh->elf.def_regular;
6905 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6906 }
6907
8c5b4e52
AM
6908 if (!fh->elf.dynamic)
6909 {
6910 struct plt_entry *ent;
5bd4f169 6911
8c5b4e52
AM
6912 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6913 if (ent->plt.refcount > 0)
6914 break;
6915 if (ent == NULL)
6916 return TRUE;
6917 }
5bd4f169 6918
8c5b4e52 6919 /* Create a descriptor as undefined if necessary. */
50bc7936 6920 if (fdh == NULL
0e1862bb 6921 && !bfd_link_executable (info)
50bc7936
AM
6922 && (fh->elf.root.type == bfd_link_hash_undefined
6923 || fh->elf.root.type == bfd_link_hash_undefweak))
6924 {
908b32fc 6925 fdh = make_fdh (info, fh);
bb700d78
AM
6926 if (fdh == NULL)
6927 return FALSE;
50bc7936 6928 }
648cca2c 6929
8c5b4e52 6930 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6931 if (fdh != NULL
6932 && fdh->fake
8c5b4e52
AM
6933 && (fh->elf.root.type == bfd_link_hash_defined
6934 || fh->elf.root.type == bfd_link_hash_defweak))
6935 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6936
8c5b4e52
AM
6937 /* Transfer dynamic linking information to the function descriptor. */
6938 if (fdh != NULL)
6939 {
f5385ebf
AM
6940 fdh->elf.ref_regular |= fh->elf.ref_regular;
6941 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6942 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6943 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6944 fdh->elf.dynamic |= fh->elf.dynamic;
6945 fdh->elf.needs_plt |= (fh->elf.needs_plt
6946 || fh->elf.type == STT_FUNC
6947 || fh->elf.type == STT_GNU_IFUNC);
6948 move_plt_plist (fh, fdh);
6949
6950 if (!fdh->elf.forced_local
6951 && fh->elf.dynindx != -1)
6952 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6953 return FALSE;
e86ce104
AM
6954 }
6955
50bc7936
AM
6956 /* Now that the info is on the function descriptor, clear the
6957 function code sym info. Any function code syms for which we
6958 don't have a definition in a regular file, we force local.
6959 This prevents a shared library from exporting syms that have
6960 been imported from another library. Function code syms that
6961 are really in the library we must leave global to prevent the
6962 linker dragging in a definition from a static library. */
93f3fa99
AM
6963 force_local = (!fh->elf.def_regular
6964 || fdh == NULL
6965 || !fdh->elf.def_regular
6966 || fdh->elf.forced_local);
50bc7936
AM
6967 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6968
b34976b6 6969 return TRUE;
e86ce104 6970}
40b8271b 6971
a4b6fadd
AM
6972static const struct sfpr_def_parms save_res_funcs[] =
6973 {
6974 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6975 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6976 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6977 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6978 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6979 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6980 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6981 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6982 { "._savef", 14, 31, savefpr, savefpr1_tail },
6983 { "._restf", 14, 31, restfpr, restfpr1_tail },
6984 { "_savevr_", 20, 31, savevr, savevr_tail },
6985 { "_restvr_", 20, 31, restvr, restvr_tail }
6986 };
6987
e86ce104 6988/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6989 this hook to a) provide some gcc support functions, and b) transfer
6990 dynamic linking information gathered so far on function code symbol
6991 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6992
b34976b6 6993static bfd_boolean
4ce794b7
AM
6994ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6995 struct bfd_link_info *info)
e86ce104
AM
6996{
6997 struct ppc_link_hash_table *htab;
6998
6999 htab = ppc_hash_table (info);
4dfe6ac6
NC
7000 if (htab == NULL)
7001 return FALSE;
7002
b32547cd
AM
7003 /* Provide any missing _save* and _rest* functions. */
7004 if (htab->sfpr != NULL)
7005 {
7006 unsigned int i;
7007
7008 htab->sfpr->size = 0;
7009 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7010 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7011 return FALSE;
7012 if (htab->sfpr->size == 0)
7013 htab->sfpr->flags |= SEC_EXCLUDE;
7014 }
7015
7016 if (bfd_link_relocatable (info))
7017 return TRUE;
7018
7019 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7020 {
7021 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7022 /* Make .TOC. defined so as to prevent it being made dynamic.
7023 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7024 if (!htab->elf.hgot->def_regular
7025 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7026 {
7027 htab->elf.hgot->root.type = bfd_link_hash_defined;
7028 htab->elf.hgot->root.u.def.value = 0;
7029 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7030 htab->elf.hgot->def_regular = 1;
7031 htab->elf.hgot->root.linker_def = 1;
7032 }
dba6fa9b 7033 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7034 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7035 | STV_HIDDEN);
7036 }
c66bb0ee 7037
8c5b4e52
AM
7038 if (htab->need_func_desc_adj)
7039 {
7040 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7041 htab->need_func_desc_adj = 0;
7042 }
805fc799 7043
b34976b6 7044 return TRUE;
e86ce104
AM
7045}
7046
98bbb1b8 7047/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7048
98bbb1b8 7049static asection *
a345bc8d
AM
7050readonly_dynrelocs (struct elf_link_hash_entry *h)
7051{
7052 struct ppc_link_hash_entry *eh;
7053 struct elf_dyn_relocs *p;
7054
7055 eh = (struct ppc_link_hash_entry *) h;
7056 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7057 {
7058 asection *s = p->sec->output_section;
7059
7060 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7061 return p->sec;
a345bc8d 7062 }
98bbb1b8 7063 return NULL;
a345bc8d
AM
7064}
7065
d311bc8b 7066/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7067 aliases, that apply to read-only sections. Cannot be used after
7068 size_dynamic_sections. */
d311bc8b
AM
7069
7070static bfd_boolean
7071alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7072{
7073 struct ppc_link_hash_entry *eh;
7074
7075 eh = (struct ppc_link_hash_entry *) h;
7076 do
7077 {
7078 if (readonly_dynrelocs (&eh->elf))
7079 return TRUE;
ab2477e1 7080 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
d311bc8b
AM
7081 } while (eh != NULL && &eh->elf != h);
7082
7083 return FALSE;
7084}
8a2058b5 7085
8a9e8e72
AM
7086/* Return whether EH has pc-relative dynamic relocs. */
7087
7088static bfd_boolean
7089pc_dynrelocs (struct ppc_link_hash_entry *eh)
7090{
7091 struct elf_dyn_relocs *p;
7092
7093 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7094 if (p->pc_count != 0)
7095 return TRUE;
7096 return FALSE;
7097}
7098
8a2058b5
AM
7099/* Return true if a global entry stub will be created for H. Valid
7100 for ELFv2 before plt entries have been allocated. */
7101
7102static bfd_boolean
7103global_entry_stub (struct elf_link_hash_entry *h)
7104{
7105 struct plt_entry *pent;
7106
7107 if (!h->pointer_equality_needed
7108 || h->def_regular)
7109 return FALSE;
7110
7111 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7112 if (pent->plt.refcount > 0
7113 && pent->addend == 0)
7114 return TRUE;
7115
7116 return FALSE;
7117}
7118
e86ce104
AM
7119/* Adjust a symbol defined by a dynamic object and referenced by a
7120 regular object. The current definition is in some section of the
7121 dynamic object, but we're not including those sections. We have to
7122 change the definition to something the rest of the link can
7123 understand. */
7124
b34976b6 7125static bfd_boolean
4ce794b7
AM
7126ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7127 struct elf_link_hash_entry *h)
e86ce104
AM
7128{
7129 struct ppc_link_hash_table *htab;
5474d94f 7130 asection *s, *srel;
e86ce104
AM
7131
7132 htab = ppc_hash_table (info);
4dfe6ac6
NC
7133 if (htab == NULL)
7134 return FALSE;
e86ce104
AM
7135
7136 /* Deal with function syms. */
7137 if (h->type == STT_FUNC
e054468f 7138 || h->type == STT_GNU_IFUNC
f5385ebf 7139 || h->needs_plt)
e86ce104 7140 {
529fe20e
AM
7141 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7142 || SYMBOL_CALLS_LOCAL (info, h)
7143 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7144 /* Discard dyn_relocs when non-pic if we've decided that a
7145 function symbol is local and not an ifunc. We keep dynamic
7146 relocs for ifuncs when local rather than always emitting a
7147 plt call stub for them and defining the symbol on the call
7148 stub. We can't do that for ELFv1 anyway (a function symbol
7149 is defined on a descriptor, not code) and it can be faster at
7150 run-time due to not needing to bounce through a stub. The
7151 dyn_relocs for ifuncs will be applied even in a static
7152 executable. */
7153 if (!bfd_link_pic (info)
7154 && h->type != STT_GNU_IFUNC
7155 && local)
7156 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7157
e86ce104
AM
7158 /* Clear procedure linkage table information for any symbol that
7159 won't need a .plt entry. */
411e1bfb
AM
7160 struct plt_entry *ent;
7161 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7162 if (ent->plt.refcount > 0)
7163 break;
8387904d 7164 if (ent == NULL
529fe20e 7165 || (h->type != STT_GNU_IFUNC && local))
40b8271b 7166 {
411e1bfb 7167 h->plt.plist = NULL;
f5385ebf 7168 h->needs_plt = 0;
d1eca1e4 7169 h->pointer_equality_needed = 0;
40b8271b 7170 }
8a2058b5 7171 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7172 {
d1eca1e4
AM
7173 /* Taking a function's address in a read/write section
7174 doesn't require us to define the function symbol in the
7175 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7176 be used instead. The reason we prefer a few more dynamic
7177 relocs is that calling via a global entry stub costs a
7178 few more instructions, and pointer_equality_needed causes
7179 extra work in ld.so when resolving these symbols. */
529fe20e 7180 if (global_entry_stub (h))
d1eca1e4 7181 {
ab2477e1 7182 if (!readonly_dynrelocs (h))
529fe20e
AM
7183 {
7184 h->pointer_equality_needed = 0;
7185 /* If we haven't seen a branch reloc then we don't need
7186 a plt entry. */
7187 if (!h->needs_plt)
7188 h->plt.plist = NULL;
7189 }
7190 else if (!bfd_link_pic (info))
7191 /* We are going to be defining the function symbol on the
7192 plt stub, so no dyn_relocs needed when non-pic. */
7193 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7194 }
7195
3988aed5
AM
7196 /* ELFv2 function symbols can't have copy relocs. */
7197 return TRUE;
7198 }
7199 else if (!h->needs_plt
ab2477e1 7200 && !readonly_dynrelocs (h))
3988aed5
AM
7201 {
7202 /* If we haven't seen a branch reloc then we don't need a
7203 plt entry. */
7204 h->plt.plist = NULL;
7205 h->pointer_equality_needed = 0;
a345bc8d
AM
7206 return TRUE;
7207 }
5bd4f169 7208 }
bbd7ec4a 7209 else
411e1bfb 7210 h->plt.plist = NULL;
5bd4f169
AM
7211
7212 /* If this is a weak symbol, and there is a real definition, the
7213 processor independent code will have arranged for us to see the
7214 real definition first, and we can just use the same value. */
60d67dc8 7215 if (h->is_weakalias)
5bd4f169 7216 {
60d67dc8
AM
7217 struct elf_link_hash_entry *def = weakdef (h);
7218 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7219 h->root.u.def.section = def->root.u.def.section;
7220 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7221 if (def->root.u.def.section == htab->elf.sdynbss
7222 || def->root.u.def.section == htab->elf.sdynrelro)
7223 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7224 return TRUE;
5bd4f169
AM
7225 }
7226
5bd4f169
AM
7227 /* If we are creating a shared library, we must presume that the
7228 only references to the symbol are via the global offset table.
7229 For such cases we need not do anything here; the relocations will
7230 be handled correctly by relocate_section. */
0e1862bb 7231 if (bfd_link_pic (info))
b34976b6 7232 return TRUE;
5bd4f169 7233
65f38f15
AM
7234 /* If there are no references to this symbol that do not use the
7235 GOT, we don't need to generate a copy reloc. */
f5385ebf 7236 if (!h->non_got_ref)
b34976b6 7237 return TRUE;
65f38f15 7238
b186458a 7239 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7240 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7241
d93d1c80
AM
7242 /* If -z nocopyreloc was given, don't generate them either. */
7243 || info->nocopyreloc
a127494f 7244
dce2246a 7245 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7246 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7247 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7248
d93d1c80
AM
7249 /* Protected variables do not work with .dynbss. The copy in
7250 .dynbss won't be used by the shared library with the protected
7251 definition for the variable. Text relocations are preferable
7252 to an incorrect program. */
7253 || h->protected_def)
529fe20e 7254 return TRUE;
a127494f 7255
5d35169e 7256 if (h->plt.plist != NULL)
97b639ba
AM
7257 {
7258 /* We should never get here, but unfortunately there are versions
7259 of gcc out there that improperly (for this ABI) put initialized
7260 function pointers, vtable refs and suchlike in read-only
7261 sections. Allow them to proceed, but warn that this might
7262 break at runtime. */
25f53a85 7263 info->callbacks->einfo
c1c8c1ef 7264 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 7265 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7266 h->root.root.string);
7267 }
5d35169e
AM
7268
7269 /* This is a reference to a symbol defined by a dynamic object which
7270 is not a function. */
7271
5bd4f169
AM
7272 /* We must allocate the symbol in our .dynbss section, which will
7273 become part of the .bss section of the executable. There will be
7274 an entry for this symbol in the .dynsym section. The dynamic
7275 object will contain position independent code, so all references
7276 from the dynamic object to this symbol will go through the global
7277 offset table. The dynamic linker will use the .dynsym entry to
7278 determine the address it must put in the global offset table, so
7279 both the dynamic object and the regular object will refer to the
7280 same memory location for the variable. */
5474d94f
AM
7281 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7282 {
7283 s = htab->elf.sdynrelro;
7284 srel = htab->elf.sreldynrelro;
7285 }
7286 else
7287 {
7288 s = htab->elf.sdynbss;
7289 srel = htab->elf.srelbss;
7290 }
1d7e9d18 7291 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7292 {
4a7e5234
AM
7293 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7294 linker to copy the initial value out of the dynamic object
7295 and into the runtime process image. */
5474d94f 7296 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7297 h->needs_copy = 1;
5bd4f169
AM
7298 }
7299
529fe20e
AM
7300 /* We no longer want dyn_relocs. */
7301 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7302 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7303}
7304
e86ce104
AM
7305/* If given a function descriptor symbol, hide both the function code
7306 sym and the descriptor. */
7307static void
4ce794b7
AM
7308ppc64_elf_hide_symbol (struct bfd_link_info *info,
7309 struct elf_link_hash_entry *h,
7310 bfd_boolean force_local)
e86ce104 7311{
34814b9f 7312 struct ppc_link_hash_entry *eh;
e86ce104
AM
7313 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7314
34814b9f
AM
7315 eh = (struct ppc_link_hash_entry *) h;
7316 if (eh->is_func_descriptor)
e86ce104 7317 {
34814b9f 7318 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7319
721956f4 7320 if (fh == NULL)
d1329ca3
AM
7321 {
7322 const char *p, *q;
b8ac2841 7323 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7324 char save;
7325
7326 /* We aren't supposed to use alloca in BFD because on
7327 systems which do not have alloca the version in libiberty
7328 calls xmalloc, which might cause the program to crash
7329 when it runs out of memory. This function doesn't have a
7330 return status, so there's no way to gracefully return an
7331 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7332 accessed; It's either a string in an ELF string table,
7333 or allocated in an objalloc structure. */
d1329ca3 7334
34814b9f 7335 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7336 save = *p;
7337 *(char *) p = '.';
34814b9f 7338 fh = (struct ppc_link_hash_entry *)
b8ac2841 7339 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7340 *(char *) p = save;
7341
7342 /* Unfortunately, if it so happens that the string we were
7343 looking for was allocated immediately before this string,
7344 then we overwrote the string terminator. That's the only
7345 reason the lookup should fail. */
7346 if (fh == NULL)
7347 {
34814b9f
AM
7348 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7349 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7350 --q, --p;
34814b9f
AM
7351 if (q < eh->elf.root.root.string && *p == '.')
7352 fh = (struct ppc_link_hash_entry *)
b8ac2841 7353 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7354 }
7355 if (fh != NULL)
7356 {
34814b9f
AM
7357 eh->oh = fh;
7358 fh->oh = eh;
d1329ca3
AM
7359 }
7360 }
e86ce104 7361 if (fh != NULL)
34814b9f 7362 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7363 }
7364}
7365
411e1bfb 7366static bfd_boolean
8843416a
AM
7367get_sym_h (struct elf_link_hash_entry **hp,
7368 Elf_Internal_Sym **symp,
7369 asection **symsecp,
f961d9dd 7370 unsigned char **tls_maskp,
8843416a
AM
7371 Elf_Internal_Sym **locsymsp,
7372 unsigned long r_symndx,
7373 bfd *ibfd)
411e1bfb 7374{
0ffa91dd 7375 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7376
7377 if (r_symndx >= symtab_hdr->sh_info)
7378 {
7379 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7380 struct elf_link_hash_entry *h;
7381
7382 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7383 h = elf_follow_link (h);
411e1bfb
AM
7384
7385 if (hp != NULL)
7386 *hp = h;
7387
7388 if (symp != NULL)
7389 *symp = NULL;
7390
7391 if (symsecp != NULL)
7392 {
7393 asection *symsec = NULL;
7394 if (h->root.type == bfd_link_hash_defined
7395 || h->root.type == bfd_link_hash_defweak)
7396 symsec = h->root.u.def.section;
7397 *symsecp = symsec;
7398 }
7399
e7b938ca 7400 if (tls_maskp != NULL)
411e1bfb
AM
7401 {
7402 struct ppc_link_hash_entry *eh;
7403
7404 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7405 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7406 }
7407 }
7408 else
7409 {
7410 Elf_Internal_Sym *sym;
7411 Elf_Internal_Sym *locsyms = *locsymsp;
7412
7413 if (locsyms == NULL)
7414 {
7415 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7416 if (locsyms == NULL)
7417 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7418 symtab_hdr->sh_info,
7419 0, NULL, NULL, NULL);
7420 if (locsyms == NULL)
7421 return FALSE;
7422 *locsymsp = locsyms;
7423 }
7424 sym = locsyms + r_symndx;
7425
7426 if (hp != NULL)
7427 *hp = NULL;
7428
7429 if (symp != NULL)
7430 *symp = sym;
7431
7432 if (symsecp != NULL)
cb33740c 7433 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7434
e7b938ca 7435 if (tls_maskp != NULL)
411e1bfb
AM
7436 {
7437 struct got_entry **lgot_ents;
f961d9dd 7438 unsigned char *tls_mask;
411e1bfb 7439
e7b938ca 7440 tls_mask = NULL;
411e1bfb
AM
7441 lgot_ents = elf_local_got_ents (ibfd);
7442 if (lgot_ents != NULL)
7443 {
e054468f
AM
7444 struct plt_entry **local_plt = (struct plt_entry **)
7445 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7446 unsigned char *lgot_masks = (unsigned char *)
e054468f 7447 (local_plt + symtab_hdr->sh_info);
e7b938ca 7448 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7449 }
e7b938ca 7450 *tls_maskp = tls_mask;
411e1bfb
AM
7451 }
7452 }
7453 return TRUE;
7454}
7455
e7b938ca 7456/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7457 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7458 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7459
7460static int
f961d9dd 7461get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7462 unsigned long *toc_symndx,
7463 bfd_vma *toc_addend,
0d4792f7 7464 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7465 const Elf_Internal_Rela *rel,
7466 bfd *ibfd)
411e1bfb
AM
7467{
7468 unsigned long r_symndx;
0d4792f7 7469 int next_r;
411e1bfb
AM
7470 struct elf_link_hash_entry *h;
7471 Elf_Internal_Sym *sym;
7472 asection *sec;
7473 bfd_vma off;
7474
7475 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7476 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7477 return 0;
411e1bfb 7478
e7b938ca 7479 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7480 || sec == NULL
6bee8834 7481 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7482 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7483 return 1;
411e1bfb
AM
7484
7485 /* Look inside a TOC section too. */
7486 if (h != NULL)
7487 {
7488 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7489 off = h->root.u.def.value;
7490 }
7491 else
7492 off = sym->st_value;
7493 off += rel->r_addend;
7494 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7495 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7496 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7497 if (toc_symndx != NULL)
7498 *toc_symndx = r_symndx;
3a71aa26
AM
7499 if (toc_addend != NULL)
7500 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7501 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7502 return 0;
854b41e7 7503 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7504 && (next_r == -1 || next_r == -2))
7505 return 1 - next_r;
951fd09b 7506 return 1;
411e1bfb
AM
7507}
7508
3b421ab3
AM
7509/* Find (or create) an entry in the tocsave hash table. */
7510
7511static struct tocsave_entry *
7512tocsave_find (struct ppc_link_hash_table *htab,
7513 enum insert_option insert,
7514 Elf_Internal_Sym **local_syms,
7515 const Elf_Internal_Rela *irela,
7516 bfd *ibfd)
7517{
7518 unsigned long r_indx;
7519 struct elf_link_hash_entry *h;
7520 Elf_Internal_Sym *sym;
7521 struct tocsave_entry ent, *p;
7522 hashval_t hash;
7523 struct tocsave_entry **slot;
7524
7525 r_indx = ELF64_R_SYM (irela->r_info);
7526 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7527 return NULL;
7528 if (ent.sec == NULL || ent.sec->output_section == NULL)
7529 {
4eca0228 7530 _bfd_error_handler
871b3ab2 7531 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7532 return NULL;
7533 }
7534
7535 if (h != NULL)
7536 ent.offset = h->root.u.def.value;
7537 else
7538 ent.offset = sym->st_value;
7539 ent.offset += irela->r_addend;
7540
7541 hash = tocsave_htab_hash (&ent);
7542 slot = ((struct tocsave_entry **)
7543 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7544 if (slot == NULL)
7545 return NULL;
7546
7547 if (*slot == NULL)
7548 {
7549 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7550 if (p == NULL)
7551 return NULL;
7552 *p = ent;
7553 *slot = p;
7554 }
7555 return *slot;
7556}
7557
754021d0 7558/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7559 code for the old ABI, these will already have been done. */
754021d0
AM
7560
7561static bfd_boolean
7562adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7563{
7564 struct ppc_link_hash_entry *eh;
7565 asection *sym_sec;
74f0fb50 7566 struct _opd_sec_data *opd;
754021d0
AM
7567
7568 if (h->root.type == bfd_link_hash_indirect)
7569 return TRUE;
7570
754021d0
AM
7571 if (h->root.type != bfd_link_hash_defined
7572 && h->root.type != bfd_link_hash_defweak)
7573 return TRUE;
7574
7575 eh = (struct ppc_link_hash_entry *) h;
7576 if (eh->adjust_done)
7577 return TRUE;
7578
7579 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7580 opd = get_opd_info (sym_sec);
7581 if (opd != NULL && opd->adjust != NULL)
754021d0 7582 {
51aecdc5 7583 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7584 if (adjust == -1)
7585 {
7586 /* This entry has been deleted. */
b3fac117 7587 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7588 if (dsec == NULL)
7589 {
7590 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7591 if (discarded_section (dsec))
81688140 7592 {
b3fac117 7593 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7594 break;
7595 }
7596 }
4025353c 7597 eh->elf.root.u.def.value = 0;
81688140 7598 eh->elf.root.u.def.section = dsec;
4025353c
AM
7599 }
7600 else
7601 eh->elf.root.u.def.value += adjust;
754021d0
AM
7602 eh->adjust_done = 1;
7603 }
7604 return TRUE;
7605}
7606
8c1d1bb8 7607/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7608 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7609 have already been determined. */
7610
7611static bfd_boolean
7612dec_dynrel_count (bfd_vma r_info,
7613 asection *sec,
7614 struct bfd_link_info *info,
7615 Elf_Internal_Sym **local_syms,
7616 struct elf_link_hash_entry *h,
19e08130 7617 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7618{
7619 enum elf_ppc64_reloc_type r_type;
19e08130 7620 asection *sym_sec = NULL;
8c1d1bb8
AM
7621
7622 /* Can this reloc be dynamic? This switch, and later tests here
7623 should be kept in sync with the code in check_relocs. */
7624 r_type = ELF64_R_TYPE (r_info);
7625 switch (r_type)
7626 {
7627 default:
7628 return TRUE;
7629
7630 case R_PPC64_TPREL16:
7631 case R_PPC64_TPREL16_LO:
7632 case R_PPC64_TPREL16_HI:
7633 case R_PPC64_TPREL16_HA:
7634 case R_PPC64_TPREL16_DS:
7635 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7636 case R_PPC64_TPREL16_HIGH:
7637 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7638 case R_PPC64_TPREL16_HIGHER:
7639 case R_PPC64_TPREL16_HIGHERA:
7640 case R_PPC64_TPREL16_HIGHEST:
7641 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7642 case R_PPC64_TPREL64:
7643 case R_PPC64_DTPMOD64:
7644 case R_PPC64_DTPREL64:
7645 case R_PPC64_ADDR64:
7646 case R_PPC64_REL30:
7647 case R_PPC64_REL32:
7648 case R_PPC64_REL64:
7649 case R_PPC64_ADDR14:
7650 case R_PPC64_ADDR14_BRNTAKEN:
7651 case R_PPC64_ADDR14_BRTAKEN:
7652 case R_PPC64_ADDR16:
7653 case R_PPC64_ADDR16_DS:
7654 case R_PPC64_ADDR16_HA:
7655 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7656 case R_PPC64_ADDR16_HIGH:
7657 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7658 case R_PPC64_ADDR16_HIGHER:
7659 case R_PPC64_ADDR16_HIGHERA:
7660 case R_PPC64_ADDR16_HIGHEST:
7661 case R_PPC64_ADDR16_HIGHESTA:
7662 case R_PPC64_ADDR16_LO:
7663 case R_PPC64_ADDR16_LO_DS:
7664 case R_PPC64_ADDR24:
7665 case R_PPC64_ADDR32:
7666 case R_PPC64_UADDR16:
7667 case R_PPC64_UADDR32:
7668 case R_PPC64_UADDR64:
7669 case R_PPC64_TOC:
7670 break;
7671 }
7672
7673 if (local_syms != NULL)
7674 {
7675 unsigned long r_symndx;
8c1d1bb8
AM
7676 bfd *ibfd = sec->owner;
7677
7678 r_symndx = ELF64_R_SYM (r_info);
7679 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7680 return FALSE;
7681 }
7682
0e1862bb 7683 if ((bfd_link_pic (info)
1d483afe 7684 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7685 || (h != NULL
198f1157 7686 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7687 || h->root.type == bfd_link_hash_defweak
7688 || !h->def_regular))))
7689 || (ELIMINATE_COPY_RELOCS
0e1862bb 7690 && !bfd_link_pic (info)
8c1d1bb8
AM
7691 && h != NULL
7692 && (h->root.type == bfd_link_hash_defweak
7693 || !h->def_regular)))
7694 ;
7695 else
7696 return TRUE;
7697
7698 if (h != NULL)
6edfbbad 7699 {
19e08130
AM
7700 struct elf_dyn_relocs *p;
7701 struct elf_dyn_relocs **pp;
7702 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7703
7704 /* elf_gc_sweep may have already removed all dyn relocs associated
7705 with local syms for a given section. Also, symbol flags are
7706 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7707 report a dynreloc miscount. */
7708 if (*pp == NULL && info->gc_sections)
7709 return TRUE;
7710
7711 while ((p = *pp) != NULL)
60124e18 7712 {
19e08130
AM
7713 if (p->sec == sec)
7714 {
7715 if (!must_be_dyn_reloc (info, r_type))
7716 p->pc_count -= 1;
7717 p->count -= 1;
7718 if (p->count == 0)
7719 *pp = p->next;
7720 return TRUE;
7721 }
7722 pp = &p->next;
60124e18 7723 }
6edfbbad 7724 }
19e08130
AM
7725 else
7726 {
7727 struct ppc_dyn_relocs *p;
7728 struct ppc_dyn_relocs **pp;
7729 void *vpp;
7730 bfd_boolean is_ifunc;
8c1d1bb8 7731
19e08130
AM
7732 if (local_syms == NULL)
7733 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7734 if (sym_sec == NULL)
7735 sym_sec = sec;
c57da1a7 7736
19e08130
AM
7737 vpp = &elf_section_data (sym_sec)->local_dynrel;
7738 pp = (struct ppc_dyn_relocs **) vpp;
7739
7740 if (*pp == NULL && info->gc_sections)
7741 return TRUE;
7742
7743 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7744 while ((p = *pp) != NULL)
8c1d1bb8 7745 {
19e08130
AM
7746 if (p->sec == sec && p->ifunc == is_ifunc)
7747 {
7748 p->count -= 1;
7749 if (p->count == 0)
7750 *pp = p->next;
7751 return TRUE;
7752 }
7753 pp = &p->next;
8c1d1bb8 7754 }
8c1d1bb8
AM
7755 }
7756
695344c0 7757 /* xgettext:c-format */
cf97bcb0
AM
7758 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7759 sec->owner, sec);
8c1d1bb8
AM
7760 bfd_set_error (bfd_error_bad_value);
7761 return FALSE;
7762}
7763
754021d0
AM
7764/* Remove unused Official Procedure Descriptor entries. Currently we
7765 only remove those associated with functions in discarded link-once
7766 sections, or weakly defined functions that have been overridden. It
7767 would be possible to remove many more entries for statically linked
7768 applications. */
7769
b34976b6 7770bfd_boolean
e7d1c40c 7771ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7772{
7773 bfd *ibfd;
754021d0 7774 bfd_boolean some_edited = FALSE;
3f764659 7775 asection *need_pad = NULL;
e7d1c40c
AM
7776 struct ppc_link_hash_table *htab;
7777
7778 htab = ppc_hash_table (info);
7779 if (htab == NULL)
7780 return FALSE;
1e2f5b6e 7781
c72f2fb2 7782 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7783 {
7784 asection *sec;
7785 Elf_Internal_Rela *relstart, *rel, *relend;
7786 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7787 Elf_Internal_Sym *local_syms;
74f0fb50 7788 struct _opd_sec_data *opd;
51aecdc5 7789 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7790 bfd_size_type cnt_16b = 0;
1e2f5b6e 7791
854b41e7
AM
7792 if (!is_ppc64_elf (ibfd))
7793 continue;
7794
1e2f5b6e 7795 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7796 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7797 continue;
7798
dbaa2011 7799 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7800 continue;
7801
1e2f5b6e
AM
7802 if (sec->output_section == bfd_abs_section_ptr)
7803 continue;
7804
7805 /* Look through the section relocs. */
7806 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7807 continue;
7808
6cdc0ccc 7809 local_syms = NULL;
0ffa91dd 7810 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7811
7812 /* Read the relocations. */
4ce794b7 7813 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7814 info->keep_memory);
1e2f5b6e 7815 if (relstart == NULL)
b34976b6 7816 return FALSE;
1e2f5b6e
AM
7817
7818 /* First run through the relocs to check they are sane, and to
7819 determine whether we need to edit this opd section. */
b34976b6 7820 need_edit = FALSE;
51aecdc5 7821 broken = FALSE;
3f764659 7822 need_pad = sec;
1e2f5b6e 7823 relend = relstart + sec->reloc_count;
50bc7936 7824 for (rel = relstart; rel < relend; )
1e2f5b6e 7825 {
04c9666a 7826 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7827 unsigned long r_symndx;
7828 asection *sym_sec;
7829 struct elf_link_hash_entry *h;
7830 Elf_Internal_Sym *sym;
51aecdc5 7831 bfd_vma offset;
1e2f5b6e 7832
51aecdc5 7833 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7834 only interested in the reloc pointing to a function entry
7835 point. */
51aecdc5
AM
7836 offset = rel->r_offset;
7837 if (rel + 1 == relend
7838 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7839 {
7840 /* If someone messes with .opd alignment then after a
7841 "ld -r" we might have padding in the middle of .opd.
7842 Also, there's nothing to prevent someone putting
7843 something silly in .opd with the assembler. No .opd
b34976b6 7844 optimization for them! */
3f764659 7845 broken_opd:
4eca0228 7846 _bfd_error_handler
871b3ab2 7847 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7848 broken = TRUE;
1e2f5b6e
AM
7849 break;
7850 }
7851
50bc7936
AM
7852 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7853 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7854 {
4eca0228 7855 _bfd_error_handler
695344c0 7856 /* xgettext:c-format */
871b3ab2 7857 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7858 ibfd, r_type);
51aecdc5 7859 broken = TRUE;
50bc7936
AM
7860 break;
7861 }
7862
1e2f5b6e 7863 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7864 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7865 r_symndx, ibfd))
50bc7936 7866 goto error_ret;
1e2f5b6e
AM
7867
7868 if (sym_sec == NULL || sym_sec->owner == NULL)
7869 {
411e1bfb
AM
7870 const char *sym_name;
7871 if (h != NULL)
7872 sym_name = h->root.root.string;
7873 else
26c61ae5
L
7874 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7875 sym_sec);
411e1bfb 7876
4eca0228 7877 _bfd_error_handler
695344c0 7878 /* xgettext:c-format */
871b3ab2 7879 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7880 ibfd, sym_name);
51aecdc5 7881 broken = TRUE;
1e2f5b6e
AM
7882 break;
7883 }
7884
51020317
AM
7885 /* opd entries are always for functions defined in the
7886 current input bfd. If the symbol isn't defined in the
7887 input bfd, then we won't be using the function in this
7888 bfd; It must be defined in a linkonce section in another
7889 bfd, or is weak. It's also possible that we are
7890 discarding the function due to a linker script /DISCARD/,
7891 which we test for via the output_section. */
7892 if (sym_sec->owner != ibfd
7893 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7894 need_edit = TRUE;
1e2f5b6e 7895
50bc7936 7896 rel += 2;
51aecdc5
AM
7897 if (rel + 1 == relend
7898 || (rel + 2 < relend
7899 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7900 ++rel;
7901
7902 if (rel == relend)
3f764659
JJ
7903 {
7904 if (sec->size == offset + 24)
7905 {
7906 need_pad = NULL;
7907 break;
7908 }
51aecdc5 7909 if (sec->size == offset + 16)
3f764659
JJ
7910 {
7911 cnt_16b++;
7912 break;
7913 }
7914 goto broken_opd;
7915 }
3f764659
JJ
7916 else if (rel + 1 < relend
7917 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7918 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7919 {
51aecdc5
AM
7920 if (rel[0].r_offset == offset + 16)
7921 cnt_16b++;
7922 else if (rel[0].r_offset != offset + 24)
7923 goto broken_opd;
3f764659
JJ
7924 }
7925 else
7926 goto broken_opd;
1e2f5b6e
AM
7927 }
7928
e7d1c40c 7929 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7930
51aecdc5 7931 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7932 {
7933 Elf_Internal_Rela *write_rel;
d4730f92 7934 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7935 bfd_byte *rptr, *wptr;
983bddc8 7936 bfd_byte *new_contents;
74f0fb50
AM
7937 bfd_size_type amt;
7938
983bddc8 7939 new_contents = NULL;
51aecdc5 7940 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7941 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7942 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7943 if (opd->adjust == NULL)
7944 return FALSE;
1e2f5b6e
AM
7945
7946 /* This seems a waste of time as input .opd sections are all
7947 zeros as generated by gcc, but I suppose there's no reason
7948 this will always be so. We might start putting something in
7949 the third word of .opd entries. */
7950 if ((sec->flags & SEC_IN_MEMORY) == 0)
7951 {
eea6121a
AM
7952 bfd_byte *loc;
7953 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7954 {
eea6121a
AM
7955 if (loc != NULL)
7956 free (loc);
50bc7936 7957 error_ret:
6cdc0ccc
AM
7958 if (local_syms != NULL
7959 && symtab_hdr->contents != (unsigned char *) local_syms)
7960 free (local_syms);
6cdc0ccc
AM
7961 if (elf_section_data (sec)->relocs != relstart)
7962 free (relstart);
b34976b6 7963 return FALSE;
6cdc0ccc 7964 }
1e2f5b6e
AM
7965 sec->contents = loc;
7966 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7967 }
7968
7969 elf_section_data (sec)->relocs = relstart;
7970
3f764659 7971 new_contents = sec->contents;
3f764659
JJ
7972 if (add_aux_fields)
7973 {
7974 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7975 if (new_contents == NULL)
7976 return FALSE;
51aecdc5 7977 need_pad = NULL;
3f764659 7978 }
b4f4e59f
AM
7979 wptr = new_contents;
7980 rptr = sec->contents;
1e2f5b6e 7981 write_rel = relstart;
51aecdc5 7982 for (rel = relstart; rel < relend; )
1e2f5b6e 7983 {
50bc7936
AM
7984 unsigned long r_symndx;
7985 asection *sym_sec;
7986 struct elf_link_hash_entry *h;
51aecdc5 7987 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7988 Elf_Internal_Sym *sym;
51aecdc5
AM
7989 long opd_ent_size;
7990 Elf_Internal_Rela *next_rel;
7991 bfd_boolean skip;
50bc7936
AM
7992
7993 r_symndx = ELF64_R_SYM (rel->r_info);
7994 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7995 r_symndx, ibfd))
50bc7936
AM
7996 goto error_ret;
7997
51aecdc5
AM
7998 next_rel = rel + 2;
7999 if (next_rel + 1 == relend
8000 || (next_rel + 2 < relend
8001 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8002 ++next_rel;
8003
8004 /* See if the .opd entry is full 24 byte or
8005 16 byte (with fd_aux entry overlapped with next
8006 fd_func). */
8007 opd_ent_size = 24;
8008 if (next_rel == relend)
1e2f5b6e 8009 {
51aecdc5 8010 if (sec->size == rel->r_offset + 16)
3f764659 8011 opd_ent_size = 16;
51aecdc5
AM
8012 }
8013 else if (next_rel->r_offset == rel->r_offset + 16)
8014 opd_ent_size = 16;
3f764659 8015
51aecdc5
AM
8016 if (h != NULL
8017 && h->root.root.string[0] == '.')
8018 {
8c5b4e52
AM
8019 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8020 if (fdh != NULL)
8021 {
8022 fdh = ppc_follow_link (fdh);
8023 if (fdh->elf.root.type != bfd_link_hash_defined
8024 && fdh->elf.root.type != bfd_link_hash_defweak)
8025 fdh = NULL;
8026 }
51aecdc5 8027 }
1e2f5b6e 8028
51aecdc5
AM
8029 skip = (sym_sec->owner != ibfd
8030 || sym_sec->output_section == bfd_abs_section_ptr);
8031 if (skip)
8032 {
8033 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8034 {
51aecdc5
AM
8035 /* Arrange for the function descriptor sym
8036 to be dropped. */
8037 fdh->elf.root.u.def.value = 0;
8038 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8039 }
51aecdc5 8040 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8041
0e1862bb 8042 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8043 rel = next_rel;
8044 else
8045 while (1)
8046 {
8047 if (!dec_dynrel_count (rel->r_info, sec, info,
8048 NULL, h, sym))
8049 goto error_ret;
754021d0 8050
51aecdc5
AM
8051 if (++rel == next_rel)
8052 break;
1e2f5b6e 8053
51aecdc5
AM
8054 r_symndx = ELF64_R_SYM (rel->r_info);
8055 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8056 r_symndx, ibfd))
8057 goto error_ret;
8058 }
50bc7936
AM
8059 }
8060 else
1e2f5b6e 8061 {
51aecdc5
AM
8062 /* We'll be keeping this opd entry. */
8063 long adjust;
8064
8065 if (fdh != NULL)
8066 {
8067 /* Redefine the function descriptor symbol to
8068 this location in the opd section. It is
8069 necessary to update the value here rather
8070 than using an array of adjustments as we do
8071 for local symbols, because various places
8072 in the generic ELF code use the value
8073 stored in u.def.value. */
8074 fdh->elf.root.u.def.value = wptr - new_contents;
8075 fdh->adjust_done = 1;
8076 }
8077
8078 /* Local syms are a bit tricky. We could
8079 tweak them as they can be cached, but
8080 we'd need to look through the local syms
8081 for the function descriptor sym which we
8082 don't have at the moment. So keep an
8083 array of adjustments. */
8084 adjust = (wptr - new_contents) - (rptr - sec->contents);
8085 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8086
8087 if (wptr != rptr)
8088 memcpy (wptr, rptr, opd_ent_size);
8089 wptr += opd_ent_size;
8090 if (add_aux_fields && opd_ent_size == 16)
8091 {
8092 memset (wptr, '\0', 8);
8093 wptr += 8;
8094 }
8095
50bc7936 8096 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8097 new opd entries. */
8098 for ( ; rel != next_rel; ++rel)
8099 {
8100 rel->r_offset += adjust;
8101 if (write_rel != rel)
8102 memcpy (write_rel, rel, sizeof (*rel));
8103 ++write_rel;
8104 }
1e2f5b6e 8105 }
51aecdc5
AM
8106
8107 rptr += opd_ent_size;
1e2f5b6e
AM
8108 }
8109
3f764659 8110 sec->size = wptr - new_contents;
1e2f5b6e 8111 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8112 if (add_aux_fields)
8113 {
8114 free (sec->contents);
8115 sec->contents = new_contents;
8116 }
8117
05bf9422 8118 /* Fudge the header size too, as this is used later in
cdcf6e38 8119 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8120 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8121 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8122 some_edited = TRUE;
1e2f5b6e 8123 }
6cdc0ccc 8124 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8125 free (relstart);
6cdc0ccc 8126
411e1bfb
AM
8127 if (local_syms != NULL
8128 && symtab_hdr->contents != (unsigned char *) local_syms)
8129 {
8130 if (!info->keep_memory)
8131 free (local_syms);
8132 else
8133 symtab_hdr->contents = (unsigned char *) local_syms;
8134 }
8135 }
8136
754021d0
AM
8137 if (some_edited)
8138 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8139
3f764659
JJ
8140 /* If we are doing a final link and the last .opd entry is just 16 byte
8141 long, add a 8 byte padding after it. */
0e1862bb 8142 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8143 {
8144 bfd_byte *p;
8145
8146 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8147 {
8148 BFD_ASSERT (need_pad->size > 0);
8149
8150 p = bfd_malloc (need_pad->size + 8);
8151 if (p == NULL)
8152 return FALSE;
699733f6 8153
3f764659
JJ
8154 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8155 p, 0, need_pad->size))
8156 return FALSE;
8157
8158 need_pad->contents = p;
8159 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8160 }
8161 else
8162 {
8163 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8164 if (p == NULL)
8165 return FALSE;
8166
8167 need_pad->contents = p;
8168 }
8169
8170 memset (need_pad->contents + need_pad->size, 0, 8);
8171 need_pad->size += 8;
8172 }
8173
411e1bfb
AM
8174 return TRUE;
8175}
8176
e1918d23 8177/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8178
e1918d23 8179asection *
e7d1c40c 8180ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8181{
411e1bfb
AM
8182 struct ppc_link_hash_table *htab;
8183
411e1bfb 8184 htab = ppc_hash_table (info);
4dfe6ac6
NC
8185 if (htab == NULL)
8186 return NULL;
8187
ee67d69a
AM
8188 if (abiversion (info->output_bfd) == 1)
8189 htab->opd_abi = 1;
8190
e7d1c40c 8191 if (htab->params->no_multi_toc)
33c0ec9d
AM
8192 htab->do_multi_toc = 0;
8193 else if (!htab->do_multi_toc)
e7d1c40c 8194 htab->params->no_multi_toc = 1;
33c0ec9d 8195
8b5f1ed8
AM
8196 /* Default to --no-plt-localentry, as this option can cause problems
8197 with symbol interposition. For example, glibc libpthread.so and
8198 libc.so duplicate many pthread symbols, with a fallback
8199 implementation in libc.so. In some cases the fallback does more
8200 work than the pthread implementation. __pthread_condattr_destroy
8201 is one such symbol: the libpthread.so implementation is
8202 localentry:0 while the libc.so implementation is localentry:8.
8203 An app that "cleverly" uses dlopen to only load necessary
8204 libraries at runtime may omit loading libpthread.so when not
8205 running multi-threaded, which then results in the libc.so
8206 fallback symbols being used and ld.so complaining. Now there
8207 are workarounds in ld (see non_zero_localentry) to detect the
8208 pthread situation, but that may not be the only case where
8209 --plt-localentry can cause trouble. */
f378ab09 8210 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8211 htab->params->plt_localentry0 = 0;
d44c746a
AM
8212 if (htab->params->plt_localentry0
8213 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8214 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
8215 _bfd_error_handler
8216 (_("warning: --plt-localentry is especially dangerous without "
8217 "ld.so support to detect ABI violations"));
f378ab09 8218
3a71aa26
AM
8219 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8220 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8221 FALSE, FALSE, TRUE));
a7f2871e
AM
8222 /* Move dynamic linking info to the function descriptor sym. */
8223 if (htab->tls_get_addr != NULL)
8224 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8225 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8226 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8227 FALSE, FALSE, TRUE));
7c9cf415 8228 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8229 {
8230 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8231
8232 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8233 FALSE, FALSE, TRUE);
8234 if (opt != NULL)
8235 func_desc_adjust (opt, info);
8236 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8237 FALSE, FALSE, TRUE);
8238 if (opt_fd != NULL
8239 && (opt_fd->root.type == bfd_link_hash_defined
8240 || opt_fd->root.type == bfd_link_hash_defweak))
8241 {
8242 /* If glibc supports an optimized __tls_get_addr call stub,
8243 signalled by the presence of __tls_get_addr_opt, and we'll
8244 be calling __tls_get_addr via a plt call stub, then
8245 make __tls_get_addr point to __tls_get_addr_opt. */
8246 tga_fd = &htab->tls_get_addr_fd->elf;
8247 if (htab->elf.dynamic_sections_created
8248 && tga_fd != NULL
8249 && (tga_fd->type == STT_FUNC
8250 || tga_fd->needs_plt)
8251 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8252 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8253 {
8254 struct plt_entry *ent;
8255
8256 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8257 if (ent->plt.refcount > 0)
8258 break;
8259 if (ent != NULL)
8260 {
8261 tga_fd->root.type = bfd_link_hash_indirect;
8262 tga_fd->root.u.i.link = &opt_fd->root;
8263 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8264 opt_fd->mark = 1;
a7f2871e
AM
8265 if (opt_fd->dynindx != -1)
8266 {
8267 /* Use __tls_get_addr_opt in dynamic relocations. */
8268 opt_fd->dynindx = -1;
8269 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8270 opt_fd->dynstr_index);
8271 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8272 return NULL;
a7f2871e
AM
8273 }
8274 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8275 tga = &htab->tls_get_addr->elf;
8276 if (opt != NULL && tga != NULL)
8277 {
8278 tga->root.type = bfd_link_hash_indirect;
8279 tga->root.u.i.link = &opt->root;
8280 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8281 opt->mark = 1;
a7f2871e
AM
8282 _bfd_elf_link_hash_hide_symbol (info, opt,
8283 tga->forced_local);
8284 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8285 }
8286 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8287 htab->tls_get_addr_fd->is_func_descriptor = 1;
8288 if (htab->tls_get_addr != NULL)
8289 {
8290 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8291 htab->tls_get_addr->is_func = 1;
8292 }
8293 }
8294 }
8295 }
7c9cf415
AM
8296 else if (htab->params->tls_get_addr_opt < 0)
8297 htab->params->tls_get_addr_opt = 0;
a7f2871e 8298 }
33c0ec9d 8299 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8300}
8387904d 8301
3a71aa26
AM
8302/* Return TRUE iff REL is a branch reloc with a global symbol matching
8303 HASH1 or HASH2. */
8387904d 8304
3a71aa26
AM
8305static bfd_boolean
8306branch_reloc_hash_match (const bfd *ibfd,
8307 const Elf_Internal_Rela *rel,
8308 const struct ppc_link_hash_entry *hash1,
8309 const struct ppc_link_hash_entry *hash2)
8310{
8311 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8312 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8313 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8314
e054468f 8315 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8316 {
3a71aa26
AM
8317 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8318 struct elf_link_hash_entry *h;
8387904d 8319
3a71aa26 8320 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8321 h = elf_follow_link (h);
3a71aa26
AM
8322 if (h == &hash1->elf || h == &hash2->elf)
8323 return TRUE;
a48ebf4d 8324 }
3a71aa26 8325 return FALSE;
951fd09b 8326}
411e1bfb 8327
951fd09b
AM
8328/* Run through all the TLS relocs looking for optimization
8329 opportunities. The linker has been hacked (see ppc64elf.em) to do
8330 a preliminary section layout so that we know the TLS segment
8331 offsets. We can't optimize earlier because some optimizations need
8332 to know the tp offset, and we need to optimize before allocating
8333 dynamic relocations. */
8334
8335bfd_boolean
33c0ec9d 8336ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8337{
8338 bfd *ibfd;
8339 asection *sec;
8340 struct ppc_link_hash_table *htab;
663a1470 8341 unsigned char *toc_ref;
102890f0 8342 int pass;
951fd09b 8343
3cbc1e5e 8344 if (!bfd_link_executable (info))
411e1bfb
AM
8345 return TRUE;
8346
951fd09b 8347 htab = ppc_hash_table (info);
4dfe6ac6
NC
8348 if (htab == NULL)
8349 return FALSE;
8350
663a1470
AM
8351 /* Make two passes over the relocs. On the first pass, mark toc
8352 entries involved with tls relocs, and check that tls relocs
8353 involved in setting up a tls_get_addr call are indeed followed by
8354 such a call. If they are not, we can't do any tls optimization.
8355 On the second pass twiddle tls_mask flags to notify
8356 relocate_section that optimization can be done, and adjust got
8357 and plt refcounts. */
8358 toc_ref = NULL;
8359 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8360 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8361 {
8362 Elf_Internal_Sym *locsyms = NULL;
8363 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8364
102890f0
AM
8365 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8366 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8367 {
8368 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8369 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8370
102890f0
AM
8371 /* Read the relocations. */
8372 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8373 info->keep_memory);
8374 if (relstart == NULL)
2915c55b
JK
8375 {
8376 free (toc_ref);
8377 return FALSE;
8378 }
411e1bfb 8379
102890f0
AM
8380 relend = relstart + sec->reloc_count;
8381 for (rel = relstart; rel < relend; rel++)
8382 {
8383 enum elf_ppc64_reloc_type r_type;
8384 unsigned long r_symndx;
8385 struct elf_link_hash_entry *h;
8386 Elf_Internal_Sym *sym;
8387 asection *sym_sec;
f961d9dd
AM
8388 unsigned char *tls_mask;
8389 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8390 bfd_vma value;
8391 bfd_boolean ok_tprel, is_local;
8392 long toc_ref_index = 0;
8393 int expecting_tls_get_addr = 0;
663a1470 8394 bfd_boolean ret = FALSE;
411e1bfb 8395
102890f0
AM
8396 r_symndx = ELF64_R_SYM (rel->r_info);
8397 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8398 r_symndx, ibfd))
8399 {
8400 err_free_rel:
8401 if (elf_section_data (sec)->relocs != relstart)
8402 free (relstart);
8403 if (toc_ref != NULL)
8404 free (toc_ref);
8405 if (locsyms != NULL
0ffa91dd 8406 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8407 != (unsigned char *) locsyms))
8408 free (locsyms);
663a1470 8409 return ret;
102890f0 8410 }
411e1bfb 8411
102890f0
AM
8412 if (h != NULL)
8413 {
766bc656
AM
8414 if (h->root.type == bfd_link_hash_defined
8415 || h->root.type == bfd_link_hash_defweak)
8416 value = h->root.u.def.value;
8417 else if (h->root.type == bfd_link_hash_undefweak)
8418 value = 0;
8419 else
663a1470
AM
8420 {
8421 found_tls_get_addr_arg = 0;
8422 continue;
8423 }
102890f0
AM
8424 }
8425 else
8426 /* Symbols referenced by TLS relocs must be of type
8427 STT_TLS. So no need for .opd local sym adjust. */
8428 value = sym->st_value;
8429
8430 ok_tprel = FALSE;
8431 is_local = FALSE;
8432 if (h == NULL
8433 || !h->def_dynamic)
8434 {
8435 is_local = TRUE;
766bc656
AM
8436 if (h != NULL
8437 && h->root.type == bfd_link_hash_undefweak)
8438 ok_tprel = TRUE;
c27b8c2a
AM
8439 else if (sym_sec != NULL
8440 && sym_sec->output_section != NULL)
766bc656
AM
8441 {
8442 value += sym_sec->output_offset;
8443 value += sym_sec->output_section->vma;
8444 value -= htab->elf.tls_sec->vma;
8445 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8446 < (bfd_vma) 1 << 32);
8447 }
102890f0 8448 }
951fd09b 8449
102890f0 8450 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8451 /* If this section has old-style __tls_get_addr calls
8452 without marker relocs, then check that each
8453 __tls_get_addr call reloc is preceded by a reloc
8454 that conceivably belongs to the __tls_get_addr arg
8455 setup insn. If we don't find matching arg setup
8456 relocs, don't do any tls optimization. */
8457 if (pass == 0
8458 && sec->has_tls_get_addr_call
8459 && h != NULL
8460 && (h == &htab->tls_get_addr->elf
8461 || h == &htab->tls_get_addr_fd->elf)
8462 && !found_tls_get_addr_arg
8463 && is_branch_reloc (r_type))
8464 {
25f53a85 8465 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8466 "TLS optimization disabled\n"),
8467 ibfd, sec, rel->r_offset);
8468 ret = TRUE;
8469 goto err_free_rel;
8470 }
8471
8472 found_tls_get_addr_arg = 0;
102890f0
AM
8473 switch (r_type)
8474 {
8475 case R_PPC64_GOT_TLSLD16:
8476 case R_PPC64_GOT_TLSLD16_LO:
8477 expecting_tls_get_addr = 1;
663a1470 8478 found_tls_get_addr_arg = 1;
1a0670f3 8479 /* Fall through. */
102890f0
AM
8480
8481 case R_PPC64_GOT_TLSLD16_HI:
8482 case R_PPC64_GOT_TLSLD16_HA:
8483 /* These relocs should never be against a symbol
8484 defined in a shared lib. Leave them alone if
8485 that turns out to be the case. */
8486 if (!is_local)
8487 continue;
411e1bfb 8488
102890f0 8489 /* LD -> LE */
411e1bfb 8490 tls_set = 0;
102890f0
AM
8491 tls_clear = TLS_LD;
8492 tls_type = TLS_TLS | TLS_LD;
8493 break;
411e1bfb 8494
102890f0
AM
8495 case R_PPC64_GOT_TLSGD16:
8496 case R_PPC64_GOT_TLSGD16_LO:
8497 expecting_tls_get_addr = 1;
663a1470 8498 found_tls_get_addr_arg = 1;
1a0670f3 8499 /* Fall through. */
102890f0
AM
8500
8501 case R_PPC64_GOT_TLSGD16_HI:
8502 case R_PPC64_GOT_TLSGD16_HA:
8503 if (ok_tprel)
8504 /* GD -> LE */
411e1bfb 8505 tls_set = 0;
102890f0
AM
8506 else
8507 /* GD -> IE */
8508 tls_set = TLS_TLS | TLS_TPRELGD;
8509 tls_clear = TLS_GD;
8510 tls_type = TLS_TLS | TLS_GD;
8511 break;
8512
8513 case R_PPC64_GOT_TPREL16_DS:
8514 case R_PPC64_GOT_TPREL16_LO_DS:
8515 case R_PPC64_GOT_TPREL16_HI:
8516 case R_PPC64_GOT_TPREL16_HA:
8517 if (ok_tprel)
8518 {
8519 /* IE -> LE */
8520 tls_set = 0;
8521 tls_clear = TLS_TPREL;
8522 tls_type = TLS_TLS | TLS_TPREL;
8523 break;
8524 }
411e1bfb
AM
8525 continue;
8526
727fc41e
AM
8527 case R_PPC64_TLSGD:
8528 case R_PPC64_TLSLD:
663a1470 8529 found_tls_get_addr_arg = 1;
1a0670f3 8530 /* Fall through. */
663a1470
AM
8531
8532 case R_PPC64_TLS:
8533 case R_PPC64_TOC16:
8534 case R_PPC64_TOC16_LO:
102890f0
AM
8535 if (sym_sec == NULL || sym_sec != toc)
8536 continue;
8537
8538 /* Mark this toc entry as referenced by a TLS
8539 code sequence. We can do that now in the
8540 case of R_PPC64_TLS, and after checking for
8541 tls_get_addr for the TOC16 relocs. */
8542 if (toc_ref == NULL)
663a1470
AM
8543 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8544 if (toc_ref == NULL)
8545 goto err_free_rel;
8546
102890f0
AM
8547 if (h != NULL)
8548 value = h->root.u.def.value;
8549 else
8550 value = sym->st_value;
8551 value += rel->r_addend;
73242275
AM
8552 if (value % 8 != 0)
8553 continue;
8554 BFD_ASSERT (value < toc->size
8555 && toc->output_offset % 8 == 0);
663a1470 8556 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8557 if (r_type == R_PPC64_TLS
8558 || r_type == R_PPC64_TLSGD
8559 || r_type == R_PPC64_TLSLD)
102890f0
AM
8560 {
8561 toc_ref[toc_ref_index] = 1;
8562 continue;
8563 }
8564
8565 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8566 continue;
8567
8568 tls_set = 0;
8569 tls_clear = 0;
8570 expecting_tls_get_addr = 2;
8571 break;
8572
8573 case R_PPC64_TPREL64:
8574 if (pass == 0
8575 || sec != toc
8576 || toc_ref == NULL
663a1470 8577 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8578 continue;
8579 if (ok_tprel)
8580 {
8581 /* IE -> LE */
8582 tls_set = TLS_EXPLICIT;
8583 tls_clear = TLS_TPREL;
8584 break;
8585 }
8586 continue;
8587
8588 case R_PPC64_DTPMOD64:
8589 if (pass == 0
8590 || sec != toc
8591 || toc_ref == NULL
663a1470 8592 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8593 continue;
8594 if (rel + 1 < relend
8595 && (rel[1].r_info
8596 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8597 && rel[1].r_offset == rel->r_offset + 8)
8598 {
8599 if (ok_tprel)
8600 /* GD -> LE */
8601 tls_set = TLS_EXPLICIT | TLS_GD;
8602 else
8603 /* GD -> IE */
8604 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8605 tls_clear = TLS_GD;
8606 }
8607 else
8608 {
8609 if (!is_local)
8610 continue;
8611
8612 /* LD -> LE */
8613 tls_set = TLS_EXPLICIT;
8614 tls_clear = TLS_LD;
8615 }
8616 break;
8617
8618 default:
8619 continue;
8620 }
8621
8622 if (pass == 0)
8623 {
727fc41e
AM
8624 if (!expecting_tls_get_addr
8625 || !sec->has_tls_get_addr_call)
102890f0
AM
8626 continue;
8627
3a71aa26
AM
8628 if (rel + 1 < relend
8629 && branch_reloc_hash_match (ibfd, rel + 1,
8630 htab->tls_get_addr,
8631 htab->tls_get_addr_fd))
102890f0 8632 {
3a71aa26 8633 if (expecting_tls_get_addr == 2)
102890f0 8634 {
3a71aa26 8635 /* Check for toc tls entries. */
f961d9dd 8636 unsigned char *toc_tls;
3a71aa26
AM
8637 int retval;
8638
8639 retval = get_tls_mask (&toc_tls, NULL, NULL,
8640 &locsyms,
8641 rel, ibfd);
8642 if (retval == 0)
8643 goto err_free_rel;
663a1470
AM
8644 if (toc_tls != NULL)
8645 {
8646 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8647 found_tls_get_addr_arg = 1;
8648 if (retval > 1)
8649 toc_ref[toc_ref_index] = 1;
8650 }
102890f0 8651 }
3a71aa26 8652 continue;
102890f0
AM
8653 }
8654
8655 if (expecting_tls_get_addr != 1)
8656 continue;
8657
8658 /* Uh oh, we didn't find the expected call. We
8659 could just mark this symbol to exclude it
8660 from tls optimization but it's safer to skip
663a1470 8661 the entire optimization. */
695344c0 8662 /* xgettext:c-format */
25f53a85 8663 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8664 "TLS optimization disabled\n"),
8665 ibfd, sec, rel->r_offset);
8666 ret = TRUE;
8667 goto err_free_rel;
102890f0
AM
8668 }
8669
85f7a9cb 8670 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8671 {
8672 struct plt_entry *ent;
8673 for (ent = htab->tls_get_addr->elf.plt.plist;
8674 ent != NULL;
8675 ent = ent->next)
8676 if (ent->addend == 0)
411e1bfb 8677 {
102890f0 8678 if (ent->plt.refcount > 0)
30038c59 8679 {
102890f0
AM
8680 ent->plt.refcount -= 1;
8681 expecting_tls_get_addr = 0;
30038c59 8682 }
102890f0 8683 break;
411e1bfb 8684 }
102890f0 8685 }
411e1bfb 8686
85f7a9cb 8687 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8688 {
8689 struct plt_entry *ent;
8690 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8691 ent != NULL;
8692 ent = ent->next)
8693 if (ent->addend == 0)
411e1bfb 8694 {
102890f0
AM
8695 if (ent->plt.refcount > 0)
8696 ent->plt.refcount -= 1;
8697 break;
411e1bfb 8698 }
102890f0 8699 }
411e1bfb 8700
102890f0 8701 if (tls_clear == 0)
30038c59
AM
8702 continue;
8703
102890f0
AM
8704 if ((tls_set & TLS_EXPLICIT) == 0)
8705 {
8706 struct got_entry *ent;
411e1bfb 8707
102890f0
AM
8708 /* Adjust got entry for this reloc. */
8709 if (h != NULL)
8710 ent = h->got.glist;
8711 else
8712 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8713
102890f0
AM
8714 for (; ent != NULL; ent = ent->next)
8715 if (ent->addend == rel->r_addend
8716 && ent->owner == ibfd
8717 && ent->tls_type == tls_type)
8718 break;
8719 if (ent == NULL)
8720 abort ();
411e1bfb 8721
102890f0
AM
8722 if (tls_set == 0)
8723 {
8724 /* We managed to get rid of a got entry. */
8725 if (ent->got.refcount > 0)
8726 ent->got.refcount -= 1;
8727 }
8728 }
8729 else
8730 {
8731 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8732 we'll lose one or two dyn relocs. */
8733 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8734 NULL, h, sym))
102890f0 8735 return FALSE;
411e1bfb 8736
102890f0
AM
8737 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8738 {
8739 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8740 NULL, h, sym))
102890f0
AM
8741 return FALSE;
8742 }
8743 }
411e1bfb 8744
102890f0
AM
8745 *tls_mask |= tls_set;
8746 *tls_mask &= ~tls_clear;
8747 }
8c1d1bb8 8748
102890f0
AM
8749 if (elf_section_data (sec)->relocs != relstart)
8750 free (relstart);
8751 }
411e1bfb 8752
663a1470
AM
8753 if (locsyms != NULL
8754 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8755 {
8756 if (!info->keep_memory)
8757 free (locsyms);
8758 else
8759 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8760 }
8761 }
411e1bfb 8762
663a1470
AM
8763 if (toc_ref != NULL)
8764 free (toc_ref);
9a23f96e 8765 htab->do_tls_opt = 1;
b34976b6 8766 return TRUE;
1e2f5b6e 8767}
b34976b6 8768
c5614fa4
AM
8769/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8770 the values of any global symbols in a toc section that has been
8771 edited. Globals in toc sections should be a rarity, so this function
8772 sets a flag if any are found in toc sections other than the one just
de194d85 8773 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8774
8775struct adjust_toc_info
8776{
8777 asection *toc;
8778 unsigned long *skip;
8779 bfd_boolean global_toc_syms;
8780};
8781
ba761f19
AM
8782enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8783
c5614fa4
AM
8784static bfd_boolean
8785adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8786{
8787 struct ppc_link_hash_entry *eh;
8788 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8789 unsigned long i;
c5614fa4 8790
c5614fa4
AM
8791 if (h->root.type != bfd_link_hash_defined
8792 && h->root.type != bfd_link_hash_defweak)
8793 return TRUE;
8794
8795 eh = (struct ppc_link_hash_entry *) h;
8796 if (eh->adjust_done)
8797 return TRUE;
8798
8799 if (eh->elf.root.u.def.section == toc_inf->toc)
8800 {
854b41e7
AM
8801 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8802 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8803 else
854b41e7
AM
8804 i = eh->elf.root.u.def.value >> 3;
8805
ba761f19 8806 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8807 {
4eca0228 8808 _bfd_error_handler
854b41e7
AM
8809 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8810 do
8811 ++i;
ba761f19 8812 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8813 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8814 }
854b41e7
AM
8815
8816 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8817 eh->adjust_done = 1;
8818 }
8819 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8820 toc_inf->global_toc_syms = TRUE;
8821
8822 return TRUE;
8823}
8824
39eeab25
AM
8825/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8826 on a _LO variety toc/got reloc. */
560c8763
AM
8827
8828static bfd_boolean
39eeab25 8829ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8830{
39eeab25
AM
8831 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8832 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8833 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8834 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8835 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8836 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8837 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8838 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8839 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8840 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8841 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8842 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8843 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8844 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8845 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8846 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8847 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8848 /* Exclude lfqu by testing reloc. If relocs are ever
8849 defined for the reduced D field in psq_lu then those
8850 will need testing too. */
8851 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8852 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8853 && (insn & 1) == 0)
8854 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8855 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8856 /* Exclude stfqu. psq_stu as above for psq_lu. */
8857 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8858 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8859 && (insn & 1) == 0));
560c8763
AM
8860}
8861
c5614fa4
AM
8862/* Examine all relocs referencing .toc sections in order to remove
8863 unused .toc entries. */
8864
8865bfd_boolean
33c0ec9d 8866ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8867{
8868 bfd *ibfd;
8869 struct adjust_toc_info toc_inf;
67f0cbdb 8870 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8871
67f0cbdb 8872 htab->do_toc_opt = 1;
c5614fa4 8873 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8874 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8875 {
8876 asection *toc, *sec;
8877 Elf_Internal_Shdr *symtab_hdr;
8878 Elf_Internal_Sym *local_syms;
425b145b 8879 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8880 unsigned long *skip, *drop;
8881 unsigned char *used;
8882 unsigned char *keep, last, some_unused;
8883
854b41e7
AM
8884 if (!is_ppc64_elf (ibfd))
8885 continue;
8886
c5614fa4
AM
8887 toc = bfd_get_section_by_name (ibfd, ".toc");
8888 if (toc == NULL
92b7a70f 8889 || toc->size == 0
dbaa2011
AM
8890 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8891 || discarded_section (toc))
c5614fa4
AM
8892 continue;
8893
425b145b 8894 toc_relocs = NULL;
c5614fa4 8895 local_syms = NULL;
0ffa91dd 8896 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8897
8898 /* Look at sections dropped from the final link. */
8899 skip = NULL;
8900 relstart = NULL;
8901 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8902 {
8903 if (sec->reloc_count == 0
dbaa2011 8904 || !discarded_section (sec)
c5614fa4
AM
8905 || get_opd_info (sec)
8906 || (sec->flags & SEC_ALLOC) == 0
8907 || (sec->flags & SEC_DEBUGGING) != 0)
8908 continue;
8909
8910 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8911 if (relstart == NULL)
8912 goto error_ret;
8913
8914 /* Run through the relocs to see which toc entries might be
8915 unused. */
8916 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8917 {
8918 enum elf_ppc64_reloc_type r_type;
8919 unsigned long r_symndx;
8920 asection *sym_sec;
8921 struct elf_link_hash_entry *h;
8922 Elf_Internal_Sym *sym;
8923 bfd_vma val;
8924
8925 r_type = ELF64_R_TYPE (rel->r_info);
8926 switch (r_type)
8927 {
8928 default:
8929 continue;
8930
8931 case R_PPC64_TOC16:
8932 case R_PPC64_TOC16_LO:
8933 case R_PPC64_TOC16_HI:
8934 case R_PPC64_TOC16_HA:
8935 case R_PPC64_TOC16_DS:
8936 case R_PPC64_TOC16_LO_DS:
8937 break;
8938 }
8939
8940 r_symndx = ELF64_R_SYM (rel->r_info);
8941 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8942 r_symndx, ibfd))
8943 goto error_ret;
8944
8945 if (sym_sec != toc)
8946 continue;
8947
8948 if (h != NULL)
8949 val = h->root.u.def.value;
8950 else
8951 val = sym->st_value;
8952 val += rel->r_addend;
8953
8954 if (val >= toc->size)
8955 continue;
8956
8957 /* Anything in the toc ought to be aligned to 8 bytes.
8958 If not, don't mark as unused. */
8959 if (val & 7)
8960 continue;
8961
8962 if (skip == NULL)
8963 {
854b41e7 8964 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8965 if (skip == NULL)
8966 goto error_ret;
8967 }
8968
ba761f19 8969 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8970 }
8971
8972 if (elf_section_data (sec)->relocs != relstart)
8973 free (relstart);
8974 }
8975
ba761f19
AM
8976 /* For largetoc loads of address constants, we can convert
8977 . addis rx,2,addr@got@ha
8978 . ld ry,addr@got@l(rx)
8979 to
8980 . addis rx,2,addr@toc@ha
8981 . addi ry,rx,addr@toc@l
8982 when addr is within 2G of the toc pointer. This then means
8983 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8984
ba761f19
AM
8985 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8986 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8987 && toc->reloc_count != 0)
8988 {
8989 /* Read toc relocs. */
425b145b
AM
8990 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8991 info->keep_memory);
8992 if (toc_relocs == NULL)
ba761f19
AM
8993 goto error_ret;
8994
425b145b 8995 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8996 {
8997 enum elf_ppc64_reloc_type r_type;
8998 unsigned long r_symndx;
8999 asection *sym_sec;
9000 struct elf_link_hash_entry *h;
9001 Elf_Internal_Sym *sym;
9002 bfd_vma val, addr;
9003
9004 r_type = ELF64_R_TYPE (rel->r_info);
9005 if (r_type != R_PPC64_ADDR64)
9006 continue;
9007
9008 r_symndx = ELF64_R_SYM (rel->r_info);
9009 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9010 r_symndx, ibfd))
9011 goto error_ret;
9012
425b145b 9013 if (sym_sec == NULL
c27b8c2a 9014 || sym_sec->output_section == NULL
dbaa2011 9015 || discarded_section (sym_sec))
425b145b
AM
9016 continue;
9017
afe397ea 9018 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9019 continue;
9020
9021 if (h != NULL)
bddc25c9
AM
9022 {
9023 if (h->type == STT_GNU_IFUNC)
9024 continue;
9025 val = h->root.u.def.value;
9026 }
ba761f19 9027 else
bddc25c9
AM
9028 {
9029 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9030 continue;
9031 val = sym->st_value;
9032 }
ba761f19
AM
9033 val += rel->r_addend;
9034 val += sym_sec->output_section->vma + sym_sec->output_offset;
9035
9036 /* We don't yet know the exact toc pointer value, but we
9037 know it will be somewhere in the toc section. Don't
9038 optimize if the difference from any possible toc
9039 pointer is outside [ff..f80008000, 7fff7fff]. */
9040 addr = toc->output_section->vma + TOC_BASE_OFF;
9041 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9042 continue;
9043
9044 addr = toc->output_section->vma + toc->output_section->rawsize;
9045 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9046 continue;
9047
9048 if (skip == NULL)
9049 {
9050 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9051 if (skip == NULL)
9052 goto error_ret;
9053 }
9054
9055 skip[rel->r_offset >> 3]
425b145b 9056 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9057 }
ba761f19
AM
9058 }
9059
c5614fa4
AM
9060 if (skip == NULL)
9061 continue;
9062
9063 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9064 if (used == NULL)
9065 {
9066 error_ret:
9067 if (local_syms != NULL
9068 && symtab_hdr->contents != (unsigned char *) local_syms)
9069 free (local_syms);
9070 if (sec != NULL
9071 && relstart != NULL
9072 && elf_section_data (sec)->relocs != relstart)
9073 free (relstart);
425b145b
AM
9074 if (toc_relocs != NULL
9075 && elf_section_data (toc)->relocs != toc_relocs)
9076 free (toc_relocs);
c5614fa4
AM
9077 if (skip != NULL)
9078 free (skip);
9079 return FALSE;
9080 }
9081
30038c59
AM
9082 /* Now check all kept sections that might reference the toc.
9083 Check the toc itself last. */
9084 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9085 : ibfd->sections);
c5614fa4 9086 sec != NULL;
c5614fa4 9087 sec = (sec == toc ? NULL
c5614fa4 9088 : sec->next == NULL ? toc
30038c59 9089 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9090 : sec->next))
9091 {
9092 int repeat;
9093
9094 if (sec->reloc_count == 0
dbaa2011 9095 || discarded_section (sec)
c5614fa4
AM
9096 || get_opd_info (sec)
9097 || (sec->flags & SEC_ALLOC) == 0
9098 || (sec->flags & SEC_DEBUGGING) != 0)
9099 continue;
9100
854b41e7
AM
9101 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9102 info->keep_memory);
c5614fa4 9103 if (relstart == NULL)
2915c55b
JK
9104 {
9105 free (used);
9106 goto error_ret;
9107 }
c5614fa4
AM
9108
9109 /* Mark toc entries referenced as used. */
c5614fa4 9110 do
d4f1ee75
AM
9111 {
9112 repeat = 0;
9113 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9114 {
9115 enum elf_ppc64_reloc_type r_type;
9116 unsigned long r_symndx;
9117 asection *sym_sec;
9118 struct elf_link_hash_entry *h;
9119 Elf_Internal_Sym *sym;
9120 bfd_vma val;
9121 enum {no_check, check_lo, check_ha} insn_check;
98528052 9122
d4f1ee75
AM
9123 r_type = ELF64_R_TYPE (rel->r_info);
9124 switch (r_type)
9125 {
9126 default:
9127 insn_check = no_check;
9128 break;
98528052 9129
d4f1ee75
AM
9130 case R_PPC64_GOT_TLSLD16_HA:
9131 case R_PPC64_GOT_TLSGD16_HA:
9132 case R_PPC64_GOT_TPREL16_HA:
9133 case R_PPC64_GOT_DTPREL16_HA:
9134 case R_PPC64_GOT16_HA:
9135 case R_PPC64_TOC16_HA:
9136 insn_check = check_ha;
9137 break;
98528052 9138
d4f1ee75
AM
9139 case R_PPC64_GOT_TLSLD16_LO:
9140 case R_PPC64_GOT_TLSGD16_LO:
9141 case R_PPC64_GOT_TPREL16_LO_DS:
9142 case R_PPC64_GOT_DTPREL16_LO_DS:
9143 case R_PPC64_GOT16_LO:
9144 case R_PPC64_GOT16_LO_DS:
9145 case R_PPC64_TOC16_LO:
9146 case R_PPC64_TOC16_LO_DS:
9147 insn_check = check_lo;
9148 break;
9149 }
560c8763 9150
d4f1ee75
AM
9151 if (insn_check != no_check)
9152 {
9153 bfd_vma off = rel->r_offset & ~3;
9154 unsigned char buf[4];
9155 unsigned int insn;
c5614fa4 9156
d4f1ee75
AM
9157 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9158 {
9159 free (used);
9160 goto error_ret;
9161 }
9162 insn = bfd_get_32 (ibfd, buf);
9163 if (insn_check == check_lo
39eeab25 9164 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9165 : ((insn & ((0x3f << 26) | 0x1f << 16))
9166 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9167 {
9168 char str[12];
9169
9170 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9171 sprintf (str, "%#08x", insn);
9172 info->callbacks->einfo
695344c0 9173 /* xgettext:c-format */
174d0a74 9174 (_("%H: toc optimization is not supported for"
cf97bcb0 9175 " %s instruction\n"),
d4f1ee75
AM
9176 ibfd, sec, rel->r_offset & ~3, str);
9177 }
9178 }
c5614fa4 9179
d4f1ee75
AM
9180 switch (r_type)
9181 {
9182 case R_PPC64_TOC16:
9183 case R_PPC64_TOC16_LO:
9184 case R_PPC64_TOC16_HI:
9185 case R_PPC64_TOC16_HA:
9186 case R_PPC64_TOC16_DS:
9187 case R_PPC64_TOC16_LO_DS:
9188 /* In case we're taking addresses of toc entries. */
9189 case R_PPC64_ADDR64:
9190 break;
c5614fa4 9191
d4f1ee75
AM
9192 default:
9193 continue;
9194 }
c5614fa4 9195
d4f1ee75
AM
9196 r_symndx = ELF64_R_SYM (rel->r_info);
9197 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9198 r_symndx, ibfd))
9199 {
9200 free (used);
9201 goto error_ret;
9202 }
c5614fa4 9203
d4f1ee75
AM
9204 if (sym_sec != toc)
9205 continue;
c5614fa4 9206
d4f1ee75
AM
9207 if (h != NULL)
9208 val = h->root.u.def.value;
9209 else
9210 val = sym->st_value;
9211 val += rel->r_addend;
ba761f19 9212
d4f1ee75
AM
9213 if (val >= toc->size)
9214 continue;
ba761f19 9215
d4f1ee75
AM
9216 if ((skip[val >> 3] & can_optimize) != 0)
9217 {
9218 bfd_vma off;
9219 unsigned char opc;
9220
9221 switch (r_type)
9222 {
9223 case R_PPC64_TOC16_HA:
ba761f19 9224 break;
ba761f19 9225
d4f1ee75
AM
9226 case R_PPC64_TOC16_LO_DS:
9227 off = rel->r_offset;
9228 off += (bfd_big_endian (ibfd) ? -2 : 3);
9229 if (!bfd_get_section_contents (ibfd, sec, &opc,
9230 off, 1))
9231 {
9232 free (used);
9233 goto error_ret;
9234 }
9235 if ((opc & (0x3f << 2)) == (58u << 2))
9236 break;
1a0670f3 9237 /* Fall through. */
ba761f19 9238
d4f1ee75
AM
9239 default:
9240 /* Wrong sort of reloc, or not a ld. We may
9241 as well clear ref_from_discarded too. */
9242 skip[val >> 3] = 0;
9243 }
9244 }
9245
9246 if (sec != toc)
9247 used[val >> 3] = 1;
9248 /* For the toc section, we only mark as used if this
9249 entry itself isn't unused. */
9250 else if ((used[rel->r_offset >> 3]
9251 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9252 && !used[val >> 3])
9253 {
9254 /* Do all the relocs again, to catch reference
9255 chains. */
9256 repeat = 1;
9257 used[val >> 3] = 1;
9258 }
9259 }
9260 }
c5614fa4 9261 while (repeat);
854b41e7
AM
9262
9263 if (elf_section_data (sec)->relocs != relstart)
9264 free (relstart);
c5614fa4
AM
9265 }
9266
9267 /* Merge the used and skip arrays. Assume that TOC
9268 doublewords not appearing as either used or unused belong
de194d85 9269 to an entry more than one doubleword in size. */
c5614fa4
AM
9270 for (drop = skip, keep = used, last = 0, some_unused = 0;
9271 drop < skip + (toc->size + 7) / 8;
9272 ++drop, ++keep)
9273 {
9274 if (*keep)
9275 {
ba761f19
AM
9276 *drop &= ~ref_from_discarded;
9277 if ((*drop & can_optimize) != 0)
9278 some_unused = 1;
c5614fa4
AM
9279 last = 0;
9280 }
b140b010 9281 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9282 {
9283 some_unused = 1;
ba761f19 9284 last = ref_from_discarded;
c5614fa4
AM
9285 }
9286 else
9287 *drop = last;
9288 }
9289
9290 free (used);
9291
9292 if (some_unused)
9293 {
9294 bfd_byte *contents, *src;
9295 unsigned long off;
d62b3684 9296 Elf_Internal_Sym *sym;
ba761f19 9297 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9298
9299 /* Shuffle the toc contents, and at the same time convert the
9300 skip array from booleans into offsets. */
9301 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9302 goto error_ret;
9303
9304 elf_section_data (toc)->this_hdr.contents = contents;
9305
9306 for (src = contents, off = 0, drop = skip;
9307 src < contents + toc->size;
9308 src += 8, ++drop)
9309 {
ba761f19
AM
9310 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9311 off += 8;
c5614fa4
AM
9312 else if (off != 0)
9313 {
9314 *drop = off;
9315 memcpy (src - off, src, 8);
9316 }
9317 }
854b41e7 9318 *drop = off;
c5614fa4
AM
9319 toc->rawsize = toc->size;
9320 toc->size = src - contents - off;
9321
ba761f19
AM
9322 /* Adjust addends for relocs against the toc section sym,
9323 and optimize any accesses we can. */
c5614fa4
AM
9324 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9325 {
9326 if (sec->reloc_count == 0
dbaa2011 9327 || discarded_section (sec))
c5614fa4
AM
9328 continue;
9329
9330 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9331 info->keep_memory);
c5614fa4
AM
9332 if (relstart == NULL)
9333 goto error_ret;
9334
9335 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9336 {
9337 enum elf_ppc64_reloc_type r_type;
9338 unsigned long r_symndx;
9339 asection *sym_sec;
9340 struct elf_link_hash_entry *h;
854b41e7 9341 bfd_vma val;
c5614fa4
AM
9342
9343 r_type = ELF64_R_TYPE (rel->r_info);
9344 switch (r_type)
9345 {
9346 default:
9347 continue;
9348
9349 case R_PPC64_TOC16:
9350 case R_PPC64_TOC16_LO:
9351 case R_PPC64_TOC16_HI:
9352 case R_PPC64_TOC16_HA:
9353 case R_PPC64_TOC16_DS:
9354 case R_PPC64_TOC16_LO_DS:
9355 case R_PPC64_ADDR64:
9356 break;
9357 }
9358
9359 r_symndx = ELF64_R_SYM (rel->r_info);
9360 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9361 r_symndx, ibfd))
9362 goto error_ret;
9363
ba761f19 9364 if (sym_sec != toc)
c5614fa4
AM
9365 continue;
9366
ba761f19
AM
9367 if (h != NULL)
9368 val = h->root.u.def.value;
9369 else
9370 {
9371 val = sym->st_value;
9372 if (val != 0)
9373 local_toc_syms = TRUE;
9374 }
9375
9376 val += rel->r_addend;
854b41e7
AM
9377
9378 if (val > toc->rawsize)
9379 val = toc->rawsize;
ba761f19
AM
9380 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9381 continue;
9382 else if ((skip[val >> 3] & can_optimize) != 0)
9383 {
9384 Elf_Internal_Rela *tocrel
425b145b 9385 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9386 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9387
9388 switch (r_type)
9389 {
9390 case R_PPC64_TOC16_HA:
9391 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9392 break;
9393
9394 case R_PPC64_TOC16_LO_DS:
9395 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9396 break;
9397
9398 default:
28942f62
AM
9399 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9400 ppc_howto_init ();
b140b010 9401 info->callbacks->einfo
695344c0 9402 /* xgettext:c-format */
174d0a74 9403 (_("%H: %s references "
b140b010
AM
9404 "optimized away TOC entry\n"),
9405 ibfd, sec, rel->r_offset,
9406 ppc64_elf_howto_table[r_type]->name);
9407 bfd_set_error (bfd_error_bad_value);
9408 goto error_ret;
ba761f19
AM
9409 }
9410 rel->r_addend = tocrel->r_addend;
9411 elf_section_data (sec)->relocs = relstart;
9412 continue;
9413 }
9414
9415 if (h != NULL || sym->st_value != 0)
9416 continue;
854b41e7
AM
9417
9418 rel->r_addend -= skip[val >> 3];
9419 elf_section_data (sec)->relocs = relstart;
c5614fa4 9420 }
854b41e7
AM
9421
9422 if (elf_section_data (sec)->relocs != relstart)
9423 free (relstart);
c5614fa4
AM
9424 }
9425
9426 /* We shouldn't have local or global symbols defined in the TOC,
9427 but handle them anyway. */
df22d223
AM
9428 if (local_syms != NULL)
9429 for (sym = local_syms;
9430 sym < local_syms + symtab_hdr->sh_info;
9431 ++sym)
9432 if (sym->st_value != 0
9433 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9434 {
9435 unsigned long i;
854b41e7 9436
df22d223
AM
9437 if (sym->st_value > toc->rawsize)
9438 i = toc->rawsize >> 3;
9439 else
9440 i = sym->st_value >> 3;
854b41e7 9441
df22d223
AM
9442 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9443 {
9444 if (local_toc_syms)
4eca0228 9445 _bfd_error_handler
df22d223
AM
9446 (_("%s defined on removed toc entry"),
9447 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9448 do
9449 ++i;
9450 while ((skip[i] & (ref_from_discarded | can_optimize)));
9451 sym->st_value = (bfd_vma) i << 3;
9452 }
d62b3684 9453
df22d223
AM
9454 sym->st_value -= skip[i];
9455 symtab_hdr->contents = (unsigned char *) local_syms;
9456 }
c5614fa4 9457
854b41e7 9458 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9459 if (toc_inf.global_toc_syms)
9460 {
9461 toc_inf.toc = toc;
9462 toc_inf.skip = skip;
9463 toc_inf.global_toc_syms = FALSE;
9464 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9465 &toc_inf);
9466 }
854b41e7
AM
9467
9468 if (toc->reloc_count != 0)
9469 {
d4730f92 9470 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9471 Elf_Internal_Rela *wrel;
9472 bfd_size_type sz;
9473
854b41e7 9474 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9475 if (toc_relocs == NULL)
9476 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9477 info->keep_memory);
9478 if (toc_relocs == NULL)
9479 goto error_ret;
9480
425b145b
AM
9481 wrel = toc_relocs;
9482 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9483 if ((skip[rel->r_offset >> 3]
9484 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9485 {
9486 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9487 wrel->r_info = rel->r_info;
9488 wrel->r_addend = rel->r_addend;
9489 ++wrel;
9490 }
9491 else if (!dec_dynrel_count (rel->r_info, toc, info,
9492 &local_syms, NULL, NULL))
9493 goto error_ret;
9494
425b145b
AM
9495 elf_section_data (toc)->relocs = toc_relocs;
9496 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9497 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9498 sz = rel_hdr->sh_entsize;
9499 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9500 }
c5614fa4 9501 }
28be611c
AM
9502 else if (toc_relocs != NULL
9503 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9504 free (toc_relocs);
c5614fa4
AM
9505
9506 if (local_syms != NULL
9507 && symtab_hdr->contents != (unsigned char *) local_syms)
9508 {
9509 if (!info->keep_memory)
9510 free (local_syms);
9511 else
9512 symtab_hdr->contents = (unsigned char *) local_syms;
9513 }
9514 free (skip);
9515 }
9516
9517 return TRUE;
9518}
9519
1bbe0902
AM
9520/* Return true iff input section I references the TOC using
9521 instructions limited to +/-32k offsets. */
9522
9523bfd_boolean
9524ppc64_elf_has_small_toc_reloc (asection *i)
9525{
9526 return (is_ppc64_elf (i->owner)
9527 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9528}
9529
927be08e
AM
9530/* Allocate space for one GOT entry. */
9531
9532static void
9533allocate_got (struct elf_link_hash_entry *h,
9534 struct bfd_link_info *info,
9535 struct got_entry *gent)
9536{
9537 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9538 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9539 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9540 ? 16 : 8);
9541 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9542 ? 2 : 1) * sizeof (Elf64_External_Rela);
9543 asection *got = ppc64_elf_tdata (gent->owner)->got;
9544
9545 gent->got.offset = got->size;
9546 got->size += entsize;
9547
19e08130 9548 if (h->type == STT_GNU_IFUNC)
927be08e 9549 {
33e44f2e 9550 htab->elf.irelplt->size += rentsize;
19e08130 9551 htab->got_reli_size += rentsize;
927be08e 9552 }
f15d0b54
AM
9553 else if (((bfd_link_pic (info)
9554 && !((gent->tls_type & TLS_TPREL) != 0
9555 && bfd_link_executable (info)
9556 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9557 || (htab->elf.dynamic_sections_created
9558 && h->dynindx != -1
9559 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9560 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9561 {
19e08130 9562 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9563 relgot->size += rentsize;
927be08e
AM
9564 }
9565}
9566
7865406b
AM
9567/* This function merges got entries in the same toc group. */
9568
9569static void
9570merge_got_entries (struct got_entry **pent)
9571{
9572 struct got_entry *ent, *ent2;
9573
9574 for (ent = *pent; ent != NULL; ent = ent->next)
9575 if (!ent->is_indirect)
9576 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9577 if (!ent2->is_indirect
9578 && ent2->addend == ent->addend
9579 && ent2->tls_type == ent->tls_type
9580 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9581 {
9582 ent2->is_indirect = TRUE;
9583 ent2->got.ent = ent;
9584 }
9585}
9586
46434633 9587/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9588
9589static bfd_boolean
46434633
AM
9590ensure_undef_dynamic (struct bfd_link_info *info,
9591 struct elf_link_hash_entry *h)
f0158f44
AM
9592{
9593 struct elf_link_hash_table *htab = elf_hash_table (info);
9594
9595 if (htab->dynamic_sections_created
46434633
AM
9596 && ((info->dynamic_undefined_weak != 0
9597 && h->root.type == bfd_link_hash_undefweak)
9598 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9599 && h->dynindx == -1
9600 && !h->forced_local
9601 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9602 return bfd_elf_link_record_dynamic_symbol (info, h);
9603 return TRUE;
9604}
9605
65f38f15
AM
9606/* Allocate space in .plt, .got and associated reloc sections for
9607 dynamic relocs. */
5bd4f169 9608
b34976b6 9609static bfd_boolean
4ce794b7 9610allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9611{
65f38f15
AM
9612 struct bfd_link_info *info;
9613 struct ppc_link_hash_table *htab;
5bd4f169 9614 asection *s;
65f38f15 9615 struct ppc_link_hash_entry *eh;
0b8bcf0d 9616 struct got_entry **pgent, *gent;
5bd4f169 9617
e92d460e 9618 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9619 return TRUE;
5bd4f169 9620
65f38f15
AM
9621 info = (struct bfd_link_info *) inf;
9622 htab = ppc_hash_table (info);
4dfe6ac6
NC
9623 if (htab == NULL)
9624 return FALSE;
5bd4f169 9625
951fd09b
AM
9626 eh = (struct ppc_link_hash_entry *) h;
9627 /* Run through the TLS GD got entries first if we're changing them
9628 to TPREL. */
e7b938ca 9629 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9630 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9631 if (gent->got.refcount > 0
9632 && (gent->tls_type & TLS_GD) != 0)
9633 {
9634 /* This was a GD entry that has been converted to TPREL. If
9635 there happens to be a TPREL entry we can use that one. */
9636 struct got_entry *ent;
9637 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9638 if (ent->got.refcount > 0
9639 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9640 && ent->addend == gent->addend
9641 && ent->owner == gent->owner)
951fd09b
AM
9642 {
9643 gent->got.refcount = 0;
9644 break;
9645 }
9646
9647 /* If not, then we'll be using our own TPREL entry. */
9648 if (gent->got.refcount != 0)
9649 gent->tls_type = TLS_TLS | TLS_TPREL;
9650 }
9651
7865406b
AM
9652 /* Remove any list entry that won't generate a word in the GOT before
9653 we call merge_got_entries. Otherwise we risk merging to empty
9654 entries. */
0b8bcf0d
AM
9655 pgent = &h->got.glist;
9656 while ((gent = *pgent) != NULL)
411e1bfb 9657 if (gent->got.refcount > 0)
7865406b
AM
9658 {
9659 if ((gent->tls_type & TLS_LD) != 0
9660 && !h->def_dynamic)
9661 {
9662 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9663 *pgent = gent->next;
9664 }
9665 else
9666 pgent = &gent->next;
9667 }
9668 else
9669 *pgent = gent->next;
9670
9671 if (!htab->do_multi_toc)
9672 merge_got_entries (&h->got.glist);
9673
9674 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9675 if (!gent->is_indirect)
411e1bfb 9676 {
46434633
AM
9677 /* Make sure this symbol is output as a dynamic symbol. */
9678 if (!ensure_undef_dynamic (info, h))
f0158f44 9679 return FALSE;
65f38f15 9680
0c8d6e5c 9681 if (!is_ppc64_elf (gent->owner))
927be08e 9682 abort ();
0ffa91dd 9683
927be08e 9684 allocate_got (h, info, gent);
411e1bfb 9685 }
65f38f15 9686
954b63d4
AM
9687 /* If no dynamic sections we can't have dynamic relocs, except for
9688 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9689 if (!htab->elf.dynamic_sections_created
9690 && h->type != STT_GNU_IFUNC)
9691 eh->dyn_relocs = NULL;
9692
529fe20e
AM
9693 /* Discard relocs on undefined symbols that must be local. */
9694 else if (h->root.type == bfd_link_hash_undefined
9695 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9696 eh->dyn_relocs = NULL;
9697
954b63d4
AM
9698 /* Also discard relocs on undefined weak syms with non-default
9699 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9700 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9701 eh->dyn_relocs = NULL;
9702
8a2058b5 9703 if (eh->dyn_relocs != NULL)
65f38f15 9704 {
8a2058b5
AM
9705 struct elf_dyn_relocs *p, **pp;
9706
57e7d118
AM
9707 /* In the shared -Bsymbolic case, discard space allocated for
9708 dynamic pc-relative relocs against symbols which turn out to
9709 be defined in regular objects. For the normal shared case,
9710 discard space for relocs that have become local due to symbol
9711 visibility changes. */
9712
9713 if (bfd_link_pic (info))
65f38f15 9714 {
57e7d118
AM
9715 /* Relocs that use pc_count are those that appear on a call
9716 insn, or certain REL relocs (see must_be_dyn_reloc) that
9717 can be generated via assembly. We want calls to
9718 protected symbols to resolve directly to the function
9719 rather than going via the plt. If people want function
9720 pointer comparisons to work as expected then they should
9721 avoid writing weird assembly. */
9722 if (SYMBOL_CALLS_LOCAL (info, h))
9723 {
57e7d118
AM
9724 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9725 {
9726 p->count -= p->pc_count;
9727 p->pc_count = 0;
9728 if (p->count == 0)
9729 *pp = p->next;
9730 else
9731 pp = &p->next;
9732 }
9733 }
65f38f15 9734
954b63d4 9735 if (eh->dyn_relocs != NULL)
5bd4f169 9736 {
46434633
AM
9737 /* Make sure this symbol is output as a dynamic symbol. */
9738 if (!ensure_undef_dynamic (info, h))
f0158f44 9739 return FALSE;
5bd4f169 9740 }
65f38f15 9741 }
529fe20e 9742 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9743 {
8a2058b5 9744 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9745 symbols which turn out to need copy relocs or are not
9746 dynamic. */
529fe20e
AM
9747 if (h->dynamic_adjusted
9748 && !h->def_regular
9749 && !ELF_COMMON_DEF_P (h))
f0158f44 9750 {
46434633
AM
9751 /* Make sure this symbol is output as a dynamic symbol. */
9752 if (!ensure_undef_dynamic (info, h))
f0158f44 9753 return FALSE;
dfbb6ac9 9754
f0158f44
AM
9755 if (h->dynindx == -1)
9756 eh->dyn_relocs = NULL;
9757 }
9758 else
8a2058b5 9759 eh->dyn_relocs = NULL;
57e7d118
AM
9760 }
9761
9762 /* Finally, allocate space. */
9763 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9764 {
9765 asection *sreloc = elf_section_data (p->sec)->sreloc;
9766 if (eh->elf.type == STT_GNU_IFUNC)
9767 sreloc = htab->elf.irelplt;
9768 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9769 }
65f38f15 9770 }
57e7d118
AM
9771
9772 if ((htab->elf.dynamic_sections_created
f0158f44 9773 && h->dynindx != -1)
57e7d118 9774 || h->type == STT_GNU_IFUNC)
65f38f15 9775 {
57e7d118
AM
9776 struct plt_entry *pent;
9777 bfd_boolean doneone = FALSE;
9778 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9779 if (pent->plt.refcount > 0)
9780 {
9781 if (!htab->elf.dynamic_sections_created
9782 || h->dynindx == -1)
9783 {
9784 s = htab->elf.iplt;
9785 pent->plt.offset = s->size;
9786 s->size += PLT_ENTRY_SIZE (htab);
9787 s = htab->elf.irelplt;
9788 }
9789 else
9790 {
9791 /* If this is the first .plt entry, make room for the special
9792 first entry. */
9793 s = htab->elf.splt;
9794 if (s->size == 0)
9795 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9796
57e7d118 9797 pent->plt.offset = s->size;
65f38f15 9798
57e7d118
AM
9799 /* Make room for this entry. */
9800 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9801
57e7d118
AM
9802 /* Make room for the .glink code. */
9803 s = htab->glink;
9804 if (s->size == 0)
9e390558 9805 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9806 if (htab->opd_abi)
9807 {
9808 /* We need bigger stubs past index 32767. */
9e390558 9809 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9810 s->size += 4;
9811 s->size += 2*4;
9812 }
9813 else
9814 s->size += 4;
65f38f15 9815
57e7d118
AM
9816 /* We also need to make an entry in the .rela.plt section. */
9817 s = htab->elf.srelplt;
9818 }
9819 s->size += sizeof (Elf64_External_Rela);
9820 doneone = TRUE;
9821 }
9822 else
9823 pent->plt.offset = (bfd_vma) -1;
9824 if (!doneone)
9825 {
9826 h->plt.plist = NULL;
9827 h->needs_plt = 0;
9828 }
65f38f15 9829 }
57e7d118 9830 else
65f38f15 9831 {
57e7d118
AM
9832 h->plt.plist = NULL;
9833 h->needs_plt = 0;
65f38f15
AM
9834 }
9835
b34976b6 9836 return TRUE;
65f38f15
AM
9837}
9838
9e390558
AM
9839#define PPC_LO(v) ((v) & 0xffff)
9840#define PPC_HI(v) (((v) >> 16) & 0xffff)
9841#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9842
a345bc8d
AM
9843/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9844 to set up space for global entry stubs. These are put in glink,
9845 after the branch table. */
65f38f15 9846
b34976b6 9847static bfd_boolean
a345bc8d 9848size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9849{
a345bc8d
AM
9850 struct bfd_link_info *info;
9851 struct ppc_link_hash_table *htab;
9852 struct plt_entry *pent;
9e390558 9853 asection *s, *plt;
65f38f15 9854
a345bc8d
AM
9855 if (h->root.type == bfd_link_hash_indirect)
9856 return TRUE;
65f38f15 9857
a345bc8d
AM
9858 if (!h->pointer_equality_needed)
9859 return TRUE;
65f38f15 9860
a345bc8d
AM
9861 if (h->def_regular)
9862 return TRUE;
65f38f15 9863
a345bc8d
AM
9864 info = inf;
9865 htab = ppc_hash_table (info);
9866 if (htab == NULL)
9867 return FALSE;
9868
9e390558
AM
9869 s = htab->global_entry;
9870 plt = htab->elf.splt;
a345bc8d
AM
9871 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9872 if (pent->plt.offset != (bfd_vma) -1
9873 && pent->addend == 0)
9874 {
afe397ea
AM
9875 /* For ELFv2, if this symbol is not defined in a regular file
9876 and we are not generating a shared library or pie, then we
9877 need to define the symbol in the executable on a call stub.
9878 This is to avoid text relocations. */
9e390558
AM
9879 bfd_vma off, stub_align, stub_off, stub_size;
9880 unsigned int align_power;
9881
9882 stub_size = 16;
9883 stub_off = s->size;
9884 if (htab->params->plt_stub_align >= 0)
9885 align_power = htab->params->plt_stub_align;
9886 else
9887 align_power = -htab->params->plt_stub_align;
9888 /* Setting section alignment is delayed until we know it is
9889 non-empty. Otherwise the .text output section will be
9890 aligned at least to plt_stub_align even when no global
9891 entry stubs are needed. */
9892 if (s->alignment_power < align_power)
9893 s->alignment_power = align_power;
9894 stub_align = (bfd_vma) 1 << align_power;
9895 if (htab->params->plt_stub_align >= 0
9896 || ((((stub_off + stub_size - 1) & -stub_align)
9897 - (stub_off & -stub_align))
9898 > ((stub_size - 1) & -stub_align)))
9899 stub_off = (stub_off + stub_align - 1) & -stub_align;
9900 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9901 off -= stub_off + s->output_offset + s->output_section->vma;
9902 /* Note that for --plt-stub-align negative we have a possible
9903 dependency between stub offset and size. Break that
9904 dependency by assuming the max stub size when calculating
9905 the stub offset. */
9906 if (PPC_HA (off) == 0)
9907 stub_size -= 4;
8a2058b5 9908 h->root.type = bfd_link_hash_defined;
afe397ea 9909 h->root.u.def.section = s;
9e390558
AM
9910 h->root.u.def.value = stub_off;
9911 s->size = stub_off + stub_size;
a345bc8d
AM
9912 break;
9913 }
9914 return TRUE;
9915}
9916
9917/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9918 read-only sections. */
9919
9920static bfd_boolean
98bbb1b8 9921maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9922{
98bbb1b8
AM
9923 asection *sec;
9924
a345bc8d
AM
9925 if (h->root.type == bfd_link_hash_indirect)
9926 return TRUE;
9927
98bbb1b8
AM
9928 sec = readonly_dynrelocs (h);
9929 if (sec != NULL)
a345bc8d 9930 {
98bbb1b8
AM
9931 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9932
9933 info->flags |= DF_TEXTREL;
9934 info->callbacks->minfo
c1c8c1ef 9935 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 9936 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9937
9938 /* Not an error, just cut short the traversal. */
9939 return FALSE;
65f38f15 9940 }
b34976b6 9941 return TRUE;
65f38f15
AM
9942}
9943
9944/* Set the sizes of the dynamic sections. */
9945
b34976b6 9946static bfd_boolean
ee67d69a 9947ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9948 struct bfd_link_info *info)
65f38f15
AM
9949{
9950 struct ppc_link_hash_table *htab;
9951 bfd *dynobj;
9952 asection *s;
b34976b6 9953 bfd_boolean relocs;
65f38f15 9954 bfd *ibfd;
7865406b 9955 struct got_entry *first_tlsld;
65f38f15
AM
9956
9957 htab = ppc_hash_table (info);
4dfe6ac6
NC
9958 if (htab == NULL)
9959 return FALSE;
9960
65f38f15
AM
9961 dynobj = htab->elf.dynobj;
9962 if (dynobj == NULL)
9963 abort ();
9964
9965 if (htab->elf.dynamic_sections_created)
9966 {
9967 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9968 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9969 {
3d4d4302 9970 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9971 if (s == NULL)
9972 abort ();
eea6121a 9973 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9974 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9975 }
9976 }
9977
9978 /* Set up .got offsets for local syms, and space for local dynamic
9979 relocs. */
c72f2fb2 9980 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9981 {
411e1bfb
AM
9982 struct got_entry **lgot_ents;
9983 struct got_entry **end_lgot_ents;
e054468f
AM
9984 struct plt_entry **local_plt;
9985 struct plt_entry **end_local_plt;
f961d9dd 9986 unsigned char *lgot_masks;
65f38f15
AM
9987 bfd_size_type locsymcount;
9988 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9989
0c8d6e5c 9990 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9991 continue;
9992
9993 for (s = ibfd->sections; s != NULL; s = s->next)
9994 {
19e08130 9995 struct ppc_dyn_relocs *p;
65f38f15 9996
6edfbbad 9997 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9998 {
ec338859
AM
9999 if (!bfd_is_abs_section (p->sec)
10000 && bfd_is_abs_section (p->sec->output_section))
10001 {
10002 /* Input section has been discarded, either because
10003 it is a copy of a linkonce section or due to
10004 linker script /DISCARD/, so we'll be discarding
10005 the relocs too. */
10006 }
248866a8 10007 else if (p->count != 0)
ec338859 10008 {
19e08130
AM
10009 asection *srel = elf_section_data (p->sec)->sreloc;
10010 if (p->ifunc)
33e44f2e 10011 srel = htab->elf.irelplt;
eea6121a 10012 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10013 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10014 info->flags |= DF_TEXTREL;
ec338859 10015 }
65f38f15
AM
10016 }
10017 }
10018
411e1bfb
AM
10019 lgot_ents = elf_local_got_ents (ibfd);
10020 if (!lgot_ents)
65f38f15
AM
10021 continue;
10022
0ffa91dd 10023 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10024 locsymcount = symtab_hdr->sh_info;
411e1bfb 10025 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10026 local_plt = (struct plt_entry **) end_lgot_ents;
10027 end_local_plt = local_plt + locsymcount;
f961d9dd 10028 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10029 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10030 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10031 {
0b8bcf0d 10032 struct got_entry **pent, *ent;
411e1bfb 10033
0b8bcf0d
AM
10034 pent = lgot_ents;
10035 while ((ent = *pent) != NULL)
411e1bfb
AM
10036 if (ent->got.refcount > 0)
10037 {
e7b938ca 10038 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10039 {
927be08e 10040 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10041 *pent = ent->next;
411e1bfb
AM
10042 }
10043 else
10044 {
19e08130
AM
10045 unsigned int ent_size = 8;
10046 unsigned int rel_size = sizeof (Elf64_External_Rela);
10047
eea6121a 10048 ent->got.offset = s->size;
e7b938ca 10049 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10050 {
19e08130
AM
10051 ent_size *= 2;
10052 rel_size *= 2;
10053 }
10054 s->size += ent_size;
10055 if ((*lgot_masks & PLT_IFUNC) != 0)
10056 {
33e44f2e 10057 htab->elf.irelplt->size += rel_size;
19e08130
AM
10058 htab->got_reli_size += rel_size;
10059 }
f15d0b54
AM
10060 else if (bfd_link_pic (info)
10061 && !((ent->tls_type & TLS_TPREL) != 0
10062 && bfd_link_executable (info)))
19e08130
AM
10063 {
10064 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10065 srel->size += rel_size;
927be08e 10066 }
0b8bcf0d 10067 pent = &ent->next;
411e1bfb
AM
10068 }
10069 }
10070 else
0b8bcf0d 10071 *pent = ent->next;
65f38f15 10072 }
e054468f
AM
10073
10074 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10075 for (; local_plt < end_local_plt; ++local_plt)
10076 {
10077 struct plt_entry *ent;
10078
10079 for (ent = *local_plt; ent != NULL; ent = ent->next)
10080 if (ent->plt.refcount > 0)
10081 {
33e44f2e 10082 s = htab->elf.iplt;
e054468f 10083 ent->plt.offset = s->size;
b9e5796b 10084 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10085
33e44f2e 10086 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10087 }
10088 else
10089 ent->plt.offset = (bfd_vma) -1;
10090 }
65f38f15
AM
10091 }
10092
10093 /* Allocate global sym .plt and .got entries, and space for global
10094 sym dynamic relocs. */
4ce794b7 10095 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10096
0e1862bb 10097 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10098 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10099
7865406b 10100 first_tlsld = NULL;
c72f2fb2 10101 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10102 {
7865406b
AM
10103 struct got_entry *ent;
10104
0c8d6e5c 10105 if (!is_ppc64_elf (ibfd))
102890f0
AM
10106 continue;
10107
7865406b
AM
10108 ent = ppc64_tlsld_got (ibfd);
10109 if (ent->got.refcount > 0)
102890f0 10110 {
7865406b 10111 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10112 {
7865406b
AM
10113 ent->is_indirect = TRUE;
10114 ent->got.ent = first_tlsld;
10115 }
10116 else
10117 {
10118 if (first_tlsld == NULL)
10119 first_tlsld = ent;
10120 s = ppc64_elf_tdata (ibfd)->got;
10121 ent->got.offset = s->size;
10122 ent->owner = ibfd;
10123 s->size += 16;
0e1862bb 10124 if (bfd_link_pic (info))
7865406b
AM
10125 {
10126 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10127 srel->size += sizeof (Elf64_External_Rela);
10128 }
102890f0
AM
10129 }
10130 }
10131 else
7865406b 10132 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10133 }
10134
65f38f15
AM
10135 /* We now have determined the sizes of the various dynamic sections.
10136 Allocate memory for them. */
b34976b6 10137 relocs = FALSE;
65f38f15
AM
10138 for (s = dynobj->sections; s != NULL; s = s->next)
10139 {
10140 if ((s->flags & SEC_LINKER_CREATED) == 0)
10141 continue;
10142
4ce794b7 10143 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10144 /* These haven't been allocated yet; don't strip. */
10145 continue;
33e44f2e
AM
10146 else if (s == htab->elf.sgot
10147 || s == htab->elf.splt
10148 || s == htab->elf.iplt
c456f082 10149 || s == htab->glink
9e390558 10150 || s == htab->global_entry
5474d94f
AM
10151 || s == htab->elf.sdynbss
10152 || s == htab->elf.sdynrelro)
65f38f15
AM
10153 {
10154 /* Strip this section if we don't need it; see the
10155 comment below. */
5bd4f169 10156 }
58d180e8
AM
10157 else if (s == htab->glink_eh_frame)
10158 {
10159 if (!bfd_is_abs_section (s->output_section))
10160 /* Not sized yet. */
10161 continue;
10162 }
70cc837d 10163 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10164 {
c456f082 10165 if (s->size != 0)
5bd4f169 10166 {
33e44f2e 10167 if (s != htab->elf.srelplt)
b34976b6 10168 relocs = TRUE;
5bd4f169
AM
10169
10170 /* We use the reloc_count field as a counter if we need
10171 to copy relocs into the output file. */
10172 s->reloc_count = 0;
10173 }
10174 }
65f38f15 10175 else
5bd4f169
AM
10176 {
10177 /* It's not one of our sections, so don't allocate space. */
10178 continue;
10179 }
10180
eea6121a 10181 if (s->size == 0)
5bd4f169 10182 {
c456f082
AM
10183 /* If we don't need this section, strip it from the
10184 output file. This is mostly to handle .rela.bss and
10185 .rela.plt. We must create both sections in
10186 create_dynamic_sections, because they must be created
10187 before the linker maps input sections to output
10188 sections. The linker does that before
10189 adjust_dynamic_symbol is called, and it is that
10190 function which decides whether anything needs to go
10191 into these sections. */
8423293d 10192 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10193 continue;
10194 }
10195
06bcf541
AM
10196 if (bfd_is_abs_section (s->output_section))
10197 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10198 s->name);
10199
c456f082 10200 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10201 continue;
10202
65f38f15
AM
10203 /* Allocate memory for the section contents. We use bfd_zalloc
10204 here in case unused entries are not reclaimed before the
10205 section's contents are written out. This should not happen,
411e1bfb
AM
10206 but this way if it does we get a R_PPC64_NONE reloc in .rela
10207 sections instead of garbage.
10208 We also rely on the section contents being zero when writing
5474d94f 10209 the GOT and .dynrelro. */
eea6121a 10210 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10211 if (s->contents == NULL)
b34976b6 10212 return FALSE;
5bd4f169
AM
10213 }
10214
c72f2fb2 10215 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10216 {
0c8d6e5c 10217 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10218 continue;
10219
e717da7e 10220 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10221 if (s != NULL && s != htab->elf.sgot)
e717da7e 10222 {
eea6121a 10223 if (s->size == 0)
8423293d 10224 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10225 else
10226 {
eea6121a 10227 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10228 if (s->contents == NULL)
10229 return FALSE;
10230 }
10231 }
10232 s = ppc64_elf_tdata (ibfd)->relgot;
10233 if (s != NULL)
10234 {
eea6121a 10235 if (s->size == 0)
8423293d 10236 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10237 else
10238 {
eea6121a 10239 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10240 if (s->contents == NULL)
10241 return FALSE;
10242 relocs = TRUE;
10243 s->reloc_count = 0;
10244 }
10245 }
10246 }
10247
e86ce104 10248 if (htab->elf.dynamic_sections_created)
5bd4f169 10249 {
e8910a83
AM
10250 bfd_boolean tls_opt;
10251
5bd4f169
AM
10252 /* Add some entries to the .dynamic section. We fill in the
10253 values later, in ppc64_elf_finish_dynamic_sections, but we
10254 must add the entries now so that we get the correct size for
10255 the .dynamic section. The DT_DEBUG entry is filled in by the
10256 dynamic linker and used by the debugger. */
dc810e39 10257#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10258 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10259
0e1862bb 10260 if (bfd_link_executable (info))
5bd4f169 10261 {
dc810e39 10262 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10263 return FALSE;
5bd4f169
AM
10264 }
10265
33e44f2e 10266 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10267 {
dc810e39
AM
10268 if (!add_dynamic_entry (DT_PLTGOT, 0)
10269 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10270 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10271 || !add_dynamic_entry (DT_JMPREL, 0)
10272 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10273 return FALSE;
5bd4f169
AM
10274 }
10275
ee67d69a 10276 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10277 {
10278 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10279 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10280 return FALSE;
19397422
AM
10281 }
10282
7c9cf415 10283 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10284 && htab->tls_get_addr_fd != NULL
10285 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10286 if (tls_opt || !htab->opd_abi)
10287 {
10288 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10289 return FALSE;
10290 }
a7f2871e 10291
5bd4f169
AM
10292 if (relocs)
10293 {
dc810e39
AM
10294 if (!add_dynamic_entry (DT_RELA, 0)
10295 || !add_dynamic_entry (DT_RELASZ, 0)
10296 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10297 return FALSE;
5bd4f169 10298
65f38f15
AM
10299 /* If any dynamic relocs apply to a read-only section,
10300 then we need a DT_TEXTREL entry. */
248866a8 10301 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10302 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10303
65f38f15 10304 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10305 {
65f38f15 10306 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10307 return FALSE;
5bd4f169 10308 }
5bd4f169 10309 }
5bd4f169 10310 }
65f38f15 10311#undef add_dynamic_entry
5bd4f169 10312
b34976b6 10313 return TRUE;
5bd4f169
AM
10314}
10315
a345bc8d
AM
10316/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10317
10318static bfd_boolean
10319ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10320{
10321 if (h->plt.plist != NULL
10322 && !h->def_regular
10323 && !h->pointer_equality_needed)
10324 return FALSE;
10325
10326 return _bfd_elf_hash_symbol (h);
10327}
10328
721956f4 10329/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10330
4ce794b7
AM
10331static inline enum ppc_stub_type
10332ppc_type_of_stub (asection *input_sec,
10333 const Elf_Internal_Rela *rel,
10334 struct ppc_link_hash_entry **hash,
e054468f 10335 struct plt_entry **plt_ent,
6911b7dc
AM
10336 bfd_vma destination,
10337 unsigned long local_off)
5bd4f169 10338{
721956f4
AM
10339 struct ppc_link_hash_entry *h = *hash;
10340 bfd_vma location;
10341 bfd_vma branch_offset;
10342 bfd_vma max_branch_offset;
4ce794b7 10343 enum elf_ppc64_reloc_type r_type;
5bd4f169 10344
721956f4
AM
10345 if (h != NULL)
10346 {
e054468f 10347 struct plt_entry *ent;
7fe2b9a6 10348 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10349 if (h->oh != NULL
10350 && h->oh->is_func_descriptor)
7b8f6675
AM
10351 {
10352 fdh = ppc_follow_link (h->oh);
10353 *hash = fdh;
10354 }
8387904d 10355
e054468f
AM
10356 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10357 if (ent->addend == rel->r_addend
10358 && ent->plt.offset != (bfd_vma) -1)
10359 {
e054468f
AM
10360 *plt_ent = ent;
10361 return ppc_stub_plt_call;
10362 }
5bd4f169 10363
7fe2b9a6
AM
10364 /* Here, we know we don't have a plt entry. If we don't have a
10365 either a defined function descriptor or a defined entry symbol
10366 in a regular object file, then it is pointless trying to make
10367 any other type of stub. */
854b41e7
AM
10368 if (!is_static_defined (&fdh->elf)
10369 && !is_static_defined (&h->elf))
721956f4 10370 return ppc_stub_none;
5d1634d7 10371 }
e054468f
AM
10372 else if (elf_local_got_ents (input_sec->owner) != NULL)
10373 {
10374 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10375 struct plt_entry **local_plt = (struct plt_entry **)
10376 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10377 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10378
10379 if (local_plt[r_symndx] != NULL)
10380 {
10381 struct plt_entry *ent;
10382
10383 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10384 if (ent->addend == rel->r_addend
10385 && ent->plt.offset != (bfd_vma) -1)
10386 {
10387 *plt_ent = ent;
10388 return ppc_stub_plt_call;
10389 }
10390 }
10391 }
5d1634d7 10392
721956f4
AM
10393 /* Determine where the call point is. */
10394 location = (input_sec->output_offset
10395 + input_sec->output_section->vma
10396 + rel->r_offset);
5d1634d7 10397
721956f4
AM
10398 branch_offset = destination - location;
10399 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10400
721956f4
AM
10401 /* Determine if a long branch stub is needed. */
10402 max_branch_offset = 1 << 25;
4ce794b7 10403 if (r_type != R_PPC64_REL24)
721956f4 10404 max_branch_offset = 1 << 15;
5d1634d7 10405
6911b7dc 10406 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10407 /* We need a stub. Figure out whether a long_branch or plt_branch
10408 is needed later. */
10409 return ppc_stub_long_branch;
5d1634d7 10410
721956f4 10411 return ppc_stub_none;
5d1634d7
AM
10412}
10413
794e51c0
AM
10414/* With power7 weakly ordered memory model, it is possible for ld.so
10415 to update a plt entry in one thread and have another thread see a
10416 stale zero toc entry. To avoid this we need some sort of acquire
10417 barrier in the call stub. One solution is to make the load of the
10418 toc word seem to appear to depend on the load of the function entry
10419 word. Another solution is to test for r2 being zero, and branch to
10420 the appropriate glink entry if so.
10421
10422 . fake dep barrier compare
71a39c98
AM
10423 . ld 12,xxx(2) ld 12,xxx(2)
10424 . mtctr 12 mtctr 12
10425 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10426 . add 2,2,11 cmpldi 2,0
10427 . ld 2,xxx+8(2) bnectr+
10428 . bctr b <glink_entry>
10429
10430 The solution involving the compare turns out to be faster, so
10431 that's what we use unless the branch won't reach. */
10432
10433#define ALWAYS_USE_FAKE_DEP 0
10434#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10435
794e51c0
AM
10436static inline unsigned int
10437plt_stub_size (struct ppc_link_hash_table *htab,
10438 struct ppc_stub_hash_entry *stub_entry,
10439 bfd_vma off)
10440{
b9e5796b
AM
10441 unsigned size = 12;
10442
10443 if (ALWAYS_EMIT_R2SAVE
10444 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10445 size += 4;
10446 if (PPC_HA (off) != 0)
794e51c0 10447 size += 4;
b9e5796b
AM
10448 if (htab->opd_abi)
10449 {
10450 size += 4;
e7d1c40c 10451 if (htab->params->plt_static_chain)
b9e5796b 10452 size += 4;
bd4d2eaa
AM
10453 if (htab->params->plt_thread_safe
10454 && htab->elf.dynamic_sections_created
10455 && stub_entry->h != NULL
10456 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10457 size += 8;
e7d1c40c 10458 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10459 size += 4;
10460 }
794e51c0
AM
10461 if (stub_entry->h != NULL
10462 && (stub_entry->h == htab->tls_get_addr_fd
10463 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10464 && htab->params->tls_get_addr_opt)
f378ab09
AM
10465 {
10466 size += 7 * 4;
10467 if (ALWAYS_EMIT_R2SAVE
10468 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10469 size += 6 * 4;
f378ab09 10470 }
794e51c0
AM
10471 return size;
10472}
10473
2420fff6
AM
10474/* Depending on the sign of plt_stub_align:
10475 If positive, return the padding to align to a 2**plt_stub_align
10476 boundary.
10477 If negative, if this stub would cross fewer 2**plt_stub_align
10478 boundaries if we align, then return the padding needed to do so. */
10479
794e51c0
AM
10480static inline unsigned int
10481plt_stub_pad (struct ppc_link_hash_table *htab,
10482 struct ppc_stub_hash_entry *stub_entry,
10483 bfd_vma plt_off)
10484{
2420fff6 10485 int stub_align;
794e51c0 10486 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10487 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10488
2420fff6
AM
10489 if (htab->params->plt_stub_align >= 0)
10490 {
10491 stub_align = 1 << htab->params->plt_stub_align;
10492 if ((stub_off & (stub_align - 1)) != 0)
10493 return stub_align - (stub_off & (stub_align - 1));
10494 return 0;
10495 }
10496
10497 stub_align = 1 << -htab->params->plt_stub_align;
794e51c0 10498 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10499 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10500 return stub_align - (stub_off & (stub_align - 1));
10501 return 0;
10502}
10503
10504/* Build a .plt call stub. */
10505
10506static inline bfd_byte *
10507build_plt_stub (struct ppc_link_hash_table *htab,
10508 struct ppc_stub_hash_entry *stub_entry,
10509 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10510{
e7d1c40c 10511 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10512 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10513 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10514 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10515 && htab->elf.dynamic_sections_created
10516 && stub_entry->h != NULL
10517 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10518 bfd_boolean use_fake_dep = plt_thread_safe;
10519 bfd_vma cmp_branch_off = 0;
10520
10521 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10522 && plt_load_toc
794e51c0 10523 && plt_thread_safe
bd4d2eaa
AM
10524 && !((stub_entry->h == htab->tls_get_addr_fd
10525 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10526 && htab->params->tls_get_addr_opt))
794e51c0
AM
10527 {
10528 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10529 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10530 / PLT_ENTRY_SIZE (htab));
9e390558 10531 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10532 bfd_vma to, from;
10533
68d62958
AM
10534 if (pltindex > 32768)
10535 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10536 to = (glinkoff
10537 + htab->glink->output_offset
10538 + htab->glink->output_section->vma);
6f20ed8a 10539 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10540 + 4 * (ALWAYS_EMIT_R2SAVE
10541 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10542 + 4 * (PPC_HA (offset) != 0)
10543 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10544 != PPC_HA (offset))
10545 + 4 * (plt_static_chain != 0)
10546 + 20
6f20ed8a
AM
10547 + stub_entry->group->stub_sec->output_offset
10548 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10549 cmp_branch_off = to - from;
10550 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10551 }
10552
ac2df442
AM
10553 if (PPC_HA (offset) != 0)
10554 {
176a0d42
AM
10555 if (r != NULL)
10556 {
794e51c0
AM
10557 if (ALWAYS_EMIT_R2SAVE
10558 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10559 r[0].r_offset += 4;
176a0d42 10560 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10561 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10562 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10563 r[1].r_addend = r[0].r_addend;
b9e5796b 10564 if (plt_load_toc)
176a0d42 10565 {
b9e5796b 10566 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10567 {
b9e5796b
AM
10568 r[2].r_offset = r[1].r_offset + 4;
10569 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10570 r[2].r_addend = r[0].r_addend;
10571 }
10572 else
10573 {
10574 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10575 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10576 r[2].r_addend = r[0].r_addend + 8;
10577 if (plt_static_chain)
10578 {
10579 r[3].r_offset = r[2].r_offset + 4;
10580 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10581 r[3].r_addend = r[0].r_addend + 16;
10582 }
c7131b65 10583 }
176a0d42
AM
10584 }
10585 }
794e51c0
AM
10586 if (ALWAYS_EMIT_R2SAVE
10587 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10588 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10589 if (plt_load_toc)
10590 {
10591 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10592 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10593 }
10594 else
10595 {
10596 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10597 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10598 }
b9e5796b
AM
10599 if (plt_load_toc
10600 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10601 {
71a39c98 10602 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10603 offset = 0;
10604 }
71a39c98 10605 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10606 if (plt_load_toc)
794e51c0 10607 {
b9e5796b
AM
10608 if (use_fake_dep)
10609 {
10610 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10611 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10612 }
10613 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10614 if (plt_static_chain)
10615 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10616 }
ac2df442
AM
10617 }
10618 else
10619 {
176a0d42
AM
10620 if (r != NULL)
10621 {
794e51c0
AM
10622 if (ALWAYS_EMIT_R2SAVE
10623 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10624 r[0].r_offset += 4;
176a0d42 10625 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10626 if (plt_load_toc)
176a0d42 10627 {
b9e5796b 10628 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10629 {
b9e5796b
AM
10630 r[1].r_offset = r[0].r_offset + 4;
10631 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10632 r[1].r_addend = r[0].r_addend;
10633 }
10634 else
10635 {
10636 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10637 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10638 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10639 if (plt_static_chain)
10640 {
10641 r[2].r_offset = r[1].r_offset + 4;
10642 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10643 r[2].r_addend = r[0].r_addend + 8;
10644 }
c7131b65 10645 }
176a0d42
AM
10646 }
10647 }
794e51c0
AM
10648 if (ALWAYS_EMIT_R2SAVE
10649 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10650 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10651 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10652 if (plt_load_toc
10653 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10654 {
10655 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10656 offset = 0;
10657 }
71a39c98 10658 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10659 if (plt_load_toc)
794e51c0 10660 {
b9e5796b
AM
10661 if (use_fake_dep)
10662 {
10663 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10664 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10665 }
10666 if (plt_static_chain)
10667 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10668 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10669 }
ac2df442 10670 }
b9e5796b 10671 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10672 {
10673 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10674 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10675 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10676 }
10677 else
407aa07c 10678 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10679 return p;
10680}
10681
a7f2871e
AM
10682/* Build a special .plt call stub for __tls_get_addr. */
10683
10684#define LD_R11_0R3 0xe9630000
10685#define LD_R12_0R3 0xe9830000
10686#define MR_R0_R3 0x7c601b78
10687#define CMPDI_R11_0 0x2c2b0000
10688#define ADD_R3_R12_R13 0x7c6c6a14
10689#define BEQLR 0x4d820020
10690#define MR_R3_R0 0x7c030378
a7f2871e
AM
10691#define STD_R11_0R1 0xf9610000
10692#define BCTRL 0x4e800421
10693#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10694#define MTLR_R11 0x7d6803a6
10695
10696static inline bfd_byte *
794e51c0
AM
10697build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10698 struct ppc_stub_hash_entry *stub_entry,
10699 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10700{
e7d1c40c 10701 bfd *obfd = htab->params->stub_bfd;
794e51c0 10702
a7f2871e
AM
10703 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10704 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10705 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10706 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10707 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10708 bfd_put_32 (obfd, BEQLR, p), p += 4;
10709 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10710 if (r != NULL)
10711 r[0].r_offset += 7 * 4;
10712 if (!ALWAYS_EMIT_R2SAVE
10713 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10714 return build_plt_stub (htab, stub_entry, p, offset, r);
10715
a7f2871e 10716 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10717 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10718
10719 if (r != NULL)
f378ab09 10720 r[0].r_offset += 2 * 4;
794e51c0 10721 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10722 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10723
a078d95a 10724 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10725 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10726 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10727 bfd_put_32 (obfd, BLR, p), p += 4;
10728
10729 return p;
10730}
10731
176a0d42
AM
10732static Elf_Internal_Rela *
10733get_relocs (asection *sec, int count)
10734{
10735 Elf_Internal_Rela *relocs;
10736 struct bfd_elf_section_data *elfsec_data;
10737
10738 elfsec_data = elf_section_data (sec);
10739 relocs = elfsec_data->relocs;
10740 if (relocs == NULL)
10741 {
10742 bfd_size_type relsize;
10743 relsize = sec->reloc_count * sizeof (*relocs);
10744 relocs = bfd_alloc (sec->owner, relsize);
10745 if (relocs == NULL)
10746 return NULL;
10747 elfsec_data->relocs = relocs;
d4730f92
BS
10748 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10749 sizeof (Elf_Internal_Shdr));
10750 if (elfsec_data->rela.hdr == NULL)
10751 return NULL;
10752 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10753 * sizeof (Elf64_External_Rela));
10754 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10755 sec->reloc_count = 0;
10756 }
10757 relocs += sec->reloc_count;
10758 sec->reloc_count += count;
10759 return relocs;
10760}
10761
aa374f67 10762static bfd_vma
25f53a85 10763get_r2off (struct bfd_link_info *info,
aa374f67
AM
10764 struct ppc_stub_hash_entry *stub_entry)
10765{
25f53a85 10766 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10767 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10768
10769 if (r2off == 0)
10770 {
10771 /* Support linking -R objects. Get the toc pointer from the
10772 opd entry. */
10773 char buf[8];
b9e5796b
AM
10774 if (!htab->opd_abi)
10775 return r2off;
aa374f67
AM
10776 asection *opd = stub_entry->h->elf.root.u.def.section;
10777 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10778
10779 if (strcmp (opd->name, ".opd") != 0
10780 || opd->reloc_count != 0)
10781 {
c1c8c1ef 10782 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
25f53a85 10783 stub_entry->h->elf.root.root.string);
aa374f67 10784 bfd_set_error (bfd_error_bad_value);
a7c49797 10785 return (bfd_vma) -1;
aa374f67
AM
10786 }
10787 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10788 return (bfd_vma) -1;
aa374f67 10789 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10790 r2off -= elf_gp (info->output_bfd);
aa374f67 10791 }
6f20ed8a 10792 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10793 return r2off;
10794}
10795
b34976b6 10796static bfd_boolean
4ce794b7 10797ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10798{
721956f4
AM
10799 struct ppc_stub_hash_entry *stub_entry;
10800 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10801 struct bfd_link_info *info;
10802 struct ppc_link_hash_table *htab;
721956f4
AM
10803 bfd_byte *loc;
10804 bfd_byte *p;
ee75fd95 10805 bfd_vma dest, off;
176a0d42 10806 Elf_Internal_Rela *r;
e054468f 10807 asection *plt;
5d1634d7 10808
721956f4
AM
10809 /* Massage our args to the form they really have. */
10810 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10811 info = in_arg;
5d1634d7 10812
5d1634d7 10813 htab = ppc_hash_table (info);
4dfe6ac6
NC
10814 if (htab == NULL)
10815 return FALSE;
5d1634d7 10816
721956f4 10817 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10818 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10819 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10820
4ce794b7 10821 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10822 switch (stub_entry->stub_type)
5d1634d7 10823 {
721956f4 10824 case ppc_stub_long_branch:
ad8e1ba5 10825 case ppc_stub_long_branch_r2off:
721956f4 10826 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10827 dest = (stub_entry->target_value
10828 + stub_entry->target_section->output_offset
10829 + stub_entry->target_section->output_section->vma);
10830 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10831 off = dest;
5d1634d7 10832
721956f4
AM
10833 /* And this is where we are coming from. */
10834 off -= (stub_entry->stub_offset
6f20ed8a
AM
10835 + stub_entry->group->stub_sec->output_offset
10836 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10837
9e390558 10838 p = loc;
ac2df442 10839 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10840 {
25f53a85 10841 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10842
a7c49797 10843 if (r2off == (bfd_vma) -1)
aa374f67
AM
10844 {
10845 htab->stub_error = TRUE;
10846 return FALSE;
10847 }
9e390558
AM
10848 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10849 p += 4;
ac2df442
AM
10850 if (PPC_HA (r2off) != 0)
10851 {
e7d1c40c 10852 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10853 ADDIS_R2_R2 | PPC_HA (r2off), p);
10854 p += 4;
a7c49797
AM
10855 }
10856 if (PPC_LO (r2off) != 0)
10857 {
10858 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10859 ADDI_R2_R2 | PPC_LO (r2off), p);
10860 p += 4;
ac2df442 10861 }
9e390558 10862 off -= p - loc;
ad8e1ba5 10863 }
9e390558
AM
10864 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10865 p += 4;
ad8e1ba5 10866
5c3dead3
AM
10867 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10868 {
cf97bcb0
AM
10869 _bfd_error_handler
10870 (_("long branch stub `%s' offset overflow"),
bc30df16 10871 stub_entry->root.string);
5c3dead3
AM
10872 htab->stub_error = TRUE;
10873 return FALSE;
10874 }
ee75fd95
AM
10875
10876 if (info->emitrelocations)
10877 {
6f20ed8a 10878 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10879 if (r == NULL)
10880 return FALSE;
9e390558 10881 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10882 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10883 r->r_addend = dest;
10884 if (stub_entry->h != NULL)
10885 {
10886 struct elf_link_hash_entry **hashes;
10887 unsigned long symndx;
10888 struct ppc_link_hash_entry *h;
10889
e7d1c40c 10890 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10891 if (hashes == NULL)
10892 {
10893 bfd_size_type hsize;
10894
10895 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10896 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10897 if (hashes == NULL)
10898 return FALSE;
e7d1c40c 10899 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10900 htab->stub_globals = 1;
10901 }
10902 symndx = htab->stub_globals++;
10903 h = stub_entry->h;
10904 hashes[symndx] = &h->elf;
10905 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10906 if (h->oh != NULL && h->oh->is_func)
b31867b6 10907 h = ppc_follow_link (h->oh);
ee75fd95
AM
10908 if (h->elf.root.u.def.section != stub_entry->target_section)
10909 /* H is an opd symbol. The addend must be zero. */
10910 r->r_addend = 0;
10911 else
10912 {
10913 off = (h->elf.root.u.def.value
10914 + h->elf.root.u.def.section->output_offset
10915 + h->elf.root.u.def.section->output_section->vma);
10916 r->r_addend -= off;
10917 }
10918 }
10919 }
721956f4 10920 break;
e86ce104 10921
721956f4 10922 case ppc_stub_plt_branch:
ad8e1ba5 10923 case ppc_stub_plt_branch_r2off:
721956f4
AM
10924 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10925 stub_entry->root.string + 9,
b34976b6 10926 FALSE, FALSE);
721956f4
AM
10927 if (br_entry == NULL)
10928 {
cf97bcb0
AM
10929 _bfd_error_handler (_("can't find branch stub `%s'"),
10930 stub_entry->root.string);
b34976b6
AM
10931 htab->stub_error = TRUE;
10932 return FALSE;
721956f4
AM
10933 }
10934
176a0d42
AM
10935 dest = (stub_entry->target_value
10936 + stub_entry->target_section->output_offset
10937 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10938 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10939 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10940
176a0d42 10941 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10942 htab->brlt->contents + br_entry->offset);
721956f4 10943
f94498ff 10944 if (br_entry->iter == htab->stub_iteration)
721956f4 10945 {
f94498ff 10946 br_entry->iter = 0;
84f5d08e 10947
f94498ff 10948 if (htab->relbrlt != NULL)
84f5d08e 10949 {
f94498ff
AM
10950 /* Create a reloc for the branch lookup table entry. */
10951 Elf_Internal_Rela rela;
10952 bfd_byte *rl;
10953
10954 rela.r_offset = (br_entry->offset
10955 + htab->brlt->output_offset
10956 + htab->brlt->output_section->vma);
10957 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10958 rela.r_addend = dest;
f94498ff
AM
10959
10960 rl = htab->relbrlt->contents;
10961 rl += (htab->relbrlt->reloc_count++
10962 * sizeof (Elf64_External_Rela));
10963 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10964 }
10965 else if (info->emitrelocations)
10966 {
176a0d42
AM
10967 r = get_relocs (htab->brlt, 1);
10968 if (r == NULL)
10969 return FALSE;
10970 /* brlt, being SEC_LINKER_CREATED does not go through the
10971 normal reloc processing. Symbols and offsets are not
10972 translated from input file to output file form, so
10973 set up the offset per the output file. */
f94498ff
AM
10974 r->r_offset = (br_entry->offset
10975 + htab->brlt->output_offset
10976 + htab->brlt->output_section->vma);
10977 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10978 r->r_addend = dest;
84f5d08e 10979 }
84f5d08e 10980 }
721956f4 10981
176a0d42
AM
10982 dest = (br_entry->offset
10983 + htab->brlt->output_offset
10984 + htab->brlt->output_section->vma);
10985
10986 off = (dest
06bcf541 10987 - elf_gp (info->output_bfd)
6f20ed8a 10988 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 10989
ad8e1ba5 10990 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10991 {
25f53a85 10992 info->callbacks->einfo
c1c8c1ef 10993 (_("%P: linkage table error against `%pT'\n"),
721956f4 10994 stub_entry->root.string);
5d1634d7 10995 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10996 htab->stub_error = TRUE;
10997 return FALSE;
5d1634d7 10998 }
41bd81ab 10999
176a0d42
AM
11000 if (info->emitrelocations)
11001 {
6f20ed8a 11002 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11003 if (r == NULL)
11004 return FALSE;
6f20ed8a 11005 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11006 if (bfd_big_endian (info->output_bfd))
11007 r[0].r_offset += 2;
00f412ee 11008 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11009 r[0].r_offset += 4;
11010 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11011 r[0].r_addend = dest;
11012 if (PPC_HA (off) != 0)
11013 {
11014 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11015 r[1].r_offset = r[0].r_offset + 4;
11016 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11017 r[1].r_addend = r[0].r_addend;
11018 }
11019 }
11020
9e390558 11021 p = loc;
00f412ee 11022 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11023 {
176a0d42 11024 if (PPC_HA (off) != 0)
ac2df442 11025 {
e7d1c40c 11026 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11027 ADDIS_R12_R2 | PPC_HA (off), p);
11028 p += 4;
e7d1c40c 11029 bfd_put_32 (htab->params->stub_bfd,
9e390558 11030 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11031 }
11032 else
9e390558
AM
11033 bfd_put_32 (htab->params->stub_bfd,
11034 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11035 }
11036 else
11037 {
25f53a85 11038 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11039
a7c49797 11040 if (r2off == (bfd_vma) -1)
aa374f67
AM
11041 {
11042 htab->stub_error = TRUE;
11043 return FALSE;
11044 }
ad8e1ba5 11045
9e390558
AM
11046 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11047 p += 4;
176a0d42 11048 if (PPC_HA (off) != 0)
ac2df442 11049 {
e7d1c40c 11050 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11051 ADDIS_R12_R2 | PPC_HA (off), p);
11052 p += 4;
e7d1c40c 11053 bfd_put_32 (htab->params->stub_bfd,
9e390558 11054 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11055 }
11056 else
9e390558 11057 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11058
11059 if (PPC_HA (r2off) != 0)
11060 {
9e390558 11061 p += 4;
e7d1c40c 11062 bfd_put_32 (htab->params->stub_bfd,
9e390558 11063 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11064 }
11065 if (PPC_LO (r2off) != 0)
11066 {
9e390558 11067 p += 4;
e7d1c40c 11068 bfd_put_32 (htab->params->stub_bfd,
9e390558 11069 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11070 }
ad8e1ba5 11071 }
9e390558
AM
11072 p += 4;
11073 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11074 p += 4;
407aa07c
AM
11075 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11076 p += 4;
721956f4 11077 break;
5d1634d7 11078
721956f4 11079 case ppc_stub_plt_call:
794e51c0 11080 case ppc_stub_plt_call_r2save:
e054468f 11081 if (stub_entry->h != NULL
b31867b6
AM
11082 && stub_entry->h->is_func_descriptor
11083 && stub_entry->h->oh != NULL)
c862ae31 11084 {
b31867b6
AM
11085 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11086
11087 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11088 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11089 if (fh->elf.root.type == bfd_link_hash_undefined
11090 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11091 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11092 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11093 }
11094
721956f4 11095 /* Now build the stub. */
e054468f 11096 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11097 if (dest >= (bfd_vma) -2)
721956f4
AM
11098 abort ();
11099
33e44f2e 11100 plt = htab->elf.splt;
25f23106
AM
11101 if (!htab->elf.dynamic_sections_created
11102 || stub_entry->h == NULL
11103 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11104 plt = htab->elf.iplt;
e054468f
AM
11105
11106 dest += plt->output_offset + plt->output_section->vma;
11107
11108 if (stub_entry->h == NULL
11109 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11110 {
11111 Elf_Internal_Rela rela;
11112 bfd_byte *rl;
11113
11114 rela.r_offset = dest;
ee67d69a
AM
11115 if (htab->opd_abi)
11116 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11117 else
11118 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11119 rela.r_addend = (stub_entry->target_value
11120 + stub_entry->target_section->output_offset
11121 + stub_entry->target_section->output_section->vma);
11122
33e44f2e
AM
11123 rl = (htab->elf.irelplt->contents
11124 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11125 * sizeof (Elf64_External_Rela)));
11126 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f 11127 stub_entry->plt_ent->plt.offset |= 1;
82e66161 11128 htab->local_ifunc_resolver = 1;
e054468f 11129 }
176a0d42
AM
11130
11131 off = (dest
06bcf541 11132 - elf_gp (info->output_bfd)
6f20ed8a 11133 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11134
ad8e1ba5 11135 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11136 {
25f53a85 11137 info->callbacks->einfo
695344c0 11138 /* xgettext:c-format */
c1c8c1ef 11139 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11140 stub_entry->h != NULL
11141 ? stub_entry->h->elf.root.root.string
11142 : "<local sym>");
721956f4 11143 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11144 htab->stub_error = TRUE;
11145 return FALSE;
721956f4
AM
11146 }
11147
e7d1c40c 11148 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11149 {
11150 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11151
6f20ed8a
AM
11152 stub_entry->group->stub_sec->size += pad;
11153 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11154 loc += pad;
11155 }
11156
176a0d42
AM
11157 r = NULL;
11158 if (info->emitrelocations)
11159 {
6f20ed8a 11160 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11161 ((PPC_HA (off) != 0)
11162 + (htab->opd_abi
e7d1c40c 11163 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11164 && PPC_HA (off + 16) == PPC_HA (off))
11165 : 1)));
176a0d42
AM
11166 if (r == NULL)
11167 return FALSE;
6f20ed8a 11168 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11169 if (bfd_big_endian (info->output_bfd))
11170 r[0].r_offset += 2;
176a0d42
AM
11171 r[0].r_addend = dest;
11172 }
a7f2871e
AM
11173 if (stub_entry->h != NULL
11174 && (stub_entry->h == htab->tls_get_addr_fd
11175 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11176 && htab->params->tls_get_addr_opt)
794e51c0 11177 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11178 else
794e51c0 11179 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11180 break;
11181
a4b6fadd
AM
11182 case ppc_stub_save_res:
11183 return TRUE;
11184
721956f4
AM
11185 default:
11186 BFD_FAIL ();
b34976b6 11187 return FALSE;
721956f4
AM
11188 }
11189
9e390558 11190 stub_entry->group->stub_sec->size += p - loc;
97b639ba 11191
e7d1c40c 11192 if (htab->params->emit_stub_syms)
97b639ba
AM
11193 {
11194 struct elf_link_hash_entry *h;
ee75fd95
AM
11195 size_t len1, len2;
11196 char *name;
11197 const char *const stub_str[] = { "long_branch",
11198 "long_branch_r2off",
11199 "plt_branch",
11200 "plt_branch_r2off",
794e51c0 11201 "plt_call",
ee75fd95
AM
11202 "plt_call" };
11203
11204 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11205 len2 = strlen (stub_entry->root.string);
11206 name = bfd_malloc (len1 + len2 + 2);
11207 if (name == NULL)
11208 return FALSE;
11209 memcpy (name, stub_entry->root.string, 9);
11210 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11211 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11212 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11213 if (h == NULL)
11214 return FALSE;
11215 if (h->root.type == bfd_link_hash_new)
11216 {
11217 h->root.type = bfd_link_hash_defined;
6f20ed8a 11218 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11219 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11220 h->ref_regular = 1;
11221 h->def_regular = 1;
11222 h->ref_regular_nonweak = 1;
11223 h->forced_local = 1;
11224 h->non_elf = 0;
2ec55de3 11225 h->root.linker_def = 1;
97b639ba
AM
11226 }
11227 }
11228
b34976b6 11229 return TRUE;
721956f4
AM
11230}
11231
11232/* As above, but don't actually build the stub. Just bump offset so
11233 we know stub section sizes, and select plt_branch stubs where
11234 long_branch stubs won't do. */
11235
b34976b6 11236static bfd_boolean
4ce794b7 11237ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11238{
11239 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11240 struct bfd_link_info *info;
721956f4
AM
11241 struct ppc_link_hash_table *htab;
11242 bfd_vma off;
11243 int size;
11244
11245 /* Massage our args to the form they really have. */
11246 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11247 info = in_arg;
11248
11249 htab = ppc_hash_table (info);
4dfe6ac6
NC
11250 if (htab == NULL)
11251 return FALSE;
721956f4 11252
a4b6fadd
AM
11253 if (stub_entry->h != NULL
11254 && stub_entry->h->save_res
11255 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11256 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11257 {
11258 /* Don't make stubs to out-of-line register save/restore
11259 functions. Instead, emit copies of the functions. */
11260 stub_entry->group->needs_save_res = 1;
11261 stub_entry->stub_type = ppc_stub_save_res;
11262 return TRUE;
11263 }
11264
794e51c0
AM
11265 if (stub_entry->stub_type == ppc_stub_plt_call
11266 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11267 {
e054468f
AM
11268 asection *plt;
11269 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11270 if (off >= (bfd_vma) -2)
411e1bfb 11271 abort ();
33e44f2e 11272 plt = htab->elf.splt;
25f23106
AM
11273 if (!htab->elf.dynamic_sections_created
11274 || stub_entry->h == NULL
11275 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11276 plt = htab->elf.iplt;
e054468f
AM
11277 off += (plt->output_offset
11278 + plt->output_section->vma
06bcf541 11279 - elf_gp (info->output_bfd)
6f20ed8a 11280 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11281
794e51c0 11282 size = plt_stub_size (htab, stub_entry, off);
d4aaa2a0
AM
11283 if (stub_entry->h != NULL
11284 && (stub_entry->h == htab->tls_get_addr_fd
11285 || stub_entry->h == htab->tls_get_addr)
11286 && htab->params->tls_get_addr_opt
11287 && (ALWAYS_EMIT_R2SAVE
11288 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11289 stub_entry->group->tls_get_addr_opt_bctrl
11290 = stub_entry->group->stub_sec->size + size - 5 * 4;
11291
e7d1c40c 11292 if (htab->params->plt_stub_align)
794e51c0 11293 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11294 if (info->emitrelocations)
11295 {
6f20ed8a 11296 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11297 += ((PPC_HA (off) != 0)
11298 + (htab->opd_abi
e7d1c40c 11299 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11300 && PPC_HA (off + 16) == PPC_HA (off))
11301 : 1));
6f20ed8a 11302 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11303 }
721956f4
AM
11304 }
11305 else
11306 {
ad8e1ba5
AM
11307 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11308 variants. */
ac2df442 11309 bfd_vma r2off = 0;
6911b7dc 11310 bfd_vma local_off = 0;
ac2df442 11311
721956f4
AM
11312 off = (stub_entry->target_value
11313 + stub_entry->target_section->output_offset
11314 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11315 off -= (stub_entry->group->stub_sec->size
11316 + stub_entry->group->stub_sec->output_offset
11317 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11318
ad8e1ba5
AM
11319 /* Reset the stub type from the plt variant in case we now
11320 can reach with a shorter stub. */
11321 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11322 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11323
11324 size = 4;
11325 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11326 {
25f53a85 11327 r2off = get_r2off (info, stub_entry);
a7c49797 11328 if (r2off == (bfd_vma) -1)
aa374f67
AM
11329 {
11330 htab->stub_error = TRUE;
11331 return FALSE;
11332 }
a7c49797 11333 size = 8;
ac2df442 11334 if (PPC_HA (r2off) != 0)
a7c49797
AM
11335 size += 4;
11336 if (PPC_LO (r2off) != 0)
11337 size += 4;
ac2df442 11338 off -= size - 4;
ad8e1ba5
AM
11339 }
11340
6911b7dc
AM
11341 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11342
b9e5796b
AM
11343 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11344 Do the same for -R objects without function descriptors. */
11345 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11346 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11347 && r2off == 0
11348 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11349 {
11350 struct ppc_branch_hash_entry *br_entry;
11351
11352 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11353 stub_entry->root.string + 9,
b34976b6 11354 TRUE, FALSE);
721956f4
AM
11355 if (br_entry == NULL)
11356 {
cf97bcb0
AM
11357 _bfd_error_handler (_("can't build branch stub `%s'"),
11358 stub_entry->root.string);
b34976b6
AM
11359 htab->stub_error = TRUE;
11360 return FALSE;
721956f4
AM
11361 }
11362
11363 if (br_entry->iter != htab->stub_iteration)
11364 {
11365 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11366 br_entry->offset = htab->brlt->size;
11367 htab->brlt->size += 8;
63bc6f6c 11368
ee75fd95 11369 if (htab->relbrlt != NULL)
eea6121a 11370 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11371 else if (info->emitrelocations)
11372 {
11373 htab->brlt->reloc_count += 1;
11374 htab->brlt->flags |= SEC_RELOC;
11375 }
721956f4 11376 }
ad8e1ba5
AM
11377
11378 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11379 off = (br_entry->offset
11380 + htab->brlt->output_offset
11381 + htab->brlt->output_section->vma
06bcf541 11382 - elf_gp (info->output_bfd)
6f20ed8a 11383 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11384
176a0d42
AM
11385 if (info->emitrelocations)
11386 {
6f20ed8a
AM
11387 stub_entry->group->stub_sec->reloc_count
11388 += 1 + (PPC_HA (off) != 0);
11389 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11390 }
11391
00f412ee 11392 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11393 {
11394 size = 12;
176a0d42 11395 if (PPC_HA (off) != 0)
ac2df442
AM
11396 size = 16;
11397 }
11398 else
11399 {
00f412ee 11400 size = 16;
176a0d42 11401 if (PPC_HA (off) != 0)
ac2df442
AM
11402 size += 4;
11403
11404 if (PPC_HA (r2off) != 0)
11405 size += 4;
00f412ee
AM
11406 if (PPC_LO (r2off) != 0)
11407 size += 4;
ac2df442 11408 }
721956f4 11409 }
84f5d08e
AM
11410 else if (info->emitrelocations)
11411 {
6f20ed8a
AM
11412 stub_entry->group->stub_sec->reloc_count += 1;
11413 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11414 }
721956f4
AM
11415 }
11416
6f20ed8a 11417 stub_entry->group->stub_sec->size += size;
b34976b6 11418 return TRUE;
721956f4
AM
11419}
11420
11421/* Set up various things so that we can make a list of input sections
11422 for each output section included in the link. Returns -1 on error,
cedb70c5 11423 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11424
11425int
e7d1c40c 11426ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11427{
6f20ed8a 11428 unsigned int id;
721956f4
AM
11429 bfd_size_type amt;
11430 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11431
4dfe6ac6
NC
11432 if (htab == NULL)
11433 return -1;
4c52953f 11434
6f20ed8a
AM
11435 htab->sec_info_arr_size = bfd_get_next_section_id ();
11436 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11437 htab->sec_info = bfd_zmalloc (amt);
11438 if (htab->sec_info == NULL)
721956f4
AM
11439 return -1;
11440
3d6f9012
AM
11441 /* Set toc_off for com, und, abs and ind sections. */
11442 for (id = 0; id < 3; id++)
6f20ed8a 11443 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11444
721956f4
AM
11445 return 1;
11446}
11447
927be08e
AM
11448/* Set up for first pass at multitoc partitioning. */
11449
11450void
11451ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11452{
11453 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11454
1c865ab2 11455 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11456 htab->toc_bfd = NULL;
11457 htab->toc_first_sec = NULL;
11458}
11459
e717da7e
AM
11460/* The linker repeatedly calls this function for each TOC input section
11461 and linker generated GOT section. Group input bfds such that the toc
927be08e 11462 within a group is less than 64k in size. */
ad8e1ba5 11463
927be08e 11464bfd_boolean
4ce794b7 11465ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11466{
11467 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11468 bfd_vma addr, off, limit;
ad8e1ba5 11469
4dfe6ac6
NC
11470 if (htab == NULL)
11471 return FALSE;
11472
927be08e 11473 if (!htab->second_toc_pass)
4c52953f 11474 {
927be08e 11475 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11476 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11477
11478 if (new_bfd)
bf102f86
AM
11479 {
11480 htab->toc_bfd = isec->owner;
11481 htab->toc_first_sec = isec;
11482 }
927be08e 11483
bf102f86
AM
11484 addr = isec->output_offset + isec->output_section->vma;
11485 off = addr - htab->toc_curr;
d77c8a4b
AM
11486 limit = 0x80008000;
11487 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11488 limit = 0x10000;
11489 if (off + isec->size > limit)
bf102f86
AM
11490 {
11491 addr = (htab->toc_first_sec->output_offset
11492 + htab->toc_first_sec->output_section->vma);
11493 htab->toc_curr = addr;
a27e685f 11494 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11495 }
99877b66 11496
927be08e
AM
11497 /* toc_curr is the base address of this toc group. Set elf_gp
11498 for the input section to be the offset relative to the
11499 output toc base plus 0x8000. Making the input elf_gp an
11500 offset allows us to move the toc as a whole without
11501 recalculating input elf_gp. */
06bcf541 11502 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11503 off += TOC_BASE_OFF;
11504
11505 /* Die if someone uses a linker script that doesn't keep input
11506 file .toc and .got together. */
a4fd3de5
AM
11507 if (new_bfd
11508 && elf_gp (isec->owner) != 0
927be08e
AM
11509 && elf_gp (isec->owner) != off)
11510 return FALSE;
11511
11512 elf_gp (isec->owner) = off;
11513 return TRUE;
4c52953f 11514 }
927be08e
AM
11515
11516 /* During the second pass toc_first_sec points to the start of
11517 a toc group, and toc_curr is used to track the old elf_gp.
11518 We use toc_bfd to ensure we only look at each bfd once. */
11519 if (htab->toc_bfd == isec->owner)
11520 return TRUE;
11521 htab->toc_bfd = isec->owner;
11522
11523 if (htab->toc_first_sec == NULL
11524 || htab->toc_curr != elf_gp (isec->owner))
11525 {
11526 htab->toc_curr = elf_gp (isec->owner);
11527 htab->toc_first_sec = isec;
11528 }
11529 addr = (htab->toc_first_sec->output_offset
11530 + htab->toc_first_sec->output_section->vma);
06bcf541 11531 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11532 elf_gp (isec->owner) = off;
11533
11534 return TRUE;
ad8e1ba5
AM
11535}
11536
927be08e
AM
11537/* Called via elf_link_hash_traverse to merge GOT entries for global
11538 symbol H. */
11539
11540static bfd_boolean
11541merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11542{
11543 if (h->root.type == bfd_link_hash_indirect)
11544 return TRUE;
11545
927be08e
AM
11546 merge_got_entries (&h->got.glist);
11547
11548 return TRUE;
11549}
11550
11551/* Called via elf_link_hash_traverse to allocate GOT entries for global
11552 symbol H. */
11553
11554static bfd_boolean
11555reallocate_got (struct elf_link_hash_entry *h, void *inf)
11556{
11557 struct got_entry *gent;
11558
11559 if (h->root.type == bfd_link_hash_indirect)
11560 return TRUE;
11561
927be08e
AM
11562 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11563 if (!gent->is_indirect)
11564 allocate_got (h, (struct bfd_link_info *) inf, gent);
11565 return TRUE;
11566}
11567
11568/* Called on the first multitoc pass after the last call to
11569 ppc64_elf_next_toc_section. This function removes duplicate GOT
11570 entries. */
11571
11572bfd_boolean
11573ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11574{
11575 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11576 struct bfd *ibfd, *ibfd2;
11577 bfd_boolean done_something;
11578
11579 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11580
7865406b
AM
11581 if (!htab->do_multi_toc)
11582 return FALSE;
11583
d0fae19d 11584 /* Merge global sym got entries within a toc group. */
927be08e
AM
11585 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11586
11587 /* And tlsld_got. */
c72f2fb2 11588 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11589 {
11590 struct got_entry *ent, *ent2;
11591
11592 if (!is_ppc64_elf (ibfd))
11593 continue;
11594
11595 ent = ppc64_tlsld_got (ibfd);
11596 if (!ent->is_indirect
11597 && ent->got.offset != (bfd_vma) -1)
11598 {
c72f2fb2 11599 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11600 {
11601 if (!is_ppc64_elf (ibfd2))
11602 continue;
11603
11604 ent2 = ppc64_tlsld_got (ibfd2);
11605 if (!ent2->is_indirect
11606 && ent2->got.offset != (bfd_vma) -1
11607 && elf_gp (ibfd2) == elf_gp (ibfd))
11608 {
11609 ent2->is_indirect = TRUE;
11610 ent2->got.ent = ent;
11611 }
11612 }
11613 }
11614 }
11615
11616 /* Zap sizes of got sections. */
33e44f2e
AM
11617 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11618 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11619 htab->got_reli_size = 0;
11620
c72f2fb2 11621 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11622 {
11623 asection *got, *relgot;
11624
11625 if (!is_ppc64_elf (ibfd))
11626 continue;
11627
11628 got = ppc64_elf_tdata (ibfd)->got;
11629 if (got != NULL)
11630 {
11631 got->rawsize = got->size;
11632 got->size = 0;
11633 relgot = ppc64_elf_tdata (ibfd)->relgot;
11634 relgot->rawsize = relgot->size;
11635 relgot->size = 0;
11636 }
11637 }
11638
11639 /* Now reallocate the got, local syms first. We don't need to
11640 allocate section contents again since we never increase size. */
c72f2fb2 11641 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11642 {
11643 struct got_entry **lgot_ents;
11644 struct got_entry **end_lgot_ents;
11645 struct plt_entry **local_plt;
11646 struct plt_entry **end_local_plt;
f961d9dd 11647 unsigned char *lgot_masks;
927be08e
AM
11648 bfd_size_type locsymcount;
11649 Elf_Internal_Shdr *symtab_hdr;
19e08130 11650 asection *s;
927be08e
AM
11651
11652 if (!is_ppc64_elf (ibfd))
11653 continue;
11654
11655 lgot_ents = elf_local_got_ents (ibfd);
11656 if (!lgot_ents)
11657 continue;
11658
11659 symtab_hdr = &elf_symtab_hdr (ibfd);
11660 locsymcount = symtab_hdr->sh_info;
11661 end_lgot_ents = lgot_ents + locsymcount;
11662 local_plt = (struct plt_entry **) end_lgot_ents;
11663 end_local_plt = local_plt + locsymcount;
f961d9dd 11664 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11665 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11666 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11667 {
11668 struct got_entry *ent;
11669
11670 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11671 {
19e08130
AM
11672 unsigned int ent_size = 8;
11673 unsigned int rel_size = sizeof (Elf64_External_Rela);
11674
d0fae19d
AM
11675 ent->got.offset = s->size;
11676 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11677 {
19e08130
AM
11678 ent_size *= 2;
11679 rel_size *= 2;
11680 }
11681 s->size += ent_size;
11682 if ((*lgot_masks & PLT_IFUNC) != 0)
11683 {
33e44f2e 11684 htab->elf.irelplt->size += rel_size;
19e08130
AM
11685 htab->got_reli_size += rel_size;
11686 }
0e1862bb 11687 else if (bfd_link_pic (info))
19e08130
AM
11688 {
11689 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11690 srel->size += rel_size;
d0fae19d
AM
11691 }
11692 }
927be08e
AM
11693 }
11694 }
11695
11696 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11697
c72f2fb2 11698 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11699 {
11700 struct got_entry *ent;
11701
11702 if (!is_ppc64_elf (ibfd))
11703 continue;
11704
11705 ent = ppc64_tlsld_got (ibfd);
11706 if (!ent->is_indirect
11707 && ent->got.offset != (bfd_vma) -1)
11708 {
11709 asection *s = ppc64_elf_tdata (ibfd)->got;
11710 ent->got.offset = s->size;
11711 s->size += 16;
0e1862bb 11712 if (bfd_link_pic (info))
927be08e
AM
11713 {
11714 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11715 srel->size += sizeof (Elf64_External_Rela);
11716 }
11717 }
11718 }
11719
33e44f2e 11720 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11721 if (!done_something)
c72f2fb2 11722 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11723 {
11724 asection *got;
11725
11726 if (!is_ppc64_elf (ibfd))
11727 continue;
11728
11729 got = ppc64_elf_tdata (ibfd)->got;
11730 if (got != NULL)
11731 {
11732 done_something = got->rawsize != got->size;
11733 if (done_something)
11734 break;
11735 }
11736 }
11737
11738 if (done_something)
e7d1c40c 11739 (*htab->params->layout_sections_again) ();
927be08e
AM
11740
11741 /* Set up for second pass over toc sections to recalculate elf_gp
11742 on input sections. */
11743 htab->toc_bfd = NULL;
11744 htab->toc_first_sec = NULL;
11745 htab->second_toc_pass = TRUE;
11746 return done_something;
11747}
11748
11749/* Called after second pass of multitoc partitioning. */
11750
11751void
11752ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11753{
11754 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11755
11756 /* After the second pass, toc_curr tracks the TOC offset used
11757 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11758 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11759}
11760
9b5ecbd0
AM
11761/* No toc references were found in ISEC. If the code in ISEC makes no
11762 calls, then there's no need to use toc adjusting stubs when branching
11763 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11764 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11765 needed, and 2 if a cyclical call-graph was found but no other reason
11766 for a stub was detected. If called from the top level, a return of
11767 2 means the same as a return of 0. */
9b5ecbd0
AM
11768
11769static int
4ce794b7 11770toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11771{
9b5ecbd0 11772 int ret;
70cc837d
AM
11773
11774 /* Mark this section as checked. */
11775 isec->call_check_done = 1;
9b5ecbd0 11776
772119ce
AM
11777 /* We know none of our code bearing sections will need toc stubs. */
11778 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11779 return 0;
11780
eea6121a 11781 if (isec->size == 0)
082c50f8
AM
11782 return 0;
11783
4c52953f
AM
11784 if (isec->output_section == NULL)
11785 return 0;
11786
4c52953f 11787 ret = 0;
70cc837d 11788 if (isec->reloc_count != 0)
9b5ecbd0 11789 {
70cc837d
AM
11790 Elf_Internal_Rela *relstart, *rel;
11791 Elf_Internal_Sym *local_syms;
11792 struct ppc_link_hash_table *htab;
2917689a 11793
70cc837d
AM
11794 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11795 info->keep_memory);
11796 if (relstart == NULL)
11797 return -1;
90aecf7a 11798
70cc837d
AM
11799 /* Look for branches to outside of this section. */
11800 local_syms = NULL;
11801 htab = ppc_hash_table (info);
11802 if (htab == NULL)
11803 return -1;
4c52953f 11804
70cc837d 11805 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11806 {
70cc837d
AM
11807 enum elf_ppc64_reloc_type r_type;
11808 unsigned long r_symndx;
11809 struct elf_link_hash_entry *h;
11810 struct ppc_link_hash_entry *eh;
11811 Elf_Internal_Sym *sym;
11812 asection *sym_sec;
11813 struct _opd_sec_data *opd;
11814 bfd_vma sym_value;
11815 bfd_vma dest;
11816
11817 r_type = ELF64_R_TYPE (rel->r_info);
11818 if (r_type != R_PPC64_REL24
11819 && r_type != R_PPC64_REL14
11820 && r_type != R_PPC64_REL14_BRTAKEN
11821 && r_type != R_PPC64_REL14_BRNTAKEN)
11822 continue;
4c52953f 11823
70cc837d
AM
11824 r_symndx = ELF64_R_SYM (rel->r_info);
11825 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11826 isec->owner))
4c52953f 11827 {
70cc837d
AM
11828 ret = -1;
11829 break;
11830 }
4c52953f 11831
70cc837d
AM
11832 /* Calls to dynamic lib functions go through a plt call stub
11833 that uses r2. */
11834 eh = (struct ppc_link_hash_entry *) h;
11835 if (eh != NULL
11836 && (eh->elf.plt.plist != NULL
11837 || (eh->oh != NULL
11838 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11839 {
11840 ret = 1;
11841 break;
4c52953f
AM
11842 }
11843
70cc837d
AM
11844 if (sym_sec == NULL)
11845 /* Ignore other undefined symbols. */
4c52953f 11846 continue;
4c52953f 11847
70cc837d
AM
11848 /* Assume branches to other sections not included in the
11849 link need stubs too, to cover -R and absolute syms. */
11850 if (sym_sec->output_section == NULL)
11851 {
11852 ret = 1;
11853 break;
11854 }
4c52953f 11855
70cc837d
AM
11856 if (h == NULL)
11857 sym_value = sym->st_value;
11858 else
11859 {
11860 if (h->root.type != bfd_link_hash_defined
11861 && h->root.type != bfd_link_hash_defweak)
11862 abort ();
11863 sym_value = h->root.u.def.value;
11864 }
11865 sym_value += rel->r_addend;
4c52953f 11866
70cc837d
AM
11867 /* If this branch reloc uses an opd sym, find the code section. */
11868 opd = get_opd_info (sym_sec);
11869 if (opd != NULL)
11870 {
11871 if (h == NULL && opd->adjust != NULL)
11872 {
11873 long adjust;
4c52953f 11874
92a9c616 11875 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11876 if (adjust == -1)
11877 /* Assume deleted functions won't ever be called. */
11878 continue;
11879 sym_value += adjust;
11880 }
4c52953f 11881
aef36ac1
AM
11882 dest = opd_entry_value (sym_sec, sym_value,
11883 &sym_sec, NULL, FALSE);
70cc837d
AM
11884 if (dest == (bfd_vma) -1)
11885 continue;
11886 }
11887 else
11888 dest = (sym_value
11889 + sym_sec->output_offset
11890 + sym_sec->output_section->vma);
4c52953f 11891
70cc837d
AM
11892 /* Ignore branch to self. */
11893 if (sym_sec == isec)
11894 continue;
4c52953f 11895
70cc837d
AM
11896 /* If the called function uses the toc, we need a stub. */
11897 if (sym_sec->has_toc_reloc
11898 || sym_sec->makes_toc_func_call)
4c52953f 11899 {
70cc837d 11900 ret = 1;
4c52953f
AM
11901 break;
11902 }
70cc837d
AM
11903
11904 /* Assume any branch that needs a long branch stub might in fact
11905 need a plt_branch stub. A plt_branch stub uses r2. */
11906 else if (dest - (isec->output_offset
11907 + isec->output_section->vma
6911b7dc
AM
11908 + rel->r_offset) + (1 << 25)
11909 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11910 ? h->other
11911 : sym->st_other))
4c52953f 11912 {
70cc837d
AM
11913 ret = 1;
11914 break;
11915 }
11916
11917 /* If calling back to a section in the process of being
11918 tested, we can't say for sure that no toc adjusting stubs
11919 are needed, so don't return zero. */
11920 else if (sym_sec->call_check_in_progress)
11921 ret = 2;
11922
11923 /* Branches to another section that itself doesn't have any TOC
11924 references are OK. Recursively call ourselves to check. */
11925 else if (!sym_sec->call_check_done)
11926 {
11927 int recur;
11928
11929 /* Mark current section as indeterminate, so that other
11930 sections that call back to current won't be marked as
11931 known. */
11932 isec->call_check_in_progress = 1;
11933 recur = toc_adjusting_stub_needed (info, sym_sec);
11934 isec->call_check_in_progress = 0;
11935
4c52953f
AM
11936 if (recur != 0)
11937 {
70cc837d
AM
11938 ret = recur;
11939 if (recur != 2)
11940 break;
4c52953f
AM
11941 }
11942 }
4c52953f 11943 }
70cc837d
AM
11944
11945 if (local_syms != NULL
11946 && (elf_symtab_hdr (isec->owner).contents
11947 != (unsigned char *) local_syms))
11948 free (local_syms);
11949 if (elf_section_data (isec)->relocs != relstart)
11950 free (relstart);
9b5ecbd0
AM
11951 }
11952
70cc837d
AM
11953 if ((ret & 1) == 0
11954 && isec->map_head.s != NULL
11955 && (strcmp (isec->output_section->name, ".init") == 0
11956 || strcmp (isec->output_section->name, ".fini") == 0))
11957 {
11958 if (isec->map_head.s->has_toc_reloc
11959 || isec->map_head.s->makes_toc_func_call)
11960 ret = 1;
11961 else if (!isec->map_head.s->call_check_done)
11962 {
11963 int recur;
11964 isec->call_check_in_progress = 1;
11965 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11966 isec->call_check_in_progress = 0;
11967 if (recur != 0)
11968 ret = recur;
11969 }
11970 }
11971
11972 if (ret == 1)
11973 isec->makes_toc_func_call = 1;
4c52953f 11974
9b5ecbd0
AM
11975 return ret;
11976}
11977
721956f4
AM
11978/* The linker repeatedly calls this function for each input section,
11979 in the order that input sections are linked into output sections.
11980 Build lists of input sections to determine groupings between which
11981 we may insert linker stubs. */
11982
9b5ecbd0 11983bfd_boolean
4ce794b7 11984ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11985{
11986 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11987
4dfe6ac6
NC
11988 if (htab == NULL)
11989 return FALSE;
11990
734b6cf9 11991 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11992 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11993 {
3d6f9012
AM
11994 /* This happens to make the list in reverse order,
11995 which is what we want. */
6f20ed8a
AM
11996 htab->sec_info[isec->id].u.list
11997 = htab->sec_info[isec->output_section->id].u.list;
11998 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 11999 }
ad8e1ba5 12000
4c52953f 12001 if (htab->multi_toc_needed)
9b5ecbd0 12002 {
8b974ba3
AM
12003 /* Analyse sections that aren't already flagged as needing a
12004 valid toc pointer. Exclude .fixup for the linux kernel.
12005 .fixup contains branches, but only back to the function that
12006 hit an exception. */
12007 if (!(isec->has_toc_reloc
12008 || (isec->flags & SEC_CODE) == 0
12009 || strcmp (isec->name, ".fixup") == 0
12010 || isec->call_check_done))
12011 {
12012 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12013 return FALSE;
8b974ba3
AM
12014 }
12015 /* Make all sections use the TOC assigned for this object file.
12016 This will be wrong for pasted sections; We fix that in
12017 check_pasted_section(). */
12018 if (elf_gp (isec->owner) != 0)
12019 htab->toc_curr = elf_gp (isec->owner);
12020 }
12021
6f20ed8a 12022 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12023 return TRUE;
721956f4
AM
12024}
12025
70cc837d
AM
12026/* Check that all .init and .fini sections use the same toc, if they
12027 have toc relocs. */
12028
12029static bfd_boolean
12030check_pasted_section (struct bfd_link_info *info, const char *name)
12031{
12032 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12033
12034 if (o != NULL)
12035 {
12036 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12037 bfd_vma toc_off = 0;
12038 asection *i;
12039
12040 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12041 if (i->has_toc_reloc)
12042 {
12043 if (toc_off == 0)
6f20ed8a
AM
12044 toc_off = htab->sec_info[i->id].toc_off;
12045 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12046 return FALSE;
12047 }
6683a28d
AM
12048
12049 if (toc_off == 0)
12050 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12051 if (i->makes_toc_func_call)
12052 {
6f20ed8a 12053 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12054 break;
12055 }
12056
70cc837d
AM
12057 /* Make sure the whole pasted function uses the same toc offset. */
12058 if (toc_off != 0)
12059 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12060 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12061 }
12062 return TRUE;
12063}
12064
12065bfd_boolean
12066ppc64_elf_check_init_fini (struct bfd_link_info *info)
12067{
12068 return (check_pasted_section (info, ".init")
12069 & check_pasted_section (info, ".fini"));
12070}
12071
721956f4
AM
12072/* See whether we can group stub sections together. Grouping stub
12073 sections may result in fewer stubs. More importantly, we need to
12074 put all .init* and .fini* stubs at the beginning of the .init or
12075 .fini output sections respectively, because glibc splits the
12076 _init and _fini functions into multiple parts. Putting a stub in
12077 the middle of a function is not a good idea. */
12078
6f20ed8a
AM
12079static bfd_boolean
12080group_sections (struct bfd_link_info *info,
4ce794b7
AM
12081 bfd_size_type stub_group_size,
12082 bfd_boolean stubs_always_before_branch)
721956f4 12083{
6f20ed8a
AM
12084 struct ppc_link_hash_table *htab;
12085 asection *osec;
7c8fe5c4
AM
12086 bfd_boolean suppress_size_errors;
12087
6f20ed8a
AM
12088 htab = ppc_hash_table (info);
12089 if (htab == NULL)
12090 return FALSE;
12091
7c8fe5c4 12092 suppress_size_errors = FALSE;
7c8fe5c4
AM
12093 if (stub_group_size == 1)
12094 {
12095 /* Default values. */
12096 if (stubs_always_before_branch)
09f92717 12097 stub_group_size = 0x1e00000;
7c8fe5c4 12098 else
09f92717 12099 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12100 suppress_size_errors = TRUE;
12101 }
12102
6f20ed8a 12103 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12104 {
6f20ed8a
AM
12105 asection *tail;
12106
12107 if (osec->id >= htab->sec_info_arr_size)
12108 continue;
12109
12110 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12111 while (tail != NULL)
721956f4 12112 {
734b6cf9
AM
12113 asection *curr;
12114 asection *prev;
12115 bfd_size_type total;
12116 bfd_boolean big_sec;
12117 bfd_vma curr_toc;
6f20ed8a 12118 struct map_stub *group;
09f92717 12119 bfd_size_type group_size;
734b6cf9
AM
12120
12121 curr = tail;
eea6121a 12122 total = tail->size;
09f92717
AM
12123 group_size = (ppc64_elf_section_data (tail) != NULL
12124 && ppc64_elf_section_data (tail)->has_14bit_branch
12125 ? stub_group_size >> 10 : stub_group_size);
12126
12127 big_sec = total > group_size;
7c8fe5c4 12128 if (big_sec && !suppress_size_errors)
695344c0 12129 /* xgettext:c-format */
871b3ab2 12130 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12131 tail->owner, tail);
6f20ed8a 12132 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12133
6f20ed8a 12134 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12135 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12136 < (ppc64_elf_section_data (prev) != NULL
12137 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12138 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12139 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12140 curr = prev;
12141
12142 /* OK, the size from the start of CURR to the end is less
09f92717 12143 than group_size and thus can be handled by one stub
734b6cf9 12144 section. (or the tail section is itself larger than
09f92717
AM
12145 group_size, in which case we may be toast.) We should
12146 really be keeping track of the total size of stubs added
12147 here, as stubs contribute to the final output section
12148 size. That's a little tricky, and this way will only
12149 break if stubs added make the total size more than 2^25,
12150 ie. for the default stub_group_size, if stubs total more
12151 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12152 group = bfd_alloc (curr->owner, sizeof (*group));
12153 if (group == NULL)
12154 return FALSE;
12155 group->link_sec = curr;
12156 group->stub_sec = NULL;
a4b6fadd 12157 group->needs_save_res = 0;
d4aaa2a0 12158 group->tls_get_addr_opt_bctrl = -1u;
a4b6fadd
AM
12159 group->next = htab->group;
12160 htab->group = group;
734b6cf9 12161 do
721956f4 12162 {
6f20ed8a 12163 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12164 /* Set up this stub group. */
6f20ed8a 12165 htab->sec_info[tail->id].u.group = group;
721956f4 12166 }
734b6cf9
AM
12167 while (tail != curr && (tail = prev) != NULL);
12168
09f92717 12169 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12170 bytes before the stub section can be handled by it too.
12171 Don't do this if we have a really large section after the
12172 stubs, as adding more stubs increases the chance that
12173 branches may not reach into the stub section. */
12174 if (!stubs_always_before_branch && !big_sec)
12175 {
12176 total = 0;
12177 while (prev != NULL
12178 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12179 < (ppc64_elf_section_data (prev) != NULL
12180 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12181 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12182 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12183 {
12184 tail = prev;
6f20ed8a
AM
12185 prev = htab->sec_info[tail->id].u.list;
12186 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12187 }
12188 }
12189 tail = prev;
721956f4
AM
12190 }
12191 }
6f20ed8a 12192 return TRUE;
721956f4
AM
12193}
12194
58d180e8
AM
12195static const unsigned char glink_eh_frame_cie[] =
12196{
12197 0, 0, 0, 16, /* length. */
12198 0, 0, 0, 0, /* id. */
12199 1, /* CIE version. */
12200 'z', 'R', 0, /* Augmentation string. */
12201 4, /* Code alignment. */
12202 0x78, /* Data alignment. */
12203 65, /* RA reg. */
12204 1, /* Augmentation size. */
12205 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12206 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12207};
12208
d4aaa2a0
AM
12209static size_t
12210stub_eh_frame_size (struct map_stub *group, size_t align)
12211{
12212 size_t this_size = 17;
12213 if (group->tls_get_addr_opt_bctrl != -1u)
12214 {
12215 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12216 if (to_bctrl < 64)
12217 this_size += 1;
12218 else if (to_bctrl < 256)
12219 this_size += 2;
12220 else if (to_bctrl < 65536)
12221 this_size += 3;
12222 else
12223 this_size += 5;
12224 this_size += 6;
12225 }
12226 this_size = (this_size + align - 1) & -align;
12227 return this_size;
12228}
12229
d969d15f
AM
12230/* Stripping output sections is normally done before dynamic section
12231 symbols have been allocated. This function is called later, and
12232 handles cases like htab->brlt which is mapped to its own output
12233 section. */
12234
12235static void
12236maybe_strip_output (struct bfd_link_info *info, asection *isec)
12237{
12238 if (isec->size == 0
12239 && isec->output_section->size == 0
53d8967a 12240 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12241 && !bfd_section_removed_from_list (info->output_bfd,
12242 isec->output_section)
12243 && elf_section_data (isec->output_section)->dynindx == 0)
12244 {
12245 isec->output_section->flags |= SEC_EXCLUDE;
12246 bfd_section_list_remove (info->output_bfd, isec->output_section);
12247 info->output_bfd->section_count--;
12248 }
12249}
12250
721956f4
AM
12251/* Determine and set the size of the stub section for a final link.
12252
12253 The basic idea here is to examine all the relocations looking for
12254 PC-relative calls to a target that is unreachable with a "bl"
12255 instruction. */
12256
b34976b6 12257bfd_boolean
e7d1c40c 12258ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12259{
12260 bfd_size_type stub_group_size;
b34976b6 12261 bfd_boolean stubs_always_before_branch;
721956f4
AM
12262 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12263
4dfe6ac6
NC
12264 if (htab == NULL)
12265 return FALSE;
12266
0e1862bb 12267 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12268 htab->params->plt_thread_safe = 1;
b9e5796b 12269 if (!htab->opd_abi)
e7d1c40c
AM
12270 htab->params->plt_thread_safe = 0;
12271 else if (htab->params->plt_thread_safe == -1)
794e51c0 12272 {
e2458743 12273 static const char *const thread_starter[] =
794e51c0
AM
12274 {
12275 "pthread_create",
12276 /* libstdc++ */
12277 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12278 /* librt */
12279 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12280 "mq_notify", "create_timer",
12281 /* libanl */
12282 "getaddrinfo_a",
12283 /* libgomp */
2300b5a1 12284 "GOMP_parallel",
794e51c0 12285 "GOMP_parallel_start",
2300b5a1 12286 "GOMP_parallel_loop_static",
794e51c0 12287 "GOMP_parallel_loop_static_start",
2300b5a1 12288 "GOMP_parallel_loop_dynamic",
794e51c0 12289 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12290 "GOMP_parallel_loop_guided",
794e51c0 12291 "GOMP_parallel_loop_guided_start",
2300b5a1 12292 "GOMP_parallel_loop_runtime",
794e51c0 12293 "GOMP_parallel_loop_runtime_start",
2300b5a1 12294 "GOMP_parallel_sections",
68ffbac6 12295 "GOMP_parallel_sections_start",
f9dffbf0
AM
12296 /* libgo */
12297 "__go_go",
794e51c0
AM
12298 };
12299 unsigned i;
12300
a4b6fadd 12301 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12302 {
12303 struct elf_link_hash_entry *h;
12304 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12305 FALSE, FALSE, TRUE);
e7d1c40c
AM
12306 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12307 if (htab->params->plt_thread_safe)
794e51c0
AM
12308 break;
12309 }
12310 }
e7d1c40c
AM
12311 stubs_always_before_branch = htab->params->group_size < 0;
12312 if (htab->params->group_size < 0)
12313 stub_group_size = -htab->params->group_size;
721956f4 12314 else
e7d1c40c 12315 stub_group_size = htab->params->group_size;
721956f4 12316
6f20ed8a
AM
12317 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12318 return FALSE;
721956f4 12319
c9301e31
AM
12320#define STUB_SHRINK_ITER 20
12321 /* Loop until no stubs added. After iteration 20 of this loop we may
12322 exit on a stub section shrinking. This is to break out of a
12323 pathological case where adding stubs on one iteration decreases
12324 section gaps (perhaps due to alignment), which then requires
12325 fewer or smaller stubs on the next iteration. */
12326
721956f4
AM
12327 while (1)
12328 {
12329 bfd *input_bfd;
12330 unsigned int bfd_indx;
a4b6fadd 12331 struct map_stub *group;
721956f4
AM
12332
12333 htab->stub_iteration += 1;
721956f4
AM
12334
12335 for (input_bfd = info->input_bfds, bfd_indx = 0;
12336 input_bfd != NULL;
c72f2fb2 12337 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12338 {
12339 Elf_Internal_Shdr *symtab_hdr;
12340 asection *section;
6cdc0ccc 12341 Elf_Internal_Sym *local_syms = NULL;
721956f4 12342
0c8d6e5c 12343 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12344 continue;
12345
721956f4 12346 /* We'll need the symbol table in a second. */
0ffa91dd 12347 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12348 if (symtab_hdr->sh_info == 0)
12349 continue;
12350
721956f4
AM
12351 /* Walk over each section attached to the input bfd. */
12352 for (section = input_bfd->sections;
12353 section != NULL;
12354 section = section->next)
12355 {
721956f4 12356 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12357
12358 /* If there aren't any relocs, then there's nothing more
12359 to do. */
12360 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12361 || (section->flags & SEC_ALLOC) == 0
12362 || (section->flags & SEC_LOAD) == 0
12363 || (section->flags & SEC_CODE) == 0
721956f4
AM
12364 || section->reloc_count == 0)
12365 continue;
12366
12367 /* If this section is a link-once section that will be
12368 discarded, then don't create any stubs. */
12369 if (section->output_section == NULL
927be08e 12370 || section->output_section->owner != info->output_bfd)
721956f4
AM
12371 continue;
12372
1e2f5b6e
AM
12373 /* Get the relocs. */
12374 internal_relocs
4ce794b7 12375 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12376 info->keep_memory);
721956f4 12377 if (internal_relocs == NULL)
1e2f5b6e 12378 goto error_ret_free_local;
721956f4
AM
12379
12380 /* Now examine each relocation. */
12381 irela = internal_relocs;
12382 irelaend = irela + section->reloc_count;
12383 for (; irela < irelaend; irela++)
12384 {
4ce794b7
AM
12385 enum elf_ppc64_reloc_type r_type;
12386 unsigned int r_indx;
721956f4
AM
12387 enum ppc_stub_type stub_type;
12388 struct ppc_stub_hash_entry *stub_entry;
8387904d 12389 asection *sym_sec, *code_sec;
e054468f 12390 bfd_vma sym_value, code_value;
721956f4 12391 bfd_vma destination;
6911b7dc 12392 unsigned long local_off;
8843416a 12393 bfd_boolean ok_dest;
721956f4 12394 struct ppc_link_hash_entry *hash;
8387904d 12395 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12396 struct elf_link_hash_entry *h;
12397 Elf_Internal_Sym *sym;
721956f4
AM
12398 char *stub_name;
12399 const asection *id_sec;
74f0fb50 12400 struct _opd_sec_data *opd;
e054468f 12401 struct plt_entry *plt_ent;
721956f4
AM
12402
12403 r_type = ELF64_R_TYPE (irela->r_info);
12404 r_indx = ELF64_R_SYM (irela->r_info);
12405
4ce794b7 12406 if (r_type >= R_PPC64_max)
721956f4
AM
12407 {
12408 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12409 goto error_ret_free_internal;
721956f4
AM
12410 }
12411
12412 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12413 if (r_type != R_PPC64_REL24
12414 && r_type != R_PPC64_REL14
12415 && r_type != R_PPC64_REL14_BRTAKEN
12416 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12417 continue;
12418
12419 /* Now determine the call target, its name, value,
12420 section. */
411e1bfb
AM
12421 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12422 r_indx, input_bfd))
12423 goto error_ret_free_internal;
12424 hash = (struct ppc_link_hash_entry *) h;
12425
8843416a 12426 ok_dest = FALSE;
8387904d 12427 fdh = NULL;
7fe2b9a6 12428 sym_value = 0;
411e1bfb 12429 if (hash == NULL)
721956f4 12430 {
411e1bfb 12431 sym_value = sym->st_value;
c27b8c2a
AM
12432 if (sym_sec != NULL
12433 && sym_sec->output_section != NULL)
12434 ok_dest = TRUE;
721956f4 12435 }
7fe2b9a6
AM
12436 else if (hash->elf.root.type == bfd_link_hash_defined
12437 || hash->elf.root.type == bfd_link_hash_defweak)
12438 {
12439 sym_value = hash->elf.root.u.def.value;
12440 if (sym_sec->output_section != NULL)
12441 ok_dest = TRUE;
12442 }
12443 else if (hash->elf.root.type == bfd_link_hash_undefweak
12444 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12445 {
99877b66 12446 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12447 use the func descriptor sym instead if it is
12448 defined. */
ceb1f1ef 12449 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12450 && hash->oh != NULL)
8387904d 12451 {
8c5b4e52 12452 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12453 if (fdh->elf.root.type == bfd_link_hash_defined
12454 || fdh->elf.root.type == bfd_link_hash_defweak)
12455 {
12456 sym_sec = fdh->elf.root.u.def.section;
12457 sym_value = fdh->elf.root.u.def.value;
12458 if (sym_sec->output_section != NULL)
12459 ok_dest = TRUE;
12460 }
99877b66
AM
12461 else
12462 fdh = NULL;
8387904d 12463 }
7fe2b9a6
AM
12464 }
12465 else
12466 {
12467 bfd_set_error (bfd_error_bad_value);
12468 goto error_ret_free_internal;
721956f4
AM
12469 }
12470
8843416a 12471 destination = 0;
6911b7dc 12472 local_off = 0;
8843416a
AM
12473 if (ok_dest)
12474 {
12475 sym_value += irela->r_addend;
12476 destination = (sym_value
12477 + sym_sec->output_offset
12478 + sym_sec->output_section->vma);
6911b7dc
AM
12479 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12480 ? hash->elf.other
12481 : sym->st_other);
8843416a
AM
12482 }
12483
8387904d 12484 code_sec = sym_sec;
e054468f 12485 code_value = sym_value;
74f0fb50
AM
12486 opd = get_opd_info (sym_sec);
12487 if (opd != NULL)
8387904d
AM
12488 {
12489 bfd_vma dest;
12490
74f0fb50 12491 if (hash == NULL && opd->adjust != NULL)
8387904d 12492 {
51aecdc5 12493 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12494 if (adjust == -1)
12495 continue;
e054468f 12496 code_value += adjust;
8387904d
AM
12497 sym_value += adjust;
12498 }
12499 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12500 &code_sec, &code_value, FALSE);
8387904d
AM
12501 if (dest != (bfd_vma) -1)
12502 {
12503 destination = dest;
12504 if (fdh != NULL)
12505 {
12506 /* Fixup old ABI sym to point at code
12507 entry. */
99877b66 12508 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12509 hash->elf.root.u.def.section = code_sec;
e054468f 12510 hash->elf.root.u.def.value = code_value;
8387904d
AM
12511 }
12512 }
12513 }
12514
721956f4 12515 /* Determine what (if any) linker stub is needed. */
e054468f 12516 plt_ent = NULL;
721956f4 12517 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12518 &plt_ent, destination,
12519 local_off);
ad8e1ba5
AM
12520
12521 if (stub_type != ppc_stub_plt_call)
12522 {
12523 /* Check whether we need a TOC adjusting stub.
12524 Since the linker pastes together pieces from
12525 different object files when creating the
12526 _init and _fini functions, it may be that a
12527 call to what looks like a local sym is in
12528 fact a call needing a TOC adjustment. */
8387904d
AM
12529 if (code_sec != NULL
12530 && code_sec->output_section != NULL
6f20ed8a
AM
12531 && (htab->sec_info[code_sec->id].toc_off
12532 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12533 && (code_sec->has_toc_reloc
12534 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12535 stub_type = ppc_stub_long_branch_r2off;
12536 }
12537
721956f4
AM
12538 if (stub_type == ppc_stub_none)
12539 continue;
12540
411e1bfb
AM
12541 /* __tls_get_addr calls might be eliminated. */
12542 if (stub_type != ppc_stub_plt_call
12543 && hash != NULL
8387904d
AM
12544 && (hash == htab->tls_get_addr
12545 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12546 && section->has_tls_reloc
12547 && irela != internal_relocs)
12548 {
12549 /* Get tls info. */
f961d9dd 12550 unsigned char *tls_mask;
411e1bfb 12551
3a71aa26 12552 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12553 irela - 1, input_bfd))
12554 goto error_ret_free_internal;
e7b938ca 12555 if (*tls_mask != 0)
411e1bfb
AM
12556 continue;
12557 }
12558
f378ab09 12559 if (stub_type == ppc_stub_plt_call)
794e51c0 12560 {
6e1816be
AM
12561 if (!htab->opd_abi
12562 && htab->params->plt_localentry0 != 0
12563 && is_elfv2_localentry0 (&hash->elf))
12564 htab->has_plt_localentry0 = 1;
12565 else if (irela + 1 < irelaend
12566 && irela[1].r_offset == irela->r_offset + 4
12567 && (ELF64_R_TYPE (irela[1].r_info)
12568 == R_PPC64_TOCSAVE))
f378ab09
AM
12569 {
12570 if (!tocsave_find (htab, INSERT,
12571 &local_syms, irela + 1, input_bfd))
12572 goto error_ret_free_internal;
12573 }
f378ab09
AM
12574 else
12575 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12576 }
3b421ab3 12577
721956f4 12578 /* Support for grouping stub sections. */
6f20ed8a 12579 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12580
12581 /* Get the name of this stub. */
12582 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12583 if (!stub_name)
12584 goto error_ret_free_internal;
12585
12586 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12587 stub_name, FALSE, FALSE);
721956f4
AM
12588 if (stub_entry != NULL)
12589 {
12590 /* The proper stub has already been created. */
12591 free (stub_name);
794e51c0
AM
12592 if (stub_type == ppc_stub_plt_call_r2save)
12593 stub_entry->stub_type = stub_type;
721956f4
AM
12594 continue;
12595 }
12596
25f53a85 12597 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12598 if (stub_entry == NULL)
12599 {
12600 free (stub_name);
6cdc0ccc
AM
12601 error_ret_free_internal:
12602 if (elf_section_data (section)->relocs == NULL)
12603 free (internal_relocs);
12604 error_ret_free_local:
12605 if (local_syms != NULL
12606 && (symtab_hdr->contents
12607 != (unsigned char *) local_syms))
12608 free (local_syms);
b34976b6 12609 return FALSE;
721956f4
AM
12610 }
12611
ad8e1ba5 12612 stub_entry->stub_type = stub_type;
794e51c0
AM
12613 if (stub_type != ppc_stub_plt_call
12614 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12615 {
12616 stub_entry->target_value = code_value;
12617 stub_entry->target_section = code_sec;
12618 }
12619 else
12620 {
12621 stub_entry->target_value = sym_value;
12622 stub_entry->target_section = sym_sec;
12623 }
721956f4 12624 stub_entry->h = hash;
e054468f 12625 stub_entry->plt_ent = plt_ent;
6911b7dc 12626 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12627
12628 if (stub_entry->h != NULL)
12629 htab->stub_globals += 1;
721956f4
AM
12630 }
12631
12632 /* We're done with the internal relocs, free them. */
6cdc0ccc 12633 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12634 free (internal_relocs);
721956f4 12635 }
6cdc0ccc
AM
12636
12637 if (local_syms != NULL
12638 && symtab_hdr->contents != (unsigned char *) local_syms)
12639 {
12640 if (!info->keep_memory)
12641 free (local_syms);
12642 else
12643 symtab_hdr->contents = (unsigned char *) local_syms;
12644 }
721956f4
AM
12645 }
12646
5c3dead3 12647 /* We may have added some stubs. Find out the new size of the
721956f4 12648 stub sections. */
d4aaa2a0
AM
12649 for (group = htab->group; group != NULL; group = group->next)
12650 if (group->stub_sec != NULL)
ee75fd95 12651 {
d4aaa2a0
AM
12652 asection *stub_sec = group->stub_sec;
12653
ea3d7d1c
AM
12654 if (htab->stub_iteration <= STUB_SHRINK_ITER
12655 || stub_sec->rawsize < stub_sec->size)
12656 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12657 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12658 stub_sec->size = 0;
12659 stub_sec->reloc_count = 0;
84f5d08e 12660 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12661 }
eea6121a 12662
ba21f564
AM
12663 if (htab->stub_iteration <= STUB_SHRINK_ITER
12664 || htab->brlt->rawsize < htab->brlt->size)
12665 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12666 htab->brlt->size = 0;
84f5d08e
AM
12667 htab->brlt->reloc_count = 0;
12668 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12669 if (htab->relbrlt != NULL)
eea6121a 12670 htab->relbrlt->size = 0;
721956f4 12671
63bc6f6c 12672 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12673
a4b6fadd
AM
12674 for (group = htab->group; group != NULL; group = group->next)
12675 if (group->needs_save_res)
12676 group->stub_sec->size += htab->sfpr->size;
12677
176a0d42
AM
12678 if (info->emitrelocations
12679 && htab->glink != NULL && htab->glink->size != 0)
12680 {
12681 htab->glink->reloc_count = 1;
12682 htab->glink->flags |= SEC_RELOC;
12683 }
12684
58d180e8
AM
12685 if (htab->glink_eh_frame != NULL
12686 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12687 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12688 {
2e0ce1c8 12689 size_t size = 0, align = 4;
58d180e8 12690
d4aaa2a0
AM
12691 for (group = htab->group; group != NULL; group = group->next)
12692 if (group->stub_sec != NULL)
12693 size += stub_eh_frame_size (group, align);
58d180e8 12694 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12695 size += (24 + align - 1) & -align;
58d180e8 12696 if (size != 0)
2e0ce1c8
AM
12697 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12698 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12699 size = (size + align - 1) & -align;
58d180e8
AM
12700 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12701 htab->glink_eh_frame->size = size;
12702 }
12703
e7d1c40c 12704 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12705 for (group = htab->group; group != NULL; group = group->next)
12706 if (group->stub_sec != NULL)
691d2e9a
AM
12707 {
12708 int align = abs (htab->params->plt_stub_align);
12709 group->stub_sec->size
12710 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12711 }
d4aaa2a0
AM
12712
12713 for (group = htab->group; group != NULL; group = group->next)
12714 if (group->stub_sec != NULL
12715 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12716 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12717 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12718 break;
12719
d4aaa2a0 12720 if (group == NULL
ba21f564
AM
12721 && (htab->brlt->rawsize == htab->brlt->size
12722 || (htab->stub_iteration > STUB_SHRINK_ITER
12723 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
12724 && (htab->glink_eh_frame == NULL
12725 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12726 break;
12727
721956f4 12728 /* Ask the linker to do its stuff. */
e7d1c40c 12729 (*htab->params->layout_sections_again) ();
721956f4
AM
12730 }
12731
da44f4e5
AM
12732 if (htab->glink_eh_frame != NULL
12733 && htab->glink_eh_frame->size != 0)
12734 {
12735 bfd_vma val;
12736 bfd_byte *p, *last_fde;
12737 size_t last_fde_len, size, align, pad;
d4aaa2a0 12738 struct map_stub *group;
da44f4e5
AM
12739
12740 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12741 if (p == NULL)
12742 return FALSE;
12743 htab->glink_eh_frame->contents = p;
12744 last_fde = p;
2e0ce1c8 12745 align = 4;
da44f4e5
AM
12746
12747 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12748 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12749 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12750 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12751 p += last_fde_len + 4;
da44f4e5 12752
d4aaa2a0
AM
12753 for (group = htab->group; group != NULL; group = group->next)
12754 if (group->stub_sec != NULL)
da44f4e5
AM
12755 {
12756 last_fde = p;
d4aaa2a0 12757 last_fde_len = stub_eh_frame_size (group, align) - 4;
da44f4e5 12758 /* FDE length. */
2e0ce1c8 12759 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12760 p += 4;
12761 /* CIE pointer. */
12762 val = p - htab->glink_eh_frame->contents;
12763 bfd_put_32 (htab->elf.dynobj, val, p);
12764 p += 4;
12765 /* Offset to stub section, written later. */
12766 p += 4;
12767 /* stub section size. */
d4aaa2a0 12768 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12769 p += 4;
12770 /* Augmentation. */
12771 p += 1;
d4aaa2a0
AM
12772 if (group->tls_get_addr_opt_bctrl != -1u)
12773 {
12774 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12775
12776 /* This FDE needs more than just the default.
12777 Describe __tls_get_addr_opt stub LR. */
12778 if (to_bctrl < 64)
12779 *p++ = DW_CFA_advance_loc + to_bctrl;
12780 else if (to_bctrl < 256)
12781 {
12782 *p++ = DW_CFA_advance_loc1;
12783 *p++ = to_bctrl;
12784 }
12785 else if (to_bctrl < 65536)
12786 {
12787 *p++ = DW_CFA_advance_loc2;
12788 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12789 p += 2;
12790 }
12791 else
12792 {
12793 *p++ = DW_CFA_advance_loc4;
12794 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12795 p += 4;
12796 }
12797 *p++ = DW_CFA_offset_extended_sf;
12798 *p++ = 65;
12799 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12800 *p++ = DW_CFA_advance_loc + 4;
12801 *p++ = DW_CFA_restore_extended;
12802 *p++ = 65;
12803 }
da44f4e5 12804 /* Pad. */
d4aaa2a0 12805 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12806 }
12807 if (htab->glink != NULL && htab->glink->size != 0)
12808 {
12809 last_fde = p;
2e0ce1c8 12810 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12811 /* FDE length. */
2e0ce1c8 12812 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12813 p += 4;
12814 /* CIE pointer. */
12815 val = p - htab->glink_eh_frame->contents;
12816 bfd_put_32 (htab->elf.dynobj, val, p);
12817 p += 4;
12818 /* Offset to .glink, written later. */
12819 p += 4;
12820 /* .glink size. */
12821 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12822 p += 4;
12823 /* Augmentation. */
12824 p += 1;
12825
12826 *p++ = DW_CFA_advance_loc + 1;
12827 *p++ = DW_CFA_register;
12828 *p++ = 65;
9f08fa5c 12829 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 12830 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
12831 *p++ = DW_CFA_restore_extended;
12832 *p++ = 65;
2e0ce1c8 12833 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12834 }
12835 /* Subsume any padding into the last FDE if user .eh_frame
12836 sections are aligned more than glink_eh_frame. Otherwise any
12837 zero padding will be seen as a terminator. */
2e0ce1c8 12838 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12839 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12840 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12841 htab->glink_eh_frame->size = size + pad;
12842 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12843 }
12844
d969d15f
AM
12845 maybe_strip_output (info, htab->brlt);
12846 if (htab->glink_eh_frame != NULL)
12847 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12848
b34976b6 12849 return TRUE;
721956f4
AM
12850}
12851
12852/* Called after we have determined section placement. If sections
805fc799 12853 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12854
805fc799 12855bfd_vma
1c865ab2 12856ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12857{
805fc799 12858 asection *s;
a27e685f 12859 bfd_vma TOCstart, adjust;
721956f4 12860
43417696
AM
12861 if (info != NULL)
12862 {
12863 struct elf_link_hash_entry *h;
12864 struct elf_link_hash_table *htab = elf_hash_table (info);
12865
12866 if (is_elf_hash_table (htab)
12867 && htab->hgot != NULL)
12868 h = htab->hgot;
12869 else
12870 {
12871 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12872 if (is_elf_hash_table (htab))
12873 htab->hgot = h;
12874 }
12875 if (h != NULL
12876 && h->root.type == bfd_link_hash_defined
12877 && !h->root.linker_def
12878 && (!is_elf_hash_table (htab)
12879 || h->def_regular))
12880 {
12881 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12882 + h->root.u.def.section->output_offset
12883 + h->root.u.def.section->output_section->vma);
12884 _bfd_set_gp_value (obfd, TOCstart);
12885 return TOCstart;
12886 }
12887 }
12888
805fc799
AM
12889 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12890 order. The TOC starts where the first of these sections starts. */
12891 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12892 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12893 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12894 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12895 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12896 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12897 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12898 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12899 {
12900 /* This may happen for
12901 o references to TOC base (SYM@toc / TOC[tc0]) without a
12902 .toc directive
12903 o bad linker script
12904 o --gc-sections and empty TOC sections
12905
12906 FIXME: Warn user? */
12907
12908 /* Look for a likely section. We probably won't even be
12909 using TOCstart. */
12910 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12911 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12912 | SEC_EXCLUDE))
805fc799
AM
12913 == (SEC_ALLOC | SEC_SMALL_DATA))
12914 break;
721956f4 12915 if (s == NULL)
805fc799 12916 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12917 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12918 == (SEC_ALLOC | SEC_SMALL_DATA))
12919 break;
721956f4 12920 if (s == NULL)
805fc799 12921 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12922 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12923 == SEC_ALLOC)
805fc799 12924 break;
721956f4 12925 if (s == NULL)
805fc799 12926 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12927 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12928 break;
12929 }
721956f4 12930
805fc799
AM
12931 TOCstart = 0;
12932 if (s != NULL)
12933 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12934
a27e685f
AM
12935 /* Force alignment. */
12936 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12937 TOCstart -= adjust;
1c865ab2
AM
12938 _bfd_set_gp_value (obfd, TOCstart);
12939
810d4e75 12940 if (info != NULL && s != NULL)
1c865ab2
AM
12941 {
12942 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12943
810d4e75
AM
12944 if (htab != NULL)
12945 {
12946 if (htab->elf.hgot != NULL)
12947 {
a27e685f 12948 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12949 htab->elf.hgot->root.u.def.section = s;
12950 }
12951 }
12952 else
1c865ab2 12953 {
810d4e75
AM
12954 struct bfd_link_hash_entry *bh = NULL;
12955 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12956 s, TOC_BASE_OFF - adjust,
12957 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12958 }
12959 }
805fc799 12960 return TOCstart;
721956f4
AM
12961}
12962
a345bc8d
AM
12963/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12964 write out any global entry stubs. */
12965
12966static bfd_boolean
12967build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12968{
12969 struct bfd_link_info *info;
12970 struct ppc_link_hash_table *htab;
12971 struct plt_entry *pent;
12972 asection *s;
12973
12974 if (h->root.type == bfd_link_hash_indirect)
12975 return TRUE;
12976
12977 if (!h->pointer_equality_needed)
12978 return TRUE;
12979
12980 if (h->def_regular)
12981 return TRUE;
12982
12983 info = inf;
12984 htab = ppc_hash_table (info);
12985 if (htab == NULL)
12986 return FALSE;
12987
9e390558 12988 s = htab->global_entry;
a345bc8d
AM
12989 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12990 if (pent->plt.offset != (bfd_vma) -1
12991 && pent->addend == 0)
12992 {
12993 bfd_byte *p;
12994 asection *plt;
12995 bfd_vma off;
12996
a345bc8d 12997 p = s->contents + h->root.u.def.value;
33e44f2e 12998 plt = htab->elf.splt;
a345bc8d
AM
12999 if (!htab->elf.dynamic_sections_created
13000 || h->dynindx == -1)
33e44f2e 13001 plt = htab->elf.iplt;
a345bc8d
AM
13002 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13003 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13004
13005 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13006 {
13007 info->callbacks->einfo
c1c8c1ef 13008 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13009 h->root.root.string);
13010 bfd_set_error (bfd_error_bad_value);
13011 htab->stub_error = TRUE;
13012 }
13013
7341d5e2
AM
13014 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13015 if (htab->params->emit_stub_syms)
13016 {
13017 size_t len = strlen (h->root.root.string);
13018 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13019
13020 if (name == NULL)
13021 return FALSE;
13022
13023 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13024 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13025 if (h == NULL)
13026 return FALSE;
13027 if (h->root.type == bfd_link_hash_new)
13028 {
13029 h->root.type = bfd_link_hash_defined;
13030 h->root.u.def.section = s;
13031 h->root.u.def.value = p - s->contents;
13032 h->ref_regular = 1;
13033 h->def_regular = 1;
13034 h->ref_regular_nonweak = 1;
13035 h->forced_local = 1;
13036 h->non_elf = 0;
2ec55de3 13037 h->root.linker_def = 1;
7341d5e2
AM
13038 }
13039 }
13040
a345bc8d
AM
13041 if (PPC_HA (off) != 0)
13042 {
13043 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13044 p += 4;
13045 }
13046 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13047 p += 4;
13048 bfd_put_32 (s->owner, MTCTR_R12, p);
13049 p += 4;
407aa07c 13050 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13051 break;
13052 }
13053 return TRUE;
13054}
13055
721956f4
AM
13056/* Build all the stubs associated with the current output file.
13057 The stubs are kept in a hash table attached to the main linker
13058 hash table. This function is called via gldelf64ppc_finish. */
13059
b34976b6 13060bfd_boolean
e7d1c40c 13061ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13062 char **stats)
5d1634d7
AM
13063{
13064 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13065 struct map_stub *group;
721956f4 13066 asection *stub_sec;
5d1634d7 13067 bfd_byte *p;
e717da7e 13068 int stub_sec_count = 0;
5d1634d7 13069
4dfe6ac6
NC
13070 if (htab == NULL)
13071 return FALSE;
13072
eea6121a 13073 /* Allocate memory to hold the linker stubs. */
d4aaa2a0
AM
13074 for (group = htab->group; group != NULL; group = group->next)
13075 if ((stub_sec = group->stub_sec) != NULL
eea6121a 13076 && stub_sec->size != 0)
e717da7e 13077 {
e7d1c40c 13078 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13079 if (stub_sec->contents == NULL)
13080 return FALSE;
eea6121a 13081 stub_sec->size = 0;
e717da7e 13082 }
5d1634d7 13083
23eb7e01 13084 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13085 {
9f951329 13086 unsigned int indx;
ad8e1ba5 13087 bfd_vma plt0;
9f951329 13088
721956f4 13089 /* Build the .glink plt call stub. */
e7d1c40c 13090 if (htab->params->emit_stub_syms)
97b639ba
AM
13091 {
13092 struct elf_link_hash_entry *h;
468392fb
AM
13093 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13094 TRUE, FALSE, FALSE);
97b639ba
AM
13095 if (h == NULL)
13096 return FALSE;
13097 if (h->root.type == bfd_link_hash_new)
13098 {
13099 h->root.type = bfd_link_hash_defined;
13100 h->root.u.def.section = htab->glink;
ee4bf8d2 13101 h->root.u.def.value = 8;
f5385ebf
AM
13102 h->ref_regular = 1;
13103 h->def_regular = 1;
13104 h->ref_regular_nonweak = 1;
13105 h->forced_local = 1;
13106 h->non_elf = 0;
2ec55de3 13107 h->root.linker_def = 1;
97b639ba
AM
13108 }
13109 }
33e44f2e
AM
13110 plt0 = (htab->elf.splt->output_section->vma
13111 + htab->elf.splt->output_offset
13112 - 16);
176a0d42
AM
13113 if (info->emitrelocations)
13114 {
13115 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13116 if (r == NULL)
13117 return FALSE;
13118 r->r_offset = (htab->glink->output_offset
13119 + htab->glink->output_section->vma);
13120 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13121 r->r_addend = plt0;
13122 }
4ce794b7 13123 p = htab->glink->contents;
176a0d42 13124 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13125 bfd_put_64 (htab->glink->owner, plt0, p);
13126 p += 8;
b9e5796b
AM
13127 if (htab->opd_abi)
13128 {
13129 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13130 p += 4;
13131 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13132 p += 4;
13133 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13134 p += 4;
13135 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13136 p += 4;
13137 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13138 p += 4;
13139 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13140 p += 4;
13141 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13142 p += 4;
13143 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13144 p += 4;
13145 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13146 p += 4;
13147 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13148 p += 4;
13149 }
13150 else
13151 {
13152 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13153 p += 4;
13154 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13155 p += 4;
13156 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13157 p += 4;
f378ab09
AM
13158 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13159 p += 4;
b9e5796b
AM
13160 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13161 p += 4;
13162 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13163 p += 4;
13164 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13165 p += 4;
13166 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13167 p += 4;
13168 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13169 p += 4;
13170 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13171 p += 4;
13172 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13173 p += 4;
13174 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13175 p += 4;
13176 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13177 p += 4;
13178 }
407aa07c
AM
13179 bfd_put_32 (htab->glink->owner, BCTR, p);
13180 p += 4;
c75bc4f7 13181 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13182
9f951329
AM
13183 /* Build the .glink lazy link call stubs. */
13184 indx = 0;
9e390558 13185 while (p < htab->glink->contents + htab->glink->size)
9f951329 13186 {
b9e5796b 13187 if (htab->opd_abi)
9f951329 13188 {
b9e5796b
AM
13189 if (indx < 0x8000)
13190 {
13191 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13192 p += 4;
13193 }
13194 else
13195 {
13196 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13197 p += 4;
13198 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13199 p);
13200 p += 4;
13201 }
9f951329 13202 }
4ce794b7 13203 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13204 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13205 indx++;
9f951329
AM
13206 p += 4;
13207 }
5d1634d7 13208 }
5d1634d7 13209
9e390558
AM
13210 /* Build .glink global entry stubs. */
13211 if (htab->global_entry != NULL && htab->global_entry->size != 0)
13212 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13213
7341d5e2 13214 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13215 {
4ce794b7 13216 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13217 htab->brlt->size);
4ce794b7 13218 if (htab->brlt->contents == NULL)
b34976b6 13219 return FALSE;
721956f4 13220 }
ee75fd95 13221 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13222 {
13223 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13224 htab->relbrlt->size);
63bc6f6c
AM
13225 if (htab->relbrlt->contents == NULL)
13226 return FALSE;
13227 }
5d1634d7 13228
721956f4
AM
13229 /* Build the stubs as directed by the stub hash table. */
13230 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13231
a4b6fadd
AM
13232 for (group = htab->group; group != NULL; group = group->next)
13233 if (group->needs_save_res)
13234 {
13235 stub_sec = group->stub_sec;
13236 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13237 htab->sfpr->size);
13238 if (htab->params->emit_stub_syms)
13239 {
13240 unsigned int i;
13241
13242 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13243 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13244 return FALSE;
13245 }
13246 stub_sec->size += htab->sfpr->size;
13247 }
13248
aa8a7074
AM
13249 if (htab->relbrlt != NULL)
13250 htab->relbrlt->reloc_count = 0;
13251
e7d1c40c 13252 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13253 for (group = htab->group; group != NULL; group = group->next)
13254 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13255 {
13256 int align = abs (htab->params->plt_stub_align);
13257 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13258 }
794e51c0 13259
d4aaa2a0
AM
13260 for (group = htab->group; group != NULL; group = group->next)
13261 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13262 {
13263 stub_sec_count += 1;
c9301e31
AM
13264 if (stub_sec->rawsize != stub_sec->size
13265 && (htab->stub_iteration <= STUB_SHRINK_ITER
13266 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13267 break;
13268 }
5d1634d7 13269
25516cc5 13270 if (group != NULL)
5d1634d7 13271 {
b34976b6 13272 htab->stub_error = TRUE;
cf97bcb0 13273 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13274 }
721956f4 13275
d2a300cf
AM
13276 if (htab->stub_error)
13277 return FALSE;
13278
13279 if (stats != NULL)
13280 {
db84fff3 13281 size_t len;
d2a300cf
AM
13282 *stats = bfd_malloc (500);
13283 if (*stats == NULL)
13284 return FALSE;
13285
db84fff3
AM
13286 len = sprintf (*stats,
13287 ngettext ("linker stubs in %u group\n",
13288 "linker stubs in %u groups\n",
13289 stub_sec_count),
13290 stub_sec_count);
13291 sprintf (*stats + len, _(" branch %lu\n"
13292 " toc adjust %lu\n"
13293 " long branch %lu\n"
13294 " long toc adj %lu\n"
13295 " plt call %lu\n"
13296 " plt call toc %lu\n"
13297 " global entry %lu"),
4ce794b7
AM
13298 htab->stub_count[ppc_stub_long_branch - 1],
13299 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13300 htab->stub_count[ppc_stub_plt_branch - 1],
13301 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13302 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13303 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13304 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13305 }
13306 return TRUE;
5bd4f169
AM
13307}
13308
60124e18
AM
13309/* What to do when ld finds relocations against symbols defined in
13310 discarded sections. */
13311
13312static unsigned int
13313ppc64_elf_action_discarded (asection *sec)
13314{
13315 if (strcmp (".opd", sec->name) == 0)
13316 return 0;
13317
13318 if (strcmp (".toc", sec->name) == 0)
13319 return 0;
13320
bce50a28
JJ
13321 if (strcmp (".toc1", sec->name) == 0)
13322 return 0;
13323
60124e18
AM
13324 return _bfd_elf_default_action_discarded (sec);
13325}
13326
5bd4f169
AM
13327/* The RELOCATE_SECTION function is called by the ELF backend linker
13328 to handle the relocations for a section.
13329
13330 The relocs are always passed as Rela structures; if the section
13331 actually uses Rel structures, the r_addend field will always be
13332 zero.
13333
13334 This function is responsible for adjust the section contents as
13335 necessary, and (if using Rela relocs and generating a
1049f94e 13336 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13337 necessary.
13338
13339 This function does not have to worry about setting the reloc
13340 address or the reloc symbol index.
13341
13342 LOCAL_SYMS is a pointer to the swapped in local symbols.
13343
13344 LOCAL_SECTIONS is an array giving the section in the input file
13345 corresponding to the st_shndx field of each local symbol.
13346
13347 The global hash table entry for the global symbols can be found
13348 via elf_sym_hashes (input_bfd).
13349
1049f94e 13350 When generating relocatable output, this function must handle
5bd4f169
AM
13351 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13352 going to be the section symbol corresponding to the output
13353 section, which means that the addend must be adjusted
13354 accordingly. */
13355
b34976b6 13356static bfd_boolean
4ce794b7
AM
13357ppc64_elf_relocate_section (bfd *output_bfd,
13358 struct bfd_link_info *info,
13359 bfd *input_bfd,
13360 asection *input_section,
13361 bfd_byte *contents,
13362 Elf_Internal_Rela *relocs,
13363 Elf_Internal_Sym *local_syms,
13364 asection **local_sections)
5bd4f169 13365{
65f38f15 13366 struct ppc_link_hash_table *htab;
5bd4f169
AM
13367 Elf_Internal_Shdr *symtab_hdr;
13368 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13369 Elf_Internal_Rela *rel;
c316a17c 13370 Elf_Internal_Rela *wrel;
5bd4f169 13371 Elf_Internal_Rela *relend;
411e1bfb
AM
13372 Elf_Internal_Rela outrel;
13373 bfd_byte *loc;
411e1bfb 13374 struct got_entry **local_got_ents;
5bd4f169 13375 bfd_vma TOCstart;
b34976b6
AM
13376 bfd_boolean ret = TRUE;
13377 bfd_boolean is_opd;
794e51c0
AM
13378 /* Assume 'at' branch hints. */
13379 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13380 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13381
65f38f15 13382 /* Initialize howto table if needed. */
5bd4f169 13383 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13384 ppc_howto_init ();
13385
65f38f15 13386 htab = ppc_hash_table (info);
4dfe6ac6
NC
13387 if (htab == NULL)
13388 return FALSE;
ee75fd95
AM
13389
13390 /* Don't relocate stub sections. */
e7d1c40c 13391 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13392 return TRUE;
13393
0c8d6e5c 13394 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13395
411e1bfb 13396 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13397 TOCstart = elf_gp (output_bfd);
0ffa91dd 13398 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13399 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13400 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13401
c316a17c 13402 rel = wrel = relocs;
5bd4f169 13403 relend = relocs + input_section->reloc_count;
c316a17c 13404 for (; rel < relend; wrel++, rel++)
5bd4f169 13405 {
04c9666a 13406 enum elf_ppc64_reloc_type r_type;
31c76678 13407 bfd_vma addend;
5bd4f169
AM
13408 bfd_reloc_status_type r;
13409 Elf_Internal_Sym *sym;
13410 asection *sec;
039b3fef
AM
13411 struct elf_link_hash_entry *h_elf;
13412 struct ppc_link_hash_entry *h;
13413 struct ppc_link_hash_entry *fdh;
5bd4f169 13414 const char *sym_name;
0d4792f7 13415 unsigned long r_symndx, toc_symndx;
3a71aa26 13416 bfd_vma toc_addend;
f961d9dd
AM
13417 unsigned char tls_mask, tls_gd, tls_type;
13418 unsigned char sym_type;
5bd4f169 13419 bfd_vma relocation;
b34976b6
AM
13420 bfd_boolean unresolved_reloc;
13421 bfd_boolean warned;
bc30df16 13422 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13423 unsigned int insn;
e11840f9 13424 unsigned int mask;
721956f4
AM
13425 struct ppc_stub_hash_entry *stub_entry;
13426 bfd_vma max_br_offset;
13427 bfd_vma from;
c316a17c 13428 Elf_Internal_Rela orig_rel;
b80eed39
AM
13429 reloc_howto_type *howto;
13430 struct reloc_howto_struct alt_howto;
5bd4f169 13431
c316a17c
AM
13432 again:
13433 orig_rel = *rel;
13434
4ce794b7 13435 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13436 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13437
13438 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13439 symbol of the previous ADDR64 reloc. The symbol gives us the
13440 proper TOC base to use. */
13441 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13442 && wrel != relocs
13443 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13444 && is_opd)
c316a17c 13445 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13446
4ce794b7
AM
13447 sym = NULL;
13448 sec = NULL;
039b3fef 13449 h_elf = NULL;
4ce794b7 13450 sym_name = NULL;
b34976b6
AM
13451 unresolved_reloc = FALSE;
13452 warned = FALSE;
65f38f15 13453
0b13192e 13454 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13455 {
13456 /* It's a local symbol. */
74f0fb50 13457 struct _opd_sec_data *opd;
4025353c 13458
5bd4f169
AM
13459 sym = local_syms + r_symndx;
13460 sec = local_sections[r_symndx];
26c61ae5 13461 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13462 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13463 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13464 opd = get_opd_info (sec);
13465 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13466 {
51aecdc5
AM
13467 long adjust = opd->adjust[OPD_NDX (sym->st_value
13468 + rel->r_addend)];
4025353c
AM
13469 if (adjust == -1)
13470 relocation = 0;
13471 else
4cc603a5
AM
13472 {
13473 /* If this is a relocation against the opd section sym
13474 and we have edited .opd, adjust the reloc addend so
13475 that ld -r and ld --emit-relocs output is correct.
13476 If it is a reloc against some other .opd symbol,
13477 then the symbol value will be adjusted later. */
13478 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13479 rel->r_addend += adjust;
13480 else
13481 relocation += adjust;
13482 }
1e2f5b6e 13483 }
5bd4f169
AM
13484 }
13485 else
13486 {
62d887d4
L
13487 bfd_boolean ignored;
13488
b2a8e766
AM
13489 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13490 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13491 h_elf, sec, relocation,
62d887d4 13492 unresolved_reloc, warned, ignored);
039b3fef
AM
13493 sym_name = h_elf->root.root.string;
13494 sym_type = h_elf->type;
b69fdb4e
AM
13495 if (sec != NULL
13496 && sec->owner == output_bfd
13497 && strcmp (sec->name, ".opd") == 0)
13498 {
13499 /* This is a symbol defined in a linker script. All
13500 such are defined in output sections, even those
13501 defined by simple assignment from a symbol defined in
13502 an input section. Transfer the symbol to an
13503 appropriate input .opd section, so that a branch to
13504 this symbol will be mapped to the location specified
13505 by the opd entry. */
13506 struct bfd_link_order *lo;
13507 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13508 if (lo->type == bfd_indirect_link_order)
13509 {
13510 asection *isec = lo->u.indirect.section;
13511 if (h_elf->root.u.def.value >= isec->output_offset
13512 && h_elf->root.u.def.value < (isec->output_offset
13513 + isec->size))
13514 {
13515 h_elf->root.u.def.value -= isec->output_offset;
13516 h_elf->root.u.def.section = isec;
13517 sec = isec;
13518 break;
13519 }
13520 }
13521 }
5bd4f169 13522 }
039b3fef 13523 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13524
dbaa2011 13525 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13526 {
13527 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13528 input_bfd, input_section,
13529 contents + rel->r_offset);
13530 wrel->r_offset = rel->r_offset;
13531 wrel->r_info = 0;
13532 wrel->r_addend = 0;
13533
13534 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 13535 symbols defined in discarded sections. Not done for
c316a17c
AM
13536 non-debug to preserve relocs in .eh_frame which the
13537 eh_frame editing code expects to be present. */
13538 if (bfd_link_relocatable (info)
13539 && (input_section->flags & SEC_DEBUGGING))
13540 wrel--;
13541
13542 continue;
13543 }
ab96bf03 13544
0e1862bb 13545 if (bfd_link_relocatable (info))
c316a17c 13546 goto copy_reloc;
ab96bf03 13547
f40da81b
AM
13548 if (h != NULL && &h->elf == htab->elf.hgot)
13549 {
6f20ed8a 13550 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13551 sec = bfd_abs_section_ptr;
13552 unresolved_reloc = FALSE;
13553 }
13554
951fd09b
AM
13555 /* TLS optimizations. Replace instruction sequences and relocs
13556 based on information we collected in tls_optimize. We edit
13557 RELOCS so that --emit-relocs will output something sensible
13558 for the final instruction stream. */
13559 tls_mask = 0;
13560 tls_gd = 0;
0d4792f7 13561 toc_symndx = 0;
727fc41e
AM
13562 if (h != NULL)
13563 tls_mask = h->tls_mask;
13564 else if (local_got_ents != NULL)
411e1bfb 13565 {
e054468f
AM
13566 struct plt_entry **local_plt = (struct plt_entry **)
13567 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13568 unsigned char *lgot_masks = (unsigned char *)
e054468f 13569 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13570 tls_mask = lgot_masks[r_symndx];
13571 }
13572 if (tls_mask == 0
13573 && (r_type == R_PPC64_TLS
13574 || r_type == R_PPC64_TLSGD
13575 || r_type == R_PPC64_TLSLD))
13576 {
13577 /* Check for toc tls entries. */
f961d9dd 13578 unsigned char *toc_tls;
0d4792f7 13579
727fc41e
AM
13580 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13581 &local_syms, rel, input_bfd))
13582 return FALSE;
0d4792f7 13583
727fc41e
AM
13584 if (toc_tls)
13585 tls_mask = *toc_tls;
0d4792f7
AM
13586 }
13587
13588 /* Check that tls relocs are used with tls syms, and non-tls
13589 relocs are used with non-tls syms. */
cf35638d 13590 if (r_symndx != STN_UNDEF
0d4792f7
AM
13591 && r_type != R_PPC64_NONE
13592 && (h == NULL
039b3fef
AM
13593 || h->elf.root.type == bfd_link_hash_defined
13594 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13595 && (IS_PPC64_TLS_RELOC (r_type)
13596 != (sym_type == STT_TLS
13597 || (sym_type == STT_SECTION
13598 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13599 {
727fc41e
AM
13600 if (tls_mask != 0
13601 && (r_type == R_PPC64_TLS
13602 || r_type == R_PPC64_TLSGD
13603 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13604 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13605 ;
13606 else
25f53a85 13607 info->callbacks->einfo
1d483afe 13608 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13609 /* xgettext:c-format */
c1c8c1ef 13610 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 13611 /* xgettext:c-format */
c1c8c1ef 13612 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 13613 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13614 ppc64_elf_howto_table[r_type]->name,
13615 sym_name);
411e1bfb
AM
13616 }
13617
13618 /* Ensure reloc mapping code below stays sane. */
13619 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13620 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13621 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13622 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13623 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13624 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13625 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13626 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13627 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13628 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13629 abort ();
0d4792f7 13630
411e1bfb
AM
13631 switch (r_type)
13632 {
13633 default:
411e1bfb
AM
13634 break;
13635
ba761f19 13636 case R_PPC64_LO_DS_OPT:
95f0d0d2 13637 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13638 if ((insn & (0x3f << 26)) != 58u << 26)
13639 abort ();
13640 insn += (14u << 26) - (58u << 26);
95f0d0d2 13641 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13642 r_type = R_PPC64_TOC16_LO;
13643 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13644 break;
13645
411e1bfb
AM
13646 case R_PPC64_TOC16:
13647 case R_PPC64_TOC16_LO:
13648 case R_PPC64_TOC16_DS:
13649 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13650 {
13651 /* Check for toc tls entries. */
f961d9dd 13652 unsigned char *toc_tls;
951fd09b 13653 int retval;
411e1bfb 13654
3a71aa26
AM
13655 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13656 &local_syms, rel, input_bfd);
951fd09b 13657 if (retval == 0)
411e1bfb
AM
13658 return FALSE;
13659
13660 if (toc_tls)
13661 {
951fd09b 13662 tls_mask = *toc_tls;
411e1bfb
AM
13663 if (r_type == R_PPC64_TOC16_DS
13664 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13665 {
13666 if (tls_mask != 0
13667 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13668 goto toctprel;
13669 }
411e1bfb 13670 else
951fd09b
AM
13671 {
13672 /* If we found a GD reloc pair, then we might be
13673 doing a GD->IE transition. */
13674 if (retval == 2)
13675 {
13676 tls_gd = TLS_TPRELGD;
13677 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13678 goto tls_ldgd_opt;
951fd09b
AM
13679 }
13680 else if (retval == 3)
13681 {
13682 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13683 goto tls_ldgd_opt;
951fd09b
AM
13684 }
13685 }
411e1bfb
AM
13686 }
13687 }
13688 break;
13689
9d6ded02
AM
13690 case R_PPC64_GOT_TPREL16_HI:
13691 case R_PPC64_GOT_TPREL16_HA:
13692 if (tls_mask != 0
13693 && (tls_mask & TLS_TPREL) == 0)
13694 {
13695 rel->r_offset -= d_offset;
95f0d0d2 13696 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13697 r_type = R_PPC64_NONE;
13698 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13699 }
13700 break;
13701
411e1bfb
AM
13702 case R_PPC64_GOT_TPREL16_DS:
13703 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13704 if (tls_mask != 0
13705 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13706 {
81407a69 13707 toctprel:
95f0d0d2 13708 insn = bfd_get_32 (input_bfd,
c316a17c 13709 contents + rel->r_offset - d_offset);
411e1bfb
AM
13710 insn &= 31 << 21;
13711 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13712 bfd_put_32 (input_bfd, insn,
c316a17c 13713 contents + rel->r_offset - d_offset);
411e1bfb 13714 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13715 if (toc_symndx != 0)
13716 {
13717 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13718 rel->r_addend = toc_addend;
0d4792f7
AM
13719 /* We changed the symbol. Start over in order to
13720 get h, sym, sec etc. right. */
c316a17c 13721 goto again;
0d4792f7
AM
13722 }
13723 else
13724 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13725 }
13726 break;
13727
13728 case R_PPC64_TLS:
951fd09b
AM
13729 if (tls_mask != 0
13730 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13731 {
95f0d0d2 13732 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13733 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13734 if (insn == 0)
411e1bfb 13735 abort ();
95f0d0d2 13736 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13737 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13738 PPC64_TPREL16_LO which is at low-order half-word. */
13739 rel->r_offset += d_offset;
0d4792f7
AM
13740 r_type = R_PPC64_TPREL16_LO;
13741 if (toc_symndx != 0)
13742 {
13743 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13744 rel->r_addend = toc_addend;
0d4792f7
AM
13745 /* We changed the symbol. Start over in order to
13746 get h, sym, sec etc. right. */
c316a17c 13747 goto again;
0d4792f7
AM
13748 }
13749 else
13750 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13751 }
13752 break;
13753
411e1bfb
AM
13754 case R_PPC64_GOT_TLSGD16_HI:
13755 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13756 tls_gd = TLS_TPRELGD;
13757 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13758 goto tls_gdld_hi;
13759 break;
13760
411e1bfb
AM
13761 case R_PPC64_GOT_TLSLD16_HI:
13762 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13763 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13764 {
951fd09b
AM
13765 tls_gdld_hi:
13766 if ((tls_mask & tls_gd) != 0)
13767 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13768 + R_PPC64_GOT_TPREL16_DS);
13769 else
411e1bfb 13770 {
4fe5ca5b 13771 rel->r_offset -= d_offset;
95f0d0d2 13772 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13773 r_type = R_PPC64_NONE;
411e1bfb 13774 }
951fd09b 13775 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13776 }
13777 break;
13778
951fd09b
AM
13779 case R_PPC64_GOT_TLSGD16:
13780 case R_PPC64_GOT_TLSGD16_LO:
13781 tls_gd = TLS_TPRELGD;
13782 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13783 goto tls_ldgd_opt;
951fd09b 13784 break;
411e1bfb 13785
951fd09b
AM
13786 case R_PPC64_GOT_TLSLD16:
13787 case R_PPC64_GOT_TLSLD16_LO:
13788 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13789 {
b9f04fe0 13790 unsigned int insn1, insn2;
102890f0
AM
13791 bfd_vma offset;
13792
13793 tls_ldgd_opt:
727fc41e
AM
13794 offset = (bfd_vma) -1;
13795 /* If not using the newer R_PPC64_TLSGD/LD to mark
13796 __tls_get_addr calls, we must trust that the call
13797 stays with its arg setup insns, ie. that the next
13798 reloc is the __tls_get_addr call associated with
13799 the current reloc. Edit both insns. */
13800 if (input_section->has_tls_get_addr_call
13801 && rel + 1 < relend
13802 && branch_reloc_hash_match (input_bfd, rel + 1,
13803 htab->tls_get_addr,
13804 htab->tls_get_addr_fd))
13805 offset = rel[1].r_offset;
b86ac8e3
AM
13806 /* We read the low GOT_TLS (or TOC16) insn because we
13807 need to keep the destination reg. It may be
13808 something other than the usual r3, and moved to r3
13809 before the call by intervening code. */
95f0d0d2 13810 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13811 contents + rel->r_offset - d_offset);
102890f0 13812 if ((tls_mask & tls_gd) != 0)
411e1bfb 13813 {
102890f0 13814 /* IE */
b86ac8e3 13815 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13816 insn1 |= 58 << 26; /* ld */
13817 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13818 if (offset != (bfd_vma) -1)
f58d5a2d 13819 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13820 if ((tls_mask & TLS_EXPLICIT) == 0)
13821 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13822 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13823 else
102890f0
AM
13824 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13825 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13826 }
13827 else
13828 {
13829 /* LE */
b86ac8e3
AM
13830 insn1 &= 0x1f << 21;
13831 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13832 insn2 = 0x38630000; /* addi 3,3,0 */
13833 if (tls_gd == 0)
951fd09b 13834 {
102890f0 13835 /* Was an LD reloc. */
1d483afe
AM
13836 if (toc_symndx)
13837 sec = local_sections[toc_symndx];
13838 for (r_symndx = 0;
13839 r_symndx < symtab_hdr->sh_info;
13840 r_symndx++)
13841 if (local_sections[r_symndx] == sec)
13842 break;
13843 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13844 r_symndx = STN_UNDEF;
102890f0 13845 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13846 if (r_symndx != STN_UNDEF)
1d483afe
AM
13847 rel->r_addend -= (local_syms[r_symndx].st_value
13848 + sec->output_offset
13849 + sec->output_section->vma);
951fd09b 13850 }
102890f0 13851 else if (toc_symndx != 0)
3a71aa26
AM
13852 {
13853 r_symndx = toc_symndx;
13854 rel->r_addend = toc_addend;
13855 }
102890f0
AM
13856 r_type = R_PPC64_TPREL16_HA;
13857 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13858 if (offset != (bfd_vma) -1)
13859 {
13860 rel[1].r_info = ELF64_R_INFO (r_symndx,
13861 R_PPC64_TPREL16_LO);
13862 rel[1].r_offset = offset + d_offset;
13863 rel[1].r_addend = rel->r_addend;
13864 }
102890f0 13865 }
95f0d0d2 13866 bfd_put_32 (input_bfd, insn1,
3a71aa26 13867 contents + rel->r_offset - d_offset);
727fc41e 13868 if (offset != (bfd_vma) -1)
b9f04fe0 13869 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13870 if ((tls_mask & tls_gd) == 0
13871 && (tls_gd == 0 || toc_symndx != 0))
13872 {
13873 /* We changed the symbol. Start over in order
13874 to get h, sym, sec etc. right. */
c316a17c 13875 goto again;
727fc41e
AM
13876 }
13877 }
13878 break;
13879
13880 case R_PPC64_TLSGD:
13881 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13882 {
b9f04fe0 13883 unsigned int insn2;
727fc41e
AM
13884 bfd_vma offset = rel->r_offset;
13885
13886 if ((tls_mask & TLS_TPRELGD) != 0)
13887 {
13888 /* IE */
13889 r_type = R_PPC64_NONE;
13890 insn2 = 0x7c636a14; /* add 3,3,13 */
13891 }
13892 else
13893 {
13894 /* LE */
13895 if (toc_symndx != 0)
13896 {
13897 r_symndx = toc_symndx;
13898 rel->r_addend = toc_addend;
13899 }
13900 r_type = R_PPC64_TPREL16_LO;
13901 rel->r_offset = offset + d_offset;
13902 insn2 = 0x38630000; /* addi 3,3,0 */
13903 }
13904 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13905 /* Zap the reloc on the _tls_get_addr call too. */
13906 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13907 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13908 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13909 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13910 goto again;
411e1bfb 13911 }
411e1bfb
AM
13912 break;
13913
727fc41e
AM
13914 case R_PPC64_TLSLD:
13915 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13916 {
b9f04fe0 13917 unsigned int insn2;
727fc41e
AM
13918 bfd_vma offset = rel->r_offset;
13919
13920 if (toc_symndx)
13921 sec = local_sections[toc_symndx];
13922 for (r_symndx = 0;
13923 r_symndx < symtab_hdr->sh_info;
13924 r_symndx++)
13925 if (local_sections[r_symndx] == sec)
13926 break;
13927 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13928 r_symndx = STN_UNDEF;
727fc41e 13929 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13930 if (r_symndx != STN_UNDEF)
727fc41e
AM
13931 rel->r_addend -= (local_syms[r_symndx].st_value
13932 + sec->output_offset
13933 + sec->output_section->vma);
13934
13935 r_type = R_PPC64_TPREL16_LO;
13936 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13937 rel->r_offset = offset + d_offset;
13938 /* Zap the reloc on the _tls_get_addr call too. */
13939 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13940 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13941 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13942 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13943 goto again;
727fc41e
AM
13944 }
13945 break;
13946
411e1bfb 13947 case R_PPC64_DTPMOD64:
951fd09b
AM
13948 if (rel + 1 < relend
13949 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13950 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13951 {
951fd09b
AM
13952 if ((tls_mask & TLS_GD) == 0)
13953 {
13954 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13955 if ((tls_mask & TLS_TPRELGD) != 0)
13956 r_type = R_PPC64_TPREL64;
13957 else
13958 {
4ce794b7 13959 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13960 r_type = R_PPC64_NONE;
13961 }
13962 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13963 }
13964 }
13965 else
13966 {
13967 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13968 {
4ce794b7 13969 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13970 r_type = R_PPC64_NONE;
951fd09b 13971 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13972 }
411e1bfb
AM
13973 }
13974 break;
13975
13976 case R_PPC64_TPREL64:
951fd09b 13977 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13978 {
13979 r_type = R_PPC64_NONE;
13980 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13981 }
13982 break;
52a82034 13983
006589cf
AM
13984 case R_PPC64_ENTRY:
13985 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13986 if (!bfd_link_pic (info)
13987 && !info->traditional_format
13988 && relocation + 0x80008000 <= 0xffffffff)
13989 {
13990 unsigned int insn1, insn2;
13991
13992 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13993 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13994 if ((insn1 & ~0xfffc) == LD_R2_0R12
13995 && insn2 == ADD_R2_R2_R12)
13996 {
95f0d0d2 13997 bfd_put_32 (input_bfd,
006589cf
AM
13998 LIS_R2 + PPC_HA (relocation),
13999 contents + rel->r_offset);
95f0d0d2 14000 bfd_put_32 (input_bfd,
006589cf
AM
14001 ADDI_R2_R2 + PPC_LO (relocation),
14002 contents + rel->r_offset + 4);
14003 }
14004 }
14005 else
14006 {
14007 relocation -= (rel->r_offset
14008 + input_section->output_offset
14009 + input_section->output_section->vma);
14010 if (relocation + 0x80008000 <= 0xffffffff)
14011 {
14012 unsigned int insn1, insn2;
14013
14014 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14015 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14016 if ((insn1 & ~0xfffc) == LD_R2_0R12
14017 && insn2 == ADD_R2_R2_R12)
14018 {
95f0d0d2 14019 bfd_put_32 (input_bfd,
006589cf
AM
14020 ADDIS_R2_R12 + PPC_HA (relocation),
14021 contents + rel->r_offset);
95f0d0d2 14022 bfd_put_32 (input_bfd,
006589cf
AM
14023 ADDI_R2_R2 + PPC_LO (relocation),
14024 contents + rel->r_offset + 4);
14025 }
14026 }
14027 }
14028 break;
14029
52a82034
AM
14030 case R_PPC64_REL16_HA:
14031 /* If we are generating a non-PIC executable, edit
14032 . 0: addis 2,12,.TOC.-0b@ha
14033 . addi 2,2,.TOC.-0b@l
14034 used by ELFv2 global entry points to set up r2, to
14035 . lis 2,.TOC.@ha
14036 . addi 2,2,.TOC.@l
14037 if .TOC. is in range. */
0e1862bb 14038 if (!bfd_link_pic (info)
810d4e75 14039 && !info->traditional_format
006589cf 14040 && !htab->opd_abi
4f038ee5 14041 && rel->r_addend == d_offset
52a82034
AM
14042 && h != NULL && &h->elf == htab->elf.hgot
14043 && rel + 1 < relend
14044 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14045 && rel[1].r_offset == rel->r_offset + 4
14046 && rel[1].r_addend == rel->r_addend + 4
14047 && relocation + 0x80008000 <= 0xffffffff)
14048 {
14049 unsigned int insn1, insn2;
14050 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14051 insn1 = bfd_get_32 (input_bfd, contents + offset);
14052 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14053 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14054 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14055 {
14056 r_type = R_PPC64_ADDR16_HA;
14057 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14058 rel->r_addend -= d_offset;
14059 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14060 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14061 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14062 }
14063 }
14064 break;
411e1bfb
AM
14065 }
14066
14067 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14068 insn = 0;
b25116a9
AM
14069 max_br_offset = 1 << 25;
14070 addend = rel->r_addend;
bc30df16 14071 reloc_dest = DEST_NORMAL;
65f38f15 14072 switch (r_type)
5bd4f169
AM
14073 {
14074 default:
65f38f15 14075 break;
5bd4f169 14076
3b421ab3
AM
14077 case R_PPC64_TOCSAVE:
14078 if (relocation + addend == (rel->r_offset
14079 + input_section->output_offset
14080 + input_section->output_section->vma)
14081 && tocsave_find (htab, NO_INSERT,
14082 &local_syms, rel, input_bfd))
14083 {
14084 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14085 if (insn == NOP
14086 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14087 bfd_put_32 (input_bfd,
14088 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14089 contents + rel->r_offset);
14090 }
14091 break;
14092
65f38f15
AM
14093 /* Branch taken prediction relocations. */
14094 case R_PPC64_ADDR14_BRTAKEN:
14095 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14096 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14097 /* Fall through. */
65f38f15 14098
86c76c7b 14099 /* Branch not taken prediction relocations. */
65f38f15
AM
14100 case R_PPC64_ADDR14_BRNTAKEN:
14101 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14102 insn |= bfd_get_32 (input_bfd,
411e1bfb 14103 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14104 /* Fall through. */
86c76c7b 14105
b25116a9
AM
14106 case R_PPC64_REL14:
14107 max_br_offset = 1 << 15;
1a0670f3 14108 /* Fall through. */
5bd4f169 14109
65f38f15 14110 case R_PPC64_REL24:
ad8e1ba5
AM
14111 /* Calls to functions with a different TOC, such as calls to
14112 shared objects, need to alter the TOC pointer. This is
14113 done using a linkage stub. A REL24 branching to these
14114 linkage stubs needs to be followed by a nop, as the nop
14115 will be replaced with an instruction to restore the TOC
14116 base pointer. */
8387904d 14117 fdh = h;
b31867b6
AM
14118 if (h != NULL
14119 && h->oh != NULL
14120 && h->oh->is_func_descriptor)
14121 fdh = ppc_follow_link (h->oh);
31c76678
DK
14122 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14123 htab);
6abec6bc 14124 if (stub_entry != NULL
ad8e1ba5 14125 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14126 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14127 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14128 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14129 {
b25116a9 14130 bfd_boolean can_plt_call = FALSE;
721956f4 14131
6e1816be
AM
14132 if (stub_entry->stub_type == ppc_stub_plt_call
14133 && !htab->opd_abi
14134 && htab->params->plt_localentry0 != 0
14135 && is_elfv2_localentry0 (&h->elf))
14136 {
14137 /* The function doesn't use or change r2. */
14138 can_plt_call = TRUE;
14139 }
14140
f378ab09 14141 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14142 branch and link followed by a nop. The nop is
14143 replaced by an insn to restore r2. */
6e1816be 14144 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14145 {
ba8ca3e7
AM
14146 unsigned long br;
14147
14148 br = bfd_get_32 (input_bfd,
14149 contents + rel->r_offset);
14150 if ((br & 1) != 0)
41bd81ab 14151 {
ba8ca3e7
AM
14152 unsigned long nop;
14153
14154 nop = bfd_get_32 (input_bfd,
14155 contents + rel->r_offset + 4);
14156 if (nop == NOP
14157 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14158 {
ba8ca3e7
AM
14159 if (h != NULL
14160 && (h == htab->tls_get_addr_fd
14161 || h == htab->tls_get_addr)
7c9cf415 14162 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14163 {
14164 /* Special stub used, leave nop alone. */
14165 }
14166 else
a078d95a
AM
14167 bfd_put_32 (input_bfd,
14168 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14169 contents + rel->r_offset + 4);
14170 can_plt_call = TRUE;
a7f2871e 14171 }
41bd81ab 14172 }
5bd4f169 14173 }
721956f4 14174
ba8ca3e7 14175 if (!can_plt_call && h != NULL)
721956f4 14176 {
ba8ca3e7
AM
14177 const char *name = h->elf.root.root.string;
14178
14179 if (*name == '.')
14180 ++name;
14181
14182 if (strncmp (name, "__libc_start_main", 17) == 0
14183 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14184 {
ba8ca3e7
AM
14185 /* Allow crt1 branch to go via a toc adjusting
14186 stub. Other calls that never return could do
14187 the same, if we could detect such. */
b25116a9 14188 can_plt_call = TRUE;
6ab189d5 14189 }
ba8ca3e7
AM
14190 }
14191
14192 if (!can_plt_call)
14193 {
14194 /* g++ as of 20130507 emits self-calls without a
14195 following nop. This is arguably wrong since we
14196 have conflicting information. On the one hand a
14197 global symbol and on the other a local call
14198 sequence, but don't error for this special case.
14199 It isn't possible to cheaply verify we have
14200 exactly such a call. Allow all calls to the same
14201 section. */
14202 asection *code_sec = sec;
14203
14204 if (get_opd_info (sec) != NULL)
ad8e1ba5 14205 {
ba8ca3e7
AM
14206 bfd_vma off = (relocation + addend
14207 - sec->output_section->vma
14208 - sec->output_offset);
bc30df16 14209
ba8ca3e7 14210 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14211 }
ba8ca3e7
AM
14212 if (code_sec == input_section)
14213 can_plt_call = TRUE;
14214 }
14215
14216 if (!can_plt_call)
14217 {
4805fc55
AM
14218 if (stub_entry->stub_type == ppc_stub_plt_call
14219 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14220 info->callbacks->einfo
695344c0 14221 /* xgettext:c-format */
c1c8c1ef 14222 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14223 "recompile with -fPIC\n"),
14224 input_bfd, input_section, rel->r_offset, sym_name);
14225 else
14226 info->callbacks->einfo
695344c0 14227 /* xgettext:c-format */
c1c8c1ef 14228 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14229 "(-mcmodel=small toc adjust stub)\n"),
14230 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14231
14232 bfd_set_error (bfd_error_bad_value);
14233 ret = FALSE;
721956f4
AM
14234 }
14235
b25116a9 14236 if (can_plt_call
794e51c0
AM
14237 && (stub_entry->stub_type == ppc_stub_plt_call
14238 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14239 unresolved_reloc = FALSE;
14240 }
14241
6abec6bc
AM
14242 if ((stub_entry == NULL
14243 || stub_entry->stub_type == ppc_stub_long_branch
14244 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14245 && get_opd_info (sec) != NULL)
14246 {
14247 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14248 bfd_vma off = (relocation + addend
14249 - sec->output_section->vma
14250 - sec->output_offset);
aef36ac1 14251 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14252 if (dest != (bfd_vma) -1)
14253 {
14254 relocation = dest;
14255 addend = 0;
bc30df16 14256 reloc_dest = DEST_OPD;
8387904d
AM
14257 }
14258 }
14259
b25116a9
AM
14260 /* If the branch is out of reach we ought to have a long
14261 branch stub. */
14262 from = (rel->r_offset
14263 + input_section->output_offset
14264 + input_section->output_section->vma);
14265
6911b7dc
AM
14266 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14267 ? fdh->elf.other
14268 : sym->st_other);
14269
6abec6bc
AM
14270 if (stub_entry != NULL
14271 && (stub_entry->stub_type == ppc_stub_long_branch
14272 || stub_entry->stub_type == ppc_stub_plt_branch)
14273 && (r_type == R_PPC64_ADDR14_BRTAKEN
14274 || r_type == R_PPC64_ADDR14_BRNTAKEN
14275 || (relocation + addend - from + max_br_offset
14276 < 2 * max_br_offset)))
14277 /* Don't use the stub if this branch is in range. */
14278 stub_entry = NULL;
b25116a9
AM
14279
14280 if (stub_entry != NULL)
14281 {
14282 /* Munge up the value and addend so that we call the stub
14283 rather than the procedure directly. */
a4b6fadd
AM
14284 asection *stub_sec = stub_entry->group->stub_sec;
14285
14286 if (stub_entry->stub_type == ppc_stub_save_res)
14287 relocation += (stub_sec->output_offset
14288 + stub_sec->output_section->vma
14289 + stub_sec->size - htab->sfpr->size
14290 - htab->sfpr->output_offset
14291 - htab->sfpr->output_section->vma);
14292 else
14293 relocation = (stub_entry->stub_offset
14294 + stub_sec->output_offset
14295 + stub_sec->output_section->vma);
b25116a9 14296 addend = 0;
bc30df16 14297 reloc_dest = DEST_STUB;
3b421ab3 14298
954b63d4 14299 if ((stub_entry->stub_type == ppc_stub_plt_call
794e51c0
AM
14300 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14301 && (ALWAYS_EMIT_R2SAVE
14302 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14303 && rel + 1 < relend
14304 && rel[1].r_offset == rel->r_offset + 4
14305 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14306 relocation += 4;
b25116a9
AM
14307 }
14308
14309 if (insn != 0)
14310 {
794e51c0 14311 if (is_isa_v2)
721956f4 14312 {
b25116a9
AM
14313 /* Set 'a' bit. This is 0b00010 in BO field for branch
14314 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14315 for branch on CTR insns (BO == 1a00t or 1a01t). */
14316 if ((insn & (0x14 << 21)) == (0x04 << 21))
14317 insn |= 0x02 << 21;
14318 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14319 insn |= 0x08 << 21;
14320 else
14321 break;
14322 }
14323 else
14324 {
14325 /* Invert 'y' bit if not the default. */
4cc603a5 14326 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14327 insn ^= 0x01 << 21;
721956f4 14328 }
b25116a9 14329
95f0d0d2 14330 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14331 }
e86ce104 14332
06da1e8e
AM
14333 /* NOP out calls to undefined weak functions.
14334 We can thus call a weak function without first
14335 checking whether the function is defined. */
b25116a9 14336 else if (h != NULL
039b3fef 14337 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14338 && h->elf.dynindx == -1
b25116a9
AM
14339 && r_type == R_PPC64_REL24
14340 && relocation == 0
4cc603a5 14341 && addend == 0)
e86ce104 14342 {
95f0d0d2 14343 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14344 goto copy_reloc;
e86ce104 14345 }
65f38f15
AM
14346 break;
14347 }
5bd4f169 14348
65f38f15 14349 /* Set `addend'. */
411e1bfb 14350 tls_type = 0;
65f38f15
AM
14351 switch (r_type)
14352 {
14353 default:
cf97bcb0
AM
14354 /* xgettext:c-format */
14355 _bfd_error_handler (_("%pB: %s unsupported"),
14356 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14357
65f38f15 14358 bfd_set_error (bfd_error_bad_value);
b34976b6 14359 ret = FALSE;
c316a17c 14360 goto copy_reloc;
5bd4f169 14361
65f38f15 14362 case R_PPC64_NONE:
411e1bfb 14363 case R_PPC64_TLS:
727fc41e
AM
14364 case R_PPC64_TLSGD:
14365 case R_PPC64_TLSLD:
3b421ab3 14366 case R_PPC64_TOCSAVE:
04c9666a
AM
14367 case R_PPC64_GNU_VTINHERIT:
14368 case R_PPC64_GNU_VTENTRY:
006589cf 14369 case R_PPC64_ENTRY:
c316a17c 14370 goto copy_reloc;
5bd4f169
AM
14371
14372 /* GOT16 relocations. Like an ADDR16 using the symbol's
14373 address in the GOT as relocation value instead of the
411e1bfb 14374 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14375 symbol and put the symbol value there. */
411e1bfb
AM
14376 case R_PPC64_GOT_TLSGD16:
14377 case R_PPC64_GOT_TLSGD16_LO:
14378 case R_PPC64_GOT_TLSGD16_HI:
14379 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14380 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14381 goto dogot;
14382
14383 case R_PPC64_GOT_TLSLD16:
14384 case R_PPC64_GOT_TLSLD16_LO:
14385 case R_PPC64_GOT_TLSLD16_HI:
14386 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14387 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14388 goto dogot;
14389
14390 case R_PPC64_GOT_TPREL16_DS:
14391 case R_PPC64_GOT_TPREL16_LO_DS:
14392 case R_PPC64_GOT_TPREL16_HI:
14393 case R_PPC64_GOT_TPREL16_HA:
14394 tls_type = TLS_TLS | TLS_TPREL;
14395 goto dogot;
14396
14397 case R_PPC64_GOT_DTPREL16_DS:
14398 case R_PPC64_GOT_DTPREL16_LO_DS:
14399 case R_PPC64_GOT_DTPREL16_HI:
14400 case R_PPC64_GOT_DTPREL16_HA:
14401 tls_type = TLS_TLS | TLS_DTPREL;
14402 goto dogot;
14403
65f38f15
AM
14404 case R_PPC64_GOT16:
14405 case R_PPC64_GOT16_LO:
14406 case R_PPC64_GOT16_HI:
14407 case R_PPC64_GOT16_HA:
14408 case R_PPC64_GOT16_DS:
14409 case R_PPC64_GOT16_LO_DS:
411e1bfb 14410 dogot:
5bd4f169
AM
14411 {
14412 /* Relocation is to the entry for this symbol in the global
14413 offset table. */
e717da7e 14414 asection *got;
d881513a 14415 bfd_vma *offp;
5bd4f169 14416 bfd_vma off;
d881513a 14417 unsigned long indx = 0;
927be08e 14418 struct got_entry *ent;
65f38f15 14419
d881513a
AM
14420 if (tls_type == (TLS_TLS | TLS_LD)
14421 && (h == NULL
f5385ebf 14422 || !h->elf.def_dynamic))
927be08e 14423 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14424 else
5bd4f169 14425 {
d881513a
AM
14426 if (h != NULL)
14427 {
f0158f44
AM
14428 if (!htab->elf.dynamic_sections_created
14429 || h->elf.dynindx == -1
14430 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 14431 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
14432 /* This is actually a static link, or it is a
14433 -Bsymbolic link and the symbol is defined
14434 locally, or the symbol was forced to be local
14435 because of a version file. */
14436 ;
14437 else
14438 {
039b3fef 14439 indx = h->elf.dynindx;
d881513a
AM
14440 unresolved_reloc = FALSE;
14441 }
039b3fef 14442 ent = h->elf.got.glist;
d881513a 14443 }
411e1bfb 14444 else
5bd4f169 14445 {
d881513a
AM
14446 if (local_got_ents == NULL)
14447 abort ();
14448 ent = local_got_ents[r_symndx];
5bd4f169 14449 }
d881513a
AM
14450
14451 for (; ent != NULL; ent = ent->next)
31c76678 14452 if (ent->addend == orig_rel.r_addend
e717da7e 14453 && ent->owner == input_bfd
d881513a
AM
14454 && ent->tls_type == tls_type)
14455 break;
5bd4f169 14456 }
411e1bfb 14457
927be08e
AM
14458 if (ent == NULL)
14459 abort ();
14460 if (ent->is_indirect)
14461 ent = ent->got.ent;
14462 offp = &ent->got.offset;
14463 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14464 if (got == NULL)
14465 abort ();
14466
411e1bfb
AM
14467 /* The offset must always be a multiple of 8. We use the
14468 least significant bit to record whether we have already
14469 processed this entry. */
d881513a 14470 off = *offp;
411e1bfb
AM
14471 if ((off & 1) != 0)
14472 off &= ~1;
5bd4f169
AM
14473 else
14474 {
411e1bfb
AM
14475 /* Generate relocs for the dynamic linker, except in
14476 the case of TLSLD where we'll use one entry per
14477 module. */
25f23106
AM
14478 asection *relgot;
14479 bfd_boolean ifunc;
e717da7e 14480
d881513a 14481 *offp = off | 1;
25f23106
AM
14482 relgot = NULL;
14483 ifunc = (h != NULL
14484 ? h->elf.type == STT_GNU_IFUNC
14485 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14486 if (ifunc)
82e66161
AM
14487 {
14488 relgot = htab->elf.irelplt;
14489 if (indx == 0)
14490 htab->local_ifunc_resolver = 1;
14491 else if (is_static_defined (&h->elf))
14492 htab->maybe_local_ifunc_resolver = 1;
14493 }
f0158f44
AM
14494 else if (indx != 0
14495 || (bfd_link_pic (info)
14496 && (h == NULL
21d68fcd 14497 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 14498 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
14499 && !h->elf.def_dynamic))
14500 && !(tls_type == (TLS_TLS | TLS_TPREL)
14501 && bfd_link_executable (info)
14502 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 14503 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14504 if (relgot != NULL)
5bd4f169 14505 {
e717da7e
AM
14506 outrel.r_offset = (got->output_section->vma
14507 + got->output_offset
411e1bfb 14508 + off);
4cc603a5 14509 outrel.r_addend = addend;
d881513a 14510 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14511 {
411e1bfb 14512 outrel.r_addend = 0;
e515b051 14513 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14514 if (tls_type == (TLS_TLS | TLS_GD))
14515 {
e717da7e
AM
14516 loc = relgot->contents;
14517 loc += (relgot->reloc_count++
d881513a
AM
14518 * sizeof (Elf64_External_Rela));
14519 bfd_elf64_swap_reloca_out (output_bfd,
14520 &outrel, loc);
e515b051 14521 outrel.r_offset += 8;
4cc603a5 14522 outrel.r_addend = addend;
d881513a
AM
14523 outrel.r_info
14524 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14525 }
411e1bfb 14526 }
951fd09b 14527 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14528 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14529 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14530 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14531 else if (indx != 0)
14532 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14533 else
81407a69 14534 {
25f23106
AM
14535 if (ifunc)
14536 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14537 else
14538 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14539
14540 /* Write the .got section contents for the sake
14541 of prelink. */
e717da7e 14542 loc = got->contents + off;
23fbd6fa
JJ
14543 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14544 loc);
81407a69 14545 }
81407a69
AM
14546
14547 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14548 {
14549 outrel.r_addend += relocation;
14550 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14551 {
14552 if (htab->elf.tls_sec == NULL)
14553 outrel.r_addend = 0;
14554 else
14555 outrel.r_addend -= htab->elf.tls_sec->vma;
14556 }
e515b051 14557 }
e717da7e
AM
14558 loc = relgot->contents;
14559 loc += (relgot->reloc_count++
411e1bfb
AM
14560 * sizeof (Elf64_External_Rela));
14561 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14562 }
14563
ad8e1ba5 14564 /* Init the .got section contents here if we're not
81407a69 14565 emitting a reloc. */
d881513a 14566 else
411e1bfb 14567 {
4cc603a5 14568 relocation += addend;
f0158f44 14569 if (tls_type != 0)
411e1bfb 14570 {
989f9879
AM
14571 if (htab->elf.tls_sec == NULL)
14572 relocation = 0;
14573 else
14574 {
f0158f44
AM
14575 if (tls_type & TLS_LD)
14576 relocation = 0;
14577 else
14578 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 14579 if (tls_type & TLS_TPREL)
989f9879
AM
14580 relocation += DTP_OFFSET - TP_OFFSET;
14581 }
5bd4f169 14582
f0158f44 14583 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14584 {
14585 bfd_put_64 (output_bfd, relocation,
e717da7e 14586 got->contents + off + 8);
676ee2b5 14587 relocation = 1;
7b609f53 14588 }
411e1bfb
AM
14589 }
14590 bfd_put_64 (output_bfd, relocation,
e717da7e 14591 got->contents + off);
5bd4f169
AM
14592 }
14593 }
14594
65f38f15
AM
14595 if (off >= (bfd_vma) -2)
14596 abort ();
14597
bf102f86 14598 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14599 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14600 }
65f38f15
AM
14601 break;
14602
14603 case R_PPC64_PLT16_HA:
14604 case R_PPC64_PLT16_HI:
14605 case R_PPC64_PLT16_LO:
14606 case R_PPC64_PLT32:
14607 case R_PPC64_PLT64:
14608 /* Relocation is to the entry for this symbol in the
14609 procedure linkage table. */
cbf95972
AM
14610 {
14611 struct plt_entry **plt_list = NULL;
14612 if (h != NULL)
14613 plt_list = &h->elf.plt.plist;
14614 else if (local_got_ents != NULL)
14615 {
14616 struct plt_entry **local_plt = (struct plt_entry **)
14617 (local_got_ents + symtab_hdr->sh_info);
14618 unsigned char *local_got_tls_masks = (unsigned char *)
14619 (local_plt + symtab_hdr->sh_info);
14620 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14621 plt_list = local_plt + r_symndx;
14622 }
14623 if (plt_list)
14624 {
14625 struct plt_entry *ent;
65f38f15 14626
cbf95972
AM
14627 for (ent = *plt_list; ent != NULL; ent = ent->next)
14628 if (ent->plt.offset != (bfd_vma) -1
14629 && ent->addend == orig_rel.r_addend)
14630 {
14631 asection *plt;
14632
14633 plt = htab->elf.splt;
14634 if (!htab->elf.dynamic_sections_created
14635 || h == NULL
14636 || h->elf.dynindx == -1)
14637 plt = htab->elf.iplt;
14638 relocation = (plt->output_section->vma
14639 + plt->output_offset
14640 + ent->plt.offset);
14641 addend = 0;
14642 unresolved_reloc = FALSE;
14643 break;
14644 }
14645 }
14646 }
65f38f15 14647 break;
5bd4f169 14648
0b13192e
AM
14649 case R_PPC64_TOC:
14650 /* Relocation value is TOC base. */
14651 relocation = TOCstart;
cf35638d 14652 if (r_symndx == STN_UNDEF)
6f20ed8a 14653 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14654 else if (unresolved_reloc)
14655 ;
6f20ed8a
AM
14656 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14657 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14658 else
14659 unresolved_reloc = TRUE;
ab96bf03 14660 goto dodyn;
0b13192e 14661
5bd4f169
AM
14662 /* TOC16 relocs. We want the offset relative to the TOC base,
14663 which is the address of the start of the TOC plus 0x8000.
14664 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14665 in this order. */
65f38f15
AM
14666 case R_PPC64_TOC16:
14667 case R_PPC64_TOC16_LO:
14668 case R_PPC64_TOC16_HI:
14669 case R_PPC64_TOC16_DS:
14670 case R_PPC64_TOC16_LO_DS:
14671 case R_PPC64_TOC16_HA:
6f20ed8a 14672 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14673 break;
14674
14675 /* Relocate against the beginning of the section. */
65f38f15
AM
14676 case R_PPC64_SECTOFF:
14677 case R_PPC64_SECTOFF_LO:
14678 case R_PPC64_SECTOFF_HI:
14679 case R_PPC64_SECTOFF_DS:
14680 case R_PPC64_SECTOFF_LO_DS:
14681 case R_PPC64_SECTOFF_HA:
4ce794b7 14682 if (sec != NULL)
65f38f15 14683 addend -= sec->output_section->vma;
5bd4f169
AM
14684 break;
14685
25f23106
AM
14686 case R_PPC64_REL16:
14687 case R_PPC64_REL16_LO:
14688 case R_PPC64_REL16_HI:
14689 case R_PPC64_REL16_HA:
a680de9a 14690 case R_PPC64_REL16DX_HA:
25f23106
AM
14691 break;
14692
721956f4
AM
14693 case R_PPC64_REL14:
14694 case R_PPC64_REL14_BRNTAKEN:
14695 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14696 case R_PPC64_REL24:
14697 break;
14698
411e1bfb
AM
14699 case R_PPC64_TPREL16:
14700 case R_PPC64_TPREL16_LO:
14701 case R_PPC64_TPREL16_HI:
14702 case R_PPC64_TPREL16_HA:
14703 case R_PPC64_TPREL16_DS:
14704 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14705 case R_PPC64_TPREL16_HIGH:
14706 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14707 case R_PPC64_TPREL16_HIGHER:
14708 case R_PPC64_TPREL16_HIGHERA:
14709 case R_PPC64_TPREL16_HIGHEST:
14710 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14711 if (h != NULL
14712 && h->elf.root.type == bfd_link_hash_undefweak
14713 && h->elf.dynindx == -1)
14714 {
14715 /* Make this relocation against an undefined weak symbol
14716 resolve to zero. This is really just a tweak, since
14717 code using weak externs ought to check that they are
14718 defined before using them. */
14719 bfd_byte *p = contents + rel->r_offset - d_offset;
14720
95f0d0d2 14721 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14722 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14723 if (insn != 0)
95f0d0d2 14724 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14725 break;
14726 }
989f9879
AM
14727 if (htab->elf.tls_sec != NULL)
14728 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
14729 /* The TPREL16 relocs shouldn't really be used in shared
14730 libs or with non-local symbols as that will result in
14731 DT_TEXTREL being set, but support them anyway. */
14732 goto dodyn;
411e1bfb
AM
14733
14734 case R_PPC64_DTPREL16:
14735 case R_PPC64_DTPREL16_LO:
14736 case R_PPC64_DTPREL16_HI:
14737 case R_PPC64_DTPREL16_HA:
14738 case R_PPC64_DTPREL16_DS:
14739 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14740 case R_PPC64_DTPREL16_HIGH:
14741 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14742 case R_PPC64_DTPREL16_HIGHER:
14743 case R_PPC64_DTPREL16_HIGHERA:
14744 case R_PPC64_DTPREL16_HIGHEST:
14745 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14746 if (htab->elf.tls_sec != NULL)
14747 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14748 break;
14749
45965137
AM
14750 case R_PPC64_ADDR64_LOCAL:
14751 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14752 ? h->elf.other
14753 : sym->st_other);
14754 break;
14755
e515b051
AM
14756 case R_PPC64_DTPMOD64:
14757 relocation = 1;
14758 addend = 0;
14759 goto dodyn;
14760
411e1bfb 14761 case R_PPC64_TPREL64:
989f9879
AM
14762 if (htab->elf.tls_sec != NULL)
14763 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14764 goto dodyn;
14765
14766 case R_PPC64_DTPREL64:
989f9879
AM
14767 if (htab->elf.tls_sec != NULL)
14768 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14769 /* Fall through. */
411e1bfb 14770
65f38f15
AM
14771 /* Relocations that may need to be propagated if this is a
14772 dynamic object. */
04c9666a 14773 case R_PPC64_REL30:
65f38f15
AM
14774 case R_PPC64_REL32:
14775 case R_PPC64_REL64:
14776 case R_PPC64_ADDR14:
14777 case R_PPC64_ADDR14_BRNTAKEN:
14778 case R_PPC64_ADDR14_BRTAKEN:
14779 case R_PPC64_ADDR16:
14780 case R_PPC64_ADDR16_DS:
14781 case R_PPC64_ADDR16_HA:
14782 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14783 case R_PPC64_ADDR16_HIGH:
14784 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14785 case R_PPC64_ADDR16_HIGHER:
14786 case R_PPC64_ADDR16_HIGHERA:
14787 case R_PPC64_ADDR16_HIGHEST:
14788 case R_PPC64_ADDR16_HIGHESTA:
14789 case R_PPC64_ADDR16_LO:
14790 case R_PPC64_ADDR16_LO_DS:
14791 case R_PPC64_ADDR24:
65f38f15
AM
14792 case R_PPC64_ADDR32:
14793 case R_PPC64_ADDR64:
14794 case R_PPC64_UADDR16:
14795 case R_PPC64_UADDR32:
14796 case R_PPC64_UADDR64:
411e1bfb 14797 dodyn:
5d1634d7 14798 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14799 break;
14800
41bd81ab
AM
14801 if (NO_OPD_RELOCS && is_opd)
14802 break;
14803
8a9e8e72 14804 if (bfd_link_pic (info)
b1b07054
AM
14805 ? ((h == NULL
14806 || h->dyn_relocs != NULL)
14807 && ((h != NULL && pc_dynrelocs (h))
14808 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
14809 : (h != NULL
14810 ? h->dyn_relocs != NULL
d311bc8b 14811 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14812 {
b34976b6 14813 bfd_boolean skip, relocate;
65f38f15 14814 asection *sreloc;
1cf1f670 14815 bfd_vma out_off;
82e66161 14816 long indx = 0;
65f38f15
AM
14817
14818 /* When generating a dynamic object, these relocations
14819 are copied into the output file to be resolved at run
14820 time. */
14821
b34976b6
AM
14822 skip = FALSE;
14823 relocate = FALSE;
65f38f15 14824
1cf1f670
AM
14825 out_off = _bfd_elf_section_offset (output_bfd, info,
14826 input_section, rel->r_offset);
14827 if (out_off == (bfd_vma) -1)
b34976b6 14828 skip = TRUE;
1cf1f670 14829 else if (out_off == (bfd_vma) -2)
b34976b6 14830 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14831 out_off += (input_section->output_section->vma
14832 + input_section->output_offset);
14833 outrel.r_offset = out_off;
411e1bfb 14834 outrel.r_addend = rel->r_addend;
65f38f15 14835
1cf1f670
AM
14836 /* Optimize unaligned reloc use. */
14837 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14838 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14839 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14840 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14841 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14842 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14843 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14844 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14845 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14846
65f38f15 14847 if (skip)
0bb2d96a 14848 memset (&outrel, 0, sizeof outrel);
afe397ea 14849 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14850 && !is_opd
14851 && r_type != R_PPC64_TOC)
14acf4dc 14852 {
82e66161
AM
14853 indx = h->elf.dynindx;
14854 BFD_ASSERT (indx != -1);
14855 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 14856 }
65f38f15
AM
14857 else
14858 {
41bd81ab
AM
14859 /* This symbol is local, or marked to become local,
14860 or this is an opd section reloc which must point
14861 at a local function. */
65f38f15 14862 outrel.r_addend += relocation;
e86ce104 14863 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14864 {
3fad3c7c 14865 if (is_opd && h != NULL)
afbe61cf
AM
14866 {
14867 /* Lie about opd entries. This case occurs
14868 when building shared libraries and we
14869 reference a function in another shared
3fad3c7c
AM
14870 lib. The same thing happens for a weak
14871 definition in an application that's
14872 overridden by a strong definition in a
14873 shared lib. (I believe this is a generic
14874 bug in binutils handling of weak syms.)
14875 In these cases we won't use the opd
1e2f5b6e 14876 entry in this lib. */
b34976b6 14877 unresolved_reloc = FALSE;
afbe61cf 14878 }
25f23106
AM
14879 if (!is_opd
14880 && r_type == R_PPC64_ADDR64
14881 && (h != NULL
14882 ? h->elf.type == STT_GNU_IFUNC
14883 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14884 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14885 else
14886 {
14887 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14888
25f23106
AM
14889 /* We need to relocate .opd contents for ld.so.
14890 Prelink also wants simple and consistent rules
14891 for relocs. This make all RELATIVE relocs have
14892 *r_offset equal to r_addend. */
14893 relocate = TRUE;
14894 }
65f38f15
AM
14895 }
14896 else
14897 {
25f23106
AM
14898 if (h != NULL
14899 ? h->elf.type == STT_GNU_IFUNC
14900 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14901 {
25f53a85 14902 info->callbacks->einfo
695344c0 14903 /* xgettext:c-format */
174d0a74 14904 (_("%H: %s for indirect "
c1c8c1ef 14905 "function `%pT' unsupported\n"),
25f53a85 14906 input_bfd, input_section, rel->r_offset,
25f23106
AM
14907 ppc64_elf_howto_table[r_type]->name,
14908 sym_name);
14909 ret = FALSE;
14910 }
cf35638d 14911 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14912 ;
14913 else if (sec == NULL || sec->owner == NULL)
14914 {
14915 bfd_set_error (bfd_error_bad_value);
b34976b6 14916 return FALSE;
65f38f15
AM
14917 }
14918 else
14919 {
14920 asection *osec;
14921
14922 osec = sec->output_section;
14923 indx = elf_section_data (osec)->dynindx;
14924
74541ad4
AM
14925 if (indx == 0)
14926 {
14927 if ((osec->flags & SEC_READONLY) == 0
14928 && htab->elf.data_index_section != NULL)
14929 osec = htab->elf.data_index_section;
14930 else
14931 osec = htab->elf.text_index_section;
14932 indx = elf_section_data (osec)->dynindx;
14933 }
14934 BFD_ASSERT (indx != 0);
14935
65f38f15
AM
14936 /* We are turning this relocation into one
14937 against a section symbol, so subtract out
14938 the output section's address but not the
14939 offset of the input section in the output
14940 section. */
14941 outrel.r_addend -= osec->vma;
14942 }
14943
14944 outrel.r_info = ELF64_R_INFO (indx, r_type);
14945 }
14946 }
14947
14948 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14949 if (h != NULL
14950 ? h->elf.type == STT_GNU_IFUNC
14951 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
14952 {
14953 sreloc = htab->elf.irelplt;
14954 if (indx == 0)
14955 htab->local_ifunc_resolver = 1;
14956 else if (is_static_defined (&h->elf))
14957 htab->maybe_local_ifunc_resolver = 1;
14958 }
65f38f15
AM
14959 if (sreloc == NULL)
14960 abort ();
14961
dfbb6ac9
AM
14962 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14963 >= sreloc->size)
14964 abort ();
947216bf
AM
14965 loc = sreloc->contents;
14966 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14967 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14968
14969 /* If this reloc is against an external symbol, it will
14970 be computed at runtime, so there's no need to do
81407a69
AM
14971 anything now. However, for the sake of prelink ensure
14972 that the section contents are a known value. */
65f38f15 14973 if (! relocate)
81407a69
AM
14974 {
14975 unresolved_reloc = FALSE;
14976 /* The value chosen here is quite arbitrary as ld.so
14977 ignores section contents except for the special
14978 case of .opd where the contents might be accessed
14979 before relocation. Choose zero, as that won't
14980 cause reloc overflow. */
14981 relocation = 0;
14982 addend = 0;
14983 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14984 to improve backward compatibility with older
14985 versions of ld. */
14986 if (r_type == R_PPC64_ADDR64)
14987 addend = outrel.r_addend;
14988 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14989 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 14990 addend = outrel.r_offset;
81407a69 14991 }
65f38f15 14992 }
5bd4f169
AM
14993 break;
14994
65f38f15
AM
14995 case R_PPC64_COPY:
14996 case R_PPC64_GLOB_DAT:
14997 case R_PPC64_JMP_SLOT:
25f23106 14998 case R_PPC64_JMP_IREL:
65f38f15
AM
14999 case R_PPC64_RELATIVE:
15000 /* We shouldn't ever see these dynamic relocs in relocatable
15001 files. */
ae9a127f 15002 /* Fall through. */
65f38f15
AM
15003
15004 case R_PPC64_PLTGOT16:
15005 case R_PPC64_PLTGOT16_DS:
15006 case R_PPC64_PLTGOT16_HA:
15007 case R_PPC64_PLTGOT16_HI:
15008 case R_PPC64_PLTGOT16_LO:
15009 case R_PPC64_PLTGOT16_LO_DS:
15010 case R_PPC64_PLTREL32:
15011 case R_PPC64_PLTREL64:
15012 /* These ones haven't been implemented yet. */
15013
25f53a85 15014 info->callbacks->einfo
695344c0 15015 /* xgettext:c-format */
c1c8c1ef 15016 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15017 input_bfd,
4ce794b7 15018 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15019
15020 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15021 ret = FALSE;
c316a17c 15022 goto copy_reloc;
65f38f15 15023 }
5bd4f169 15024
67f0cbdb
AM
15025 /* Multi-instruction sequences that access the TOC can be
15026 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15027 to nop; addi rb,r2,x; */
67f0cbdb
AM
15028 switch (r_type)
15029 {
15030 default:
15031 break;
15032
15033 case R_PPC64_GOT_TLSLD16_HI:
15034 case R_PPC64_GOT_TLSGD16_HI:
15035 case R_PPC64_GOT_TPREL16_HI:
15036 case R_PPC64_GOT_DTPREL16_HI:
15037 case R_PPC64_GOT16_HI:
15038 case R_PPC64_TOC16_HI:
15039 /* These relocs would only be useful if building up an
15040 offset to later add to r2, perhaps in an indexed
15041 addressing mode instruction. Don't try to optimize.
15042 Unfortunately, the possibility of someone building up an
15043 offset like this or even with the HA relocs, means that
15044 we need to check the high insn when optimizing the low
15045 insn. */
15046 break;
15047
15048 case R_PPC64_GOT_TLSLD16_HA:
15049 case R_PPC64_GOT_TLSGD16_HA:
15050 case R_PPC64_GOT_TPREL16_HA:
15051 case R_PPC64_GOT_DTPREL16_HA:
15052 case R_PPC64_GOT16_HA:
15053 case R_PPC64_TOC16_HA:
98528052 15054 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15055 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15056 {
15057 bfd_byte *p = contents + (rel->r_offset & ~3);
15058 bfd_put_32 (input_bfd, NOP, p);
d830549d 15059 goto copy_reloc;
98528052 15060 }
67f0cbdb
AM
15061 break;
15062
15063 case R_PPC64_GOT_TLSLD16_LO:
15064 case R_PPC64_GOT_TLSGD16_LO:
15065 case R_PPC64_GOT_TPREL16_LO_DS:
15066 case R_PPC64_GOT_DTPREL16_LO_DS:
15067 case R_PPC64_GOT16_LO:
15068 case R_PPC64_GOT16_LO_DS:
15069 case R_PPC64_TOC16_LO:
15070 case R_PPC64_TOC16_LO_DS:
98528052 15071 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15072 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15073 {
15074 bfd_byte *p = contents + (rel->r_offset & ~3);
15075 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15076 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15077 {
15078 /* Transform addic to addi when we change reg. */
15079 insn &= ~((0x3f << 26) | (0x1f << 16));
15080 insn |= (14u << 26) | (2 << 16);
15081 }
15082 else
67f0cbdb 15083 {
98528052
AM
15084 insn &= ~(0x1f << 16);
15085 insn |= 2 << 16;
67f0cbdb 15086 }
560c8763 15087 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15088 }
15089 break;
9a23f96e
AM
15090
15091 case R_PPC64_TPREL16_HA:
15092 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15093 {
15094 bfd_byte *p = contents + (rel->r_offset & ~3);
15095 insn = bfd_get_32 (input_bfd, p);
15096 if ((insn & ((0x3f << 26) | 0x1f << 16))
15097 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15098 /* xgettext:c-format */
15099 info->callbacks->minfo
15100 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15101 input_bfd, input_section, rel->r_offset,
15102 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15103 else
d830549d
AM
15104 {
15105 bfd_put_32 (input_bfd, NOP, p);
15106 goto copy_reloc;
15107 }
9a23f96e
AM
15108 }
15109 break;
15110
15111 case R_PPC64_TPREL16_LO:
15112 case R_PPC64_TPREL16_LO_DS:
15113 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15114 {
15115 bfd_byte *p = contents + (rel->r_offset & ~3);
15116 insn = bfd_get_32 (input_bfd, p);
15117 insn &= ~(0x1f << 16);
15118 insn |= 13 << 16;
15119 bfd_put_32 (input_bfd, insn, p);
15120 }
15121 break;
67f0cbdb
AM
15122 }
15123
65f38f15
AM
15124 /* Do any further special processing. */
15125 switch (r_type)
15126 {
15127 default:
15128 break;
15129
25f23106 15130 case R_PPC64_REL16_HA:
a680de9a 15131 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15132 case R_PPC64_ADDR16_HA:
15133 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15134 case R_PPC64_ADDR16_HIGHERA:
15135 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15136 case R_PPC64_TOC16_HA:
15137 case R_PPC64_SECTOFF_HA:
411e1bfb 15138 case R_PPC64_TPREL16_HA:
f9c6b907 15139 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15140 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15141 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15142 case R_PPC64_DTPREL16_HA:
15143 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15144 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15145 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15146 /* It's just possible that this symbol is a weak symbol
15147 that's not actually defined anywhere. In that case,
15148 'sec' would be NULL, and we should leave the symbol
15149 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15150 if (sec == NULL)
15151 break;
1a0670f3 15152 /* Fall through. */
5c5f6e17
AM
15153
15154 case R_PPC64_GOT16_HA:
15155 case R_PPC64_PLTGOT16_HA:
15156 case R_PPC64_PLT16_HA:
15157 case R_PPC64_GOT_TLSGD16_HA:
15158 case R_PPC64_GOT_TLSLD16_HA:
15159 case R_PPC64_GOT_TPREL16_HA:
15160 case R_PPC64_GOT_DTPREL16_HA:
15161 /* Add 0x10000 if sign bit in 0:15 is set.
15162 Bits 0:15 are not used. */
15163 addend += 0x8000;
65f38f15
AM
15164 break;
15165
15166 case R_PPC64_ADDR16_DS:
15167 case R_PPC64_ADDR16_LO_DS:
15168 case R_PPC64_GOT16_DS:
15169 case R_PPC64_GOT16_LO_DS:
15170 case R_PPC64_PLT16_LO_DS:
15171 case R_PPC64_SECTOFF_DS:
15172 case R_PPC64_SECTOFF_LO_DS:
15173 case R_PPC64_TOC16_DS:
15174 case R_PPC64_TOC16_LO_DS:
15175 case R_PPC64_PLTGOT16_DS:
15176 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15177 case R_PPC64_GOT_TPREL16_DS:
15178 case R_PPC64_GOT_TPREL16_LO_DS:
15179 case R_PPC64_GOT_DTPREL16_DS:
15180 case R_PPC64_GOT_DTPREL16_LO_DS:
15181 case R_PPC64_TPREL16_DS:
15182 case R_PPC64_TPREL16_LO_DS:
15183 case R_PPC64_DTPREL16_DS:
15184 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15185 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15186 mask = 3;
a680de9a
PB
15187 /* If this reloc is against an lq, lxv, or stxv insn, then
15188 the value must be a multiple of 16. This is somewhat of
15189 a hack, but the "correct" way to do this by defining _DQ
15190 forms of all the _DS relocs bloats all reloc switches in
15191 this file. It doesn't make much sense to use these
15192 relocs in data, so testing the insn should be safe. */
15193 if ((insn & (0x3f << 26)) == (56u << 26)
15194 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15195 mask = 15;
a680de9a
PB
15196 relocation += addend;
15197 addend = insn & (mask ^ 3);
15198 if ((relocation & mask) != 0)
65f38f15 15199 {
a680de9a 15200 relocation ^= relocation & mask;
25f53a85 15201 info->callbacks->einfo
695344c0 15202 /* xgettext:c-format */
174d0a74 15203 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15204 input_bfd, input_section, rel->r_offset,
d830549d 15205 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15206 mask + 1);
65f38f15 15207 bfd_set_error (bfd_error_bad_value);
b34976b6 15208 ret = FALSE;
c316a17c 15209 goto copy_reloc;
65f38f15
AM
15210 }
15211 break;
5bd4f169
AM
15212 }
15213
239e1f3a
AM
15214 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15215 because such sections are not SEC_ALLOC and thus ld.so will
15216 not process them. */
d830549d 15217 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15218 if (unresolved_reloc
239e1f3a 15219 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15220 && h->elf.def_dynamic)
15221 && _bfd_elf_section_offset (output_bfd, info, input_section,
15222 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15223 {
25f53a85 15224 info->callbacks->einfo
695344c0 15225 /* xgettext:c-format */
c1c8c1ef 15226 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15227 input_bfd, input_section, rel->r_offset,
b80eed39 15228 howto->name,
039b3fef 15229 h->elf.root.root.string);
b34976b6 15230 ret = FALSE;
9c07fe7c 15231 }
5bd4f169 15232
b80eed39
AM
15233 /* 16-bit fields in insns mostly have signed values, but a
15234 few insns have 16-bit unsigned values. Really, we should
15235 have different reloc types. */
15236 if (howto->complain_on_overflow != complain_overflow_dont
15237 && howto->dst_mask == 0xffff
15238 && (input_section->flags & SEC_CODE) != 0)
15239 {
15240 enum complain_overflow complain = complain_overflow_signed;
15241
15242 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15243 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15244 complain = complain_overflow_bitfield;
15245 else if (howto->rightshift == 0
15246 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15247 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15248 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15249 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15250 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15251 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15252 complain = complain_overflow_unsigned;
15253 if (howto->complain_on_overflow != complain)
15254 {
15255 alt_howto = *howto;
15256 alt_howto.complain_on_overflow = complain;
15257 howto = &alt_howto;
15258 }
15259 }
15260
a680de9a
PB
15261 if (r_type == R_PPC64_REL16DX_HA)
15262 {
15263 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15264 if (rel->r_offset + 4 > input_section->size)
15265 r = bfd_reloc_outofrange;
15266 else
15267 {
15268 relocation += addend;
15269 relocation -= (rel->r_offset
15270 + input_section->output_offset
15271 + input_section->output_section->vma);
3de43e7b 15272 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15273 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15274 insn &= ~0x1fffc1;
3de43e7b 15275 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15276 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15277 r = bfd_reloc_ok;
3de43e7b 15278 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15279 r = bfd_reloc_overflow;
15280 }
15281 }
15282 else
15283 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15284 rel->r_offset, relocation, addend);
5bd4f169 15285
ef60b7ff 15286 if (r != bfd_reloc_ok)
5bd4f169 15287 {
bc30df16 15288 char *more_info = NULL;
b80eed39 15289 const char *reloc_name = howto->name;
bc30df16
AM
15290
15291 if (reloc_dest != DEST_NORMAL)
15292 {
15293 more_info = bfd_malloc (strlen (reloc_name) + 8);
15294 if (more_info != NULL)
15295 {
15296 strcpy (more_info, reloc_name);
15297 strcat (more_info, (reloc_dest == DEST_OPD
15298 ? " (OPD)" : " (stub)"));
15299 reloc_name = more_info;
15300 }
15301 }
15302
cd27b276 15303 if (r == bfd_reloc_overflow)
5bd4f169 15304 {
8131c122
AM
15305 /* On code like "if (foo) foo();" don't report overflow
15306 on a branch to zero when foo is undefined. */
15307 if (!warned
15308 && (reloc_dest == DEST_STUB
15309 || !(h != NULL
15310 && (h->elf.root.type == bfd_link_hash_undefweak
15311 || h->elf.root.type == bfd_link_hash_undefined)
15312 && is_branch_reloc (r_type))))
1a72702b
AM
15313 info->callbacks->reloc_overflow (info, &h->elf.root,
15314 sym_name, reloc_name,
15315 orig_rel.r_addend,
15316 input_bfd, input_section,
15317 rel->r_offset);
ef60b7ff
AM
15318 }
15319 else
15320 {
25f53a85 15321 info->callbacks->einfo
695344c0 15322 /* xgettext:c-format */
c1c8c1ef 15323 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15324 input_bfd, input_section, rel->r_offset,
bc30df16 15325 reloc_name, sym_name, (int) r);
b34976b6 15326 ret = FALSE;
ef60b7ff 15327 }
bc30df16
AM
15328 if (more_info != NULL)
15329 free (more_info);
5bd4f169 15330 }
c316a17c
AM
15331 copy_reloc:
15332 if (wrel != rel)
15333 *wrel = *rel;
15334 }
15335
15336 if (wrel != rel)
15337 {
15338 Elf_Internal_Shdr *rel_hdr;
15339 size_t deleted = rel - wrel;
15340
15341 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15342 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15343 if (rel_hdr->sh_size == 0)
15344 {
15345 /* It is too late to remove an empty reloc section. Leave
15346 one NONE reloc.
15347 ??? What is wrong with an empty section??? */
15348 rel_hdr->sh_size = rel_hdr->sh_entsize;
15349 deleted -= 1;
15350 }
15351 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15352 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15353 input_section->reloc_count -= deleted;
5bd4f169
AM
15354 }
15355
645ea6a9
AM
15356 /* If we're emitting relocations, then shortly after this function
15357 returns, reloc offsets and addends for this section will be
15358 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15359 file rather than the input. Save a copy of the relocs for
15360 opd_entry_value. */
0e1862bb 15361 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15362 {
15363 bfd_size_type amt;
15364 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15365 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15366 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15367 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15368 if (rel == NULL)
15369 return FALSE;
15370 memcpy (rel, relocs, amt);
15371 }
5bd4f169
AM
15372 return ret;
15373}
15374
754021d0
AM
15375/* Adjust the value of any local symbols in opd sections. */
15376
6e0b88f1 15377static int
754021d0
AM
15378ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15379 const char *name ATTRIBUTE_UNUSED,
15380 Elf_Internal_Sym *elfsym,
15381 asection *input_sec,
15382 struct elf_link_hash_entry *h)
15383{
74f0fb50
AM
15384 struct _opd_sec_data *opd;
15385 long adjust;
754021d0
AM
15386 bfd_vma value;
15387
4025353c 15388 if (h != NULL)
6e0b88f1 15389 return 1;
4025353c 15390
74f0fb50
AM
15391 opd = get_opd_info (input_sec);
15392 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15393 return 1;
754021d0
AM
15394
15395 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15396 if (!bfd_link_relocatable (info))
754021d0
AM
15397 value -= input_sec->output_section->vma;
15398
51aecdc5 15399 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15400 if (adjust == -1)
6e0b88f1
AM
15401 return 2;
15402
15403 elfsym->st_value += adjust;
15404 return 1;
754021d0
AM
15405}
15406
5bd4f169
AM
15407/* Finish up dynamic symbol handling. We set the contents of various
15408 dynamic sections here. */
15409
b34976b6 15410static bfd_boolean
4ce794b7
AM
15411ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15412 struct bfd_link_info *info,
15413 struct elf_link_hash_entry *h,
4aef7643 15414 Elf_Internal_Sym *sym)
5bd4f169 15415{
65f38f15 15416 struct ppc_link_hash_table *htab;
8387904d
AM
15417 struct plt_entry *ent;
15418 Elf_Internal_Rela rela;
15419 bfd_byte *loc;
5bd4f169 15420
65f38f15 15421 htab = ppc_hash_table (info);
4dfe6ac6
NC
15422 if (htab == NULL)
15423 return FALSE;
5bd4f169 15424
8387904d
AM
15425 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15426 if (ent->plt.offset != (bfd_vma) -1)
15427 {
15428 /* This symbol has an entry in the procedure linkage
15429 table. Set it up. */
e054468f
AM
15430 if (!htab->elf.dynamic_sections_created
15431 || h->dynindx == -1)
15432 {
15433 BFD_ASSERT (h->type == STT_GNU_IFUNC
15434 && h->def_regular
15435 && (h->root.type == bfd_link_hash_defined
15436 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15437 rela.r_offset = (htab->elf.iplt->output_section->vma
15438 + htab->elf.iplt->output_offset
25f23106 15439 + ent->plt.offset);
ee67d69a
AM
15440 if (htab->opd_abi)
15441 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15442 else
15443 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15444 rela.r_addend = (h->root.u.def.value
15445 + h->root.u.def.section->output_offset
15446 + h->root.u.def.section->output_section->vma
15447 + ent->addend);
33e44f2e
AM
15448 loc = (htab->elf.irelplt->contents
15449 + (htab->elf.irelplt->reloc_count++
25f23106 15450 * sizeof (Elf64_External_Rela)));
82e66161 15451 htab->local_ifunc_resolver = 1;
e054468f
AM
15452 }
15453 else
15454 {
33e44f2e
AM
15455 rela.r_offset = (htab->elf.splt->output_section->vma
15456 + htab->elf.splt->output_offset
25f23106 15457 + ent->plt.offset);
e054468f
AM
15458 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15459 rela.r_addend = ent->addend;
33e44f2e 15460 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15461 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15462 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
82e66161
AM
15463 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15464 htab->maybe_local_ifunc_resolver = 1;
e054468f 15465 }
8387904d 15466 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15467
15468 if (!htab->opd_abi)
15469 {
15470 if (!h->def_regular)
15471 {
15472 /* Mark the symbol as undefined, rather than as
15473 defined in glink. Leave the value if there were
15474 any relocations where pointer equality matters
15475 (this is a clue for the dynamic linker, to make
15476 function pointer comparisons work between an
15477 application and shared library), otherwise set it
15478 to zero. */
15479 sym->st_shndx = SHN_UNDEF;
15480 if (!h->pointer_equality_needed)
15481 sym->st_value = 0;
15482 else if (!h->ref_regular_nonweak)
15483 {
15484 /* This breaks function pointer comparisons, but
15485 that is better than breaking tests for a NULL
15486 function pointer. */
15487 sym->st_value = 0;
15488 }
15489 }
15490 }
8387904d 15491 }
5bd4f169 15492
f5385ebf 15493 if (h->needs_copy)
5bd4f169 15494 {
65f38f15 15495 /* This symbol needs a copy reloc. Set it up. */
5474d94f 15496 asection *srel;
5bd4f169 15497
65f38f15
AM
15498 if (h->dynindx == -1
15499 || (h->root.type != bfd_link_hash_defined
15500 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15501 || htab->elf.srelbss == NULL
15502 || htab->elf.sreldynrelro == NULL)
65f38f15 15503 abort ();
5bd4f169
AM
15504
15505 rela.r_offset = (h->root.u.def.value
15506 + h->root.u.def.section->output_section->vma
15507 + h->root.u.def.section->output_offset);
15508 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15509 rela.r_addend = 0;
afbf7e8e 15510 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15511 srel = htab->elf.sreldynrelro;
15512 else
15513 srel = htab->elf.srelbss;
15514 loc = srel->contents;
15515 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15516 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15517 }
15518
b34976b6 15519 return TRUE;
5bd4f169
AM
15520}
15521
65f38f15
AM
15522/* Used to decide how to sort relocs in an optimal manner for the
15523 dynamic linker, before writing them out. */
15524
15525static enum elf_reloc_type_class
7e612e98
AM
15526ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15527 const asection *rel_sec,
15528 const Elf_Internal_Rela *rela)
65f38f15 15529{
04c9666a 15530 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15531 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15532
33e44f2e 15533 if (rel_sec == htab->elf.irelplt)
7e612e98 15534 return reloc_class_ifunc;
a33d1f77 15535
4ce794b7 15536 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15537 switch (r_type)
65f38f15
AM
15538 {
15539 case R_PPC64_RELATIVE:
15540 return reloc_class_relative;
15541 case R_PPC64_JMP_SLOT:
15542 return reloc_class_plt;
15543 case R_PPC64_COPY:
15544 return reloc_class_copy;
15545 default:
15546 return reloc_class_normal;
15547 }
15548}
15549
5bd4f169
AM
15550/* Finish up the dynamic sections. */
15551
b34976b6 15552static bfd_boolean
4ce794b7
AM
15553ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15554 struct bfd_link_info *info)
5bd4f169 15555{
65f38f15
AM
15556 struct ppc_link_hash_table *htab;
15557 bfd *dynobj;
5bd4f169 15558 asection *sdyn;
5bd4f169 15559
65f38f15 15560 htab = ppc_hash_table (info);
4dfe6ac6
NC
15561 if (htab == NULL)
15562 return FALSE;
15563
65f38f15 15564 dynobj = htab->elf.dynobj;
3d4d4302 15565 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15566
65f38f15 15567 if (htab->elf.dynamic_sections_created)
5bd4f169 15568 {
5bd4f169
AM
15569 Elf64_External_Dyn *dyncon, *dynconend;
15570
33e44f2e 15571 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15572 abort ();
5bd4f169
AM
15573
15574 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15575 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15576 for (; dyncon < dynconend; dyncon++)
15577 {
15578 Elf_Internal_Dyn dyn;
19397422 15579 asection *s;
5bd4f169
AM
15580
15581 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15582
15583 switch (dyn.d_tag)
15584 {
65f38f15
AM
15585 default:
15586 continue;
5bd4f169 15587
5d1634d7 15588 case DT_PPC64_GLINK:
4ce794b7 15589 s = htab->glink;
6348e046 15590 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15591 /* We stupidly defined DT_PPC64_GLINK to be the start
15592 of glink rather than the first entry point, which is
15593 what ld.so needs, and now have a bigger stub to
15594 support automatic multiple TOCs. */
9e390558 15595 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
15596 break;
15597
19397422
AM
15598 case DT_PPC64_OPD:
15599 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15600 if (s == NULL)
15601 continue;
15602 dyn.d_un.d_ptr = s->vma;
19397422
AM
15603 break;
15604
e8910a83
AM
15605 case DT_PPC64_OPT:
15606 if (htab->do_multi_toc && htab->multi_toc_needed)
15607 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
15608 if (htab->has_plt_localentry0)
15609 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
15610 break;
15611
19397422
AM
15612 case DT_PPC64_OPDSZ:
15613 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15614 if (s == NULL)
15615 continue;
eea6121a 15616 dyn.d_un.d_val = s->size;
19397422
AM
15617 break;
15618
65f38f15 15619 case DT_PLTGOT:
33e44f2e 15620 s = htab->elf.splt;
6348e046 15621 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15622 break;
15623
15624 case DT_JMPREL:
33e44f2e 15625 s = htab->elf.srelplt;
6348e046 15626 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15627 break;
5bd4f169 15628
65f38f15 15629 case DT_PLTRELSZ:
33e44f2e 15630 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15631 break;
82e66161
AM
15632
15633 case DT_TEXTREL:
15634 if (htab->local_ifunc_resolver)
15635 info->callbacks->einfo
15636 (_("%X%P: text relocations and GNU indirect "
15637 "functions will result in a segfault at runtime\n"));
15638 else if (htab->maybe_local_ifunc_resolver)
15639 info->callbacks->einfo
15640 (_("%P: warning: text relocations and GNU indirect "
15641 "functions may result in a segfault at runtime\n"));
15642 continue;
5bd4f169 15643 }
5bd4f169 15644
65f38f15 15645 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15646 }
5bd4f169
AM
15647 }
15648
6528b6eb
AM
15649 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15650 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15651 {
15652 /* Fill in the first entry in the global offset table.
15653 We use it to hold the link-time TOCbase. */
15654 bfd_put_64 (output_bfd,
60ee0d4a 15655 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15656 htab->elf.sgot->contents);
5d1634d7
AM
15657
15658 /* Set .got entry size. */
33e44f2e 15659 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15660 }
15661
6528b6eb
AM
15662 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15663 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15664 {
15665 /* Set .plt entry size. */
33e44f2e 15666 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15667 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15668 }
15669
84f5d08e
AM
15670 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15671 brlt ourselves if emitrelocations. */
15672 if (htab->brlt != NULL
15673 && htab->brlt->reloc_count != 0
15674 && !_bfd_elf_link_output_relocs (output_bfd,
15675 htab->brlt,
d4730f92 15676 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15677 elf_section_data (htab->brlt)->relocs,
15678 NULL))
15679 return FALSE;
15680
176a0d42
AM
15681 if (htab->glink != NULL
15682 && htab->glink->reloc_count != 0
15683 && !_bfd_elf_link_output_relocs (output_bfd,
15684 htab->glink,
d4730f92 15685 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15686 elf_section_data (htab->glink)->relocs,
15687 NULL))
15688 return FALSE;
15689
58d180e8 15690 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15691 && htab->glink_eh_frame->size != 0)
15692 {
15693 bfd_vma val;
15694 bfd_byte *p;
d4aaa2a0 15695 struct map_stub *group;
2e0ce1c8 15696 size_t align = 4;
da44f4e5 15697
2e0ce1c8
AM
15698 p = htab->glink_eh_frame->contents;
15699 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
d4aaa2a0
AM
15700
15701 for (group = htab->group; group != NULL; group = group->next)
15702 if (group->stub_sec != NULL)
da44f4e5 15703 {
da44f4e5 15704 /* Offset to stub section. */
d4aaa2a0
AM
15705 val = (group->stub_sec->output_section->vma
15706 + group->stub_sec->output_offset);
da44f4e5
AM
15707 val -= (htab->glink_eh_frame->output_section->vma
15708 + htab->glink_eh_frame->output_offset
d4aaa2a0 15709 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15710 if (val + 0x80000000 > 0xffffffff)
15711 {
cf97bcb0
AM
15712 _bfd_error_handler
15713 (_("%s offset too large for .eh_frame sdata4 encoding"),
d4aaa2a0 15714 group->stub_sec->name);
da44f4e5
AM
15715 return FALSE;
15716 }
d4aaa2a0
AM
15717 bfd_put_32 (dynobj, val, p + 8);
15718 p += stub_eh_frame_size (group, align);
da44f4e5
AM
15719 }
15720 if (htab->glink != NULL && htab->glink->size != 0)
15721 {
da44f4e5
AM
15722 /* Offset to .glink. */
15723 val = (htab->glink->output_section->vma
15724 + htab->glink->output_offset
15725 + 8);
15726 val -= (htab->glink_eh_frame->output_section->vma
15727 + htab->glink_eh_frame->output_offset
d4aaa2a0 15728 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15729 if (val + 0x80000000 > 0xffffffff)
15730 {
cf97bcb0
AM
15731 _bfd_error_handler
15732 (_("%s offset too large for .eh_frame sdata4 encoding"),
da44f4e5
AM
15733 htab->glink->name);
15734 return FALSE;
15735 }
d4aaa2a0
AM
15736 bfd_put_32 (dynobj, val, p + 8);
15737 p += (24 + align - 1) & -align;
da44f4e5
AM
15738 }
15739
15740 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15741 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15742 htab->glink_eh_frame,
15743 htab->glink_eh_frame->contents))
15744 return FALSE;
15745 }
58d180e8 15746
e717da7e 15747 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15748 since we didn't add them to DYNOBJ. We know dynobj is the first
15749 bfd. */
c72f2fb2 15750 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15751 {
15752 asection *s;
7b53ace3 15753
0c8d6e5c 15754 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15755 continue;
15756
e717da7e
AM
15757 s = ppc64_elf_tdata (dynobj)->got;
15758 if (s != NULL
eea6121a 15759 && s->size != 0
e717da7e
AM
15760 && s->output_section != bfd_abs_section_ptr
15761 && !bfd_set_section_contents (output_bfd, s->output_section,
15762 s->contents, s->output_offset,
eea6121a 15763 s->size))
e717da7e
AM
15764 return FALSE;
15765 s = ppc64_elf_tdata (dynobj)->relgot;
15766 if (s != NULL
eea6121a 15767 && s->size != 0
e717da7e
AM
15768 && s->output_section != bfd_abs_section_ptr
15769 && !bfd_set_section_contents (output_bfd, s->output_section,
15770 s->contents, s->output_offset,
eea6121a 15771 s->size))
e717da7e
AM
15772 return FALSE;
15773 }
f6c52c13 15774
b34976b6 15775 return TRUE;
5bd4f169
AM
15776}
15777
5bd4f169 15778#include "elf64-target.h"
7b8e7dad
AM
15779
15780/* FreeBSD support */
15781
15782#undef TARGET_LITTLE_SYM
15783#undef TARGET_LITTLE_NAME
15784
15785#undef TARGET_BIG_SYM
6d00b590 15786#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15787#undef TARGET_BIG_NAME
15788#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15789
15790#undef ELF_OSABI
15791#define ELF_OSABI ELFOSABI_FREEBSD
15792
15793#undef elf64_bed
15794#define elf64_bed elf64_powerpc_fbsd_bed
15795
15796#include "elf64-target.h"
This page took 3.950218 seconds and 4 git commands to generate.