]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Fix ldah being disassembled as ldaexh
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
b90efa5b 2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 56static bfd_vma opd_entry_value
aef36ac1 57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 58
6d00b590 59#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 60#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 61#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
04c6a44c 67#define ELF_COMMONPAGESIZE 0x10000
ad8e1ba5
AM
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
ad8e1ba5 74#define elf_backend_got_header_size 8
ad8e1ba5
AM
75#define elf_backend_can_gc_sections 1
76#define elf_backend_can_refcount 1
77#define elf_backend_rela_normal 1
6bfdb61b 78#define elf_backend_default_execstack 0
ad8e1ba5 79
e717da7e 80#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 81#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 82#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
83#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
84#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
85#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
86#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 87#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 88#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
89
90#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
91#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
92#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 93#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
94#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
95#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 96#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 97#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 98#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 99#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 100#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 101#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 102#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
103#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
104#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
105#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
106#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 107#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
108#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
109#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 110#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 111#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 112#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
113#define elf_backend_relocate_section ppc64_elf_relocate_section
114#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
115#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
116#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 117#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 118#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 119#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
ad8e1ba5 120
5bd4f169
AM
121/* The name of the dynamic interpreter. This is put in the .interp
122 section. */
123#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
124
125/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 126#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
127
128/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 129#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 130
a078d95a
AM
131/* Offsets to some stack save slots. */
132#define STK_LR 16
133#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 134/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
135 CR save slot. Used only by optimised __tls_get_addr call stub,
136 relying on __tls_get_addr_opt not saving CR.. */
137#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
138
5bd4f169 139/* TOC base pointers offset from start of TOC. */
411e1bfb 140#define TOC_BASE_OFF 0x8000
a27e685f
AM
141/* TOC base alignment. */
142#define TOC_BASE_ALIGN 256
411e1bfb
AM
143
144/* Offset of tp and dtp pointers from start of TLS block. */
145#define TP_OFFSET 0x7000
146#define DTP_OFFSET 0x8000
5bd4f169 147
ad8e1ba5
AM
148/* .plt call stub instructions. The normal stub is like this, but
149 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 150 insert an addi to adjust r11. */
a078d95a 151#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
152#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
153#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
154#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
155#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
156#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
157#define BCTR 0x4e800420 /* bctr */
158
71a39c98 159#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
ad8e1ba5
AM
160#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
161#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
162
71a39c98
AM
163#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
164#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
165#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
794e51c0
AM
166#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
167#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
168#define BNECTR 0x4ca20420 /* bnectr+ */
169#define BNECTR_P4 0x4ce20420 /* bnectr+ */
170
71a39c98 171#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442
AM
172#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
173#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
174
a078d95a 175#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
ad8e1ba5 176
397998fc 177#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d
AM
178#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
179#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
180
ee4bf8d2 181/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 182#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
183 /* 0: */
184 /* .quad plt0-1f */
185 /* __glink: */
186#define MFLR_R12 0x7d8802a6 /* mflr %12 */
187#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
188 /* 1: */
189#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 190 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 191#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
192#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
193 /* ld %12,0(%11) */
194 /* ld %2,8(%11) */
195 /* mtctr %12 */
196 /* ld %11,16(%11) */
ee4bf8d2 197 /* bctr */
b9e5796b
AM
198#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
199#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
200#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
201#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
202#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
203
204/* Pad with this. */
205#define NOP 0x60000000
206
721956f4
AM
207/* Some other nops. */
208#define CROR_151515 0x4def7b82
209#define CROR_313131 0x4ffffb82
210
cedb70c5 211/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
212#define LI_R0_0 0x38000000 /* li %r0,0 */
213#define B_DOT 0x48000000 /* b . */
214
215/* After that, we need two instructions to load the index, followed by
216 a branch. */
217#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 218#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 219
deb0e272
AM
220/* Instructions used by the save and restore reg functions. */
221#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
222#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
223#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
224#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
225#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
226#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
227#define LI_R12_0 0x39800000 /* li %r12,0 */
228#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
229#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
230#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
231#define BLR 0x4e800020 /* blr */
232
41bd81ab
AM
233/* Since .opd is an array of descriptors and each entry will end up
234 with identical R_PPC64_RELATIVE relocs, there is really no need to
235 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 236 relocate .opd without reloc entries. */
41bd81ab
AM
237#ifndef NO_OPD_RELOCS
238#define NO_OPD_RELOCS 0
239#endif
810d4e75 240
a4b6fadd
AM
241#ifndef ARRAY_SIZE
242#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
243#endif
244
810d4e75
AM
245static inline int
246abiversion (bfd *abfd)
247{
248 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
249}
250
251static inline void
252set_abiversion (bfd *abfd, int ver)
253{
254 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
255 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
256}
5bd4f169 257\f
f5e87a1d 258#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 259
5bd4f169 260/* Relocation HOWTO's. */
04c9666a 261static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
262
263static reloc_howto_type ppc64_elf_howto_raw[] = {
264 /* This reloc does nothing. */
265 HOWTO (R_PPC64_NONE, /* type */
266 0, /* rightshift */
6346d5ca
AM
267 3, /* size (0 = byte, 1 = short, 2 = long) */
268 0, /* bitsize */
b34976b6 269 FALSE, /* pc_relative */
5bd4f169 270 0, /* bitpos */
f5e87a1d 271 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
272 bfd_elf_generic_reloc, /* special_function */
273 "R_PPC64_NONE", /* name */
b34976b6 274 FALSE, /* partial_inplace */
d006db6c 275 0, /* src_mask */
5bd4f169 276 0, /* dst_mask */
b34976b6 277 FALSE), /* pcrel_offset */
5bd4f169
AM
278
279 /* A standard 32 bit relocation. */
280 HOWTO (R_PPC64_ADDR32, /* type */
281 0, /* rightshift */
282 2, /* size (0 = byte, 1 = short, 2 = long) */
283 32, /* bitsize */
b34976b6 284 FALSE, /* pc_relative */
5bd4f169
AM
285 0, /* bitpos */
286 complain_overflow_bitfield, /* complain_on_overflow */
287 bfd_elf_generic_reloc, /* special_function */
288 "R_PPC64_ADDR32", /* name */
b34976b6 289 FALSE, /* partial_inplace */
5bd4f169
AM
290 0, /* src_mask */
291 0xffffffff, /* dst_mask */
b34976b6 292 FALSE), /* pcrel_offset */
5bd4f169
AM
293
294 /* An absolute 26 bit branch; the lower two bits must be zero.
295 FIXME: we don't check that, we just clear them. */
296 HOWTO (R_PPC64_ADDR24, /* type */
297 0, /* rightshift */
298 2, /* size (0 = byte, 1 = short, 2 = long) */
299 26, /* bitsize */
b34976b6 300 FALSE, /* pc_relative */
5bd4f169
AM
301 0, /* bitpos */
302 complain_overflow_bitfield, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* special_function */
304 "R_PPC64_ADDR24", /* name */
b34976b6 305 FALSE, /* partial_inplace */
d006db6c 306 0, /* src_mask */
f5e87a1d 307 0x03fffffc, /* dst_mask */
b34976b6 308 FALSE), /* pcrel_offset */
5bd4f169
AM
309
310 /* A standard 16 bit relocation. */
311 HOWTO (R_PPC64_ADDR16, /* type */
312 0, /* rightshift */
313 1, /* size (0 = byte, 1 = short, 2 = long) */
314 16, /* bitsize */
b34976b6 315 FALSE, /* pc_relative */
5bd4f169
AM
316 0, /* bitpos */
317 complain_overflow_bitfield, /* complain_on_overflow */
318 bfd_elf_generic_reloc, /* special_function */
319 "R_PPC64_ADDR16", /* name */
b34976b6 320 FALSE, /* partial_inplace */
5bd4f169
AM
321 0, /* src_mask */
322 0xffff, /* dst_mask */
b34976b6 323 FALSE), /* pcrel_offset */
5bd4f169
AM
324
325 /* A 16 bit relocation without overflow. */
326 HOWTO (R_PPC64_ADDR16_LO, /* type */
327 0, /* rightshift */
328 1, /* size (0 = byte, 1 = short, 2 = long) */
329 16, /* bitsize */
b34976b6 330 FALSE, /* pc_relative */
5bd4f169
AM
331 0, /* bitpos */
332 complain_overflow_dont,/* complain_on_overflow */
333 bfd_elf_generic_reloc, /* special_function */
334 "R_PPC64_ADDR16_LO", /* name */
b34976b6 335 FALSE, /* partial_inplace */
5bd4f169
AM
336 0, /* src_mask */
337 0xffff, /* dst_mask */
b34976b6 338 FALSE), /* pcrel_offset */
5bd4f169
AM
339
340 /* Bits 16-31 of an address. */
341 HOWTO (R_PPC64_ADDR16_HI, /* type */
342 16, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 16, /* bitsize */
b34976b6 345 FALSE, /* pc_relative */
5bd4f169 346 0, /* bitpos */
f9c6b907 347 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
348 bfd_elf_generic_reloc, /* special_function */
349 "R_PPC64_ADDR16_HI", /* name */
b34976b6 350 FALSE, /* partial_inplace */
5bd4f169
AM
351 0, /* src_mask */
352 0xffff, /* dst_mask */
b34976b6 353 FALSE), /* pcrel_offset */
5bd4f169
AM
354
355 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
356 bits, treated as a signed number, is negative. */
357 HOWTO (R_PPC64_ADDR16_HA, /* type */
358 16, /* rightshift */
359 1, /* size (0 = byte, 1 = short, 2 = long) */
360 16, /* bitsize */
b34976b6 361 FALSE, /* pc_relative */
5bd4f169 362 0, /* bitpos */
f9c6b907 363 complain_overflow_signed, /* complain_on_overflow */
805fc799 364 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 365 "R_PPC64_ADDR16_HA", /* name */
b34976b6 366 FALSE, /* partial_inplace */
5bd4f169
AM
367 0, /* src_mask */
368 0xffff, /* dst_mask */
b34976b6 369 FALSE), /* pcrel_offset */
5bd4f169
AM
370
371 /* An absolute 16 bit branch; the lower two bits must be zero.
372 FIXME: we don't check that, we just clear them. */
373 HOWTO (R_PPC64_ADDR14, /* type */
374 0, /* rightshift */
375 2, /* size (0 = byte, 1 = short, 2 = long) */
376 16, /* bitsize */
b34976b6 377 FALSE, /* pc_relative */
5bd4f169 378 0, /* bitpos */
b80eed39 379 complain_overflow_signed, /* complain_on_overflow */
2441e016 380 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 381 "R_PPC64_ADDR14", /* name */
b34976b6 382 FALSE, /* partial_inplace */
d006db6c 383 0, /* src_mask */
f5e87a1d 384 0x0000fffc, /* dst_mask */
b34976b6 385 FALSE), /* pcrel_offset */
5bd4f169
AM
386
387 /* An absolute 16 bit branch, for which bit 10 should be set to
388 indicate that the branch is expected to be taken. The lower two
389 bits must be zero. */
390 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
391 0, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 16, /* bitsize */
b34976b6 394 FALSE, /* pc_relative */
5bd4f169 395 0, /* bitpos */
b80eed39 396 complain_overflow_signed, /* complain_on_overflow */
805fc799 397 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 398 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 399 FALSE, /* partial_inplace */
d006db6c 400 0, /* src_mask */
f5e87a1d 401 0x0000fffc, /* dst_mask */
b34976b6 402 FALSE), /* pcrel_offset */
5bd4f169
AM
403
404 /* An absolute 16 bit branch, for which bit 10 should be set to
405 indicate that the branch is not expected to be taken. The lower
406 two bits must be zero. */
407 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
408 0, /* rightshift */
409 2, /* size (0 = byte, 1 = short, 2 = long) */
410 16, /* bitsize */
b34976b6 411 FALSE, /* pc_relative */
5bd4f169 412 0, /* bitpos */
b80eed39 413 complain_overflow_signed, /* complain_on_overflow */
805fc799 414 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 415 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 416 FALSE, /* partial_inplace */
d006db6c 417 0, /* src_mask */
f5e87a1d 418 0x0000fffc, /* dst_mask */
b34976b6 419 FALSE), /* pcrel_offset */
5bd4f169
AM
420
421 /* A relative 26 bit branch; the lower two bits must be zero. */
422 HOWTO (R_PPC64_REL24, /* type */
423 0, /* rightshift */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
425 26, /* bitsize */
b34976b6 426 TRUE, /* pc_relative */
5bd4f169
AM
427 0, /* bitpos */
428 complain_overflow_signed, /* complain_on_overflow */
2441e016 429 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 430 "R_PPC64_REL24", /* name */
b34976b6 431 FALSE, /* partial_inplace */
d006db6c 432 0, /* src_mask */
f5e87a1d 433 0x03fffffc, /* dst_mask */
b34976b6 434 TRUE), /* pcrel_offset */
5bd4f169
AM
435
436 /* A relative 16 bit branch; the lower two bits must be zero. */
437 HOWTO (R_PPC64_REL14, /* type */
438 0, /* rightshift */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
440 16, /* bitsize */
b34976b6 441 TRUE, /* pc_relative */
5bd4f169
AM
442 0, /* bitpos */
443 complain_overflow_signed, /* complain_on_overflow */
2441e016 444 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 445 "R_PPC64_REL14", /* name */
b34976b6 446 FALSE, /* partial_inplace */
d006db6c 447 0, /* src_mask */
f5e87a1d 448 0x0000fffc, /* dst_mask */
b34976b6 449 TRUE), /* pcrel_offset */
5bd4f169
AM
450
451 /* A relative 16 bit branch. Bit 10 should be set to indicate that
452 the branch is expected to be taken. The lower two bits must be
453 zero. */
454 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
455 0, /* rightshift */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
457 16, /* bitsize */
b34976b6 458 TRUE, /* pc_relative */
5bd4f169
AM
459 0, /* bitpos */
460 complain_overflow_signed, /* complain_on_overflow */
805fc799 461 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 462 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 463 FALSE, /* partial_inplace */
d006db6c 464 0, /* src_mask */
f5e87a1d 465 0x0000fffc, /* dst_mask */
b34976b6 466 TRUE), /* pcrel_offset */
5bd4f169
AM
467
468 /* A relative 16 bit branch. Bit 10 should be set to indicate that
469 the branch is not expected to be taken. The lower two bits must
470 be zero. */
471 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
472 0, /* rightshift */
473 2, /* size (0 = byte, 1 = short, 2 = long) */
474 16, /* bitsize */
b34976b6 475 TRUE, /* pc_relative */
5bd4f169
AM
476 0, /* bitpos */
477 complain_overflow_signed, /* complain_on_overflow */
805fc799 478 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 479 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 480 FALSE, /* partial_inplace */
d006db6c 481 0, /* src_mask */
f5e87a1d 482 0x0000fffc, /* dst_mask */
b34976b6 483 TRUE), /* pcrel_offset */
5bd4f169
AM
484
485 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
486 symbol. */
487 HOWTO (R_PPC64_GOT16, /* type */
488 0, /* rightshift */
489 1, /* size (0 = byte, 1 = short, 2 = long) */
490 16, /* bitsize */
b34976b6 491 FALSE, /* pc_relative */
5bd4f169
AM
492 0, /* bitpos */
493 complain_overflow_signed, /* complain_on_overflow */
805fc799 494 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 495 "R_PPC64_GOT16", /* name */
b34976b6 496 FALSE, /* partial_inplace */
5bd4f169
AM
497 0, /* src_mask */
498 0xffff, /* dst_mask */
b34976b6 499 FALSE), /* pcrel_offset */
5bd4f169
AM
500
501 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
502 the symbol. */
503 HOWTO (R_PPC64_GOT16_LO, /* type */
504 0, /* rightshift */
505 1, /* size (0 = byte, 1 = short, 2 = long) */
506 16, /* bitsize */
b34976b6 507 FALSE, /* pc_relative */
5bd4f169
AM
508 0, /* bitpos */
509 complain_overflow_dont, /* complain_on_overflow */
805fc799 510 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 511 "R_PPC64_GOT16_LO", /* name */
b34976b6 512 FALSE, /* partial_inplace */
5bd4f169
AM
513 0, /* src_mask */
514 0xffff, /* dst_mask */
b34976b6 515 FALSE), /* pcrel_offset */
5bd4f169
AM
516
517 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
518 the symbol. */
519 HOWTO (R_PPC64_GOT16_HI, /* type */
520 16, /* rightshift */
521 1, /* size (0 = byte, 1 = short, 2 = long) */
522 16, /* bitsize */
b34976b6 523 FALSE, /* pc_relative */
5bd4f169 524 0, /* bitpos */
f9c6b907 525 complain_overflow_signed,/* complain_on_overflow */
805fc799 526 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 527 "R_PPC64_GOT16_HI", /* name */
b34976b6 528 FALSE, /* partial_inplace */
5bd4f169
AM
529 0, /* src_mask */
530 0xffff, /* dst_mask */
b34976b6 531 FALSE), /* pcrel_offset */
5bd4f169
AM
532
533 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
534 the symbol. */
535 HOWTO (R_PPC64_GOT16_HA, /* type */
536 16, /* rightshift */
537 1, /* size (0 = byte, 1 = short, 2 = long) */
538 16, /* bitsize */
b34976b6 539 FALSE, /* pc_relative */
5bd4f169 540 0, /* bitpos */
f9c6b907 541 complain_overflow_signed,/* complain_on_overflow */
805fc799 542 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 543 "R_PPC64_GOT16_HA", /* name */
b34976b6 544 FALSE, /* partial_inplace */
5bd4f169
AM
545 0, /* src_mask */
546 0xffff, /* dst_mask */
b34976b6 547 FALSE), /* pcrel_offset */
5bd4f169
AM
548
549 /* This is used only by the dynamic linker. The symbol should exist
550 both in the object being run and in some shared library. The
551 dynamic linker copies the data addressed by the symbol from the
552 shared library into the object, because the object being
553 run has to have the data at some particular address. */
554 HOWTO (R_PPC64_COPY, /* type */
555 0, /* rightshift */
f5e87a1d
AM
556 0, /* this one is variable size */
557 0, /* bitsize */
b34976b6 558 FALSE, /* pc_relative */
5bd4f169 559 0, /* bitpos */
f5e87a1d
AM
560 complain_overflow_dont, /* complain_on_overflow */
561 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 562 "R_PPC64_COPY", /* name */
b34976b6 563 FALSE, /* partial_inplace */
5bd4f169
AM
564 0, /* src_mask */
565 0, /* dst_mask */
b34976b6 566 FALSE), /* pcrel_offset */
5bd4f169
AM
567
568 /* Like R_PPC64_ADDR64, but used when setting global offset table
569 entries. */
570 HOWTO (R_PPC64_GLOB_DAT, /* type */
571 0, /* rightshift */
572 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
573 64, /* bitsize */
b34976b6 574 FALSE, /* pc_relative */
5bd4f169
AM
575 0, /* bitpos */
576 complain_overflow_dont, /* complain_on_overflow */
805fc799 577 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 578 "R_PPC64_GLOB_DAT", /* name */
b34976b6 579 FALSE, /* partial_inplace */
5bd4f169 580 0, /* src_mask */
f5e87a1d 581 ONES (64), /* dst_mask */
b34976b6 582 FALSE), /* pcrel_offset */
5bd4f169
AM
583
584 /* Created by the link editor. Marks a procedure linkage table
585 entry for a symbol. */
586 HOWTO (R_PPC64_JMP_SLOT, /* type */
587 0, /* rightshift */
588 0, /* size (0 = byte, 1 = short, 2 = long) */
589 0, /* bitsize */
b34976b6 590 FALSE, /* pc_relative */
5bd4f169
AM
591 0, /* bitpos */
592 complain_overflow_dont, /* complain_on_overflow */
805fc799 593 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 594 "R_PPC64_JMP_SLOT", /* name */
b34976b6 595 FALSE, /* partial_inplace */
5bd4f169
AM
596 0, /* src_mask */
597 0, /* dst_mask */
b34976b6 598 FALSE), /* pcrel_offset */
5bd4f169
AM
599
600 /* Used only by the dynamic linker. When the object is run, this
601 doubleword64 is set to the load address of the object, plus the
602 addend. */
603 HOWTO (R_PPC64_RELATIVE, /* type */
604 0, /* rightshift */
605 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
606 64, /* bitsize */
b34976b6 607 FALSE, /* pc_relative */
5bd4f169
AM
608 0, /* bitpos */
609 complain_overflow_dont, /* complain_on_overflow */
610 bfd_elf_generic_reloc, /* special_function */
611 "R_PPC64_RELATIVE", /* name */
b34976b6 612 FALSE, /* partial_inplace */
5bd4f169 613 0, /* src_mask */
f5e87a1d 614 ONES (64), /* dst_mask */
b34976b6 615 FALSE), /* pcrel_offset */
5bd4f169
AM
616
617 /* Like R_PPC64_ADDR32, but may be unaligned. */
618 HOWTO (R_PPC64_UADDR32, /* type */
619 0, /* rightshift */
620 2, /* size (0 = byte, 1 = short, 2 = long) */
621 32, /* bitsize */
b34976b6 622 FALSE, /* pc_relative */
5bd4f169
AM
623 0, /* bitpos */
624 complain_overflow_bitfield, /* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
626 "R_PPC64_UADDR32", /* name */
b34976b6 627 FALSE, /* partial_inplace */
5bd4f169
AM
628 0, /* src_mask */
629 0xffffffff, /* dst_mask */
b34976b6 630 FALSE), /* pcrel_offset */
5bd4f169
AM
631
632 /* Like R_PPC64_ADDR16, but may be unaligned. */
633 HOWTO (R_PPC64_UADDR16, /* type */
634 0, /* rightshift */
635 1, /* size (0 = byte, 1 = short, 2 = long) */
636 16, /* bitsize */
b34976b6 637 FALSE, /* pc_relative */
5bd4f169
AM
638 0, /* bitpos */
639 complain_overflow_bitfield, /* complain_on_overflow */
640 bfd_elf_generic_reloc, /* special_function */
641 "R_PPC64_UADDR16", /* name */
b34976b6 642 FALSE, /* partial_inplace */
5bd4f169
AM
643 0, /* src_mask */
644 0xffff, /* dst_mask */
b34976b6 645 FALSE), /* pcrel_offset */
5bd4f169
AM
646
647 /* 32-bit PC relative. */
648 HOWTO (R_PPC64_REL32, /* type */
649 0, /* rightshift */
650 2, /* size (0 = byte, 1 = short, 2 = long) */
651 32, /* bitsize */
b34976b6 652 TRUE, /* pc_relative */
5bd4f169 653 0, /* bitpos */
5bd4f169
AM
654 complain_overflow_signed, /* complain_on_overflow */
655 bfd_elf_generic_reloc, /* special_function */
656 "R_PPC64_REL32", /* name */
b34976b6 657 FALSE, /* partial_inplace */
5bd4f169
AM
658 0, /* src_mask */
659 0xffffffff, /* dst_mask */
b34976b6 660 TRUE), /* pcrel_offset */
5bd4f169 661
10ed1bba 662 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
663 HOWTO (R_PPC64_PLT32, /* type */
664 0, /* rightshift */
665 2, /* size (0 = byte, 1 = short, 2 = long) */
666 32, /* bitsize */
b34976b6 667 FALSE, /* pc_relative */
5bd4f169
AM
668 0, /* bitpos */
669 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 670 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 671 "R_PPC64_PLT32", /* name */
b34976b6 672 FALSE, /* partial_inplace */
5bd4f169 673 0, /* src_mask */
f5e87a1d 674 0xffffffff, /* dst_mask */
b34976b6 675 FALSE), /* pcrel_offset */
5bd4f169
AM
676
677 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
678 FIXME: R_PPC64_PLTREL32 not supported. */
679 HOWTO (R_PPC64_PLTREL32, /* type */
680 0, /* rightshift */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
682 32, /* bitsize */
b34976b6 683 TRUE, /* pc_relative */
5bd4f169
AM
684 0, /* bitpos */
685 complain_overflow_signed, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_PPC64_PLTREL32", /* name */
b34976b6 688 FALSE, /* partial_inplace */
5bd4f169 689 0, /* src_mask */
f5e87a1d 690 0xffffffff, /* dst_mask */
b34976b6 691 TRUE), /* pcrel_offset */
5bd4f169
AM
692
693 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
694 the symbol. */
695 HOWTO (R_PPC64_PLT16_LO, /* type */
696 0, /* rightshift */
697 1, /* size (0 = byte, 1 = short, 2 = long) */
698 16, /* bitsize */
b34976b6 699 FALSE, /* pc_relative */
5bd4f169
AM
700 0, /* bitpos */
701 complain_overflow_dont, /* complain_on_overflow */
805fc799 702 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 703 "R_PPC64_PLT16_LO", /* name */
b34976b6 704 FALSE, /* partial_inplace */
5bd4f169
AM
705 0, /* src_mask */
706 0xffff, /* dst_mask */
b34976b6 707 FALSE), /* pcrel_offset */
5bd4f169
AM
708
709 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
710 the symbol. */
711 HOWTO (R_PPC64_PLT16_HI, /* type */
712 16, /* rightshift */
713 1, /* size (0 = byte, 1 = short, 2 = long) */
714 16, /* bitsize */
b34976b6 715 FALSE, /* pc_relative */
5bd4f169 716 0, /* bitpos */
f9c6b907 717 complain_overflow_signed, /* complain_on_overflow */
805fc799 718 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 719 "R_PPC64_PLT16_HI", /* name */
b34976b6 720 FALSE, /* partial_inplace */
5bd4f169
AM
721 0, /* src_mask */
722 0xffff, /* dst_mask */
b34976b6 723 FALSE), /* pcrel_offset */
5bd4f169
AM
724
725 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
726 the symbol. */
727 HOWTO (R_PPC64_PLT16_HA, /* type */
728 16, /* rightshift */
729 1, /* size (0 = byte, 1 = short, 2 = long) */
730 16, /* bitsize */
b34976b6 731 FALSE, /* pc_relative */
5bd4f169 732 0, /* bitpos */
f9c6b907 733 complain_overflow_signed, /* complain_on_overflow */
805fc799 734 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 735 "R_PPC64_PLT16_HA", /* name */
b34976b6 736 FALSE, /* partial_inplace */
5bd4f169
AM
737 0, /* src_mask */
738 0xffff, /* dst_mask */
b34976b6 739 FALSE), /* pcrel_offset */
5bd4f169 740
c061c2d8 741 /* 16-bit section relative relocation. */
5bd4f169
AM
742 HOWTO (R_PPC64_SECTOFF, /* type */
743 0, /* rightshift */
c061c2d8
AM
744 1, /* size (0 = byte, 1 = short, 2 = long) */
745 16, /* bitsize */
b34976b6 746 FALSE, /* pc_relative */
5bd4f169 747 0, /* bitpos */
b80eed39 748 complain_overflow_signed, /* complain_on_overflow */
805fc799 749 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 750 "R_PPC64_SECTOFF", /* name */
b34976b6 751 FALSE, /* partial_inplace */
5bd4f169 752 0, /* src_mask */
c061c2d8 753 0xffff, /* dst_mask */
b34976b6 754 FALSE), /* pcrel_offset */
5bd4f169 755
c061c2d8 756 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
757 HOWTO (R_PPC64_SECTOFF_LO, /* type */
758 0, /* rightshift */
759 1, /* size (0 = byte, 1 = short, 2 = long) */
760 16, /* bitsize */
b34976b6 761 FALSE, /* pc_relative */
5bd4f169
AM
762 0, /* bitpos */
763 complain_overflow_dont, /* complain_on_overflow */
805fc799 764 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 765 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 766 FALSE, /* partial_inplace */
5bd4f169
AM
767 0, /* src_mask */
768 0xffff, /* dst_mask */
b34976b6 769 FALSE), /* pcrel_offset */
5bd4f169
AM
770
771 /* 16-bit upper half section relative relocation. */
772 HOWTO (R_PPC64_SECTOFF_HI, /* type */
773 16, /* rightshift */
774 1, /* size (0 = byte, 1 = short, 2 = long) */
775 16, /* bitsize */
b34976b6 776 FALSE, /* pc_relative */
5bd4f169 777 0, /* bitpos */
f9c6b907 778 complain_overflow_signed, /* complain_on_overflow */
805fc799 779 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 780 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 781 FALSE, /* partial_inplace */
5bd4f169
AM
782 0, /* src_mask */
783 0xffff, /* dst_mask */
b34976b6 784 FALSE), /* pcrel_offset */
5bd4f169
AM
785
786 /* 16-bit upper half adjusted section relative relocation. */
787 HOWTO (R_PPC64_SECTOFF_HA, /* type */
788 16, /* rightshift */
789 1, /* size (0 = byte, 1 = short, 2 = long) */
790 16, /* bitsize */
b34976b6 791 FALSE, /* pc_relative */
5bd4f169 792 0, /* bitpos */
f9c6b907 793 complain_overflow_signed, /* complain_on_overflow */
805fc799 794 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 795 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 796 FALSE, /* partial_inplace */
5bd4f169
AM
797 0, /* src_mask */
798 0xffff, /* dst_mask */
b34976b6 799 FALSE), /* pcrel_offset */
5bd4f169 800
04c9666a
AM
801 /* Like R_PPC64_REL24 without touching the two least significant bits. */
802 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
803 2, /* rightshift */
804 2, /* size (0 = byte, 1 = short, 2 = long) */
805 30, /* bitsize */
b34976b6 806 TRUE, /* pc_relative */
5bd4f169
AM
807 0, /* bitpos */
808 complain_overflow_dont, /* complain_on_overflow */
809 bfd_elf_generic_reloc, /* special_function */
04c9666a 810 "R_PPC64_REL30", /* name */
b34976b6 811 FALSE, /* partial_inplace */
d006db6c 812 0, /* src_mask */
5bd4f169 813 0xfffffffc, /* dst_mask */
b34976b6 814 TRUE), /* pcrel_offset */
5bd4f169
AM
815
816 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
817
818 /* A standard 64-bit relocation. */
819 HOWTO (R_PPC64_ADDR64, /* type */
820 0, /* rightshift */
821 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
822 64, /* bitsize */
b34976b6 823 FALSE, /* pc_relative */
5bd4f169
AM
824 0, /* bitpos */
825 complain_overflow_dont, /* complain_on_overflow */
826 bfd_elf_generic_reloc, /* special_function */
827 "R_PPC64_ADDR64", /* name */
b34976b6 828 FALSE, /* partial_inplace */
5bd4f169 829 0, /* src_mask */
f5e87a1d 830 ONES (64), /* dst_mask */
b34976b6 831 FALSE), /* pcrel_offset */
5bd4f169
AM
832
833 /* The bits 32-47 of an address. */
834 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
835 32, /* rightshift */
836 1, /* size (0 = byte, 1 = short, 2 = long) */
837 16, /* bitsize */
b34976b6 838 FALSE, /* pc_relative */
5bd4f169
AM
839 0, /* bitpos */
840 complain_overflow_dont, /* complain_on_overflow */
841 bfd_elf_generic_reloc, /* special_function */
842 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 843 FALSE, /* partial_inplace */
5bd4f169
AM
844 0, /* src_mask */
845 0xffff, /* dst_mask */
b34976b6 846 FALSE), /* pcrel_offset */
5bd4f169
AM
847
848 /* The bits 32-47 of an address, plus 1 if the contents of the low
849 16 bits, treated as a signed number, is negative. */
850 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
851 32, /* rightshift */
852 1, /* size (0 = byte, 1 = short, 2 = long) */
853 16, /* bitsize */
b34976b6 854 FALSE, /* pc_relative */
5bd4f169
AM
855 0, /* bitpos */
856 complain_overflow_dont, /* complain_on_overflow */
805fc799 857 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 858 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 859 FALSE, /* partial_inplace */
5bd4f169
AM
860 0, /* src_mask */
861 0xffff, /* dst_mask */
b34976b6 862 FALSE), /* pcrel_offset */
5bd4f169
AM
863
864 /* The bits 48-63 of an address. */
865 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
866 48, /* rightshift */
867 1, /* size (0 = byte, 1 = short, 2 = long) */
868 16, /* bitsize */
b34976b6 869 FALSE, /* pc_relative */
5bd4f169
AM
870 0, /* bitpos */
871 complain_overflow_dont, /* complain_on_overflow */
872 bfd_elf_generic_reloc, /* special_function */
873 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 874 FALSE, /* partial_inplace */
5bd4f169
AM
875 0, /* src_mask */
876 0xffff, /* dst_mask */
b34976b6 877 FALSE), /* pcrel_offset */
5bd4f169
AM
878
879 /* The bits 48-63 of an address, plus 1 if the contents of the low
880 16 bits, treated as a signed number, is negative. */
881 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
882 48, /* rightshift */
883 1, /* size (0 = byte, 1 = short, 2 = long) */
884 16, /* bitsize */
b34976b6 885 FALSE, /* pc_relative */
5bd4f169
AM
886 0, /* bitpos */
887 complain_overflow_dont, /* complain_on_overflow */
805fc799 888 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 889 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 890 FALSE, /* partial_inplace */
5bd4f169
AM
891 0, /* src_mask */
892 0xffff, /* dst_mask */
b34976b6 893 FALSE), /* pcrel_offset */
5bd4f169
AM
894
895 /* Like ADDR64, but may be unaligned. */
896 HOWTO (R_PPC64_UADDR64, /* type */
897 0, /* rightshift */
898 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
899 64, /* bitsize */
b34976b6 900 FALSE, /* pc_relative */
5bd4f169
AM
901 0, /* bitpos */
902 complain_overflow_dont, /* complain_on_overflow */
903 bfd_elf_generic_reloc, /* special_function */
904 "R_PPC64_UADDR64", /* name */
b34976b6 905 FALSE, /* partial_inplace */
5bd4f169 906 0, /* src_mask */
f5e87a1d 907 ONES (64), /* dst_mask */
b34976b6 908 FALSE), /* pcrel_offset */
5bd4f169
AM
909
910 /* 64-bit relative relocation. */
911 HOWTO (R_PPC64_REL64, /* type */
912 0, /* rightshift */
913 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
914 64, /* bitsize */
b34976b6 915 TRUE, /* pc_relative */
5bd4f169
AM
916 0, /* bitpos */
917 complain_overflow_dont, /* complain_on_overflow */
918 bfd_elf_generic_reloc, /* special_function */
919 "R_PPC64_REL64", /* name */
b34976b6 920 FALSE, /* partial_inplace */
5bd4f169 921 0, /* src_mask */
f5e87a1d 922 ONES (64), /* dst_mask */
b34976b6 923 TRUE), /* pcrel_offset */
5bd4f169 924
cedb70c5 925 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
926 HOWTO (R_PPC64_PLT64, /* type */
927 0, /* rightshift */
928 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
929 64, /* bitsize */
b34976b6 930 FALSE, /* pc_relative */
5bd4f169
AM
931 0, /* bitpos */
932 complain_overflow_dont, /* complain_on_overflow */
805fc799 933 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 934 "R_PPC64_PLT64", /* name */
b34976b6 935 FALSE, /* partial_inplace */
5bd4f169 936 0, /* src_mask */
f5e87a1d 937 ONES (64), /* dst_mask */
b34976b6 938 FALSE), /* pcrel_offset */
5bd4f169
AM
939
940 /* 64-bit PC relative relocation to the symbol's procedure linkage
941 table. */
942 /* FIXME: R_PPC64_PLTREL64 not supported. */
943 HOWTO (R_PPC64_PLTREL64, /* type */
944 0, /* rightshift */
945 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
946 64, /* bitsize */
b34976b6 947 TRUE, /* pc_relative */
5bd4f169
AM
948 0, /* bitpos */
949 complain_overflow_dont, /* complain_on_overflow */
805fc799 950 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 951 "R_PPC64_PLTREL64", /* name */
b34976b6 952 FALSE, /* partial_inplace */
5bd4f169 953 0, /* src_mask */
f5e87a1d 954 ONES (64), /* dst_mask */
b34976b6 955 TRUE), /* pcrel_offset */
5bd4f169
AM
956
957 /* 16 bit TOC-relative relocation. */
958
959 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
960 HOWTO (R_PPC64_TOC16, /* type */
961 0, /* rightshift */
962 1, /* size (0 = byte, 1 = short, 2 = long) */
963 16, /* bitsize */
b34976b6 964 FALSE, /* pc_relative */
5bd4f169
AM
965 0, /* bitpos */
966 complain_overflow_signed, /* complain_on_overflow */
805fc799 967 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 968 "R_PPC64_TOC16", /* name */
b34976b6 969 FALSE, /* partial_inplace */
5bd4f169
AM
970 0, /* src_mask */
971 0xffff, /* dst_mask */
b34976b6 972 FALSE), /* pcrel_offset */
5bd4f169
AM
973
974 /* 16 bit TOC-relative relocation without overflow. */
975
976 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
977 HOWTO (R_PPC64_TOC16_LO, /* type */
978 0, /* rightshift */
979 1, /* size (0 = byte, 1 = short, 2 = long) */
980 16, /* bitsize */
b34976b6 981 FALSE, /* pc_relative */
5bd4f169
AM
982 0, /* bitpos */
983 complain_overflow_dont, /* complain_on_overflow */
805fc799 984 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 985 "R_PPC64_TOC16_LO", /* name */
b34976b6 986 FALSE, /* partial_inplace */
5bd4f169
AM
987 0, /* src_mask */
988 0xffff, /* dst_mask */
b34976b6 989 FALSE), /* pcrel_offset */
5bd4f169
AM
990
991 /* 16 bit TOC-relative relocation, high 16 bits. */
992
993 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
994 HOWTO (R_PPC64_TOC16_HI, /* type */
995 16, /* rightshift */
996 1, /* size (0 = byte, 1 = short, 2 = long) */
997 16, /* bitsize */
b34976b6 998 FALSE, /* pc_relative */
5bd4f169 999 0, /* bitpos */
f9c6b907 1000 complain_overflow_signed, /* complain_on_overflow */
805fc799 1001 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1002 "R_PPC64_TOC16_HI", /* name */
b34976b6 1003 FALSE, /* partial_inplace */
5bd4f169
AM
1004 0, /* src_mask */
1005 0xffff, /* dst_mask */
b34976b6 1006 FALSE), /* pcrel_offset */
5bd4f169
AM
1007
1008 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1009 contents of the low 16 bits, treated as a signed number, is
1010 negative. */
1011
1012 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1013 HOWTO (R_PPC64_TOC16_HA, /* type */
1014 16, /* rightshift */
1015 1, /* size (0 = byte, 1 = short, 2 = long) */
1016 16, /* bitsize */
b34976b6 1017 FALSE, /* pc_relative */
5bd4f169 1018 0, /* bitpos */
f9c6b907 1019 complain_overflow_signed, /* complain_on_overflow */
805fc799 1020 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1021 "R_PPC64_TOC16_HA", /* name */
b34976b6 1022 FALSE, /* partial_inplace */
5bd4f169
AM
1023 0, /* src_mask */
1024 0xffff, /* dst_mask */
b34976b6 1025 FALSE), /* pcrel_offset */
5bd4f169
AM
1026
1027 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1028
1029 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1030 HOWTO (R_PPC64_TOC, /* type */
1031 0, /* rightshift */
1032 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1033 64, /* bitsize */
b34976b6 1034 FALSE, /* pc_relative */
5bd4f169 1035 0, /* bitpos */
b80eed39 1036 complain_overflow_dont, /* complain_on_overflow */
805fc799 1037 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1038 "R_PPC64_TOC", /* name */
b34976b6 1039 FALSE, /* partial_inplace */
5bd4f169 1040 0, /* src_mask */
f5e87a1d 1041 ONES (64), /* dst_mask */
b34976b6 1042 FALSE), /* pcrel_offset */
5bd4f169
AM
1043
1044 /* Like R_PPC64_GOT16, but also informs the link editor that the
1045 value to relocate may (!) refer to a PLT entry which the link
1046 editor (a) may replace with the symbol value. If the link editor
1047 is unable to fully resolve the symbol, it may (b) create a PLT
1048 entry and store the address to the new PLT entry in the GOT.
1049 This permits lazy resolution of function symbols at run time.
1050 The link editor may also skip all of this and just (c) emit a
1051 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1052 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1053 HOWTO (R_PPC64_PLTGOT16, /* type */
1054 0, /* rightshift */
1055 1, /* size (0 = byte, 1 = short, 2 = long) */
1056 16, /* bitsize */
b34976b6 1057 FALSE, /* pc_relative */
5bd4f169
AM
1058 0, /* bitpos */
1059 complain_overflow_signed, /* complain_on_overflow */
805fc799 1060 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1061 "R_PPC64_PLTGOT16", /* name */
1062 FALSE, /* partial_inplace */
1063 0, /* src_mask */
1064 0xffff, /* dst_mask */
1065 FALSE), /* pcrel_offset */
1066
1067 /* Like R_PPC64_PLTGOT16, but without overflow. */
1068 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1069 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1070 0, /* rightshift */
1071 1, /* size (0 = byte, 1 = short, 2 = long) */
1072 16, /* bitsize */
1073 FALSE, /* pc_relative */
1074 0, /* bitpos */
1075 complain_overflow_dont, /* complain_on_overflow */
1076 ppc64_elf_unhandled_reloc, /* special_function */
1077 "R_PPC64_PLTGOT16_LO", /* name */
1078 FALSE, /* partial_inplace */
1079 0, /* src_mask */
1080 0xffff, /* dst_mask */
1081 FALSE), /* pcrel_offset */
1082
1083 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1084 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1085 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1086 16, /* rightshift */
1087 1, /* size (0 = byte, 1 = short, 2 = long) */
1088 16, /* bitsize */
1089 FALSE, /* pc_relative */
1090 0, /* bitpos */
f9c6b907 1091 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1092 ppc64_elf_unhandled_reloc, /* special_function */
1093 "R_PPC64_PLTGOT16_HI", /* name */
1094 FALSE, /* partial_inplace */
1095 0, /* src_mask */
1096 0xffff, /* dst_mask */
1097 FALSE), /* pcrel_offset */
1098
1099 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1100 1 if the contents of the low 16 bits, treated as a signed number,
1101 is negative. */
1102 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1103 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1104 16, /* rightshift */
1105 1, /* size (0 = byte, 1 = short, 2 = long) */
1106 16, /* bitsize */
1107 FALSE, /* pc_relative */
1108 0, /* bitpos */
f9c6b907 1109 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1110 ppc64_elf_unhandled_reloc, /* special_function */
1111 "R_PPC64_PLTGOT16_HA", /* name */
1112 FALSE, /* partial_inplace */
1113 0, /* src_mask */
1114 0xffff, /* dst_mask */
1115 FALSE), /* pcrel_offset */
1116
1117 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1118 HOWTO (R_PPC64_ADDR16_DS, /* type */
1119 0, /* rightshift */
1120 1, /* size (0 = byte, 1 = short, 2 = long) */
1121 16, /* bitsize */
1122 FALSE, /* pc_relative */
1123 0, /* bitpos */
b80eed39 1124 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1125 bfd_elf_generic_reloc, /* special_function */
1126 "R_PPC64_ADDR16_DS", /* name */
1127 FALSE, /* partial_inplace */
1128 0, /* src_mask */
1129 0xfffc, /* dst_mask */
1130 FALSE), /* pcrel_offset */
1131
1132 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1133 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1134 0, /* rightshift */
1135 1, /* size (0 = byte, 1 = short, 2 = long) */
1136 16, /* bitsize */
1137 FALSE, /* pc_relative */
1138 0, /* bitpos */
1139 complain_overflow_dont,/* complain_on_overflow */
1140 bfd_elf_generic_reloc, /* special_function */
1141 "R_PPC64_ADDR16_LO_DS",/* name */
1142 FALSE, /* partial_inplace */
1143 0, /* src_mask */
1144 0xfffc, /* dst_mask */
1145 FALSE), /* pcrel_offset */
1146
1147 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1148 HOWTO (R_PPC64_GOT16_DS, /* type */
1149 0, /* rightshift */
1150 1, /* size (0 = byte, 1 = short, 2 = long) */
1151 16, /* bitsize */
1152 FALSE, /* pc_relative */
1153 0, /* bitpos */
1154 complain_overflow_signed, /* complain_on_overflow */
1155 ppc64_elf_unhandled_reloc, /* special_function */
1156 "R_PPC64_GOT16_DS", /* name */
1157 FALSE, /* partial_inplace */
1158 0, /* src_mask */
1159 0xfffc, /* dst_mask */
1160 FALSE), /* pcrel_offset */
1161
1162 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1163 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1164 0, /* rightshift */
1165 1, /* size (0 = byte, 1 = short, 2 = long) */
1166 16, /* bitsize */
1167 FALSE, /* pc_relative */
1168 0, /* bitpos */
1169 complain_overflow_dont, /* complain_on_overflow */
1170 ppc64_elf_unhandled_reloc, /* special_function */
1171 "R_PPC64_GOT16_LO_DS", /* name */
1172 FALSE, /* partial_inplace */
1173 0, /* src_mask */
1174 0xfffc, /* dst_mask */
1175 FALSE), /* pcrel_offset */
1176
1177 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1178 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1179 0, /* rightshift */
1180 1, /* size (0 = byte, 1 = short, 2 = long) */
1181 16, /* bitsize */
1182 FALSE, /* pc_relative */
1183 0, /* bitpos */
1184 complain_overflow_dont, /* complain_on_overflow */
1185 ppc64_elf_unhandled_reloc, /* special_function */
1186 "R_PPC64_PLT16_LO_DS", /* name */
1187 FALSE, /* partial_inplace */
1188 0, /* src_mask */
1189 0xfffc, /* dst_mask */
1190 FALSE), /* pcrel_offset */
1191
1192 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1193 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1194 0, /* rightshift */
1195 1, /* size (0 = byte, 1 = short, 2 = long) */
1196 16, /* bitsize */
1197 FALSE, /* pc_relative */
1198 0, /* bitpos */
b80eed39 1199 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1200 ppc64_elf_sectoff_reloc, /* special_function */
1201 "R_PPC64_SECTOFF_DS", /* name */
1202 FALSE, /* partial_inplace */
1203 0, /* src_mask */
1204 0xfffc, /* dst_mask */
1205 FALSE), /* pcrel_offset */
1206
1207 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1208 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1209 0, /* rightshift */
1210 1, /* size (0 = byte, 1 = short, 2 = long) */
1211 16, /* bitsize */
1212 FALSE, /* pc_relative */
1213 0, /* bitpos */
1214 complain_overflow_dont, /* complain_on_overflow */
1215 ppc64_elf_sectoff_reloc, /* special_function */
1216 "R_PPC64_SECTOFF_LO_DS",/* name */
1217 FALSE, /* partial_inplace */
1218 0, /* src_mask */
1219 0xfffc, /* dst_mask */
1220 FALSE), /* pcrel_offset */
1221
1222 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1223 HOWTO (R_PPC64_TOC16_DS, /* type */
1224 0, /* rightshift */
1225 1, /* size (0 = byte, 1 = short, 2 = long) */
1226 16, /* bitsize */
1227 FALSE, /* pc_relative */
1228 0, /* bitpos */
1229 complain_overflow_signed, /* complain_on_overflow */
1230 ppc64_elf_toc_reloc, /* special_function */
1231 "R_PPC64_TOC16_DS", /* name */
1232 FALSE, /* partial_inplace */
1233 0, /* src_mask */
1234 0xfffc, /* dst_mask */
1235 FALSE), /* pcrel_offset */
1236
1237 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1238 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1239 0, /* rightshift */
1240 1, /* size (0 = byte, 1 = short, 2 = long) */
1241 16, /* bitsize */
1242 FALSE, /* pc_relative */
1243 0, /* bitpos */
1244 complain_overflow_dont, /* complain_on_overflow */
1245 ppc64_elf_toc_reloc, /* special_function */
1246 "R_PPC64_TOC16_LO_DS", /* name */
1247 FALSE, /* partial_inplace */
1248 0, /* src_mask */
1249 0xfffc, /* dst_mask */
1250 FALSE), /* pcrel_offset */
1251
1252 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1253 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1254 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1255 0, /* rightshift */
1256 1, /* size (0 = byte, 1 = short, 2 = long) */
1257 16, /* bitsize */
1258 FALSE, /* pc_relative */
1259 0, /* bitpos */
1260 complain_overflow_signed, /* complain_on_overflow */
1261 ppc64_elf_unhandled_reloc, /* special_function */
1262 "R_PPC64_PLTGOT16_DS", /* name */
1263 FALSE, /* partial_inplace */
1264 0, /* src_mask */
1265 0xfffc, /* dst_mask */
1266 FALSE), /* pcrel_offset */
1267
1268 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1269 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1270 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1271 0, /* rightshift */
1272 1, /* size (0 = byte, 1 = short, 2 = long) */
1273 16, /* bitsize */
1274 FALSE, /* pc_relative */
1275 0, /* bitpos */
1276 complain_overflow_dont, /* complain_on_overflow */
1277 ppc64_elf_unhandled_reloc, /* special_function */
1278 "R_PPC64_PLTGOT16_LO_DS",/* name */
1279 FALSE, /* partial_inplace */
1280 0, /* src_mask */
1281 0xfffc, /* dst_mask */
1282 FALSE), /* pcrel_offset */
1283
727fc41e 1284 /* Marker relocs for TLS. */
411e1bfb
AM
1285 HOWTO (R_PPC64_TLS,
1286 0, /* rightshift */
1287 2, /* size (0 = byte, 1 = short, 2 = long) */
1288 32, /* bitsize */
1289 FALSE, /* pc_relative */
1290 0, /* bitpos */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 bfd_elf_generic_reloc, /* special_function */
1293 "R_PPC64_TLS", /* name */
1294 FALSE, /* partial_inplace */
1295 0, /* src_mask */
1296 0, /* dst_mask */
1297 FALSE), /* pcrel_offset */
1298
727fc41e
AM
1299 HOWTO (R_PPC64_TLSGD,
1300 0, /* rightshift */
1301 2, /* size (0 = byte, 1 = short, 2 = long) */
1302 32, /* bitsize */
1303 FALSE, /* pc_relative */
1304 0, /* bitpos */
1305 complain_overflow_dont, /* complain_on_overflow */
1306 bfd_elf_generic_reloc, /* special_function */
1307 "R_PPC64_TLSGD", /* name */
1308 FALSE, /* partial_inplace */
1309 0, /* src_mask */
1310 0, /* dst_mask */
1311 FALSE), /* pcrel_offset */
1312
1313 HOWTO (R_PPC64_TLSLD,
1314 0, /* rightshift */
1315 2, /* size (0 = byte, 1 = short, 2 = long) */
1316 32, /* bitsize */
1317 FALSE, /* pc_relative */
1318 0, /* bitpos */
1319 complain_overflow_dont, /* complain_on_overflow */
1320 bfd_elf_generic_reloc, /* special_function */
1321 "R_PPC64_TLSLD", /* name */
1322 FALSE, /* partial_inplace */
1323 0, /* src_mask */
1324 0, /* dst_mask */
1325 FALSE), /* pcrel_offset */
1326
3b421ab3
AM
1327 HOWTO (R_PPC64_TOCSAVE,
1328 0, /* rightshift */
1329 2, /* size (0 = byte, 1 = short, 2 = long) */
1330 32, /* bitsize */
1331 FALSE, /* pc_relative */
1332 0, /* bitpos */
1333 complain_overflow_dont, /* complain_on_overflow */
1334 bfd_elf_generic_reloc, /* special_function */
1335 "R_PPC64_TOCSAVE", /* name */
1336 FALSE, /* partial_inplace */
1337 0, /* src_mask */
1338 0, /* dst_mask */
1339 FALSE), /* pcrel_offset */
1340
411e1bfb
AM
1341 /* Computes the load module index of the load module that contains the
1342 definition of its TLS sym. */
1343 HOWTO (R_PPC64_DTPMOD64,
1344 0, /* rightshift */
1345 4, /* size (0 = byte, 1 = short, 2 = long) */
1346 64, /* bitsize */
1347 FALSE, /* pc_relative */
1348 0, /* bitpos */
1349 complain_overflow_dont, /* complain_on_overflow */
1350 ppc64_elf_unhandled_reloc, /* special_function */
1351 "R_PPC64_DTPMOD64", /* name */
1352 FALSE, /* partial_inplace */
1353 0, /* src_mask */
1354 ONES (64), /* dst_mask */
1355 FALSE), /* pcrel_offset */
1356
1357 /* Computes a dtv-relative displacement, the difference between the value
1358 of sym+add and the base address of the thread-local storage block that
1359 contains the definition of sym, minus 0x8000. */
1360 HOWTO (R_PPC64_DTPREL64,
1361 0, /* rightshift */
1362 4, /* size (0 = byte, 1 = short, 2 = long) */
1363 64, /* bitsize */
1364 FALSE, /* pc_relative */
1365 0, /* bitpos */
1366 complain_overflow_dont, /* complain_on_overflow */
1367 ppc64_elf_unhandled_reloc, /* special_function */
1368 "R_PPC64_DTPREL64", /* name */
1369 FALSE, /* partial_inplace */
1370 0, /* src_mask */
1371 ONES (64), /* dst_mask */
1372 FALSE), /* pcrel_offset */
1373
1374 /* A 16 bit dtprel reloc. */
1375 HOWTO (R_PPC64_DTPREL16,
1376 0, /* rightshift */
1377 1, /* size (0 = byte, 1 = short, 2 = long) */
1378 16, /* bitsize */
1379 FALSE, /* pc_relative */
1380 0, /* bitpos */
1381 complain_overflow_signed, /* complain_on_overflow */
1382 ppc64_elf_unhandled_reloc, /* special_function */
1383 "R_PPC64_DTPREL16", /* name */
1384 FALSE, /* partial_inplace */
1385 0, /* src_mask */
1386 0xffff, /* dst_mask */
1387 FALSE), /* pcrel_offset */
1388
1389 /* Like DTPREL16, but no overflow. */
1390 HOWTO (R_PPC64_DTPREL16_LO,
1391 0, /* rightshift */
1392 1, /* size (0 = byte, 1 = short, 2 = long) */
1393 16, /* bitsize */
1394 FALSE, /* pc_relative */
1395 0, /* bitpos */
1396 complain_overflow_dont, /* complain_on_overflow */
1397 ppc64_elf_unhandled_reloc, /* special_function */
1398 "R_PPC64_DTPREL16_LO", /* name */
1399 FALSE, /* partial_inplace */
1400 0, /* src_mask */
1401 0xffff, /* dst_mask */
1402 FALSE), /* pcrel_offset */
1403
1404 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1405 HOWTO (R_PPC64_DTPREL16_HI,
1406 16, /* rightshift */
1407 1, /* size (0 = byte, 1 = short, 2 = long) */
1408 16, /* bitsize */
1409 FALSE, /* pc_relative */
1410 0, /* bitpos */
f9c6b907 1411 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1412 ppc64_elf_unhandled_reloc, /* special_function */
1413 "R_PPC64_DTPREL16_HI", /* name */
1414 FALSE, /* partial_inplace */
1415 0, /* src_mask */
1416 0xffff, /* dst_mask */
1417 FALSE), /* pcrel_offset */
1418
1419 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1420 HOWTO (R_PPC64_DTPREL16_HA,
1421 16, /* rightshift */
1422 1, /* size (0 = byte, 1 = short, 2 = long) */
1423 16, /* bitsize */
1424 FALSE, /* pc_relative */
1425 0, /* bitpos */
f9c6b907 1426 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1427 ppc64_elf_unhandled_reloc, /* special_function */
1428 "R_PPC64_DTPREL16_HA", /* name */
1429 FALSE, /* partial_inplace */
1430 0, /* src_mask */
1431 0xffff, /* dst_mask */
1432 FALSE), /* pcrel_offset */
1433
1434 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1435 HOWTO (R_PPC64_DTPREL16_HIGHER,
1436 32, /* rightshift */
1437 1, /* size (0 = byte, 1 = short, 2 = long) */
1438 16, /* bitsize */
1439 FALSE, /* pc_relative */
1440 0, /* bitpos */
1441 complain_overflow_dont, /* complain_on_overflow */
1442 ppc64_elf_unhandled_reloc, /* special_function */
1443 "R_PPC64_DTPREL16_HIGHER", /* name */
1444 FALSE, /* partial_inplace */
1445 0, /* src_mask */
1446 0xffff, /* dst_mask */
1447 FALSE), /* pcrel_offset */
1448
1449 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1450 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1451 32, /* rightshift */
1452 1, /* size (0 = byte, 1 = short, 2 = long) */
1453 16, /* bitsize */
1454 FALSE, /* pc_relative */
1455 0, /* bitpos */
1456 complain_overflow_dont, /* complain_on_overflow */
1457 ppc64_elf_unhandled_reloc, /* special_function */
1458 "R_PPC64_DTPREL16_HIGHERA", /* name */
1459 FALSE, /* partial_inplace */
1460 0, /* src_mask */
1461 0xffff, /* dst_mask */
1462 FALSE), /* pcrel_offset */
1463
1464 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1465 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1466 48, /* rightshift */
1467 1, /* size (0 = byte, 1 = short, 2 = long) */
1468 16, /* bitsize */
1469 FALSE, /* pc_relative */
1470 0, /* bitpos */
1471 complain_overflow_dont, /* complain_on_overflow */
1472 ppc64_elf_unhandled_reloc, /* special_function */
1473 "R_PPC64_DTPREL16_HIGHEST", /* name */
1474 FALSE, /* partial_inplace */
1475 0, /* src_mask */
1476 0xffff, /* dst_mask */
1477 FALSE), /* pcrel_offset */
1478
1479 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1480 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1481 48, /* rightshift */
1482 1, /* size (0 = byte, 1 = short, 2 = long) */
1483 16, /* bitsize */
1484 FALSE, /* pc_relative */
1485 0, /* bitpos */
1486 complain_overflow_dont, /* complain_on_overflow */
1487 ppc64_elf_unhandled_reloc, /* special_function */
1488 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1489 FALSE, /* partial_inplace */
1490 0, /* src_mask */
1491 0xffff, /* dst_mask */
1492 FALSE), /* pcrel_offset */
1493
1494 /* Like DTPREL16, but for insns with a DS field. */
1495 HOWTO (R_PPC64_DTPREL16_DS,
1496 0, /* rightshift */
1497 1, /* size (0 = byte, 1 = short, 2 = long) */
1498 16, /* bitsize */
1499 FALSE, /* pc_relative */
1500 0, /* bitpos */
1501 complain_overflow_signed, /* complain_on_overflow */
1502 ppc64_elf_unhandled_reloc, /* special_function */
1503 "R_PPC64_DTPREL16_DS", /* name */
1504 FALSE, /* partial_inplace */
1505 0, /* src_mask */
1506 0xfffc, /* dst_mask */
1507 FALSE), /* pcrel_offset */
1508
1509 /* Like DTPREL16_DS, but no overflow. */
1510 HOWTO (R_PPC64_DTPREL16_LO_DS,
1511 0, /* rightshift */
1512 1, /* size (0 = byte, 1 = short, 2 = long) */
1513 16, /* bitsize */
1514 FALSE, /* pc_relative */
1515 0, /* bitpos */
1516 complain_overflow_dont, /* complain_on_overflow */
1517 ppc64_elf_unhandled_reloc, /* special_function */
1518 "R_PPC64_DTPREL16_LO_DS", /* name */
1519 FALSE, /* partial_inplace */
1520 0, /* src_mask */
1521 0xfffc, /* dst_mask */
1522 FALSE), /* pcrel_offset */
1523
1524 /* Computes a tp-relative displacement, the difference between the value of
1525 sym+add and the value of the thread pointer (r13). */
1526 HOWTO (R_PPC64_TPREL64,
1527 0, /* rightshift */
1528 4, /* size (0 = byte, 1 = short, 2 = long) */
1529 64, /* bitsize */
1530 FALSE, /* pc_relative */
1531 0, /* bitpos */
1532 complain_overflow_dont, /* complain_on_overflow */
1533 ppc64_elf_unhandled_reloc, /* special_function */
1534 "R_PPC64_TPREL64", /* name */
1535 FALSE, /* partial_inplace */
1536 0, /* src_mask */
1537 ONES (64), /* dst_mask */
1538 FALSE), /* pcrel_offset */
1539
1540 /* A 16 bit tprel reloc. */
1541 HOWTO (R_PPC64_TPREL16,
1542 0, /* rightshift */
1543 1, /* size (0 = byte, 1 = short, 2 = long) */
1544 16, /* bitsize */
1545 FALSE, /* pc_relative */
1546 0, /* bitpos */
1547 complain_overflow_signed, /* complain_on_overflow */
1548 ppc64_elf_unhandled_reloc, /* special_function */
1549 "R_PPC64_TPREL16", /* name */
1550 FALSE, /* partial_inplace */
1551 0, /* src_mask */
1552 0xffff, /* dst_mask */
1553 FALSE), /* pcrel_offset */
1554
1555 /* Like TPREL16, but no overflow. */
1556 HOWTO (R_PPC64_TPREL16_LO,
1557 0, /* rightshift */
1558 1, /* size (0 = byte, 1 = short, 2 = long) */
1559 16, /* bitsize */
1560 FALSE, /* pc_relative */
1561 0, /* bitpos */
1562 complain_overflow_dont, /* complain_on_overflow */
1563 ppc64_elf_unhandled_reloc, /* special_function */
1564 "R_PPC64_TPREL16_LO", /* name */
1565 FALSE, /* partial_inplace */
1566 0, /* src_mask */
1567 0xffff, /* dst_mask */
1568 FALSE), /* pcrel_offset */
1569
1570 /* Like TPREL16_LO, but next higher group of 16 bits. */
1571 HOWTO (R_PPC64_TPREL16_HI,
1572 16, /* rightshift */
1573 1, /* size (0 = byte, 1 = short, 2 = long) */
1574 16, /* bitsize */
1575 FALSE, /* pc_relative */
1576 0, /* bitpos */
f9c6b907 1577 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1578 ppc64_elf_unhandled_reloc, /* special_function */
1579 "R_PPC64_TPREL16_HI", /* name */
1580 FALSE, /* partial_inplace */
1581 0, /* src_mask */
1582 0xffff, /* dst_mask */
1583 FALSE), /* pcrel_offset */
1584
1585 /* Like TPREL16_HI, but adjust for low 16 bits. */
1586 HOWTO (R_PPC64_TPREL16_HA,
1587 16, /* rightshift */
1588 1, /* size (0 = byte, 1 = short, 2 = long) */
1589 16, /* bitsize */
1590 FALSE, /* pc_relative */
1591 0, /* bitpos */
f9c6b907 1592 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1593 ppc64_elf_unhandled_reloc, /* special_function */
1594 "R_PPC64_TPREL16_HA", /* name */
1595 FALSE, /* partial_inplace */
1596 0, /* src_mask */
1597 0xffff, /* dst_mask */
1598 FALSE), /* pcrel_offset */
1599
1600 /* Like TPREL16_HI, but next higher group of 16 bits. */
1601 HOWTO (R_PPC64_TPREL16_HIGHER,
1602 32, /* rightshift */
1603 1, /* size (0 = byte, 1 = short, 2 = long) */
1604 16, /* bitsize */
1605 FALSE, /* pc_relative */
1606 0, /* bitpos */
1607 complain_overflow_dont, /* complain_on_overflow */
1608 ppc64_elf_unhandled_reloc, /* special_function */
1609 "R_PPC64_TPREL16_HIGHER", /* name */
1610 FALSE, /* partial_inplace */
1611 0, /* src_mask */
1612 0xffff, /* dst_mask */
1613 FALSE), /* pcrel_offset */
1614
1615 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1616 HOWTO (R_PPC64_TPREL16_HIGHERA,
1617 32, /* rightshift */
1618 1, /* size (0 = byte, 1 = short, 2 = long) */
1619 16, /* bitsize */
1620 FALSE, /* pc_relative */
1621 0, /* bitpos */
1622 complain_overflow_dont, /* complain_on_overflow */
1623 ppc64_elf_unhandled_reloc, /* special_function */
1624 "R_PPC64_TPREL16_HIGHERA", /* name */
1625 FALSE, /* partial_inplace */
1626 0, /* src_mask */
1627 0xffff, /* dst_mask */
1628 FALSE), /* pcrel_offset */
1629
1630 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1631 HOWTO (R_PPC64_TPREL16_HIGHEST,
1632 48, /* rightshift */
1633 1, /* size (0 = byte, 1 = short, 2 = long) */
1634 16, /* bitsize */
1635 FALSE, /* pc_relative */
1636 0, /* bitpos */
1637 complain_overflow_dont, /* complain_on_overflow */
1638 ppc64_elf_unhandled_reloc, /* special_function */
1639 "R_PPC64_TPREL16_HIGHEST", /* name */
1640 FALSE, /* partial_inplace */
1641 0, /* src_mask */
1642 0xffff, /* dst_mask */
1643 FALSE), /* pcrel_offset */
1644
1645 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1646 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1647 48, /* rightshift */
1648 1, /* size (0 = byte, 1 = short, 2 = long) */
1649 16, /* bitsize */
1650 FALSE, /* pc_relative */
1651 0, /* bitpos */
1652 complain_overflow_dont, /* complain_on_overflow */
1653 ppc64_elf_unhandled_reloc, /* special_function */
1654 "R_PPC64_TPREL16_HIGHESTA", /* name */
1655 FALSE, /* partial_inplace */
1656 0, /* src_mask */
1657 0xffff, /* dst_mask */
1658 FALSE), /* pcrel_offset */
1659
1660 /* Like TPREL16, but for insns with a DS field. */
1661 HOWTO (R_PPC64_TPREL16_DS,
1662 0, /* rightshift */
1663 1, /* size (0 = byte, 1 = short, 2 = long) */
1664 16, /* bitsize */
1665 FALSE, /* pc_relative */
1666 0, /* bitpos */
1667 complain_overflow_signed, /* complain_on_overflow */
1668 ppc64_elf_unhandled_reloc, /* special_function */
1669 "R_PPC64_TPREL16_DS", /* name */
1670 FALSE, /* partial_inplace */
1671 0, /* src_mask */
1672 0xfffc, /* dst_mask */
1673 FALSE), /* pcrel_offset */
1674
1675 /* Like TPREL16_DS, but no overflow. */
1676 HOWTO (R_PPC64_TPREL16_LO_DS,
1677 0, /* rightshift */
1678 1, /* size (0 = byte, 1 = short, 2 = long) */
1679 16, /* bitsize */
1680 FALSE, /* pc_relative */
1681 0, /* bitpos */
1682 complain_overflow_dont, /* complain_on_overflow */
1683 ppc64_elf_unhandled_reloc, /* special_function */
1684 "R_PPC64_TPREL16_LO_DS", /* name */
1685 FALSE, /* partial_inplace */
1686 0, /* src_mask */
1687 0xfffc, /* dst_mask */
1688 FALSE), /* pcrel_offset */
1689
1690 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1691 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1692 to the first entry relative to the TOC base (r2). */
1693 HOWTO (R_PPC64_GOT_TLSGD16,
1694 0, /* rightshift */
1695 1, /* size (0 = byte, 1 = short, 2 = long) */
1696 16, /* bitsize */
1697 FALSE, /* pc_relative */
1698 0, /* bitpos */
1699 complain_overflow_signed, /* complain_on_overflow */
1700 ppc64_elf_unhandled_reloc, /* special_function */
1701 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1702 FALSE, /* partial_inplace */
5bd4f169
AM
1703 0, /* src_mask */
1704 0xffff, /* dst_mask */
b34976b6 1705 FALSE), /* pcrel_offset */
5bd4f169 1706
411e1bfb
AM
1707 /* Like GOT_TLSGD16, but no overflow. */
1708 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1709 0, /* rightshift */
1710 1, /* size (0 = byte, 1 = short, 2 = long) */
1711 16, /* bitsize */
b34976b6 1712 FALSE, /* pc_relative */
5bd4f169
AM
1713 0, /* bitpos */
1714 complain_overflow_dont, /* complain_on_overflow */
805fc799 1715 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1716 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1717 FALSE, /* partial_inplace */
5bd4f169
AM
1718 0, /* src_mask */
1719 0xffff, /* dst_mask */
b34976b6 1720 FALSE), /* pcrel_offset */
5bd4f169 1721
411e1bfb
AM
1722 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1723 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1724 16, /* rightshift */
1725 1, /* size (0 = byte, 1 = short, 2 = long) */
1726 16, /* bitsize */
b34976b6 1727 FALSE, /* pc_relative */
5bd4f169 1728 0, /* bitpos */
f9c6b907 1729 complain_overflow_signed, /* complain_on_overflow */
805fc799 1730 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1731 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1732 FALSE, /* partial_inplace */
5bd4f169
AM
1733 0, /* src_mask */
1734 0xffff, /* dst_mask */
b34976b6 1735 FALSE), /* pcrel_offset */
5bd4f169 1736
411e1bfb
AM
1737 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1738 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1739 16, /* rightshift */
1740 1, /* size (0 = byte, 1 = short, 2 = long) */
1741 16, /* bitsize */
b34976b6 1742 FALSE, /* pc_relative */
5bd4f169 1743 0, /* bitpos */
f9c6b907 1744 complain_overflow_signed, /* complain_on_overflow */
805fc799 1745 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1746 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1747 FALSE, /* partial_inplace */
5bd4f169
AM
1748 0, /* src_mask */
1749 0xffff, /* dst_mask */
b34976b6 1750 FALSE), /* pcrel_offset */
5bd4f169 1751
411e1bfb
AM
1752 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1753 with values (sym+add)@dtpmod and zero, and computes the offset to the
1754 first entry relative to the TOC base (r2). */
1755 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1756 0, /* rightshift */
1757 1, /* size (0 = byte, 1 = short, 2 = long) */
1758 16, /* bitsize */
b34976b6 1759 FALSE, /* pc_relative */
5bd4f169 1760 0, /* bitpos */
411e1bfb
AM
1761 complain_overflow_signed, /* complain_on_overflow */
1762 ppc64_elf_unhandled_reloc, /* special_function */
1763 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1764 FALSE, /* partial_inplace */
d006db6c 1765 0, /* src_mask */
411e1bfb 1766 0xffff, /* dst_mask */
b34976b6 1767 FALSE), /* pcrel_offset */
5bd4f169 1768
411e1bfb
AM
1769 /* Like GOT_TLSLD16, but no overflow. */
1770 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1771 0, /* rightshift */
1772 1, /* size (0 = byte, 1 = short, 2 = long) */
1773 16, /* bitsize */
b34976b6 1774 FALSE, /* pc_relative */
5bd4f169 1775 0, /* bitpos */
411e1bfb
AM
1776 complain_overflow_dont, /* complain_on_overflow */
1777 ppc64_elf_unhandled_reloc, /* special_function */
1778 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1779 FALSE, /* partial_inplace */
d006db6c 1780 0, /* src_mask */
411e1bfb 1781 0xffff, /* dst_mask */
b34976b6 1782 FALSE), /* pcrel_offset */
5bd4f169 1783
411e1bfb
AM
1784 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1785 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1786 16, /* rightshift */
5bd4f169
AM
1787 1, /* size (0 = byte, 1 = short, 2 = long) */
1788 16, /* bitsize */
b34976b6 1789 FALSE, /* pc_relative */
5bd4f169 1790 0, /* bitpos */
f9c6b907 1791 complain_overflow_signed, /* complain_on_overflow */
805fc799 1792 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1793 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1794 FALSE, /* partial_inplace */
d006db6c 1795 0, /* src_mask */
411e1bfb 1796 0xffff, /* dst_mask */
b34976b6 1797 FALSE), /* pcrel_offset */
5bd4f169 1798
411e1bfb
AM
1799 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1800 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1801 16, /* rightshift */
5bd4f169
AM
1802 1, /* size (0 = byte, 1 = short, 2 = long) */
1803 16, /* bitsize */
b34976b6 1804 FALSE, /* pc_relative */
5bd4f169 1805 0, /* bitpos */
f9c6b907 1806 complain_overflow_signed, /* complain_on_overflow */
805fc799 1807 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1808 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1809 FALSE, /* partial_inplace */
d006db6c 1810 0, /* src_mask */
411e1bfb 1811 0xffff, /* dst_mask */
b34976b6 1812 FALSE), /* pcrel_offset */
5bd4f169 1813
411e1bfb
AM
1814 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1815 the offset to the entry relative to the TOC base (r2). */
1816 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1817 0, /* rightshift */
1818 1, /* size (0 = byte, 1 = short, 2 = long) */
1819 16, /* bitsize */
b34976b6 1820 FALSE, /* pc_relative */
5bd4f169 1821 0, /* bitpos */
411e1bfb 1822 complain_overflow_signed, /* complain_on_overflow */
805fc799 1823 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1824 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1825 FALSE, /* partial_inplace */
d006db6c 1826 0, /* src_mask */
5bd4f169 1827 0xfffc, /* dst_mask */
b34976b6 1828 FALSE), /* pcrel_offset */
5bd4f169 1829
411e1bfb
AM
1830 /* Like GOT_DTPREL16_DS, but no overflow. */
1831 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1832 0, /* rightshift */
c061c2d8
AM
1833 1, /* size (0 = byte, 1 = short, 2 = long) */
1834 16, /* bitsize */
b34976b6 1835 FALSE, /* pc_relative */
5bd4f169 1836 0, /* bitpos */
411e1bfb
AM
1837 complain_overflow_dont, /* complain_on_overflow */
1838 ppc64_elf_unhandled_reloc, /* special_function */
1839 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1840 FALSE, /* partial_inplace */
d006db6c 1841 0, /* src_mask */
c061c2d8 1842 0xfffc, /* dst_mask */
b34976b6 1843 FALSE), /* pcrel_offset */
5bd4f169 1844
411e1bfb
AM
1845 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1846 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1847 16, /* rightshift */
5bd4f169
AM
1848 1, /* size (0 = byte, 1 = short, 2 = long) */
1849 16, /* bitsize */
b34976b6 1850 FALSE, /* pc_relative */
5bd4f169 1851 0, /* bitpos */
f9c6b907 1852 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1853 ppc64_elf_unhandled_reloc, /* special_function */
1854 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1855 FALSE, /* partial_inplace */
d006db6c 1856 0, /* src_mask */
411e1bfb 1857 0xffff, /* dst_mask */
b34976b6 1858 FALSE), /* pcrel_offset */
5bd4f169 1859
411e1bfb
AM
1860 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1861 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1862 16, /* rightshift */
1863 1, /* size (0 = byte, 1 = short, 2 = long) */
1864 16, /* bitsize */
1865 FALSE, /* pc_relative */
1866 0, /* bitpos */
f9c6b907 1867 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1868 ppc64_elf_unhandled_reloc, /* special_function */
1869 "R_PPC64_GOT_DTPREL16_HA", /* name */
1870 FALSE, /* partial_inplace */
1871 0, /* src_mask */
1872 0xffff, /* dst_mask */
1873 FALSE), /* pcrel_offset */
1874
1875 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1876 offset to the entry relative to the TOC base (r2). */
1877 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1878 0, /* rightshift */
1879 1, /* size (0 = byte, 1 = short, 2 = long) */
1880 16, /* bitsize */
b34976b6 1881 FALSE, /* pc_relative */
5bd4f169
AM
1882 0, /* bitpos */
1883 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1884 ppc64_elf_unhandled_reloc, /* special_function */
1885 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1886 FALSE, /* partial_inplace */
d006db6c 1887 0, /* src_mask */
ad8e1ba5 1888 0xfffc, /* dst_mask */
b34976b6 1889 FALSE), /* pcrel_offset */
5bd4f169 1890
411e1bfb
AM
1891 /* Like GOT_TPREL16_DS, but no overflow. */
1892 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1893 0, /* rightshift */
1894 1, /* size (0 = byte, 1 = short, 2 = long) */
1895 16, /* bitsize */
b34976b6 1896 FALSE, /* pc_relative */
5bd4f169
AM
1897 0, /* bitpos */
1898 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1899 ppc64_elf_unhandled_reloc, /* special_function */
1900 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1901 FALSE, /* partial_inplace */
d006db6c 1902 0, /* src_mask */
ad8e1ba5 1903 0xfffc, /* dst_mask */
b34976b6 1904 FALSE), /* pcrel_offset */
5bd4f169 1905
411e1bfb
AM
1906 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1907 HOWTO (R_PPC64_GOT_TPREL16_HI,
1908 16, /* rightshift */
5bd4f169
AM
1909 1, /* size (0 = byte, 1 = short, 2 = long) */
1910 16, /* bitsize */
b34976b6 1911 FALSE, /* pc_relative */
5bd4f169 1912 0, /* bitpos */
f9c6b907 1913 complain_overflow_signed, /* complain_on_overflow */
805fc799 1914 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1915 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1916 FALSE, /* partial_inplace */
d006db6c 1917 0, /* src_mask */
411e1bfb 1918 0xffff, /* dst_mask */
b34976b6 1919 FALSE), /* pcrel_offset */
5bd4f169 1920
411e1bfb
AM
1921 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1922 HOWTO (R_PPC64_GOT_TPREL16_HA,
1923 16, /* rightshift */
5bd4f169
AM
1924 1, /* size (0 = byte, 1 = short, 2 = long) */
1925 16, /* bitsize */
b34976b6 1926 FALSE, /* pc_relative */
5bd4f169 1927 0, /* bitpos */
f9c6b907 1928 complain_overflow_signed, /* complain_on_overflow */
805fc799 1929 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1930 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1931 FALSE, /* partial_inplace */
d006db6c 1932 0, /* src_mask */
411e1bfb 1933 0xffff, /* dst_mask */
b34976b6 1934 FALSE), /* pcrel_offset */
5bd4f169 1935
25f23106
AM
1936 HOWTO (R_PPC64_JMP_IREL, /* type */
1937 0, /* rightshift */
1938 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1939 0, /* bitsize */
1940 FALSE, /* pc_relative */
1941 0, /* bitpos */
1942 complain_overflow_dont, /* complain_on_overflow */
1943 ppc64_elf_unhandled_reloc, /* special_function */
1944 "R_PPC64_JMP_IREL", /* name */
1945 FALSE, /* partial_inplace */
1946 0, /* src_mask */
1947 0, /* dst_mask */
1948 FALSE), /* pcrel_offset */
1949
e054468f
AM
1950 HOWTO (R_PPC64_IRELATIVE, /* type */
1951 0, /* rightshift */
1952 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1953 64, /* bitsize */
1954 FALSE, /* pc_relative */
1955 0, /* bitpos */
1956 complain_overflow_dont, /* complain_on_overflow */
1957 bfd_elf_generic_reloc, /* special_function */
1958 "R_PPC64_IRELATIVE", /* name */
1959 FALSE, /* partial_inplace */
1960 0, /* src_mask */
1961 ONES (64), /* dst_mask */
1962 FALSE), /* pcrel_offset */
1963
25f23106
AM
1964 /* A 16 bit relative relocation. */
1965 HOWTO (R_PPC64_REL16, /* type */
1966 0, /* rightshift */
1967 1, /* size (0 = byte, 1 = short, 2 = long) */
1968 16, /* bitsize */
1969 TRUE, /* pc_relative */
1970 0, /* bitpos */
b80eed39 1971 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1972 bfd_elf_generic_reloc, /* special_function */
1973 "R_PPC64_REL16", /* name */
1974 FALSE, /* partial_inplace */
1975 0, /* src_mask */
1976 0xffff, /* dst_mask */
1977 TRUE), /* pcrel_offset */
1978
1979 /* A 16 bit relative relocation without overflow. */
1980 HOWTO (R_PPC64_REL16_LO, /* type */
1981 0, /* rightshift */
1982 1, /* size (0 = byte, 1 = short, 2 = long) */
1983 16, /* bitsize */
1984 TRUE, /* pc_relative */
1985 0, /* bitpos */
1986 complain_overflow_dont,/* complain_on_overflow */
1987 bfd_elf_generic_reloc, /* special_function */
1988 "R_PPC64_REL16_LO", /* name */
1989 FALSE, /* partial_inplace */
1990 0, /* src_mask */
1991 0xffff, /* dst_mask */
1992 TRUE), /* pcrel_offset */
1993
1994 /* The high order 16 bits of a relative address. */
1995 HOWTO (R_PPC64_REL16_HI, /* type */
1996 16, /* rightshift */
1997 1, /* size (0 = byte, 1 = short, 2 = long) */
1998 16, /* bitsize */
1999 TRUE, /* pc_relative */
2000 0, /* bitpos */
f9c6b907 2001 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2002 bfd_elf_generic_reloc, /* special_function */
2003 "R_PPC64_REL16_HI", /* name */
2004 FALSE, /* partial_inplace */
2005 0, /* src_mask */
2006 0xffff, /* dst_mask */
2007 TRUE), /* pcrel_offset */
2008
2009 /* The high order 16 bits of a relative address, plus 1 if the contents of
2010 the low 16 bits, treated as a signed number, is negative. */
2011 HOWTO (R_PPC64_REL16_HA, /* type */
2012 16, /* rightshift */
2013 1, /* size (0 = byte, 1 = short, 2 = long) */
2014 16, /* bitsize */
2015 TRUE, /* pc_relative */
2016 0, /* bitpos */
f9c6b907 2017 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2018 ppc64_elf_ha_reloc, /* special_function */
2019 "R_PPC64_REL16_HA", /* name */
2020 FALSE, /* partial_inplace */
2021 0, /* src_mask */
2022 0xffff, /* dst_mask */
2023 TRUE), /* pcrel_offset */
2024
a680de9a
PB
2025 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2026 HOWTO (R_PPC64_REL16DX_HA, /* type */
2027 16, /* rightshift */
2028 2, /* size (0 = byte, 1 = short, 2 = long) */
2029 16, /* bitsize */
2030 TRUE, /* pc_relative */
2031 0, /* bitpos */
2032 complain_overflow_signed, /* complain_on_overflow */
2033 ppc64_elf_ha_reloc, /* special_function */
2034 "R_PPC64_REL16DX_HA", /* name */
2035 FALSE, /* partial_inplace */
2036 0, /* src_mask */
2037 0x1fffc1, /* dst_mask */
2038 TRUE), /* pcrel_offset */
2039
f9c6b907
AM
2040 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2041 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2042 16, /* rightshift */
2043 1, /* size (0 = byte, 1 = short, 2 = long) */
2044 16, /* bitsize */
2045 FALSE, /* pc_relative */
2046 0, /* bitpos */
2047 complain_overflow_dont, /* complain_on_overflow */
2048 bfd_elf_generic_reloc, /* special_function */
2049 "R_PPC64_ADDR16_HIGH", /* name */
2050 FALSE, /* partial_inplace */
2051 0, /* src_mask */
2052 0xffff, /* dst_mask */
2053 FALSE), /* pcrel_offset */
2054
2055 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2056 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2057 16, /* rightshift */
2058 1, /* size (0 = byte, 1 = short, 2 = long) */
2059 16, /* bitsize */
2060 FALSE, /* pc_relative */
2061 0, /* bitpos */
2062 complain_overflow_dont, /* complain_on_overflow */
2063 ppc64_elf_ha_reloc, /* special_function */
2064 "R_PPC64_ADDR16_HIGHA", /* name */
2065 FALSE, /* partial_inplace */
2066 0, /* src_mask */
2067 0xffff, /* dst_mask */
2068 FALSE), /* pcrel_offset */
2069
2070 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2071 HOWTO (R_PPC64_DTPREL16_HIGH,
2072 16, /* rightshift */
2073 1, /* size (0 = byte, 1 = short, 2 = long) */
2074 16, /* bitsize */
2075 FALSE, /* pc_relative */
2076 0, /* bitpos */
2077 complain_overflow_dont, /* complain_on_overflow */
2078 ppc64_elf_unhandled_reloc, /* special_function */
2079 "R_PPC64_DTPREL16_HIGH", /* name */
2080 FALSE, /* partial_inplace */
2081 0, /* src_mask */
2082 0xffff, /* dst_mask */
2083 FALSE), /* pcrel_offset */
2084
2085 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2086 HOWTO (R_PPC64_DTPREL16_HIGHA,
2087 16, /* rightshift */
2088 1, /* size (0 = byte, 1 = short, 2 = long) */
2089 16, /* bitsize */
2090 FALSE, /* pc_relative */
2091 0, /* bitpos */
2092 complain_overflow_dont, /* complain_on_overflow */
2093 ppc64_elf_unhandled_reloc, /* special_function */
2094 "R_PPC64_DTPREL16_HIGHA", /* name */
2095 FALSE, /* partial_inplace */
2096 0, /* src_mask */
2097 0xffff, /* dst_mask */
2098 FALSE), /* pcrel_offset */
2099
2100 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2101 HOWTO (R_PPC64_TPREL16_HIGH,
2102 16, /* rightshift */
2103 1, /* size (0 = byte, 1 = short, 2 = long) */
2104 16, /* bitsize */
2105 FALSE, /* pc_relative */
2106 0, /* bitpos */
2107 complain_overflow_dont, /* complain_on_overflow */
2108 ppc64_elf_unhandled_reloc, /* special_function */
2109 "R_PPC64_TPREL16_HIGH", /* name */
2110 FALSE, /* partial_inplace */
2111 0, /* src_mask */
2112 0xffff, /* dst_mask */
2113 FALSE), /* pcrel_offset */
2114
2115 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2116 HOWTO (R_PPC64_TPREL16_HIGHA,
2117 16, /* rightshift */
2118 1, /* size (0 = byte, 1 = short, 2 = long) */
2119 16, /* bitsize */
2120 FALSE, /* pc_relative */
2121 0, /* bitpos */
2122 complain_overflow_dont, /* complain_on_overflow */
2123 ppc64_elf_unhandled_reloc, /* special_function */
2124 "R_PPC64_TPREL16_HIGHA", /* name */
2125 FALSE, /* partial_inplace */
2126 0, /* src_mask */
2127 0xffff, /* dst_mask */
2128 FALSE), /* pcrel_offset */
2129
45965137
AM
2130 /* Like ADDR64, but use local entry point of function. */
2131 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2132 0, /* rightshift */
2133 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2134 64, /* bitsize */
2135 FALSE, /* pc_relative */
2136 0, /* bitpos */
2137 complain_overflow_dont, /* complain_on_overflow */
2138 bfd_elf_generic_reloc, /* special_function */
2139 "R_PPC64_ADDR64_LOCAL", /* name */
2140 FALSE, /* partial_inplace */
2141 0, /* src_mask */
2142 ONES (64), /* dst_mask */
2143 FALSE), /* pcrel_offset */
2144
5bd4f169
AM
2145 /* GNU extension to record C++ vtable hierarchy. */
2146 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2147 0, /* rightshift */
2148 0, /* size (0 = byte, 1 = short, 2 = long) */
2149 0, /* bitsize */
b34976b6 2150 FALSE, /* pc_relative */
5bd4f169
AM
2151 0, /* bitpos */
2152 complain_overflow_dont, /* complain_on_overflow */
2153 NULL, /* special_function */
2154 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2155 FALSE, /* partial_inplace */
5bd4f169
AM
2156 0, /* src_mask */
2157 0, /* dst_mask */
b34976b6 2158 FALSE), /* pcrel_offset */
5bd4f169
AM
2159
2160 /* GNU extension to record C++ vtable member usage. */
2161 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2162 0, /* rightshift */
2163 0, /* size (0 = byte, 1 = short, 2 = long) */
2164 0, /* bitsize */
b34976b6 2165 FALSE, /* pc_relative */
5bd4f169
AM
2166 0, /* bitpos */
2167 complain_overflow_dont, /* complain_on_overflow */
2168 NULL, /* special_function */
2169 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2170 FALSE, /* partial_inplace */
5bd4f169
AM
2171 0, /* src_mask */
2172 0, /* dst_mask */
b34976b6 2173 FALSE), /* pcrel_offset */
5bd4f169
AM
2174};
2175
2176\f
2177/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2178 be done. */
2179
2180static void
4ce794b7 2181ppc_howto_init (void)
5bd4f169
AM
2182{
2183 unsigned int i, type;
2184
a4b6fadd 2185 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2186 {
2187 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2188 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2189 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2190 }
2191}
2192
2193static reloc_howto_type *
4ce794b7
AM
2194ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2195 bfd_reloc_code_real_type code)
5bd4f169 2196{
411e1bfb 2197 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2198
2199 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2200 /* Initialize howto table if needed. */
2201 ppc_howto_init ();
2202
4ce794b7 2203 switch (code)
5bd4f169
AM
2204 {
2205 default:
4ce794b7 2206 return NULL;
5bd4f169 2207
411e1bfb
AM
2208 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2209 break;
2210 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2211 break;
2212 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2213 break;
2214 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2215 break;
2216 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2217 break;
2218 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2219 break;
f9c6b907
AM
2220 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2221 break;
411e1bfb 2222 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2223 break;
f9c6b907
AM
2224 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2225 break;
411e1bfb 2226 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2227 break;
411e1bfb 2228 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2229 break;
411e1bfb 2230 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2231 break;
411e1bfb 2232 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2233 break;
411e1bfb 2234 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2235 break;
411e1bfb 2236 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2237 break;
411e1bfb 2238 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2239 break;
411e1bfb 2240 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2241 break;
411e1bfb 2242 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2243 break;
411e1bfb 2244 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2245 break;
411e1bfb 2246 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2247 break;
411e1bfb 2248 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2249 break;
411e1bfb 2250 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2251 break;
411e1bfb 2252 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2253 break;
411e1bfb 2254 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2255 break;
411e1bfb 2256 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2257 break;
411e1bfb 2258 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2259 break;
411e1bfb 2260 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2261 break;
411e1bfb 2262 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2263 break;
411e1bfb 2264 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2265 break;
411e1bfb 2266 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2267 break;
411e1bfb 2268 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2269 break;
411e1bfb 2270 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2271 break;
411e1bfb 2272 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2273 break;
411e1bfb 2274 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2275 break;
411e1bfb 2276 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2277 break;
411e1bfb 2278 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2279 break;
411e1bfb 2280 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2281 break;
411e1bfb 2282 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2283 break;
411e1bfb 2284 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2285 break;
411e1bfb 2286 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2287 break;
411e1bfb 2288 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2289 break;
411e1bfb 2290 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2291 break;
411e1bfb 2292 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2293 break;
411e1bfb 2294 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2295 break;
411e1bfb 2296 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2297 break;
411e1bfb 2298 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2299 break;
411e1bfb 2300 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2301 break;
411e1bfb 2302 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2303 break;
411e1bfb 2304 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2305 break;
411e1bfb 2306 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2307 break;
411e1bfb 2308 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2309 break;
411e1bfb 2310 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2311 break;
411e1bfb 2312 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2313 break;
411e1bfb 2314 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2315 break;
411e1bfb 2316 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2317 break;
411e1bfb 2318 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2319 break;
411e1bfb 2320 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2321 break;
411e1bfb 2322 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2323 break;
411e1bfb 2324 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2325 break;
411e1bfb 2326 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2327 break;
411e1bfb 2328 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2329 break;
411e1bfb 2330 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2331 break;
727fc41e
AM
2332 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2333 break;
2334 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2335 break;
411e1bfb 2336 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2337 break;
411e1bfb 2338 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2339 break;
411e1bfb 2340 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2341 break;
411e1bfb 2342 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2343 break;
f9c6b907
AM
2344 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2345 break;
411e1bfb 2346 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2347 break;
f9c6b907
AM
2348 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2349 break;
411e1bfb 2350 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2351 break;
411e1bfb
AM
2352 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2353 break;
2354 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2355 break;
2356 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2357 break;
f9c6b907
AM
2358 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2359 break;
411e1bfb
AM
2360 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2361 break;
f9c6b907
AM
2362 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2363 break;
411e1bfb
AM
2364 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2365 break;
2366 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2367 break;
2368 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2369 break;
2370 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2371 break;
2372 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2373 break;
2374 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2375 break;
2376 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2377 break;
2378 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2379 break;
2380 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2381 break;
2382 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2383 break;
2384 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2385 break;
2386 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2387 break;
2388 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2389 break;
2390 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2391 break;
2392 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2393 break;
2394 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2395 break;
2396 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2397 break;
2398 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2399 break;
2400 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2401 break;
2402 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2403 break;
2404 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2405 break;
2406 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2407 break;
2408 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2409 break;
2410 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2411 break;
2412 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2413 break;
2414 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2415 break;
2416 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2417 break;
2418 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2419 break;
2420 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2421 break;
25f23106
AM
2422 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2423 break;
2424 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2425 break;
2426 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2427 break;
2428 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2429 break;
a680de9a
PB
2430 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2431 break;
45965137
AM
2432 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2433 break;
411e1bfb
AM
2434 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2435 break;
2436 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2437 break;
2438 }
2439
4ce794b7 2440 return ppc64_elf_howto_table[r];
5bd4f169
AM
2441};
2442
157090f7
AM
2443static reloc_howto_type *
2444ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2445 const char *r_name)
2446{
2447 unsigned int i;
2448
a4b6fadd 2449 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2450 if (ppc64_elf_howto_raw[i].name != NULL
2451 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2452 return &ppc64_elf_howto_raw[i];
2453
2454 return NULL;
2455}
2456
5bd4f169
AM
2457/* Set the howto pointer for a PowerPC ELF reloc. */
2458
2459static void
4ce794b7
AM
2460ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2461 Elf_Internal_Rela *dst)
5bd4f169 2462{
65f38f15
AM
2463 unsigned int type;
2464
ef60b7ff 2465 /* Initialize howto table if needed. */
5bd4f169 2466 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2467 ppc_howto_init ();
2468
65f38f15 2469 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2470 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d
JJ
2471 {
2472 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2473 abfd, (int) type);
2474 type = R_PPC64_NONE;
d0fb9a8d 2475 }
65f38f15 2476 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2477}
2478
04c9666a 2479/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2480
2481static bfd_reloc_status_type
4ce794b7
AM
2482ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2483 void *data, asection *input_section,
2484 bfd *output_bfd, char **error_message)
5bd4f169 2485{
a680de9a
PB
2486 enum elf_ppc64_reloc_type r_type;
2487 long insn;
2488 bfd_size_type octets;
2489 bfd_vma value;
2490
805fc799
AM
2491 /* If this is a relocatable link (output_bfd test tells us), just
2492 call the generic function. Any adjustment will be done at final
2493 link time. */
2494 if (output_bfd != NULL)
cedb70c5 2495 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2496 input_section, output_bfd, error_message);
2497
2498 /* Adjust the addend for sign extension of the low 16 bits.
2499 We won't actually be using the low 16 bits, so trashing them
2500 doesn't matter. */
2501 reloc_entry->addend += 0x8000;
a680de9a
PB
2502 r_type = reloc_entry->howto->type;
2503 if (r_type != R_PPC64_REL16DX_HA)
2504 return bfd_reloc_continue;
2505
2506 value = 0;
2507 if (!bfd_is_com_section (symbol->section))
2508 value = symbol->value;
2509 value += (reloc_entry->addend
2510 + symbol->section->output_offset
2511 + symbol->section->output_section->vma);
2512 value -= (reloc_entry->address
2513 + input_section->output_offset
2514 + input_section->output_section->vma);
2515 value = (bfd_signed_vma) value >> 16;
2516
2517 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2518 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2519 insn &= ~0x1fffc1;
2520 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2521 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2522 if (value + 0x8000 > 0xffff)
2523 return bfd_reloc_overflow;
2524 return bfd_reloc_ok;
805fc799 2525}
5bd4f169 2526
2441e016
AM
2527static bfd_reloc_status_type
2528ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2529 void *data, asection *input_section,
2530 bfd *output_bfd, char **error_message)
2531{
2532 if (output_bfd != NULL)
2533 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2534 input_section, output_bfd, error_message);
2535
699733f6
AM
2536 if (strcmp (symbol->section->name, ".opd") == 0
2537 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2538 {
2539 bfd_vma dest = opd_entry_value (symbol->section,
2540 symbol->value + reloc_entry->addend,
aef36ac1 2541 NULL, NULL, FALSE);
2441e016
AM
2542 if (dest != (bfd_vma) -1)
2543 reloc_entry->addend = dest - (symbol->value
2544 + symbol->section->output_section->vma
2545 + symbol->section->output_offset);
2546 }
810d4e75
AM
2547 else
2548 {
2549 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2550
2551 if (symbol->section->owner != abfd
2552 && abiversion (symbol->section->owner) >= 2)
2553 {
2554 unsigned int i;
2555
2556 for (i = 0; i < symbol->section->owner->symcount; ++i)
2557 {
2558 asymbol *symdef = symbol->section->owner->outsymbols[i];
2559
2560 if (strcmp (symdef->name, symbol->name) == 0)
2561 {
2562 elfsym = (elf_symbol_type *) symdef;
2563 break;
2564 }
2565 }
2566 }
2567 reloc_entry->addend
2568 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2569 }
2441e016
AM
2570 return bfd_reloc_continue;
2571}
2572
805fc799 2573static bfd_reloc_status_type
4ce794b7
AM
2574ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2575 void *data, asection *input_section,
2576 bfd *output_bfd, char **error_message)
805fc799
AM
2577{
2578 long insn;
04c9666a 2579 enum elf_ppc64_reloc_type r_type;
805fc799 2580 bfd_size_type octets;
794e51c0
AM
2581 /* Assume 'at' branch hints. */
2582 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2583
2584 /* If this is a relocatable link (output_bfd test tells us), just
2585 call the generic function. Any adjustment will be done at final
2586 link time. */
5bd4f169 2587 if (output_bfd != NULL)
cedb70c5 2588 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2589 input_section, output_bfd, error_message);
2590
2591 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2592 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2593 insn &= ~(0x01 << 21);
4ce794b7 2594 r_type = reloc_entry->howto->type;
805fc799
AM
2595 if (r_type == R_PPC64_ADDR14_BRTAKEN
2596 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2597 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2598
794e51c0 2599 if (is_isa_v2)
5bd4f169 2600 {
805fc799
AM
2601 /* Set 'a' bit. This is 0b00010 in BO field for branch
2602 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2603 for branch on CTR insns (BO == 1a00t or 1a01t). */
2604 if ((insn & (0x14 << 21)) == (0x04 << 21))
2605 insn |= 0x02 << 21;
2606 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2607 insn |= 0x08 << 21;
2608 else
2441e016 2609 goto out;
5bd4f169 2610 }
805fc799
AM
2611 else
2612 {
2613 bfd_vma target = 0;
2614 bfd_vma from;
5bd4f169 2615
805fc799
AM
2616 if (!bfd_is_com_section (symbol->section))
2617 target = symbol->value;
2618 target += symbol->section->output_section->vma;
2619 target += symbol->section->output_offset;
2620 target += reloc_entry->addend;
5bd4f169 2621
805fc799
AM
2622 from = (reloc_entry->address
2623 + input_section->output_offset
2624 + input_section->output_section->vma);
5bd4f169 2625
805fc799
AM
2626 /* Invert 'y' bit if not the default. */
2627 if ((bfd_signed_vma) (target - from) < 0)
2628 insn ^= 0x01 << 21;
2629 }
4ce794b7 2630 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2631 out:
2632 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2633 input_section, output_bfd, error_message);
805fc799 2634}
5bd4f169 2635
805fc799 2636static bfd_reloc_status_type
4ce794b7
AM
2637ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2638 void *data, asection *input_section,
2639 bfd *output_bfd, char **error_message)
805fc799
AM
2640{
2641 /* If this is a relocatable link (output_bfd test tells us), just
2642 call the generic function. Any adjustment will be done at final
2643 link time. */
2644 if (output_bfd != NULL)
cedb70c5 2645 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2646 input_section, output_bfd, error_message);
5bd4f169 2647
805fc799
AM
2648 /* Subtract the symbol section base address. */
2649 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2650 return bfd_reloc_continue;
2651}
2652
805fc799 2653static bfd_reloc_status_type
4ce794b7
AM
2654ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2655 void *data, asection *input_section,
2656 bfd *output_bfd, char **error_message)
805fc799
AM
2657{
2658 /* If this is a relocatable link (output_bfd test tells us), just
2659 call the generic function. Any adjustment will be done at final
2660 link time. */
2661 if (output_bfd != NULL)
cedb70c5 2662 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2663 input_section, output_bfd, error_message);
2664
2665 /* Subtract the symbol section base address. */
2666 reloc_entry->addend -= symbol->section->output_section->vma;
2667
2668 /* Adjust the addend for sign extension of the low 16 bits. */
2669 reloc_entry->addend += 0x8000;
2670 return bfd_reloc_continue;
2671}
2672
2673static bfd_reloc_status_type
4ce794b7
AM
2674ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2675 void *data, asection *input_section,
2676 bfd *output_bfd, char **error_message)
805fc799
AM
2677{
2678 bfd_vma TOCstart;
2679
2680 /* If this is a relocatable link (output_bfd test tells us), just
2681 call the generic function. Any adjustment will be done at final
2682 link time. */
2683 if (output_bfd != NULL)
cedb70c5 2684 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2685 input_section, output_bfd, error_message);
2686
2687 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2688 if (TOCstart == 0)
1c865ab2 2689 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2690
2691 /* Subtract the TOC base address. */
2692 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2693 return bfd_reloc_continue;
2694}
2695
2696static bfd_reloc_status_type
4ce794b7
AM
2697ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2698 void *data, asection *input_section,
2699 bfd *output_bfd, char **error_message)
805fc799
AM
2700{
2701 bfd_vma TOCstart;
2702
2703 /* If this is a relocatable link (output_bfd test tells us), just
2704 call the generic function. Any adjustment will be done at final
2705 link time. */
2706 if (output_bfd != NULL)
cedb70c5 2707 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2708 input_section, output_bfd, error_message);
2709
2710 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2711 if (TOCstart == 0)
1c865ab2 2712 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2713
2714 /* Subtract the TOC base address. */
2715 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2716
2717 /* Adjust the addend for sign extension of the low 16 bits. */
2718 reloc_entry->addend += 0x8000;
2719 return bfd_reloc_continue;
2720}
2721
2722static bfd_reloc_status_type
4ce794b7
AM
2723ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2724 void *data, asection *input_section,
2725 bfd *output_bfd, char **error_message)
805fc799
AM
2726{
2727 bfd_vma TOCstart;
2728 bfd_size_type octets;
2729
2730 /* If this is a relocatable link (output_bfd test tells us), just
2731 call the generic function. Any adjustment will be done at final
2732 link time. */
2733 if (output_bfd != NULL)
cedb70c5 2734 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2735 input_section, output_bfd, error_message);
2736
2737 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2738 if (TOCstart == 0)
1c865ab2 2739 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2740
2741 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2742 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2743 return bfd_reloc_ok;
2744}
2745
2746static bfd_reloc_status_type
4ce794b7
AM
2747ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2748 void *data, asection *input_section,
2749 bfd *output_bfd, char **error_message)
805fc799
AM
2750{
2751 /* If this is a relocatable link (output_bfd test tells us), just
2752 call the generic function. Any adjustment will be done at final
2753 link time. */
2754 if (output_bfd != NULL)
cedb70c5 2755 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2756 input_section, output_bfd, error_message);
2757
2758 if (error_message != NULL)
2759 {
2760 static char buf[60];
2761 sprintf (buf, "generic linker can't handle %s",
2762 reloc_entry->howto->name);
2763 *error_message = buf;
2764 }
2765 return bfd_reloc_dangerous;
2766}
2767
927be08e
AM
2768/* Track GOT entries needed for a given symbol. We might need more
2769 than one got entry per symbol. */
2770struct got_entry
2771{
2772 struct got_entry *next;
2773
2774 /* The symbol addend that we'll be placing in the GOT. */
2775 bfd_vma addend;
2776
2777 /* Unlike other ELF targets, we use separate GOT entries for the same
2778 symbol referenced from different input files. This is to support
2779 automatic multiple TOC/GOT sections, where the TOC base can vary
2780 from one input file to another. After partitioning into TOC groups
2781 we merge entries within the group.
2782
2783 Point to the BFD owning this GOT entry. */
2784 bfd *owner;
2785
2786 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2787 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2788 unsigned char tls_type;
927be08e
AM
2789
2790 /* Non-zero if got.ent points to real entry. */
f961d9dd 2791 unsigned char is_indirect;
927be08e
AM
2792
2793 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2794 union
2795 {
2796 bfd_signed_vma refcount;
2797 bfd_vma offset;
2798 struct got_entry *ent;
2799 } got;
2800};
2801
2802/* The same for PLT. */
2803struct plt_entry
2804{
2805 struct plt_entry *next;
2806
2807 bfd_vma addend;
2808
2809 union
2810 {
2811 bfd_signed_vma refcount;
2812 bfd_vma offset;
2813 } plt;
2814};
2815
e717da7e
AM
2816struct ppc64_elf_obj_tdata
2817{
2818 struct elf_obj_tdata elf;
2819
2820 /* Shortcuts to dynamic linker sections. */
2821 asection *got;
2822 asection *relgot;
2823
b3fac117
AM
2824 /* Used during garbage collection. We attach global symbols defined
2825 on removed .opd entries to this section so that the sym is removed. */
2826 asection *deleted_section;
81688140 2827
927be08e 2828 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2829 sections means we potentially need one of these for each input bfd. */
927be08e 2830 struct got_entry tlsld_got;
8860955f 2831
729eabd5
AM
2832 union {
2833 /* A copy of relocs before they are modified for --emit-relocs. */
2834 Elf_Internal_Rela *relocs;
2835
2836 /* Section contents. */
2837 bfd_byte *contents;
2838 } opd;
d77c8a4b
AM
2839
2840 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2841 the reloc to be in the range -32768 to 32767. */
98528052
AM
2842 unsigned int has_small_toc_reloc : 1;
2843
560c8763
AM
2844 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2845 instruction not one we handle. */
2846 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2847};
2848
2849#define ppc64_elf_tdata(bfd) \
2850 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2851
2852#define ppc64_tlsld_got(bfd) \
2853 (&ppc64_elf_tdata (bfd)->tlsld_got)
2854
0c8d6e5c
AM
2855#define is_ppc64_elf(bfd) \
2856 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2857 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2858
e717da7e
AM
2859/* Override the generic function because we store some extras. */
2860
2861static bfd_boolean
2862ppc64_elf_mkobject (bfd *abfd)
2863{
0ffa91dd 2864 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2865 PPC64_ELF_DATA);
e717da7e
AM
2866}
2867
feee612b
AM
2868/* Fix bad default arch selected for a 64 bit input bfd when the
2869 default is 32 bit. */
2870
b34976b6 2871static bfd_boolean
4ce794b7 2872ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2873{
2874 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2875 {
2876 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2877
2878 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2879 {
2880 /* Relies on arch after 32 bit default being 64 bit default. */
2881 abfd->arch_info = abfd->arch_info->next;
2882 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2883 }
2884 }
b34976b6 2885 return TRUE;
feee612b
AM
2886}
2887
d37c89e5
AM
2888/* Support for core dump NOTE sections. */
2889
2890static bfd_boolean
2891ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2892{
eea6121a 2893 size_t offset, size;
d37c89e5
AM
2894
2895 if (note->descsz != 504)
2896 return FALSE;
2897
2898 /* pr_cursig */
228e534f 2899 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2900
2901 /* pr_pid */
228e534f 2902 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2903
2904 /* pr_reg */
2905 offset = 112;
eea6121a 2906 size = 384;
d37c89e5
AM
2907
2908 /* Make a ".reg/999" section. */
2909 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2910 size, note->descpos + offset);
d37c89e5
AM
2911}
2912
2913static bfd_boolean
2914ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2915{
2916 if (note->descsz != 136)
2917 return FALSE;
2918
228e534f 2919 elf_tdata (abfd)->core->pid
bc989cdc 2920 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2921 elf_tdata (abfd)->core->program
d37c89e5 2922 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2923 elf_tdata (abfd)->core->command
d37c89e5
AM
2924 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2925
2926 return TRUE;
2927}
2928
183e98be
AM
2929static char *
2930ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2931 ...)
2932{
2933 switch (note_type)
2934 {
2935 default:
2936 return NULL;
2937
2938 case NT_PRPSINFO:
2939 {
2940 char data[136];
2941 va_list ap;
2942
2943 va_start (ap, note_type);
75cd47ed 2944 memset (data, 0, sizeof (data));
183e98be
AM
2945 strncpy (data + 40, va_arg (ap, const char *), 16);
2946 strncpy (data + 56, va_arg (ap, const char *), 80);
2947 va_end (ap);
2948 return elfcore_write_note (abfd, buf, bufsiz,
2949 "CORE", note_type, data, sizeof (data));
2950 }
2951
2952 case NT_PRSTATUS:
2953 {
2954 char data[504];
2955 va_list ap;
2956 long pid;
2957 int cursig;
2958 const void *greg;
2959
2960 va_start (ap, note_type);
2961 memset (data, 0, 112);
2962 pid = va_arg (ap, long);
2963 bfd_put_32 (abfd, pid, data + 32);
2964 cursig = va_arg (ap, int);
2965 bfd_put_16 (abfd, cursig, data + 12);
2966 greg = va_arg (ap, const void *);
2967 memcpy (data + 112, greg, 384);
2968 memset (data + 496, 0, 8);
2969 va_end (ap);
2970 return elfcore_write_note (abfd, buf, bufsiz,
2971 "CORE", note_type, data, sizeof (data));
2972 }
2973 }
2974}
2975
5d35169e
AM
2976/* Add extra PPC sections. */
2977
b35d266b 2978static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2979{
0112cd26
NC
2980 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2981 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2982 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2983 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2984 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2985 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2986 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2987};
2988
7c8fe5c4
AM
2989enum _ppc64_sec_type {
2990 sec_normal = 0,
2991 sec_opd = 1,
2992 sec_toc = 2
2993};
2994
f0abc2a1
AM
2995struct _ppc64_elf_section_data
2996{
2997 struct bfd_elf_section_data elf;
411e1bfb 2998
f0abc2a1
AM
2999 union
3000 {
51aecdc5
AM
3001 /* An array with one entry for each opd function descriptor,
3002 and some spares since opd entries may be either 16 or 24 bytes. */
3003#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3004 struct _opd_sec_data
3005 {
3006 /* Points to the function code section for local opd entries. */
3007 asection **func_sec;
3008
3009 /* After editing .opd, adjust references to opd local syms. */
3010 long *adjust;
3011 } opd;
7c8fe5c4 3012
3a71aa26
AM
3013 /* An array for toc sections, indexed by offset/8. */
3014 struct _toc_sec_data
3015 {
3016 /* Specifies the relocation symbol index used at a given toc offset. */
3017 unsigned *symndx;
3018
3019 /* And the relocation addend. */
3020 bfd_vma *add;
3021 } toc;
7c8fe5c4
AM
3022 } u;
3023
3024 enum _ppc64_sec_type sec_type:2;
411e1bfb 3025
7c8fe5c4
AM
3026 /* Flag set when small branches are detected. Used to
3027 select suitable defaults for the stub group size. */
3028 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3029};
3030
3031#define ppc64_elf_section_data(sec) \
411e1bfb 3032 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3033
3034static bfd_boolean
4ce794b7 3035ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3036{
f592407e
AM
3037 if (!sec->used_by_bfd)
3038 {
3039 struct _ppc64_elf_section_data *sdata;
3040 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3041
f592407e
AM
3042 sdata = bfd_zalloc (abfd, amt);
3043 if (sdata == NULL)
3044 return FALSE;
3045 sec->used_by_bfd = sdata;
3046 }
f0abc2a1
AM
3047
3048 return _bfd_elf_new_section_hook (abfd, sec);
3049}
4025353c 3050
74f0fb50 3051static struct _opd_sec_data *
4025353c
AM
3052get_opd_info (asection * sec)
3053{
3054 if (sec != NULL
3055 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3056 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3057 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3058 return NULL;
3059}
90e3cdf2
JJ
3060\f
3061/* Parameters for the qsort hook. */
90e3cdf2
JJ
3062static bfd_boolean synthetic_relocatable;
3063
699733f6 3064/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3065
3066static int
3067compare_symbols (const void *ap, const void *bp)
3068{
3069 const asymbol *a = * (const asymbol **) ap;
3070 const asymbol *b = * (const asymbol **) bp;
3071
699733f6
AM
3072 /* Section symbols first. */
3073 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3074 return -1;
699733f6 3075 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3076 return 1;
3077
699733f6 3078 /* then .opd symbols. */
ffcfec52
AM
3079 if (strcmp (a->section->name, ".opd") == 0
3080 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 3081 return -1;
ffcfec52
AM
3082 if (strcmp (a->section->name, ".opd") != 0
3083 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
3084 return 1;
3085
699733f6 3086 /* then other code symbols. */
90e3cdf2
JJ
3087 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3088 == (SEC_CODE | SEC_ALLOC)
3089 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3090 != (SEC_CODE | SEC_ALLOC))
3091 return -1;
3092
3093 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3094 != (SEC_CODE | SEC_ALLOC)
3095 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3096 == (SEC_CODE | SEC_ALLOC))
3097 return 1;
3098
3099 if (synthetic_relocatable)
3100 {
3101 if (a->section->id < b->section->id)
3102 return -1;
3103
3104 if (a->section->id > b->section->id)
3105 return 1;
3106 }
3107
3108 if (a->value + a->section->vma < b->value + b->section->vma)
3109 return -1;
3110
3111 if (a->value + a->section->vma > b->value + b->section->vma)
3112 return 1;
3113
4d35a0aa
AM
3114 /* For syms with the same value, prefer strong dynamic global function
3115 syms over other syms. */
3116 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3117 return -1;
3118
3119 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3120 return 1;
3121
3122 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3123 return -1;
3124
3125 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3126 return 1;
3127
3128 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3129 return -1;
3130
3131 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3132 return 1;
3133
3134 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3135 return -1;
3136
3137 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3138 return 1;
3139
90e3cdf2
JJ
3140 return 0;
3141}
3142
699733f6 3143/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3144
699733f6 3145static asymbol *
7292b3ac 3146sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3147{
699733f6 3148 long mid;
90e3cdf2 3149
7292b3ac 3150 if (id == (unsigned) -1)
699733f6
AM
3151 {
3152 while (lo < hi)
3153 {
3154 mid = (lo + hi) >> 1;
3155 if (syms[mid]->value + syms[mid]->section->vma < value)
3156 lo = mid + 1;
3157 else if (syms[mid]->value + syms[mid]->section->vma > value)
3158 hi = mid;
3159 else
3160 return syms[mid];
3161 }
3162 }
3163 else
3164 {
3165 while (lo < hi)
3166 {
3167 mid = (lo + hi) >> 1;
3168 if (syms[mid]->section->id < id)
3169 lo = mid + 1;
3170 else if (syms[mid]->section->id > id)
3171 hi = mid;
3172 else if (syms[mid]->value < value)
3173 lo = mid + 1;
3174 else if (syms[mid]->value > value)
3175 hi = mid;
3176 else
3177 return syms[mid];
3178 }
3179 }
3180 return NULL;
90e3cdf2
JJ
3181}
3182
468392fb
AM
3183static bfd_boolean
3184section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3185{
3186 bfd_vma vma = *(bfd_vma *) ptr;
3187 return ((section->flags & SEC_ALLOC) != 0
3188 && section->vma <= vma
3189 && vma < section->vma + section->size);
3190}
3191
699733f6 3192/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3193 entry syms. Also generate @plt symbols for the glink branch table.
3194 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3195
3196static long
a7535cf3
AM
3197ppc64_elf_get_synthetic_symtab (bfd *abfd,
3198 long static_count, asymbol **static_syms,
3199 long dyn_count, asymbol **dyn_syms,
c9727e01 3200 asymbol **ret)
90e3cdf2
JJ
3201{
3202 asymbol *s;
699733f6
AM
3203 long i;
3204 long count;
90e3cdf2 3205 char *names;
a7535cf3 3206 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3207 asection *opd = NULL;
90e3cdf2 3208 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3209 asymbol **syms;
ee67d69a 3210 int abi = abiversion (abfd);
90e3cdf2
JJ
3211
3212 *ret = NULL;
3213
ee67d69a
AM
3214 if (abi < 2)
3215 {
3216 opd = bfd_get_section_by_name (abfd, ".opd");
3217 if (opd == NULL && abi == 1)
3218 return 0;
3219 }
90e3cdf2 3220
a7535cf3 3221 symcount = static_count;
c9727e01 3222 if (!relocatable)
a7535cf3 3223 symcount += dyn_count;
90e3cdf2 3224 if (symcount == 0)
c9727e01 3225 return 0;
90e3cdf2 3226
a7535cf3
AM
3227 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3228 if (syms == NULL)
7356fed5 3229 return -1;
a7535cf3
AM
3230
3231 if (!relocatable && static_count != 0 && dyn_count != 0)
3232 {
3233 /* Use both symbol tables. */
3234 memcpy (syms, static_syms, static_count * sizeof (*syms));
3235 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3236 }
3237 else if (!relocatable && static_count == 0)
3238 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3239 else
3240 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3241
90e3cdf2 3242 synthetic_relocatable = relocatable;
595da8c5 3243 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3244
c9727e01
AM
3245 if (!relocatable && symcount > 1)
3246 {
3247 long j;
3248 /* Trim duplicate syms, since we may have merged the normal and
3249 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3250 different values, so trim any with the same value. */
c9727e01
AM
3251 for (i = 1, j = 1; i < symcount; ++i)
3252 if (syms[i - 1]->value + syms[i - 1]->section->vma
3253 != syms[i]->value + syms[i]->section->vma)
3254 syms[j++] = syms[i];
3255 symcount = j;
3256 }
3257
699733f6 3258 i = 0;
ffcfec52 3259 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3260 ++i;
3261 codesecsym = i;
90e3cdf2 3262
699733f6
AM
3263 for (; i < symcount; ++i)
3264 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3265 != (SEC_CODE | SEC_ALLOC))
3266 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3267 break;
3268 codesecsymend = i;
90e3cdf2 3269
699733f6
AM
3270 for (; i < symcount; ++i)
3271 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3272 break;
3273 secsymend = i;
90e3cdf2 3274
699733f6 3275 for (; i < symcount; ++i)
ffcfec52 3276 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3277 break;
3278 opdsymend = i;
90e3cdf2 3279
699733f6
AM
3280 for (; i < symcount; ++i)
3281 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3282 != (SEC_CODE | SEC_ALLOC))
3283 break;
3284 symcount = i;
3285
c9727e01 3286 count = 0;
90e3cdf2 3287
699733f6 3288 if (relocatable)
90e3cdf2 3289 {
699733f6
AM
3290 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3291 arelent *r;
3292 size_t size;
3293 long relcount;
90e3cdf2 3294
468392fb
AM
3295 if (opdsymend == secsymend)
3296 goto done;
3297
699733f6 3298 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3299 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3300 if (relcount == 0)
c9727e01 3301 goto done;
90e3cdf2 3302
7356fed5
AM
3303 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3304 {
3305 count = -1;
3306 goto done;
3307 }
3308
699733f6 3309 size = 0;
595da8c5 3310 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3311 {
3312 asymbol *sym;
90e3cdf2 3313
595da8c5 3314 while (r < opd->relocation + relcount
699733f6
AM
3315 && r->address < syms[i]->value + opd->vma)
3316 ++r;
90e3cdf2 3317
595da8c5 3318 if (r == opd->relocation + relcount)
699733f6 3319 break;
90e3cdf2 3320
699733f6
AM
3321 if (r->address != syms[i]->value + opd->vma)
3322 continue;
90e3cdf2 3323
699733f6
AM
3324 if (r->howto->type != R_PPC64_ADDR64)
3325 continue;
90e3cdf2 3326
699733f6
AM
3327 sym = *r->sym_ptr_ptr;
3328 if (!sym_exists_at (syms, opdsymend, symcount,
3329 sym->section->id, sym->value + r->addend))
3330 {
3331 ++count;
3332 size += sizeof (asymbol);
3333 size += strlen (syms[i]->name) + 2;
3334 }
3335 }
90e3cdf2 3336
c4b0b099
AM
3337 if (size == 0)
3338 goto done;
699733f6
AM
3339 s = *ret = bfd_malloc (size);
3340 if (s == NULL)
3341 {
7356fed5 3342 count = -1;
c9727e01 3343 goto done;
699733f6 3344 }
90e3cdf2 3345
699733f6 3346 names = (char *) (s + count);
90e3cdf2 3347
595da8c5 3348 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3349 {
699733f6 3350 asymbol *sym;
90e3cdf2 3351
595da8c5 3352 while (r < opd->relocation + relcount
699733f6
AM
3353 && r->address < syms[i]->value + opd->vma)
3354 ++r;
90e3cdf2 3355
595da8c5 3356 if (r == opd->relocation + relcount)
699733f6
AM
3357 break;
3358
3359 if (r->address != syms[i]->value + opd->vma)
3360 continue;
3361
3362 if (r->howto->type != R_PPC64_ADDR64)
3363 continue;
90e3cdf2 3364
699733f6
AM
3365 sym = *r->sym_ptr_ptr;
3366 if (!sym_exists_at (syms, opdsymend, symcount,
3367 sym->section->id, sym->value + r->addend))
3368 {
3369 size_t len;
3370
3371 *s = *syms[i];
6ba2a415 3372 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3373 s->section = sym->section;
3374 s->value = sym->value + r->addend;
3375 s->name = names;
3376 *names++ = '.';
3377 len = strlen (syms[i]->name);
3378 memcpy (names, syms[i]->name, len + 1);
3379 names += len + 1;
6f610d07
UW
3380 /* Have udata.p point back to the original symbol this
3381 synthetic symbol was derived from. */
3382 s->udata.p = syms[i];
699733f6
AM
3383 s++;
3384 }
3385 }
3386 }
3387 else
90e3cdf2 3388 {
468392fb 3389 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3390 bfd_byte *contents = NULL;
699733f6 3391 size_t size;
468392fb
AM
3392 long plt_count = 0;
3393 bfd_vma glink_vma = 0, resolv_vma = 0;
3394 asection *dynamic, *glink = NULL, *relplt = NULL;
3395 arelent *p;
90e3cdf2 3396
ee67d69a 3397 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3398 {
c4b0b099
AM
3399 free_contents_and_exit_err:
3400 count = -1;
ee67d69a 3401 free_contents_and_exit:
699733f6 3402 if (contents)
ee67d69a 3403 free (contents);
c9727e01 3404 goto done;
699733f6 3405 }
90e3cdf2 3406
699733f6
AM
3407 size = 0;
3408 for (i = secsymend; i < opdsymend; ++i)
3409 {
3410 bfd_vma ent;
90e3cdf2 3411
5ef11c02
AM
3412 /* Ignore bogus symbols. */
3413 if (syms[i]->value > opd->size - 8)
3414 continue;
3415
699733f6
AM
3416 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3417 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3418 {
3419 ++count;
3420 size += sizeof (asymbol);
3421 size += strlen (syms[i]->name) + 2;
3422 }
3423 }
90e3cdf2 3424
468392fb 3425 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3426 if (dyn_count != 0
3427 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3428 {
3429 bfd_byte *dynbuf, *extdyn, *extdynend;
3430 size_t extdynsize;
3431 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3432
3433 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3434 goto free_contents_and_exit_err;
468392fb
AM
3435
3436 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3437 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3438
3439 extdyn = dynbuf;
3440 extdynend = extdyn + dynamic->size;
3441 for (; extdyn < extdynend; extdyn += extdynsize)
3442 {
3443 Elf_Internal_Dyn dyn;
3444 (*swap_dyn_in) (abfd, extdyn, &dyn);
3445
3446 if (dyn.d_tag == DT_NULL)
3447 break;
3448
3449 if (dyn.d_tag == DT_PPC64_GLINK)
3450 {
b9e5796b
AM
3451 /* The first glink stub starts at offset 32; see
3452 comment in ppc64_elf_finish_dynamic_sections. */
3453 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3454 /* The .glink section usually does not survive the final
3455 link; search for the section (usually .text) where the
3456 glink stubs now reside. */
3457 glink = bfd_sections_find_if (abfd, section_covers_vma,
3458 &glink_vma);
3459 break;
3460 }
3461 }
3462
3463 free (dynbuf);
3464 }
3465
3466 if (glink != NULL)
3467 {
3468 /* Determine __glink trampoline by reading the relative branch
3469 from the first glink stub. */
3470 bfd_byte buf[4];
b9e5796b
AM
3471 unsigned int off = 0;
3472
3473 while (bfd_get_section_contents (abfd, glink, buf,
3474 glink_vma + off - glink->vma, 4))
468392fb
AM
3475 {
3476 unsigned int insn = bfd_get_32 (abfd, buf);
3477 insn ^= B_DOT;
3478 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3479 {
3480 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3481 break;
3482 }
3483 off += 4;
3484 if (off > 4)
3485 break;
468392fb
AM
3486 }
3487
3488 if (resolv_vma)
3489 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3490
066ee829
AM
3491 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3492 if (relplt != NULL)
3493 {
3494 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3495 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3496 goto free_contents_and_exit_err;
68ffbac6 3497
066ee829
AM
3498 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3499 size += plt_count * sizeof (asymbol);
468392fb 3500
066ee829
AM
3501 p = relplt->relocation;
3502 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3503 {
3504 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3505 if (p->addend != 0)
3506 size += sizeof ("+0x") - 1 + 16;
3507 }
066ee829 3508 }
468392fb
AM
3509 }
3510
c4b0b099
AM
3511 if (size == 0)
3512 goto free_contents_and_exit;
699733f6
AM
3513 s = *ret = bfd_malloc (size);
3514 if (s == NULL)
c4b0b099 3515 goto free_contents_and_exit_err;
90e3cdf2 3516
468392fb 3517 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3518
699733f6 3519 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3520 {
699733f6 3521 bfd_vma ent;
90e3cdf2 3522
5ef11c02
AM
3523 if (syms[i]->value > opd->size - 8)
3524 continue;
3525
699733f6
AM
3526 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3527 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3528 {
c9727e01 3529 long lo, hi;
699733f6 3530 size_t len;
c9727e01 3531 asection *sec = abfd->sections;
90e3cdf2 3532
699733f6
AM
3533 *s = *syms[i];
3534 lo = codesecsym;
3535 hi = codesecsymend;
3536 while (lo < hi)
3537 {
c9727e01 3538 long mid = (lo + hi) >> 1;
699733f6
AM
3539 if (syms[mid]->section->vma < ent)
3540 lo = mid + 1;
3541 else if (syms[mid]->section->vma > ent)
3542 hi = mid;
3543 else
c9727e01
AM
3544 {
3545 sec = syms[mid]->section;
3546 break;
3547 }
699733f6
AM
3548 }
3549
c9727e01 3550 if (lo >= hi && lo > codesecsym)
699733f6 3551 sec = syms[lo - 1]->section;
699733f6
AM
3552
3553 for (; sec != NULL; sec = sec->next)
3554 {
3555 if (sec->vma > ent)
3556 break;
63524580
JK
3557 /* SEC_LOAD may not be set if SEC is from a separate debug
3558 info file. */
3559 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3560 break;
3561 if ((sec->flags & SEC_CODE) != 0)
3562 s->section = sec;
3563 }
6ba2a415 3564 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3565 s->value = ent - s->section->vma;
3566 s->name = names;
3567 *names++ = '.';
3568 len = strlen (syms[i]->name);
3569 memcpy (names, syms[i]->name, len + 1);
3570 names += len + 1;
6f610d07
UW
3571 /* Have udata.p point back to the original symbol this
3572 synthetic symbol was derived from. */
3573 s->udata.p = syms[i];
699733f6 3574 s++;
90e3cdf2 3575 }
90e3cdf2 3576 }
699733f6 3577 free (contents);
468392fb
AM
3578
3579 if (glink != NULL && relplt != NULL)
3580 {
3581 if (resolv_vma)
3582 {
3583 /* Add a symbol for the main glink trampoline. */
86a4952b 3584 memset (s, 0, sizeof *s);
468392fb 3585 s->the_bfd = abfd;
6ba2a415 3586 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3587 s->section = glink;
3588 s->value = resolv_vma - glink->vma;
3589 s->name = names;
3590 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3591 names += sizeof ("__glink_PLTresolve");
3592 s++;
3593 count++;
3594 }
3595
3596 /* FIXME: It would be very much nicer to put sym@plt on the
3597 stub rather than on the glink branch table entry. The
3598 objdump disassembler would then use a sensible symbol
3599 name on plt calls. The difficulty in doing so is
3600 a) finding the stubs, and,
3601 b) matching stubs against plt entries, and,
3602 c) there can be multiple stubs for a given plt entry.
3603
3604 Solving (a) could be done by code scanning, but older
3605 ppc64 binaries used different stubs to current code.
3606 (b) is the tricky one since you need to known the toc
3607 pointer for at least one function that uses a pic stub to
3608 be able to calculate the plt address referenced.
3609 (c) means gdb would need to set multiple breakpoints (or
3610 find the glink branch itself) when setting breakpoints
3611 for pending shared library loads. */
3612 p = relplt->relocation;
3613 for (i = 0; i < plt_count; i++, p++)
3614 {
3615 size_t len;
3616
3617 *s = **p->sym_ptr_ptr;
3618 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3619 we are defining a symbol, ensure one of them is set. */
3620 if ((s->flags & BSF_LOCAL) == 0)
3621 s->flags |= BSF_GLOBAL;
6ba2a415 3622 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3623 s->section = glink;
3624 s->value = glink_vma - glink->vma;
3625 s->name = names;
3626 s->udata.p = NULL;
3627 len = strlen ((*p->sym_ptr_ptr)->name);
3628 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3629 names += len;
e054468f
AM
3630 if (p->addend != 0)
3631 {
3632 memcpy (names, "+0x", sizeof ("+0x") - 1);
3633 names += sizeof ("+0x") - 1;
3634 bfd_sprintf_vma (abfd, names, p->addend);
3635 names += strlen (names);
3636 }
468392fb
AM
3637 memcpy (names, "@plt", sizeof ("@plt"));
3638 names += sizeof ("@plt");
3639 s++;
b9e5796b
AM
3640 if (abi < 2)
3641 {
3642 glink_vma += 8;
3643 if (i >= 0x8000)
3644 glink_vma += 4;
3645 }
3646 else
468392fb
AM
3647 glink_vma += 4;
3648 }
3649 count += plt_count;
3650 }
90e3cdf2
JJ
3651 }
3652
c9727e01 3653 done:
a7535cf3 3654 free (syms);
90e3cdf2
JJ
3655 return count;
3656}
5bd4f169 3657\f
65f38f15
AM
3658/* The following functions are specific to the ELF linker, while
3659 functions above are used generally. Those named ppc64_elf_* are
3660 called by the main ELF linker code. They appear in this file more
3661 or less in the order in which they are called. eg.
3662 ppc64_elf_check_relocs is called early in the link process,
3663 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3664 called.
3665
3666 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3667 functions have both a function code symbol and a function descriptor
3668 symbol. A call to foo in a relocatable object file looks like:
3669
3670 . .text
3671 . x:
3672 . bl .foo
3673 . nop
3674
3675 The function definition in another object file might be:
3676
3677 . .section .opd
3678 . foo: .quad .foo
3679 . .quad .TOC.@tocbase
3680 . .quad 0
3681 .
3682 . .text
3683 . .foo: blr
3684
3685 When the linker resolves the call during a static link, the branch
3686 unsurprisingly just goes to .foo and the .opd information is unused.
3687 If the function definition is in a shared library, things are a little
3688 different: The call goes via a plt call stub, the opd information gets
3689 copied to the plt, and the linker patches the nop.
3690
3691 . x:
3692 . bl .foo_stub
3693 . ld 2,40(1)
3694 .
3695 .
3696 . .foo_stub:
71a39c98
AM
3697 . std 2,40(1) # in practice, the call stub
3698 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3699 . addi 11,11,Lfoo@toc@l # this is the general idea
3700 . ld 12,0(11)
3701 . ld 2,8(11)
3702 . mtctr 12
3703 . ld 11,16(11)
e86ce104
AM
3704 . bctr
3705 .
3706 . .section .plt
3707 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3708
3709 The "reloc ()" notation is supposed to indicate that the linker emits
3710 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3711 copying.
3712
3713 What are the difficulties here? Well, firstly, the relocations
3714 examined by the linker in check_relocs are against the function code
3715 sym .foo, while the dynamic relocation in the plt is emitted against
3716 the function descriptor symbol, foo. Somewhere along the line, we need
3717 to carefully copy dynamic link information from one symbol to the other.
3718 Secondly, the generic part of the elf linker will make .foo a dynamic
3719 symbol as is normal for most other backends. We need foo dynamic
3720 instead, at least for an application final link. However, when
3721 creating a shared library containing foo, we need to have both symbols
3722 dynamic so that references to .foo are satisfied during the early
3723 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3724 definition from some other object, eg. a static library.
3725
3726 Update: As of August 2004, we support a new convention. Function
3727 calls may use the function descriptor symbol, ie. "bl foo". This
3728 behaves exactly as "bl .foo". */
65f38f15 3729
1d483afe 3730/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3731 selects those that must be copied when linking a shared library,
3732 even when the symbol is local. */
65f38f15 3733
1d483afe
AM
3734static int
3735must_be_dyn_reloc (struct bfd_link_info *info,
3736 enum elf_ppc64_reloc_type r_type)
3737{
3738 switch (r_type)
3739 {
3740 default:
3741 return 1;
3742
3743 case R_PPC64_REL32:
3744 case R_PPC64_REL64:
3745 case R_PPC64_REL30:
3746 return 0;
3747
3748 case R_PPC64_TPREL16:
3749 case R_PPC64_TPREL16_LO:
3750 case R_PPC64_TPREL16_HI:
3751 case R_PPC64_TPREL16_HA:
3752 case R_PPC64_TPREL16_DS:
3753 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3754 case R_PPC64_TPREL16_HIGH:
3755 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3756 case R_PPC64_TPREL16_HIGHER:
3757 case R_PPC64_TPREL16_HIGHERA:
3758 case R_PPC64_TPREL16_HIGHEST:
3759 case R_PPC64_TPREL16_HIGHESTA:
3760 case R_PPC64_TPREL64:
0e1862bb 3761 return !bfd_link_executable (info);
1d483afe
AM
3762 }
3763}
65f38f15 3764
f4656909
AM
3765/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3766 copying dynamic variables from a shared lib into an app's dynbss
3767 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3768 shared lib. With code that gcc generates, it's vital that this be
3769 enabled; In the PowerPC64 ABI, the address of a function is actually
3770 the address of a function descriptor, which resides in the .opd
3771 section. gcc uses the descriptor directly rather than going via the
3772 GOT as some other ABI's do, which means that initialized function
3773 pointers must reference the descriptor. Thus, a function pointer
3774 initialized to the address of a function in a shared library will
3775 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3776 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3777 presents a problem as a plt entry for that function is also
3778 initialized from the function descriptor symbol and the copy reloc
3779 may not be initialized first. */
a23b6845 3780#define ELIMINATE_COPY_RELOCS 1
f4656909 3781
721956f4 3782/* Section name for stubs is the associated section name plus this
29942be8
NC
3783 string. */
3784#define STUB_SUFFIX ".stub"
721956f4
AM
3785
3786/* Linker stubs.
3787 ppc_stub_long_branch:
3788 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3789 destination, but a 24 bit branch in a stub section will reach.
3790 . b dest
3791
3792 ppc_stub_plt_branch:
3793 Similar to the above, but a 24 bit branch in the stub section won't
3794 reach its destination.
71a39c98
AM
3795 . addis %r11,%r2,xxx@toc@ha
3796 . ld %r12,xxx@toc@l(%r11)
3797 . mtctr %r12
721956f4
AM
3798 . bctr
3799
3800 ppc_stub_plt_call:
2c66dc6c
AM
3801 Used to call a function in a shared library. If it so happens that
3802 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3803 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3804 . std %r2,40(%r1)
71a39c98
AM
3805 . addis %r11,%r2,xxx@toc@ha
3806 . ld %r12,xxx+0@toc@l(%r11)
3807 . mtctr %r12
3808 . ld %r2,xxx+8@toc@l(%r11)
3809 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3810 . bctr
ad8e1ba5
AM
3811
3812 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3813 code to adjust the value and save r2 to support multiple toc sections.
3814 A ppc_stub_long_branch with an r2 offset looks like:
3815 . std %r2,40(%r1)
3816 . addis %r2,%r2,off@ha
3817 . addi %r2,%r2,off@l
3818 . b dest
3819
3820 A ppc_stub_plt_branch with an r2 offset looks like:
3821 . std %r2,40(%r1)
71a39c98
AM
3822 . addis %r11,%r2,xxx@toc@ha
3823 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3824 . addis %r2,%r2,off@ha
3825 . addi %r2,%r2,off@l
71a39c98 3826 . mtctr %r12
ad8e1ba5 3827 . bctr
ac2df442
AM
3828
3829 In cases where the "addis" instruction would add zero, the "addis" is
3830 omitted and following instructions modified slightly in some cases.
721956f4
AM
3831*/
3832
3833enum ppc_stub_type {
3834 ppc_stub_none,
3835 ppc_stub_long_branch,
ad8e1ba5 3836 ppc_stub_long_branch_r2off,
721956f4 3837 ppc_stub_plt_branch,
ad8e1ba5 3838 ppc_stub_plt_branch_r2off,
794e51c0 3839 ppc_stub_plt_call,
7341d5e2 3840 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3841 ppc_stub_global_entry,
3842 ppc_stub_save_res
721956f4
AM
3843};
3844
6f20ed8a
AM
3845/* Information on stub grouping. */
3846struct map_stub
3847{
3848 /* The stub section. */
3849 asection *stub_sec;
3850 /* This is the section to which stubs in the group will be attached. */
3851 asection *link_sec;
a4b6fadd
AM
3852 /* Next group. */
3853 struct map_stub *next;
3854 /* Whether to emit a copy of register save/restore functions in this
3855 group. */
3856 int needs_save_res;
6f20ed8a
AM
3857};
3858
721956f4
AM
3859struct ppc_stub_hash_entry {
3860
3861 /* Base hash table entry structure. */
3862 struct bfd_hash_entry root;
3863
ad8e1ba5
AM
3864 enum ppc_stub_type stub_type;
3865
6f20ed8a
AM
3866 /* Group information. */
3867 struct map_stub *group;
721956f4
AM
3868
3869 /* Offset within stub_sec of the beginning of this stub. */
3870 bfd_vma stub_offset;
3871
3872 /* Given the symbol's value and its section we can determine its final
3873 value when building the stubs (so the stub knows where to jump. */
3874 bfd_vma target_value;
3875 asection *target_section;
3876
721956f4
AM
3877 /* The symbol table entry, if any, that this was derived from. */
3878 struct ppc_link_hash_entry *h;
e054468f 3879 struct plt_entry *plt_ent;
721956f4 3880
6911b7dc
AM
3881 /* Symbol st_other. */
3882 unsigned char other;
721956f4
AM
3883};
3884
3885struct ppc_branch_hash_entry {
3886
3887 /* Base hash table entry structure. */
3888 struct bfd_hash_entry root;
3889
c456f082 3890 /* Offset within branch lookup table. */
721956f4
AM
3891 unsigned int offset;
3892
3893 /* Generation marker. */
3894 unsigned int iter;
3895};
65f38f15 3896
19e08130
AM
3897/* Used to track dynamic relocations for local symbols. */
3898struct ppc_dyn_relocs
3899{
3900 struct ppc_dyn_relocs *next;
3901
3902 /* The input section of the reloc. */
3903 asection *sec;
3904
3905 /* Total number of relocs copied for the input section. */
3906 unsigned int count : 31;
3907
3908 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3909 unsigned int ifunc : 1;
3910};
3911
65f38f15
AM
3912struct ppc_link_hash_entry
3913{
3914 struct elf_link_hash_entry elf;
3915
b3fac117
AM
3916 union {
3917 /* A pointer to the most recently used stub hash entry against this
3918 symbol. */
3919 struct ppc_stub_hash_entry *stub_cache;
3920
3921 /* A pointer to the next symbol starting with a '.' */
3922 struct ppc_link_hash_entry *next_dot_sym;
3923 } u;
721956f4 3924
65f38f15 3925 /* Track dynamic relocs copied for this symbol. */
6061a67d 3926 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3927
721956f4 3928 /* Link between function code and descriptor symbols. */
34814b9f 3929 struct ppc_link_hash_entry *oh;
721956f4 3930
e86ce104
AM
3931 /* Flag function code and descriptor symbols. */
3932 unsigned int is_func:1;
3933 unsigned int is_func_descriptor:1;
908b32fc 3934 unsigned int fake:1;
411e1bfb 3935
c5614fa4
AM
3936 /* Whether global opd/toc sym has been adjusted or not.
3937 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3938 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3939 unsigned int adjust_done:1;
3940
99877b66
AM
3941 /* Set if we twiddled this symbol to weak at some stage. */
3942 unsigned int was_undefined:1;
3943
a4b6fadd
AM
3944 /* Set if this is an out-of-line register save/restore function,
3945 with non-standard calling convention. */
3946 unsigned int save_res:1;
3947
411e1bfb 3948 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3949 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3950 corresponding relocs are encountered during check_relocs.
3951 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3952 indicate the corresponding GOT entry type is not needed.
3953 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3954 a TPREL one. We use a separate flag rather than setting TPREL
3955 just for convenience in distinguishing the two cases. */
3956#define TLS_GD 1 /* GD reloc. */
3957#define TLS_LD 2 /* LD reloc. */
3958#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3959#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3960#define TLS_TLS 16 /* Any TLS reloc. */
3961#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3962#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3963#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3964 unsigned char tls_mask;
65f38f15
AM
3965};
3966
3967/* ppc64 ELF linker hash table. */
3968
3969struct ppc_link_hash_table
3970{
3971 struct elf_link_hash_table elf;
3972
721956f4
AM
3973 /* The stub hash table. */
3974 struct bfd_hash_table stub_hash_table;
3975
3976 /* Another hash table for plt_branch stubs. */
3977 struct bfd_hash_table branch_hash_table;
3978
3b421ab3
AM
3979 /* Hash table for function prologue tocsave. */
3980 htab_t tocsave_htab;
3981
e7d1c40c
AM
3982 /* Various options and other info passed from the linker. */
3983 struct ppc64_elf_params *params;
721956f4 3984
6f20ed8a
AM
3985 /* The size of sec_info below. */
3986 unsigned int sec_info_arr_size;
3987
3988 /* Per-section array of extra section info. Done this way rather
3989 than as part of ppc64_elf_section_data so we have the info for
3990 non-ppc64 sections. */
3991 struct
3992 {
3993 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3994 bfd_vma toc_off;
6f20ed8a
AM
3995
3996 union
3997 {
3998 /* The section group that this section belongs to. */
3999 struct map_stub *group;
4000 /* A temp section list pointer. */
4001 asection *list;
4002 } u;
4003 } *sec_info;
721956f4 4004
a4b6fadd
AM
4005 /* Linked list of groups. */
4006 struct map_stub *group;
4007
ad8e1ba5
AM
4008 /* Temp used when calculating TOC pointers. */
4009 bfd_vma toc_curr;
bf102f86
AM
4010 bfd *toc_bfd;
4011 asection *toc_first_sec;
ad8e1ba5 4012
b3fac117
AM
4013 /* Used when adding symbols. */
4014 struct ppc_link_hash_entry *dot_syms;
4015
33e44f2e 4016 /* Shortcuts to get to dynamic linker sections. */
4ce794b7
AM
4017 asection *dynbss;
4018 asection *relbss;
4019 asection *glink;
82bd7b59 4020 asection *sfpr;
4ce794b7
AM
4021 asection *brlt;
4022 asection *relbrlt;
58d180e8 4023 asection *glink_eh_frame;
ec338859 4024
8387904d
AM
4025 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4026 struct ppc_link_hash_entry *tls_get_addr;
4027 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4028
927be08e
AM
4029 /* The size of reliplt used by got entry relocs. */
4030 bfd_size_type got_reli_size;
4031
9b5ecbd0 4032 /* Statistics. */
7341d5e2 4033 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4034
ee75fd95
AM
4035 /* Number of stubs against global syms. */
4036 unsigned long stub_globals;
4037
ee67d69a
AM
4038 /* Set if we're linking code with function descriptors. */
4039 unsigned int opd_abi:1;
4040
4c52953f 4041 /* Support for multiple toc sections. */
33c0ec9d 4042 unsigned int do_multi_toc:1;
4c52953f 4043 unsigned int multi_toc_needed:1;
927be08e 4044 unsigned int second_toc_pass:1;
67f0cbdb 4045 unsigned int do_toc_opt:1;
4c52953f 4046
5d1634d7 4047 /* Set on error. */
99877b66 4048 unsigned int stub_error:1;
721956f4 4049
f6c7c3e8 4050 /* Temp used by ppc64_elf_before_check_relocs. */
99877b66 4051 unsigned int twiddled_syms:1;
721956f4
AM
4052
4053 /* Incremented every time we size stubs. */
4054 unsigned int stub_iteration;
5d1634d7 4055
87d72d41
AM
4056 /* Small local sym cache. */
4057 struct sym_cache sym_cache;
65f38f15
AM
4058};
4059
4c52953f
AM
4060/* Rename some of the generic section flags to better document how they
4061 are used here. */
b0dddeec
AM
4062
4063/* Nonzero if this section has TLS related relocations. */
4064#define has_tls_reloc sec_flg0
4065
4066/* Nonzero if this section has a call to __tls_get_addr. */
4067#define has_tls_get_addr_call sec_flg1
4068
4069/* Nonzero if this section has any toc or got relocs. */
4070#define has_toc_reloc sec_flg2
4071
4072/* Nonzero if this section has a call to another section that uses
4073 the toc or got. */
d77c8a4b 4074#define makes_toc_func_call sec_flg3
b0dddeec
AM
4075
4076/* Recursion protection when determining above flag. */
d77c8a4b 4077#define call_check_in_progress sec_flg4
70cc837d 4078#define call_check_done sec_flg5
4c52953f 4079
65f38f15
AM
4080/* Get the ppc64 ELF linker hash table from a link_info structure. */
4081
4082#define ppc_hash_table(p) \
4dfe6ac6
NC
4083 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4084 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4085
721956f4
AM
4086#define ppc_stub_hash_lookup(table, string, create, copy) \
4087 ((struct ppc_stub_hash_entry *) \
4088 bfd_hash_lookup ((table), (string), (create), (copy)))
4089
4090#define ppc_branch_hash_lookup(table, string, create, copy) \
4091 ((struct ppc_branch_hash_entry *) \
4092 bfd_hash_lookup ((table), (string), (create), (copy)))
4093
4094/* Create an entry in the stub hash table. */
4095
4096static struct bfd_hash_entry *
4ce794b7
AM
4097stub_hash_newfunc (struct bfd_hash_entry *entry,
4098 struct bfd_hash_table *table,
4099 const char *string)
721956f4
AM
4100{
4101 /* Allocate the structure if it has not already been allocated by a
4102 subclass. */
4103 if (entry == NULL)
4104 {
4105 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4106 if (entry == NULL)
4107 return entry;
4108 }
4109
4110 /* Call the allocation method of the superclass. */
4111 entry = bfd_hash_newfunc (entry, table, string);
4112 if (entry != NULL)
4113 {
4114 struct ppc_stub_hash_entry *eh;
4115
4116 /* Initialize the local fields. */
4117 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4118 eh->stub_type = ppc_stub_none;
6f20ed8a 4119 eh->group = NULL;
721956f4
AM
4120 eh->stub_offset = 0;
4121 eh->target_value = 0;
4122 eh->target_section = NULL;
721956f4 4123 eh->h = NULL;
6911b7dc 4124 eh->plt_ent = NULL;
6911b7dc 4125 eh->other = 0;
721956f4
AM
4126 }
4127
4128 return entry;
4129}
4130
4131/* Create an entry in the branch hash table. */
4132
4133static struct bfd_hash_entry *
4ce794b7
AM
4134branch_hash_newfunc (struct bfd_hash_entry *entry,
4135 struct bfd_hash_table *table,
4136 const char *string)
721956f4
AM
4137{
4138 /* Allocate the structure if it has not already been allocated by a
4139 subclass. */
4140 if (entry == NULL)
4141 {
4142 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4143 if (entry == NULL)
4144 return entry;
4145 }
4146
4147 /* Call the allocation method of the superclass. */
4148 entry = bfd_hash_newfunc (entry, table, string);
4149 if (entry != NULL)
4150 {
4151 struct ppc_branch_hash_entry *eh;
4152
4153 /* Initialize the local fields. */
4154 eh = (struct ppc_branch_hash_entry *) entry;
4155 eh->offset = 0;
4156 eh->iter = 0;
4157 }
4158
4159 return entry;
4160}
4161
65f38f15
AM
4162/* Create an entry in a ppc64 ELF linker hash table. */
4163
4164static struct bfd_hash_entry *
4ce794b7
AM
4165link_hash_newfunc (struct bfd_hash_entry *entry,
4166 struct bfd_hash_table *table,
4167 const char *string)
65f38f15
AM
4168{
4169 /* Allocate the structure if it has not already been allocated by a
4170 subclass. */
4171 if (entry == NULL)
4172 {
4173 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4174 if (entry == NULL)
4175 return entry;
4176 }
4177
4178 /* Call the allocation method of the superclass. */
4179 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4180 if (entry != NULL)
4181 {
4182 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4183
b3fac117 4184 memset (&eh->u.stub_cache, 0,
908b32fc 4185 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4186 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4187
4188 /* When making function calls, old ABI code references function entry
4189 points (dot symbols), while new ABI code references the function
4190 descriptor symbol. We need to make any combination of reference and
4191 definition work together, without breaking archive linking.
4192
4193 For a defined function "foo" and an undefined call to "bar":
4194 An old object defines "foo" and ".foo", references ".bar" (possibly
4195 "bar" too).
4196 A new object defines "foo" and references "bar".
4197
4198 A new object thus has no problem with its undefined symbols being
4199 satisfied by definitions in an old object. On the other hand, the
4200 old object won't have ".bar" satisfied by a new object.
4201
4202 Keep a list of newly added dot-symbols. */
4203
4204 if (string[0] == '.')
4205 {
4206 struct ppc_link_hash_table *htab;
4207
4208 htab = (struct ppc_link_hash_table *) table;
4209 eh->u.next_dot_sym = htab->dot_syms;
4210 htab->dot_syms = eh;
4211 }
65f38f15
AM
4212 }
4213
4214 return entry;
4215}
4216
3b421ab3
AM
4217struct tocsave_entry {
4218 asection *sec;
4219 bfd_vma offset;
4220};
4221
4222static hashval_t
4223tocsave_htab_hash (const void *p)
4224{
4225 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4226 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4227}
4228
4229static int
4230tocsave_htab_eq (const void *p1, const void *p2)
4231{
4232 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4233 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4234 return e1->sec == e2->sec && e1->offset == e2->offset;
4235}
4236
68faa637
AM
4237/* Destroy a ppc64 ELF linker hash table. */
4238
4239static void
d495ab0d 4240ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4241{
d495ab0d 4242 struct ppc_link_hash_table *htab;
68faa637 4243
d495ab0d 4244 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4245 if (htab->tocsave_htab)
4246 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4247 bfd_hash_table_free (&htab->branch_hash_table);
4248 bfd_hash_table_free (&htab->stub_hash_table);
4249 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4250}
4251
65f38f15
AM
4252/* Create a ppc64 ELF linker hash table. */
4253
4254static struct bfd_link_hash_table *
4ce794b7 4255ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4256{
4257 struct ppc_link_hash_table *htab;
4258 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4259
4ce794b7 4260 htab = bfd_zmalloc (amt);
65f38f15
AM
4261 if (htab == NULL)
4262 return NULL;
4263
66eb6687 4264 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4265 sizeof (struct ppc_link_hash_entry),
4266 PPC64_ELF_DATA))
65f38f15 4267 {
e2d34d7d 4268 free (htab);
65f38f15
AM
4269 return NULL;
4270 }
4271
721956f4 4272 /* Init the stub hash table too. */
66eb6687
AM
4273 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4274 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4275 {
d495ab0d 4276 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4277 return NULL;
4278 }
721956f4
AM
4279
4280 /* And the branch hash table. */
66eb6687
AM
4281 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4282 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4283 {
4284 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4285 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4286 return NULL;
4287 }
721956f4 4288
3b421ab3
AM
4289 htab->tocsave_htab = htab_try_create (1024,
4290 tocsave_htab_hash,
4291 tocsave_htab_eq,
4292 NULL);
4293 if (htab->tocsave_htab == NULL)
2915c55b 4294 {
d495ab0d 4295 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4296 return NULL;
4297 }
d495ab0d 4298 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4299
3254fd24
AM
4300 /* Initializing two fields of the union is just cosmetic. We really
4301 only care about glist, but when compiled on a 32-bit host the
4302 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4303 debugger inspection of these fields look nicer. */
a6aa5195
AM
4304 htab->elf.init_got_refcount.refcount = 0;
4305 htab->elf.init_got_refcount.glist = NULL;
4306 htab->elf.init_plt_refcount.refcount = 0;
4307 htab->elf.init_plt_refcount.glist = NULL;
4308 htab->elf.init_got_offset.offset = 0;
4309 htab->elf.init_got_offset.glist = NULL;
4310 htab->elf.init_plt_offset.offset = 0;
4311 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4312
65f38f15
AM
4313 return &htab->elf.root;
4314}
4315
bfeb4a28
AM
4316/* Create sections for linker generated code. */
4317
4318static bfd_boolean
4319create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4320{
4321 struct ppc_link_hash_table *htab;
4322 flagword flags;
4323
4324 htab = ppc_hash_table (info);
4325
4326 /* Create .sfpr for code to save and restore fp regs. */
4327 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4328 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4329 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4330 flags);
4331 if (htab->sfpr == NULL
4332 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4333 return FALSE;
4334
4335 /* Create .glink for lazy dynamic linking support. */
4336 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4337 flags);
4338 if (htab->glink == NULL
4339 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4340 return FALSE;
4341
4342 if (!info->no_ld_generated_unwind_info)
4343 {
4344 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4345 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4346 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4347 ".eh_frame",
4348 flags);
4349 if (htab->glink_eh_frame == NULL
4350 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4351 return FALSE;
4352 }
4353
4354 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4355 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4356 if (htab->elf.iplt == NULL
4357 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4358 return FALSE;
4359
4360 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4361 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4362 htab->elf.irelplt
4363 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4364 if (htab->elf.irelplt == NULL
4365 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4366 return FALSE;
4367
4368 /* Create branch lookup table for plt_branch stubs. */
4369 flags = (SEC_ALLOC | SEC_LOAD
4370 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4371 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4372 flags);
4373 if (htab->brlt == NULL
4374 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4375 return FALSE;
4376
0e1862bb 4377 if (!bfd_link_pic (info))
bfeb4a28
AM
4378 return TRUE;
4379
4380 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4381 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4382 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4383 ".rela.branch_lt",
4384 flags);
4385 if (htab->relbrlt == NULL
4386 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4387 return FALSE;
4388
4389 return TRUE;
4390}
4391
e717da7e
AM
4392/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4393
bfeb4a28 4394bfd_boolean
e7d1c40c
AM
4395ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4396 struct ppc64_elf_params *params)
e717da7e
AM
4397{
4398 struct ppc_link_hash_table *htab;
4399
e7d1c40c 4400 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4401
4402/* Always hook our dynamic sections into the first bfd, which is the
4403 linker created stub bfd. This ensures that the GOT header is at
4404 the start of the output TOC section. */
4405 htab = ppc_hash_table (info);
4dfe6ac6 4406 if (htab == NULL)
bfeb4a28 4407 return FALSE;
e7d1c40c
AM
4408 htab->elf.dynobj = params->stub_bfd;
4409 htab->params = params;
bfeb4a28 4410
0e1862bb 4411 if (bfd_link_relocatable (info))
bfeb4a28
AM
4412 return TRUE;
4413
4414 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4415}
4416
721956f4
AM
4417/* Build a name for an entry in the stub hash table. */
4418
4419static char *
4ce794b7
AM
4420ppc_stub_name (const asection *input_section,
4421 const asection *sym_sec,
4422 const struct ppc_link_hash_entry *h,
4423 const Elf_Internal_Rela *rel)
721956f4
AM
4424{
4425 char *stub_name;
bcaa2f82 4426 ssize_t len;
721956f4
AM
4427
4428 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4429 offsets from a sym as a branch target? In fact, we could
4430 probably assume the addend is always zero. */
4431 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4432
4433 if (h)
4434 {
4435 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4436 stub_name = bfd_malloc (len);
46de2a7c
AM
4437 if (stub_name == NULL)
4438 return stub_name;
4439
bcaa2f82
AM
4440 len = sprintf (stub_name, "%08x.%s+%x",
4441 input_section->id & 0xffffffff,
4442 h->elf.root.root.string,
4443 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4444 }
4445 else
4446 {
ad8e1ba5 4447 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4448 stub_name = bfd_malloc (len);
46de2a7c
AM
4449 if (stub_name == NULL)
4450 return stub_name;
4451
bcaa2f82
AM
4452 len = sprintf (stub_name, "%08x.%x:%x+%x",
4453 input_section->id & 0xffffffff,
4454 sym_sec->id & 0xffffffff,
4455 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4456 (int) rel->r_addend & 0xffffffff);
721956f4 4457 }
bcaa2f82 4458 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4459 stub_name[len - 2] = 0;
721956f4
AM
4460 return stub_name;
4461}
4462
4463/* Look up an entry in the stub hash. Stub entries are cached because
4464 creating the stub name takes a bit of time. */
4465
4466static struct ppc_stub_hash_entry *
4ce794b7
AM
4467ppc_get_stub_entry (const asection *input_section,
4468 const asection *sym_sec,
039b3fef 4469 struct ppc_link_hash_entry *h,
4ce794b7
AM
4470 const Elf_Internal_Rela *rel,
4471 struct ppc_link_hash_table *htab)
721956f4
AM
4472{
4473 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4474 struct map_stub *group;
721956f4
AM
4475
4476 /* If this input section is part of a group of sections sharing one
4477 stub section, then use the id of the first section in the group.
4478 Stub names need to include a section id, as there may well be
4479 more than one stub used to reach say, printf, and we need to
4480 distinguish between them. */
6f20ed8a 4481 group = htab->sec_info[input_section->id].u.group;
721956f4 4482
b3fac117
AM
4483 if (h != NULL && h->u.stub_cache != NULL
4484 && h->u.stub_cache->h == h
6f20ed8a 4485 && h->u.stub_cache->group == group)
721956f4 4486 {
b3fac117 4487 stub_entry = h->u.stub_cache;
721956f4
AM
4488 }
4489 else
4490 {
4491 char *stub_name;
4492
6f20ed8a 4493 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4494 if (stub_name == NULL)
4495 return NULL;
4496
4497 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4498 stub_name, FALSE, FALSE);
721956f4 4499 if (h != NULL)
b3fac117 4500 h->u.stub_cache = stub_entry;
721956f4
AM
4501
4502 free (stub_name);
4503 }
4504
4505 return stub_entry;
4506}
4507
4508/* Add a new stub entry to the stub hash. Not all fields of the new
4509 stub entry are initialised. */
4510
4511static struct ppc_stub_hash_entry *
4ce794b7
AM
4512ppc_add_stub (const char *stub_name,
4513 asection *section,
25f53a85 4514 struct bfd_link_info *info)
721956f4 4515{
25f53a85 4516 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4517 struct map_stub *group;
721956f4
AM
4518 asection *link_sec;
4519 asection *stub_sec;
4520 struct ppc_stub_hash_entry *stub_entry;
4521
6f20ed8a
AM
4522 group = htab->sec_info[section->id].u.group;
4523 link_sec = group->link_sec;
4524 stub_sec = group->stub_sec;
721956f4
AM
4525 if (stub_sec == NULL)
4526 {
6f20ed8a
AM
4527 size_t namelen;
4528 bfd_size_type len;
4529 char *s_name;
721956f4 4530
6f20ed8a
AM
4531 namelen = strlen (link_sec->name);
4532 len = namelen + sizeof (STUB_SUFFIX);
4533 s_name = bfd_alloc (htab->params->stub_bfd, len);
4534 if (s_name == NULL)
4535 return NULL;
721956f4 4536
6f20ed8a
AM
4537 memcpy (s_name, link_sec->name, namelen);
4538 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4539 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4540 if (stub_sec == NULL)
4541 return NULL;
4542 group->stub_sec = stub_sec;
721956f4
AM
4543 }
4544
4545 /* Enter this entry into the linker stub hash table. */
4546 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4547 TRUE, FALSE);
721956f4
AM
4548 if (stub_entry == NULL)
4549 {
8de848d8 4550 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4551 section->owner, stub_name);
721956f4
AM
4552 return NULL;
4553 }
4554
6f20ed8a 4555 stub_entry->group = group;
721956f4 4556 stub_entry->stub_offset = 0;
721956f4
AM
4557 return stub_entry;
4558}
4559
e717da7e
AM
4560/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4561 not already done. */
65f38f15 4562
b34976b6 4563static bfd_boolean
e717da7e 4564create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4565{
e717da7e
AM
4566 asection *got, *relgot;
4567 flagword flags;
4568 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4569
0c8d6e5c 4570 if (!is_ppc64_elf (abfd))
0ffa91dd 4571 return FALSE;
4dfe6ac6
NC
4572 if (htab == NULL)
4573 return FALSE;
0ffa91dd 4574
33e44f2e
AM
4575 if (!htab->elf.sgot
4576 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4577 return FALSE;
e717da7e
AM
4578
4579 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4580 | SEC_LINKER_CREATED);
4581
c456f082 4582 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4583 if (!got
e717da7e
AM
4584 || !bfd_set_section_alignment (abfd, got, 3))
4585 return FALSE;
65f38f15 4586
c456f082
AM
4587 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4588 flags | SEC_READONLY);
e717da7e 4589 if (!relgot
e717da7e 4590 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4591 return FALSE;
e717da7e
AM
4592
4593 ppc64_elf_tdata (abfd)->got = got;
4594 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4595 return TRUE;
65f38f15 4596}
5bd4f169 4597
82bd7b59 4598/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4599
b34976b6 4600static bfd_boolean
4ce794b7 4601ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4602{
65f38f15 4603 struct ppc_link_hash_table *htab;
5bd4f169 4604
65f38f15 4605 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4606 return FALSE;
65f38f15 4607
e717da7e 4608 htab = ppc_hash_table (info);
4dfe6ac6
NC
4609 if (htab == NULL)
4610 return FALSE;
4611
3d4d4302 4612 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
0e1862bb 4613 if (!bfd_link_pic (info))
3d4d4302 4614 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4615
33e44f2e 4616 if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
0e1862bb 4617 || (!bfd_link_pic (info) && !htab->relbss))
65f38f15
AM
4618 abort ();
4619
b34976b6 4620 return TRUE;
5bd4f169
AM
4621}
4622
b31867b6
AM
4623/* Follow indirect and warning symbol links. */
4624
4625static inline struct bfd_link_hash_entry *
4626follow_link (struct bfd_link_hash_entry *h)
4627{
4628 while (h->type == bfd_link_hash_indirect
4629 || h->type == bfd_link_hash_warning)
4630 h = h->u.i.link;
4631 return h;
4632}
4633
4634static inline struct elf_link_hash_entry *
4635elf_follow_link (struct elf_link_hash_entry *h)
4636{
4637 return (struct elf_link_hash_entry *) follow_link (&h->root);
4638}
4639
4640static inline struct ppc_link_hash_entry *
4641ppc_follow_link (struct ppc_link_hash_entry *h)
4642{
4643 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4644}
4645
40d16e0b
AM
4646/* Merge PLT info on FROM with that on TO. */
4647
4648static void
4649move_plt_plist (struct ppc_link_hash_entry *from,
4650 struct ppc_link_hash_entry *to)
4651{
4652 if (from->elf.plt.plist != NULL)
4653 {
4654 if (to->elf.plt.plist != NULL)
4655 {
4656 struct plt_entry **entp;
4657 struct plt_entry *ent;
4658
4659 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4660 {
4661 struct plt_entry *dent;
4662
4663 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4664 if (dent->addend == ent->addend)
4665 {
4666 dent->plt.refcount += ent->plt.refcount;
4667 *entp = ent->next;
4668 break;
4669 }
4670 if (dent == NULL)
4671 entp = &ent->next;
4672 }
4673 *entp = to->elf.plt.plist;
4674 }
4675
4676 to->elf.plt.plist = from->elf.plt.plist;
4677 from->elf.plt.plist = NULL;
4678 }
4679}
4680
65f38f15
AM
4681/* Copy the extra info we tack onto an elf_link_hash_entry. */
4682
4683static void
fcfa13d2
AM
4684ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4685 struct elf_link_hash_entry *dir,
4686 struct elf_link_hash_entry *ind)
65f38f15
AM
4687{
4688 struct ppc_link_hash_entry *edir, *eind;
4689
4690 edir = (struct ppc_link_hash_entry *) dir;
4691 eind = (struct ppc_link_hash_entry *) ind;
4692
c79d6685
AM
4693 edir->is_func |= eind->is_func;
4694 edir->is_func_descriptor |= eind->is_func_descriptor;
4695 edir->tls_mask |= eind->tls_mask;
4696 if (eind->oh != NULL)
4697 edir->oh = ppc_follow_link (eind->oh);
4698
4699 /* If called to transfer flags for a weakdef during processing
4700 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4701 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4702 if (!(ELIMINATE_COPY_RELOCS
4703 && eind->elf.root.type != bfd_link_hash_indirect
4704 && edir->elf.dynamic_adjusted))
4705 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4706
4707 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4708 edir->elf.ref_regular |= eind->elf.ref_regular;
4709 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4710 edir->elf.needs_plt |= eind->elf.needs_plt;
a345bc8d 4711 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4712
411e1bfb 4713 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4714 if (eind->dyn_relocs != NULL)
65f38f15 4715 {
bbd7ec4a
AM
4716 if (edir->dyn_relocs != NULL)
4717 {
6061a67d
AM
4718 struct elf_dyn_relocs **pp;
4719 struct elf_dyn_relocs *p;
bbd7ec4a 4720
fcfa13d2 4721 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4722 list. Merge any entries against the same section. */
4723 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4724 {
6061a67d 4725 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4726
4727 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4728 if (q->sec == p->sec)
4729 {
4730 q->pc_count += p->pc_count;
4731 q->count += p->count;
4732 *pp = p->next;
4733 break;
4734 }
4735 if (q == NULL)
4736 pp = &p->next;
4737 }
4738 *pp = edir->dyn_relocs;
4739 }
4740
65f38f15
AM
4741 edir->dyn_relocs = eind->dyn_relocs;
4742 eind->dyn_relocs = NULL;
4743 }
65f38f15 4744
68ba6d40
AM
4745 /* If we were called to copy over info for a weak sym, that's all.
4746 You might think dyn_relocs need not be copied over; After all,
4747 both syms will be dynamic or both non-dynamic so we're just
68ffbac6 4748 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
68ba6d40
AM
4749 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4750 dyn_relocs in read-only sections, and it does so on what is the
4751 DIR sym here. */
4752 if (eind->elf.root.type != bfd_link_hash_indirect)
4753 return;
4754
81848ca0
AM
4755 /* Copy over got entries that we may have already seen to the
4756 symbol which just became indirect. */
411e1bfb
AM
4757 if (eind->elf.got.glist != NULL)
4758 {
4759 if (edir->elf.got.glist != NULL)
4760 {
4761 struct got_entry **entp;
4762 struct got_entry *ent;
4763
4764 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4765 {
4766 struct got_entry *dent;
4767
4768 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4769 if (dent->addend == ent->addend
e717da7e 4770 && dent->owner == ent->owner
411e1bfb
AM
4771 && dent->tls_type == ent->tls_type)
4772 {
4773 dent->got.refcount += ent->got.refcount;
4774 *entp = ent->next;
4775 break;
4776 }
4777 if (dent == NULL)
4778 entp = &ent->next;
4779 }
4780 *entp = edir->elf.got.glist;
4781 }
4782
4783 edir->elf.got.glist = eind->elf.got.glist;
4784 eind->elf.got.glist = NULL;
4785 }
4786
4787 /* And plt entries. */
40d16e0b 4788 move_plt_plist (eind, edir);
411e1bfb 4789
fcfa13d2 4790 if (eind->elf.dynindx != -1)
411e1bfb 4791 {
fcfa13d2
AM
4792 if (edir->elf.dynindx != -1)
4793 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4794 edir->elf.dynstr_index);
411e1bfb
AM
4795 edir->elf.dynindx = eind->elf.dynindx;
4796 edir->elf.dynstr_index = eind->elf.dynstr_index;
4797 eind->elf.dynindx = -1;
4798 eind->elf.dynstr_index = 0;
4799 }
411e1bfb
AM
4800}
4801
8387904d
AM
4802/* Find the function descriptor hash entry from the given function code
4803 hash entry FH. Link the entries via their OH fields. */
4804
4805static struct ppc_link_hash_entry *
b31867b6 4806lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4807{
4808 struct ppc_link_hash_entry *fdh = fh->oh;
4809
4810 if (fdh == NULL)
4811 {
4812 const char *fd_name = fh->elf.root.root.string + 1;
4813
4814 fdh = (struct ppc_link_hash_entry *)
4815 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4816 if (fdh == NULL)
4817 return fdh;
4818
4819 fdh->is_func_descriptor = 1;
4820 fdh->oh = fh;
4821 fh->is_func = 1;
4822 fh->oh = fdh;
8387904d
AM
4823 }
4824
b31867b6 4825 return ppc_follow_link (fdh);
8387904d
AM
4826}
4827
bb700d78
AM
4828/* Make a fake function descriptor sym for the code sym FH. */
4829
4830static struct ppc_link_hash_entry *
4831make_fdh (struct bfd_link_info *info,
908b32fc 4832 struct ppc_link_hash_entry *fh)
bb700d78
AM
4833{
4834 bfd *abfd;
4835 asymbol *newsym;
4836 struct bfd_link_hash_entry *bh;
4837 struct ppc_link_hash_entry *fdh;
4838
4839 abfd = fh->elf.root.u.undef.abfd;
4840 newsym = bfd_make_empty_symbol (abfd);
4841 newsym->name = fh->elf.root.root.string + 1;
4842 newsym->section = bfd_und_section_ptr;
4843 newsym->value = 0;
908b32fc 4844 newsym->flags = BSF_WEAK;
bb700d78
AM
4845
4846 bh = NULL;
4847 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4848 newsym->flags, newsym->section,
4849 newsym->value, NULL, FALSE, FALSE,
4850 &bh))
4851 return NULL;
4852
4853 fdh = (struct ppc_link_hash_entry *) bh;
4854 fdh->elf.non_elf = 0;
908b32fc
AM
4855 fdh->fake = 1;
4856 fdh->is_func_descriptor = 1;
4857 fdh->oh = fh;
4858 fh->is_func = 1;
4859 fh->oh = fdh;
bb700d78
AM
4860 return fdh;
4861}
4862
8387904d
AM
4863/* Fix function descriptor symbols defined in .opd sections to be
4864 function type. */
555cd476
AM
4865
4866static bfd_boolean
c16153ae 4867ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4868 struct bfd_link_info *info,
555cd476 4869 Elf_Internal_Sym *isym,
6911b7dc 4870 const char **name,
555cd476
AM
4871 flagword *flags ATTRIBUTE_UNUSED,
4872 asection **sec,
b53dfeb2 4873 bfd_vma *value)
555cd476 4874{
f1885d1e
AM
4875 if ((ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4876 || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4877 && (ibfd->flags & DYNAMIC) == 0
4878 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
13a2df29 4879 elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
f64b2e8d 4880
b53dfeb2 4881 if (*sec != NULL
f1885d1e 4882 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4883 {
4884 asection *code_sec;
4885
4886 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4887 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4888 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4889
4890 /* If the symbol is a function defined in .opd, and the function
4891 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4892 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4893 && (*sec)->reloc_count != 0
4894 && opd_entry_value (*sec, *value, &code_sec, NULL,
4895 FALSE) != (bfd_vma) -1
4896 && discarded_section (code_sec))
4897 {
4898 *sec = bfd_und_section_ptr;
4899 isym->st_shndx = SHN_UNDEF;
4900 }
4901 }
dbd1e97e
AM
4902 else if (*sec != NULL
4903 && strcmp ((*sec)->name, ".toc") == 0
4904 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4905 {
4906 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4907 if (htab != NULL)
4908 htab->params->object_in_toc = 1;
4909 }
433817dd 4910
6911b7dc
AM
4911 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4912 {
4913 if (abiversion (ibfd) == 0)
4914 set_abiversion (ibfd, 2);
4915 else if (abiversion (ibfd) == 1)
4916 {
4917 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4918 " for ABI version 1\n"), name);
4919 bfd_set_error (bfd_error_bad_value);
4920 return FALSE;
4921 }
4922 }
4923
555cd476
AM
4924 return TRUE;
4925}
4926
6911b7dc
AM
4927/* Merge non-visibility st_other attributes: local entry point. */
4928
4929static void
4930ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4931 const Elf_Internal_Sym *isym,
4932 bfd_boolean definition,
4933 bfd_boolean dynamic)
4934{
4935 if (definition && !dynamic)
4936 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4937 | ELF_ST_VISIBILITY (h->other));
4938}
4939
8387904d 4940/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4941 inclusion of a new ABI object archive that defines "bar".
4942 NAME is a symbol defined in an archive. Return a symbol in the hash
4943 table that might be satisfied by the archive symbols. */
8387904d
AM
4944
4945static struct elf_link_hash_entry *
4946ppc64_elf_archive_symbol_lookup (bfd *abfd,
4947 struct bfd_link_info *info,
4948 const char *name)
4949{
4950 struct elf_link_hash_entry *h;
4951 char *dot_name;
4952 size_t len;
4953
4954 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4955 if (h != NULL
4956 /* Don't return this sym if it is a fake function descriptor
4957 created by add_symbol_adjust. */
4958 && !(h->root.type == bfd_link_hash_undefweak
4959 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4960 return h;
4961
4962 if (name[0] == '.')
4963 return h;
4964
4965 len = strlen (name);
4966 dot_name = bfd_alloc (abfd, len + 2);
4967 if (dot_name == NULL)
4968 return (struct elf_link_hash_entry *) 0 - 1;
4969 dot_name[0] = '.';
4970 memcpy (dot_name + 1, name, len + 1);
4971 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4972 bfd_release (abfd, dot_name);
4973 return h;
4974}
4975
4976/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4977 new ABI object defines "bar". Well, at least, undefined dot symbols
4978 are made weak. This stops later archive searches from including an
4979 object if we already have a function descriptor definition. It also
35b0ce59
AM
4980 prevents the linker complaining about undefined symbols.
4981 We also check and correct mismatched symbol visibility here. The
4982 most restrictive visibility of the function descriptor and the
4983 function entry symbol is used. */
8387904d
AM
4984
4985static bfd_boolean
b3fac117 4986add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4987{
8387904d
AM
4988 struct ppc_link_hash_table *htab;
4989 struct ppc_link_hash_entry *fdh;
4990
b3fac117 4991 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4992 return TRUE;
4993
b3fac117
AM
4994 if (eh->elf.root.type == bfd_link_hash_warning)
4995 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4996
b3fac117
AM
4997 if (eh->elf.root.root.string[0] != '.')
4998 abort ();
8387904d 4999
b3fac117 5000 htab = ppc_hash_table (info);
4dfe6ac6
NC
5001 if (htab == NULL)
5002 return FALSE;
5003
b31867b6
AM
5004 fdh = lookup_fdh (eh, htab);
5005 if (fdh == NULL)
5006 {
0e1862bb 5007 if (!bfd_link_relocatable (info)
b31867b6
AM
5008 && (eh->elf.root.type == bfd_link_hash_undefined
5009 || eh->elf.root.type == bfd_link_hash_undefweak)
5010 && eh->elf.ref_regular)
5011 {
5012 /* Make an undefweak function descriptor sym, which is enough to
5013 pull in an --as-needed shared lib, but won't cause link
5014 errors. Archives are handled elsewhere. */
5015 fdh = make_fdh (info, eh);
5016 if (fdh == NULL)
5017 return FALSE;
5018 fdh->elf.ref_regular = 1;
5019 }
bb700d78 5020 }
b31867b6 5021 else
8387904d 5022 {
35b0ce59
AM
5023 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5024 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5025 if (entry_vis < descr_vis)
5026 fdh->elf.other += entry_vis - descr_vis;
5027 else if (entry_vis > descr_vis)
5028 eh->elf.other += descr_vis - entry_vis;
5029
e87d886e
AM
5030 if ((fdh->elf.root.type == bfd_link_hash_defined
5031 || fdh->elf.root.type == bfd_link_hash_defweak)
5032 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
5033 {
5034 eh->elf.root.type = bfd_link_hash_undefweak;
5035 eh->was_undefined = 1;
5036 htab->twiddled_syms = 1;
5037 }
8387904d 5038 }
99877b66 5039
8387904d
AM
5040 return TRUE;
5041}
5042
f6c7c3e8
AM
5043/* Set up opd section info and abiversion for IBFD, and process list
5044 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5045
8387904d 5046static bfd_boolean
f6c7c3e8 5047ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5048{
99877b66 5049 struct ppc_link_hash_table *htab;
b3fac117 5050 struct ppc_link_hash_entry **p, *eh;
459609d6 5051 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5052
459609d6 5053 if (opd != NULL && opd->size != 0)
b3fac117 5054 {
459609d6
AM
5055 if (abiversion (ibfd) == 0)
5056 set_abiversion (ibfd, 1);
5057 else if (abiversion (ibfd) == 2)
f6c7c3e8 5058 {
459609d6
AM
5059 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5060 " version %d\n"),
5061 ibfd, abiversion (ibfd));
5062 bfd_set_error (bfd_error_bad_value);
5063 return FALSE;
f6c7c3e8
AM
5064 }
5065
459609d6
AM
5066 if ((ibfd->flags & DYNAMIC) == 0
5067 && (opd->flags & SEC_RELOC) != 0
5068 && opd->reloc_count != 0
5069 && !bfd_is_abs_section (opd->output_section))
b3fac117 5070 {
459609d6
AM
5071 /* Garbage collection needs some extra help with .opd sections.
5072 We don't want to necessarily keep everything referenced by
5073 relocs in .opd, as that would keep all functions. Instead,
5074 if we reference an .opd symbol (a function descriptor), we
5075 want to keep the function code symbol's section. This is
5076 easy for global symbols, but for local syms we need to keep
5077 information about the associated function section. */
5078 bfd_size_type amt;
5079 asection **opd_sym_map;
5080
51aecdc5 5081 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5082 opd_sym_map = bfd_zalloc (ibfd, amt);
5083 if (opd_sym_map == NULL)
b3fac117 5084 return FALSE;
459609d6
AM
5085 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5086 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5087 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5088 }
5089 }
5090
459609d6
AM
5091 if (!is_ppc64_elf (info->output_bfd))
5092 return TRUE;
5093 htab = ppc_hash_table (info);
5094 if (htab == NULL)
5095 return FALSE;
5096
5097 /* For input files without an explicit abiversion in e_flags
5098 we should have flagged any with symbol st_other bits set
5099 as ELFv1 and above flagged those with .opd as ELFv2.
5100 Set the output abiversion if not yet set, and for any input
5101 still ambiguous, take its abiversion from the output.
5102 Differences in ABI are reported later. */
5103 if (abiversion (info->output_bfd) == 0)
5104 set_abiversion (info->output_bfd, abiversion (ibfd));
5105 else if (abiversion (ibfd) == 0)
5106 set_abiversion (ibfd, abiversion (info->output_bfd));
5107
5108 p = &htab->dot_syms;
5109 while ((eh = *p) != NULL)
5110 {
5111 *p = NULL;
5112 if (&eh->elf == htab->elf.hgot)
5113 ;
5114 else if (htab->elf.hgot == NULL
5115 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5116 htab->elf.hgot = &eh->elf;
5117 else if (!add_symbol_adjust (eh, info))
5118 return FALSE;
5119 p = &eh->u.next_dot_sym;
5120 }
5121
b3fac117
AM
5122 /* Clear the list for non-ppc64 input files. */
5123 p = &htab->dot_syms;
5124 while ((eh = *p) != NULL)
5125 {
5126 *p = NULL;
5127 p = &eh->u.next_dot_sym;
5128 }
99877b66
AM
5129
5130 /* We need to fix the undefs list for any syms we have twiddled to
5131 undef_weak. */
5132 if (htab->twiddled_syms)
5133 {
77cfaee6 5134 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
5135 htab->twiddled_syms = 0;
5136 }
b3fac117 5137 return TRUE;
8387904d
AM
5138}
5139
97fed1c9
JJ
5140/* Undo hash table changes when an --as-needed input file is determined
5141 not to be needed. */
5142
5143static bfd_boolean
e5034e59
AM
5144ppc64_elf_notice_as_needed (bfd *ibfd,
5145 struct bfd_link_info *info,
5146 enum notice_asneeded_action act)
97fed1c9 5147{
e5034e59
AM
5148 if (act == notice_not_needed)
5149 {
5150 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5151
e5034e59
AM
5152 if (htab == NULL)
5153 return FALSE;
4dfe6ac6 5154
e5034e59
AM
5155 htab->dot_syms = NULL;
5156 }
5157 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5158}
5159
aa374f67
AM
5160/* If --just-symbols against a final linked binary, then assume we need
5161 toc adjusting stubs when calling functions defined there. */
5162
5163static void
5164ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5165{
5166 if ((sec->flags & SEC_CODE) != 0
5167 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5168 && is_ppc64_elf (sec->owner))
5169 {
2c3f079f
AM
5170 if (abiversion (sec->owner) >= 2
5171 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5172 sec->has_toc_reloc = 1;
5173 }
5174 _bfd_elf_link_just_syms (sec, info);
5175}
5176
e054468f 5177static struct plt_entry **
4ce794b7
AM
5178update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5179 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5180{
5181 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5182 struct plt_entry **local_plt;
f961d9dd 5183 unsigned char *local_got_tls_masks;
411e1bfb
AM
5184
5185 if (local_got_ents == NULL)
5186 {
5187 bfd_size_type size = symtab_hdr->sh_info;
5188
e054468f
AM
5189 size *= (sizeof (*local_got_ents)
5190 + sizeof (*local_plt)
5191 + sizeof (*local_got_tls_masks));
4ce794b7 5192 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5193 if (local_got_ents == NULL)
e054468f 5194 return NULL;
411e1bfb
AM
5195 elf_local_got_ents (abfd) = local_got_ents;
5196 }
5197
e054468f 5198 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5199 {
5200 struct got_entry *ent;
5201
5202 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5203 if (ent->addend == r_addend
5204 && ent->owner == abfd
5205 && ent->tls_type == tls_type)
411e1bfb
AM
5206 break;
5207 if (ent == NULL)
5208 {
5209 bfd_size_type amt = sizeof (*ent);
4ce794b7 5210 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5211 if (ent == NULL)
5212 return FALSE;
5213 ent->next = local_got_ents[r_symndx];
5214 ent->addend = r_addend;
e717da7e 5215 ent->owner = abfd;
411e1bfb 5216 ent->tls_type = tls_type;
927be08e 5217 ent->is_indirect = FALSE;
411e1bfb
AM
5218 ent->got.refcount = 0;
5219 local_got_ents[r_symndx] = ent;
5220 }
5221 ent->got.refcount += 1;
5222 }
5223
e054468f 5224 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5225 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5226 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5227
5228 return local_plt + r_symndx;
65f38f15
AM
5229}
5230
411e1bfb 5231static bfd_boolean
e054468f 5232update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5233{
411e1bfb 5234 struct plt_entry *ent;
1e2f5b6e 5235
e054468f 5236 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5237 if (ent->addend == addend)
5238 break;
5239 if (ent == NULL)
1e2f5b6e 5240 {
411e1bfb 5241 bfd_size_type amt = sizeof (*ent);
4ce794b7 5242 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5243 if (ent == NULL)
5244 return FALSE;
e054468f 5245 ent->next = *plist;
411e1bfb
AM
5246 ent->addend = addend;
5247 ent->plt.refcount = 0;
e054468f 5248 *plist = ent;
1e2f5b6e 5249 }
411e1bfb 5250 ent->plt.refcount += 1;
b34976b6 5251 return TRUE;
1e2f5b6e
AM
5252}
5253
e054468f
AM
5254static bfd_boolean
5255is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5256{
5257 return (r_type == R_PPC64_REL24
5258 || r_type == R_PPC64_REL14
5259 || r_type == R_PPC64_REL14_BRTAKEN
5260 || r_type == R_PPC64_REL14_BRNTAKEN
5261 || r_type == R_PPC64_ADDR24
5262 || r_type == R_PPC64_ADDR14
5263 || r_type == R_PPC64_ADDR14_BRTAKEN
5264 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5265}
5266
5bd4f169 5267/* Look through the relocs for a section during the first phase, and
65f38f15 5268 calculate needed space in the global offset table, procedure
5d1634d7 5269 linkage table, and dynamic reloc sections. */
5bd4f169 5270
b34976b6 5271static bfd_boolean
4ce794b7
AM
5272ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5273 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5274{
65f38f15 5275 struct ppc_link_hash_table *htab;
5bd4f169 5276 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5277 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5278 const Elf_Internal_Rela *rel;
5279 const Elf_Internal_Rela *rel_end;
5bd4f169 5280 asection *sreloc;
1e2f5b6e 5281 asection **opd_sym_map;
3a71aa26 5282 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5283
0e1862bb 5284 if (bfd_link_relocatable (info))
b34976b6 5285 return TRUE;
5bd4f169 5286
680a3378
AM
5287 /* Don't do anything special with non-loaded, non-alloced sections.
5288 In particular, any relocs in such sections should not affect GOT
5289 and PLT reference counting (ie. we don't allow them to create GOT
5290 or PLT entries), there's no possibility or desire to optimize TLS
5291 relocs, and there's not much point in propagating relocs to shared
5292 libs that the dynamic linker won't relocate. */
5293 if ((sec->flags & SEC_ALLOC) == 0)
5294 return TRUE;
5295
0c8d6e5c 5296 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5297
65f38f15 5298 htab = ppc_hash_table (info);
4dfe6ac6
NC
5299 if (htab == NULL)
5300 return FALSE;
5301
3a71aa26
AM
5302 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5303 FALSE, FALSE, TRUE);
5304 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5305 FALSE, FALSE, TRUE);
0ffa91dd 5306 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5307 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5308 sreloc = NULL;
1e2f5b6e 5309 opd_sym_map = NULL;
f6c7c3e8
AM
5310 if (ppc64_elf_section_data (sec) != NULL
5311 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5312 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5313
5314 rel_end = relocs + sec->reloc_count;
5315 for (rel = relocs; rel < rel_end; rel++)
5316 {
5317 unsigned long r_symndx;
5318 struct elf_link_hash_entry *h;
04c9666a 5319 enum elf_ppc64_reloc_type r_type;
727fc41e 5320 int tls_type;
7c8fe5c4 5321 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 5322 struct plt_entry **ifunc;
5bd4f169
AM
5323
5324 r_symndx = ELF64_R_SYM (rel->r_info);
5325 if (r_symndx < symtab_hdr->sh_info)
5326 h = NULL;
5327 else
973a3492
L
5328 {
5329 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5330 h = elf_follow_link (h);
81fbe831
AM
5331
5332 /* PR15323, ref flags aren't set for references in the same
5333 object. */
5334 h->root.non_ir_ref = 1;
1c865ab2
AM
5335
5336 if (h == htab->elf.hgot)
5337 sec->has_toc_reloc = 1;
973a3492 5338 }
5bd4f169 5339
727fc41e 5340 tls_type = 0;
e054468f 5341 ifunc = NULL;
25f23106
AM
5342 if (h != NULL)
5343 {
5344 if (h->type == STT_GNU_IFUNC)
5345 {
5346 h->needs_plt = 1;
5347 ifunc = &h->plt.plist;
5348 }
5349 }
5350 else
5351 {
5352 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5353 abfd, r_symndx);
5354 if (isym == NULL)
5355 return FALSE;
5356
5357 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5358 {
5359 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5360 rel->r_addend, PLT_IFUNC);
5361 if (ifunc == NULL)
5362 return FALSE;
5363 }
5364 }
4ce794b7 5365 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
5366 if (is_branch_reloc (r_type))
5367 {
5368 if (h != NULL && (h == tga || h == dottga))
5369 {
5370 if (rel != relocs
5371 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5372 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5373 /* We have a new-style __tls_get_addr call with a marker
5374 reloc. */
5375 ;
5376 else
5377 /* Mark this section as having an old-style call. */
5378 sec->has_tls_get_addr_call = 1;
5379 }
727fc41e 5380
e054468f 5381 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5382 if (ifunc != NULL
5383 && !update_plt_info (abfd, ifunc, rel->r_addend))
5384 return FALSE;
5385 }
727fc41e 5386
a33d1f77 5387 switch (r_type)
5bd4f169 5388 {
727fc41e
AM
5389 case R_PPC64_TLSGD:
5390 case R_PPC64_TLSLD:
5391 /* These special tls relocs tie a call to __tls_get_addr with
5392 its parameter symbol. */
5393 break;
5394
411e1bfb
AM
5395 case R_PPC64_GOT_TLSLD16:
5396 case R_PPC64_GOT_TLSLD16_LO:
5397 case R_PPC64_GOT_TLSLD16_HI:
5398 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5399 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5400 goto dogottls;
5401
5402 case R_PPC64_GOT_TLSGD16:
5403 case R_PPC64_GOT_TLSGD16_LO:
5404 case R_PPC64_GOT_TLSGD16_HI:
5405 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5406 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5407 goto dogottls;
5408
5409 case R_PPC64_GOT_TPREL16_DS:
5410 case R_PPC64_GOT_TPREL16_LO_DS:
5411 case R_PPC64_GOT_TPREL16_HI:
5412 case R_PPC64_GOT_TPREL16_HA:
0e1862bb 5413 if (bfd_link_pic (info))
411e1bfb
AM
5414 info->flags |= DF_STATIC_TLS;
5415 tls_type = TLS_TLS | TLS_TPREL;
5416 goto dogottls;
5417
5418 case R_PPC64_GOT_DTPREL16_DS:
5419 case R_PPC64_GOT_DTPREL16_LO_DS:
5420 case R_PPC64_GOT_DTPREL16_HI:
5421 case R_PPC64_GOT_DTPREL16_HA:
5422 tls_type = TLS_TLS | TLS_DTPREL;
5423 dogottls:
5424 sec->has_tls_reloc = 1;
5425 /* Fall thru */
5426
5bd4f169 5427 case R_PPC64_GOT16:
5bd4f169 5428 case R_PPC64_GOT16_DS:
65f38f15
AM
5429 case R_PPC64_GOT16_HA:
5430 case R_PPC64_GOT16_HI:
5431 case R_PPC64_GOT16_LO:
5bd4f169 5432 case R_PPC64_GOT16_LO_DS:
65f38f15 5433 /* This symbol requires a global offset table entry. */
4c52953f 5434 sec->has_toc_reloc = 1;
33c0ec9d
AM
5435 if (r_type == R_PPC64_GOT_TLSLD16
5436 || r_type == R_PPC64_GOT_TLSGD16
5437 || r_type == R_PPC64_GOT_TPREL16_DS
5438 || r_type == R_PPC64_GOT_DTPREL16_DS
5439 || r_type == R_PPC64_GOT16
5440 || r_type == R_PPC64_GOT16_DS)
5441 {
5442 htab->do_multi_toc = 1;
d77c8a4b 5443 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5444 }
5445
e717da7e
AM
5446 if (ppc64_elf_tdata (abfd)->got == NULL
5447 && !create_got_section (abfd, info))
b34976b6 5448 return FALSE;
5bd4f169
AM
5449
5450 if (h != NULL)
5451 {
411e1bfb
AM
5452 struct ppc_link_hash_entry *eh;
5453 struct got_entry *ent;
65f38f15 5454
411e1bfb
AM
5455 eh = (struct ppc_link_hash_entry *) h;
5456 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5457 if (ent->addend == rel->r_addend
e717da7e 5458 && ent->owner == abfd
411e1bfb
AM
5459 && ent->tls_type == tls_type)
5460 break;
5461 if (ent == NULL)
5bd4f169 5462 {
411e1bfb 5463 bfd_size_type amt = sizeof (*ent);
4ce794b7 5464 ent = bfd_alloc (abfd, amt);
411e1bfb 5465 if (ent == NULL)
b34976b6 5466 return FALSE;
411e1bfb
AM
5467 ent->next = eh->elf.got.glist;
5468 ent->addend = rel->r_addend;
e717da7e 5469 ent->owner = abfd;
411e1bfb 5470 ent->tls_type = tls_type;
927be08e 5471 ent->is_indirect = FALSE;
411e1bfb
AM
5472 ent->got.refcount = 0;
5473 eh->elf.got.glist = ent;
5bd4f169 5474 }
411e1bfb 5475 ent->got.refcount += 1;
e7b938ca 5476 eh->tls_mask |= tls_type;
5bd4f169 5477 }
411e1bfb
AM
5478 else
5479 /* This is a global offset table entry for a local symbol. */
5480 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5481 rel->r_addend, tls_type))
5482 return FALSE;
a345bc8d
AM
5483
5484 /* We may also need a plt entry if the symbol turns out to be
5485 an ifunc. */
0e1862bb 5486 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5487 {
5488 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5489 return FALSE;
5490 }
5bd4f169
AM
5491 break;
5492
5bd4f169 5493 case R_PPC64_PLT16_HA:
65f38f15
AM
5494 case R_PPC64_PLT16_HI:
5495 case R_PPC64_PLT16_LO:
5496 case R_PPC64_PLT32:
5497 case R_PPC64_PLT64:
5bd4f169 5498 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5499 actually build the entry in adjust_dynamic_symbol,
5500 because this might be a case of linking PIC code without
5501 linking in any dynamic objects, in which case we don't
5502 need to generate a procedure linkage table after all. */
5bd4f169
AM
5503 if (h == NULL)
5504 {
5505 /* It does not make sense to have a procedure linkage
3fad3c7c 5506 table entry for a local symbol. */
5bd4f169 5507 bfd_set_error (bfd_error_bad_value);
b34976b6 5508 return FALSE;
5bd4f169 5509 }
411e1bfb 5510 else
e054468f
AM
5511 {
5512 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5513 return FALSE;
5514 h->needs_plt = 1;
5515 if (h->root.root.string[0] == '.'
5516 && h->root.root.string[1] != '\0')
5517 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5518 }
5bd4f169
AM
5519 break;
5520
5521 /* The following relocations don't need to propagate the
5522 relocation if linking a shared object since they are
5523 section relative. */
5524 case R_PPC64_SECTOFF:
5525 case R_PPC64_SECTOFF_LO:
5526 case R_PPC64_SECTOFF_HI:
5527 case R_PPC64_SECTOFF_HA:
5528 case R_PPC64_SECTOFF_DS:
5529 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5530 case R_PPC64_DTPREL16:
5531 case R_PPC64_DTPREL16_LO:
5532 case R_PPC64_DTPREL16_HI:
5533 case R_PPC64_DTPREL16_HA:
5534 case R_PPC64_DTPREL16_DS:
5535 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5536 case R_PPC64_DTPREL16_HIGH:
5537 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5538 case R_PPC64_DTPREL16_HIGHER:
5539 case R_PPC64_DTPREL16_HIGHERA:
5540 case R_PPC64_DTPREL16_HIGHEST:
5541 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5542 break;
5543
ad8e1ba5 5544 /* Nor do these. */
25f23106
AM
5545 case R_PPC64_REL16:
5546 case R_PPC64_REL16_LO:
5547 case R_PPC64_REL16_HI:
5548 case R_PPC64_REL16_HA:
a680de9a 5549 case R_PPC64_REL16DX_HA:
25f23106
AM
5550 break;
5551
45965137
AM
5552 /* Not supported as a dynamic relocation. */
5553 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5554 if (bfd_link_pic (info))
45965137
AM
5555 {
5556 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5557 ppc_howto_init ();
5558 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5559 "in shared libraries and PIEs.\n"),
5560 abfd, sec, rel->r_offset,
5561 ppc64_elf_howto_table[r_type]->name);
5562 bfd_set_error (bfd_error_bad_value);
5563 return FALSE;
5564 }
5565 break;
5566
ad8e1ba5 5567 case R_PPC64_TOC16:
33c0ec9d
AM
5568 case R_PPC64_TOC16_DS:
5569 htab->do_multi_toc = 1;
d77c8a4b 5570 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5571 case R_PPC64_TOC16_LO:
5572 case R_PPC64_TOC16_HI:
5573 case R_PPC64_TOC16_HA:
ad8e1ba5 5574 case R_PPC64_TOC16_LO_DS:
4c52953f 5575 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5576 break;
5577
5bd4f169
AM
5578 /* This relocation describes the C++ object vtable hierarchy.
5579 Reconstruct it for later use during GC. */
5580 case R_PPC64_GNU_VTINHERIT:
c152c796 5581 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5582 return FALSE;
5bd4f169
AM
5583 break;
5584
5585 /* This relocation describes which C++ vtable entries are actually
5586 used. Record for later use during GC. */
5587 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5588 BFD_ASSERT (h != NULL);
5589 if (h != NULL
5590 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5591 return FALSE;
5bd4f169
AM
5592 break;
5593
721956f4
AM
5594 case R_PPC64_REL14:
5595 case R_PPC64_REL14_BRTAKEN:
5596 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5597 {
5598 asection *dest = NULL;
5599
5600 /* Heuristic: If jumping outside our section, chances are
5601 we are going to need a stub. */
5602 if (h != NULL)
5603 {
5604 /* If the sym is weak it may be overridden later, so
5605 don't assume we know where a weak sym lives. */
5606 if (h->root.type == bfd_link_hash_defined)
5607 dest = h->root.u.def.section;
5608 }
5609 else
87d72d41
AM
5610 {
5611 Elf_Internal_Sym *isym;
5612
5613 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5614 abfd, r_symndx);
5615 if (isym == NULL)
5616 return FALSE;
5617
5618 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5619 }
5620
220c76dd 5621 if (dest != sec)
7c8fe5c4 5622 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5623 }
721956f4
AM
5624 /* Fall through. */
5625
5d1634d7 5626 case R_PPC64_REL24:
e054468f 5627 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5628 {
5629 /* We may need a .plt entry if the function this reloc
5630 refers to is in a shared lib. */
e054468f 5631 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5632 return FALSE;
e054468f
AM
5633 h->needs_plt = 1;
5634 if (h->root.root.string[0] == '.'
5635 && h->root.root.string[1] != '\0')
5636 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5637 if (h == tga || h == dottga)
411e1bfb 5638 sec->has_tls_reloc = 1;
411e1bfb
AM
5639 }
5640 break;
5641
5642 case R_PPC64_TPREL64:
5643 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
0e1862bb 5644 if (bfd_link_pic (info))
411e1bfb
AM
5645 info->flags |= DF_STATIC_TLS;
5646 goto dotlstoc;
5647
5648 case R_PPC64_DTPMOD64:
5649 if (rel + 1 < rel_end
5650 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5651 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5652 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5653 else
951fd09b 5654 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5655 goto dotlstoc;
5656
5657 case R_PPC64_DTPREL64:
5658 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5659 if (rel != relocs
5660 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5661 && rel[-1].r_offset == rel->r_offset - 8)
5662 /* This is the second reloc of a dtpmod, dtprel pair.
5663 Don't mark with TLS_DTPREL. */
5664 goto dodyn;
5665
5666 dotlstoc:
5667 sec->has_tls_reloc = 1;
5668 if (h != NULL)
5669 {
5670 struct ppc_link_hash_entry *eh;
5671 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5672 eh->tls_mask |= tls_type;
411e1bfb
AM
5673 }
5674 else
5675 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5676 rel->r_addend, tls_type))
5677 return FALSE;
5678
7c8fe5c4
AM
5679 ppc64_sec = ppc64_elf_section_data (sec);
5680 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5681 {
3a71aa26
AM
5682 bfd_size_type amt;
5683
e7b938ca 5684 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5685 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5686 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5687 if (ppc64_sec->u.toc.symndx == NULL)
5688 return FALSE;
5689 amt = sec->size * sizeof (bfd_vma) / 8;
5690 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5691 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5692 return FALSE;
7c8fe5c4
AM
5693 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5694 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5695 }
5696 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5697 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5698 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5699
5700 /* Mark the second slot of a GD or LD entry.
5701 -1 to indicate GD and -2 to indicate LD. */
5702 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5703 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5704 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5705 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5706 goto dodyn;
5707
5708 case R_PPC64_TPREL16:
5709 case R_PPC64_TPREL16_LO:
5710 case R_PPC64_TPREL16_HI:
5711 case R_PPC64_TPREL16_HA:
5712 case R_PPC64_TPREL16_DS:
5713 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5714 case R_PPC64_TPREL16_HIGH:
5715 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5716 case R_PPC64_TPREL16_HIGHER:
5717 case R_PPC64_TPREL16_HIGHERA:
5718 case R_PPC64_TPREL16_HIGHEST:
5719 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 5720 if (bfd_link_pic (info))
411e1bfb 5721 {
afb93314 5722 info->flags |= DF_STATIC_TLS;
411e1bfb 5723 goto dodyn;
5d1634d7
AM
5724 }
5725 break;
5726
e86ce104 5727 case R_PPC64_ADDR64:
1e2f5b6e 5728 if (opd_sym_map != NULL
1e2f5b6e 5729 && rel + 1 < rel_end
4ce794b7 5730 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5731 {
8387904d
AM
5732 if (h != NULL)
5733 {
5734 if (h->root.root.string[0] == '.'
5735 && h->root.root.string[1] != 0
b31867b6 5736 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5737 ;
5738 else
5739 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5740 }
5741 else
5742 {
5743 asection *s;
87d72d41 5744 Elf_Internal_Sym *isym;
1e2f5b6e 5745
87d72d41
AM
5746 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5747 abfd, r_symndx);
5748 if (isym == NULL)
8387904d 5749 return FALSE;
87d72d41
AM
5750
5751 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5752 if (s != NULL && s != sec)
51aecdc5 5753 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5754 }
1e2f5b6e 5755 }
e86ce104
AM
5756 /* Fall through. */
5757
65f38f15
AM
5758 case R_PPC64_ADDR16:
5759 case R_PPC64_ADDR16_DS:
5760 case R_PPC64_ADDR16_HA:
5761 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5762 case R_PPC64_ADDR16_HIGH:
5763 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5764 case R_PPC64_ADDR16_HIGHER:
5765 case R_PPC64_ADDR16_HIGHERA:
5766 case R_PPC64_ADDR16_HIGHEST:
5767 case R_PPC64_ADDR16_HIGHESTA:
5768 case R_PPC64_ADDR16_LO:
5769 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5770 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5771 && rel->r_addend == 0)
5772 {
5773 /* We may need a .plt entry if this reloc refers to a
5774 function in a shared lib. */
5775 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5776 return FALSE;
5777 h->pointer_equality_needed = 1;
5778 }
5779 /* Fall through. */
5780
5781 case R_PPC64_REL30:
5782 case R_PPC64_REL32:
5783 case R_PPC64_REL64:
5784 case R_PPC64_ADDR14:
5785 case R_PPC64_ADDR14_BRNTAKEN:
5786 case R_PPC64_ADDR14_BRTAKEN:
65f38f15 5787 case R_PPC64_ADDR24:
65f38f15 5788 case R_PPC64_ADDR32:
65f38f15
AM
5789 case R_PPC64_UADDR16:
5790 case R_PPC64_UADDR32:
5791 case R_PPC64_UADDR64:
5bd4f169 5792 case R_PPC64_TOC:
0e1862bb 5793 if (h != NULL && !bfd_link_pic (info))
81848ca0 5794 /* We may need a copy reloc. */
f5385ebf 5795 h->non_got_ref = 1;
81848ca0 5796
41bd81ab 5797 /* Don't propagate .opd relocs. */
1e2f5b6e 5798 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5799 break;
e86ce104 5800
65f38f15
AM
5801 /* If we are creating a shared library, and this is a reloc
5802 against a global symbol, or a non PC relative reloc
5803 against a local symbol, then we need to copy the reloc
5804 into the shared library. However, if we are linking with
5805 -Bsymbolic, we do not need to copy a reloc against a
5806 global symbol which is defined in an object we are
5807 including in the link (i.e., DEF_REGULAR is set). At
5808 this point we have not seen all the input files, so it is
5809 possible that DEF_REGULAR is not set now but will be set
5810 later (it is never cleared). In case of a weak definition,
5811 DEF_REGULAR may be cleared later by a strong definition in
5812 a shared library. We account for that possibility below by
f4656909 5813 storing information in the dyn_relocs field of the hash
65f38f15
AM
5814 table entry. A similar situation occurs when creating
5815 shared libraries and symbol visibility changes render the
5816 symbol local.
5817
5818 If on the other hand, we are creating an executable, we
5819 may need to keep relocations for symbols satisfied by a
5820 dynamic library if we manage to avoid copy relocs for the
5821 symbol. */
411e1bfb 5822 dodyn:
0e1862bb 5823 if ((bfd_link_pic (info)
1d483afe 5824 && (must_be_dyn_reloc (info, r_type)
65f38f15 5825 || (h != NULL
198f1157 5826 && (!SYMBOLIC_BIND (info, h)
65f38f15 5827 || h->root.type == bfd_link_hash_defweak
f5385ebf 5828 || !h->def_regular))))
f4656909 5829 || (ELIMINATE_COPY_RELOCS
0e1862bb 5830 && !bfd_link_pic (info)
65f38f15
AM
5831 && h != NULL
5832 && (h->root.type == bfd_link_hash_defweak
25f23106 5833 || !h->def_regular))
0e1862bb 5834 || (!bfd_link_pic (info)
25f23106 5835 && ifunc != NULL))
5bd4f169 5836 {
65f38f15
AM
5837 /* We must copy these reloc types into the output file.
5838 Create a reloc section in dynobj and make room for
5839 this reloc. */
5bd4f169
AM
5840 if (sreloc == NULL)
5841 {
83bac4b0
NC
5842 sreloc = _bfd_elf_make_dynamic_reloc_section
5843 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5844
5bd4f169 5845 if (sreloc == NULL)
83bac4b0 5846 return FALSE;
5bd4f169
AM
5847 }
5848
65f38f15
AM
5849 /* If this is a global symbol, we count the number of
5850 relocations we need for this symbol. */
5851 if (h != NULL)
5852 {
19e08130
AM
5853 struct elf_dyn_relocs *p;
5854 struct elf_dyn_relocs **head;
5855
ec338859 5856 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5857 p = *head;
5858 if (p == NULL || p->sec != sec)
5859 {
5860 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5861 if (p == NULL)
5862 return FALSE;
5863 p->next = *head;
5864 *head = p;
5865 p->sec = sec;
5866 p->count = 0;
5867 p->pc_count = 0;
5868 }
5869 p->count += 1;
5870 if (!must_be_dyn_reloc (info, r_type))
5871 p->pc_count += 1;
65f38f15
AM
5872 }
5873 else
5874 {
ec338859
AM
5875 /* Track dynamic relocs needed for local syms too.
5876 We really need local syms available to do this
5877 easily. Oh well. */
19e08130
AM
5878 struct ppc_dyn_relocs *p;
5879 struct ppc_dyn_relocs **head;
5880 bfd_boolean is_ifunc;
ec338859 5881 asection *s;
6edfbbad 5882 void *vpp;
87d72d41 5883 Elf_Internal_Sym *isym;
6edfbbad 5884
87d72d41
AM
5885 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5886 abfd, r_symndx);
5887 if (isym == NULL)
b34976b6 5888 return FALSE;
ec338859 5889
87d72d41
AM
5890 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5891 if (s == NULL)
5892 s = sec;
5893
6edfbbad 5894 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5895 head = (struct ppc_dyn_relocs **) vpp;
5896 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5897 p = *head;
5898 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5899 p = p->next;
5900 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5901 {
5902 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5903 if (p == NULL)
5904 return FALSE;
5905 p->next = *head;
5906 *head = p;
5907 p->sec = sec;
5908 p->ifunc = is_ifunc;
5909 p->count = 0;
5910 }
5911 p->count += 1;
ec338859 5912 }
65f38f15 5913 }
5bd4f169 5914 break;
65f38f15
AM
5915
5916 default:
96e0dda4 5917 break;
5bd4f169
AM
5918 }
5919 }
5920
b34976b6 5921 return TRUE;
5bd4f169
AM
5922}
5923
ee67d69a
AM
5924/* Merge backend specific data from an object file to the output
5925 object file when linking. */
5926
5927static bfd_boolean
5928ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5929{
5930 unsigned long iflags, oflags;
5931
5932 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5933 return TRUE;
5934
5935 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5936 return TRUE;
5937
5938 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5939 return FALSE;
5940
5941 iflags = elf_elfheader (ibfd)->e_flags;
5942 oflags = elf_elfheader (obfd)->e_flags;
5943
f6c7c3e8 5944 if (iflags & ~EF_PPC64_ABI)
ee67d69a
AM
5945 {
5946 (*_bfd_error_handler)
5947 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5948 bfd_set_error (bfd_error_bad_value);
5949 return FALSE;
5950 }
f6c7c3e8 5951 else if (iflags != oflags && iflags != 0)
ee67d69a
AM
5952 {
5953 (*_bfd_error_handler)
5954 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5955 ibfd, iflags, oflags);
5956 bfd_set_error (bfd_error_bad_value);
5957 return FALSE;
5958 }
5959
5960 /* Merge Tag_compatibility attributes and any common GNU ones. */
5961 _bfd_elf_merge_object_attributes (ibfd, obfd);
5962
5963 return TRUE;
5964}
5965
5966static bfd_boolean
5967ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5968{
5969 /* Print normal ELF private data. */
5970 _bfd_elf_print_private_bfd_data (abfd, ptr);
5971
5972 if (elf_elfheader (abfd)->e_flags != 0)
5973 {
5974 FILE *file = ptr;
5975
5976 /* xgettext:c-format */
5977 fprintf (file, _("private flags = 0x%lx:"),
5978 elf_elfheader (abfd)->e_flags);
5979
5980 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5981 fprintf (file, _(" [abiv%ld]"),
5982 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5983 fputc ('\n', file);
5984 }
5985
5986 return TRUE;
5987}
5988
8387904d 5989/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5990 of the code entry point, and its section, which must be in the same
5991 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5992
5993static bfd_vma
5994opd_entry_value (asection *opd_sec,
5995 bfd_vma offset,
5996 asection **code_sec,
aef36ac1
AM
5997 bfd_vma *code_off,
5998 bfd_boolean in_code_sec)
8387904d
AM
5999{
6000 bfd *opd_bfd = opd_sec->owner;
8860955f 6001 Elf_Internal_Rela *relocs;
8387904d 6002 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6003 bfd_vma val;
8387904d 6004
9f296da3
AM
6005 /* No relocs implies we are linking a --just-symbols object, or looking
6006 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6007 if (opd_sec->reloc_count == 0)
6008 {
729eabd5 6009 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6010
729eabd5
AM
6011 if (contents == NULL)
6012 {
6013 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6014 return (bfd_vma) -1;
6015 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6016 }
ee1e4ede 6017
dbb3fbbb 6018 /* PR 17512: file: 64b9dfbb. */
451dfd38 6019 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6020 return (bfd_vma) -1;
6021
729eabd5 6022 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6023 if (code_sec != NULL)
6024 {
6025 asection *sec, *likely = NULL;
ee1e4ede 6026
aef36ac1 6027 if (in_code_sec)
4b85d634 6028 {
aef36ac1
AM
6029 sec = *code_sec;
6030 if (sec->vma <= val
6031 && val < sec->vma + sec->size)
6032 likely = sec;
6033 else
6034 val = -1;
6035 }
6036 else
6037 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6038 if (sec->vma <= val
6039 && (sec->flags & SEC_LOAD) != 0
6040 && (sec->flags & SEC_ALLOC) != 0)
6041 likely = sec;
6042 if (likely != NULL)
6043 {
6044 *code_sec = likely;
6045 if (code_off != NULL)
6046 *code_off = val - likely->vma;
4b85d634
AM
6047 }
6048 }
aef36ac1 6049 return val;
4b85d634
AM
6050 }
6051
0c8d6e5c 6052 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6053
729eabd5 6054 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6055 if (relocs == NULL)
6056 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6057 /* PR 17512: file: df8e1fd6. */
6058 if (relocs == NULL)
6059 return (bfd_vma) -1;
645ea6a9 6060
8387904d 6061 /* Go find the opd reloc at the sym address. */
8860955f 6062 lo = relocs;
8387904d 6063 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6064 val = (bfd_vma) -1;
8387904d
AM
6065 while (lo < hi)
6066 {
6067 look = lo + (hi - lo) / 2;
6068 if (look->r_offset < offset)
6069 lo = look + 1;
6070 else if (look->r_offset > offset)
6071 hi = look;
6072 else
6073 {
0ffa91dd
NC
6074 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6075
8387904d
AM
6076 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6077 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6078 {
6079 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6080 asection *sec = NULL;
8387904d 6081
b53dfeb2
AM
6082 if (symndx >= symtab_hdr->sh_info
6083 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6084 {
6085 struct elf_link_hash_entry **sym_hashes;
6086 struct elf_link_hash_entry *rh;
6087
6088 sym_hashes = elf_sym_hashes (opd_bfd);
6089 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6090 if (rh != NULL)
6091 {
6092 rh = elf_follow_link (rh);
bb854a36
AM
6093 if (rh->root.type != bfd_link_hash_defined
6094 && rh->root.type != bfd_link_hash_defweak)
6095 break;
6096 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6097 {
bb854a36
AM
6098 val = rh->root.u.def.value;
6099 sec = rh->root.u.def.section;
b53dfeb2
AM
6100 }
6101 }
6102 }
6103
6104 if (sec == NULL)
6105 {
6106 Elf_Internal_Sym *sym;
6107
6108 if (symndx < symtab_hdr->sh_info)
6109 {
6110 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6111 if (sym == NULL)
6112 {
6113 size_t symcnt = symtab_hdr->sh_info;
6114 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6115 symcnt, 0,
6116 NULL, NULL, NULL);
6117 if (sym == NULL)
6118 break;
6119 symtab_hdr->contents = (bfd_byte *) sym;
6120 }
6121 sym += symndx;
128205bb
AM
6122 }
6123 else
6124 {
b53dfeb2
AM
6125 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6126 1, symndx,
6127 NULL, NULL, NULL);
128205bb
AM
6128 if (sym == NULL)
6129 break;
128205bb 6130 }
b53dfeb2
AM
6131 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6132 if (sec == NULL)
6133 break;
6134 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6135 val = sym->st_value;
8387904d 6136 }
b53dfeb2 6137
8387904d
AM
6138 val += look->r_addend;
6139 if (code_off != NULL)
6140 *code_off = val;
6141 if (code_sec != NULL)
aef36ac1
AM
6142 {
6143 if (in_code_sec && *code_sec != sec)
6144 return -1;
6145 else
6146 *code_sec = sec;
6147 }
b53dfeb2 6148 if (sec->output_section != NULL)
8387904d 6149 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6150 }
6151 break;
6152 }
6153 }
645ea6a9 6154
645ea6a9 6155 return val;
8387904d
AM
6156}
6157
aef36ac1
AM
6158/* If the ELF symbol SYM might be a function in SEC, return the
6159 function size and set *CODE_OFF to the function's entry point,
6160 otherwise return zero. */
9f296da3 6161
aef36ac1
AM
6162static bfd_size_type
6163ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6164 bfd_vma *code_off)
9f296da3 6165{
aef36ac1
AM
6166 bfd_size_type size;
6167
6168 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6169 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6170 return 0;
6171
6172 size = 0;
6173 if (!(sym->flags & BSF_SYNTHETIC))
6174 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6175
6176 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6177 {
b07bca4e
AM
6178 struct _opd_sec_data *opd = get_opd_info (sym->section);
6179 bfd_vma symval = sym->value;
6180
6181 if (opd != NULL
6182 && opd->adjust != NULL
6183 && elf_section_data (sym->section)->relocs != NULL)
6184 {
6185 /* opd_entry_value will use cached relocs that have been
6186 adjusted, but with raw symbols. That means both local
6187 and global symbols need adjusting. */
6188 long adjust = opd->adjust[OPD_NDX (symval)];
6189 if (adjust == -1)
6190 return 0;
6191 symval += adjust;
6192 }
6193
6194 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6195 &sec, code_off, TRUE) == (bfd_vma) -1)
6196 return 0;
6197 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6198 symbol. This size has nothing to do with the code size of the
6199 function, which is what we're supposed to return, but the
6200 code size isn't available without looking up the dot-sym.
6201 However, doing that would be a waste of time particularly
6202 since elf_find_function will look at the dot-sym anyway.
6203 Now, elf_find_function will keep the largest size of any
6204 function sym found at the code address of interest, so return
6205 1 here to avoid it incorrectly caching a larger function size
6206 for a small function. This does mean we return the wrong
6207 size for a new-ABI function of size 24, but all that does is
6208 disable caching for such functions. */
6209 if (size == 24)
6210 size = 1;
9f296da3 6211 }
aef36ac1
AM
6212 else
6213 {
6214 if (sym->section != sec)
6215 return 0;
6216 *code_off = sym->value;
6217 }
6218 if (size == 0)
6219 size = 1;
6220 return size;
9f296da3
AM
6221}
6222
854b41e7
AM
6223/* Return true if symbol is defined in a regular object file. */
6224
6225static bfd_boolean
6226is_static_defined (struct elf_link_hash_entry *h)
6227{
6228 return ((h->root.type == bfd_link_hash_defined
6229 || h->root.type == bfd_link_hash_defweak)
6230 && h->root.u.def.section != NULL
6231 && h->root.u.def.section->output_section != NULL);
6232}
6233
b31867b6
AM
6234/* If FDH is a function descriptor symbol, return the associated code
6235 entry symbol if it is defined. Return NULL otherwise. */
6236
6237static struct ppc_link_hash_entry *
6238defined_code_entry (struct ppc_link_hash_entry *fdh)
6239{
6240 if (fdh->is_func_descriptor)
6241 {
6242 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6243 if (fh->elf.root.type == bfd_link_hash_defined
6244 || fh->elf.root.type == bfd_link_hash_defweak)
6245 return fh;
6246 }
6247 return NULL;
6248}
6249
6250/* If FH is a function code entry symbol, return the associated
6251 function descriptor symbol if it is defined. Return NULL otherwise. */
6252
6253static struct ppc_link_hash_entry *
6254defined_func_desc (struct ppc_link_hash_entry *fh)
6255{
6256 if (fh->oh != NULL
6257 && fh->oh->is_func_descriptor)
6258 {
6259 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6260 if (fdh->elf.root.type == bfd_link_hash_defined
6261 || fdh->elf.root.type == bfd_link_hash_defweak)
6262 return fdh;
6263 }
6264 return NULL;
6265}
6266
74f0fb50
AM
6267/* Mark all our entry sym sections, both opd and code section. */
6268
6269static void
6270ppc64_elf_gc_keep (struct bfd_link_info *info)
6271{
6272 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6273 struct bfd_sym_chain *sym;
6274
4dfe6ac6
NC
6275 if (htab == NULL)
6276 return;
6277
74f0fb50
AM
6278 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6279 {
b31867b6 6280 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6281 asection *sec;
6282
6283 eh = (struct ppc_link_hash_entry *)
b31867b6 6284 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6285 if (eh == NULL)
6286 continue;
6287 if (eh->elf.root.type != bfd_link_hash_defined
6288 && eh->elf.root.type != bfd_link_hash_defweak)
6289 continue;
6290
b31867b6
AM
6291 fh = defined_code_entry (eh);
6292 if (fh != NULL)
74f0fb50 6293 {
b31867b6 6294 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6295 sec->flags |= SEC_KEEP;
6296 }
6297 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6298 && opd_entry_value (eh->elf.root.u.def.section,
6299 eh->elf.root.u.def.value,
aef36ac1 6300 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6301 sec->flags |= SEC_KEEP;
6302
6303 sec = eh->elf.root.u.def.section;
6304 sec->flags |= SEC_KEEP;
6305 }
6306}
6307
64d03ab5
AM
6308/* Mark sections containing dynamically referenced symbols. When
6309 building shared libraries, we must assume that any visible symbol is
6310 referenced. */
6311
6312static bfd_boolean
6313ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6314{
6315 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6316 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6317 struct ppc_link_hash_entry *fdh;
b407645f 6318 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6319
64d03ab5 6320 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6321 fdh = defined_func_desc (eh);
6322 if (fdh != NULL)
6323 eh = fdh;
64d03ab5
AM
6324
6325 if ((eh->elf.root.type == bfd_link_hash_defined
6326 || eh->elf.root.type == bfd_link_hash_defweak)
6327 && (eh->elf.ref_dynamic
1c9177d9 6328 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6329 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6330 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6331 && (!bfd_link_executable (info)
b407645f
AM
6332 || info->export_dynamic
6333 || (eh->elf.dynamic
6334 && d != NULL
6335 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
4c58e0d8
AM
6336 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6337 || !bfd_hide_sym_by_version (info->version_info,
6338 eh->elf.root.root.string)))))
64d03ab5
AM
6339 {
6340 asection *code_sec;
b31867b6 6341 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6342
6343 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6344
6345 /* Function descriptor syms cause the associated
6346 function code sym section to be marked. */
b31867b6
AM
6347 fh = defined_code_entry (eh);
6348 if (fh != NULL)
6349 {
6350 code_sec = fh->elf.root.u.def.section;
6351 code_sec->flags |= SEC_KEEP;
6352 }
64d03ab5
AM
6353 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6354 && opd_entry_value (eh->elf.root.u.def.section,
6355 eh->elf.root.u.def.value,
aef36ac1 6356 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6357 code_sec->flags |= SEC_KEEP;
6358 }
6359
6360 return TRUE;
6361}
6362
5bd4f169
AM
6363/* Return the section that should be marked against GC for a given
6364 relocation. */
6365
6366static asection *
4ce794b7 6367ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6368 struct bfd_link_info *info,
4ce794b7
AM
6369 Elf_Internal_Rela *rel,
6370 struct elf_link_hash_entry *h,
6371 Elf_Internal_Sym *sym)
5bd4f169 6372{
ccfa59ea
AM
6373 asection *rsec;
6374
ccfa59ea
AM
6375 /* Syms return NULL if we're marking .opd, so we avoid marking all
6376 function sections, as all functions are referenced in .opd. */
6377 rsec = NULL;
6378 if (get_opd_info (sec) != NULL)
6379 return rsec;
1e2f5b6e 6380
5bd4f169
AM
6381 if (h != NULL)
6382 {
04c9666a 6383 enum elf_ppc64_reloc_type r_type;
b31867b6 6384 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6385
4ce794b7 6386 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6387 switch (r_type)
5bd4f169
AM
6388 {
6389 case R_PPC64_GNU_VTINHERIT:
6390 case R_PPC64_GNU_VTENTRY:
6391 break;
6392
6393 default:
6394 switch (h->root.type)
6395 {
6396 case bfd_link_hash_defined:
6397 case bfd_link_hash_defweak:
ccfa59ea 6398 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6399 fdh = defined_func_desc (eh);
6400 if (fdh != NULL)
6401 eh = fdh;
1e2f5b6e
AM
6402
6403 /* Function descriptor syms cause the associated
6404 function code sym section to be marked. */
b31867b6
AM
6405 fh = defined_code_entry (eh);
6406 if (fh != NULL)
ccfa59ea
AM
6407 {
6408 /* They also mark their opd section. */
74f0fb50 6409 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6410
b31867b6 6411 rsec = fh->elf.root.u.def.section;
ccfa59ea 6412 }
8387904d
AM
6413 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6414 && opd_entry_value (eh->elf.root.u.def.section,
6415 eh->elf.root.u.def.value,
aef36ac1 6416 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6417 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6418 else
1e2f5b6e
AM
6419 rsec = h->root.u.def.section;
6420 break;
5bd4f169
AM
6421
6422 case bfd_link_hash_common:
1e2f5b6e
AM
6423 rsec = h->root.u.c.p->section;
6424 break;
5bd4f169
AM
6425
6426 default:
fb34365b 6427 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6428 }
6429 }
6430 }
6431 else
6432 {
74f0fb50 6433 struct _opd_sec_data *opd;
1e2f5b6e
AM
6434
6435 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6436 opd = get_opd_info (rsec);
6437 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6438 {
74f0fb50 6439 rsec->gc_mark = 1;
ccfa59ea 6440
51aecdc5 6441 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6442 }
5bd4f169
AM
6443 }
6444
1e2f5b6e 6445 return rsec;
5bd4f169
AM
6446}
6447
65f38f15
AM
6448/* Update the .got, .plt. and dynamic reloc reference counts for the
6449 section being removed. */
5bd4f169 6450
b34976b6 6451static bfd_boolean
4ce794b7
AM
6452ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6453 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6454{
411e1bfb 6455 struct ppc_link_hash_table *htab;
5bd4f169
AM
6456 Elf_Internal_Shdr *symtab_hdr;
6457 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6458 struct got_entry **local_got_ents;
5bd4f169 6459 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6460
0e1862bb 6461 if (bfd_link_relocatable (info))
7dda2462
TG
6462 return TRUE;
6463
680a3378
AM
6464 if ((sec->flags & SEC_ALLOC) == 0)
6465 return TRUE;
6466
ec338859
AM
6467 elf_section_data (sec)->local_dynrel = NULL;
6468
411e1bfb 6469 htab = ppc_hash_table (info);
4dfe6ac6
NC
6470 if (htab == NULL)
6471 return FALSE;
6472
0ffa91dd 6473 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6474 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6475 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6476
6477 relend = relocs + sec->reloc_count;
6478 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6479 {
6480 unsigned long r_symndx;
04c9666a 6481 enum elf_ppc64_reloc_type r_type;
58ac9f71 6482 struct elf_link_hash_entry *h = NULL;
f961d9dd 6483 unsigned char tls_type = 0;
5bd4f169 6484
a33d1f77 6485 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6486 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6487 if (r_symndx >= symtab_hdr->sh_info)
6488 {
6489 struct ppc_link_hash_entry *eh;
6061a67d
AM
6490 struct elf_dyn_relocs **pp;
6491 struct elf_dyn_relocs *p;
58ac9f71
AM
6492
6493 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6494 h = elf_follow_link (h);
58ac9f71
AM
6495 eh = (struct ppc_link_hash_entry *) h;
6496
6497 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6498 if (p->sec == sec)
6499 {
6500 /* Everything must go for SEC. */
6501 *pp = p->next;
6502 break;
6503 }
6504 }
6505
e054468f
AM
6506 if (is_branch_reloc (r_type))
6507 {
6508 struct plt_entry **ifunc = NULL;
6509 if (h != NULL)
6510 {
6511 if (h->type == STT_GNU_IFUNC)
6512 ifunc = &h->plt.plist;
6513 }
6514 else if (local_got_ents != NULL)
6515 {
6516 struct plt_entry **local_plt = (struct plt_entry **)
6517 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 6518 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
6519 (local_plt + symtab_hdr->sh_info);
6520 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6521 ifunc = local_plt + r_symndx;
6522 }
6523 if (ifunc != NULL)
6524 {
6525 struct plt_entry *ent;
6526
6527 for (ent = *ifunc; ent != NULL; ent = ent->next)
6528 if (ent->addend == rel->r_addend)
6529 break;
6530 if (ent == NULL)
6531 abort ();
6532 if (ent->plt.refcount > 0)
6533 ent->plt.refcount -= 1;
6534 continue;
6535 }
6536 }
6537
a33d1f77
AM
6538 switch (r_type)
6539 {
411e1bfb
AM
6540 case R_PPC64_GOT_TLSLD16:
6541 case R_PPC64_GOT_TLSLD16_LO:
6542 case R_PPC64_GOT_TLSLD16_HI:
6543 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6544 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6545 goto dogot;
6546
6547 case R_PPC64_GOT_TLSGD16:
6548 case R_PPC64_GOT_TLSGD16_LO:
6549 case R_PPC64_GOT_TLSGD16_HI:
6550 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6551 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6552 goto dogot;
6553
6554 case R_PPC64_GOT_TPREL16_DS:
6555 case R_PPC64_GOT_TPREL16_LO_DS:
6556 case R_PPC64_GOT_TPREL16_HI:
6557 case R_PPC64_GOT_TPREL16_HA:
6558 tls_type = TLS_TLS | TLS_TPREL;
6559 goto dogot;
6560
6561 case R_PPC64_GOT_DTPREL16_DS:
6562 case R_PPC64_GOT_DTPREL16_LO_DS:
6563 case R_PPC64_GOT_DTPREL16_HI:
6564 case R_PPC64_GOT_DTPREL16_HA:
6565 tls_type = TLS_TLS | TLS_DTPREL;
6566 goto dogot;
6567
a33d1f77
AM
6568 case R_PPC64_GOT16:
6569 case R_PPC64_GOT16_DS:
6570 case R_PPC64_GOT16_HA:
6571 case R_PPC64_GOT16_HI:
6572 case R_PPC64_GOT16_LO:
6573 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6574 dogot:
6575 {
6576 struct got_entry *ent;
6577
58ac9f71
AM
6578 if (h != NULL)
6579 ent = h->got.glist;
411e1bfb
AM
6580 else
6581 ent = local_got_ents[r_symndx];
6582
6583 for (; ent != NULL; ent = ent->next)
6584 if (ent->addend == rel->r_addend
e717da7e 6585 && ent->owner == abfd
411e1bfb
AM
6586 && ent->tls_type == tls_type)
6587 break;
6588 if (ent == NULL)
6589 abort ();
6590 if (ent->got.refcount > 0)
6591 ent->got.refcount -= 1;
6592 }
a33d1f77 6593 break;
65f38f15 6594
a33d1f77
AM
6595 case R_PPC64_PLT16_HA:
6596 case R_PPC64_PLT16_HI:
6597 case R_PPC64_PLT16_LO:
6598 case R_PPC64_PLT32:
6599 case R_PPC64_PLT64:
721956f4
AM
6600 case R_PPC64_REL14:
6601 case R_PPC64_REL14_BRNTAKEN:
6602 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6603 case R_PPC64_REL24:
58ac9f71 6604 if (h != NULL)
5d1634d7 6605 {
411e1bfb
AM
6606 struct plt_entry *ent;
6607
411e1bfb
AM
6608 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6609 if (ent->addend == rel->r_addend)
6610 break;
7c6c1722 6611 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6612 ent->plt.refcount -= 1;
5d1634d7 6613 }
e86ce104 6614 break;
5d1634d7 6615
a33d1f77
AM
6616 default:
6617 break;
6618 }
6619 }
b34976b6 6620 return TRUE;
5bd4f169
AM
6621}
6622
deb0e272
AM
6623/* The maximum size of .sfpr. */
6624#define SFPR_MAX (218*4)
6625
6626struct sfpr_def_parms
6627{
699733f6
AM
6628 const char name[12];
6629 unsigned char lo, hi;
deb0e272
AM
6630 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6631 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6632};
6633
a4b6fadd
AM
6634/* Auto-generate _save*, _rest* functions in .sfpr.
6635 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6636 instead. */
deb0e272 6637
4dfe6ac6 6638static bfd_boolean
a4b6fadd
AM
6639sfpr_define (struct bfd_link_info *info,
6640 const struct sfpr_def_parms *parm,
6641 asection *stub_sec)
deb0e272
AM
6642{
6643 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6644 unsigned int i;
6645 size_t len = strlen (parm->name);
6646 bfd_boolean writing = FALSE;
699733f6 6647 char sym[16];
deb0e272 6648
4dfe6ac6
NC
6649 if (htab == NULL)
6650 return FALSE;
6651
deb0e272
AM
6652 memcpy (sym, parm->name, len);
6653 sym[len + 2] = 0;
6654
6655 for (i = parm->lo; i <= parm->hi; i++)
6656 {
a4b6fadd 6657 struct ppc_link_hash_entry *h;
deb0e272
AM
6658
6659 sym[len + 0] = i / 10 + '0';
6660 sym[len + 1] = i % 10 + '0';
a4b6fadd
AM
6661 h = (struct ppc_link_hash_entry *)
6662 elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6663 if (stub_sec != NULL)
deb0e272 6664 {
a4b6fadd
AM
6665 if (h != NULL
6666 && h->elf.root.type == bfd_link_hash_defined
6667 && h->elf.root.u.def.section == htab->sfpr)
6668 {
6669 struct elf_link_hash_entry *s;
6670 char buf[32];
6671 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6672 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6673 if (s == NULL)
6674 return FALSE;
6675 if (s->root.type == bfd_link_hash_new
6676 || (s->root.type = bfd_link_hash_defined
6677 && s->root.u.def.section == stub_sec))
6678 {
6679 s->root.type = bfd_link_hash_defined;
6680 s->root.u.def.section = stub_sec;
6681 s->root.u.def.value = (stub_sec->size
6682 + h->elf.root.u.def.value);
6683 s->ref_regular = 1;
6684 s->def_regular = 1;
6685 s->ref_regular_nonweak = 1;
6686 s->forced_local = 1;
6687 s->non_elf = 0;
6688 s->root.linker_def = 1;
6689 }
6690 }
6691 continue;
6692 }
6693 if (h != NULL)
6694 {
6695 h->save_res = 1;
6696 if (!h->elf.def_regular)
deb0e272 6697 {
a4b6fadd
AM
6698 h->elf.root.type = bfd_link_hash_defined;
6699 h->elf.root.u.def.section = htab->sfpr;
6700 h->elf.root.u.def.value = htab->sfpr->size;
6701 h->elf.type = STT_FUNC;
6702 h->elf.def_regular = 1;
6703 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6704 writing = TRUE;
deb0e272 6705 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6706 {
6707 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6708 if (htab->sfpr->contents == NULL)
6709 return FALSE;
6710 }
deb0e272
AM
6711 }
6712 }
6713 if (writing)
6714 {
6715 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6716 if (i != parm->hi)
6717 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6718 else
6719 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6720 htab->sfpr->size = p - htab->sfpr->contents;
6721 }
6722 }
6723
6724 return TRUE;
6725}
6726
6727static bfd_byte *
6728savegpr0 (bfd *abfd, bfd_byte *p, int r)
6729{
6730 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6731 return p + 4;
6732}
6733
6734static bfd_byte *
6735savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6736{
6737 p = savegpr0 (abfd, p, r);
a078d95a 6738 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6739 p = p + 4;
6740 bfd_put_32 (abfd, BLR, p);
6741 return p + 4;
6742}
6743
6744static bfd_byte *
6745restgpr0 (bfd *abfd, bfd_byte *p, int r)
6746{
6747 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6748 return p + 4;
6749}
6750
6751static bfd_byte *
6752restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6753{
a078d95a 6754 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6755 p = p + 4;
6756 p = restgpr0 (abfd, p, r);
6757 bfd_put_32 (abfd, MTLR_R0, p);
6758 p = p + 4;
6759 if (r == 29)
6760 {
6761 p = restgpr0 (abfd, p, 30);
6762 p = restgpr0 (abfd, p, 31);
6763 }
6764 bfd_put_32 (abfd, BLR, p);
6765 return p + 4;
6766}
6767
6768static bfd_byte *
6769savegpr1 (bfd *abfd, bfd_byte *p, int r)
6770{
6771 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6772 return p + 4;
6773}
6774
6775static bfd_byte *
6776savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6777{
6778 p = savegpr1 (abfd, p, r);
6779 bfd_put_32 (abfd, BLR, p);
6780 return p + 4;
6781}
6782
6783static bfd_byte *
6784restgpr1 (bfd *abfd, bfd_byte *p, int r)
6785{
6786 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6787 return p + 4;
6788}
6789
6790static bfd_byte *
6791restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6792{
6793 p = restgpr1 (abfd, p, r);
6794 bfd_put_32 (abfd, BLR, p);
6795 return p + 4;
6796}
6797
6798static bfd_byte *
6799savefpr (bfd *abfd, bfd_byte *p, int r)
6800{
6801 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6802 return p + 4;
6803}
6804
6805static bfd_byte *
6806savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6807{
6808 p = savefpr (abfd, p, r);
a078d95a 6809 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6810 p = p + 4;
6811 bfd_put_32 (abfd, BLR, p);
6812 return p + 4;
6813}
6814
6815static bfd_byte *
6816restfpr (bfd *abfd, bfd_byte *p, int r)
6817{
6818 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6819 return p + 4;
6820}
6821
6822static bfd_byte *
6823restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6824{
a078d95a 6825 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6826 p = p + 4;
6827 p = restfpr (abfd, p, r);
6828 bfd_put_32 (abfd, MTLR_R0, p);
6829 p = p + 4;
6830 if (r == 29)
6831 {
6832 p = restfpr (abfd, p, 30);
6833 p = restfpr (abfd, p, 31);
6834 }
6835 bfd_put_32 (abfd, BLR, p);
6836 return p + 4;
6837}
6838
6839static bfd_byte *
6840savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6841{
6842 p = savefpr (abfd, p, r);
6843 bfd_put_32 (abfd, BLR, p);
6844 return p + 4;
6845}
6846
6847static bfd_byte *
6848restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6849{
6850 p = restfpr (abfd, p, r);
6851 bfd_put_32 (abfd, BLR, p);
6852 return p + 4;
6853}
6854
6855static bfd_byte *
6856savevr (bfd *abfd, bfd_byte *p, int r)
6857{
6858 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6859 p = p + 4;
6860 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6861 return p + 4;
6862}
6863
6864static bfd_byte *
6865savevr_tail (bfd *abfd, bfd_byte *p, int r)
6866{
6867 p = savevr (abfd, p, r);
6868 bfd_put_32 (abfd, BLR, p);
6869 return p + 4;
6870}
6871
6872static bfd_byte *
6873restvr (bfd *abfd, bfd_byte *p, int r)
6874{
6875 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6876 p = p + 4;
6877 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6878 return p + 4;
6879}
6880
6881static bfd_byte *
6882restvr_tail (bfd *abfd, bfd_byte *p, int r)
6883{
6884 p = restvr (abfd, p, r);
6885 bfd_put_32 (abfd, BLR, p);
6886 return p + 4;
6887}
6888
e86ce104
AM
6889/* Called via elf_link_hash_traverse to transfer dynamic linking
6890 information on function code symbol entries to their corresponding
6891 function descriptor symbol entries. */
deb0e272 6892
b34976b6 6893static bfd_boolean
4ce794b7 6894func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6895{
e86ce104 6896 struct bfd_link_info *info;
65f38f15 6897 struct ppc_link_hash_table *htab;
411e1bfb 6898 struct plt_entry *ent;
50bc7936
AM
6899 struct ppc_link_hash_entry *fh;
6900 struct ppc_link_hash_entry *fdh;
6901 bfd_boolean force_local;
5bd4f169 6902
50bc7936
AM
6903 fh = (struct ppc_link_hash_entry *) h;
6904 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6905 return TRUE;
e86ce104 6906
4ce794b7 6907 info = inf;
65f38f15 6908 htab = ppc_hash_table (info);
4dfe6ac6
NC
6909 if (htab == NULL)
6910 return FALSE;
5bd4f169 6911
c09bdfe5
AM
6912 /* Resolve undefined references to dot-symbols as the value
6913 in the function descriptor, if we have one in a regular object.
6914 This is to satisfy cases like ".quad .foo". Calls to functions
6915 in dynamic objects are handled elsewhere. */
6916 if (fh->elf.root.type == bfd_link_hash_undefweak
6917 && fh->was_undefined
b31867b6
AM
6918 && (fdh = defined_func_desc (fh)) != NULL
6919 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6920 && opd_entry_value (fdh->elf.root.u.def.section,
6921 fdh->elf.root.u.def.value,
c09bdfe5 6922 &fh->elf.root.u.def.section,
aef36ac1 6923 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6924 {
b31867b6 6925 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6926 fh->elf.forced_local = 1;
b31867b6
AM
6927 fh->elf.def_regular = fdh->elf.def_regular;
6928 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6929 }
6930
e86ce104
AM
6931 /* If this is a function code symbol, transfer dynamic linking
6932 information to the function descriptor symbol. */
50bc7936 6933 if (!fh->is_func)
b34976b6 6934 return TRUE;
e86ce104 6935
50bc7936 6936 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6937 if (ent->plt.refcount > 0)
6938 break;
50bc7936
AM
6939 if (ent == NULL
6940 || fh->elf.root.root.string[0] != '.'
6941 || fh->elf.root.root.string[1] == '\0')
6942 return TRUE;
5bd4f169 6943
50bc7936
AM
6944 /* Find the corresponding function descriptor symbol. Create it
6945 as undefined if necessary. */
5bd4f169 6946
b31867b6 6947 fdh = lookup_fdh (fh, htab);
50bc7936 6948 if (fdh == NULL
0e1862bb 6949 && !bfd_link_executable (info)
50bc7936
AM
6950 && (fh->elf.root.type == bfd_link_hash_undefined
6951 || fh->elf.root.type == bfd_link_hash_undefweak))
6952 {
908b32fc 6953 fdh = make_fdh (info, fh);
bb700d78
AM
6954 if (fdh == NULL)
6955 return FALSE;
50bc7936 6956 }
648cca2c 6957
908b32fc 6958 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6959 code symbol is strong undefined, make the fake sym the same.
6960 If the function code symbol is defined, then force the fake
6961 descriptor local; We can't support overriding of symbols in a
6962 shared library on a fake descriptor. */
908b32fc
AM
6963
6964 if (fdh != NULL
6965 && fdh->fake
433817dd 6966 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6967 {
433817dd
AM
6968 if (fh->elf.root.type == bfd_link_hash_undefined)
6969 {
6970 fdh->elf.root.type = bfd_link_hash_undefined;
6971 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6972 }
6973 else if (fh->elf.root.type == bfd_link_hash_defined
6974 || fh->elf.root.type == bfd_link_hash_defweak)
6975 {
6976 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6977 }
908b32fc
AM
6978 }
6979
50bc7936 6980 if (fdh != NULL
f5385ebf 6981 && !fdh->elf.forced_local
0e1862bb 6982 && (!bfd_link_executable (info)
f5385ebf
AM
6983 || fdh->elf.def_dynamic
6984 || fdh->elf.ref_dynamic
50bc7936
AM
6985 || (fdh->elf.root.type == bfd_link_hash_undefweak
6986 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6987 {
6988 if (fdh->elf.dynindx == -1)
c152c796 6989 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6990 return FALSE;
f5385ebf
AM
6991 fdh->elf.ref_regular |= fh->elf.ref_regular;
6992 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6993 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6994 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6995 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6996 {
40d16e0b 6997 move_plt_plist (fh, fdh);
f5385ebf 6998 fdh->elf.needs_plt = 1;
e86ce104 6999 }
50bc7936 7000 fdh->is_func_descriptor = 1;
34814b9f
AM
7001 fdh->oh = fh;
7002 fh->oh = fdh;
e86ce104
AM
7003 }
7004
50bc7936
AM
7005 /* Now that the info is on the function descriptor, clear the
7006 function code sym info. Any function code syms for which we
7007 don't have a definition in a regular file, we force local.
7008 This prevents a shared library from exporting syms that have
7009 been imported from another library. Function code syms that
7010 are really in the library we must leave global to prevent the
7011 linker dragging in a definition from a static library. */
93f3fa99
AM
7012 force_local = (!fh->elf.def_regular
7013 || fdh == NULL
7014 || !fdh->elf.def_regular
7015 || fdh->elf.forced_local);
50bc7936
AM
7016 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7017
b34976b6 7018 return TRUE;
e86ce104 7019}
40b8271b 7020
a4b6fadd
AM
7021static const struct sfpr_def_parms save_res_funcs[] =
7022 {
7023 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7024 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7025 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7026 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7027 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7028 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7029 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7030 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7031 { "._savef", 14, 31, savefpr, savefpr1_tail },
7032 { "._restf", 14, 31, restfpr, restfpr1_tail },
7033 { "_savevr_", 20, 31, savevr, savevr_tail },
7034 { "_restvr_", 20, 31, restvr, restvr_tail }
7035 };
7036
e86ce104 7037/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7038 this hook to a) provide some gcc support functions, and b) transfer
7039 dynamic linking information gathered so far on function code symbol
7040 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7041
b34976b6 7042static bfd_boolean
4ce794b7
AM
7043ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7044 struct bfd_link_info *info)
e86ce104
AM
7045{
7046 struct ppc_link_hash_table *htab;
82bd7b59 7047 unsigned int i;
e86ce104
AM
7048
7049 htab = ppc_hash_table (info);
4dfe6ac6
NC
7050 if (htab == NULL)
7051 return FALSE;
7052
0e1862bb 7053 if (!bfd_link_relocatable (info)
5295321c 7054 && htab->elf.hgot != NULL)
dba6fa9b
AM
7055 {
7056 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7057 /* Make .TOC. defined so as to prevent it being made dynamic.
7058 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7059 if (!htab->elf.hgot->def_regular
7060 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7061 {
7062 htab->elf.hgot->root.type = bfd_link_hash_defined;
7063 htab->elf.hgot->root.u.def.value = 0;
7064 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7065 htab->elf.hgot->def_regular = 1;
7066 htab->elf.hgot->root.linker_def = 1;
7067 }
dba6fa9b 7068 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7069 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7070 | STV_HIDDEN);
7071 }
c66bb0ee 7072
82bd7b59
AM
7073 if (htab->sfpr == NULL)
7074 /* We don't have any relocs. */
b34976b6 7075 return TRUE;
82bd7b59 7076
deb0e272
AM
7077 /* Provide any missing _save* and _rest* functions. */
7078 htab->sfpr->size = 0;
7d4c687d 7079 if (htab->params->save_restore_funcs)
a4b6fadd
AM
7080 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7081 if (!sfpr_define (info, &save_res_funcs[i], NULL))
27fc25a1 7082 return FALSE;
82bd7b59 7083
4ce794b7 7084 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 7085
eea6121a 7086 if (htab->sfpr->size == 0)
8423293d 7087 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 7088
b34976b6 7089 return TRUE;
e86ce104
AM
7090}
7091
a345bc8d
AM
7092/* Return true if we have dynamic relocs that apply to read-only sections. */
7093
7094static bfd_boolean
7095readonly_dynrelocs (struct elf_link_hash_entry *h)
7096{
7097 struct ppc_link_hash_entry *eh;
7098 struct elf_dyn_relocs *p;
7099
7100 eh = (struct ppc_link_hash_entry *) h;
7101 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7102 {
7103 asection *s = p->sec->output_section;
7104
7105 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7106 return TRUE;
7107 }
7108 return FALSE;
7109}
7110
e86ce104
AM
7111/* Adjust a symbol defined by a dynamic object and referenced by a
7112 regular object. The current definition is in some section of the
7113 dynamic object, but we're not including those sections. We have to
7114 change the definition to something the rest of the link can
7115 understand. */
7116
b34976b6 7117static bfd_boolean
4ce794b7
AM
7118ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7119 struct elf_link_hash_entry *h)
e86ce104
AM
7120{
7121 struct ppc_link_hash_table *htab;
e86ce104 7122 asection *s;
e86ce104
AM
7123
7124 htab = ppc_hash_table (info);
4dfe6ac6
NC
7125 if (htab == NULL)
7126 return FALSE;
e86ce104
AM
7127
7128 /* Deal with function syms. */
7129 if (h->type == STT_FUNC
e054468f 7130 || h->type == STT_GNU_IFUNC
f5385ebf 7131 || h->needs_plt)
e86ce104
AM
7132 {
7133 /* Clear procedure linkage table information for any symbol that
7134 won't need a .plt entry. */
411e1bfb
AM
7135 struct plt_entry *ent;
7136 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7137 if (ent->plt.refcount > 0)
7138 break;
8387904d 7139 if (ent == NULL
e054468f
AM
7140 || (h->type != STT_GNU_IFUNC
7141 && (SYMBOL_CALLS_LOCAL (info, h)
7142 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
a4b6fadd
AM
7143 && h->root.type == bfd_link_hash_undefweak)))
7144 || ((struct ppc_link_hash_entry *) h)->save_res)
40b8271b 7145 {
411e1bfb 7146 h->plt.plist = NULL;
f5385ebf 7147 h->needs_plt = 0;
d1eca1e4 7148 h->pointer_equality_needed = 0;
40b8271b 7149 }
a345bc8d
AM
7150 else if (abiversion (info->output_bfd) == 2)
7151 {
d1eca1e4
AM
7152 /* Taking a function's address in a read/write section
7153 doesn't require us to define the function symbol in the
7154 executable on a global entry stub. A dynamic reloc can
7155 be used instead. */
7156 if (h->pointer_equality_needed
1009ef28 7157 && h->type != STT_GNU_IFUNC
d1eca1e4
AM
7158 && !readonly_dynrelocs (h))
7159 {
7160 h->pointer_equality_needed = 0;
7161 h->non_got_ref = 0;
7162 }
7163
a345bc8d
AM
7164 /* After adjust_dynamic_symbol, non_got_ref set in the
7165 non-shared case means that we have allocated space in
7166 .dynbss for the symbol and thus dyn_relocs for this
7167 symbol should be discarded.
7168 If we get here we know we are making a PLT entry for this
7169 symbol, and in an executable we'd normally resolve
7170 relocations against this symbol to the PLT entry. Allow
7171 dynamic relocs if the reference is weak, and the dynamic
7172 relocs will not cause text relocation. */
d1eca1e4
AM
7173 else if (!h->ref_regular_nonweak
7174 && h->non_got_ref
7175 && h->type != STT_GNU_IFUNC
7176 && !readonly_dynrelocs (h))
a345bc8d
AM
7177 h->non_got_ref = 0;
7178
7179 /* If making a plt entry, then we don't need copy relocs. */
7180 return TRUE;
7181 }
5bd4f169 7182 }
bbd7ec4a 7183 else
411e1bfb 7184 h->plt.plist = NULL;
5bd4f169
AM
7185
7186 /* If this is a weak symbol, and there is a real definition, the
7187 processor independent code will have arranged for us to see the
7188 real definition first, and we can just use the same value. */
f6e332e6 7189 if (h->u.weakdef != NULL)
5bd4f169 7190 {
f6e332e6
AM
7191 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7192 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7193 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7194 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7195 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7196 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7197 return TRUE;
5bd4f169
AM
7198 }
7199
5bd4f169
AM
7200 /* If we are creating a shared library, we must presume that the
7201 only references to the symbol are via the global offset table.
7202 For such cases we need not do anything here; the relocations will
7203 be handled correctly by relocate_section. */
0e1862bb 7204 if (bfd_link_pic (info))
b34976b6 7205 return TRUE;
5bd4f169 7206
65f38f15
AM
7207 /* If there are no references to this symbol that do not use the
7208 GOT, we don't need to generate a copy reloc. */
f5385ebf 7209 if (!h->non_got_ref)
b34976b6 7210 return TRUE;
65f38f15 7211
b186458a
JJ
7212 /* Don't generate a copy reloc for symbols defined in the executable. */
7213 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
7214 return TRUE;
7215
a127494f
AM
7216 /* If -z nocopyreloc was given, don't generate them either. */
7217 if (info->nocopyreloc)
7218 {
7219 h->non_got_ref = 0;
7220 return TRUE;
7221 }
7222
a345bc8d
AM
7223 /* If we didn't find any dynamic relocs in read-only sections, then
7224 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7225 if (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
65f38f15 7226 {
a345bc8d
AM
7227 h->non_got_ref = 0;
7228 return TRUE;
65f38f15
AM
7229 }
7230
a127494f
AM
7231 /* Protected variables do not work with .dynbss. The copy in
7232 .dynbss won't be used by the shared library with the protected
7233 definition for the variable. Text relocations are preferable
7234 to an incorrect program. */
7235 if (h->protected_def)
7236 {
7237 h->non_got_ref = 0;
7238 return TRUE;
7239 }
7240
5d35169e 7241 if (h->plt.plist != NULL)
97b639ba
AM
7242 {
7243 /* We should never get here, but unfortunately there are versions
7244 of gcc out there that improperly (for this ABI) put initialized
7245 function pointers, vtable refs and suchlike in read-only
7246 sections. Allow them to proceed, but warn that this might
7247 break at runtime. */
25f53a85 7248 info->callbacks->einfo
bc30df16 7249 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7250 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7251 h->root.root.string);
7252 }
5d35169e
AM
7253
7254 /* This is a reference to a symbol defined by a dynamic object which
7255 is not a function. */
7256
5bd4f169
AM
7257 /* We must allocate the symbol in our .dynbss section, which will
7258 become part of the .bss section of the executable. There will be
7259 an entry for this symbol in the .dynsym section. The dynamic
7260 object will contain position independent code, so all references
7261 from the dynamic object to this symbol will go through the global
7262 offset table. The dynamic linker will use the .dynsym entry to
7263 determine the address it must put in the global offset table, so
7264 both the dynamic object and the regular object will refer to the
7265 same memory location for the variable. */
5bd4f169 7266
04c9666a
AM
7267 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7268 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7269 runtime process image. We need to remember the offset into the
7270 .rela.bss section we are going to use. */
1d7e9d18 7271 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7272 {
eea6121a 7273 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 7274 h->needs_copy = 1;
5bd4f169
AM
7275 }
7276
4ce794b7 7277 s = htab->dynbss;
5bd4f169 7278
6cabe1ea 7279 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7280}
7281
e86ce104
AM
7282/* If given a function descriptor symbol, hide both the function code
7283 sym and the descriptor. */
7284static void
4ce794b7
AM
7285ppc64_elf_hide_symbol (struct bfd_link_info *info,
7286 struct elf_link_hash_entry *h,
7287 bfd_boolean force_local)
e86ce104 7288{
34814b9f 7289 struct ppc_link_hash_entry *eh;
e86ce104
AM
7290 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7291
34814b9f
AM
7292 eh = (struct ppc_link_hash_entry *) h;
7293 if (eh->is_func_descriptor)
e86ce104 7294 {
34814b9f 7295 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7296
721956f4 7297 if (fh == NULL)
d1329ca3
AM
7298 {
7299 const char *p, *q;
7300 struct ppc_link_hash_table *htab;
7301 char save;
7302
7303 /* We aren't supposed to use alloca in BFD because on
7304 systems which do not have alloca the version in libiberty
7305 calls xmalloc, which might cause the program to crash
7306 when it runs out of memory. This function doesn't have a
7307 return status, so there's no way to gracefully return an
7308 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7309 accessed; It's either a string in an ELF string table,
7310 or allocated in an objalloc structure. */
d1329ca3 7311
34814b9f 7312 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7313 save = *p;
7314 *(char *) p = '.';
7315 htab = ppc_hash_table (info);
4dfe6ac6
NC
7316 if (htab == NULL)
7317 return;
7318
34814b9f
AM
7319 fh = (struct ppc_link_hash_entry *)
7320 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7321 *(char *) p = save;
7322
7323 /* Unfortunately, if it so happens that the string we were
7324 looking for was allocated immediately before this string,
7325 then we overwrote the string terminator. That's the only
7326 reason the lookup should fail. */
7327 if (fh == NULL)
7328 {
34814b9f
AM
7329 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7330 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7331 --q, --p;
34814b9f
AM
7332 if (q < eh->elf.root.root.string && *p == '.')
7333 fh = (struct ppc_link_hash_entry *)
7334 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7335 }
7336 if (fh != NULL)
7337 {
34814b9f
AM
7338 eh->oh = fh;
7339 fh->oh = eh;
d1329ca3
AM
7340 }
7341 }
e86ce104 7342 if (fh != NULL)
34814b9f 7343 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7344 }
7345}
7346
411e1bfb 7347static bfd_boolean
8843416a
AM
7348get_sym_h (struct elf_link_hash_entry **hp,
7349 Elf_Internal_Sym **symp,
7350 asection **symsecp,
f961d9dd 7351 unsigned char **tls_maskp,
8843416a
AM
7352 Elf_Internal_Sym **locsymsp,
7353 unsigned long r_symndx,
7354 bfd *ibfd)
411e1bfb 7355{
0ffa91dd 7356 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7357
7358 if (r_symndx >= symtab_hdr->sh_info)
7359 {
7360 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7361 struct elf_link_hash_entry *h;
7362
7363 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7364 h = elf_follow_link (h);
411e1bfb
AM
7365
7366 if (hp != NULL)
7367 *hp = h;
7368
7369 if (symp != NULL)
7370 *symp = NULL;
7371
7372 if (symsecp != NULL)
7373 {
7374 asection *symsec = NULL;
7375 if (h->root.type == bfd_link_hash_defined
7376 || h->root.type == bfd_link_hash_defweak)
7377 symsec = h->root.u.def.section;
7378 *symsecp = symsec;
7379 }
7380
e7b938ca 7381 if (tls_maskp != NULL)
411e1bfb
AM
7382 {
7383 struct ppc_link_hash_entry *eh;
7384
7385 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7386 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7387 }
7388 }
7389 else
7390 {
7391 Elf_Internal_Sym *sym;
7392 Elf_Internal_Sym *locsyms = *locsymsp;
7393
7394 if (locsyms == NULL)
7395 {
7396 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7397 if (locsyms == NULL)
7398 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7399 symtab_hdr->sh_info,
7400 0, NULL, NULL, NULL);
7401 if (locsyms == NULL)
7402 return FALSE;
7403 *locsymsp = locsyms;
7404 }
7405 sym = locsyms + r_symndx;
7406
7407 if (hp != NULL)
7408 *hp = NULL;
7409
7410 if (symp != NULL)
7411 *symp = sym;
7412
7413 if (symsecp != NULL)
cb33740c 7414 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7415
e7b938ca 7416 if (tls_maskp != NULL)
411e1bfb
AM
7417 {
7418 struct got_entry **lgot_ents;
f961d9dd 7419 unsigned char *tls_mask;
411e1bfb 7420
e7b938ca 7421 tls_mask = NULL;
411e1bfb
AM
7422 lgot_ents = elf_local_got_ents (ibfd);
7423 if (lgot_ents != NULL)
7424 {
e054468f
AM
7425 struct plt_entry **local_plt = (struct plt_entry **)
7426 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7427 unsigned char *lgot_masks = (unsigned char *)
e054468f 7428 (local_plt + symtab_hdr->sh_info);
e7b938ca 7429 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7430 }
e7b938ca 7431 *tls_maskp = tls_mask;
411e1bfb
AM
7432 }
7433 }
7434 return TRUE;
7435}
7436
e7b938ca 7437/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7438 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7439 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7440
7441static int
f961d9dd 7442get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7443 unsigned long *toc_symndx,
7444 bfd_vma *toc_addend,
0d4792f7 7445 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7446 const Elf_Internal_Rela *rel,
7447 bfd *ibfd)
411e1bfb
AM
7448{
7449 unsigned long r_symndx;
0d4792f7 7450 int next_r;
411e1bfb
AM
7451 struct elf_link_hash_entry *h;
7452 Elf_Internal_Sym *sym;
7453 asection *sec;
7454 bfd_vma off;
7455
7456 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7457 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7458 return 0;
411e1bfb 7459
e7b938ca 7460 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7461 || sec == NULL
6bee8834 7462 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7463 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7464 return 1;
411e1bfb
AM
7465
7466 /* Look inside a TOC section too. */
7467 if (h != NULL)
7468 {
7469 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7470 off = h->root.u.def.value;
7471 }
7472 else
7473 off = sym->st_value;
7474 off += rel->r_addend;
7475 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7476 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7477 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7478 if (toc_symndx != NULL)
7479 *toc_symndx = r_symndx;
3a71aa26
AM
7480 if (toc_addend != NULL)
7481 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7482 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7483 return 0;
854b41e7 7484 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7485 && (next_r == -1 || next_r == -2))
7486 return 1 - next_r;
951fd09b 7487 return 1;
411e1bfb
AM
7488}
7489
3b421ab3
AM
7490/* Find (or create) an entry in the tocsave hash table. */
7491
7492static struct tocsave_entry *
7493tocsave_find (struct ppc_link_hash_table *htab,
7494 enum insert_option insert,
7495 Elf_Internal_Sym **local_syms,
7496 const Elf_Internal_Rela *irela,
7497 bfd *ibfd)
7498{
7499 unsigned long r_indx;
7500 struct elf_link_hash_entry *h;
7501 Elf_Internal_Sym *sym;
7502 struct tocsave_entry ent, *p;
7503 hashval_t hash;
7504 struct tocsave_entry **slot;
7505
7506 r_indx = ELF64_R_SYM (irela->r_info);
7507 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7508 return NULL;
7509 if (ent.sec == NULL || ent.sec->output_section == NULL)
7510 {
7511 (*_bfd_error_handler)
7512 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7513 return NULL;
7514 }
7515
7516 if (h != NULL)
7517 ent.offset = h->root.u.def.value;
7518 else
7519 ent.offset = sym->st_value;
7520 ent.offset += irela->r_addend;
7521
7522 hash = tocsave_htab_hash (&ent);
7523 slot = ((struct tocsave_entry **)
7524 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7525 if (slot == NULL)
7526 return NULL;
7527
7528 if (*slot == NULL)
7529 {
7530 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7531 if (p == NULL)
7532 return NULL;
7533 *p = ent;
7534 *slot = p;
7535 }
7536 return *slot;
7537}
7538
754021d0 7539/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7540 code for the old ABI, these will already have been done. */
754021d0
AM
7541
7542static bfd_boolean
7543adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7544{
7545 struct ppc_link_hash_entry *eh;
7546 asection *sym_sec;
74f0fb50 7547 struct _opd_sec_data *opd;
754021d0
AM
7548
7549 if (h->root.type == bfd_link_hash_indirect)
7550 return TRUE;
7551
754021d0
AM
7552 if (h->root.type != bfd_link_hash_defined
7553 && h->root.type != bfd_link_hash_defweak)
7554 return TRUE;
7555
7556 eh = (struct ppc_link_hash_entry *) h;
7557 if (eh->adjust_done)
7558 return TRUE;
7559
7560 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7561 opd = get_opd_info (sym_sec);
7562 if (opd != NULL && opd->adjust != NULL)
754021d0 7563 {
51aecdc5 7564 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7565 if (adjust == -1)
7566 {
7567 /* This entry has been deleted. */
b3fac117 7568 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7569 if (dsec == NULL)
7570 {
7571 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7572 if (discarded_section (dsec))
81688140 7573 {
b3fac117 7574 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7575 break;
7576 }
7577 }
4025353c 7578 eh->elf.root.u.def.value = 0;
81688140 7579 eh->elf.root.u.def.section = dsec;
4025353c
AM
7580 }
7581 else
7582 eh->elf.root.u.def.value += adjust;
754021d0
AM
7583 eh->adjust_done = 1;
7584 }
7585 return TRUE;
7586}
7587
8c1d1bb8 7588/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7589 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7590 have already been determined. */
7591
7592static bfd_boolean
7593dec_dynrel_count (bfd_vma r_info,
7594 asection *sec,
7595 struct bfd_link_info *info,
7596 Elf_Internal_Sym **local_syms,
7597 struct elf_link_hash_entry *h,
19e08130 7598 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7599{
7600 enum elf_ppc64_reloc_type r_type;
19e08130 7601 asection *sym_sec = NULL;
8c1d1bb8
AM
7602
7603 /* Can this reloc be dynamic? This switch, and later tests here
7604 should be kept in sync with the code in check_relocs. */
7605 r_type = ELF64_R_TYPE (r_info);
7606 switch (r_type)
7607 {
7608 default:
7609 return TRUE;
7610
7611 case R_PPC64_TPREL16:
7612 case R_PPC64_TPREL16_LO:
7613 case R_PPC64_TPREL16_HI:
7614 case R_PPC64_TPREL16_HA:
7615 case R_PPC64_TPREL16_DS:
7616 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7617 case R_PPC64_TPREL16_HIGH:
7618 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7619 case R_PPC64_TPREL16_HIGHER:
7620 case R_PPC64_TPREL16_HIGHERA:
7621 case R_PPC64_TPREL16_HIGHEST:
7622 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 7623 if (!bfd_link_pic (info))
8c1d1bb8
AM
7624 return TRUE;
7625
7626 case R_PPC64_TPREL64:
7627 case R_PPC64_DTPMOD64:
7628 case R_PPC64_DTPREL64:
7629 case R_PPC64_ADDR64:
7630 case R_PPC64_REL30:
7631 case R_PPC64_REL32:
7632 case R_PPC64_REL64:
7633 case R_PPC64_ADDR14:
7634 case R_PPC64_ADDR14_BRNTAKEN:
7635 case R_PPC64_ADDR14_BRTAKEN:
7636 case R_PPC64_ADDR16:
7637 case R_PPC64_ADDR16_DS:
7638 case R_PPC64_ADDR16_HA:
7639 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7640 case R_PPC64_ADDR16_HIGH:
7641 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7642 case R_PPC64_ADDR16_HIGHER:
7643 case R_PPC64_ADDR16_HIGHERA:
7644 case R_PPC64_ADDR16_HIGHEST:
7645 case R_PPC64_ADDR16_HIGHESTA:
7646 case R_PPC64_ADDR16_LO:
7647 case R_PPC64_ADDR16_LO_DS:
7648 case R_PPC64_ADDR24:
7649 case R_PPC64_ADDR32:
7650 case R_PPC64_UADDR16:
7651 case R_PPC64_UADDR32:
7652 case R_PPC64_UADDR64:
7653 case R_PPC64_TOC:
7654 break;
7655 }
7656
7657 if (local_syms != NULL)
7658 {
7659 unsigned long r_symndx;
8c1d1bb8
AM
7660 bfd *ibfd = sec->owner;
7661
7662 r_symndx = ELF64_R_SYM (r_info);
7663 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7664 return FALSE;
7665 }
7666
0e1862bb 7667 if ((bfd_link_pic (info)
1d483afe 7668 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7669 || (h != NULL
198f1157 7670 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7671 || h->root.type == bfd_link_hash_defweak
7672 || !h->def_regular))))
7673 || (ELIMINATE_COPY_RELOCS
0e1862bb 7674 && !bfd_link_pic (info)
8c1d1bb8
AM
7675 && h != NULL
7676 && (h->root.type == bfd_link_hash_defweak
7677 || !h->def_regular)))
7678 ;
7679 else
7680 return TRUE;
7681
7682 if (h != NULL)
6edfbbad 7683 {
19e08130
AM
7684 struct elf_dyn_relocs *p;
7685 struct elf_dyn_relocs **pp;
7686 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7687
7688 /* elf_gc_sweep may have already removed all dyn relocs associated
7689 with local syms for a given section. Also, symbol flags are
7690 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7691 report a dynreloc miscount. */
7692 if (*pp == NULL && info->gc_sections)
7693 return TRUE;
7694
7695 while ((p = *pp) != NULL)
60124e18 7696 {
19e08130
AM
7697 if (p->sec == sec)
7698 {
7699 if (!must_be_dyn_reloc (info, r_type))
7700 p->pc_count -= 1;
7701 p->count -= 1;
7702 if (p->count == 0)
7703 *pp = p->next;
7704 return TRUE;
7705 }
7706 pp = &p->next;
60124e18 7707 }
6edfbbad 7708 }
19e08130
AM
7709 else
7710 {
7711 struct ppc_dyn_relocs *p;
7712 struct ppc_dyn_relocs **pp;
7713 void *vpp;
7714 bfd_boolean is_ifunc;
8c1d1bb8 7715
19e08130
AM
7716 if (local_syms == NULL)
7717 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7718 if (sym_sec == NULL)
7719 sym_sec = sec;
c57da1a7 7720
19e08130
AM
7721 vpp = &elf_section_data (sym_sec)->local_dynrel;
7722 pp = (struct ppc_dyn_relocs **) vpp;
7723
7724 if (*pp == NULL && info->gc_sections)
7725 return TRUE;
7726
7727 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7728 while ((p = *pp) != NULL)
8c1d1bb8 7729 {
19e08130
AM
7730 if (p->sec == sec && p->ifunc == is_ifunc)
7731 {
7732 p->count -= 1;
7733 if (p->count == 0)
7734 *pp = p->next;
7735 return TRUE;
7736 }
7737 pp = &p->next;
8c1d1bb8 7738 }
8c1d1bb8
AM
7739 }
7740
8de848d8 7741 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7742 sec->owner, sec);
8c1d1bb8
AM
7743 bfd_set_error (bfd_error_bad_value);
7744 return FALSE;
7745}
7746
754021d0
AM
7747/* Remove unused Official Procedure Descriptor entries. Currently we
7748 only remove those associated with functions in discarded link-once
7749 sections, or weakly defined functions that have been overridden. It
7750 would be possible to remove many more entries for statically linked
7751 applications. */
7752
b34976b6 7753bfd_boolean
e7d1c40c 7754ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7755{
7756 bfd *ibfd;
754021d0 7757 bfd_boolean some_edited = FALSE;
3f764659 7758 asection *need_pad = NULL;
e7d1c40c
AM
7759 struct ppc_link_hash_table *htab;
7760
7761 htab = ppc_hash_table (info);
7762 if (htab == NULL)
7763 return FALSE;
1e2f5b6e 7764
c72f2fb2 7765 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7766 {
7767 asection *sec;
7768 Elf_Internal_Rela *relstart, *rel, *relend;
7769 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7770 Elf_Internal_Sym *local_syms;
74f0fb50 7771 struct _opd_sec_data *opd;
51aecdc5 7772 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7773 bfd_size_type cnt_16b = 0;
1e2f5b6e 7774
854b41e7
AM
7775 if (!is_ppc64_elf (ibfd))
7776 continue;
7777
1e2f5b6e 7778 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7779 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7780 continue;
7781
dbaa2011 7782 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7783 continue;
7784
1e2f5b6e
AM
7785 if (sec->output_section == bfd_abs_section_ptr)
7786 continue;
7787
7788 /* Look through the section relocs. */
7789 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7790 continue;
7791
6cdc0ccc 7792 local_syms = NULL;
0ffa91dd 7793 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7794
7795 /* Read the relocations. */
4ce794b7 7796 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7797 info->keep_memory);
1e2f5b6e 7798 if (relstart == NULL)
b34976b6 7799 return FALSE;
1e2f5b6e
AM
7800
7801 /* First run through the relocs to check they are sane, and to
7802 determine whether we need to edit this opd section. */
b34976b6 7803 need_edit = FALSE;
51aecdc5 7804 broken = FALSE;
3f764659 7805 need_pad = sec;
1e2f5b6e 7806 relend = relstart + sec->reloc_count;
50bc7936 7807 for (rel = relstart; rel < relend; )
1e2f5b6e 7808 {
04c9666a 7809 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7810 unsigned long r_symndx;
7811 asection *sym_sec;
7812 struct elf_link_hash_entry *h;
7813 Elf_Internal_Sym *sym;
51aecdc5 7814 bfd_vma offset;
1e2f5b6e 7815
51aecdc5 7816 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7817 only interested in the reloc pointing to a function entry
7818 point. */
51aecdc5
AM
7819 offset = rel->r_offset;
7820 if (rel + 1 == relend
7821 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7822 {
7823 /* If someone messes with .opd alignment then after a
7824 "ld -r" we might have padding in the middle of .opd.
7825 Also, there's nothing to prevent someone putting
7826 something silly in .opd with the assembler. No .opd
b34976b6 7827 optimization for them! */
3f764659 7828 broken_opd:
1e2f5b6e 7829 (*_bfd_error_handler)
d003868e 7830 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7831 broken = TRUE;
1e2f5b6e
AM
7832 break;
7833 }
7834
50bc7936
AM
7835 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7836 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7837 {
7838 (*_bfd_error_handler)
d003868e
AM
7839 (_("%B: unexpected reloc type %u in .opd section"),
7840 ibfd, r_type);
51aecdc5 7841 broken = TRUE;
50bc7936
AM
7842 break;
7843 }
7844
1e2f5b6e 7845 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7846 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7847 r_symndx, ibfd))
50bc7936 7848 goto error_ret;
1e2f5b6e
AM
7849
7850 if (sym_sec == NULL || sym_sec->owner == NULL)
7851 {
411e1bfb
AM
7852 const char *sym_name;
7853 if (h != NULL)
7854 sym_name = h->root.root.string;
7855 else
26c61ae5
L
7856 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7857 sym_sec);
411e1bfb 7858
1e2f5b6e 7859 (*_bfd_error_handler)
d003868e
AM
7860 (_("%B: undefined sym `%s' in .opd section"),
7861 ibfd, sym_name);
51aecdc5 7862 broken = TRUE;
1e2f5b6e
AM
7863 break;
7864 }
7865
51020317
AM
7866 /* opd entries are always for functions defined in the
7867 current input bfd. If the symbol isn't defined in the
7868 input bfd, then we won't be using the function in this
7869 bfd; It must be defined in a linkonce section in another
7870 bfd, or is weak. It's also possible that we are
7871 discarding the function due to a linker script /DISCARD/,
7872 which we test for via the output_section. */
7873 if (sym_sec->owner != ibfd
7874 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7875 need_edit = TRUE;
1e2f5b6e 7876
50bc7936 7877 rel += 2;
51aecdc5
AM
7878 if (rel + 1 == relend
7879 || (rel + 2 < relend
7880 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7881 ++rel;
7882
7883 if (rel == relend)
3f764659
JJ
7884 {
7885 if (sec->size == offset + 24)
7886 {
7887 need_pad = NULL;
7888 break;
7889 }
51aecdc5 7890 if (sec->size == offset + 16)
3f764659
JJ
7891 {
7892 cnt_16b++;
7893 break;
7894 }
7895 goto broken_opd;
7896 }
3f764659
JJ
7897 else if (rel + 1 < relend
7898 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7899 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7900 {
51aecdc5
AM
7901 if (rel[0].r_offset == offset + 16)
7902 cnt_16b++;
7903 else if (rel[0].r_offset != offset + 24)
7904 goto broken_opd;
3f764659
JJ
7905 }
7906 else
7907 goto broken_opd;
1e2f5b6e
AM
7908 }
7909
e7d1c40c 7910 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7911
51aecdc5 7912 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7913 {
7914 Elf_Internal_Rela *write_rel;
d4730f92 7915 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7916 bfd_byte *rptr, *wptr;
983bddc8 7917 bfd_byte *new_contents;
74f0fb50
AM
7918 bfd_size_type amt;
7919
983bddc8 7920 new_contents = NULL;
51aecdc5 7921 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7922 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7923 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7924 if (opd->adjust == NULL)
7925 return FALSE;
7926 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7927
7928 /* This seems a waste of time as input .opd sections are all
7929 zeros as generated by gcc, but I suppose there's no reason
7930 this will always be so. We might start putting something in
7931 the third word of .opd entries. */
7932 if ((sec->flags & SEC_IN_MEMORY) == 0)
7933 {
eea6121a
AM
7934 bfd_byte *loc;
7935 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7936 {
eea6121a
AM
7937 if (loc != NULL)
7938 free (loc);
50bc7936 7939 error_ret:
6cdc0ccc
AM
7940 if (local_syms != NULL
7941 && symtab_hdr->contents != (unsigned char *) local_syms)
7942 free (local_syms);
6cdc0ccc
AM
7943 if (elf_section_data (sec)->relocs != relstart)
7944 free (relstart);
b34976b6 7945 return FALSE;
6cdc0ccc 7946 }
1e2f5b6e
AM
7947 sec->contents = loc;
7948 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7949 }
7950
7951 elf_section_data (sec)->relocs = relstart;
7952
3f764659 7953 new_contents = sec->contents;
3f764659
JJ
7954 if (add_aux_fields)
7955 {
7956 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7957 if (new_contents == NULL)
7958 return FALSE;
51aecdc5 7959 need_pad = NULL;
3f764659 7960 }
b4f4e59f
AM
7961 wptr = new_contents;
7962 rptr = sec->contents;
1e2f5b6e 7963 write_rel = relstart;
51aecdc5 7964 for (rel = relstart; rel < relend; )
1e2f5b6e 7965 {
50bc7936
AM
7966 unsigned long r_symndx;
7967 asection *sym_sec;
7968 struct elf_link_hash_entry *h;
51aecdc5 7969 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7970 Elf_Internal_Sym *sym;
51aecdc5
AM
7971 long opd_ent_size;
7972 Elf_Internal_Rela *next_rel;
7973 bfd_boolean skip;
50bc7936
AM
7974
7975 r_symndx = ELF64_R_SYM (rel->r_info);
7976 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7977 r_symndx, ibfd))
50bc7936
AM
7978 goto error_ret;
7979
51aecdc5
AM
7980 next_rel = rel + 2;
7981 if (next_rel + 1 == relend
7982 || (next_rel + 2 < relend
7983 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7984 ++next_rel;
7985
7986 /* See if the .opd entry is full 24 byte or
7987 16 byte (with fd_aux entry overlapped with next
7988 fd_func). */
7989 opd_ent_size = 24;
7990 if (next_rel == relend)
1e2f5b6e 7991 {
51aecdc5 7992 if (sec->size == rel->r_offset + 16)
3f764659 7993 opd_ent_size = 16;
51aecdc5
AM
7994 }
7995 else if (next_rel->r_offset == rel->r_offset + 16)
7996 opd_ent_size = 16;
3f764659 7997
51aecdc5
AM
7998 if (h != NULL
7999 && h->root.root.string[0] == '.')
8000 {
8001 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
8002 if (fdh != NULL
8003 && fdh->elf.root.type != bfd_link_hash_defined
8004 && fdh->elf.root.type != bfd_link_hash_defweak)
8005 fdh = NULL;
8006 }
1e2f5b6e 8007
51aecdc5
AM
8008 skip = (sym_sec->owner != ibfd
8009 || sym_sec->output_section == bfd_abs_section_ptr);
8010 if (skip)
8011 {
8012 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8013 {
51aecdc5
AM
8014 /* Arrange for the function descriptor sym
8015 to be dropped. */
8016 fdh->elf.root.u.def.value = 0;
8017 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8018 }
51aecdc5 8019 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8020
0e1862bb 8021 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8022 rel = next_rel;
8023 else
8024 while (1)
8025 {
8026 if (!dec_dynrel_count (rel->r_info, sec, info,
8027 NULL, h, sym))
8028 goto error_ret;
754021d0 8029
51aecdc5
AM
8030 if (++rel == next_rel)
8031 break;
1e2f5b6e 8032
51aecdc5
AM
8033 r_symndx = ELF64_R_SYM (rel->r_info);
8034 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8035 r_symndx, ibfd))
8036 goto error_ret;
8037 }
50bc7936
AM
8038 }
8039 else
1e2f5b6e 8040 {
51aecdc5
AM
8041 /* We'll be keeping this opd entry. */
8042 long adjust;
8043
8044 if (fdh != NULL)
8045 {
8046 /* Redefine the function descriptor symbol to
8047 this location in the opd section. It is
8048 necessary to update the value here rather
8049 than using an array of adjustments as we do
8050 for local symbols, because various places
8051 in the generic ELF code use the value
8052 stored in u.def.value. */
8053 fdh->elf.root.u.def.value = wptr - new_contents;
8054 fdh->adjust_done = 1;
8055 }
8056
8057 /* Local syms are a bit tricky. We could
8058 tweak them as they can be cached, but
8059 we'd need to look through the local syms
8060 for the function descriptor sym which we
8061 don't have at the moment. So keep an
8062 array of adjustments. */
8063 adjust = (wptr - new_contents) - (rptr - sec->contents);
8064 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8065
8066 if (wptr != rptr)
8067 memcpy (wptr, rptr, opd_ent_size);
8068 wptr += opd_ent_size;
8069 if (add_aux_fields && opd_ent_size == 16)
8070 {
8071 memset (wptr, '\0', 8);
8072 wptr += 8;
8073 }
8074
50bc7936 8075 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8076 new opd entries. */
8077 for ( ; rel != next_rel; ++rel)
8078 {
8079 rel->r_offset += adjust;
8080 if (write_rel != rel)
8081 memcpy (write_rel, rel, sizeof (*rel));
8082 ++write_rel;
8083 }
1e2f5b6e 8084 }
51aecdc5
AM
8085
8086 rptr += opd_ent_size;
1e2f5b6e
AM
8087 }
8088
3f764659 8089 sec->size = wptr - new_contents;
1e2f5b6e 8090 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8091 if (add_aux_fields)
8092 {
8093 free (sec->contents);
8094 sec->contents = new_contents;
8095 }
8096
05bf9422 8097 /* Fudge the header size too, as this is used later in
cdcf6e38 8098 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8099 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8100 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8101 some_edited = TRUE;
1e2f5b6e 8102 }
6cdc0ccc 8103 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8104 free (relstart);
6cdc0ccc 8105
411e1bfb
AM
8106 if (local_syms != NULL
8107 && symtab_hdr->contents != (unsigned char *) local_syms)
8108 {
8109 if (!info->keep_memory)
8110 free (local_syms);
8111 else
8112 symtab_hdr->contents = (unsigned char *) local_syms;
8113 }
8114 }
8115
754021d0
AM
8116 if (some_edited)
8117 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8118
3f764659
JJ
8119 /* If we are doing a final link and the last .opd entry is just 16 byte
8120 long, add a 8 byte padding after it. */
0e1862bb 8121 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8122 {
8123 bfd_byte *p;
8124
8125 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8126 {
8127 BFD_ASSERT (need_pad->size > 0);
8128
8129 p = bfd_malloc (need_pad->size + 8);
8130 if (p == NULL)
8131 return FALSE;
699733f6 8132
3f764659
JJ
8133 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8134 p, 0, need_pad->size))
8135 return FALSE;
8136
8137 need_pad->contents = p;
8138 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8139 }
8140 else
8141 {
8142 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8143 if (p == NULL)
8144 return FALSE;
8145
8146 need_pad->contents = p;
8147 }
8148
8149 memset (need_pad->contents + need_pad->size, 0, 8);
8150 need_pad->size += 8;
8151 }
8152
411e1bfb
AM
8153 return TRUE;
8154}
8155
e1918d23 8156/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8157
e1918d23 8158asection *
e7d1c40c 8159ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8160{
411e1bfb
AM
8161 struct ppc_link_hash_table *htab;
8162
411e1bfb 8163 htab = ppc_hash_table (info);
4dfe6ac6
NC
8164 if (htab == NULL)
8165 return NULL;
8166
ee67d69a
AM
8167 if (abiversion (info->output_bfd) == 1)
8168 htab->opd_abi = 1;
8169
e7d1c40c 8170 if (htab->params->no_multi_toc)
33c0ec9d
AM
8171 htab->do_multi_toc = 0;
8172 else if (!htab->do_multi_toc)
e7d1c40c 8173 htab->params->no_multi_toc = 1;
33c0ec9d 8174
3a71aa26
AM
8175 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8176 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8177 FALSE, FALSE, TRUE));
a7f2871e
AM
8178 /* Move dynamic linking info to the function descriptor sym. */
8179 if (htab->tls_get_addr != NULL)
8180 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8181 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8182 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8183 FALSE, FALSE, TRUE));
7c9cf415 8184 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8185 {
8186 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8187
8188 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8189 FALSE, FALSE, TRUE);
8190 if (opt != NULL)
8191 func_desc_adjust (opt, info);
8192 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8193 FALSE, FALSE, TRUE);
8194 if (opt_fd != NULL
8195 && (opt_fd->root.type == bfd_link_hash_defined
8196 || opt_fd->root.type == bfd_link_hash_defweak))
8197 {
8198 /* If glibc supports an optimized __tls_get_addr call stub,
8199 signalled by the presence of __tls_get_addr_opt, and we'll
8200 be calling __tls_get_addr via a plt call stub, then
8201 make __tls_get_addr point to __tls_get_addr_opt. */
8202 tga_fd = &htab->tls_get_addr_fd->elf;
8203 if (htab->elf.dynamic_sections_created
8204 && tga_fd != NULL
8205 && (tga_fd->type == STT_FUNC
8206 || tga_fd->needs_plt)
8207 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8208 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8209 && tga_fd->root.type == bfd_link_hash_undefweak)))
8210 {
8211 struct plt_entry *ent;
8212
8213 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8214 if (ent->plt.refcount > 0)
8215 break;
8216 if (ent != NULL)
8217 {
8218 tga_fd->root.type = bfd_link_hash_indirect;
8219 tga_fd->root.u.i.link = &opt_fd->root;
8220 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8221 if (opt_fd->dynindx != -1)
8222 {
8223 /* Use __tls_get_addr_opt in dynamic relocations. */
8224 opt_fd->dynindx = -1;
8225 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8226 opt_fd->dynstr_index);
8227 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8228 return NULL;
a7f2871e
AM
8229 }
8230 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8231 tga = &htab->tls_get_addr->elf;
8232 if (opt != NULL && tga != NULL)
8233 {
8234 tga->root.type = bfd_link_hash_indirect;
8235 tga->root.u.i.link = &opt->root;
8236 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8237 _bfd_elf_link_hash_hide_symbol (info, opt,
8238 tga->forced_local);
8239 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8240 }
8241 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8242 htab->tls_get_addr_fd->is_func_descriptor = 1;
8243 if (htab->tls_get_addr != NULL)
8244 {
8245 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8246 htab->tls_get_addr->is_func = 1;
8247 }
8248 }
8249 }
8250 }
7c9cf415
AM
8251 else if (htab->params->tls_get_addr_opt < 0)
8252 htab->params->tls_get_addr_opt = 0;
a7f2871e 8253 }
33c0ec9d 8254 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8255}
8387904d 8256
3a71aa26
AM
8257/* Return TRUE iff REL is a branch reloc with a global symbol matching
8258 HASH1 or HASH2. */
8387904d 8259
3a71aa26
AM
8260static bfd_boolean
8261branch_reloc_hash_match (const bfd *ibfd,
8262 const Elf_Internal_Rela *rel,
8263 const struct ppc_link_hash_entry *hash1,
8264 const struct ppc_link_hash_entry *hash2)
8265{
8266 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8267 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8268 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8269
e054468f 8270 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8271 {
3a71aa26
AM
8272 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8273 struct elf_link_hash_entry *h;
8387904d 8274
3a71aa26 8275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8276 h = elf_follow_link (h);
3a71aa26
AM
8277 if (h == &hash1->elf || h == &hash2->elf)
8278 return TRUE;
a48ebf4d 8279 }
3a71aa26 8280 return FALSE;
951fd09b 8281}
411e1bfb 8282
951fd09b
AM
8283/* Run through all the TLS relocs looking for optimization
8284 opportunities. The linker has been hacked (see ppc64elf.em) to do
8285 a preliminary section layout so that we know the TLS segment
8286 offsets. We can't optimize earlier because some optimizations need
8287 to know the tp offset, and we need to optimize before allocating
8288 dynamic relocations. */
8289
8290bfd_boolean
33c0ec9d 8291ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8292{
8293 bfd *ibfd;
8294 asection *sec;
8295 struct ppc_link_hash_table *htab;
663a1470 8296 unsigned char *toc_ref;
102890f0 8297 int pass;
951fd09b 8298
3cbc1e5e 8299 if (!bfd_link_executable (info))
411e1bfb
AM
8300 return TRUE;
8301
951fd09b 8302 htab = ppc_hash_table (info);
4dfe6ac6
NC
8303 if (htab == NULL)
8304 return FALSE;
8305
663a1470
AM
8306 /* Make two passes over the relocs. On the first pass, mark toc
8307 entries involved with tls relocs, and check that tls relocs
8308 involved in setting up a tls_get_addr call are indeed followed by
8309 such a call. If they are not, we can't do any tls optimization.
8310 On the second pass twiddle tls_mask flags to notify
8311 relocate_section that optimization can be done, and adjust got
8312 and plt refcounts. */
8313 toc_ref = NULL;
8314 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8315 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8316 {
8317 Elf_Internal_Sym *locsyms = NULL;
8318 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8319
102890f0
AM
8320 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8321 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8322 {
8323 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8324 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8325
102890f0
AM
8326 /* Read the relocations. */
8327 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8328 info->keep_memory);
8329 if (relstart == NULL)
2915c55b
JK
8330 {
8331 free (toc_ref);
8332 return FALSE;
8333 }
411e1bfb 8334
102890f0
AM
8335 relend = relstart + sec->reloc_count;
8336 for (rel = relstart; rel < relend; rel++)
8337 {
8338 enum elf_ppc64_reloc_type r_type;
8339 unsigned long r_symndx;
8340 struct elf_link_hash_entry *h;
8341 Elf_Internal_Sym *sym;
8342 asection *sym_sec;
f961d9dd
AM
8343 unsigned char *tls_mask;
8344 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8345 bfd_vma value;
8346 bfd_boolean ok_tprel, is_local;
8347 long toc_ref_index = 0;
8348 int expecting_tls_get_addr = 0;
663a1470 8349 bfd_boolean ret = FALSE;
411e1bfb 8350
102890f0
AM
8351 r_symndx = ELF64_R_SYM (rel->r_info);
8352 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8353 r_symndx, ibfd))
8354 {
8355 err_free_rel:
8356 if (elf_section_data (sec)->relocs != relstart)
8357 free (relstart);
8358 if (toc_ref != NULL)
8359 free (toc_ref);
8360 if (locsyms != NULL
0ffa91dd 8361 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8362 != (unsigned char *) locsyms))
8363 free (locsyms);
663a1470 8364 return ret;
102890f0 8365 }
411e1bfb 8366
102890f0
AM
8367 if (h != NULL)
8368 {
766bc656
AM
8369 if (h->root.type == bfd_link_hash_defined
8370 || h->root.type == bfd_link_hash_defweak)
8371 value = h->root.u.def.value;
8372 else if (h->root.type == bfd_link_hash_undefweak)
8373 value = 0;
8374 else
663a1470
AM
8375 {
8376 found_tls_get_addr_arg = 0;
8377 continue;
8378 }
102890f0
AM
8379 }
8380 else
8381 /* Symbols referenced by TLS relocs must be of type
8382 STT_TLS. So no need for .opd local sym adjust. */
8383 value = sym->st_value;
8384
8385 ok_tprel = FALSE;
8386 is_local = FALSE;
8387 if (h == NULL
8388 || !h->def_dynamic)
8389 {
8390 is_local = TRUE;
766bc656
AM
8391 if (h != NULL
8392 && h->root.type == bfd_link_hash_undefweak)
8393 ok_tprel = TRUE;
8394 else
8395 {
8396 value += sym_sec->output_offset;
8397 value += sym_sec->output_section->vma;
8398 value -= htab->elf.tls_sec->vma;
8399 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8400 < (bfd_vma) 1 << 32);
8401 }
102890f0 8402 }
951fd09b 8403
102890f0 8404 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8405 /* If this section has old-style __tls_get_addr calls
8406 without marker relocs, then check that each
8407 __tls_get_addr call reloc is preceded by a reloc
8408 that conceivably belongs to the __tls_get_addr arg
8409 setup insn. If we don't find matching arg setup
8410 relocs, don't do any tls optimization. */
8411 if (pass == 0
8412 && sec->has_tls_get_addr_call
8413 && h != NULL
8414 && (h == &htab->tls_get_addr->elf
8415 || h == &htab->tls_get_addr_fd->elf)
8416 && !found_tls_get_addr_arg
8417 && is_branch_reloc (r_type))
8418 {
25f53a85 8419 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8420 "TLS optimization disabled\n"),
8421 ibfd, sec, rel->r_offset);
8422 ret = TRUE;
8423 goto err_free_rel;
8424 }
8425
8426 found_tls_get_addr_arg = 0;
102890f0
AM
8427 switch (r_type)
8428 {
8429 case R_PPC64_GOT_TLSLD16:
8430 case R_PPC64_GOT_TLSLD16_LO:
8431 expecting_tls_get_addr = 1;
663a1470 8432 found_tls_get_addr_arg = 1;
102890f0
AM
8433 /* Fall thru */
8434
8435 case R_PPC64_GOT_TLSLD16_HI:
8436 case R_PPC64_GOT_TLSLD16_HA:
8437 /* These relocs should never be against a symbol
8438 defined in a shared lib. Leave them alone if
8439 that turns out to be the case. */
8440 if (!is_local)
8441 continue;
411e1bfb 8442
102890f0 8443 /* LD -> LE */
411e1bfb 8444 tls_set = 0;
102890f0
AM
8445 tls_clear = TLS_LD;
8446 tls_type = TLS_TLS | TLS_LD;
8447 break;
411e1bfb 8448
102890f0
AM
8449 case R_PPC64_GOT_TLSGD16:
8450 case R_PPC64_GOT_TLSGD16_LO:
8451 expecting_tls_get_addr = 1;
663a1470 8452 found_tls_get_addr_arg = 1;
102890f0
AM
8453 /* Fall thru */
8454
8455 case R_PPC64_GOT_TLSGD16_HI:
8456 case R_PPC64_GOT_TLSGD16_HA:
8457 if (ok_tprel)
8458 /* GD -> LE */
411e1bfb 8459 tls_set = 0;
102890f0
AM
8460 else
8461 /* GD -> IE */
8462 tls_set = TLS_TLS | TLS_TPRELGD;
8463 tls_clear = TLS_GD;
8464 tls_type = TLS_TLS | TLS_GD;
8465 break;
8466
8467 case R_PPC64_GOT_TPREL16_DS:
8468 case R_PPC64_GOT_TPREL16_LO_DS:
8469 case R_PPC64_GOT_TPREL16_HI:
8470 case R_PPC64_GOT_TPREL16_HA:
8471 if (ok_tprel)
8472 {
8473 /* IE -> LE */
8474 tls_set = 0;
8475 tls_clear = TLS_TPREL;
8476 tls_type = TLS_TLS | TLS_TPREL;
8477 break;
8478 }
411e1bfb
AM
8479 continue;
8480
727fc41e
AM
8481 case R_PPC64_TLSGD:
8482 case R_PPC64_TLSLD:
663a1470
AM
8483 found_tls_get_addr_arg = 1;
8484 /* Fall thru */
8485
8486 case R_PPC64_TLS:
8487 case R_PPC64_TOC16:
8488 case R_PPC64_TOC16_LO:
102890f0
AM
8489 if (sym_sec == NULL || sym_sec != toc)
8490 continue;
8491
8492 /* Mark this toc entry as referenced by a TLS
8493 code sequence. We can do that now in the
8494 case of R_PPC64_TLS, and after checking for
8495 tls_get_addr for the TOC16 relocs. */
8496 if (toc_ref == NULL)
663a1470
AM
8497 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8498 if (toc_ref == NULL)
8499 goto err_free_rel;
8500
102890f0
AM
8501 if (h != NULL)
8502 value = h->root.u.def.value;
8503 else
8504 value = sym->st_value;
8505 value += rel->r_addend;
73242275
AM
8506 if (value % 8 != 0)
8507 continue;
8508 BFD_ASSERT (value < toc->size
8509 && toc->output_offset % 8 == 0);
663a1470 8510 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8511 if (r_type == R_PPC64_TLS
8512 || r_type == R_PPC64_TLSGD
8513 || r_type == R_PPC64_TLSLD)
102890f0
AM
8514 {
8515 toc_ref[toc_ref_index] = 1;
8516 continue;
8517 }
8518
8519 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8520 continue;
8521
8522 tls_set = 0;
8523 tls_clear = 0;
8524 expecting_tls_get_addr = 2;
8525 break;
8526
8527 case R_PPC64_TPREL64:
8528 if (pass == 0
8529 || sec != toc
8530 || toc_ref == NULL
663a1470 8531 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8532 continue;
8533 if (ok_tprel)
8534 {
8535 /* IE -> LE */
8536 tls_set = TLS_EXPLICIT;
8537 tls_clear = TLS_TPREL;
8538 break;
8539 }
8540 continue;
8541
8542 case R_PPC64_DTPMOD64:
8543 if (pass == 0
8544 || sec != toc
8545 || toc_ref == NULL
663a1470 8546 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8547 continue;
8548 if (rel + 1 < relend
8549 && (rel[1].r_info
8550 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8551 && rel[1].r_offset == rel->r_offset + 8)
8552 {
8553 if (ok_tprel)
8554 /* GD -> LE */
8555 tls_set = TLS_EXPLICIT | TLS_GD;
8556 else
8557 /* GD -> IE */
8558 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8559 tls_clear = TLS_GD;
8560 }
8561 else
8562 {
8563 if (!is_local)
8564 continue;
8565
8566 /* LD -> LE */
8567 tls_set = TLS_EXPLICIT;
8568 tls_clear = TLS_LD;
8569 }
8570 break;
8571
8572 default:
8573 continue;
8574 }
8575
8576 if (pass == 0)
8577 {
727fc41e
AM
8578 if (!expecting_tls_get_addr
8579 || !sec->has_tls_get_addr_call)
102890f0
AM
8580 continue;
8581
3a71aa26
AM
8582 if (rel + 1 < relend
8583 && branch_reloc_hash_match (ibfd, rel + 1,
8584 htab->tls_get_addr,
8585 htab->tls_get_addr_fd))
102890f0 8586 {
3a71aa26 8587 if (expecting_tls_get_addr == 2)
102890f0 8588 {
3a71aa26 8589 /* Check for toc tls entries. */
f961d9dd 8590 unsigned char *toc_tls;
3a71aa26
AM
8591 int retval;
8592
8593 retval = get_tls_mask (&toc_tls, NULL, NULL,
8594 &locsyms,
8595 rel, ibfd);
8596 if (retval == 0)
8597 goto err_free_rel;
663a1470
AM
8598 if (toc_tls != NULL)
8599 {
8600 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8601 found_tls_get_addr_arg = 1;
8602 if (retval > 1)
8603 toc_ref[toc_ref_index] = 1;
8604 }
102890f0 8605 }
3a71aa26 8606 continue;
102890f0
AM
8607 }
8608
8609 if (expecting_tls_get_addr != 1)
8610 continue;
8611
8612 /* Uh oh, we didn't find the expected call. We
8613 could just mark this symbol to exclude it
8614 from tls optimization but it's safer to skip
663a1470 8615 the entire optimization. */
25f53a85 8616 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8617 "TLS optimization disabled\n"),
8618 ibfd, sec, rel->r_offset);
8619 ret = TRUE;
8620 goto err_free_rel;
102890f0
AM
8621 }
8622
85f7a9cb 8623 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8624 {
8625 struct plt_entry *ent;
8626 for (ent = htab->tls_get_addr->elf.plt.plist;
8627 ent != NULL;
8628 ent = ent->next)
8629 if (ent->addend == 0)
411e1bfb 8630 {
102890f0 8631 if (ent->plt.refcount > 0)
30038c59 8632 {
102890f0
AM
8633 ent->plt.refcount -= 1;
8634 expecting_tls_get_addr = 0;
30038c59 8635 }
102890f0 8636 break;
411e1bfb 8637 }
102890f0 8638 }
411e1bfb 8639
85f7a9cb 8640 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8641 {
8642 struct plt_entry *ent;
8643 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8644 ent != NULL;
8645 ent = ent->next)
8646 if (ent->addend == 0)
411e1bfb 8647 {
102890f0
AM
8648 if (ent->plt.refcount > 0)
8649 ent->plt.refcount -= 1;
8650 break;
411e1bfb 8651 }
102890f0 8652 }
411e1bfb 8653
102890f0 8654 if (tls_clear == 0)
30038c59
AM
8655 continue;
8656
102890f0
AM
8657 if ((tls_set & TLS_EXPLICIT) == 0)
8658 {
8659 struct got_entry *ent;
411e1bfb 8660
102890f0
AM
8661 /* Adjust got entry for this reloc. */
8662 if (h != NULL)
8663 ent = h->got.glist;
8664 else
8665 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8666
102890f0
AM
8667 for (; ent != NULL; ent = ent->next)
8668 if (ent->addend == rel->r_addend
8669 && ent->owner == ibfd
8670 && ent->tls_type == tls_type)
8671 break;
8672 if (ent == NULL)
8673 abort ();
411e1bfb 8674
102890f0
AM
8675 if (tls_set == 0)
8676 {
8677 /* We managed to get rid of a got entry. */
8678 if (ent->got.refcount > 0)
8679 ent->got.refcount -= 1;
8680 }
8681 }
8682 else
8683 {
8684 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8685 we'll lose one or two dyn relocs. */
8686 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8687 NULL, h, sym))
102890f0 8688 return FALSE;
411e1bfb 8689
102890f0
AM
8690 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8691 {
8692 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8693 NULL, h, sym))
102890f0
AM
8694 return FALSE;
8695 }
8696 }
411e1bfb 8697
102890f0
AM
8698 *tls_mask |= tls_set;
8699 *tls_mask &= ~tls_clear;
8700 }
8c1d1bb8 8701
102890f0
AM
8702 if (elf_section_data (sec)->relocs != relstart)
8703 free (relstart);
8704 }
411e1bfb 8705
663a1470
AM
8706 if (locsyms != NULL
8707 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8708 {
8709 if (!info->keep_memory)
8710 free (locsyms);
8711 else
8712 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8713 }
8714 }
411e1bfb 8715
663a1470
AM
8716 if (toc_ref != NULL)
8717 free (toc_ref);
b34976b6 8718 return TRUE;
1e2f5b6e 8719}
b34976b6 8720
c5614fa4
AM
8721/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8722 the values of any global symbols in a toc section that has been
8723 edited. Globals in toc sections should be a rarity, so this function
8724 sets a flag if any are found in toc sections other than the one just
8725 edited, so that futher hash table traversals can be avoided. */
8726
8727struct adjust_toc_info
8728{
8729 asection *toc;
8730 unsigned long *skip;
8731 bfd_boolean global_toc_syms;
8732};
8733
ba761f19
AM
8734enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8735
c5614fa4
AM
8736static bfd_boolean
8737adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8738{
8739 struct ppc_link_hash_entry *eh;
8740 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8741 unsigned long i;
c5614fa4 8742
c5614fa4
AM
8743 if (h->root.type != bfd_link_hash_defined
8744 && h->root.type != bfd_link_hash_defweak)
8745 return TRUE;
8746
8747 eh = (struct ppc_link_hash_entry *) h;
8748 if (eh->adjust_done)
8749 return TRUE;
8750
8751 if (eh->elf.root.u.def.section == toc_inf->toc)
8752 {
854b41e7
AM
8753 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8754 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8755 else
854b41e7
AM
8756 i = eh->elf.root.u.def.value >> 3;
8757
ba761f19 8758 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8759 {
8760 (*_bfd_error_handler)
854b41e7
AM
8761 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8762 do
8763 ++i;
ba761f19 8764 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8765 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8766 }
854b41e7
AM
8767
8768 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8769 eh->adjust_done = 1;
8770 }
8771 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8772 toc_inf->global_toc_syms = TRUE;
8773
8774 return TRUE;
8775}
8776
560c8763
AM
8777/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8778
8779static bfd_boolean
8780ok_lo_toc_insn (unsigned int insn)
8781{
8782 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8783 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8784 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8785 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8786 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8787 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8788 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8789 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8790 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8791 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8792 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8793 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8794 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8795 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8796 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8797 && (insn & 3) != 1)
8798 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8799 && ((insn & 3) == 0 || (insn & 3) == 3))
8800 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8801}
8802
c5614fa4
AM
8803/* Examine all relocs referencing .toc sections in order to remove
8804 unused .toc entries. */
8805
8806bfd_boolean
33c0ec9d 8807ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8808{
8809 bfd *ibfd;
8810 struct adjust_toc_info toc_inf;
67f0cbdb 8811 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8812
67f0cbdb 8813 htab->do_toc_opt = 1;
c5614fa4 8814 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8815 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8816 {
8817 asection *toc, *sec;
8818 Elf_Internal_Shdr *symtab_hdr;
8819 Elf_Internal_Sym *local_syms;
425b145b 8820 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8821 unsigned long *skip, *drop;
8822 unsigned char *used;
8823 unsigned char *keep, last, some_unused;
8824
854b41e7
AM
8825 if (!is_ppc64_elf (ibfd))
8826 continue;
8827
c5614fa4
AM
8828 toc = bfd_get_section_by_name (ibfd, ".toc");
8829 if (toc == NULL
92b7a70f 8830 || toc->size == 0
dbaa2011
AM
8831 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8832 || discarded_section (toc))
c5614fa4
AM
8833 continue;
8834
425b145b 8835 toc_relocs = NULL;
c5614fa4 8836 local_syms = NULL;
0ffa91dd 8837 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8838
8839 /* Look at sections dropped from the final link. */
8840 skip = NULL;
8841 relstart = NULL;
8842 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8843 {
8844 if (sec->reloc_count == 0
dbaa2011 8845 || !discarded_section (sec)
c5614fa4
AM
8846 || get_opd_info (sec)
8847 || (sec->flags & SEC_ALLOC) == 0
8848 || (sec->flags & SEC_DEBUGGING) != 0)
8849 continue;
8850
8851 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8852 if (relstart == NULL)
8853 goto error_ret;
8854
8855 /* Run through the relocs to see which toc entries might be
8856 unused. */
8857 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8858 {
8859 enum elf_ppc64_reloc_type r_type;
8860 unsigned long r_symndx;
8861 asection *sym_sec;
8862 struct elf_link_hash_entry *h;
8863 Elf_Internal_Sym *sym;
8864 bfd_vma val;
8865
8866 r_type = ELF64_R_TYPE (rel->r_info);
8867 switch (r_type)
8868 {
8869 default:
8870 continue;
8871
8872 case R_PPC64_TOC16:
8873 case R_PPC64_TOC16_LO:
8874 case R_PPC64_TOC16_HI:
8875 case R_PPC64_TOC16_HA:
8876 case R_PPC64_TOC16_DS:
8877 case R_PPC64_TOC16_LO_DS:
8878 break;
8879 }
8880
8881 r_symndx = ELF64_R_SYM (rel->r_info);
8882 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8883 r_symndx, ibfd))
8884 goto error_ret;
8885
8886 if (sym_sec != toc)
8887 continue;
8888
8889 if (h != NULL)
8890 val = h->root.u.def.value;
8891 else
8892 val = sym->st_value;
8893 val += rel->r_addend;
8894
8895 if (val >= toc->size)
8896 continue;
8897
8898 /* Anything in the toc ought to be aligned to 8 bytes.
8899 If not, don't mark as unused. */
8900 if (val & 7)
8901 continue;
8902
8903 if (skip == NULL)
8904 {
854b41e7 8905 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8906 if (skip == NULL)
8907 goto error_ret;
8908 }
8909
ba761f19 8910 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8911 }
8912
8913 if (elf_section_data (sec)->relocs != relstart)
8914 free (relstart);
8915 }
8916
ba761f19
AM
8917 /* For largetoc loads of address constants, we can convert
8918 . addis rx,2,addr@got@ha
8919 . ld ry,addr@got@l(rx)
8920 to
8921 . addis rx,2,addr@toc@ha
8922 . addi ry,rx,addr@toc@l
8923 when addr is within 2G of the toc pointer. This then means
8924 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8925
ba761f19
AM
8926 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8927 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8928 && toc->reloc_count != 0)
8929 {
8930 /* Read toc relocs. */
425b145b
AM
8931 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8932 info->keep_memory);
8933 if (toc_relocs == NULL)
ba761f19
AM
8934 goto error_ret;
8935
425b145b 8936 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8937 {
8938 enum elf_ppc64_reloc_type r_type;
8939 unsigned long r_symndx;
8940 asection *sym_sec;
8941 struct elf_link_hash_entry *h;
8942 Elf_Internal_Sym *sym;
8943 bfd_vma val, addr;
8944
8945 r_type = ELF64_R_TYPE (rel->r_info);
8946 if (r_type != R_PPC64_ADDR64)
8947 continue;
8948
8949 r_symndx = ELF64_R_SYM (rel->r_info);
8950 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8951 r_symndx, ibfd))
8952 goto error_ret;
8953
425b145b 8954 if (sym_sec == NULL
dbaa2011 8955 || discarded_section (sym_sec))
425b145b
AM
8956 continue;
8957
afe397ea 8958 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8959 continue;
8960
8961 if (h != NULL)
bddc25c9
AM
8962 {
8963 if (h->type == STT_GNU_IFUNC)
8964 continue;
8965 val = h->root.u.def.value;
8966 }
ba761f19 8967 else
bddc25c9
AM
8968 {
8969 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8970 continue;
8971 val = sym->st_value;
8972 }
ba761f19
AM
8973 val += rel->r_addend;
8974 val += sym_sec->output_section->vma + sym_sec->output_offset;
8975
8976 /* We don't yet know the exact toc pointer value, but we
8977 know it will be somewhere in the toc section. Don't
8978 optimize if the difference from any possible toc
8979 pointer is outside [ff..f80008000, 7fff7fff]. */
8980 addr = toc->output_section->vma + TOC_BASE_OFF;
8981 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8982 continue;
8983
8984 addr = toc->output_section->vma + toc->output_section->rawsize;
8985 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8986 continue;
8987
8988 if (skip == NULL)
8989 {
8990 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8991 if (skip == NULL)
8992 goto error_ret;
8993 }
8994
8995 skip[rel->r_offset >> 3]
425b145b 8996 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8997 }
ba761f19
AM
8998 }
8999
c5614fa4
AM
9000 if (skip == NULL)
9001 continue;
9002
9003 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9004 if (used == NULL)
9005 {
9006 error_ret:
9007 if (local_syms != NULL
9008 && symtab_hdr->contents != (unsigned char *) local_syms)
9009 free (local_syms);
9010 if (sec != NULL
9011 && relstart != NULL
9012 && elf_section_data (sec)->relocs != relstart)
9013 free (relstart);
425b145b
AM
9014 if (toc_relocs != NULL
9015 && elf_section_data (toc)->relocs != toc_relocs)
9016 free (toc_relocs);
c5614fa4
AM
9017 if (skip != NULL)
9018 free (skip);
9019 return FALSE;
9020 }
9021
30038c59
AM
9022 /* Now check all kept sections that might reference the toc.
9023 Check the toc itself last. */
9024 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9025 : ibfd->sections);
c5614fa4 9026 sec != NULL;
c5614fa4 9027 sec = (sec == toc ? NULL
c5614fa4 9028 : sec->next == NULL ? toc
30038c59 9029 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9030 : sec->next))
9031 {
9032 int repeat;
9033
9034 if (sec->reloc_count == 0
dbaa2011 9035 || discarded_section (sec)
c5614fa4
AM
9036 || get_opd_info (sec)
9037 || (sec->flags & SEC_ALLOC) == 0
9038 || (sec->flags & SEC_DEBUGGING) != 0)
9039 continue;
9040
854b41e7
AM
9041 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9042 info->keep_memory);
c5614fa4 9043 if (relstart == NULL)
2915c55b
JK
9044 {
9045 free (used);
9046 goto error_ret;
9047 }
c5614fa4
AM
9048
9049 /* Mark toc entries referenced as used. */
c5614fa4 9050 do
d4f1ee75
AM
9051 {
9052 repeat = 0;
9053 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9054 {
9055 enum elf_ppc64_reloc_type r_type;
9056 unsigned long r_symndx;
9057 asection *sym_sec;
9058 struct elf_link_hash_entry *h;
9059 Elf_Internal_Sym *sym;
9060 bfd_vma val;
9061 enum {no_check, check_lo, check_ha} insn_check;
98528052 9062
d4f1ee75
AM
9063 r_type = ELF64_R_TYPE (rel->r_info);
9064 switch (r_type)
9065 {
9066 default:
9067 insn_check = no_check;
9068 break;
98528052 9069
d4f1ee75
AM
9070 case R_PPC64_GOT_TLSLD16_HA:
9071 case R_PPC64_GOT_TLSGD16_HA:
9072 case R_PPC64_GOT_TPREL16_HA:
9073 case R_PPC64_GOT_DTPREL16_HA:
9074 case R_PPC64_GOT16_HA:
9075 case R_PPC64_TOC16_HA:
9076 insn_check = check_ha;
9077 break;
98528052 9078
d4f1ee75
AM
9079 case R_PPC64_GOT_TLSLD16_LO:
9080 case R_PPC64_GOT_TLSGD16_LO:
9081 case R_PPC64_GOT_TPREL16_LO_DS:
9082 case R_PPC64_GOT_DTPREL16_LO_DS:
9083 case R_PPC64_GOT16_LO:
9084 case R_PPC64_GOT16_LO_DS:
9085 case R_PPC64_TOC16_LO:
9086 case R_PPC64_TOC16_LO_DS:
9087 insn_check = check_lo;
9088 break;
9089 }
560c8763 9090
d4f1ee75
AM
9091 if (insn_check != no_check)
9092 {
9093 bfd_vma off = rel->r_offset & ~3;
9094 unsigned char buf[4];
9095 unsigned int insn;
c5614fa4 9096
d4f1ee75
AM
9097 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9098 {
9099 free (used);
9100 goto error_ret;
9101 }
9102 insn = bfd_get_32 (ibfd, buf);
9103 if (insn_check == check_lo
9104 ? !ok_lo_toc_insn (insn)
9105 : ((insn & ((0x3f << 26) | 0x1f << 16))
9106 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9107 {
9108 char str[12];
9109
9110 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9111 sprintf (str, "%#08x", insn);
9112 info->callbacks->einfo
9113 (_("%P: %H: toc optimization is not supported for"
9114 " %s instruction.\n"),
9115 ibfd, sec, rel->r_offset & ~3, str);
9116 }
9117 }
c5614fa4 9118
d4f1ee75
AM
9119 switch (r_type)
9120 {
9121 case R_PPC64_TOC16:
9122 case R_PPC64_TOC16_LO:
9123 case R_PPC64_TOC16_HI:
9124 case R_PPC64_TOC16_HA:
9125 case R_PPC64_TOC16_DS:
9126 case R_PPC64_TOC16_LO_DS:
9127 /* In case we're taking addresses of toc entries. */
9128 case R_PPC64_ADDR64:
9129 break;
c5614fa4 9130
d4f1ee75
AM
9131 default:
9132 continue;
9133 }
c5614fa4 9134
d4f1ee75
AM
9135 r_symndx = ELF64_R_SYM (rel->r_info);
9136 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9137 r_symndx, ibfd))
9138 {
9139 free (used);
9140 goto error_ret;
9141 }
c5614fa4 9142
d4f1ee75
AM
9143 if (sym_sec != toc)
9144 continue;
c5614fa4 9145
d4f1ee75
AM
9146 if (h != NULL)
9147 val = h->root.u.def.value;
9148 else
9149 val = sym->st_value;
9150 val += rel->r_addend;
ba761f19 9151
d4f1ee75
AM
9152 if (val >= toc->size)
9153 continue;
ba761f19 9154
d4f1ee75
AM
9155 if ((skip[val >> 3] & can_optimize) != 0)
9156 {
9157 bfd_vma off;
9158 unsigned char opc;
9159
9160 switch (r_type)
9161 {
9162 case R_PPC64_TOC16_HA:
ba761f19 9163 break;
ba761f19 9164
d4f1ee75
AM
9165 case R_PPC64_TOC16_LO_DS:
9166 off = rel->r_offset;
9167 off += (bfd_big_endian (ibfd) ? -2 : 3);
9168 if (!bfd_get_section_contents (ibfd, sec, &opc,
9169 off, 1))
9170 {
9171 free (used);
9172 goto error_ret;
9173 }
9174 if ((opc & (0x3f << 2)) == (58u << 2))
9175 break;
9176 /* Fall thru */
ba761f19 9177
d4f1ee75
AM
9178 default:
9179 /* Wrong sort of reloc, or not a ld. We may
9180 as well clear ref_from_discarded too. */
9181 skip[val >> 3] = 0;
9182 }
9183 }
9184
9185 if (sec != toc)
9186 used[val >> 3] = 1;
9187 /* For the toc section, we only mark as used if this
9188 entry itself isn't unused. */
9189 else if ((used[rel->r_offset >> 3]
9190 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9191 && !used[val >> 3])
9192 {
9193 /* Do all the relocs again, to catch reference
9194 chains. */
9195 repeat = 1;
9196 used[val >> 3] = 1;
9197 }
9198 }
9199 }
c5614fa4 9200 while (repeat);
854b41e7
AM
9201
9202 if (elf_section_data (sec)->relocs != relstart)
9203 free (relstart);
c5614fa4
AM
9204 }
9205
9206 /* Merge the used and skip arrays. Assume that TOC
9207 doublewords not appearing as either used or unused belong
9208 to to an entry more than one doubleword in size. */
9209 for (drop = skip, keep = used, last = 0, some_unused = 0;
9210 drop < skip + (toc->size + 7) / 8;
9211 ++drop, ++keep)
9212 {
9213 if (*keep)
9214 {
ba761f19
AM
9215 *drop &= ~ref_from_discarded;
9216 if ((*drop & can_optimize) != 0)
9217 some_unused = 1;
c5614fa4
AM
9218 last = 0;
9219 }
b140b010 9220 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9221 {
9222 some_unused = 1;
ba761f19 9223 last = ref_from_discarded;
c5614fa4
AM
9224 }
9225 else
9226 *drop = last;
9227 }
9228
9229 free (used);
9230
9231 if (some_unused)
9232 {
9233 bfd_byte *contents, *src;
9234 unsigned long off;
d62b3684 9235 Elf_Internal_Sym *sym;
ba761f19 9236 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9237
9238 /* Shuffle the toc contents, and at the same time convert the
9239 skip array from booleans into offsets. */
9240 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9241 goto error_ret;
9242
9243 elf_section_data (toc)->this_hdr.contents = contents;
9244
9245 for (src = contents, off = 0, drop = skip;
9246 src < contents + toc->size;
9247 src += 8, ++drop)
9248 {
ba761f19
AM
9249 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9250 off += 8;
c5614fa4
AM
9251 else if (off != 0)
9252 {
9253 *drop = off;
9254 memcpy (src - off, src, 8);
9255 }
9256 }
854b41e7 9257 *drop = off;
c5614fa4
AM
9258 toc->rawsize = toc->size;
9259 toc->size = src - contents - off;
9260
ba761f19
AM
9261 /* Adjust addends for relocs against the toc section sym,
9262 and optimize any accesses we can. */
c5614fa4
AM
9263 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9264 {
9265 if (sec->reloc_count == 0
dbaa2011 9266 || discarded_section (sec))
c5614fa4
AM
9267 continue;
9268
9269 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9270 info->keep_memory);
c5614fa4
AM
9271 if (relstart == NULL)
9272 goto error_ret;
9273
9274 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9275 {
9276 enum elf_ppc64_reloc_type r_type;
9277 unsigned long r_symndx;
9278 asection *sym_sec;
9279 struct elf_link_hash_entry *h;
854b41e7 9280 bfd_vma val;
c5614fa4
AM
9281
9282 r_type = ELF64_R_TYPE (rel->r_info);
9283 switch (r_type)
9284 {
9285 default:
9286 continue;
9287
9288 case R_PPC64_TOC16:
9289 case R_PPC64_TOC16_LO:
9290 case R_PPC64_TOC16_HI:
9291 case R_PPC64_TOC16_HA:
9292 case R_PPC64_TOC16_DS:
9293 case R_PPC64_TOC16_LO_DS:
9294 case R_PPC64_ADDR64:
9295 break;
9296 }
9297
9298 r_symndx = ELF64_R_SYM (rel->r_info);
9299 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9300 r_symndx, ibfd))
9301 goto error_ret;
9302
ba761f19 9303 if (sym_sec != toc)
c5614fa4
AM
9304 continue;
9305
ba761f19
AM
9306 if (h != NULL)
9307 val = h->root.u.def.value;
9308 else
9309 {
9310 val = sym->st_value;
9311 if (val != 0)
9312 local_toc_syms = TRUE;
9313 }
9314
9315 val += rel->r_addend;
854b41e7
AM
9316
9317 if (val > toc->rawsize)
9318 val = toc->rawsize;
ba761f19
AM
9319 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9320 continue;
9321 else if ((skip[val >> 3] & can_optimize) != 0)
9322 {
9323 Elf_Internal_Rela *tocrel
425b145b 9324 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9325 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9326
9327 switch (r_type)
9328 {
9329 case R_PPC64_TOC16_HA:
9330 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9331 break;
9332
9333 case R_PPC64_TOC16_LO_DS:
9334 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9335 break;
9336
9337 default:
28942f62
AM
9338 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9339 ppc_howto_init ();
b140b010 9340 info->callbacks->einfo
bc30df16 9341 (_("%P: %H: %s references "
b140b010
AM
9342 "optimized away TOC entry\n"),
9343 ibfd, sec, rel->r_offset,
9344 ppc64_elf_howto_table[r_type]->name);
9345 bfd_set_error (bfd_error_bad_value);
9346 goto error_ret;
ba761f19
AM
9347 }
9348 rel->r_addend = tocrel->r_addend;
9349 elf_section_data (sec)->relocs = relstart;
9350 continue;
9351 }
9352
9353 if (h != NULL || sym->st_value != 0)
9354 continue;
854b41e7
AM
9355
9356 rel->r_addend -= skip[val >> 3];
9357 elf_section_data (sec)->relocs = relstart;
c5614fa4 9358 }
854b41e7
AM
9359
9360 if (elf_section_data (sec)->relocs != relstart)
9361 free (relstart);
c5614fa4
AM
9362 }
9363
9364 /* We shouldn't have local or global symbols defined in the TOC,
9365 but handle them anyway. */
df22d223
AM
9366 if (local_syms != NULL)
9367 for (sym = local_syms;
9368 sym < local_syms + symtab_hdr->sh_info;
9369 ++sym)
9370 if (sym->st_value != 0
9371 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9372 {
9373 unsigned long i;
854b41e7 9374
df22d223
AM
9375 if (sym->st_value > toc->rawsize)
9376 i = toc->rawsize >> 3;
9377 else
9378 i = sym->st_value >> 3;
854b41e7 9379
df22d223
AM
9380 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9381 {
9382 if (local_toc_syms)
9383 (*_bfd_error_handler)
9384 (_("%s defined on removed toc entry"),
9385 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9386 do
9387 ++i;
9388 while ((skip[i] & (ref_from_discarded | can_optimize)));
9389 sym->st_value = (bfd_vma) i << 3;
9390 }
d62b3684 9391
df22d223
AM
9392 sym->st_value -= skip[i];
9393 symtab_hdr->contents = (unsigned char *) local_syms;
9394 }
c5614fa4 9395
854b41e7 9396 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9397 if (toc_inf.global_toc_syms)
9398 {
9399 toc_inf.toc = toc;
9400 toc_inf.skip = skip;
9401 toc_inf.global_toc_syms = FALSE;
9402 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9403 &toc_inf);
9404 }
854b41e7
AM
9405
9406 if (toc->reloc_count != 0)
9407 {
d4730f92 9408 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9409 Elf_Internal_Rela *wrel;
9410 bfd_size_type sz;
9411
854b41e7 9412 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9413 if (toc_relocs == NULL)
9414 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9415 info->keep_memory);
9416 if (toc_relocs == NULL)
9417 goto error_ret;
9418
425b145b
AM
9419 wrel = toc_relocs;
9420 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9421 if ((skip[rel->r_offset >> 3]
9422 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9423 {
9424 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9425 wrel->r_info = rel->r_info;
9426 wrel->r_addend = rel->r_addend;
9427 ++wrel;
9428 }
9429 else if (!dec_dynrel_count (rel->r_info, toc, info,
9430 &local_syms, NULL, NULL))
9431 goto error_ret;
9432
425b145b
AM
9433 elf_section_data (toc)->relocs = toc_relocs;
9434 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9435 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9436 sz = rel_hdr->sh_entsize;
9437 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9438 }
c5614fa4 9439 }
28be611c
AM
9440 else if (toc_relocs != NULL
9441 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9442 free (toc_relocs);
c5614fa4
AM
9443
9444 if (local_syms != NULL
9445 && symtab_hdr->contents != (unsigned char *) local_syms)
9446 {
9447 if (!info->keep_memory)
9448 free (local_syms);
9449 else
9450 symtab_hdr->contents = (unsigned char *) local_syms;
9451 }
9452 free (skip);
9453 }
9454
9455 return TRUE;
9456}
9457
1bbe0902
AM
9458/* Return true iff input section I references the TOC using
9459 instructions limited to +/-32k offsets. */
9460
9461bfd_boolean
9462ppc64_elf_has_small_toc_reloc (asection *i)
9463{
9464 return (is_ppc64_elf (i->owner)
9465 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9466}
9467
927be08e
AM
9468/* Allocate space for one GOT entry. */
9469
9470static void
9471allocate_got (struct elf_link_hash_entry *h,
9472 struct bfd_link_info *info,
9473 struct got_entry *gent)
9474{
9475 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9476 bfd_boolean dyn;
9477 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9478 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9479 ? 16 : 8);
9480 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9481 ? 2 : 1) * sizeof (Elf64_External_Rela);
9482 asection *got = ppc64_elf_tdata (gent->owner)->got;
9483
9484 gent->got.offset = got->size;
9485 got->size += entsize;
9486
9487 dyn = htab->elf.dynamic_sections_created;
19e08130 9488 if (h->type == STT_GNU_IFUNC)
927be08e 9489 {
33e44f2e 9490 htab->elf.irelplt->size += rentsize;
19e08130 9491 htab->got_reli_size += rentsize;
927be08e 9492 }
0e1862bb 9493 else if ((bfd_link_pic (info)
19e08130
AM
9494 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9495 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9496 || h->root.type != bfd_link_hash_undefweak))
927be08e 9497 {
19e08130 9498 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9499 relgot->size += rentsize;
927be08e
AM
9500 }
9501}
9502
7865406b
AM
9503/* This function merges got entries in the same toc group. */
9504
9505static void
9506merge_got_entries (struct got_entry **pent)
9507{
9508 struct got_entry *ent, *ent2;
9509
9510 for (ent = *pent; ent != NULL; ent = ent->next)
9511 if (!ent->is_indirect)
9512 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9513 if (!ent2->is_indirect
9514 && ent2->addend == ent->addend
9515 && ent2->tls_type == ent->tls_type
9516 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9517 {
9518 ent2->is_indirect = TRUE;
9519 ent2->got.ent = ent;
9520 }
9521}
9522
65f38f15
AM
9523/* Allocate space in .plt, .got and associated reloc sections for
9524 dynamic relocs. */
5bd4f169 9525
b34976b6 9526static bfd_boolean
4ce794b7 9527allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9528{
65f38f15
AM
9529 struct bfd_link_info *info;
9530 struct ppc_link_hash_table *htab;
5bd4f169 9531 asection *s;
65f38f15 9532 struct ppc_link_hash_entry *eh;
6061a67d 9533 struct elf_dyn_relocs *p;
0b8bcf0d 9534 struct got_entry **pgent, *gent;
5bd4f169 9535
e92d460e 9536 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9537 return TRUE;
5bd4f169 9538
65f38f15
AM
9539 info = (struct bfd_link_info *) inf;
9540 htab = ppc_hash_table (info);
4dfe6ac6
NC
9541 if (htab == NULL)
9542 return FALSE;
5bd4f169 9543
e054468f
AM
9544 if ((htab->elf.dynamic_sections_created
9545 && h->dynindx != -1
0e1862bb 9546 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
e054468f 9547 || h->type == STT_GNU_IFUNC)
5bd4f169 9548 {
411e1bfb
AM
9549 struct plt_entry *pent;
9550 bfd_boolean doneone = FALSE;
9551 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9552 if (pent->plt.refcount > 0)
9553 {
25f23106
AM
9554 if (!htab->elf.dynamic_sections_created
9555 || h->dynindx == -1)
e054468f 9556 {
33e44f2e 9557 s = htab->elf.iplt;
e054468f 9558 pent->plt.offset = s->size;
b9e5796b 9559 s->size += PLT_ENTRY_SIZE (htab);
33e44f2e 9560 s = htab->elf.irelplt;
e054468f
AM
9561 }
9562 else
9563 {
9564 /* If this is the first .plt entry, make room for the special
9565 first entry. */
33e44f2e 9566 s = htab->elf.splt;
e054468f 9567 if (s->size == 0)
b9e5796b 9568 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
e054468f
AM
9569
9570 pent->plt.offset = s->size;
9571
9572 /* Make room for this entry. */
b9e5796b 9573 s->size += PLT_ENTRY_SIZE (htab);
e054468f
AM
9574
9575 /* Make room for the .glink code. */
9576 s = htab->glink;
9577 if (s->size == 0)
9578 s->size += GLINK_CALL_STUB_SIZE;
b9e5796b
AM
9579 if (htab->opd_abi)
9580 {
9581 /* We need bigger stubs past index 32767. */
9582 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9583 s->size += 4;
9584 s->size += 2*4;
9585 }
9586 else
e054468f 9587 s->size += 4;
e054468f
AM
9588
9589 /* We also need to make an entry in the .rela.plt section. */
33e44f2e 9590 s = htab->elf.srelplt;
e054468f 9591 }
eea6121a 9592 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
9593 doneone = TRUE;
9594 }
9595 else
9596 pent->plt.offset = (bfd_vma) -1;
9597 if (!doneone)
65f38f15 9598 {
411e1bfb 9599 h->plt.plist = NULL;
f5385ebf 9600 h->needs_plt = 0;
65f38f15
AM
9601 }
9602 }
9603 else
9604 {
411e1bfb 9605 h->plt.plist = NULL;
f5385ebf 9606 h->needs_plt = 0;
65f38f15
AM
9607 }
9608
951fd09b
AM
9609 eh = (struct ppc_link_hash_entry *) h;
9610 /* Run through the TLS GD got entries first if we're changing them
9611 to TPREL. */
e7b938ca 9612 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9613 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9614 if (gent->got.refcount > 0
9615 && (gent->tls_type & TLS_GD) != 0)
9616 {
9617 /* This was a GD entry that has been converted to TPREL. If
9618 there happens to be a TPREL entry we can use that one. */
9619 struct got_entry *ent;
9620 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9621 if (ent->got.refcount > 0
9622 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9623 && ent->addend == gent->addend
9624 && ent->owner == gent->owner)
951fd09b
AM
9625 {
9626 gent->got.refcount = 0;
9627 break;
9628 }
9629
9630 /* If not, then we'll be using our own TPREL entry. */
9631 if (gent->got.refcount != 0)
9632 gent->tls_type = TLS_TLS | TLS_TPREL;
9633 }
9634
7865406b
AM
9635 /* Remove any list entry that won't generate a word in the GOT before
9636 we call merge_got_entries. Otherwise we risk merging to empty
9637 entries. */
0b8bcf0d
AM
9638 pgent = &h->got.glist;
9639 while ((gent = *pgent) != NULL)
411e1bfb 9640 if (gent->got.refcount > 0)
7865406b
AM
9641 {
9642 if ((gent->tls_type & TLS_LD) != 0
9643 && !h->def_dynamic)
9644 {
9645 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9646 *pgent = gent->next;
9647 }
9648 else
9649 pgent = &gent->next;
9650 }
9651 else
9652 *pgent = gent->next;
9653
9654 if (!htab->do_multi_toc)
9655 merge_got_entries (&h->got.glist);
9656
9657 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9658 if (!gent->is_indirect)
411e1bfb
AM
9659 {
9660 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9661 Undefined weak syms won't yet be marked as dynamic,
9662 nor will all TLS symbols. */
411e1bfb 9663 if (h->dynindx == -1
b099ab9f 9664 && !h->forced_local
25f23106 9665 && h->type != STT_GNU_IFUNC
b099ab9f 9666 && htab->elf.dynamic_sections_created)
411e1bfb 9667 {
c152c796 9668 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9669 return FALSE;
9670 }
65f38f15 9671
0c8d6e5c 9672 if (!is_ppc64_elf (gent->owner))
927be08e 9673 abort ();
0ffa91dd 9674
927be08e 9675 allocate_got (h, info, gent);
411e1bfb 9676 }
65f38f15 9677
b099ab9f 9678 if (eh->dyn_relocs == NULL
25f23106 9679 || (!htab->elf.dynamic_sections_created
14b5f73f 9680 && h->type != STT_GNU_IFUNC))
b34976b6 9681 return TRUE;
65f38f15
AM
9682
9683 /* In the shared -Bsymbolic case, discard space allocated for
9684 dynamic pc-relative relocs against symbols which turn out to be
9685 defined in regular objects. For the normal shared case, discard
9686 space for relocs that have become local due to symbol visibility
9687 changes. */
9688
0e1862bb 9689 if (bfd_link_pic (info))
65f38f15 9690 {
9c7a29a3 9691 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 9692 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
9693 generated via assembly. We want calls to protected symbols to
9694 resolve directly to the function rather than going via the plt.
9695 If people want function pointer comparisons to work as expected
9696 then they should avoid writing weird assembly. */
09695f56 9697 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 9698 {
6061a67d 9699 struct elf_dyn_relocs **pp;
65f38f15
AM
9700
9701 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 9702 {
65f38f15
AM
9703 p->count -= p->pc_count;
9704 p->pc_count = 0;
9705 if (p->count == 0)
9706 *pp = p->next;
9707 else
9708 pp = &p->next;
5bd4f169 9709 }
65f38f15 9710 }
4e795f50
AM
9711
9712 /* Also discard relocs on undefined weak syms with non-default
9713 visibility. */
cab87ef9
AM
9714 if (eh->dyn_relocs != NULL
9715 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
9716 {
9717 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9718 eh->dyn_relocs = NULL;
9719
9720 /* Make sure this symbol is output as a dynamic symbol.
9721 Undefined weak syms won't yet be marked as dynamic. */
9722 else if (h->dynindx == -1
9723 && !h->forced_local)
9724 {
9725 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9726 return FALSE;
9727 }
9728 }
65f38f15 9729 }
25f23106
AM
9730 else if (h->type == STT_GNU_IFUNC)
9731 {
9732 if (!h->non_got_ref)
9733 eh->dyn_relocs = NULL;
9734 }
f4656909 9735 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
9736 {
9737 /* For the non-shared case, discard space for relocs against
9738 symbols which turn out to need copy relocs or are not
9739 dynamic. */
9740
f5385ebf 9741 if (!h->non_got_ref
f5385ebf 9742 && !h->def_regular)
65f38f15
AM
9743 {
9744 /* Make sure this symbol is output as a dynamic symbol.
9745 Undefined weak syms won't yet be marked as dynamic. */
9746 if (h->dynindx == -1
f5385ebf 9747 && !h->forced_local)
65f38f15 9748 {
c152c796 9749 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 9750 return FALSE;
65f38f15
AM
9751 }
9752
9753 /* If that succeeded, we know we'll be keeping all the
9754 relocs. */
9755 if (h->dynindx != -1)
9756 goto keep;
9757 }
9758
9759 eh->dyn_relocs = NULL;
9760
ec338859 9761 keep: ;
65f38f15
AM
9762 }
9763
9764 /* Finally, allocate space. */
9765 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9766 {
9767 asection *sreloc = elf_section_data (p->sec)->sreloc;
19e08130 9768 if (eh->elf.type == STT_GNU_IFUNC)
33e44f2e 9769 sreloc = htab->elf.irelplt;
eea6121a 9770 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9771 }
9772
b34976b6 9773 return TRUE;
65f38f15
AM
9774}
9775
a345bc8d
AM
9776/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9777 to set up space for global entry stubs. These are put in glink,
9778 after the branch table. */
65f38f15 9779
b34976b6 9780static bfd_boolean
a345bc8d 9781size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9782{
a345bc8d
AM
9783 struct bfd_link_info *info;
9784 struct ppc_link_hash_table *htab;
9785 struct plt_entry *pent;
9786 asection *s;
65f38f15 9787
a345bc8d
AM
9788 if (h->root.type == bfd_link_hash_indirect)
9789 return TRUE;
65f38f15 9790
a345bc8d
AM
9791 if (!h->pointer_equality_needed)
9792 return TRUE;
65f38f15 9793
a345bc8d
AM
9794 if (h->def_regular)
9795 return TRUE;
65f38f15 9796
a345bc8d
AM
9797 info = inf;
9798 htab = ppc_hash_table (info);
9799 if (htab == NULL)
9800 return FALSE;
9801
9802 s = htab->glink;
9803 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9804 if (pent->plt.offset != (bfd_vma) -1
9805 && pent->addend == 0)
9806 {
afe397ea
AM
9807 /* For ELFv2, if this symbol is not defined in a regular file
9808 and we are not generating a shared library or pie, then we
9809 need to define the symbol in the executable on a call stub.
9810 This is to avoid text relocations. */
a345bc8d 9811 s->size = (s->size + 15) & -16;
afe397ea
AM
9812 h->root.u.def.section = s;
9813 h->root.u.def.value = s->size;
a345bc8d
AM
9814 s->size += 16;
9815 break;
9816 }
9817 return TRUE;
9818}
9819
9820/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9821 read-only sections. */
9822
9823static bfd_boolean
9824maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9825{
9826 if (h->root.type == bfd_link_hash_indirect)
9827 return TRUE;
9828
9829 if (readonly_dynrelocs (h))
9830 {
9831 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9832
9833 /* Not an error, just cut short the traversal. */
9834 return FALSE;
65f38f15 9835 }
b34976b6 9836 return TRUE;
65f38f15
AM
9837}
9838
9839/* Set the sizes of the dynamic sections. */
9840
b34976b6 9841static bfd_boolean
ee67d69a 9842ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9843 struct bfd_link_info *info)
65f38f15
AM
9844{
9845 struct ppc_link_hash_table *htab;
9846 bfd *dynobj;
9847 asection *s;
b34976b6 9848 bfd_boolean relocs;
65f38f15 9849 bfd *ibfd;
7865406b 9850 struct got_entry *first_tlsld;
65f38f15
AM
9851
9852 htab = ppc_hash_table (info);
4dfe6ac6
NC
9853 if (htab == NULL)
9854 return FALSE;
9855
65f38f15
AM
9856 dynobj = htab->elf.dynobj;
9857 if (dynobj == NULL)
9858 abort ();
9859
9860 if (htab->elf.dynamic_sections_created)
9861 {
9862 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9863 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9864 {
3d4d4302 9865 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9866 if (s == NULL)
9867 abort ();
eea6121a 9868 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9869 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9870 }
9871 }
9872
9873 /* Set up .got offsets for local syms, and space for local dynamic
9874 relocs. */
c72f2fb2 9875 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9876 {
411e1bfb
AM
9877 struct got_entry **lgot_ents;
9878 struct got_entry **end_lgot_ents;
e054468f
AM
9879 struct plt_entry **local_plt;
9880 struct plt_entry **end_local_plt;
f961d9dd 9881 unsigned char *lgot_masks;
65f38f15
AM
9882 bfd_size_type locsymcount;
9883 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9884
0c8d6e5c 9885 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9886 continue;
9887
9888 for (s = ibfd->sections; s != NULL; s = s->next)
9889 {
19e08130 9890 struct ppc_dyn_relocs *p;
65f38f15 9891
6edfbbad 9892 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9893 {
ec338859
AM
9894 if (!bfd_is_abs_section (p->sec)
9895 && bfd_is_abs_section (p->sec->output_section))
9896 {
9897 /* Input section has been discarded, either because
9898 it is a copy of a linkonce section or due to
9899 linker script /DISCARD/, so we'll be discarding
9900 the relocs too. */
9901 }
248866a8 9902 else if (p->count != 0)
ec338859 9903 {
19e08130
AM
9904 asection *srel = elf_section_data (p->sec)->sreloc;
9905 if (p->ifunc)
33e44f2e 9906 srel = htab->elf.irelplt;
eea6121a 9907 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9908 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9909 info->flags |= DF_TEXTREL;
ec338859 9910 }
65f38f15
AM
9911 }
9912 }
9913
411e1bfb
AM
9914 lgot_ents = elf_local_got_ents (ibfd);
9915 if (!lgot_ents)
65f38f15
AM
9916 continue;
9917
0ffa91dd 9918 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9919 locsymcount = symtab_hdr->sh_info;
411e1bfb 9920 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9921 local_plt = (struct plt_entry **) end_lgot_ents;
9922 end_local_plt = local_plt + locsymcount;
f961d9dd 9923 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9924 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9925 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9926 {
0b8bcf0d 9927 struct got_entry **pent, *ent;
411e1bfb 9928
0b8bcf0d
AM
9929 pent = lgot_ents;
9930 while ((ent = *pent) != NULL)
411e1bfb
AM
9931 if (ent->got.refcount > 0)
9932 {
e7b938ca 9933 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9934 {
927be08e 9935 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9936 *pent = ent->next;
411e1bfb
AM
9937 }
9938 else
9939 {
19e08130
AM
9940 unsigned int ent_size = 8;
9941 unsigned int rel_size = sizeof (Elf64_External_Rela);
9942
eea6121a 9943 ent->got.offset = s->size;
e7b938ca 9944 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9945 {
19e08130
AM
9946 ent_size *= 2;
9947 rel_size *= 2;
9948 }
9949 s->size += ent_size;
9950 if ((*lgot_masks & PLT_IFUNC) != 0)
9951 {
33e44f2e 9952 htab->elf.irelplt->size += rel_size;
19e08130
AM
9953 htab->got_reli_size += rel_size;
9954 }
0e1862bb 9955 else if (bfd_link_pic (info))
19e08130
AM
9956 {
9957 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9958 srel->size += rel_size;
927be08e 9959 }
0b8bcf0d 9960 pent = &ent->next;
411e1bfb
AM
9961 }
9962 }
9963 else
0b8bcf0d 9964 *pent = ent->next;
65f38f15 9965 }
e054468f
AM
9966
9967 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9968 for (; local_plt < end_local_plt; ++local_plt)
9969 {
9970 struct plt_entry *ent;
9971
9972 for (ent = *local_plt; ent != NULL; ent = ent->next)
9973 if (ent->plt.refcount > 0)
9974 {
33e44f2e 9975 s = htab->elf.iplt;
e054468f 9976 ent->plt.offset = s->size;
b9e5796b 9977 s->size += PLT_ENTRY_SIZE (htab);
e054468f 9978
33e44f2e 9979 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9980 }
9981 else
9982 ent->plt.offset = (bfd_vma) -1;
9983 }
65f38f15
AM
9984 }
9985
9986 /* Allocate global sym .plt and .got entries, and space for global
9987 sym dynamic relocs. */
4ce794b7 9988 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
9989 /* Stash the end of glink branch table. */
9990 if (htab->glink != NULL)
9991 htab->glink->rawsize = htab->glink->size;
9992
0e1862bb 9993 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9994 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9995
7865406b 9996 first_tlsld = NULL;
c72f2fb2 9997 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9998 {
7865406b
AM
9999 struct got_entry *ent;
10000
0c8d6e5c 10001 if (!is_ppc64_elf (ibfd))
102890f0
AM
10002 continue;
10003
7865406b
AM
10004 ent = ppc64_tlsld_got (ibfd);
10005 if (ent->got.refcount > 0)
102890f0 10006 {
7865406b 10007 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10008 {
7865406b
AM
10009 ent->is_indirect = TRUE;
10010 ent->got.ent = first_tlsld;
10011 }
10012 else
10013 {
10014 if (first_tlsld == NULL)
10015 first_tlsld = ent;
10016 s = ppc64_elf_tdata (ibfd)->got;
10017 ent->got.offset = s->size;
10018 ent->owner = ibfd;
10019 s->size += 16;
0e1862bb 10020 if (bfd_link_pic (info))
7865406b
AM
10021 {
10022 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10023 srel->size += sizeof (Elf64_External_Rela);
10024 }
102890f0
AM
10025 }
10026 }
10027 else
7865406b 10028 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10029 }
10030
65f38f15
AM
10031 /* We now have determined the sizes of the various dynamic sections.
10032 Allocate memory for them. */
b34976b6 10033 relocs = FALSE;
65f38f15
AM
10034 for (s = dynobj->sections; s != NULL; s = s->next)
10035 {
10036 if ((s->flags & SEC_LINKER_CREATED) == 0)
10037 continue;
10038
4ce794b7 10039 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10040 /* These haven't been allocated yet; don't strip. */
10041 continue;
33e44f2e
AM
10042 else if (s == htab->elf.sgot
10043 || s == htab->elf.splt
10044 || s == htab->elf.iplt
c456f082
AM
10045 || s == htab->glink
10046 || s == htab->dynbss)
65f38f15
AM
10047 {
10048 /* Strip this section if we don't need it; see the
10049 comment below. */
5bd4f169 10050 }
58d180e8
AM
10051 else if (s == htab->glink_eh_frame)
10052 {
10053 if (!bfd_is_abs_section (s->output_section))
10054 /* Not sized yet. */
10055 continue;
10056 }
70cc837d 10057 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10058 {
c456f082 10059 if (s->size != 0)
5bd4f169 10060 {
33e44f2e 10061 if (s != htab->elf.srelplt)
b34976b6 10062 relocs = TRUE;
5bd4f169
AM
10063
10064 /* We use the reloc_count field as a counter if we need
10065 to copy relocs into the output file. */
10066 s->reloc_count = 0;
10067 }
10068 }
65f38f15 10069 else
5bd4f169
AM
10070 {
10071 /* It's not one of our sections, so don't allocate space. */
10072 continue;
10073 }
10074
eea6121a 10075 if (s->size == 0)
5bd4f169 10076 {
c456f082
AM
10077 /* If we don't need this section, strip it from the
10078 output file. This is mostly to handle .rela.bss and
10079 .rela.plt. We must create both sections in
10080 create_dynamic_sections, because they must be created
10081 before the linker maps input sections to output
10082 sections. The linker does that before
10083 adjust_dynamic_symbol is called, and it is that
10084 function which decides whether anything needs to go
10085 into these sections. */
8423293d 10086 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10087 continue;
10088 }
10089
c456f082 10090 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10091 continue;
10092
65f38f15
AM
10093 /* Allocate memory for the section contents. We use bfd_zalloc
10094 here in case unused entries are not reclaimed before the
10095 section's contents are written out. This should not happen,
411e1bfb
AM
10096 but this way if it does we get a R_PPC64_NONE reloc in .rela
10097 sections instead of garbage.
10098 We also rely on the section contents being zero when writing
10099 the GOT. */
eea6121a 10100 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10101 if (s->contents == NULL)
b34976b6 10102 return FALSE;
5bd4f169
AM
10103 }
10104
c72f2fb2 10105 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10106 {
0c8d6e5c 10107 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10108 continue;
10109
e717da7e 10110 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10111 if (s != NULL && s != htab->elf.sgot)
e717da7e 10112 {
eea6121a 10113 if (s->size == 0)
8423293d 10114 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10115 else
10116 {
eea6121a 10117 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10118 if (s->contents == NULL)
10119 return FALSE;
10120 }
10121 }
10122 s = ppc64_elf_tdata (ibfd)->relgot;
10123 if (s != NULL)
10124 {
eea6121a 10125 if (s->size == 0)
8423293d 10126 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10127 else
10128 {
eea6121a 10129 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10130 if (s->contents == NULL)
10131 return FALSE;
10132 relocs = TRUE;
10133 s->reloc_count = 0;
10134 }
10135 }
10136 }
10137
e86ce104 10138 if (htab->elf.dynamic_sections_created)
5bd4f169 10139 {
e8910a83
AM
10140 bfd_boolean tls_opt;
10141
5bd4f169
AM
10142 /* Add some entries to the .dynamic section. We fill in the
10143 values later, in ppc64_elf_finish_dynamic_sections, but we
10144 must add the entries now so that we get the correct size for
10145 the .dynamic section. The DT_DEBUG entry is filled in by the
10146 dynamic linker and used by the debugger. */
dc810e39 10147#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10148 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10149
0e1862bb 10150 if (bfd_link_executable (info))
5bd4f169 10151 {
dc810e39 10152 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10153 return FALSE;
5bd4f169
AM
10154 }
10155
33e44f2e 10156 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10157 {
dc810e39
AM
10158 if (!add_dynamic_entry (DT_PLTGOT, 0)
10159 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10160 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10161 || !add_dynamic_entry (DT_JMPREL, 0)
10162 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10163 return FALSE;
5bd4f169
AM
10164 }
10165
ee67d69a 10166 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10167 {
10168 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10169 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10170 return FALSE;
19397422
AM
10171 }
10172
7c9cf415 10173 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10174 && htab->tls_get_addr_fd != NULL
10175 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10176 if (tls_opt || !htab->opd_abi)
10177 {
10178 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10179 return FALSE;
10180 }
a7f2871e 10181
5bd4f169
AM
10182 if (relocs)
10183 {
dc810e39
AM
10184 if (!add_dynamic_entry (DT_RELA, 0)
10185 || !add_dynamic_entry (DT_RELASZ, 0)
10186 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10187 return FALSE;
5bd4f169 10188
65f38f15
AM
10189 /* If any dynamic relocs apply to a read-only section,
10190 then we need a DT_TEXTREL entry. */
248866a8 10191 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10192 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10193
65f38f15 10194 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10195 {
65f38f15 10196 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10197 return FALSE;
5bd4f169 10198 }
5bd4f169 10199 }
5bd4f169 10200 }
65f38f15 10201#undef add_dynamic_entry
5bd4f169 10202
b34976b6 10203 return TRUE;
5bd4f169
AM
10204}
10205
a345bc8d
AM
10206/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10207
10208static bfd_boolean
10209ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10210{
10211 if (h->plt.plist != NULL
10212 && !h->def_regular
10213 && !h->pointer_equality_needed)
10214 return FALSE;
10215
10216 return _bfd_elf_hash_symbol (h);
10217}
10218
721956f4 10219/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10220
4ce794b7
AM
10221static inline enum ppc_stub_type
10222ppc_type_of_stub (asection *input_sec,
10223 const Elf_Internal_Rela *rel,
10224 struct ppc_link_hash_entry **hash,
e054468f 10225 struct plt_entry **plt_ent,
6911b7dc
AM
10226 bfd_vma destination,
10227 unsigned long local_off)
5bd4f169 10228{
721956f4
AM
10229 struct ppc_link_hash_entry *h = *hash;
10230 bfd_vma location;
10231 bfd_vma branch_offset;
10232 bfd_vma max_branch_offset;
4ce794b7 10233 enum elf_ppc64_reloc_type r_type;
5bd4f169 10234
721956f4
AM
10235 if (h != NULL)
10236 {
e054468f 10237 struct plt_entry *ent;
7fe2b9a6 10238 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10239 if (h->oh != NULL
10240 && h->oh->is_func_descriptor)
7b8f6675
AM
10241 {
10242 fdh = ppc_follow_link (h->oh);
10243 *hash = fdh;
10244 }
8387904d 10245
e054468f
AM
10246 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10247 if (ent->addend == rel->r_addend
10248 && ent->plt.offset != (bfd_vma) -1)
10249 {
e054468f
AM
10250 *plt_ent = ent;
10251 return ppc_stub_plt_call;
10252 }
5bd4f169 10253
7fe2b9a6
AM
10254 /* Here, we know we don't have a plt entry. If we don't have a
10255 either a defined function descriptor or a defined entry symbol
10256 in a regular object file, then it is pointless trying to make
10257 any other type of stub. */
854b41e7
AM
10258 if (!is_static_defined (&fdh->elf)
10259 && !is_static_defined (&h->elf))
721956f4 10260 return ppc_stub_none;
5d1634d7 10261 }
e054468f
AM
10262 else if (elf_local_got_ents (input_sec->owner) != NULL)
10263 {
10264 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10265 struct plt_entry **local_plt = (struct plt_entry **)
10266 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10267 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10268
10269 if (local_plt[r_symndx] != NULL)
10270 {
10271 struct plt_entry *ent;
10272
10273 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10274 if (ent->addend == rel->r_addend
10275 && ent->plt.offset != (bfd_vma) -1)
10276 {
10277 *plt_ent = ent;
10278 return ppc_stub_plt_call;
10279 }
10280 }
10281 }
5d1634d7 10282
721956f4
AM
10283 /* Determine where the call point is. */
10284 location = (input_sec->output_offset
10285 + input_sec->output_section->vma
10286 + rel->r_offset);
5d1634d7 10287
721956f4
AM
10288 branch_offset = destination - location;
10289 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10290
721956f4
AM
10291 /* Determine if a long branch stub is needed. */
10292 max_branch_offset = 1 << 25;
4ce794b7 10293 if (r_type != R_PPC64_REL24)
721956f4 10294 max_branch_offset = 1 << 15;
5d1634d7 10295
6911b7dc 10296 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10297 /* We need a stub. Figure out whether a long_branch or plt_branch
10298 is needed later. */
10299 return ppc_stub_long_branch;
5d1634d7 10300
721956f4 10301 return ppc_stub_none;
5d1634d7
AM
10302}
10303
794e51c0
AM
10304/* With power7 weakly ordered memory model, it is possible for ld.so
10305 to update a plt entry in one thread and have another thread see a
10306 stale zero toc entry. To avoid this we need some sort of acquire
10307 barrier in the call stub. One solution is to make the load of the
10308 toc word seem to appear to depend on the load of the function entry
10309 word. Another solution is to test for r2 being zero, and branch to
10310 the appropriate glink entry if so.
10311
10312 . fake dep barrier compare
71a39c98
AM
10313 . ld 12,xxx(2) ld 12,xxx(2)
10314 . mtctr 12 mtctr 12
10315 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10316 . add 2,2,11 cmpldi 2,0
10317 . ld 2,xxx+8(2) bnectr+
10318 . bctr b <glink_entry>
10319
10320 The solution involving the compare turns out to be faster, so
10321 that's what we use unless the branch won't reach. */
10322
10323#define ALWAYS_USE_FAKE_DEP 0
10324#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10325
5d1634d7
AM
10326#define PPC_LO(v) ((v) & 0xffff)
10327#define PPC_HI(v) (((v) >> 16) & 0xffff)
10328#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10329
794e51c0
AM
10330static inline unsigned int
10331plt_stub_size (struct ppc_link_hash_table *htab,
10332 struct ppc_stub_hash_entry *stub_entry,
10333 bfd_vma off)
10334{
b9e5796b
AM
10335 unsigned size = 12;
10336
10337 if (ALWAYS_EMIT_R2SAVE
10338 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10339 size += 4;
10340 if (PPC_HA (off) != 0)
794e51c0 10341 size += 4;
b9e5796b
AM
10342 if (htab->opd_abi)
10343 {
10344 size += 4;
e7d1c40c 10345 if (htab->params->plt_static_chain)
b9e5796b 10346 size += 4;
bd4d2eaa
AM
10347 if (htab->params->plt_thread_safe
10348 && htab->elf.dynamic_sections_created
10349 && stub_entry->h != NULL
10350 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10351 size += 8;
e7d1c40c 10352 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10353 size += 4;
10354 }
794e51c0
AM
10355 if (stub_entry->h != NULL
10356 && (stub_entry->h == htab->tls_get_addr_fd
10357 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10358 && htab->params->tls_get_addr_opt)
794e51c0
AM
10359 size += 13 * 4;
10360 return size;
10361}
10362
10363/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10364 then return the padding needed to do so. */
10365static inline unsigned int
10366plt_stub_pad (struct ppc_link_hash_table *htab,
10367 struct ppc_stub_hash_entry *stub_entry,
10368 bfd_vma plt_off)
10369{
e7d1c40c 10370 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0 10371 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10372 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0
AM
10373
10374 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10375 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10376 return stub_align - (stub_off & (stub_align - 1));
10377 return 0;
10378}
10379
10380/* Build a .plt call stub. */
10381
10382static inline bfd_byte *
10383build_plt_stub (struct ppc_link_hash_table *htab,
10384 struct ppc_stub_hash_entry *stub_entry,
10385 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10386{
e7d1c40c 10387 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10388 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10389 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10390 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10391 && htab->elf.dynamic_sections_created
10392 && stub_entry->h != NULL
10393 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10394 bfd_boolean use_fake_dep = plt_thread_safe;
10395 bfd_vma cmp_branch_off = 0;
10396
10397 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10398 && plt_load_toc
794e51c0 10399 && plt_thread_safe
bd4d2eaa
AM
10400 && !((stub_entry->h == htab->tls_get_addr_fd
10401 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10402 && htab->params->tls_get_addr_opt))
794e51c0
AM
10403 {
10404 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10405 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10406 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10407 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10408 bfd_vma to, from;
10409
68d62958
AM
10410 if (pltindex > 32768)
10411 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10412 to = (glinkoff
10413 + htab->glink->output_offset
10414 + htab->glink->output_section->vma);
6f20ed8a 10415 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10416 + 4 * (ALWAYS_EMIT_R2SAVE
10417 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10418 + 4 * (PPC_HA (offset) != 0)
10419 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10420 != PPC_HA (offset))
10421 + 4 * (plt_static_chain != 0)
10422 + 20
6f20ed8a
AM
10423 + stub_entry->group->stub_sec->output_offset
10424 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10425 cmp_branch_off = to - from;
10426 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10427 }
10428
ac2df442
AM
10429 if (PPC_HA (offset) != 0)
10430 {
176a0d42
AM
10431 if (r != NULL)
10432 {
794e51c0
AM
10433 if (ALWAYS_EMIT_R2SAVE
10434 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10435 r[0].r_offset += 4;
176a0d42 10436 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10437 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10438 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10439 r[1].r_addend = r[0].r_addend;
b9e5796b 10440 if (plt_load_toc)
176a0d42 10441 {
b9e5796b 10442 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10443 {
b9e5796b
AM
10444 r[2].r_offset = r[1].r_offset + 4;
10445 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10446 r[2].r_addend = r[0].r_addend;
10447 }
10448 else
10449 {
10450 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10451 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10452 r[2].r_addend = r[0].r_addend + 8;
10453 if (plt_static_chain)
10454 {
10455 r[3].r_offset = r[2].r_offset + 4;
10456 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10457 r[3].r_addend = r[0].r_addend + 16;
10458 }
c7131b65 10459 }
176a0d42
AM
10460 }
10461 }
794e51c0
AM
10462 if (ALWAYS_EMIT_R2SAVE
10463 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10464 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10465 if (plt_load_toc)
10466 {
10467 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10468 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10469 }
10470 else
10471 {
10472 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10473 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10474 }
b9e5796b
AM
10475 if (plt_load_toc
10476 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10477 {
71a39c98 10478 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10479 offset = 0;
10480 }
71a39c98 10481 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10482 if (plt_load_toc)
794e51c0 10483 {
b9e5796b
AM
10484 if (use_fake_dep)
10485 {
10486 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10487 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10488 }
10489 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10490 if (plt_static_chain)
10491 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10492 }
ac2df442
AM
10493 }
10494 else
10495 {
176a0d42
AM
10496 if (r != NULL)
10497 {
794e51c0
AM
10498 if (ALWAYS_EMIT_R2SAVE
10499 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10500 r[0].r_offset += 4;
176a0d42 10501 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10502 if (plt_load_toc)
176a0d42 10503 {
b9e5796b 10504 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10505 {
b9e5796b
AM
10506 r[1].r_offset = r[0].r_offset + 4;
10507 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10508 r[1].r_addend = r[0].r_addend;
10509 }
10510 else
10511 {
10512 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10513 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10514 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10515 if (plt_static_chain)
10516 {
10517 r[2].r_offset = r[1].r_offset + 4;
10518 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10519 r[2].r_addend = r[0].r_addend + 8;
10520 }
c7131b65 10521 }
176a0d42
AM
10522 }
10523 }
794e51c0
AM
10524 if (ALWAYS_EMIT_R2SAVE
10525 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10526 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10527 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10528 if (plt_load_toc
10529 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10530 {
10531 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10532 offset = 0;
10533 }
71a39c98 10534 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10535 if (plt_load_toc)
794e51c0 10536 {
b9e5796b
AM
10537 if (use_fake_dep)
10538 {
10539 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10540 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10541 }
10542 if (plt_static_chain)
10543 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10544 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10545 }
ac2df442 10546 }
b9e5796b 10547 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10548 {
10549 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10550 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10551 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10552 }
10553 else
10554 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10555 return p;
10556}
10557
a7f2871e
AM
10558/* Build a special .plt call stub for __tls_get_addr. */
10559
10560#define LD_R11_0R3 0xe9630000
10561#define LD_R12_0R3 0xe9830000
10562#define MR_R0_R3 0x7c601b78
10563#define CMPDI_R11_0 0x2c2b0000
10564#define ADD_R3_R12_R13 0x7c6c6a14
10565#define BEQLR 0x4d820020
10566#define MR_R3_R0 0x7c030378
a7f2871e
AM
10567#define STD_R11_0R1 0xf9610000
10568#define BCTRL 0x4e800421
10569#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10570#define MTLR_R11 0x7d6803a6
10571
10572static inline bfd_byte *
794e51c0
AM
10573build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10574 struct ppc_stub_hash_entry *stub_entry,
10575 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10576{
e7d1c40c 10577 bfd *obfd = htab->params->stub_bfd;
794e51c0 10578
a7f2871e
AM
10579 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10580 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10581 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10582 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10583 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10584 bfd_put_32 (obfd, BEQLR, p), p += 4;
10585 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10586 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10587 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10588
10589 if (r != NULL)
10590 r[0].r_offset += 9 * 4;
794e51c0 10591 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10592 bfd_put_32 (obfd, BCTRL, p - 4);
10593
a078d95a 10594 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10595 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10596 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10597 bfd_put_32 (obfd, BLR, p), p += 4;
10598
10599 return p;
10600}
10601
176a0d42
AM
10602static Elf_Internal_Rela *
10603get_relocs (asection *sec, int count)
10604{
10605 Elf_Internal_Rela *relocs;
10606 struct bfd_elf_section_data *elfsec_data;
10607
10608 elfsec_data = elf_section_data (sec);
10609 relocs = elfsec_data->relocs;
10610 if (relocs == NULL)
10611 {
10612 bfd_size_type relsize;
10613 relsize = sec->reloc_count * sizeof (*relocs);
10614 relocs = bfd_alloc (sec->owner, relsize);
10615 if (relocs == NULL)
10616 return NULL;
10617 elfsec_data->relocs = relocs;
d4730f92
BS
10618 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10619 sizeof (Elf_Internal_Shdr));
10620 if (elfsec_data->rela.hdr == NULL)
10621 return NULL;
10622 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10623 * sizeof (Elf64_External_Rela));
10624 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10625 sec->reloc_count = 0;
10626 }
10627 relocs += sec->reloc_count;
10628 sec->reloc_count += count;
10629 return relocs;
10630}
10631
aa374f67 10632static bfd_vma
25f53a85 10633get_r2off (struct bfd_link_info *info,
aa374f67
AM
10634 struct ppc_stub_hash_entry *stub_entry)
10635{
25f53a85 10636 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10637 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10638
10639 if (r2off == 0)
10640 {
10641 /* Support linking -R objects. Get the toc pointer from the
10642 opd entry. */
10643 char buf[8];
b9e5796b
AM
10644 if (!htab->opd_abi)
10645 return r2off;
aa374f67
AM
10646 asection *opd = stub_entry->h->elf.root.u.def.section;
10647 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10648
10649 if (strcmp (opd->name, ".opd") != 0
10650 || opd->reloc_count != 0)
10651 {
bc30df16 10652 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10653 stub_entry->h->elf.root.root.string);
aa374f67 10654 bfd_set_error (bfd_error_bad_value);
a7c49797 10655 return (bfd_vma) -1;
aa374f67
AM
10656 }
10657 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10658 return (bfd_vma) -1;
aa374f67 10659 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10660 r2off -= elf_gp (info->output_bfd);
aa374f67 10661 }
6f20ed8a 10662 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10663 return r2off;
10664}
10665
b34976b6 10666static bfd_boolean
4ce794b7 10667ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10668{
721956f4
AM
10669 struct ppc_stub_hash_entry *stub_entry;
10670 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10671 struct bfd_link_info *info;
10672 struct ppc_link_hash_table *htab;
721956f4
AM
10673 bfd_byte *loc;
10674 bfd_byte *p;
ee75fd95 10675 bfd_vma dest, off;
721956f4 10676 int size;
176a0d42 10677 Elf_Internal_Rela *r;
e054468f 10678 asection *plt;
5d1634d7 10679
721956f4
AM
10680 /* Massage our args to the form they really have. */
10681 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10682 info = in_arg;
5d1634d7 10683
5d1634d7 10684 htab = ppc_hash_table (info);
4dfe6ac6
NC
10685 if (htab == NULL)
10686 return FALSE;
5d1634d7 10687
721956f4 10688 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10689 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10690 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10691
4ce794b7 10692 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10693 switch (stub_entry->stub_type)
5d1634d7 10694 {
721956f4 10695 case ppc_stub_long_branch:
ad8e1ba5 10696 case ppc_stub_long_branch_r2off:
721956f4 10697 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10698 dest = (stub_entry->target_value
10699 + stub_entry->target_section->output_offset
10700 + stub_entry->target_section->output_section->vma);
10701 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10702 off = dest;
5d1634d7 10703
721956f4
AM
10704 /* And this is where we are coming from. */
10705 off -= (stub_entry->stub_offset
6f20ed8a
AM
10706 + stub_entry->group->stub_sec->output_offset
10707 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10708
ac2df442
AM
10709 size = 4;
10710 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10711 {
25f53a85 10712 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10713
a7c49797 10714 if (r2off == (bfd_vma) -1)
aa374f67
AM
10715 {
10716 htab->stub_error = TRUE;
10717 return FALSE;
10718 }
e7d1c40c 10719 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10720 loc += 4;
a7c49797 10721 size = 8;
ac2df442
AM
10722 if (PPC_HA (r2off) != 0)
10723 {
e7d1c40c
AM
10724 bfd_put_32 (htab->params->stub_bfd,
10725 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442 10726 loc += 4;
a7c49797
AM
10727 size += 4;
10728 }
10729 if (PPC_LO (r2off) != 0)
10730 {
10731 bfd_put_32 (htab->params->stub_bfd,
10732 ADDI_R2_R2 | PPC_LO (r2off), loc);
10733 loc += 4;
10734 size += 4;
ac2df442 10735 }
ac2df442 10736 off -= size - 4;
ad8e1ba5 10737 }
e7d1c40c 10738 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10739
5c3dead3
AM
10740 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10741 {
bc30df16
AM
10742 info->callbacks->einfo
10743 (_("%P: long branch stub `%s' offset overflow\n"),
10744 stub_entry->root.string);
5c3dead3
AM
10745 htab->stub_error = TRUE;
10746 return FALSE;
10747 }
ee75fd95
AM
10748
10749 if (info->emitrelocations)
10750 {
6f20ed8a 10751 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10752 if (r == NULL)
10753 return FALSE;
6f20ed8a 10754 r->r_offset = loc - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10755 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10756 r->r_addend = dest;
10757 if (stub_entry->h != NULL)
10758 {
10759 struct elf_link_hash_entry **hashes;
10760 unsigned long symndx;
10761 struct ppc_link_hash_entry *h;
10762
e7d1c40c 10763 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10764 if (hashes == NULL)
10765 {
10766 bfd_size_type hsize;
10767
10768 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10769 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10770 if (hashes == NULL)
10771 return FALSE;
e7d1c40c 10772 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10773 htab->stub_globals = 1;
10774 }
10775 symndx = htab->stub_globals++;
10776 h = stub_entry->h;
10777 hashes[symndx] = &h->elf;
10778 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10779 if (h->oh != NULL && h->oh->is_func)
b31867b6 10780 h = ppc_follow_link (h->oh);
ee75fd95
AM
10781 if (h->elf.root.u.def.section != stub_entry->target_section)
10782 /* H is an opd symbol. The addend must be zero. */
10783 r->r_addend = 0;
10784 else
10785 {
10786 off = (h->elf.root.u.def.value
10787 + h->elf.root.u.def.section->output_offset
10788 + h->elf.root.u.def.section->output_section->vma);
10789 r->r_addend -= off;
10790 }
10791 }
10792 }
721956f4 10793 break;
e86ce104 10794
721956f4 10795 case ppc_stub_plt_branch:
ad8e1ba5 10796 case ppc_stub_plt_branch_r2off:
721956f4
AM
10797 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10798 stub_entry->root.string + 9,
b34976b6 10799 FALSE, FALSE);
721956f4
AM
10800 if (br_entry == NULL)
10801 {
8de848d8 10802 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10803 stub_entry->root.string);
b34976b6
AM
10804 htab->stub_error = TRUE;
10805 return FALSE;
721956f4
AM
10806 }
10807
176a0d42
AM
10808 dest = (stub_entry->target_value
10809 + stub_entry->target_section->output_offset
10810 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10811 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10812 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10813
176a0d42 10814 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10815 htab->brlt->contents + br_entry->offset);
721956f4 10816
f94498ff 10817 if (br_entry->iter == htab->stub_iteration)
721956f4 10818 {
f94498ff 10819 br_entry->iter = 0;
84f5d08e 10820
f94498ff 10821 if (htab->relbrlt != NULL)
84f5d08e 10822 {
f94498ff
AM
10823 /* Create a reloc for the branch lookup table entry. */
10824 Elf_Internal_Rela rela;
10825 bfd_byte *rl;
10826
10827 rela.r_offset = (br_entry->offset
10828 + htab->brlt->output_offset
10829 + htab->brlt->output_section->vma);
10830 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10831 rela.r_addend = dest;
f94498ff
AM
10832
10833 rl = htab->relbrlt->contents;
10834 rl += (htab->relbrlt->reloc_count++
10835 * sizeof (Elf64_External_Rela));
10836 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10837 }
10838 else if (info->emitrelocations)
10839 {
176a0d42
AM
10840 r = get_relocs (htab->brlt, 1);
10841 if (r == NULL)
10842 return FALSE;
10843 /* brlt, being SEC_LINKER_CREATED does not go through the
10844 normal reloc processing. Symbols and offsets are not
10845 translated from input file to output file form, so
10846 set up the offset per the output file. */
f94498ff
AM
10847 r->r_offset = (br_entry->offset
10848 + htab->brlt->output_offset
10849 + htab->brlt->output_section->vma);
10850 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10851 r->r_addend = dest;
84f5d08e 10852 }
84f5d08e 10853 }
721956f4 10854
176a0d42
AM
10855 dest = (br_entry->offset
10856 + htab->brlt->output_offset
10857 + htab->brlt->output_section->vma);
10858
10859 off = (dest
4ce794b7 10860 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 10861 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 10862
ad8e1ba5 10863 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10864 {
25f53a85 10865 info->callbacks->einfo
bc30df16 10866 (_("%P: linkage table error against `%T'\n"),
721956f4 10867 stub_entry->root.string);
5d1634d7 10868 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10869 htab->stub_error = TRUE;
10870 return FALSE;
5d1634d7 10871 }
41bd81ab 10872
176a0d42
AM
10873 if (info->emitrelocations)
10874 {
6f20ed8a 10875 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
10876 if (r == NULL)
10877 return FALSE;
6f20ed8a 10878 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10879 if (bfd_big_endian (info->output_bfd))
10880 r[0].r_offset += 2;
00f412ee 10881 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
10882 r[0].r_offset += 4;
10883 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10884 r[0].r_addend = dest;
10885 if (PPC_HA (off) != 0)
10886 {
10887 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10888 r[1].r_offset = r[0].r_offset + 4;
10889 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10890 r[1].r_addend = r[0].r_addend;
10891 }
10892 }
10893
00f412ee 10894 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 10895 {
176a0d42 10896 if (PPC_HA (off) != 0)
ac2df442
AM
10897 {
10898 size = 16;
e7d1c40c 10899 bfd_put_32 (htab->params->stub_bfd,
397998fc 10900 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10901 loc += 4;
e7d1c40c 10902 bfd_put_32 (htab->params->stub_bfd,
397998fc 10903 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
10904 }
10905 else
10906 {
10907 size = 12;
e7d1c40c
AM
10908 bfd_put_32 (htab->params->stub_bfd,
10909 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 10910 }
ad8e1ba5
AM
10911 }
10912 else
10913 {
25f53a85 10914 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 10915
a7c49797 10916 if (r2off == (bfd_vma) -1)
aa374f67
AM
10917 {
10918 htab->stub_error = TRUE;
10919 return FALSE;
10920 }
ad8e1ba5 10921
e7d1c40c 10922 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10923 loc += 4;
00f412ee 10924 size = 16;
176a0d42 10925 if (PPC_HA (off) != 0)
ac2df442
AM
10926 {
10927 size += 4;
e7d1c40c 10928 bfd_put_32 (htab->params->stub_bfd,
397998fc 10929 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10930 loc += 4;
e7d1c40c 10931 bfd_put_32 (htab->params->stub_bfd,
397998fc 10932 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
10933 }
10934 else
e7d1c40c 10935 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
10936
10937 if (PPC_HA (r2off) != 0)
10938 {
10939 size += 4;
00f412ee 10940 loc += 4;
e7d1c40c
AM
10941 bfd_put_32 (htab->params->stub_bfd,
10942 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
10943 }
10944 if (PPC_LO (r2off) != 0)
10945 {
10946 size += 4;
ac2df442 10947 loc += 4;
e7d1c40c
AM
10948 bfd_put_32 (htab->params->stub_bfd,
10949 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 10950 }
ad8e1ba5
AM
10951 }
10952 loc += 4;
e7d1c40c 10953 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 10954 loc += 4;
e7d1c40c 10955 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
721956f4 10956 break;
5d1634d7 10957
721956f4 10958 case ppc_stub_plt_call:
794e51c0 10959 case ppc_stub_plt_call_r2save:
e054468f 10960 if (stub_entry->h != NULL
b31867b6
AM
10961 && stub_entry->h->is_func_descriptor
10962 && stub_entry->h->oh != NULL)
c862ae31 10963 {
b31867b6
AM
10964 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10965
10966 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 10967 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
b31867b6
AM
10968 if (fh->elf.root.type == bfd_link_hash_undefined)
10969 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
10970 /* Stop undo_symbol_twiddle changing it back to undefined. */
10971 fh->was_undefined = 0;
c862ae31
AM
10972 }
10973
721956f4 10974 /* Now build the stub. */
e054468f 10975 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 10976 if (dest >= (bfd_vma) -2)
721956f4
AM
10977 abort ();
10978
33e44f2e 10979 plt = htab->elf.splt;
25f23106
AM
10980 if (!htab->elf.dynamic_sections_created
10981 || stub_entry->h == NULL
10982 || stub_entry->h->elf.dynindx == -1)
33e44f2e 10983 plt = htab->elf.iplt;
e054468f
AM
10984
10985 dest += plt->output_offset + plt->output_section->vma;
10986
10987 if (stub_entry->h == NULL
10988 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10989 {
10990 Elf_Internal_Rela rela;
10991 bfd_byte *rl;
10992
10993 rela.r_offset = dest;
ee67d69a
AM
10994 if (htab->opd_abi)
10995 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10996 else
10997 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
10998 rela.r_addend = (stub_entry->target_value
10999 + stub_entry->target_section->output_offset
11000 + stub_entry->target_section->output_section->vma);
11001
33e44f2e
AM
11002 rl = (htab->elf.irelplt->contents
11003 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11004 * sizeof (Elf64_External_Rela)));
11005 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
11006 stub_entry->plt_ent->plt.offset |= 1;
11007 }
176a0d42
AM
11008
11009 off = (dest
e054468f 11010 - elf_gp (plt->output_section->owner)
6f20ed8a 11011 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11012
ad8e1ba5 11013 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11014 {
25f53a85 11015 info->callbacks->einfo
bc30df16 11016 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
11017 stub_entry->h != NULL
11018 ? stub_entry->h->elf.root.root.string
11019 : "<local sym>");
721956f4 11020 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11021 htab->stub_error = TRUE;
11022 return FALSE;
721956f4
AM
11023 }
11024
e7d1c40c 11025 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11026 {
11027 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11028
6f20ed8a
AM
11029 stub_entry->group->stub_sec->size += pad;
11030 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11031 loc += pad;
11032 }
11033
176a0d42
AM
11034 r = NULL;
11035 if (info->emitrelocations)
11036 {
6f20ed8a 11037 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11038 ((PPC_HA (off) != 0)
11039 + (htab->opd_abi
e7d1c40c 11040 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11041 && PPC_HA (off + 16) == PPC_HA (off))
11042 : 1)));
176a0d42
AM
11043 if (r == NULL)
11044 return FALSE;
6f20ed8a 11045 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11046 if (bfd_big_endian (info->output_bfd))
11047 r[0].r_offset += 2;
176a0d42
AM
11048 r[0].r_addend = dest;
11049 }
a7f2871e
AM
11050 if (stub_entry->h != NULL
11051 && (stub_entry->h == htab->tls_get_addr_fd
11052 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11053 && htab->params->tls_get_addr_opt)
794e51c0 11054 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11055 else
794e51c0 11056 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11057 size = p - loc;
11058 break;
11059
a4b6fadd
AM
11060 case ppc_stub_save_res:
11061 return TRUE;
11062
721956f4
AM
11063 default:
11064 BFD_FAIL ();
b34976b6 11065 return FALSE;
721956f4
AM
11066 }
11067
6f20ed8a 11068 stub_entry->group->stub_sec->size += size;
97b639ba 11069
e7d1c40c 11070 if (htab->params->emit_stub_syms)
97b639ba
AM
11071 {
11072 struct elf_link_hash_entry *h;
ee75fd95
AM
11073 size_t len1, len2;
11074 char *name;
11075 const char *const stub_str[] = { "long_branch",
11076 "long_branch_r2off",
11077 "plt_branch",
11078 "plt_branch_r2off",
794e51c0 11079 "plt_call",
ee75fd95
AM
11080 "plt_call" };
11081
11082 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11083 len2 = strlen (stub_entry->root.string);
11084 name = bfd_malloc (len1 + len2 + 2);
11085 if (name == NULL)
11086 return FALSE;
11087 memcpy (name, stub_entry->root.string, 9);
11088 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11089 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11090 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11091 if (h == NULL)
11092 return FALSE;
11093 if (h->root.type == bfd_link_hash_new)
11094 {
11095 h->root.type = bfd_link_hash_defined;
6f20ed8a 11096 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11097 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11098 h->ref_regular = 1;
11099 h->def_regular = 1;
11100 h->ref_regular_nonweak = 1;
11101 h->forced_local = 1;
11102 h->non_elf = 0;
2ec55de3 11103 h->root.linker_def = 1;
97b639ba
AM
11104 }
11105 }
11106
b34976b6 11107 return TRUE;
721956f4
AM
11108}
11109
11110/* As above, but don't actually build the stub. Just bump offset so
11111 we know stub section sizes, and select plt_branch stubs where
11112 long_branch stubs won't do. */
11113
b34976b6 11114static bfd_boolean
4ce794b7 11115ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11116{
11117 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11118 struct bfd_link_info *info;
721956f4
AM
11119 struct ppc_link_hash_table *htab;
11120 bfd_vma off;
11121 int size;
11122
11123 /* Massage our args to the form they really have. */
11124 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11125 info = in_arg;
11126
11127 htab = ppc_hash_table (info);
4dfe6ac6
NC
11128 if (htab == NULL)
11129 return FALSE;
721956f4 11130
a4b6fadd
AM
11131 if (stub_entry->h != NULL
11132 && stub_entry->h->save_res
11133 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11134 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11135 {
11136 /* Don't make stubs to out-of-line register save/restore
11137 functions. Instead, emit copies of the functions. */
11138 stub_entry->group->needs_save_res = 1;
11139 stub_entry->stub_type = ppc_stub_save_res;
11140 return TRUE;
11141 }
11142
794e51c0
AM
11143 if (stub_entry->stub_type == ppc_stub_plt_call
11144 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11145 {
e054468f
AM
11146 asection *plt;
11147 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11148 if (off >= (bfd_vma) -2)
411e1bfb 11149 abort ();
33e44f2e 11150 plt = htab->elf.splt;
25f23106
AM
11151 if (!htab->elf.dynamic_sections_created
11152 || stub_entry->h == NULL
11153 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11154 plt = htab->elf.iplt;
e054468f
AM
11155 off += (plt->output_offset
11156 + plt->output_section->vma
11157 - elf_gp (plt->output_section->owner)
6f20ed8a 11158 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11159
794e51c0 11160 size = plt_stub_size (htab, stub_entry, off);
e7d1c40c 11161 if (htab->params->plt_stub_align)
794e51c0 11162 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11163 if (info->emitrelocations)
11164 {
6f20ed8a 11165 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11166 += ((PPC_HA (off) != 0)
11167 + (htab->opd_abi
e7d1c40c 11168 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11169 && PPC_HA (off + 16) == PPC_HA (off))
11170 : 1));
6f20ed8a 11171 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11172 }
721956f4
AM
11173 }
11174 else
11175 {
ad8e1ba5
AM
11176 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11177 variants. */
ac2df442 11178 bfd_vma r2off = 0;
6911b7dc 11179 bfd_vma local_off = 0;
ac2df442 11180
721956f4
AM
11181 off = (stub_entry->target_value
11182 + stub_entry->target_section->output_offset
11183 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11184 off -= (stub_entry->group->stub_sec->size
11185 + stub_entry->group->stub_sec->output_offset
11186 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11187
ad8e1ba5
AM
11188 /* Reset the stub type from the plt variant in case we now
11189 can reach with a shorter stub. */
11190 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11191 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11192
11193 size = 4;
11194 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11195 {
25f53a85 11196 r2off = get_r2off (info, stub_entry);
a7c49797 11197 if (r2off == (bfd_vma) -1)
aa374f67
AM
11198 {
11199 htab->stub_error = TRUE;
11200 return FALSE;
11201 }
a7c49797 11202 size = 8;
ac2df442 11203 if (PPC_HA (r2off) != 0)
a7c49797
AM
11204 size += 4;
11205 if (PPC_LO (r2off) != 0)
11206 size += 4;
ac2df442 11207 off -= size - 4;
ad8e1ba5
AM
11208 }
11209
6911b7dc
AM
11210 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11211
b9e5796b
AM
11212 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11213 Do the same for -R objects without function descriptors. */
11214 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11215 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11216 && r2off == 0
11217 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11218 {
11219 struct ppc_branch_hash_entry *br_entry;
11220
11221 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11222 stub_entry->root.string + 9,
b34976b6 11223 TRUE, FALSE);
721956f4
AM
11224 if (br_entry == NULL)
11225 {
8de848d8 11226 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11227 stub_entry->root.string);
b34976b6
AM
11228 htab->stub_error = TRUE;
11229 return FALSE;
721956f4
AM
11230 }
11231
11232 if (br_entry->iter != htab->stub_iteration)
11233 {
11234 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11235 br_entry->offset = htab->brlt->size;
11236 htab->brlt->size += 8;
63bc6f6c 11237
ee75fd95 11238 if (htab->relbrlt != NULL)
eea6121a 11239 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11240 else if (info->emitrelocations)
11241 {
11242 htab->brlt->reloc_count += 1;
11243 htab->brlt->flags |= SEC_RELOC;
11244 }
721956f4 11245 }
ad8e1ba5
AM
11246
11247 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11248 off = (br_entry->offset
11249 + htab->brlt->output_offset
11250 + htab->brlt->output_section->vma
11251 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11252 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11253
176a0d42
AM
11254 if (info->emitrelocations)
11255 {
6f20ed8a
AM
11256 stub_entry->group->stub_sec->reloc_count
11257 += 1 + (PPC_HA (off) != 0);
11258 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11259 }
11260
00f412ee 11261 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11262 {
11263 size = 12;
176a0d42 11264 if (PPC_HA (off) != 0)
ac2df442
AM
11265 size = 16;
11266 }
11267 else
11268 {
00f412ee 11269 size = 16;
176a0d42 11270 if (PPC_HA (off) != 0)
ac2df442
AM
11271 size += 4;
11272
11273 if (PPC_HA (r2off) != 0)
11274 size += 4;
00f412ee
AM
11275 if (PPC_LO (r2off) != 0)
11276 size += 4;
ac2df442 11277 }
721956f4 11278 }
84f5d08e
AM
11279 else if (info->emitrelocations)
11280 {
6f20ed8a
AM
11281 stub_entry->group->stub_sec->reloc_count += 1;
11282 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11283 }
721956f4
AM
11284 }
11285
6f20ed8a 11286 stub_entry->group->stub_sec->size += size;
b34976b6 11287 return TRUE;
721956f4
AM
11288}
11289
11290/* Set up various things so that we can make a list of input sections
11291 for each output section included in the link. Returns -1 on error,
cedb70c5 11292 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11293
11294int
e7d1c40c 11295ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11296{
6f20ed8a 11297 unsigned int id;
721956f4
AM
11298 bfd_size_type amt;
11299 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11300
4dfe6ac6
NC
11301 if (htab == NULL)
11302 return -1;
4c52953f 11303
6f20ed8a
AM
11304 htab->sec_info_arr_size = bfd_get_next_section_id ();
11305 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11306 htab->sec_info = bfd_zmalloc (amt);
11307 if (htab->sec_info == NULL)
721956f4
AM
11308 return -1;
11309
3d6f9012
AM
11310 /* Set toc_off for com, und, abs and ind sections. */
11311 for (id = 0; id < 3; id++)
6f20ed8a 11312 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11313
721956f4
AM
11314 return 1;
11315}
11316
927be08e
AM
11317/* Set up for first pass at multitoc partitioning. */
11318
11319void
11320ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11321{
11322 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11323
1c865ab2 11324 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11325 htab->toc_bfd = NULL;
11326 htab->toc_first_sec = NULL;
11327}
11328
e717da7e
AM
11329/* The linker repeatedly calls this function for each TOC input section
11330 and linker generated GOT section. Group input bfds such that the toc
927be08e 11331 within a group is less than 64k in size. */
ad8e1ba5 11332
927be08e 11333bfd_boolean
4ce794b7 11334ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11335{
11336 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11337 bfd_vma addr, off, limit;
ad8e1ba5 11338
4dfe6ac6
NC
11339 if (htab == NULL)
11340 return FALSE;
11341
927be08e 11342 if (!htab->second_toc_pass)
4c52953f 11343 {
927be08e 11344 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11345 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11346
11347 if (new_bfd)
bf102f86
AM
11348 {
11349 htab->toc_bfd = isec->owner;
11350 htab->toc_first_sec = isec;
11351 }
927be08e 11352
bf102f86
AM
11353 addr = isec->output_offset + isec->output_section->vma;
11354 off = addr - htab->toc_curr;
d77c8a4b
AM
11355 limit = 0x80008000;
11356 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11357 limit = 0x10000;
11358 if (off + isec->size > limit)
bf102f86
AM
11359 {
11360 addr = (htab->toc_first_sec->output_offset
11361 + htab->toc_first_sec->output_section->vma);
11362 htab->toc_curr = addr;
a27e685f 11363 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11364 }
99877b66 11365
927be08e
AM
11366 /* toc_curr is the base address of this toc group. Set elf_gp
11367 for the input section to be the offset relative to the
11368 output toc base plus 0x8000. Making the input elf_gp an
11369 offset allows us to move the toc as a whole without
11370 recalculating input elf_gp. */
11371 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11372 off += TOC_BASE_OFF;
11373
11374 /* Die if someone uses a linker script that doesn't keep input
11375 file .toc and .got together. */
a4fd3de5
AM
11376 if (new_bfd
11377 && elf_gp (isec->owner) != 0
927be08e
AM
11378 && elf_gp (isec->owner) != off)
11379 return FALSE;
11380
11381 elf_gp (isec->owner) = off;
11382 return TRUE;
4c52953f 11383 }
927be08e
AM
11384
11385 /* During the second pass toc_first_sec points to the start of
11386 a toc group, and toc_curr is used to track the old elf_gp.
11387 We use toc_bfd to ensure we only look at each bfd once. */
11388 if (htab->toc_bfd == isec->owner)
11389 return TRUE;
11390 htab->toc_bfd = isec->owner;
11391
11392 if (htab->toc_first_sec == NULL
11393 || htab->toc_curr != elf_gp (isec->owner))
11394 {
11395 htab->toc_curr = elf_gp (isec->owner);
11396 htab->toc_first_sec = isec;
11397 }
11398 addr = (htab->toc_first_sec->output_offset
11399 + htab->toc_first_sec->output_section->vma);
11400 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11401 elf_gp (isec->owner) = off;
11402
11403 return TRUE;
ad8e1ba5
AM
11404}
11405
927be08e
AM
11406/* Called via elf_link_hash_traverse to merge GOT entries for global
11407 symbol H. */
11408
11409static bfd_boolean
11410merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11411{
11412 if (h->root.type == bfd_link_hash_indirect)
11413 return TRUE;
11414
927be08e
AM
11415 merge_got_entries (&h->got.glist);
11416
11417 return TRUE;
11418}
11419
11420/* Called via elf_link_hash_traverse to allocate GOT entries for global
11421 symbol H. */
11422
11423static bfd_boolean
11424reallocate_got (struct elf_link_hash_entry *h, void *inf)
11425{
11426 struct got_entry *gent;
11427
11428 if (h->root.type == bfd_link_hash_indirect)
11429 return TRUE;
11430
927be08e
AM
11431 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11432 if (!gent->is_indirect)
11433 allocate_got (h, (struct bfd_link_info *) inf, gent);
11434 return TRUE;
11435}
11436
11437/* Called on the first multitoc pass after the last call to
11438 ppc64_elf_next_toc_section. This function removes duplicate GOT
11439 entries. */
11440
11441bfd_boolean
11442ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11443{
11444 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11445 struct bfd *ibfd, *ibfd2;
11446 bfd_boolean done_something;
11447
11448 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11449
7865406b
AM
11450 if (!htab->do_multi_toc)
11451 return FALSE;
11452
d0fae19d 11453 /* Merge global sym got entries within a toc group. */
927be08e
AM
11454 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11455
11456 /* And tlsld_got. */
c72f2fb2 11457 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11458 {
11459 struct got_entry *ent, *ent2;
11460
11461 if (!is_ppc64_elf (ibfd))
11462 continue;
11463
11464 ent = ppc64_tlsld_got (ibfd);
11465 if (!ent->is_indirect
11466 && ent->got.offset != (bfd_vma) -1)
11467 {
c72f2fb2 11468 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11469 {
11470 if (!is_ppc64_elf (ibfd2))
11471 continue;
11472
11473 ent2 = ppc64_tlsld_got (ibfd2);
11474 if (!ent2->is_indirect
11475 && ent2->got.offset != (bfd_vma) -1
11476 && elf_gp (ibfd2) == elf_gp (ibfd))
11477 {
11478 ent2->is_indirect = TRUE;
11479 ent2->got.ent = ent;
11480 }
11481 }
11482 }
11483 }
11484
11485 /* Zap sizes of got sections. */
33e44f2e
AM
11486 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11487 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11488 htab->got_reli_size = 0;
11489
c72f2fb2 11490 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11491 {
11492 asection *got, *relgot;
11493
11494 if (!is_ppc64_elf (ibfd))
11495 continue;
11496
11497 got = ppc64_elf_tdata (ibfd)->got;
11498 if (got != NULL)
11499 {
11500 got->rawsize = got->size;
11501 got->size = 0;
11502 relgot = ppc64_elf_tdata (ibfd)->relgot;
11503 relgot->rawsize = relgot->size;
11504 relgot->size = 0;
11505 }
11506 }
11507
11508 /* Now reallocate the got, local syms first. We don't need to
11509 allocate section contents again since we never increase size. */
c72f2fb2 11510 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11511 {
11512 struct got_entry **lgot_ents;
11513 struct got_entry **end_lgot_ents;
11514 struct plt_entry **local_plt;
11515 struct plt_entry **end_local_plt;
f961d9dd 11516 unsigned char *lgot_masks;
927be08e
AM
11517 bfd_size_type locsymcount;
11518 Elf_Internal_Shdr *symtab_hdr;
19e08130 11519 asection *s;
927be08e
AM
11520
11521 if (!is_ppc64_elf (ibfd))
11522 continue;
11523
11524 lgot_ents = elf_local_got_ents (ibfd);
11525 if (!lgot_ents)
11526 continue;
11527
11528 symtab_hdr = &elf_symtab_hdr (ibfd);
11529 locsymcount = symtab_hdr->sh_info;
11530 end_lgot_ents = lgot_ents + locsymcount;
11531 local_plt = (struct plt_entry **) end_lgot_ents;
11532 end_local_plt = local_plt + locsymcount;
f961d9dd 11533 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11534 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11535 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11536 {
11537 struct got_entry *ent;
11538
11539 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11540 {
19e08130
AM
11541 unsigned int ent_size = 8;
11542 unsigned int rel_size = sizeof (Elf64_External_Rela);
11543
d0fae19d
AM
11544 ent->got.offset = s->size;
11545 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11546 {
19e08130
AM
11547 ent_size *= 2;
11548 rel_size *= 2;
11549 }
11550 s->size += ent_size;
11551 if ((*lgot_masks & PLT_IFUNC) != 0)
11552 {
33e44f2e 11553 htab->elf.irelplt->size += rel_size;
19e08130
AM
11554 htab->got_reli_size += rel_size;
11555 }
0e1862bb 11556 else if (bfd_link_pic (info))
19e08130
AM
11557 {
11558 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11559 srel->size += rel_size;
d0fae19d
AM
11560 }
11561 }
927be08e
AM
11562 }
11563 }
11564
11565 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11566
c72f2fb2 11567 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11568 {
11569 struct got_entry *ent;
11570
11571 if (!is_ppc64_elf (ibfd))
11572 continue;
11573
11574 ent = ppc64_tlsld_got (ibfd);
11575 if (!ent->is_indirect
11576 && ent->got.offset != (bfd_vma) -1)
11577 {
11578 asection *s = ppc64_elf_tdata (ibfd)->got;
11579 ent->got.offset = s->size;
11580 s->size += 16;
0e1862bb 11581 if (bfd_link_pic (info))
927be08e
AM
11582 {
11583 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11584 srel->size += sizeof (Elf64_External_Rela);
11585 }
11586 }
11587 }
11588
33e44f2e 11589 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11590 if (!done_something)
c72f2fb2 11591 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11592 {
11593 asection *got;
11594
11595 if (!is_ppc64_elf (ibfd))
11596 continue;
11597
11598 got = ppc64_elf_tdata (ibfd)->got;
11599 if (got != NULL)
11600 {
11601 done_something = got->rawsize != got->size;
11602 if (done_something)
11603 break;
11604 }
11605 }
11606
11607 if (done_something)
e7d1c40c 11608 (*htab->params->layout_sections_again) ();
927be08e
AM
11609
11610 /* Set up for second pass over toc sections to recalculate elf_gp
11611 on input sections. */
11612 htab->toc_bfd = NULL;
11613 htab->toc_first_sec = NULL;
11614 htab->second_toc_pass = TRUE;
11615 return done_something;
11616}
11617
11618/* Called after second pass of multitoc partitioning. */
11619
11620void
11621ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11622{
11623 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11624
11625 /* After the second pass, toc_curr tracks the TOC offset used
11626 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11627 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11628}
11629
9b5ecbd0
AM
11630/* No toc references were found in ISEC. If the code in ISEC makes no
11631 calls, then there's no need to use toc adjusting stubs when branching
11632 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11633 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11634 needed, and 2 if a cyclical call-graph was found but no other reason
11635 for a stub was detected. If called from the top level, a return of
11636 2 means the same as a return of 0. */
9b5ecbd0
AM
11637
11638static int
4ce794b7 11639toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11640{
9b5ecbd0 11641 int ret;
70cc837d
AM
11642
11643 /* Mark this section as checked. */
11644 isec->call_check_done = 1;
9b5ecbd0 11645
772119ce
AM
11646 /* We know none of our code bearing sections will need toc stubs. */
11647 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11648 return 0;
11649
eea6121a 11650 if (isec->size == 0)
082c50f8
AM
11651 return 0;
11652
4c52953f
AM
11653 if (isec->output_section == NULL)
11654 return 0;
11655
4c52953f 11656 ret = 0;
70cc837d 11657 if (isec->reloc_count != 0)
9b5ecbd0 11658 {
70cc837d
AM
11659 Elf_Internal_Rela *relstart, *rel;
11660 Elf_Internal_Sym *local_syms;
11661 struct ppc_link_hash_table *htab;
2917689a 11662
70cc837d
AM
11663 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11664 info->keep_memory);
11665 if (relstart == NULL)
11666 return -1;
90aecf7a 11667
70cc837d
AM
11668 /* Look for branches to outside of this section. */
11669 local_syms = NULL;
11670 htab = ppc_hash_table (info);
11671 if (htab == NULL)
11672 return -1;
4c52953f 11673
70cc837d 11674 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11675 {
70cc837d
AM
11676 enum elf_ppc64_reloc_type r_type;
11677 unsigned long r_symndx;
11678 struct elf_link_hash_entry *h;
11679 struct ppc_link_hash_entry *eh;
11680 Elf_Internal_Sym *sym;
11681 asection *sym_sec;
11682 struct _opd_sec_data *opd;
11683 bfd_vma sym_value;
11684 bfd_vma dest;
11685
11686 r_type = ELF64_R_TYPE (rel->r_info);
11687 if (r_type != R_PPC64_REL24
11688 && r_type != R_PPC64_REL14
11689 && r_type != R_PPC64_REL14_BRTAKEN
11690 && r_type != R_PPC64_REL14_BRNTAKEN)
11691 continue;
4c52953f 11692
70cc837d
AM
11693 r_symndx = ELF64_R_SYM (rel->r_info);
11694 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11695 isec->owner))
4c52953f 11696 {
70cc837d
AM
11697 ret = -1;
11698 break;
11699 }
4c52953f 11700
70cc837d
AM
11701 /* Calls to dynamic lib functions go through a plt call stub
11702 that uses r2. */
11703 eh = (struct ppc_link_hash_entry *) h;
11704 if (eh != NULL
11705 && (eh->elf.plt.plist != NULL
11706 || (eh->oh != NULL
11707 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11708 {
11709 ret = 1;
11710 break;
4c52953f
AM
11711 }
11712
70cc837d
AM
11713 if (sym_sec == NULL)
11714 /* Ignore other undefined symbols. */
4c52953f 11715 continue;
4c52953f 11716
70cc837d
AM
11717 /* Assume branches to other sections not included in the
11718 link need stubs too, to cover -R and absolute syms. */
11719 if (sym_sec->output_section == NULL)
11720 {
11721 ret = 1;
11722 break;
11723 }
4c52953f 11724
70cc837d
AM
11725 if (h == NULL)
11726 sym_value = sym->st_value;
11727 else
11728 {
11729 if (h->root.type != bfd_link_hash_defined
11730 && h->root.type != bfd_link_hash_defweak)
11731 abort ();
11732 sym_value = h->root.u.def.value;
11733 }
11734 sym_value += rel->r_addend;
4c52953f 11735
70cc837d
AM
11736 /* If this branch reloc uses an opd sym, find the code section. */
11737 opd = get_opd_info (sym_sec);
11738 if (opd != NULL)
11739 {
11740 if (h == NULL && opd->adjust != NULL)
11741 {
11742 long adjust;
4c52953f 11743
92a9c616 11744 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11745 if (adjust == -1)
11746 /* Assume deleted functions won't ever be called. */
11747 continue;
11748 sym_value += adjust;
11749 }
4c52953f 11750
aef36ac1
AM
11751 dest = opd_entry_value (sym_sec, sym_value,
11752 &sym_sec, NULL, FALSE);
70cc837d
AM
11753 if (dest == (bfd_vma) -1)
11754 continue;
11755 }
11756 else
11757 dest = (sym_value
11758 + sym_sec->output_offset
11759 + sym_sec->output_section->vma);
4c52953f 11760
70cc837d
AM
11761 /* Ignore branch to self. */
11762 if (sym_sec == isec)
11763 continue;
4c52953f 11764
70cc837d
AM
11765 /* If the called function uses the toc, we need a stub. */
11766 if (sym_sec->has_toc_reloc
11767 || sym_sec->makes_toc_func_call)
4c52953f 11768 {
70cc837d 11769 ret = 1;
4c52953f
AM
11770 break;
11771 }
70cc837d
AM
11772
11773 /* Assume any branch that needs a long branch stub might in fact
11774 need a plt_branch stub. A plt_branch stub uses r2. */
11775 else if (dest - (isec->output_offset
11776 + isec->output_section->vma
6911b7dc
AM
11777 + rel->r_offset) + (1 << 25)
11778 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11779 ? h->other
11780 : sym->st_other))
4c52953f 11781 {
70cc837d
AM
11782 ret = 1;
11783 break;
11784 }
11785
11786 /* If calling back to a section in the process of being
11787 tested, we can't say for sure that no toc adjusting stubs
11788 are needed, so don't return zero. */
11789 else if (sym_sec->call_check_in_progress)
11790 ret = 2;
11791
11792 /* Branches to another section that itself doesn't have any TOC
11793 references are OK. Recursively call ourselves to check. */
11794 else if (!sym_sec->call_check_done)
11795 {
11796 int recur;
11797
11798 /* Mark current section as indeterminate, so that other
11799 sections that call back to current won't be marked as
11800 known. */
11801 isec->call_check_in_progress = 1;
11802 recur = toc_adjusting_stub_needed (info, sym_sec);
11803 isec->call_check_in_progress = 0;
11804
4c52953f
AM
11805 if (recur != 0)
11806 {
70cc837d
AM
11807 ret = recur;
11808 if (recur != 2)
11809 break;
4c52953f
AM
11810 }
11811 }
4c52953f 11812 }
70cc837d
AM
11813
11814 if (local_syms != NULL
11815 && (elf_symtab_hdr (isec->owner).contents
11816 != (unsigned char *) local_syms))
11817 free (local_syms);
11818 if (elf_section_data (isec)->relocs != relstart)
11819 free (relstart);
9b5ecbd0
AM
11820 }
11821
70cc837d
AM
11822 if ((ret & 1) == 0
11823 && isec->map_head.s != NULL
11824 && (strcmp (isec->output_section->name, ".init") == 0
11825 || strcmp (isec->output_section->name, ".fini") == 0))
11826 {
11827 if (isec->map_head.s->has_toc_reloc
11828 || isec->map_head.s->makes_toc_func_call)
11829 ret = 1;
11830 else if (!isec->map_head.s->call_check_done)
11831 {
11832 int recur;
11833 isec->call_check_in_progress = 1;
11834 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11835 isec->call_check_in_progress = 0;
11836 if (recur != 0)
11837 ret = recur;
11838 }
11839 }
11840
11841 if (ret == 1)
11842 isec->makes_toc_func_call = 1;
4c52953f 11843
9b5ecbd0
AM
11844 return ret;
11845}
11846
721956f4
AM
11847/* The linker repeatedly calls this function for each input section,
11848 in the order that input sections are linked into output sections.
11849 Build lists of input sections to determine groupings between which
11850 we may insert linker stubs. */
11851
9b5ecbd0 11852bfd_boolean
4ce794b7 11853ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11854{
11855 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11856
4dfe6ac6
NC
11857 if (htab == NULL)
11858 return FALSE;
11859
734b6cf9 11860 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11861 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11862 {
3d6f9012
AM
11863 /* This happens to make the list in reverse order,
11864 which is what we want. */
6f20ed8a
AM
11865 htab->sec_info[isec->id].u.list
11866 = htab->sec_info[isec->output_section->id].u.list;
11867 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 11868 }
ad8e1ba5 11869
4c52953f 11870 if (htab->multi_toc_needed)
9b5ecbd0 11871 {
8b974ba3
AM
11872 /* Analyse sections that aren't already flagged as needing a
11873 valid toc pointer. Exclude .fixup for the linux kernel.
11874 .fixup contains branches, but only back to the function that
11875 hit an exception. */
11876 if (!(isec->has_toc_reloc
11877 || (isec->flags & SEC_CODE) == 0
11878 || strcmp (isec->name, ".fixup") == 0
11879 || isec->call_check_done))
11880 {
11881 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 11882 return FALSE;
8b974ba3
AM
11883 }
11884 /* Make all sections use the TOC assigned for this object file.
11885 This will be wrong for pasted sections; We fix that in
11886 check_pasted_section(). */
11887 if (elf_gp (isec->owner) != 0)
11888 htab->toc_curr = elf_gp (isec->owner);
11889 }
11890
6f20ed8a 11891 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11892 return TRUE;
721956f4
AM
11893}
11894
70cc837d
AM
11895/* Check that all .init and .fini sections use the same toc, if they
11896 have toc relocs. */
11897
11898static bfd_boolean
11899check_pasted_section (struct bfd_link_info *info, const char *name)
11900{
11901 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11902
11903 if (o != NULL)
11904 {
11905 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11906 bfd_vma toc_off = 0;
11907 asection *i;
11908
11909 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11910 if (i->has_toc_reloc)
11911 {
11912 if (toc_off == 0)
6f20ed8a
AM
11913 toc_off = htab->sec_info[i->id].toc_off;
11914 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
11915 return FALSE;
11916 }
6683a28d
AM
11917
11918 if (toc_off == 0)
11919 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11920 if (i->makes_toc_func_call)
11921 {
6f20ed8a 11922 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
11923 break;
11924 }
11925
70cc837d
AM
11926 /* Make sure the whole pasted function uses the same toc offset. */
11927 if (toc_off != 0)
11928 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 11929 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
11930 }
11931 return TRUE;
11932}
11933
11934bfd_boolean
11935ppc64_elf_check_init_fini (struct bfd_link_info *info)
11936{
11937 return (check_pasted_section (info, ".init")
11938 & check_pasted_section (info, ".fini"));
11939}
11940
721956f4
AM
11941/* See whether we can group stub sections together. Grouping stub
11942 sections may result in fewer stubs. More importantly, we need to
11943 put all .init* and .fini* stubs at the beginning of the .init or
11944 .fini output sections respectively, because glibc splits the
11945 _init and _fini functions into multiple parts. Putting a stub in
11946 the middle of a function is not a good idea. */
11947
6f20ed8a
AM
11948static bfd_boolean
11949group_sections (struct bfd_link_info *info,
4ce794b7
AM
11950 bfd_size_type stub_group_size,
11951 bfd_boolean stubs_always_before_branch)
721956f4 11952{
6f20ed8a
AM
11953 struct ppc_link_hash_table *htab;
11954 asection *osec;
7c8fe5c4
AM
11955 bfd_size_type stub14_group_size;
11956 bfd_boolean suppress_size_errors;
11957
6f20ed8a
AM
11958 htab = ppc_hash_table (info);
11959 if (htab == NULL)
11960 return FALSE;
11961
7c8fe5c4 11962 suppress_size_errors = FALSE;
0cfb0717 11963 stub14_group_size = stub_group_size >> 10;
7c8fe5c4
AM
11964 if (stub_group_size == 1)
11965 {
11966 /* Default values. */
11967 if (stubs_always_before_branch)
11968 {
11969 stub_group_size = 0x1e00000;
11970 stub14_group_size = 0x7800;
11971 }
11972 else
11973 {
11974 stub_group_size = 0x1c00000;
11975 stub14_group_size = 0x7000;
11976 }
11977 suppress_size_errors = TRUE;
11978 }
11979
6f20ed8a 11980 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 11981 {
6f20ed8a
AM
11982 asection *tail;
11983
11984 if (osec->id >= htab->sec_info_arr_size)
11985 continue;
11986
11987 tail = htab->sec_info[osec->id].u.list;
734b6cf9 11988 while (tail != NULL)
721956f4 11989 {
734b6cf9
AM
11990 asection *curr;
11991 asection *prev;
11992 bfd_size_type total;
11993 bfd_boolean big_sec;
11994 bfd_vma curr_toc;
6f20ed8a 11995 struct map_stub *group;
734b6cf9
AM
11996
11997 curr = tail;
eea6121a 11998 total = tail->size;
6bee8834
AM
11999 big_sec = total > (ppc64_elf_section_data (tail) != NULL
12000 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
12001 ? stub14_group_size : stub_group_size);
12002 if (big_sec && !suppress_size_errors)
5c3dead3
AM
12003 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
12004 tail->owner, tail);
6f20ed8a 12005 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12006
6f20ed8a 12007 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12008 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12009 < (ppc64_elf_section_data (prev) != NULL
12010 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 12011 ? stub14_group_size : stub_group_size))
6f20ed8a 12012 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12013 curr = prev;
12014
12015 /* OK, the size from the start of CURR to the end is less
12016 than stub_group_size and thus can be handled by one stub
12017 section. (or the tail section is itself larger than
12018 stub_group_size, in which case we may be toast.) We
12019 should really be keeping track of the total size of stubs
12020 added here, as stubs contribute to the final output
12021 section size. That's a little tricky, and this way will
12022 only break if stubs added make the total size more than
12023 2^25, ie. for the default stub_group_size, if stubs total
12024 more than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12025 group = bfd_alloc (curr->owner, sizeof (*group));
12026 if (group == NULL)
12027 return FALSE;
12028 group->link_sec = curr;
12029 group->stub_sec = NULL;
a4b6fadd
AM
12030 group->needs_save_res = 0;
12031 group->next = htab->group;
12032 htab->group = group;
734b6cf9 12033 do
721956f4 12034 {
6f20ed8a 12035 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12036 /* Set up this stub group. */
6f20ed8a 12037 htab->sec_info[tail->id].u.group = group;
721956f4 12038 }
734b6cf9
AM
12039 while (tail != curr && (tail = prev) != NULL);
12040
12041 /* But wait, there's more! Input sections up to stub_group_size
12042 bytes before the stub section can be handled by it too.
12043 Don't do this if we have a really large section after the
12044 stubs, as adding more stubs increases the chance that
12045 branches may not reach into the stub section. */
12046 if (!stubs_always_before_branch && !big_sec)
12047 {
12048 total = 0;
12049 while (prev != NULL
12050 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12051 < (ppc64_elf_section_data (prev) != NULL
12052 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 12053 ? stub14_group_size : stub_group_size))
6f20ed8a 12054 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12055 {
12056 tail = prev;
6f20ed8a
AM
12057 prev = htab->sec_info[tail->id].u.list;
12058 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12059 }
12060 }
12061 tail = prev;
721956f4
AM
12062 }
12063 }
6f20ed8a 12064 return TRUE;
721956f4
AM
12065}
12066
58d180e8
AM
12067static const unsigned char glink_eh_frame_cie[] =
12068{
12069 0, 0, 0, 16, /* length. */
12070 0, 0, 0, 0, /* id. */
12071 1, /* CIE version. */
12072 'z', 'R', 0, /* Augmentation string. */
12073 4, /* Code alignment. */
12074 0x78, /* Data alignment. */
12075 65, /* RA reg. */
12076 1, /* Augmentation size. */
12077 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
da44f4e5
AM
12078 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12079 0, 0, 0, 0
58d180e8
AM
12080};
12081
d969d15f
AM
12082/* Stripping output sections is normally done before dynamic section
12083 symbols have been allocated. This function is called later, and
12084 handles cases like htab->brlt which is mapped to its own output
12085 section. */
12086
12087static void
12088maybe_strip_output (struct bfd_link_info *info, asection *isec)
12089{
12090 if (isec->size == 0
12091 && isec->output_section->size == 0
53d8967a 12092 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12093 && !bfd_section_removed_from_list (info->output_bfd,
12094 isec->output_section)
12095 && elf_section_data (isec->output_section)->dynindx == 0)
12096 {
12097 isec->output_section->flags |= SEC_EXCLUDE;
12098 bfd_section_list_remove (info->output_bfd, isec->output_section);
12099 info->output_bfd->section_count--;
12100 }
12101}
12102
721956f4
AM
12103/* Determine and set the size of the stub section for a final link.
12104
12105 The basic idea here is to examine all the relocations looking for
12106 PC-relative calls to a target that is unreachable with a "bl"
12107 instruction. */
12108
b34976b6 12109bfd_boolean
e7d1c40c 12110ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12111{
12112 bfd_size_type stub_group_size;
b34976b6 12113 bfd_boolean stubs_always_before_branch;
721956f4
AM
12114 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12115
4dfe6ac6
NC
12116 if (htab == NULL)
12117 return FALSE;
12118
0e1862bb 12119 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12120 htab->params->plt_thread_safe = 1;
b9e5796b 12121 if (!htab->opd_abi)
e7d1c40c
AM
12122 htab->params->plt_thread_safe = 0;
12123 else if (htab->params->plt_thread_safe == -1)
794e51c0 12124 {
e2458743 12125 static const char *const thread_starter[] =
794e51c0
AM
12126 {
12127 "pthread_create",
12128 /* libstdc++ */
12129 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12130 /* librt */
12131 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12132 "mq_notify", "create_timer",
12133 /* libanl */
12134 "getaddrinfo_a",
12135 /* libgomp */
2300b5a1 12136 "GOMP_parallel",
794e51c0 12137 "GOMP_parallel_start",
2300b5a1 12138 "GOMP_parallel_loop_static",
794e51c0 12139 "GOMP_parallel_loop_static_start",
2300b5a1 12140 "GOMP_parallel_loop_dynamic",
794e51c0 12141 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12142 "GOMP_parallel_loop_guided",
794e51c0 12143 "GOMP_parallel_loop_guided_start",
2300b5a1 12144 "GOMP_parallel_loop_runtime",
794e51c0 12145 "GOMP_parallel_loop_runtime_start",
2300b5a1 12146 "GOMP_parallel_sections",
68ffbac6 12147 "GOMP_parallel_sections_start",
f9dffbf0
AM
12148 /* libgo */
12149 "__go_go",
794e51c0
AM
12150 };
12151 unsigned i;
12152
a4b6fadd 12153 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12154 {
12155 struct elf_link_hash_entry *h;
12156 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12157 FALSE, FALSE, TRUE);
e7d1c40c
AM
12158 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12159 if (htab->params->plt_thread_safe)
794e51c0
AM
12160 break;
12161 }
12162 }
e7d1c40c
AM
12163 stubs_always_before_branch = htab->params->group_size < 0;
12164 if (htab->params->group_size < 0)
12165 stub_group_size = -htab->params->group_size;
721956f4 12166 else
e7d1c40c 12167 stub_group_size = htab->params->group_size;
721956f4 12168
6f20ed8a
AM
12169 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12170 return FALSE;
721956f4 12171
721956f4
AM
12172 while (1)
12173 {
12174 bfd *input_bfd;
12175 unsigned int bfd_indx;
a4b6fadd 12176 struct map_stub *group;
721956f4 12177 asection *stub_sec;
721956f4
AM
12178
12179 htab->stub_iteration += 1;
721956f4
AM
12180
12181 for (input_bfd = info->input_bfds, bfd_indx = 0;
12182 input_bfd != NULL;
c72f2fb2 12183 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12184 {
12185 Elf_Internal_Shdr *symtab_hdr;
12186 asection *section;
6cdc0ccc 12187 Elf_Internal_Sym *local_syms = NULL;
721956f4 12188
0c8d6e5c 12189 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12190 continue;
12191
721956f4 12192 /* We'll need the symbol table in a second. */
0ffa91dd 12193 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12194 if (symtab_hdr->sh_info == 0)
12195 continue;
12196
721956f4
AM
12197 /* Walk over each section attached to the input bfd. */
12198 for (section = input_bfd->sections;
12199 section != NULL;
12200 section = section->next)
12201 {
721956f4 12202 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12203
12204 /* If there aren't any relocs, then there's nothing more
12205 to do. */
12206 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12207 || (section->flags & SEC_ALLOC) == 0
12208 || (section->flags & SEC_LOAD) == 0
12209 || (section->flags & SEC_CODE) == 0
721956f4
AM
12210 || section->reloc_count == 0)
12211 continue;
12212
12213 /* If this section is a link-once section that will be
12214 discarded, then don't create any stubs. */
12215 if (section->output_section == NULL
927be08e 12216 || section->output_section->owner != info->output_bfd)
721956f4
AM
12217 continue;
12218
1e2f5b6e
AM
12219 /* Get the relocs. */
12220 internal_relocs
4ce794b7 12221 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12222 info->keep_memory);
721956f4 12223 if (internal_relocs == NULL)
1e2f5b6e 12224 goto error_ret_free_local;
721956f4
AM
12225
12226 /* Now examine each relocation. */
12227 irela = internal_relocs;
12228 irelaend = irela + section->reloc_count;
12229 for (; irela < irelaend; irela++)
12230 {
4ce794b7
AM
12231 enum elf_ppc64_reloc_type r_type;
12232 unsigned int r_indx;
721956f4
AM
12233 enum ppc_stub_type stub_type;
12234 struct ppc_stub_hash_entry *stub_entry;
8387904d 12235 asection *sym_sec, *code_sec;
e054468f 12236 bfd_vma sym_value, code_value;
721956f4 12237 bfd_vma destination;
6911b7dc 12238 unsigned long local_off;
8843416a 12239 bfd_boolean ok_dest;
721956f4 12240 struct ppc_link_hash_entry *hash;
8387904d 12241 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12242 struct elf_link_hash_entry *h;
12243 Elf_Internal_Sym *sym;
721956f4
AM
12244 char *stub_name;
12245 const asection *id_sec;
74f0fb50 12246 struct _opd_sec_data *opd;
e054468f 12247 struct plt_entry *plt_ent;
721956f4
AM
12248
12249 r_type = ELF64_R_TYPE (irela->r_info);
12250 r_indx = ELF64_R_SYM (irela->r_info);
12251
4ce794b7 12252 if (r_type >= R_PPC64_max)
721956f4
AM
12253 {
12254 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12255 goto error_ret_free_internal;
721956f4
AM
12256 }
12257
12258 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12259 if (r_type != R_PPC64_REL24
12260 && r_type != R_PPC64_REL14
12261 && r_type != R_PPC64_REL14_BRTAKEN
12262 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12263 continue;
12264
12265 /* Now determine the call target, its name, value,
12266 section. */
411e1bfb
AM
12267 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12268 r_indx, input_bfd))
12269 goto error_ret_free_internal;
12270 hash = (struct ppc_link_hash_entry *) h;
12271
8843416a 12272 ok_dest = FALSE;
8387904d 12273 fdh = NULL;
7fe2b9a6 12274 sym_value = 0;
411e1bfb 12275 if (hash == NULL)
721956f4 12276 {
411e1bfb 12277 sym_value = sym->st_value;
8843416a 12278 ok_dest = TRUE;
721956f4 12279 }
7fe2b9a6
AM
12280 else if (hash->elf.root.type == bfd_link_hash_defined
12281 || hash->elf.root.type == bfd_link_hash_defweak)
12282 {
12283 sym_value = hash->elf.root.u.def.value;
12284 if (sym_sec->output_section != NULL)
12285 ok_dest = TRUE;
12286 }
12287 else if (hash->elf.root.type == bfd_link_hash_undefweak
12288 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12289 {
99877b66 12290 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12291 use the func descriptor sym instead if it is
12292 defined. */
ceb1f1ef 12293 if (hash->elf.root.root.string[0] == '.'
b31867b6 12294 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 12295 {
8387904d
AM
12296 if (fdh->elf.root.type == bfd_link_hash_defined
12297 || fdh->elf.root.type == bfd_link_hash_defweak)
12298 {
12299 sym_sec = fdh->elf.root.u.def.section;
12300 sym_value = fdh->elf.root.u.def.value;
12301 if (sym_sec->output_section != NULL)
12302 ok_dest = TRUE;
12303 }
99877b66
AM
12304 else
12305 fdh = NULL;
8387904d 12306 }
7fe2b9a6
AM
12307 }
12308 else
12309 {
12310 bfd_set_error (bfd_error_bad_value);
12311 goto error_ret_free_internal;
721956f4
AM
12312 }
12313
8843416a 12314 destination = 0;
6911b7dc 12315 local_off = 0;
8843416a
AM
12316 if (ok_dest)
12317 {
12318 sym_value += irela->r_addend;
12319 destination = (sym_value
12320 + sym_sec->output_offset
12321 + sym_sec->output_section->vma);
6911b7dc
AM
12322 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12323 ? hash->elf.other
12324 : sym->st_other);
8843416a
AM
12325 }
12326
8387904d 12327 code_sec = sym_sec;
e054468f 12328 code_value = sym_value;
74f0fb50
AM
12329 opd = get_opd_info (sym_sec);
12330 if (opd != NULL)
8387904d
AM
12331 {
12332 bfd_vma dest;
12333
74f0fb50 12334 if (hash == NULL && opd->adjust != NULL)
8387904d 12335 {
51aecdc5 12336 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12337 if (adjust == -1)
12338 continue;
e054468f 12339 code_value += adjust;
8387904d
AM
12340 sym_value += adjust;
12341 }
12342 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12343 &code_sec, &code_value, FALSE);
8387904d
AM
12344 if (dest != (bfd_vma) -1)
12345 {
12346 destination = dest;
12347 if (fdh != NULL)
12348 {
12349 /* Fixup old ABI sym to point at code
12350 entry. */
99877b66 12351 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12352 hash->elf.root.u.def.section = code_sec;
e054468f 12353 hash->elf.root.u.def.value = code_value;
8387904d
AM
12354 }
12355 }
12356 }
12357
721956f4 12358 /* Determine what (if any) linker stub is needed. */
e054468f 12359 plt_ent = NULL;
721956f4 12360 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12361 &plt_ent, destination,
12362 local_off);
ad8e1ba5
AM
12363
12364 if (stub_type != ppc_stub_plt_call)
12365 {
12366 /* Check whether we need a TOC adjusting stub.
12367 Since the linker pastes together pieces from
12368 different object files when creating the
12369 _init and _fini functions, it may be that a
12370 call to what looks like a local sym is in
12371 fact a call needing a TOC adjustment. */
8387904d
AM
12372 if (code_sec != NULL
12373 && code_sec->output_section != NULL
6f20ed8a
AM
12374 && (htab->sec_info[code_sec->id].toc_off
12375 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12376 && (code_sec->has_toc_reloc
12377 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12378 stub_type = ppc_stub_long_branch_r2off;
12379 }
12380
721956f4
AM
12381 if (stub_type == ppc_stub_none)
12382 continue;
12383
411e1bfb
AM
12384 /* __tls_get_addr calls might be eliminated. */
12385 if (stub_type != ppc_stub_plt_call
12386 && hash != NULL
8387904d
AM
12387 && (hash == htab->tls_get_addr
12388 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12389 && section->has_tls_reloc
12390 && irela != internal_relocs)
12391 {
12392 /* Get tls info. */
f961d9dd 12393 unsigned char *tls_mask;
411e1bfb 12394
3a71aa26 12395 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12396 irela - 1, input_bfd))
12397 goto error_ret_free_internal;
e7b938ca 12398 if (*tls_mask != 0)
411e1bfb
AM
12399 continue;
12400 }
12401
3b421ab3
AM
12402 if (stub_type == ppc_stub_plt_call
12403 && irela + 1 < irelaend
12404 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12405 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12406 {
12407 if (!tocsave_find (htab, INSERT,
12408 &local_syms, irela + 1, input_bfd))
12409 goto error_ret_free_internal;
12410 }
12411 else if (stub_type == ppc_stub_plt_call)
12412 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12413
721956f4 12414 /* Support for grouping stub sections. */
6f20ed8a 12415 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12416
12417 /* Get the name of this stub. */
12418 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12419 if (!stub_name)
12420 goto error_ret_free_internal;
12421
12422 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12423 stub_name, FALSE, FALSE);
721956f4
AM
12424 if (stub_entry != NULL)
12425 {
12426 /* The proper stub has already been created. */
12427 free (stub_name);
794e51c0
AM
12428 if (stub_type == ppc_stub_plt_call_r2save)
12429 stub_entry->stub_type = stub_type;
721956f4
AM
12430 continue;
12431 }
12432
25f53a85 12433 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12434 if (stub_entry == NULL)
12435 {
12436 free (stub_name);
6cdc0ccc
AM
12437 error_ret_free_internal:
12438 if (elf_section_data (section)->relocs == NULL)
12439 free (internal_relocs);
12440 error_ret_free_local:
12441 if (local_syms != NULL
12442 && (symtab_hdr->contents
12443 != (unsigned char *) local_syms))
12444 free (local_syms);
b34976b6 12445 return FALSE;
721956f4
AM
12446 }
12447
ad8e1ba5 12448 stub_entry->stub_type = stub_type;
794e51c0
AM
12449 if (stub_type != ppc_stub_plt_call
12450 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12451 {
12452 stub_entry->target_value = code_value;
12453 stub_entry->target_section = code_sec;
12454 }
12455 else
12456 {
12457 stub_entry->target_value = sym_value;
12458 stub_entry->target_section = sym_sec;
12459 }
721956f4 12460 stub_entry->h = hash;
e054468f 12461 stub_entry->plt_ent = plt_ent;
6911b7dc 12462 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12463
12464 if (stub_entry->h != NULL)
12465 htab->stub_globals += 1;
721956f4
AM
12466 }
12467
12468 /* We're done with the internal relocs, free them. */
6cdc0ccc 12469 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12470 free (internal_relocs);
721956f4 12471 }
6cdc0ccc
AM
12472
12473 if (local_syms != NULL
12474 && symtab_hdr->contents != (unsigned char *) local_syms)
12475 {
12476 if (!info->keep_memory)
12477 free (local_syms);
12478 else
12479 symtab_hdr->contents = (unsigned char *) local_syms;
12480 }
721956f4
AM
12481 }
12482
5c3dead3 12483 /* We may have added some stubs. Find out the new size of the
721956f4 12484 stub sections. */
e7d1c40c 12485 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12486 stub_sec != NULL;
12487 stub_sec = stub_sec->next)
e717da7e 12488 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12489 {
5c3dead3 12490 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12491 stub_sec->size = 0;
12492 stub_sec->reloc_count = 0;
84f5d08e 12493 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12494 }
eea6121a
AM
12495
12496 htab->brlt->size = 0;
84f5d08e
AM
12497 htab->brlt->reloc_count = 0;
12498 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12499 if (htab->relbrlt != NULL)
eea6121a 12500 htab->relbrlt->size = 0;
721956f4 12501
63bc6f6c 12502 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12503
a4b6fadd
AM
12504 for (group = htab->group; group != NULL; group = group->next)
12505 if (group->needs_save_res)
12506 group->stub_sec->size += htab->sfpr->size;
12507
176a0d42
AM
12508 if (info->emitrelocations
12509 && htab->glink != NULL && htab->glink->size != 0)
12510 {
12511 htab->glink->reloc_count = 1;
12512 htab->glink->flags |= SEC_RELOC;
12513 }
12514
58d180e8
AM
12515 if (htab->glink_eh_frame != NULL
12516 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12517 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12518 {
4bbe044a 12519 size_t size = 0, align;
58d180e8 12520
e7d1c40c 12521 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12522 stub_sec != NULL;
12523 stub_sec = stub_sec->next)
12524 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
da44f4e5 12525 size += 24;
58d180e8
AM
12526 if (htab->glink != NULL && htab->glink->size != 0)
12527 size += 24;
12528 if (size != 0)
12529 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12530 align = 1;
12531 align <<= htab->glink_eh_frame->output_section->alignment_power;
12532 align -= 1;
12533 size = (size + align) & ~align;
58d180e8
AM
12534 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12535 htab->glink_eh_frame->size = size;
12536 }
12537
e7d1c40c
AM
12538 if (htab->params->plt_stub_align != 0)
12539 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12540 stub_sec != NULL;
12541 stub_sec = stub_sec->next)
12542 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12543 stub_sec->size = ((stub_sec->size
12544 + (1 << htab->params->plt_stub_align) - 1)
29f628db 12545 & -(1 << htab->params->plt_stub_align));
794e51c0 12546
e7d1c40c 12547 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12548 stub_sec != NULL;
12549 stub_sec = stub_sec->next)
12550 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12551 && stub_sec->rawsize != stub_sec->size)
12552 break;
12553
12554 /* Exit from this loop when no stubs have been added, and no stubs
12555 have changed size. */
58d180e8
AM
12556 if (stub_sec == NULL
12557 && (htab->glink_eh_frame == NULL
12558 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12559 break;
12560
721956f4 12561 /* Ask the linker to do its stuff. */
e7d1c40c 12562 (*htab->params->layout_sections_again) ();
721956f4
AM
12563 }
12564
da44f4e5
AM
12565 if (htab->glink_eh_frame != NULL
12566 && htab->glink_eh_frame->size != 0)
12567 {
12568 bfd_vma val;
12569 bfd_byte *p, *last_fde;
12570 size_t last_fde_len, size, align, pad;
12571 asection *stub_sec;
12572
12573 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12574 if (p == NULL)
12575 return FALSE;
12576 htab->glink_eh_frame->contents = p;
12577 last_fde = p;
12578
12579 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12580 /* CIE length (rewrite in case little-endian). */
12581 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12582 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12583 p += sizeof (glink_eh_frame_cie);
12584
12585 for (stub_sec = htab->params->stub_bfd->sections;
12586 stub_sec != NULL;
12587 stub_sec = stub_sec->next)
12588 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12589 {
12590 last_fde = p;
12591 last_fde_len = 20;
12592 /* FDE length. */
12593 bfd_put_32 (htab->elf.dynobj, 20, p);
12594 p += 4;
12595 /* CIE pointer. */
12596 val = p - htab->glink_eh_frame->contents;
12597 bfd_put_32 (htab->elf.dynobj, val, p);
12598 p += 4;
12599 /* Offset to stub section, written later. */
12600 p += 4;
12601 /* stub section size. */
12602 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12603 p += 4;
12604 /* Augmentation. */
12605 p += 1;
12606 /* Pad. */
12607 p += 7;
12608 }
12609 if (htab->glink != NULL && htab->glink->size != 0)
12610 {
12611 last_fde = p;
12612 last_fde_len = 20;
12613 /* FDE length. */
12614 bfd_put_32 (htab->elf.dynobj, 20, p);
12615 p += 4;
12616 /* CIE pointer. */
12617 val = p - htab->glink_eh_frame->contents;
12618 bfd_put_32 (htab->elf.dynobj, val, p);
12619 p += 4;
12620 /* Offset to .glink, written later. */
12621 p += 4;
12622 /* .glink size. */
12623 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12624 p += 4;
12625 /* Augmentation. */
12626 p += 1;
12627
12628 *p++ = DW_CFA_advance_loc + 1;
12629 *p++ = DW_CFA_register;
12630 *p++ = 65;
9f08fa5c 12631 *p++ = htab->opd_abi ? 12 : 0;
da44f4e5
AM
12632 *p++ = DW_CFA_advance_loc + 4;
12633 *p++ = DW_CFA_restore_extended;
12634 *p++ = 65;
12635 }
12636 /* Subsume any padding into the last FDE if user .eh_frame
12637 sections are aligned more than glink_eh_frame. Otherwise any
12638 zero padding will be seen as a terminator. */
12639 size = p - htab->glink_eh_frame->contents;
12640 align = 1;
12641 align <<= htab->glink_eh_frame->output_section->alignment_power;
12642 align -= 1;
12643 pad = ((size + align) & ~align) - size;
12644 htab->glink_eh_frame->size = size + pad;
12645 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12646 }
12647
d969d15f
AM
12648 maybe_strip_output (info, htab->brlt);
12649 if (htab->glink_eh_frame != NULL)
12650 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12651
b34976b6 12652 return TRUE;
721956f4
AM
12653}
12654
12655/* Called after we have determined section placement. If sections
805fc799 12656 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12657
805fc799 12658bfd_vma
1c865ab2 12659ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12660{
805fc799 12661 asection *s;
a27e685f 12662 bfd_vma TOCstart, adjust;
721956f4 12663
43417696
AM
12664 if (info != NULL)
12665 {
12666 struct elf_link_hash_entry *h;
12667 struct elf_link_hash_table *htab = elf_hash_table (info);
12668
12669 if (is_elf_hash_table (htab)
12670 && htab->hgot != NULL)
12671 h = htab->hgot;
12672 else
12673 {
12674 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12675 if (is_elf_hash_table (htab))
12676 htab->hgot = h;
12677 }
12678 if (h != NULL
12679 && h->root.type == bfd_link_hash_defined
12680 && !h->root.linker_def
12681 && (!is_elf_hash_table (htab)
12682 || h->def_regular))
12683 {
12684 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12685 + h->root.u.def.section->output_offset
12686 + h->root.u.def.section->output_section->vma);
12687 _bfd_set_gp_value (obfd, TOCstart);
12688 return TOCstart;
12689 }
12690 }
12691
805fc799
AM
12692 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12693 order. The TOC starts where the first of these sections starts. */
12694 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12695 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12696 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12697 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12698 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12699 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12700 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12701 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12702 {
12703 /* This may happen for
12704 o references to TOC base (SYM@toc / TOC[tc0]) without a
12705 .toc directive
12706 o bad linker script
12707 o --gc-sections and empty TOC sections
12708
12709 FIXME: Warn user? */
12710
12711 /* Look for a likely section. We probably won't even be
12712 using TOCstart. */
12713 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12714 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12715 | SEC_EXCLUDE))
805fc799
AM
12716 == (SEC_ALLOC | SEC_SMALL_DATA))
12717 break;
721956f4 12718 if (s == NULL)
805fc799 12719 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12720 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12721 == (SEC_ALLOC | SEC_SMALL_DATA))
12722 break;
721956f4 12723 if (s == NULL)
805fc799 12724 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12725 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12726 == SEC_ALLOC)
805fc799 12727 break;
721956f4 12728 if (s == NULL)
805fc799 12729 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12730 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12731 break;
12732 }
721956f4 12733
805fc799
AM
12734 TOCstart = 0;
12735 if (s != NULL)
12736 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12737
a27e685f
AM
12738 /* Force alignment. */
12739 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12740 TOCstart -= adjust;
1c865ab2
AM
12741 _bfd_set_gp_value (obfd, TOCstart);
12742
810d4e75 12743 if (info != NULL && s != NULL)
1c865ab2
AM
12744 {
12745 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12746
810d4e75
AM
12747 if (htab != NULL)
12748 {
12749 if (htab->elf.hgot != NULL)
12750 {
a27e685f 12751 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12752 htab->elf.hgot->root.u.def.section = s;
12753 }
12754 }
12755 else
1c865ab2 12756 {
810d4e75
AM
12757 struct bfd_link_hash_entry *bh = NULL;
12758 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12759 s, TOC_BASE_OFF - adjust,
12760 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12761 }
12762 }
805fc799 12763 return TOCstart;
721956f4
AM
12764}
12765
a345bc8d
AM
12766/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12767 write out any global entry stubs. */
12768
12769static bfd_boolean
12770build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12771{
12772 struct bfd_link_info *info;
12773 struct ppc_link_hash_table *htab;
12774 struct plt_entry *pent;
12775 asection *s;
12776
12777 if (h->root.type == bfd_link_hash_indirect)
12778 return TRUE;
12779
12780 if (!h->pointer_equality_needed)
12781 return TRUE;
12782
12783 if (h->def_regular)
12784 return TRUE;
12785
12786 info = inf;
12787 htab = ppc_hash_table (info);
12788 if (htab == NULL)
12789 return FALSE;
12790
12791 s = htab->glink;
12792 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12793 if (pent->plt.offset != (bfd_vma) -1
12794 && pent->addend == 0)
12795 {
12796 bfd_byte *p;
12797 asection *plt;
12798 bfd_vma off;
12799
a345bc8d 12800 p = s->contents + h->root.u.def.value;
33e44f2e 12801 plt = htab->elf.splt;
a345bc8d
AM
12802 if (!htab->elf.dynamic_sections_created
12803 || h->dynindx == -1)
33e44f2e 12804 plt = htab->elf.iplt;
a345bc8d
AM
12805 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12806 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12807
12808 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12809 {
12810 info->callbacks->einfo
12811 (_("%P: linkage table error against `%T'\n"),
12812 h->root.root.string);
12813 bfd_set_error (bfd_error_bad_value);
12814 htab->stub_error = TRUE;
12815 }
12816
7341d5e2
AM
12817 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12818 if (htab->params->emit_stub_syms)
12819 {
12820 size_t len = strlen (h->root.root.string);
12821 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12822
12823 if (name == NULL)
12824 return FALSE;
12825
12826 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12827 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12828 if (h == NULL)
12829 return FALSE;
12830 if (h->root.type == bfd_link_hash_new)
12831 {
12832 h->root.type = bfd_link_hash_defined;
12833 h->root.u.def.section = s;
12834 h->root.u.def.value = p - s->contents;
12835 h->ref_regular = 1;
12836 h->def_regular = 1;
12837 h->ref_regular_nonweak = 1;
12838 h->forced_local = 1;
12839 h->non_elf = 0;
2ec55de3 12840 h->root.linker_def = 1;
7341d5e2
AM
12841 }
12842 }
12843
a345bc8d
AM
12844 if (PPC_HA (off) != 0)
12845 {
12846 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12847 p += 4;
12848 }
12849 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12850 p += 4;
12851 bfd_put_32 (s->owner, MTCTR_R12, p);
12852 p += 4;
12853 bfd_put_32 (s->owner, BCTR, p);
12854 break;
12855 }
12856 return TRUE;
12857}
12858
721956f4
AM
12859/* Build all the stubs associated with the current output file.
12860 The stubs are kept in a hash table attached to the main linker
12861 hash table. This function is called via gldelf64ppc_finish. */
12862
b34976b6 12863bfd_boolean
e7d1c40c 12864ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 12865 char **stats)
5d1634d7
AM
12866{
12867 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 12868 struct map_stub *group;
721956f4 12869 asection *stub_sec;
5d1634d7 12870 bfd_byte *p;
e717da7e 12871 int stub_sec_count = 0;
5d1634d7 12872
4dfe6ac6
NC
12873 if (htab == NULL)
12874 return FALSE;
12875
eea6121a 12876 /* Allocate memory to hold the linker stubs. */
e7d1c40c 12877 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12878 stub_sec != NULL;
12879 stub_sec = stub_sec->next)
eea6121a
AM
12880 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12881 && stub_sec->size != 0)
e717da7e 12882 {
e7d1c40c 12883 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
12884 if (stub_sec->contents == NULL)
12885 return FALSE;
12886 /* We want to check that built size is the same as calculated
12887 size. rawsize is a convenient location to use. */
12888 stub_sec->rawsize = stub_sec->size;
12889 stub_sec->size = 0;
e717da7e 12890 }
5d1634d7 12891
23eb7e01 12892 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12893 {
9f951329 12894 unsigned int indx;
ad8e1ba5 12895 bfd_vma plt0;
9f951329 12896
721956f4 12897 /* Build the .glink plt call stub. */
e7d1c40c 12898 if (htab->params->emit_stub_syms)
97b639ba
AM
12899 {
12900 struct elf_link_hash_entry *h;
468392fb
AM
12901 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12902 TRUE, FALSE, FALSE);
97b639ba
AM
12903 if (h == NULL)
12904 return FALSE;
12905 if (h->root.type == bfd_link_hash_new)
12906 {
12907 h->root.type = bfd_link_hash_defined;
12908 h->root.u.def.section = htab->glink;
ee4bf8d2 12909 h->root.u.def.value = 8;
f5385ebf
AM
12910 h->ref_regular = 1;
12911 h->def_regular = 1;
12912 h->ref_regular_nonweak = 1;
12913 h->forced_local = 1;
12914 h->non_elf = 0;
2ec55de3 12915 h->root.linker_def = 1;
97b639ba
AM
12916 }
12917 }
33e44f2e
AM
12918 plt0 = (htab->elf.splt->output_section->vma
12919 + htab->elf.splt->output_offset
12920 - 16);
176a0d42
AM
12921 if (info->emitrelocations)
12922 {
12923 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12924 if (r == NULL)
12925 return FALSE;
12926 r->r_offset = (htab->glink->output_offset
12927 + htab->glink->output_section->vma);
12928 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12929 r->r_addend = plt0;
12930 }
4ce794b7 12931 p = htab->glink->contents;
176a0d42 12932 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
12933 bfd_put_64 (htab->glink->owner, plt0, p);
12934 p += 8;
b9e5796b
AM
12935 if (htab->opd_abi)
12936 {
12937 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12938 p += 4;
12939 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12940 p += 4;
12941 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12942 p += 4;
12943 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12944 p += 4;
12945 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12946 p += 4;
12947 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12948 p += 4;
12949 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12950 p += 4;
12951 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12952 p += 4;
12953 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12954 p += 4;
12955 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12956 p += 4;
12957 }
12958 else
12959 {
12960 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12961 p += 4;
12962 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12963 p += 4;
12964 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12965 p += 4;
12966 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12967 p += 4;
12968 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12969 p += 4;
12970 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12971 p += 4;
12972 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12973 p += 4;
12974 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12975 p += 4;
12976 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12977 p += 4;
12978 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12979 p += 4;
12980 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12981 p += 4;
12982 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12983 p += 4;
12984 }
4ce794b7 12985 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 12986 p += 4;
ee4bf8d2
AM
12987 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12988 {
12989 bfd_put_32 (htab->glink->owner, NOP, p);
12990 p += 4;
12991 }
ad8e1ba5 12992
9f951329
AM
12993 /* Build the .glink lazy link call stubs. */
12994 indx = 0;
a345bc8d 12995 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 12996 {
b9e5796b 12997 if (htab->opd_abi)
9f951329 12998 {
b9e5796b
AM
12999 if (indx < 0x8000)
13000 {
13001 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13002 p += 4;
13003 }
13004 else
13005 {
13006 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13007 p += 4;
13008 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13009 p);
13010 p += 4;
13011 }
9f951329 13012 }
4ce794b7 13013 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13014 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13015 indx++;
9f951329
AM
13016 p += 4;
13017 }
a345bc8d
AM
13018
13019 /* Build .glink global entry stubs. */
13020 if (htab->glink->size > htab->glink->rawsize)
afe397ea 13021 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 13022 }
5d1634d7 13023
7341d5e2 13024 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13025 {
4ce794b7 13026 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13027 htab->brlt->size);
4ce794b7 13028 if (htab->brlt->contents == NULL)
b34976b6 13029 return FALSE;
721956f4 13030 }
ee75fd95 13031 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13032 {
13033 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13034 htab->relbrlt->size);
63bc6f6c
AM
13035 if (htab->relbrlt->contents == NULL)
13036 return FALSE;
13037 }
5d1634d7 13038
721956f4
AM
13039 /* Build the stubs as directed by the stub hash table. */
13040 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13041
a4b6fadd
AM
13042 for (group = htab->group; group != NULL; group = group->next)
13043 if (group->needs_save_res)
13044 {
13045 stub_sec = group->stub_sec;
13046 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13047 htab->sfpr->size);
13048 if (htab->params->emit_stub_syms)
13049 {
13050 unsigned int i;
13051
13052 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13053 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13054 return FALSE;
13055 }
13056 stub_sec->size += htab->sfpr->size;
13057 }
13058
aa8a7074
AM
13059 if (htab->relbrlt != NULL)
13060 htab->relbrlt->reloc_count = 0;
13061
e7d1c40c
AM
13062 if (htab->params->plt_stub_align != 0)
13063 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
13064 stub_sec != NULL;
13065 stub_sec = stub_sec->next)
13066 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
13067 stub_sec->size = ((stub_sec->size
13068 + (1 << htab->params->plt_stub_align) - 1)
29f628db 13069 & -(1 << htab->params->plt_stub_align));
794e51c0 13070
e7d1c40c 13071 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13072 stub_sec != NULL;
13073 stub_sec = stub_sec->next)
e717da7e
AM
13074 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13075 {
13076 stub_sec_count += 1;
eea6121a 13077 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
13078 break;
13079 }
5d1634d7 13080
da44f4e5
AM
13081 /* Note that the glink_eh_frame check here is not only testing that
13082 the generated size matched the calculated size but also that
13083 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 13084 if (stub_sec != NULL
58d180e8
AM
13085 || (htab->glink_eh_frame != NULL
13086 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 13087 {
b34976b6 13088 htab->stub_error = TRUE;
8de848d8 13089 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13090 }
721956f4 13091
d2a300cf
AM
13092 if (htab->stub_error)
13093 return FALSE;
13094
13095 if (stats != NULL)
13096 {
13097 *stats = bfd_malloc (500);
13098 if (*stats == NULL)
13099 return FALSE;
13100
ee75fd95 13101 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
13102 " branch %lu\n"
13103 " toc adjust %lu\n"
13104 " long branch %lu\n"
13105 " long toc adj %lu\n"
794e51c0 13106 " plt call %lu\n"
7341d5e2
AM
13107 " plt call toc %lu\n"
13108 " global entry %lu"),
e717da7e 13109 stub_sec_count,
ee75fd95 13110 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
13111 htab->stub_count[ppc_stub_long_branch - 1],
13112 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13113 htab->stub_count[ppc_stub_plt_branch - 1],
13114 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13115 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13116 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13117 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13118 }
13119 return TRUE;
5bd4f169
AM
13120}
13121
99877b66
AM
13122/* This function undoes the changes made by add_symbol_adjust. */
13123
13124static bfd_boolean
13125undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
13126{
13127 struct ppc_link_hash_entry *eh;
13128
13129 if (h->root.type == bfd_link_hash_indirect)
13130 return TRUE;
13131
99877b66
AM
13132 eh = (struct ppc_link_hash_entry *) h;
13133 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
13134 return TRUE;
13135
13136 eh->elf.root.type = bfd_link_hash_undefined;
13137 return TRUE;
13138}
13139
13140void
13141ppc64_elf_restore_symbols (struct bfd_link_info *info)
13142{
13143 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
13144
13145 if (htab != NULL)
13146 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
13147}
13148
60124e18
AM
13149/* What to do when ld finds relocations against symbols defined in
13150 discarded sections. */
13151
13152static unsigned int
13153ppc64_elf_action_discarded (asection *sec)
13154{
13155 if (strcmp (".opd", sec->name) == 0)
13156 return 0;
13157
13158 if (strcmp (".toc", sec->name) == 0)
13159 return 0;
13160
bce50a28
JJ
13161 if (strcmp (".toc1", sec->name) == 0)
13162 return 0;
13163
60124e18
AM
13164 return _bfd_elf_default_action_discarded (sec);
13165}
13166
5bd4f169
AM
13167/* The RELOCATE_SECTION function is called by the ELF backend linker
13168 to handle the relocations for a section.
13169
13170 The relocs are always passed as Rela structures; if the section
13171 actually uses Rel structures, the r_addend field will always be
13172 zero.
13173
13174 This function is responsible for adjust the section contents as
13175 necessary, and (if using Rela relocs and generating a
1049f94e 13176 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13177 necessary.
13178
13179 This function does not have to worry about setting the reloc
13180 address or the reloc symbol index.
13181
13182 LOCAL_SYMS is a pointer to the swapped in local symbols.
13183
13184 LOCAL_SECTIONS is an array giving the section in the input file
13185 corresponding to the st_shndx field of each local symbol.
13186
13187 The global hash table entry for the global symbols can be found
13188 via elf_sym_hashes (input_bfd).
13189
1049f94e 13190 When generating relocatable output, this function must handle
5bd4f169
AM
13191 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13192 going to be the section symbol corresponding to the output
13193 section, which means that the addend must be adjusted
13194 accordingly. */
13195
b34976b6 13196static bfd_boolean
4ce794b7
AM
13197ppc64_elf_relocate_section (bfd *output_bfd,
13198 struct bfd_link_info *info,
13199 bfd *input_bfd,
13200 asection *input_section,
13201 bfd_byte *contents,
13202 Elf_Internal_Rela *relocs,
13203 Elf_Internal_Sym *local_syms,
13204 asection **local_sections)
5bd4f169 13205{
65f38f15 13206 struct ppc_link_hash_table *htab;
5bd4f169
AM
13207 Elf_Internal_Shdr *symtab_hdr;
13208 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13209 Elf_Internal_Rela *rel;
c316a17c 13210 Elf_Internal_Rela *wrel;
5bd4f169 13211 Elf_Internal_Rela *relend;
411e1bfb
AM
13212 Elf_Internal_Rela outrel;
13213 bfd_byte *loc;
411e1bfb 13214 struct got_entry **local_got_ents;
5bd4f169 13215 bfd_vma TOCstart;
b34976b6
AM
13216 bfd_boolean ret = TRUE;
13217 bfd_boolean is_opd;
794e51c0
AM
13218 /* Assume 'at' branch hints. */
13219 bfd_boolean is_isa_v2 = TRUE;
4fe5ca5b 13220 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 13221
65f38f15 13222 /* Initialize howto table if needed. */
5bd4f169 13223 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13224 ppc_howto_init ();
13225
65f38f15 13226 htab = ppc_hash_table (info);
4dfe6ac6
NC
13227 if (htab == NULL)
13228 return FALSE;
ee75fd95
AM
13229
13230 /* Don't relocate stub sections. */
e7d1c40c 13231 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13232 return TRUE;
13233
0c8d6e5c 13234 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13235
411e1bfb 13236 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13237 TOCstart = elf_gp (output_bfd);
0ffa91dd 13238 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13239 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13240 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13241
c316a17c 13242 rel = wrel = relocs;
5bd4f169 13243 relend = relocs + input_section->reloc_count;
c316a17c 13244 for (; rel < relend; wrel++, rel++)
5bd4f169 13245 {
04c9666a 13246 enum elf_ppc64_reloc_type r_type;
31c76678 13247 bfd_vma addend;
5bd4f169
AM
13248 bfd_reloc_status_type r;
13249 Elf_Internal_Sym *sym;
13250 asection *sec;
039b3fef
AM
13251 struct elf_link_hash_entry *h_elf;
13252 struct ppc_link_hash_entry *h;
13253 struct ppc_link_hash_entry *fdh;
5bd4f169 13254 const char *sym_name;
0d4792f7 13255 unsigned long r_symndx, toc_symndx;
3a71aa26 13256 bfd_vma toc_addend;
f961d9dd
AM
13257 unsigned char tls_mask, tls_gd, tls_type;
13258 unsigned char sym_type;
5bd4f169 13259 bfd_vma relocation;
b34976b6
AM
13260 bfd_boolean unresolved_reloc;
13261 bfd_boolean warned;
bc30df16 13262 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13263 unsigned int insn;
e11840f9 13264 unsigned int mask;
721956f4
AM
13265 struct ppc_stub_hash_entry *stub_entry;
13266 bfd_vma max_br_offset;
13267 bfd_vma from;
c316a17c 13268 Elf_Internal_Rela orig_rel;
b80eed39
AM
13269 reloc_howto_type *howto;
13270 struct reloc_howto_struct alt_howto;
5bd4f169 13271
c316a17c
AM
13272 again:
13273 orig_rel = *rel;
13274
4ce794b7 13275 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13276 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13277
13278 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13279 symbol of the previous ADDR64 reloc. The symbol gives us the
13280 proper TOC base to use. */
13281 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13282 && wrel != relocs
13283 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13284 && is_opd)
c316a17c 13285 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13286
4ce794b7
AM
13287 sym = NULL;
13288 sec = NULL;
039b3fef 13289 h_elf = NULL;
4ce794b7 13290 sym_name = NULL;
b34976b6
AM
13291 unresolved_reloc = FALSE;
13292 warned = FALSE;
65f38f15 13293
0b13192e 13294 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13295 {
13296 /* It's a local symbol. */
74f0fb50 13297 struct _opd_sec_data *opd;
4025353c 13298
5bd4f169
AM
13299 sym = local_syms + r_symndx;
13300 sec = local_sections[r_symndx];
26c61ae5 13301 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13302 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13303 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13304 opd = get_opd_info (sec);
13305 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13306 {
51aecdc5
AM
13307 long adjust = opd->adjust[OPD_NDX (sym->st_value
13308 + rel->r_addend)];
4025353c
AM
13309 if (adjust == -1)
13310 relocation = 0;
13311 else
4cc603a5
AM
13312 {
13313 /* If this is a relocation against the opd section sym
13314 and we have edited .opd, adjust the reloc addend so
13315 that ld -r and ld --emit-relocs output is correct.
13316 If it is a reloc against some other .opd symbol,
13317 then the symbol value will be adjusted later. */
13318 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13319 rel->r_addend += adjust;
13320 else
13321 relocation += adjust;
13322 }
1e2f5b6e 13323 }
5bd4f169
AM
13324 }
13325 else
13326 {
62d887d4
L
13327 bfd_boolean ignored;
13328
b2a8e766
AM
13329 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13330 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13331 h_elf, sec, relocation,
62d887d4 13332 unresolved_reloc, warned, ignored);
039b3fef
AM
13333 sym_name = h_elf->root.root.string;
13334 sym_type = h_elf->type;
b69fdb4e
AM
13335 if (sec != NULL
13336 && sec->owner == output_bfd
13337 && strcmp (sec->name, ".opd") == 0)
13338 {
13339 /* This is a symbol defined in a linker script. All
13340 such are defined in output sections, even those
13341 defined by simple assignment from a symbol defined in
13342 an input section. Transfer the symbol to an
13343 appropriate input .opd section, so that a branch to
13344 this symbol will be mapped to the location specified
13345 by the opd entry. */
13346 struct bfd_link_order *lo;
13347 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13348 if (lo->type == bfd_indirect_link_order)
13349 {
13350 asection *isec = lo->u.indirect.section;
13351 if (h_elf->root.u.def.value >= isec->output_offset
13352 && h_elf->root.u.def.value < (isec->output_offset
13353 + isec->size))
13354 {
13355 h_elf->root.u.def.value -= isec->output_offset;
13356 h_elf->root.u.def.section = isec;
13357 sec = isec;
13358 break;
13359 }
13360 }
13361 }
5bd4f169 13362 }
039b3fef 13363 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13364
dbaa2011 13365 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13366 {
13367 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13368 input_bfd, input_section,
13369 contents + rel->r_offset);
13370 wrel->r_offset = rel->r_offset;
13371 wrel->r_info = 0;
13372 wrel->r_addend = 0;
13373
13374 /* For ld -r, remove relocations in debug sections against
13375 sections defined in discarded sections. Not done for
13376 non-debug to preserve relocs in .eh_frame which the
13377 eh_frame editing code expects to be present. */
13378 if (bfd_link_relocatable (info)
13379 && (input_section->flags & SEC_DEBUGGING))
13380 wrel--;
13381
13382 continue;
13383 }
ab96bf03 13384
0e1862bb 13385 if (bfd_link_relocatable (info))
c316a17c 13386 goto copy_reloc;
ab96bf03 13387
f40da81b
AM
13388 if (h != NULL && &h->elf == htab->elf.hgot)
13389 {
6f20ed8a 13390 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13391 sec = bfd_abs_section_ptr;
13392 unresolved_reloc = FALSE;
13393 }
13394
951fd09b
AM
13395 /* TLS optimizations. Replace instruction sequences and relocs
13396 based on information we collected in tls_optimize. We edit
13397 RELOCS so that --emit-relocs will output something sensible
13398 for the final instruction stream. */
13399 tls_mask = 0;
13400 tls_gd = 0;
0d4792f7 13401 toc_symndx = 0;
727fc41e
AM
13402 if (h != NULL)
13403 tls_mask = h->tls_mask;
13404 else if (local_got_ents != NULL)
411e1bfb 13405 {
e054468f
AM
13406 struct plt_entry **local_plt = (struct plt_entry **)
13407 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13408 unsigned char *lgot_masks = (unsigned char *)
e054468f 13409 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13410 tls_mask = lgot_masks[r_symndx];
13411 }
13412 if (tls_mask == 0
13413 && (r_type == R_PPC64_TLS
13414 || r_type == R_PPC64_TLSGD
13415 || r_type == R_PPC64_TLSLD))
13416 {
13417 /* Check for toc tls entries. */
f961d9dd 13418 unsigned char *toc_tls;
0d4792f7 13419
727fc41e
AM
13420 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13421 &local_syms, rel, input_bfd))
13422 return FALSE;
0d4792f7 13423
727fc41e
AM
13424 if (toc_tls)
13425 tls_mask = *toc_tls;
0d4792f7
AM
13426 }
13427
13428 /* Check that tls relocs are used with tls syms, and non-tls
13429 relocs are used with non-tls syms. */
cf35638d 13430 if (r_symndx != STN_UNDEF
0d4792f7
AM
13431 && r_type != R_PPC64_NONE
13432 && (h == NULL
039b3fef
AM
13433 || h->elf.root.type == bfd_link_hash_defined
13434 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13435 && (IS_PPC64_TLS_RELOC (r_type)
13436 != (sym_type == STT_TLS
13437 || (sym_type == STT_SECTION
13438 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13439 {
727fc41e
AM
13440 if (tls_mask != 0
13441 && (r_type == R_PPC64_TLS
13442 || r_type == R_PPC64_TLSGD
13443 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13444 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13445 ;
13446 else
25f53a85 13447 info->callbacks->einfo
1d483afe 13448 (!IS_PPC64_TLS_RELOC (r_type)
bc30df16
AM
13449 ? _("%P: %H: %s used with TLS symbol `%T'\n")
13450 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13451 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13452 ppc64_elf_howto_table[r_type]->name,
13453 sym_name);
411e1bfb
AM
13454 }
13455
13456 /* Ensure reloc mapping code below stays sane. */
13457 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13458 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13459 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13460 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13461 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13462 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13463 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13464 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13465 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13466 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13467 abort ();
0d4792f7 13468
411e1bfb
AM
13469 switch (r_type)
13470 {
13471 default:
411e1bfb
AM
13472 break;
13473
ba761f19
AM
13474 case R_PPC64_LO_DS_OPT:
13475 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13476 if ((insn & (0x3f << 26)) != 58u << 26)
13477 abort ();
13478 insn += (14u << 26) - (58u << 26);
13479 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13480 r_type = R_PPC64_TOC16_LO;
13481 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13482 break;
13483
411e1bfb
AM
13484 case R_PPC64_TOC16:
13485 case R_PPC64_TOC16_LO:
13486 case R_PPC64_TOC16_DS:
13487 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13488 {
13489 /* Check for toc tls entries. */
f961d9dd 13490 unsigned char *toc_tls;
951fd09b 13491 int retval;
411e1bfb 13492
3a71aa26
AM
13493 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13494 &local_syms, rel, input_bfd);
951fd09b 13495 if (retval == 0)
411e1bfb
AM
13496 return FALSE;
13497
13498 if (toc_tls)
13499 {
951fd09b 13500 tls_mask = *toc_tls;
411e1bfb
AM
13501 if (r_type == R_PPC64_TOC16_DS
13502 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13503 {
13504 if (tls_mask != 0
13505 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13506 goto toctprel;
13507 }
411e1bfb 13508 else
951fd09b
AM
13509 {
13510 /* If we found a GD reloc pair, then we might be
13511 doing a GD->IE transition. */
13512 if (retval == 2)
13513 {
13514 tls_gd = TLS_TPRELGD;
13515 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13516 goto tls_ldgd_opt;
951fd09b
AM
13517 }
13518 else if (retval == 3)
13519 {
13520 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13521 goto tls_ldgd_opt;
951fd09b
AM
13522 }
13523 }
411e1bfb
AM
13524 }
13525 }
13526 break;
13527
9d6ded02
AM
13528 case R_PPC64_GOT_TPREL16_HI:
13529 case R_PPC64_GOT_TPREL16_HA:
13530 if (tls_mask != 0
13531 && (tls_mask & TLS_TPREL) == 0)
13532 {
13533 rel->r_offset -= d_offset;
13534 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13535 r_type = R_PPC64_NONE;
13536 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13537 }
13538 break;
13539
411e1bfb
AM
13540 case R_PPC64_GOT_TPREL16_DS:
13541 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13542 if (tls_mask != 0
13543 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13544 {
81407a69 13545 toctprel:
c316a17c
AM
13546 insn = bfd_get_32 (output_bfd,
13547 contents + rel->r_offset - d_offset);
411e1bfb
AM
13548 insn &= 31 << 21;
13549 insn |= 0x3c0d0000; /* addis 0,13,0 */
c316a17c
AM
13550 bfd_put_32 (output_bfd, insn,
13551 contents + rel->r_offset - d_offset);
411e1bfb 13552 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13553 if (toc_symndx != 0)
13554 {
13555 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13556 rel->r_addend = toc_addend;
0d4792f7
AM
13557 /* We changed the symbol. Start over in order to
13558 get h, sym, sec etc. right. */
c316a17c 13559 goto again;
0d4792f7
AM
13560 }
13561 else
13562 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13563 }
13564 break;
13565
13566 case R_PPC64_TLS:
951fd09b
AM
13567 if (tls_mask != 0
13568 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13569 {
411e1bfb 13570 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
13571 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13572 if (insn == 0)
411e1bfb 13573 abort ();
411e1bfb 13574 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 13575 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13576 PPC64_TPREL16_LO which is at low-order half-word. */
13577 rel->r_offset += d_offset;
0d4792f7
AM
13578 r_type = R_PPC64_TPREL16_LO;
13579 if (toc_symndx != 0)
13580 {
13581 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13582 rel->r_addend = toc_addend;
0d4792f7
AM
13583 /* We changed the symbol. Start over in order to
13584 get h, sym, sec etc. right. */
c316a17c 13585 goto again;
0d4792f7
AM
13586 }
13587 else
13588 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13589 }
13590 break;
13591
411e1bfb
AM
13592 case R_PPC64_GOT_TLSGD16_HI:
13593 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13594 tls_gd = TLS_TPRELGD;
13595 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13596 goto tls_gdld_hi;
13597 break;
13598
411e1bfb
AM
13599 case R_PPC64_GOT_TLSLD16_HI:
13600 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13601 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13602 {
951fd09b
AM
13603 tls_gdld_hi:
13604 if ((tls_mask & tls_gd) != 0)
13605 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13606 + R_PPC64_GOT_TPREL16_DS);
13607 else
411e1bfb 13608 {
4fe5ca5b 13609 rel->r_offset -= d_offset;
727ac201 13610 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 13611 r_type = R_PPC64_NONE;
411e1bfb 13612 }
951fd09b 13613 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13614 }
13615 break;
13616
951fd09b
AM
13617 case R_PPC64_GOT_TLSGD16:
13618 case R_PPC64_GOT_TLSGD16_LO:
13619 tls_gd = TLS_TPRELGD;
13620 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13621 goto tls_ldgd_opt;
951fd09b 13622 break;
411e1bfb 13623
951fd09b
AM
13624 case R_PPC64_GOT_TLSLD16:
13625 case R_PPC64_GOT_TLSLD16_LO:
13626 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13627 {
3a71aa26 13628 unsigned int insn1, insn2, insn3;
102890f0
AM
13629 bfd_vma offset;
13630
13631 tls_ldgd_opt:
727fc41e
AM
13632 offset = (bfd_vma) -1;
13633 /* If not using the newer R_PPC64_TLSGD/LD to mark
13634 __tls_get_addr calls, we must trust that the call
13635 stays with its arg setup insns, ie. that the next
13636 reloc is the __tls_get_addr call associated with
13637 the current reloc. Edit both insns. */
13638 if (input_section->has_tls_get_addr_call
13639 && rel + 1 < relend
13640 && branch_reloc_hash_match (input_bfd, rel + 1,
13641 htab->tls_get_addr,
13642 htab->tls_get_addr_fd))
13643 offset = rel[1].r_offset;
b86ac8e3
AM
13644 /* We read the low GOT_TLS (or TOC16) insn because we
13645 need to keep the destination reg. It may be
13646 something other than the usual r3, and moved to r3
13647 before the call by intervening code. */
13648 insn1 = bfd_get_32 (output_bfd,
13649 contents + rel->r_offset - d_offset);
102890f0 13650 if ((tls_mask & tls_gd) != 0)
411e1bfb 13651 {
102890f0 13652 /* IE */
b86ac8e3 13653 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13654 insn1 |= 58 << 26; /* ld */
13655 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13656 if (offset != (bfd_vma) -1)
f58d5a2d 13657 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13658 if ((tls_mask & TLS_EXPLICIT) == 0)
13659 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13660 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13661 else
102890f0
AM
13662 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13663 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13664 }
13665 else
13666 {
13667 /* LE */
b86ac8e3
AM
13668 insn1 &= 0x1f << 21;
13669 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13670 insn2 = 0x38630000; /* addi 3,3,0 */
13671 if (tls_gd == 0)
951fd09b 13672 {
102890f0 13673 /* Was an LD reloc. */
1d483afe
AM
13674 if (toc_symndx)
13675 sec = local_sections[toc_symndx];
13676 for (r_symndx = 0;
13677 r_symndx < symtab_hdr->sh_info;
13678 r_symndx++)
13679 if (local_sections[r_symndx] == sec)
13680 break;
13681 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13682 r_symndx = STN_UNDEF;
102890f0 13683 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13684 if (r_symndx != STN_UNDEF)
1d483afe
AM
13685 rel->r_addend -= (local_syms[r_symndx].st_value
13686 + sec->output_offset
13687 + sec->output_section->vma);
951fd09b 13688 }
102890f0 13689 else if (toc_symndx != 0)
3a71aa26
AM
13690 {
13691 r_symndx = toc_symndx;
13692 rel->r_addend = toc_addend;
13693 }
102890f0
AM
13694 r_type = R_PPC64_TPREL16_HA;
13695 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13696 if (offset != (bfd_vma) -1)
13697 {
13698 rel[1].r_info = ELF64_R_INFO (r_symndx,
13699 R_PPC64_TPREL16_LO);
13700 rel[1].r_offset = offset + d_offset;
13701 rel[1].r_addend = rel->r_addend;
13702 }
102890f0 13703 }
3a71aa26
AM
13704 bfd_put_32 (output_bfd, insn1,
13705 contents + rel->r_offset - d_offset);
727fc41e
AM
13706 if (offset != (bfd_vma) -1)
13707 {
13708 insn3 = bfd_get_32 (output_bfd,
13709 contents + offset + 4);
13710 if (insn3 == NOP
13711 || insn3 == CROR_151515 || insn3 == CROR_313131)
13712 {
13713 rel[1].r_offset += 4;
13714 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13715 insn2 = NOP;
13716 }
13717 bfd_put_32 (output_bfd, insn2, contents + offset);
13718 }
13719 if ((tls_mask & tls_gd) == 0
13720 && (tls_gd == 0 || toc_symndx != 0))
13721 {
13722 /* We changed the symbol. Start over in order
13723 to get h, sym, sec etc. right. */
c316a17c 13724 goto again;
727fc41e
AM
13725 }
13726 }
13727 break;
13728
13729 case R_PPC64_TLSGD:
13730 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13731 {
13732 unsigned int insn2, insn3;
13733 bfd_vma offset = rel->r_offset;
13734
13735 if ((tls_mask & TLS_TPRELGD) != 0)
13736 {
13737 /* IE */
13738 r_type = R_PPC64_NONE;
13739 insn2 = 0x7c636a14; /* add 3,3,13 */
13740 }
13741 else
13742 {
13743 /* LE */
13744 if (toc_symndx != 0)
13745 {
13746 r_symndx = toc_symndx;
13747 rel->r_addend = toc_addend;
13748 }
13749 r_type = R_PPC64_TPREL16_LO;
13750 rel->r_offset = offset + d_offset;
13751 insn2 = 0x38630000; /* addi 3,3,0 */
13752 }
13753 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13754 /* Zap the reloc on the _tls_get_addr call too. */
13755 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13756 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
13757 insn3 = bfd_get_32 (output_bfd,
13758 contents + offset + 4);
102890f0
AM
13759 if (insn3 == NOP
13760 || insn3 == CROR_151515 || insn3 == CROR_313131)
13761 {
727fc41e 13762 rel->r_offset += 4;
3a71aa26
AM
13763 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13764 insn2 = NOP;
102890f0 13765 }
102890f0 13766 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 13767 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13768 goto again;
411e1bfb 13769 }
411e1bfb
AM
13770 break;
13771
727fc41e
AM
13772 case R_PPC64_TLSLD:
13773 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13774 {
13775 unsigned int insn2, insn3;
13776 bfd_vma offset = rel->r_offset;
13777
13778 if (toc_symndx)
13779 sec = local_sections[toc_symndx];
13780 for (r_symndx = 0;
13781 r_symndx < symtab_hdr->sh_info;
13782 r_symndx++)
13783 if (local_sections[r_symndx] == sec)
13784 break;
13785 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13786 r_symndx = STN_UNDEF;
727fc41e 13787 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13788 if (r_symndx != STN_UNDEF)
727fc41e
AM
13789 rel->r_addend -= (local_syms[r_symndx].st_value
13790 + sec->output_offset
13791 + sec->output_section->vma);
13792
13793 r_type = R_PPC64_TPREL16_LO;
13794 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13795 rel->r_offset = offset + d_offset;
13796 /* Zap the reloc on the _tls_get_addr call too. */
13797 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13798 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
13799 insn2 = 0x38630000; /* addi 3,3,0 */
13800 insn3 = bfd_get_32 (output_bfd,
13801 contents + offset + 4);
13802 if (insn3 == NOP
13803 || insn3 == CROR_151515 || insn3 == CROR_313131)
13804 {
13805 rel->r_offset += 4;
13806 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13807 insn2 = NOP;
13808 }
13809 bfd_put_32 (output_bfd, insn2, contents + offset);
c316a17c 13810 goto again;
727fc41e
AM
13811 }
13812 break;
13813
411e1bfb 13814 case R_PPC64_DTPMOD64:
951fd09b
AM
13815 if (rel + 1 < relend
13816 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13817 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13818 {
951fd09b
AM
13819 if ((tls_mask & TLS_GD) == 0)
13820 {
13821 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13822 if ((tls_mask & TLS_TPRELGD) != 0)
13823 r_type = R_PPC64_TPREL64;
13824 else
13825 {
4ce794b7 13826 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13827 r_type = R_PPC64_NONE;
13828 }
13829 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13830 }
13831 }
13832 else
13833 {
13834 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13835 {
4ce794b7 13836 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13837 r_type = R_PPC64_NONE;
951fd09b 13838 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13839 }
411e1bfb
AM
13840 }
13841 break;
13842
13843 case R_PPC64_TPREL64:
951fd09b 13844 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13845 {
13846 r_type = R_PPC64_NONE;
13847 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13848 }
13849 break;
52a82034
AM
13850
13851 case R_PPC64_REL16_HA:
13852 /* If we are generating a non-PIC executable, edit
13853 . 0: addis 2,12,.TOC.-0b@ha
13854 . addi 2,2,.TOC.-0b@l
13855 used by ELFv2 global entry points to set up r2, to
13856 . lis 2,.TOC.@ha
13857 . addi 2,2,.TOC.@l
13858 if .TOC. is in range. */
0e1862bb 13859 if (!bfd_link_pic (info)
810d4e75 13860 && !info->traditional_format
52a82034
AM
13861 && h != NULL && &h->elf == htab->elf.hgot
13862 && rel + 1 < relend
13863 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13864 && rel[1].r_offset == rel->r_offset + 4
13865 && rel[1].r_addend == rel->r_addend + 4
13866 && relocation + 0x80008000 <= 0xffffffff)
13867 {
13868 unsigned int insn1, insn2;
13869 bfd_vma offset = rel->r_offset - d_offset;
13870 insn1 = bfd_get_32 (output_bfd, contents + offset);
13871 insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
13872 if ((insn1 & 0xffff0000) == 0x3c4c0000 /* addis 2,12 */
13873 && (insn2 & 0xffff0000) == 0x38420000 /* addi 2,2 */)
13874 {
13875 r_type = R_PPC64_ADDR16_HA;
13876 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13877 rel->r_addend -= d_offset;
13878 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13879 rel[1].r_addend -= d_offset + 4;
13880 bfd_put_32 (output_bfd, 0x3c400000, contents + offset);
13881 }
13882 }
13883 break;
411e1bfb
AM
13884 }
13885
13886 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 13887 insn = 0;
b25116a9
AM
13888 max_br_offset = 1 << 25;
13889 addend = rel->r_addend;
bc30df16 13890 reloc_dest = DEST_NORMAL;
65f38f15 13891 switch (r_type)
5bd4f169
AM
13892 {
13893 default:
65f38f15 13894 break;
5bd4f169 13895
3b421ab3
AM
13896 case R_PPC64_TOCSAVE:
13897 if (relocation + addend == (rel->r_offset
13898 + input_section->output_offset
13899 + input_section->output_section->vma)
13900 && tocsave_find (htab, NO_INSERT,
13901 &local_syms, rel, input_bfd))
13902 {
13903 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13904 if (insn == NOP
13905 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
13906 bfd_put_32 (input_bfd,
13907 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
13908 contents + rel->r_offset);
13909 }
13910 break;
13911
65f38f15
AM
13912 /* Branch taken prediction relocations. */
13913 case R_PPC64_ADDR14_BRTAKEN:
13914 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
13915 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13916 /* Fall thru. */
65f38f15 13917
86c76c7b 13918 /* Branch not taken prediction relocations. */
65f38f15
AM
13919 case R_PPC64_ADDR14_BRNTAKEN:
13920 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
13921 insn |= bfd_get_32 (output_bfd,
13922 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 13923 /* Fall thru. */
86c76c7b 13924
b25116a9
AM
13925 case R_PPC64_REL14:
13926 max_br_offset = 1 << 15;
13927 /* Fall thru. */
5bd4f169 13928
65f38f15 13929 case R_PPC64_REL24:
ad8e1ba5
AM
13930 /* Calls to functions with a different TOC, such as calls to
13931 shared objects, need to alter the TOC pointer. This is
13932 done using a linkage stub. A REL24 branching to these
13933 linkage stubs needs to be followed by a nop, as the nop
13934 will be replaced with an instruction to restore the TOC
13935 base pointer. */
8387904d 13936 fdh = h;
b31867b6
AM
13937 if (h != NULL
13938 && h->oh != NULL
13939 && h->oh->is_func_descriptor)
13940 fdh = ppc_follow_link (h->oh);
31c76678
DK
13941 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13942 htab);
6abec6bc 13943 if (stub_entry != NULL
ad8e1ba5 13944 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 13945 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
13946 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13947 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 13948 {
b25116a9 13949 bfd_boolean can_plt_call = FALSE;
721956f4 13950
ba8ca3e7
AM
13951 /* All of these stubs will modify r2, so there must be a
13952 branch and link followed by a nop. The nop is
13953 replaced by an insn to restore r2. */
eea6121a 13954 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 13955 {
ba8ca3e7
AM
13956 unsigned long br;
13957
13958 br = bfd_get_32 (input_bfd,
13959 contents + rel->r_offset);
13960 if ((br & 1) != 0)
41bd81ab 13961 {
ba8ca3e7
AM
13962 unsigned long nop;
13963
13964 nop = bfd_get_32 (input_bfd,
13965 contents + rel->r_offset + 4);
13966 if (nop == NOP
13967 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 13968 {
ba8ca3e7
AM
13969 if (h != NULL
13970 && (h == htab->tls_get_addr_fd
13971 || h == htab->tls_get_addr)
7c9cf415 13972 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
13973 {
13974 /* Special stub used, leave nop alone. */
13975 }
13976 else
a078d95a
AM
13977 bfd_put_32 (input_bfd,
13978 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
13979 contents + rel->r_offset + 4);
13980 can_plt_call = TRUE;
a7f2871e 13981 }
41bd81ab 13982 }
5bd4f169 13983 }
721956f4 13984
ba8ca3e7 13985 if (!can_plt_call && h != NULL)
721956f4 13986 {
ba8ca3e7
AM
13987 const char *name = h->elf.root.root.string;
13988
13989 if (*name == '.')
13990 ++name;
13991
13992 if (strncmp (name, "__libc_start_main", 17) == 0
13993 && (name[17] == 0 || name[17] == '@'))
6ab189d5 13994 {
ba8ca3e7
AM
13995 /* Allow crt1 branch to go via a toc adjusting
13996 stub. Other calls that never return could do
13997 the same, if we could detect such. */
b25116a9 13998 can_plt_call = TRUE;
6ab189d5 13999 }
ba8ca3e7
AM
14000 }
14001
14002 if (!can_plt_call)
14003 {
14004 /* g++ as of 20130507 emits self-calls without a
14005 following nop. This is arguably wrong since we
14006 have conflicting information. On the one hand a
14007 global symbol and on the other a local call
14008 sequence, but don't error for this special case.
14009 It isn't possible to cheaply verify we have
14010 exactly such a call. Allow all calls to the same
14011 section. */
14012 asection *code_sec = sec;
14013
14014 if (get_opd_info (sec) != NULL)
ad8e1ba5 14015 {
ba8ca3e7
AM
14016 bfd_vma off = (relocation + addend
14017 - sec->output_section->vma
14018 - sec->output_offset);
bc30df16 14019
ba8ca3e7 14020 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14021 }
ba8ca3e7
AM
14022 if (code_sec == input_section)
14023 can_plt_call = TRUE;
14024 }
14025
14026 if (!can_plt_call)
14027 {
4805fc55
AM
14028 if (stub_entry->stub_type == ppc_stub_plt_call
14029 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14030 info->callbacks->einfo
14031 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14032 "recompile with -fPIC\n"),
14033 input_bfd, input_section, rel->r_offset, sym_name);
14034 else
14035 info->callbacks->einfo
14036 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14037 "(-mcmodel=small toc adjust stub)\n"),
14038 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14039
14040 bfd_set_error (bfd_error_bad_value);
14041 ret = FALSE;
721956f4
AM
14042 }
14043
b25116a9 14044 if (can_plt_call
794e51c0
AM
14045 && (stub_entry->stub_type == ppc_stub_plt_call
14046 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14047 unresolved_reloc = FALSE;
14048 }
14049
6abec6bc
AM
14050 if ((stub_entry == NULL
14051 || stub_entry->stub_type == ppc_stub_long_branch
14052 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14053 && get_opd_info (sec) != NULL)
14054 {
14055 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14056 bfd_vma off = (relocation + addend
14057 - sec->output_section->vma
14058 - sec->output_offset);
aef36ac1 14059 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14060 if (dest != (bfd_vma) -1)
14061 {
14062 relocation = dest;
14063 addend = 0;
bc30df16 14064 reloc_dest = DEST_OPD;
8387904d
AM
14065 }
14066 }
14067
b25116a9
AM
14068 /* If the branch is out of reach we ought to have a long
14069 branch stub. */
14070 from = (rel->r_offset
14071 + input_section->output_offset
14072 + input_section->output_section->vma);
14073
6911b7dc
AM
14074 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14075 ? fdh->elf.other
14076 : sym->st_other);
14077
6abec6bc
AM
14078 if (stub_entry != NULL
14079 && (stub_entry->stub_type == ppc_stub_long_branch
14080 || stub_entry->stub_type == ppc_stub_plt_branch)
14081 && (r_type == R_PPC64_ADDR14_BRTAKEN
14082 || r_type == R_PPC64_ADDR14_BRNTAKEN
14083 || (relocation + addend - from + max_br_offset
14084 < 2 * max_br_offset)))
14085 /* Don't use the stub if this branch is in range. */
14086 stub_entry = NULL;
b25116a9
AM
14087
14088 if (stub_entry != NULL)
14089 {
14090 /* Munge up the value and addend so that we call the stub
14091 rather than the procedure directly. */
a4b6fadd
AM
14092 asection *stub_sec = stub_entry->group->stub_sec;
14093
14094 if (stub_entry->stub_type == ppc_stub_save_res)
14095 relocation += (stub_sec->output_offset
14096 + stub_sec->output_section->vma
14097 + stub_sec->size - htab->sfpr->size
14098 - htab->sfpr->output_offset
14099 - htab->sfpr->output_section->vma);
14100 else
14101 relocation = (stub_entry->stub_offset
14102 + stub_sec->output_offset
14103 + stub_sec->output_section->vma);
b25116a9 14104 addend = 0;
bc30df16 14105 reloc_dest = DEST_STUB;
3b421ab3 14106
794e51c0
AM
14107 if ((stub_entry->stub_type == ppc_stub_plt_call
14108 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14109 && (ALWAYS_EMIT_R2SAVE
14110 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14111 && rel + 1 < relend
14112 && rel[1].r_offset == rel->r_offset + 4
14113 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14114 relocation += 4;
b25116a9
AM
14115 }
14116
14117 if (insn != 0)
14118 {
794e51c0 14119 if (is_isa_v2)
721956f4 14120 {
b25116a9
AM
14121 /* Set 'a' bit. This is 0b00010 in BO field for branch
14122 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14123 for branch on CTR insns (BO == 1a00t or 1a01t). */
14124 if ((insn & (0x14 << 21)) == (0x04 << 21))
14125 insn |= 0x02 << 21;
14126 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14127 insn |= 0x08 << 21;
14128 else
14129 break;
14130 }
14131 else
14132 {
14133 /* Invert 'y' bit if not the default. */
4cc603a5 14134 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14135 insn ^= 0x01 << 21;
721956f4 14136 }
b25116a9
AM
14137
14138 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 14139 }
e86ce104 14140
06da1e8e
AM
14141 /* NOP out calls to undefined weak functions.
14142 We can thus call a weak function without first
14143 checking whether the function is defined. */
b25116a9 14144 else if (h != NULL
039b3fef 14145 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14146 && h->elf.dynindx == -1
b25116a9
AM
14147 && r_type == R_PPC64_REL24
14148 && relocation == 0
4cc603a5 14149 && addend == 0)
e86ce104 14150 {
06da1e8e 14151 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
c316a17c 14152 goto copy_reloc;
e86ce104 14153 }
65f38f15
AM
14154 break;
14155 }
5bd4f169 14156
65f38f15 14157 /* Set `addend'. */
411e1bfb 14158 tls_type = 0;
65f38f15
AM
14159 switch (r_type)
14160 {
14161 default:
25f53a85 14162 info->callbacks->einfo
bc30df16 14163 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14164 input_bfd, (int) r_type, sym_name);
5bd4f169 14165
65f38f15 14166 bfd_set_error (bfd_error_bad_value);
b34976b6 14167 ret = FALSE;
c316a17c 14168 goto copy_reloc;
5bd4f169 14169
65f38f15 14170 case R_PPC64_NONE:
411e1bfb 14171 case R_PPC64_TLS:
727fc41e
AM
14172 case R_PPC64_TLSGD:
14173 case R_PPC64_TLSLD:
3b421ab3 14174 case R_PPC64_TOCSAVE:
04c9666a
AM
14175 case R_PPC64_GNU_VTINHERIT:
14176 case R_PPC64_GNU_VTENTRY:
c316a17c 14177 goto copy_reloc;
5bd4f169
AM
14178
14179 /* GOT16 relocations. Like an ADDR16 using the symbol's
14180 address in the GOT as relocation value instead of the
411e1bfb 14181 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14182 symbol and put the symbol value there. */
411e1bfb
AM
14183 case R_PPC64_GOT_TLSGD16:
14184 case R_PPC64_GOT_TLSGD16_LO:
14185 case R_PPC64_GOT_TLSGD16_HI:
14186 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14187 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14188 goto dogot;
14189
14190 case R_PPC64_GOT_TLSLD16:
14191 case R_PPC64_GOT_TLSLD16_LO:
14192 case R_PPC64_GOT_TLSLD16_HI:
14193 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14194 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14195 goto dogot;
14196
14197 case R_PPC64_GOT_TPREL16_DS:
14198 case R_PPC64_GOT_TPREL16_LO_DS:
14199 case R_PPC64_GOT_TPREL16_HI:
14200 case R_PPC64_GOT_TPREL16_HA:
14201 tls_type = TLS_TLS | TLS_TPREL;
14202 goto dogot;
14203
14204 case R_PPC64_GOT_DTPREL16_DS:
14205 case R_PPC64_GOT_DTPREL16_LO_DS:
14206 case R_PPC64_GOT_DTPREL16_HI:
14207 case R_PPC64_GOT_DTPREL16_HA:
14208 tls_type = TLS_TLS | TLS_DTPREL;
14209 goto dogot;
14210
65f38f15
AM
14211 case R_PPC64_GOT16:
14212 case R_PPC64_GOT16_LO:
14213 case R_PPC64_GOT16_HI:
14214 case R_PPC64_GOT16_HA:
14215 case R_PPC64_GOT16_DS:
14216 case R_PPC64_GOT16_LO_DS:
411e1bfb 14217 dogot:
5bd4f169
AM
14218 {
14219 /* Relocation is to the entry for this symbol in the global
14220 offset table. */
e717da7e 14221 asection *got;
d881513a 14222 bfd_vma *offp;
5bd4f169 14223 bfd_vma off;
d881513a 14224 unsigned long indx = 0;
927be08e 14225 struct got_entry *ent;
65f38f15 14226
d881513a
AM
14227 if (tls_type == (TLS_TLS | TLS_LD)
14228 && (h == NULL
f5385ebf 14229 || !h->elf.def_dynamic))
927be08e 14230 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14231 else
5bd4f169 14232 {
5bd4f169 14233
d881513a
AM
14234 if (h != NULL)
14235 {
14236 bfd_boolean dyn = htab->elf.dynamic_sections_created;
0e1862bb 14237 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
039b3fef 14238 &h->elf)
0e1862bb 14239 || (bfd_link_pic (info)
afe397ea 14240 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
14241 /* This is actually a static link, or it is a
14242 -Bsymbolic link and the symbol is defined
14243 locally, or the symbol was forced to be local
14244 because of a version file. */
14245 ;
14246 else
14247 {
14acf4dc 14248 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 14249 indx = h->elf.dynindx;
d881513a
AM
14250 unresolved_reloc = FALSE;
14251 }
039b3fef 14252 ent = h->elf.got.glist;
d881513a 14253 }
411e1bfb 14254 else
5bd4f169 14255 {
d881513a
AM
14256 if (local_got_ents == NULL)
14257 abort ();
14258 ent = local_got_ents[r_symndx];
5bd4f169 14259 }
d881513a
AM
14260
14261 for (; ent != NULL; ent = ent->next)
31c76678 14262 if (ent->addend == orig_rel.r_addend
e717da7e 14263 && ent->owner == input_bfd
d881513a
AM
14264 && ent->tls_type == tls_type)
14265 break;
5bd4f169 14266 }
411e1bfb 14267
927be08e
AM
14268 if (ent == NULL)
14269 abort ();
14270 if (ent->is_indirect)
14271 ent = ent->got.ent;
14272 offp = &ent->got.offset;
14273 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14274 if (got == NULL)
14275 abort ();
14276
411e1bfb
AM
14277 /* The offset must always be a multiple of 8. We use the
14278 least significant bit to record whether we have already
14279 processed this entry. */
d881513a 14280 off = *offp;
411e1bfb
AM
14281 if ((off & 1) != 0)
14282 off &= ~1;
5bd4f169
AM
14283 else
14284 {
411e1bfb
AM
14285 /* Generate relocs for the dynamic linker, except in
14286 the case of TLSLD where we'll use one entry per
14287 module. */
25f23106
AM
14288 asection *relgot;
14289 bfd_boolean ifunc;
e717da7e 14290
d881513a 14291 *offp = off | 1;
25f23106
AM
14292 relgot = NULL;
14293 ifunc = (h != NULL
14294 ? h->elf.type == STT_GNU_IFUNC
14295 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14296 if (ifunc)
33e44f2e 14297 relgot = htab->elf.irelplt;
0e1862bb 14298 else if ((bfd_link_pic (info) || indx != 0)
19e08130
AM
14299 && (h == NULL
14300 || (tls_type == (TLS_TLS | TLS_LD)
14301 && !h->elf.def_dynamic)
14302 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14303 || h->elf.root.type != bfd_link_hash_undefweak))
14304 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14305 if (relgot != NULL)
5bd4f169 14306 {
e717da7e
AM
14307 outrel.r_offset = (got->output_section->vma
14308 + got->output_offset
411e1bfb 14309 + off);
4cc603a5 14310 outrel.r_addend = addend;
d881513a 14311 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14312 {
411e1bfb 14313 outrel.r_addend = 0;
e515b051 14314 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14315 if (tls_type == (TLS_TLS | TLS_GD))
14316 {
e717da7e
AM
14317 loc = relgot->contents;
14318 loc += (relgot->reloc_count++
d881513a
AM
14319 * sizeof (Elf64_External_Rela));
14320 bfd_elf64_swap_reloca_out (output_bfd,
14321 &outrel, loc);
e515b051 14322 outrel.r_offset += 8;
4cc603a5 14323 outrel.r_addend = addend;
d881513a
AM
14324 outrel.r_info
14325 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14326 }
411e1bfb 14327 }
951fd09b 14328 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14329 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14330 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14331 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14332 else if (indx != 0)
14333 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14334 else
81407a69 14335 {
25f23106
AM
14336 if (ifunc)
14337 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14338 else
14339 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14340
14341 /* Write the .got section contents for the sake
14342 of prelink. */
e717da7e 14343 loc = got->contents + off;
23fbd6fa
JJ
14344 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14345 loc);
81407a69 14346 }
81407a69
AM
14347
14348 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14349 {
14350 outrel.r_addend += relocation;
14351 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14352 {
14353 if (htab->elf.tls_sec == NULL)
14354 outrel.r_addend = 0;
14355 else
14356 outrel.r_addend -= htab->elf.tls_sec->vma;
14357 }
e515b051 14358 }
e717da7e
AM
14359 loc = relgot->contents;
14360 loc += (relgot->reloc_count++
411e1bfb
AM
14361 * sizeof (Elf64_External_Rela));
14362 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14363 }
14364
ad8e1ba5 14365 /* Init the .got section contents here if we're not
81407a69 14366 emitting a reloc. */
d881513a 14367 else
411e1bfb 14368 {
4cc603a5 14369 relocation += addend;
7b609f53
AM
14370 if (tls_type == (TLS_TLS | TLS_LD))
14371 relocation = 1;
14372 else if (tls_type != 0)
411e1bfb 14373 {
989f9879
AM
14374 if (htab->elf.tls_sec == NULL)
14375 relocation = 0;
14376 else
14377 {
14378 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14379 if (tls_type == (TLS_TLS | TLS_TPREL))
14380 relocation += DTP_OFFSET - TP_OFFSET;
14381 }
5bd4f169 14382
7b609f53
AM
14383 if (tls_type == (TLS_TLS | TLS_GD))
14384 {
14385 bfd_put_64 (output_bfd, relocation,
e717da7e 14386 got->contents + off + 8);
7b609f53
AM
14387 relocation = 1;
14388 }
411e1bfb 14389 }
7b609f53 14390
411e1bfb 14391 bfd_put_64 (output_bfd, relocation,
e717da7e 14392 got->contents + off);
5bd4f169
AM
14393 }
14394 }
14395
65f38f15
AM
14396 if (off >= (bfd_vma) -2)
14397 abort ();
14398
bf102f86 14399 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14400 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14401 }
65f38f15
AM
14402 break;
14403
14404 case R_PPC64_PLT16_HA:
14405 case R_PPC64_PLT16_HI:
14406 case R_PPC64_PLT16_LO:
14407 case R_PPC64_PLT32:
14408 case R_PPC64_PLT64:
14409 /* Relocation is to the entry for this symbol in the
14410 procedure linkage table. */
14411
14412 /* Resolve a PLT reloc against a local symbol directly,
14413 without using the procedure linkage table. */
14414 if (h == NULL)
14415 break;
14416
411e1bfb
AM
14417 /* It's possible that we didn't make a PLT entry for this
14418 symbol. This happens when statically linking PIC code,
14419 or when using -Bsymbolic. Go find a match if there is a
14420 PLT entry. */
33e44f2e 14421 if (htab->elf.splt != NULL)
65f38f15 14422 {
411e1bfb 14423 struct plt_entry *ent;
039b3fef 14424 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
a345bc8d
AM
14425 if (ent->plt.offset != (bfd_vma) -1
14426 && ent->addend == orig_rel.r_addend)
411e1bfb 14427 {
33e44f2e
AM
14428 relocation = (htab->elf.splt->output_section->vma
14429 + htab->elf.splt->output_offset
411e1bfb
AM
14430 + ent->plt.offset);
14431 unresolved_reloc = FALSE;
a345bc8d 14432 break;
411e1bfb 14433 }
65f38f15 14434 }
65f38f15 14435 break;
5bd4f169 14436
0b13192e
AM
14437 case R_PPC64_TOC:
14438 /* Relocation value is TOC base. */
14439 relocation = TOCstart;
cf35638d 14440 if (r_symndx == STN_UNDEF)
6f20ed8a 14441 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14442 else if (unresolved_reloc)
14443 ;
6f20ed8a
AM
14444 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14445 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14446 else
14447 unresolved_reloc = TRUE;
ab96bf03 14448 goto dodyn;
0b13192e 14449
5bd4f169
AM
14450 /* TOC16 relocs. We want the offset relative to the TOC base,
14451 which is the address of the start of the TOC plus 0x8000.
14452 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14453 in this order. */
65f38f15
AM
14454 case R_PPC64_TOC16:
14455 case R_PPC64_TOC16_LO:
14456 case R_PPC64_TOC16_HI:
14457 case R_PPC64_TOC16_DS:
14458 case R_PPC64_TOC16_LO_DS:
14459 case R_PPC64_TOC16_HA:
6f20ed8a 14460 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14461 break;
14462
14463 /* Relocate against the beginning of the section. */
65f38f15
AM
14464 case R_PPC64_SECTOFF:
14465 case R_PPC64_SECTOFF_LO:
14466 case R_PPC64_SECTOFF_HI:
14467 case R_PPC64_SECTOFF_DS:
14468 case R_PPC64_SECTOFF_LO_DS:
14469 case R_PPC64_SECTOFF_HA:
4ce794b7 14470 if (sec != NULL)
65f38f15 14471 addend -= sec->output_section->vma;
5bd4f169
AM
14472 break;
14473
25f23106
AM
14474 case R_PPC64_REL16:
14475 case R_PPC64_REL16_LO:
14476 case R_PPC64_REL16_HI:
14477 case R_PPC64_REL16_HA:
a680de9a 14478 case R_PPC64_REL16DX_HA:
25f23106
AM
14479 break;
14480
721956f4
AM
14481 case R_PPC64_REL14:
14482 case R_PPC64_REL14_BRNTAKEN:
14483 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14484 case R_PPC64_REL24:
14485 break;
14486
411e1bfb
AM
14487 case R_PPC64_TPREL16:
14488 case R_PPC64_TPREL16_LO:
14489 case R_PPC64_TPREL16_HI:
14490 case R_PPC64_TPREL16_HA:
14491 case R_PPC64_TPREL16_DS:
14492 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14493 case R_PPC64_TPREL16_HIGH:
14494 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14495 case R_PPC64_TPREL16_HIGHER:
14496 case R_PPC64_TPREL16_HIGHERA:
14497 case R_PPC64_TPREL16_HIGHEST:
14498 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14499 if (h != NULL
14500 && h->elf.root.type == bfd_link_hash_undefweak
14501 && h->elf.dynindx == -1)
14502 {
14503 /* Make this relocation against an undefined weak symbol
14504 resolve to zero. This is really just a tweak, since
14505 code using weak externs ought to check that they are
14506 defined before using them. */
14507 bfd_byte *p = contents + rel->r_offset - d_offset;
14508
14509 insn = bfd_get_32 (output_bfd, p);
14510 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14511 if (insn != 0)
14512 bfd_put_32 (output_bfd, insn, p);
14513 break;
14514 }
989f9879
AM
14515 if (htab->elf.tls_sec != NULL)
14516 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
0e1862bb 14517 if (bfd_link_pic (info))
411e1bfb
AM
14518 /* The TPREL16 relocs shouldn't really be used in shared
14519 libs as they will result in DT_TEXTREL being set, but
14520 support them anyway. */
14521 goto dodyn;
14522 break;
14523
14524 case R_PPC64_DTPREL16:
14525 case R_PPC64_DTPREL16_LO:
14526 case R_PPC64_DTPREL16_HI:
14527 case R_PPC64_DTPREL16_HA:
14528 case R_PPC64_DTPREL16_DS:
14529 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14530 case R_PPC64_DTPREL16_HIGH:
14531 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14532 case R_PPC64_DTPREL16_HIGHER:
14533 case R_PPC64_DTPREL16_HIGHERA:
14534 case R_PPC64_DTPREL16_HIGHEST:
14535 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14536 if (htab->elf.tls_sec != NULL)
14537 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14538 break;
14539
45965137
AM
14540 case R_PPC64_ADDR64_LOCAL:
14541 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14542 ? h->elf.other
14543 : sym->st_other);
14544 break;
14545
e515b051
AM
14546 case R_PPC64_DTPMOD64:
14547 relocation = 1;
14548 addend = 0;
14549 goto dodyn;
14550
411e1bfb 14551 case R_PPC64_TPREL64:
989f9879
AM
14552 if (htab->elf.tls_sec != NULL)
14553 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14554 goto dodyn;
14555
14556 case R_PPC64_DTPREL64:
989f9879
AM
14557 if (htab->elf.tls_sec != NULL)
14558 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14559 /* Fall thru */
14560
65f38f15
AM
14561 /* Relocations that may need to be propagated if this is a
14562 dynamic object. */
04c9666a 14563 case R_PPC64_REL30:
65f38f15
AM
14564 case R_PPC64_REL32:
14565 case R_PPC64_REL64:
14566 case R_PPC64_ADDR14:
14567 case R_PPC64_ADDR14_BRNTAKEN:
14568 case R_PPC64_ADDR14_BRTAKEN:
14569 case R_PPC64_ADDR16:
14570 case R_PPC64_ADDR16_DS:
14571 case R_PPC64_ADDR16_HA:
14572 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14573 case R_PPC64_ADDR16_HIGH:
14574 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14575 case R_PPC64_ADDR16_HIGHER:
14576 case R_PPC64_ADDR16_HIGHERA:
14577 case R_PPC64_ADDR16_HIGHEST:
14578 case R_PPC64_ADDR16_HIGHESTA:
14579 case R_PPC64_ADDR16_LO:
14580 case R_PPC64_ADDR16_LO_DS:
14581 case R_PPC64_ADDR24:
65f38f15
AM
14582 case R_PPC64_ADDR32:
14583 case R_PPC64_ADDR64:
14584 case R_PPC64_UADDR16:
14585 case R_PPC64_UADDR32:
14586 case R_PPC64_UADDR64:
411e1bfb 14587 dodyn:
5d1634d7 14588 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14589 break;
14590
41bd81ab
AM
14591 if (NO_OPD_RELOCS && is_opd)
14592 break;
14593
0e1862bb 14594 if ((bfd_link_pic (info)
4e795f50 14595 && (h == NULL
039b3fef
AM
14596 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14597 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 14598 && (must_be_dyn_reloc (info, r_type)
039b3fef 14599 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909 14600 || (ELIMINATE_COPY_RELOCS
0e1862bb 14601 && !bfd_link_pic (info)
65f38f15 14602 && h != NULL
039b3fef 14603 && h->elf.dynindx != -1
f5385ebf 14604 && !h->elf.non_got_ref
25f23106 14605 && !h->elf.def_regular)
0e1862bb 14606 || (!bfd_link_pic (info)
25f23106
AM
14607 && (h != NULL
14608 ? h->elf.type == STT_GNU_IFUNC
14609 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 14610 {
b34976b6 14611 bfd_boolean skip, relocate;
65f38f15 14612 asection *sreloc;
1cf1f670 14613 bfd_vma out_off;
65f38f15
AM
14614
14615 /* When generating a dynamic object, these relocations
14616 are copied into the output file to be resolved at run
14617 time. */
14618
b34976b6
AM
14619 skip = FALSE;
14620 relocate = FALSE;
65f38f15 14621
1cf1f670
AM
14622 out_off = _bfd_elf_section_offset (output_bfd, info,
14623 input_section, rel->r_offset);
14624 if (out_off == (bfd_vma) -1)
b34976b6 14625 skip = TRUE;
1cf1f670 14626 else if (out_off == (bfd_vma) -2)
b34976b6 14627 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14628 out_off += (input_section->output_section->vma
14629 + input_section->output_offset);
14630 outrel.r_offset = out_off;
411e1bfb 14631 outrel.r_addend = rel->r_addend;
65f38f15 14632
1cf1f670
AM
14633 /* Optimize unaligned reloc use. */
14634 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14635 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14636 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14637 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14638 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14639 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14640 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14641 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14642 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14643
65f38f15 14644 if (skip)
0bb2d96a 14645 memset (&outrel, 0, sizeof outrel);
afe397ea 14646 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14647 && !is_opd
14648 && r_type != R_PPC64_TOC)
14acf4dc
MR
14649 {
14650 BFD_ASSERT (h->elf.dynindx != -1);
14651 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14652 }
65f38f15
AM
14653 else
14654 {
41bd81ab
AM
14655 /* This symbol is local, or marked to become local,
14656 or this is an opd section reloc which must point
14657 at a local function. */
65f38f15 14658 outrel.r_addend += relocation;
e86ce104 14659 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14660 {
3fad3c7c 14661 if (is_opd && h != NULL)
afbe61cf
AM
14662 {
14663 /* Lie about opd entries. This case occurs
14664 when building shared libraries and we
14665 reference a function in another shared
3fad3c7c
AM
14666 lib. The same thing happens for a weak
14667 definition in an application that's
14668 overridden by a strong definition in a
14669 shared lib. (I believe this is a generic
14670 bug in binutils handling of weak syms.)
14671 In these cases we won't use the opd
1e2f5b6e 14672 entry in this lib. */
b34976b6 14673 unresolved_reloc = FALSE;
afbe61cf 14674 }
25f23106
AM
14675 if (!is_opd
14676 && r_type == R_PPC64_ADDR64
14677 && (h != NULL
14678 ? h->elf.type == STT_GNU_IFUNC
14679 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14680 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14681 else
14682 {
14683 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14684
25f23106
AM
14685 /* We need to relocate .opd contents for ld.so.
14686 Prelink also wants simple and consistent rules
14687 for relocs. This make all RELATIVE relocs have
14688 *r_offset equal to r_addend. */
14689 relocate = TRUE;
14690 }
65f38f15
AM
14691 }
14692 else
14693 {
14694 long indx = 0;
14695
25f23106
AM
14696 if (h != NULL
14697 ? h->elf.type == STT_GNU_IFUNC
14698 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14699 {
25f53a85 14700 info->callbacks->einfo
bc30df16
AM
14701 (_("%P: %H: %s for indirect "
14702 "function `%T' unsupported\n"),
25f53a85 14703 input_bfd, input_section, rel->r_offset,
25f23106
AM
14704 ppc64_elf_howto_table[r_type]->name,
14705 sym_name);
14706 ret = FALSE;
14707 }
cf35638d 14708 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14709 ;
14710 else if (sec == NULL || sec->owner == NULL)
14711 {
14712 bfd_set_error (bfd_error_bad_value);
b34976b6 14713 return FALSE;
65f38f15
AM
14714 }
14715 else
14716 {
14717 asection *osec;
14718
14719 osec = sec->output_section;
14720 indx = elf_section_data (osec)->dynindx;
14721
74541ad4
AM
14722 if (indx == 0)
14723 {
14724 if ((osec->flags & SEC_READONLY) == 0
14725 && htab->elf.data_index_section != NULL)
14726 osec = htab->elf.data_index_section;
14727 else
14728 osec = htab->elf.text_index_section;
14729 indx = elf_section_data (osec)->dynindx;
14730 }
14731 BFD_ASSERT (indx != 0);
14732
65f38f15
AM
14733 /* We are turning this relocation into one
14734 against a section symbol, so subtract out
14735 the output section's address but not the
14736 offset of the input section in the output
14737 section. */
14738 outrel.r_addend -= osec->vma;
14739 }
14740
14741 outrel.r_info = ELF64_R_INFO (indx, r_type);
14742 }
14743 }
14744
14745 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14746 if (h != NULL
14747 ? h->elf.type == STT_GNU_IFUNC
14748 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
33e44f2e 14749 sreloc = htab->elf.irelplt;
65f38f15
AM
14750 if (sreloc == NULL)
14751 abort ();
14752
dfbb6ac9
AM
14753 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14754 >= sreloc->size)
14755 abort ();
947216bf
AM
14756 loc = sreloc->contents;
14757 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14758 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14759
14760 /* If this reloc is against an external symbol, it will
14761 be computed at runtime, so there's no need to do
81407a69
AM
14762 anything now. However, for the sake of prelink ensure
14763 that the section contents are a known value. */
65f38f15 14764 if (! relocate)
81407a69
AM
14765 {
14766 unresolved_reloc = FALSE;
14767 /* The value chosen here is quite arbitrary as ld.so
14768 ignores section contents except for the special
14769 case of .opd where the contents might be accessed
14770 before relocation. Choose zero, as that won't
14771 cause reloc overflow. */
14772 relocation = 0;
14773 addend = 0;
14774 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14775 to improve backward compatibility with older
14776 versions of ld. */
14777 if (r_type == R_PPC64_ADDR64)
14778 addend = outrel.r_addend;
14779 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14780 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14781 addend = (input_section->output_section->vma
14782 + input_section->output_offset
14783 + rel->r_offset);
81407a69 14784 }
65f38f15 14785 }
5bd4f169
AM
14786 break;
14787
65f38f15
AM
14788 case R_PPC64_COPY:
14789 case R_PPC64_GLOB_DAT:
14790 case R_PPC64_JMP_SLOT:
25f23106 14791 case R_PPC64_JMP_IREL:
65f38f15
AM
14792 case R_PPC64_RELATIVE:
14793 /* We shouldn't ever see these dynamic relocs in relocatable
14794 files. */
ae9a127f 14795 /* Fall through. */
65f38f15
AM
14796
14797 case R_PPC64_PLTGOT16:
14798 case R_PPC64_PLTGOT16_DS:
14799 case R_PPC64_PLTGOT16_HA:
14800 case R_PPC64_PLTGOT16_HI:
14801 case R_PPC64_PLTGOT16_LO:
14802 case R_PPC64_PLTGOT16_LO_DS:
14803 case R_PPC64_PLTREL32:
14804 case R_PPC64_PLTREL64:
14805 /* These ones haven't been implemented yet. */
14806
25f53a85 14807 info->callbacks->einfo
bc30df16 14808 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14809 input_bfd,
4ce794b7 14810 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14811
14812 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14813 ret = FALSE;
c316a17c 14814 goto copy_reloc;
65f38f15 14815 }
5bd4f169 14816
67f0cbdb
AM
14817 /* Multi-instruction sequences that access the TOC can be
14818 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14819 to nop; addi rb,r2,x; */
14820 switch (r_type)
14821 {
14822 default:
14823 break;
14824
14825 case R_PPC64_GOT_TLSLD16_HI:
14826 case R_PPC64_GOT_TLSGD16_HI:
14827 case R_PPC64_GOT_TPREL16_HI:
14828 case R_PPC64_GOT_DTPREL16_HI:
14829 case R_PPC64_GOT16_HI:
14830 case R_PPC64_TOC16_HI:
14831 /* These relocs would only be useful if building up an
14832 offset to later add to r2, perhaps in an indexed
14833 addressing mode instruction. Don't try to optimize.
14834 Unfortunately, the possibility of someone building up an
14835 offset like this or even with the HA relocs, means that
14836 we need to check the high insn when optimizing the low
14837 insn. */
14838 break;
14839
14840 case R_PPC64_GOT_TLSLD16_HA:
14841 case R_PPC64_GOT_TLSGD16_HA:
14842 case R_PPC64_GOT_TPREL16_HA:
14843 case R_PPC64_GOT_DTPREL16_HA:
14844 case R_PPC64_GOT16_HA:
14845 case R_PPC64_TOC16_HA:
98528052 14846 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14847 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
14848 {
14849 bfd_byte *p = contents + (rel->r_offset & ~3);
14850 bfd_put_32 (input_bfd, NOP, p);
14851 }
67f0cbdb
AM
14852 break;
14853
14854 case R_PPC64_GOT_TLSLD16_LO:
14855 case R_PPC64_GOT_TLSGD16_LO:
14856 case R_PPC64_GOT_TPREL16_LO_DS:
14857 case R_PPC64_GOT_DTPREL16_LO_DS:
14858 case R_PPC64_GOT16_LO:
14859 case R_PPC64_GOT16_LO_DS:
14860 case R_PPC64_TOC16_LO:
14861 case R_PPC64_TOC16_LO_DS:
98528052 14862 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14863 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
14864 {
14865 bfd_byte *p = contents + (rel->r_offset & ~3);
14866 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
14867 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14868 {
14869 /* Transform addic to addi when we change reg. */
14870 insn &= ~((0x3f << 26) | (0x1f << 16));
14871 insn |= (14u << 26) | (2 << 16);
14872 }
14873 else
67f0cbdb 14874 {
98528052
AM
14875 insn &= ~(0x1f << 16);
14876 insn |= 2 << 16;
67f0cbdb 14877 }
560c8763 14878 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
14879 }
14880 break;
14881 }
14882
65f38f15 14883 /* Do any further special processing. */
b80eed39 14884 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
14885 switch (r_type)
14886 {
14887 default:
14888 break;
14889
25f23106 14890 case R_PPC64_REL16_HA:
a680de9a 14891 case R_PPC64_REL16DX_HA:
f9c6b907
AM
14892 case R_PPC64_ADDR16_HA:
14893 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14894 case R_PPC64_ADDR16_HIGHERA:
14895 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
14896 case R_PPC64_TOC16_HA:
14897 case R_PPC64_SECTOFF_HA:
411e1bfb 14898 case R_PPC64_TPREL16_HA:
f9c6b907 14899 case R_PPC64_TPREL16_HIGHA:
411e1bfb 14900 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 14901 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
14902 case R_PPC64_DTPREL16_HA:
14903 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 14904 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 14905 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
14906 /* It's just possible that this symbol is a weak symbol
14907 that's not actually defined anywhere. In that case,
14908 'sec' would be NULL, and we should leave the symbol
14909 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
14910 if (sec == NULL)
14911 break;
14912 /* Fall thru */
14913
14914 case R_PPC64_GOT16_HA:
14915 case R_PPC64_PLTGOT16_HA:
14916 case R_PPC64_PLT16_HA:
14917 case R_PPC64_GOT_TLSGD16_HA:
14918 case R_PPC64_GOT_TLSLD16_HA:
14919 case R_PPC64_GOT_TPREL16_HA:
14920 case R_PPC64_GOT_DTPREL16_HA:
14921 /* Add 0x10000 if sign bit in 0:15 is set.
14922 Bits 0:15 are not used. */
14923 addend += 0x8000;
65f38f15
AM
14924 break;
14925
14926 case R_PPC64_ADDR16_DS:
14927 case R_PPC64_ADDR16_LO_DS:
14928 case R_PPC64_GOT16_DS:
14929 case R_PPC64_GOT16_LO_DS:
14930 case R_PPC64_PLT16_LO_DS:
14931 case R_PPC64_SECTOFF_DS:
14932 case R_PPC64_SECTOFF_LO_DS:
14933 case R_PPC64_TOC16_DS:
14934 case R_PPC64_TOC16_LO_DS:
14935 case R_PPC64_PLTGOT16_DS:
14936 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
14937 case R_PPC64_GOT_TPREL16_DS:
14938 case R_PPC64_GOT_TPREL16_LO_DS:
14939 case R_PPC64_GOT_DTPREL16_DS:
14940 case R_PPC64_GOT_DTPREL16_LO_DS:
14941 case R_PPC64_TPREL16_DS:
14942 case R_PPC64_TPREL16_LO_DS:
14943 case R_PPC64_DTPREL16_DS:
14944 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
14945 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14946 mask = 3;
a680de9a
PB
14947 /* If this reloc is against an lq, lxv, or stxv insn, then
14948 the value must be a multiple of 16. This is somewhat of
14949 a hack, but the "correct" way to do this by defining _DQ
14950 forms of all the _DS relocs bloats all reloc switches in
14951 this file. It doesn't make much sense to use these
14952 relocs in data, so testing the insn should be safe. */
14953 if ((insn & (0x3f << 26)) == (56u << 26)
14954 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 14955 mask = 15;
a680de9a
PB
14956 relocation += addend;
14957 addend = insn & (mask ^ 3);
14958 if ((relocation & mask) != 0)
65f38f15 14959 {
a680de9a 14960 relocation ^= relocation & mask;
25f53a85 14961 info->callbacks->einfo
8de848d8 14962 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 14963 input_bfd, input_section, rel->r_offset,
b80eed39 14964 howto->name,
adadcc0c 14965 mask + 1);
65f38f15 14966 bfd_set_error (bfd_error_bad_value);
b34976b6 14967 ret = FALSE;
c316a17c 14968 goto copy_reloc;
65f38f15
AM
14969 }
14970 break;
5bd4f169
AM
14971 }
14972
239e1f3a
AM
14973 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14974 because such sections are not SEC_ALLOC and thus ld.so will
14975 not process them. */
65f38f15 14976 if (unresolved_reloc
239e1f3a 14977 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
14978 && h->elf.def_dynamic)
14979 && _bfd_elf_section_offset (output_bfd, info, input_section,
14980 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 14981 {
25f53a85 14982 info->callbacks->einfo
bc30df16 14983 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 14984 input_bfd, input_section, rel->r_offset,
b80eed39 14985 howto->name,
039b3fef 14986 h->elf.root.root.string);
b34976b6 14987 ret = FALSE;
9c07fe7c 14988 }
5bd4f169 14989
b80eed39
AM
14990 /* 16-bit fields in insns mostly have signed values, but a
14991 few insns have 16-bit unsigned values. Really, we should
14992 have different reloc types. */
14993 if (howto->complain_on_overflow != complain_overflow_dont
14994 && howto->dst_mask == 0xffff
14995 && (input_section->flags & SEC_CODE) != 0)
14996 {
14997 enum complain_overflow complain = complain_overflow_signed;
14998
14999 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15000 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15001 complain = complain_overflow_bitfield;
15002 else if (howto->rightshift == 0
15003 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15004 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15005 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15006 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15007 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15008 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15009 complain = complain_overflow_unsigned;
15010 if (howto->complain_on_overflow != complain)
15011 {
15012 alt_howto = *howto;
15013 alt_howto.complain_on_overflow = complain;
15014 howto = &alt_howto;
15015 }
15016 }
15017
a680de9a
PB
15018 if (r_type == R_PPC64_REL16DX_HA)
15019 {
15020 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15021 if (rel->r_offset + 4 > input_section->size)
15022 r = bfd_reloc_outofrange;
15023 else
15024 {
15025 relocation += addend;
15026 relocation -= (rel->r_offset
15027 + input_section->output_offset
15028 + input_section->output_section->vma);
15029 relocation = (bfd_signed_vma) relocation >> 16;
15030 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15031 insn &= ~0x1fffc1;
15032 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15033 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15034 r = bfd_reloc_ok;
15035 if (relocation + 0x8000 > 0xffff)
15036 r = bfd_reloc_overflow;
15037 }
15038 }
15039 else
15040 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15041 rel->r_offset, relocation, addend);
5bd4f169 15042
ef60b7ff 15043 if (r != bfd_reloc_ok)
5bd4f169 15044 {
bc30df16 15045 char *more_info = NULL;
b80eed39 15046 const char *reloc_name = howto->name;
bc30df16
AM
15047
15048 if (reloc_dest != DEST_NORMAL)
15049 {
15050 more_info = bfd_malloc (strlen (reloc_name) + 8);
15051 if (more_info != NULL)
15052 {
15053 strcpy (more_info, reloc_name);
15054 strcat (more_info, (reloc_dest == DEST_OPD
15055 ? " (OPD)" : " (stub)"));
15056 reloc_name = more_info;
15057 }
15058 }
15059
cd27b276 15060 if (r == bfd_reloc_overflow)
5bd4f169 15061 {
8131c122
AM
15062 /* On code like "if (foo) foo();" don't report overflow
15063 on a branch to zero when foo is undefined. */
15064 if (!warned
15065 && (reloc_dest == DEST_STUB
15066 || !(h != NULL
15067 && (h->elf.root.type == bfd_link_hash_undefweak
15068 || h->elf.root.type == bfd_link_hash_undefined)
15069 && is_branch_reloc (r_type))))
5bd4f169 15070 {
8131c122
AM
15071 if (!((*info->callbacks->reloc_overflow)
15072 (info, &h->elf.root, sym_name,
15073 reloc_name, orig_rel.r_addend,
15074 input_bfd, input_section, rel->r_offset)))
15075 return FALSE;
5bd4f169 15076 }
ef60b7ff
AM
15077 }
15078 else
15079 {
25f53a85 15080 info->callbacks->einfo
bc30df16 15081 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 15082 input_bfd, input_section, rel->r_offset,
bc30df16 15083 reloc_name, sym_name, (int) r);
b34976b6 15084 ret = FALSE;
ef60b7ff 15085 }
bc30df16
AM
15086 if (more_info != NULL)
15087 free (more_info);
5bd4f169 15088 }
c316a17c
AM
15089 copy_reloc:
15090 if (wrel != rel)
15091 *wrel = *rel;
15092 }
15093
15094 if (wrel != rel)
15095 {
15096 Elf_Internal_Shdr *rel_hdr;
15097 size_t deleted = rel - wrel;
15098
15099 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15100 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15101 if (rel_hdr->sh_size == 0)
15102 {
15103 /* It is too late to remove an empty reloc section. Leave
15104 one NONE reloc.
15105 ??? What is wrong with an empty section??? */
15106 rel_hdr->sh_size = rel_hdr->sh_entsize;
15107 deleted -= 1;
15108 }
15109 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15110 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15111 input_section->reloc_count -= deleted;
5bd4f169
AM
15112 }
15113
645ea6a9
AM
15114 /* If we're emitting relocations, then shortly after this function
15115 returns, reloc offsets and addends for this section will be
15116 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15117 file rather than the input. Save a copy of the relocs for
15118 opd_entry_value. */
0e1862bb 15119 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15120 {
15121 bfd_size_type amt;
15122 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15123 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15124 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15125 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15126 if (rel == NULL)
15127 return FALSE;
15128 memcpy (rel, relocs, amt);
15129 }
5bd4f169
AM
15130 return ret;
15131}
15132
754021d0
AM
15133/* Adjust the value of any local symbols in opd sections. */
15134
6e0b88f1 15135static int
754021d0
AM
15136ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15137 const char *name ATTRIBUTE_UNUSED,
15138 Elf_Internal_Sym *elfsym,
15139 asection *input_sec,
15140 struct elf_link_hash_entry *h)
15141{
74f0fb50
AM
15142 struct _opd_sec_data *opd;
15143 long adjust;
754021d0
AM
15144 bfd_vma value;
15145
4025353c 15146 if (h != NULL)
6e0b88f1 15147 return 1;
4025353c 15148
74f0fb50
AM
15149 opd = get_opd_info (input_sec);
15150 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15151 return 1;
754021d0
AM
15152
15153 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15154 if (!bfd_link_relocatable (info))
754021d0
AM
15155 value -= input_sec->output_section->vma;
15156
51aecdc5 15157 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15158 if (adjust == -1)
6e0b88f1
AM
15159 return 2;
15160
15161 elfsym->st_value += adjust;
15162 return 1;
754021d0
AM
15163}
15164
5bd4f169
AM
15165/* Finish up dynamic symbol handling. We set the contents of various
15166 dynamic sections here. */
15167
b34976b6 15168static bfd_boolean
4ce794b7
AM
15169ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15170 struct bfd_link_info *info,
15171 struct elf_link_hash_entry *h,
ab6dce23 15172 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 15173{
65f38f15 15174 struct ppc_link_hash_table *htab;
8387904d
AM
15175 struct plt_entry *ent;
15176 Elf_Internal_Rela rela;
15177 bfd_byte *loc;
5bd4f169 15178
65f38f15 15179 htab = ppc_hash_table (info);
4dfe6ac6
NC
15180 if (htab == NULL)
15181 return FALSE;
5bd4f169 15182
8387904d
AM
15183 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15184 if (ent->plt.offset != (bfd_vma) -1)
15185 {
15186 /* This symbol has an entry in the procedure linkage
15187 table. Set it up. */
e054468f
AM
15188 if (!htab->elf.dynamic_sections_created
15189 || h->dynindx == -1)
15190 {
15191 BFD_ASSERT (h->type == STT_GNU_IFUNC
15192 && h->def_regular
15193 && (h->root.type == bfd_link_hash_defined
15194 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15195 rela.r_offset = (htab->elf.iplt->output_section->vma
15196 + htab->elf.iplt->output_offset
25f23106 15197 + ent->plt.offset);
ee67d69a
AM
15198 if (htab->opd_abi)
15199 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15200 else
15201 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15202 rela.r_addend = (h->root.u.def.value
15203 + h->root.u.def.section->output_offset
15204 + h->root.u.def.section->output_section->vma
15205 + ent->addend);
33e44f2e
AM
15206 loc = (htab->elf.irelplt->contents
15207 + (htab->elf.irelplt->reloc_count++
25f23106 15208 * sizeof (Elf64_External_Rela)));
e054468f
AM
15209 }
15210 else
15211 {
33e44f2e
AM
15212 rela.r_offset = (htab->elf.splt->output_section->vma
15213 + htab->elf.splt->output_offset
25f23106 15214 + ent->plt.offset);
e054468f
AM
15215 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15216 rela.r_addend = ent->addend;
33e44f2e 15217 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15218 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15219 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 15220 }
8387904d 15221 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15222
15223 if (!htab->opd_abi)
15224 {
15225 if (!h->def_regular)
15226 {
15227 /* Mark the symbol as undefined, rather than as
15228 defined in glink. Leave the value if there were
15229 any relocations where pointer equality matters
15230 (this is a clue for the dynamic linker, to make
15231 function pointer comparisons work between an
15232 application and shared library), otherwise set it
15233 to zero. */
15234 sym->st_shndx = SHN_UNDEF;
15235 if (!h->pointer_equality_needed)
15236 sym->st_value = 0;
15237 else if (!h->ref_regular_nonweak)
15238 {
15239 /* This breaks function pointer comparisons, but
15240 that is better than breaking tests for a NULL
15241 function pointer. */
15242 sym->st_value = 0;
15243 }
15244 }
15245 }
8387904d 15246 }
5bd4f169 15247
f5385ebf 15248 if (h->needs_copy)
5bd4f169 15249 {
65f38f15 15250 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 15251
65f38f15
AM
15252 if (h->dynindx == -1
15253 || (h->root.type != bfd_link_hash_defined
15254 && h->root.type != bfd_link_hash_defweak)
4ce794b7 15255 || htab->relbss == NULL)
65f38f15 15256 abort ();
5bd4f169
AM
15257
15258 rela.r_offset = (h->root.u.def.value
15259 + h->root.u.def.section->output_section->vma
15260 + h->root.u.def.section->output_offset);
15261 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15262 rela.r_addend = 0;
4ce794b7
AM
15263 loc = htab->relbss->contents;
15264 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15265 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15266 }
15267
b34976b6 15268 return TRUE;
5bd4f169
AM
15269}
15270
65f38f15
AM
15271/* Used to decide how to sort relocs in an optimal manner for the
15272 dynamic linker, before writing them out. */
15273
15274static enum elf_reloc_type_class
7e612e98
AM
15275ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15276 const asection *rel_sec,
15277 const Elf_Internal_Rela *rela)
65f38f15 15278{
04c9666a 15279 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15280 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15281
33e44f2e 15282 if (rel_sec == htab->elf.irelplt)
7e612e98 15283 return reloc_class_ifunc;
a33d1f77 15284
4ce794b7 15285 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15286 switch (r_type)
65f38f15
AM
15287 {
15288 case R_PPC64_RELATIVE:
15289 return reloc_class_relative;
15290 case R_PPC64_JMP_SLOT:
15291 return reloc_class_plt;
15292 case R_PPC64_COPY:
15293 return reloc_class_copy;
15294 default:
15295 return reloc_class_normal;
15296 }
15297}
15298
5bd4f169
AM
15299/* Finish up the dynamic sections. */
15300
b34976b6 15301static bfd_boolean
4ce794b7
AM
15302ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15303 struct bfd_link_info *info)
5bd4f169 15304{
65f38f15
AM
15305 struct ppc_link_hash_table *htab;
15306 bfd *dynobj;
5bd4f169 15307 asection *sdyn;
5bd4f169 15308
65f38f15 15309 htab = ppc_hash_table (info);
4dfe6ac6
NC
15310 if (htab == NULL)
15311 return FALSE;
15312
65f38f15 15313 dynobj = htab->elf.dynobj;
3d4d4302 15314 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15315
65f38f15 15316 if (htab->elf.dynamic_sections_created)
5bd4f169 15317 {
5bd4f169
AM
15318 Elf64_External_Dyn *dyncon, *dynconend;
15319
33e44f2e 15320 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15321 abort ();
5bd4f169
AM
15322
15323 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15324 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15325 for (; dyncon < dynconend; dyncon++)
15326 {
15327 Elf_Internal_Dyn dyn;
19397422 15328 asection *s;
5bd4f169
AM
15329
15330 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15331
15332 switch (dyn.d_tag)
15333 {
65f38f15
AM
15334 default:
15335 continue;
5bd4f169 15336
5d1634d7 15337 case DT_PPC64_GLINK:
4ce794b7 15338 s = htab->glink;
6348e046 15339 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15340 /* We stupidly defined DT_PPC64_GLINK to be the start
15341 of glink rather than the first entry point, which is
15342 what ld.so needs, and now have a bigger stub to
15343 support automatic multiple TOCs. */
b9e5796b 15344 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15345 break;
15346
19397422
AM
15347 case DT_PPC64_OPD:
15348 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15349 if (s == NULL)
15350 continue;
15351 dyn.d_un.d_ptr = s->vma;
19397422
AM
15352 break;
15353
e8910a83
AM
15354 case DT_PPC64_OPT:
15355 if (htab->do_multi_toc && htab->multi_toc_needed)
15356 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15357 break;
15358
19397422
AM
15359 case DT_PPC64_OPDSZ:
15360 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15361 if (s == NULL)
15362 continue;
eea6121a 15363 dyn.d_un.d_val = s->size;
19397422
AM
15364 break;
15365
65f38f15 15366 case DT_PLTGOT:
33e44f2e 15367 s = htab->elf.splt;
6348e046 15368 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15369 break;
15370
15371 case DT_JMPREL:
33e44f2e 15372 s = htab->elf.srelplt;
6348e046 15373 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15374 break;
5bd4f169 15375
65f38f15 15376 case DT_PLTRELSZ:
33e44f2e 15377 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7
AM
15378 break;
15379
15380 case DT_RELASZ:
15381 /* Don't count procedure linkage table relocs in the
15382 overall reloc count. */
33e44f2e 15383 s = htab->elf.srelplt;
6348e046
AM
15384 if (s == NULL)
15385 continue;
eea6121a 15386 dyn.d_un.d_val -= s->size;
6348e046
AM
15387 break;
15388
15389 case DT_RELA:
15390 /* We may not be using the standard ELF linker script.
15391 If .rela.plt is the first .rela section, we adjust
15392 DT_RELA to not include it. */
33e44f2e 15393 s = htab->elf.srelplt;
6348e046
AM
15394 if (s == NULL)
15395 continue;
15396 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
15397 continue;
eea6121a 15398 dyn.d_un.d_ptr += s->size;
65f38f15 15399 break;
5bd4f169 15400 }
5bd4f169 15401
65f38f15 15402 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15403 }
5bd4f169
AM
15404 }
15405
33e44f2e 15406 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
5d1634d7
AM
15407 {
15408 /* Fill in the first entry in the global offset table.
15409 We use it to hold the link-time TOCbase. */
15410 bfd_put_64 (output_bfd,
60ee0d4a 15411 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15412 htab->elf.sgot->contents);
5d1634d7
AM
15413
15414 /* Set .got entry size. */
33e44f2e 15415 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15416 }
15417
33e44f2e 15418 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5d1634d7
AM
15419 {
15420 /* Set .plt entry size. */
33e44f2e 15421 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15422 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15423 }
15424
84f5d08e
AM
15425 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15426 brlt ourselves if emitrelocations. */
15427 if (htab->brlt != NULL
15428 && htab->brlt->reloc_count != 0
15429 && !_bfd_elf_link_output_relocs (output_bfd,
15430 htab->brlt,
d4730f92 15431 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15432 elf_section_data (htab->brlt)->relocs,
15433 NULL))
15434 return FALSE;
15435
176a0d42
AM
15436 if (htab->glink != NULL
15437 && htab->glink->reloc_count != 0
15438 && !_bfd_elf_link_output_relocs (output_bfd,
15439 htab->glink,
d4730f92 15440 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15441 elf_section_data (htab->glink)->relocs,
15442 NULL))
15443 return FALSE;
15444
58d180e8 15445 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15446 && htab->glink_eh_frame->size != 0)
15447 {
15448 bfd_vma val;
15449 bfd_byte *p;
15450 asection *stub_sec;
15451
15452 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15453 for (stub_sec = htab->params->stub_bfd->sections;
15454 stub_sec != NULL;
15455 stub_sec = stub_sec->next)
15456 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15457 {
15458 /* FDE length. */
15459 p += 4;
15460 /* CIE pointer. */
15461 p += 4;
15462 /* Offset to stub section. */
15463 val = (stub_sec->output_section->vma
15464 + stub_sec->output_offset);
15465 val -= (htab->glink_eh_frame->output_section->vma
15466 + htab->glink_eh_frame->output_offset
15467 + (p - htab->glink_eh_frame->contents));
15468 if (val + 0x80000000 > 0xffffffff)
15469 {
15470 info->callbacks->einfo
15471 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15472 stub_sec->name);
15473 return FALSE;
15474 }
15475 bfd_put_32 (dynobj, val, p);
15476 p += 4;
15477 /* stub section size. */
15478 p += 4;
15479 /* Augmentation. */
15480 p += 1;
15481 /* Pad. */
15482 p += 7;
15483 }
15484 if (htab->glink != NULL && htab->glink->size != 0)
15485 {
15486 /* FDE length. */
15487 p += 4;
15488 /* CIE pointer. */
15489 p += 4;
15490 /* Offset to .glink. */
15491 val = (htab->glink->output_section->vma
15492 + htab->glink->output_offset
15493 + 8);
15494 val -= (htab->glink_eh_frame->output_section->vma
15495 + htab->glink_eh_frame->output_offset
15496 + (p - htab->glink_eh_frame->contents));
15497 if (val + 0x80000000 > 0xffffffff)
15498 {
15499 info->callbacks->einfo
15500 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15501 htab->glink->name);
15502 return FALSE;
15503 }
15504 bfd_put_32 (dynobj, val, p);
15505 p += 4;
15506 /* .glink size. */
15507 p += 4;
15508 /* Augmentation. */
15509 p += 1;
15510 /* Ops. */
15511 p += 7;
15512 }
15513
15514 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15515 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15516 htab->glink_eh_frame,
15517 htab->glink_eh_frame->contents))
15518 return FALSE;
15519 }
58d180e8 15520
e717da7e 15521 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15522 since we didn't add them to DYNOBJ. We know dynobj is the first
15523 bfd. */
c72f2fb2 15524 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15525 {
15526 asection *s;
7b53ace3 15527
0c8d6e5c 15528 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15529 continue;
15530
e717da7e
AM
15531 s = ppc64_elf_tdata (dynobj)->got;
15532 if (s != NULL
eea6121a 15533 && s->size != 0
e717da7e
AM
15534 && s->output_section != bfd_abs_section_ptr
15535 && !bfd_set_section_contents (output_bfd, s->output_section,
15536 s->contents, s->output_offset,
eea6121a 15537 s->size))
e717da7e
AM
15538 return FALSE;
15539 s = ppc64_elf_tdata (dynobj)->relgot;
15540 if (s != NULL
eea6121a 15541 && s->size != 0
e717da7e
AM
15542 && s->output_section != bfd_abs_section_ptr
15543 && !bfd_set_section_contents (output_bfd, s->output_section,
15544 s->contents, s->output_offset,
eea6121a 15545 s->size))
e717da7e
AM
15546 return FALSE;
15547 }
f6c52c13 15548
b34976b6 15549 return TRUE;
5bd4f169
AM
15550}
15551
5bd4f169 15552#include "elf64-target.h"
7b8e7dad
AM
15553
15554/* FreeBSD support */
15555
15556#undef TARGET_LITTLE_SYM
15557#undef TARGET_LITTLE_NAME
15558
15559#undef TARGET_BIG_SYM
6d00b590 15560#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15561#undef TARGET_BIG_NAME
15562#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15563
15564#undef ELF_OSABI
15565#define ELF_OSABI ELFOSABI_FREEBSD
15566
15567#undef elf64_bed
15568#define elf64_bed elf64_powerpc_fbsd_bed
15569
15570#include "elf64-target.h"
15571