]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
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
f9c6b907
AM
2025 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2026 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2027 16, /* rightshift */
2028 1, /* size (0 = byte, 1 = short, 2 = long) */
2029 16, /* bitsize */
2030 FALSE, /* pc_relative */
2031 0, /* bitpos */
2032 complain_overflow_dont, /* complain_on_overflow */
2033 bfd_elf_generic_reloc, /* special_function */
2034 "R_PPC64_ADDR16_HIGH", /* name */
2035 FALSE, /* partial_inplace */
2036 0, /* src_mask */
2037 0xffff, /* dst_mask */
2038 FALSE), /* pcrel_offset */
2039
2040 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2041 HOWTO (R_PPC64_ADDR16_HIGHA, /* 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 ppc64_elf_ha_reloc, /* special_function */
2049 "R_PPC64_ADDR16_HIGHA", /* name */
2050 FALSE, /* partial_inplace */
2051 0, /* src_mask */
2052 0xffff, /* dst_mask */
2053 FALSE), /* pcrel_offset */
2054
2055 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2056 HOWTO (R_PPC64_DTPREL16_HIGH,
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_unhandled_reloc, /* special_function */
2064 "R_PPC64_DTPREL16_HIGH", /* name */
2065 FALSE, /* partial_inplace */
2066 0, /* src_mask */
2067 0xffff, /* dst_mask */
2068 FALSE), /* pcrel_offset */
2069
2070 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2071 HOWTO (R_PPC64_DTPREL16_HIGHA,
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_HIGHA", /* name */
2080 FALSE, /* partial_inplace */
2081 0, /* src_mask */
2082 0xffff, /* dst_mask */
2083 FALSE), /* pcrel_offset */
2084
2085 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2086 HOWTO (R_PPC64_TPREL16_HIGH,
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_TPREL16_HIGH", /* name */
2095 FALSE, /* partial_inplace */
2096 0, /* src_mask */
2097 0xffff, /* dst_mask */
2098 FALSE), /* pcrel_offset */
2099
2100 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2101 HOWTO (R_PPC64_TPREL16_HIGHA,
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_HIGHA", /* name */
2110 FALSE, /* partial_inplace */
2111 0, /* src_mask */
2112 0xffff, /* dst_mask */
2113 FALSE), /* pcrel_offset */
2114
45965137
AM
2115 /* Like ADDR64, but use local entry point of function. */
2116 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2117 0, /* rightshift */
2118 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2119 64, /* bitsize */
2120 FALSE, /* pc_relative */
2121 0, /* bitpos */
2122 complain_overflow_dont, /* complain_on_overflow */
2123 bfd_elf_generic_reloc, /* special_function */
2124 "R_PPC64_ADDR64_LOCAL", /* name */
2125 FALSE, /* partial_inplace */
2126 0, /* src_mask */
2127 ONES (64), /* dst_mask */
2128 FALSE), /* pcrel_offset */
2129
5bd4f169
AM
2130 /* GNU extension to record C++ vtable hierarchy. */
2131 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2132 0, /* rightshift */
2133 0, /* size (0 = byte, 1 = short, 2 = long) */
2134 0, /* bitsize */
b34976b6 2135 FALSE, /* pc_relative */
5bd4f169
AM
2136 0, /* bitpos */
2137 complain_overflow_dont, /* complain_on_overflow */
2138 NULL, /* special_function */
2139 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2140 FALSE, /* partial_inplace */
5bd4f169
AM
2141 0, /* src_mask */
2142 0, /* dst_mask */
b34976b6 2143 FALSE), /* pcrel_offset */
5bd4f169
AM
2144
2145 /* GNU extension to record C++ vtable member usage. */
2146 HOWTO (R_PPC64_GNU_VTENTRY, /* 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_VTENTRY", /* 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
2161\f
2162/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2163 be done. */
2164
2165static void
4ce794b7 2166ppc_howto_init (void)
5bd4f169
AM
2167{
2168 unsigned int i, type;
2169
a4b6fadd 2170 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2171 {
2172 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2173 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2174 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2175 }
2176}
2177
2178static reloc_howto_type *
4ce794b7
AM
2179ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2180 bfd_reloc_code_real_type code)
5bd4f169 2181{
411e1bfb 2182 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2183
2184 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2185 /* Initialize howto table if needed. */
2186 ppc_howto_init ();
2187
4ce794b7 2188 switch (code)
5bd4f169
AM
2189 {
2190 default:
4ce794b7 2191 return NULL;
5bd4f169 2192
411e1bfb
AM
2193 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2194 break;
2195 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2196 break;
2197 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2198 break;
2199 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2200 break;
2201 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2202 break;
2203 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2204 break;
f9c6b907
AM
2205 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2206 break;
411e1bfb 2207 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2208 break;
f9c6b907
AM
2209 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2210 break;
411e1bfb 2211 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2212 break;
411e1bfb 2213 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2214 break;
411e1bfb 2215 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2216 break;
411e1bfb 2217 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2218 break;
411e1bfb 2219 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2220 break;
411e1bfb 2221 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2222 break;
411e1bfb 2223 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2224 break;
411e1bfb 2225 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2226 break;
411e1bfb 2227 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2228 break;
411e1bfb 2229 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2230 break;
411e1bfb 2231 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2232 break;
411e1bfb 2233 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2234 break;
411e1bfb 2235 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2236 break;
411e1bfb 2237 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2238 break;
411e1bfb 2239 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2240 break;
411e1bfb 2241 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2242 break;
411e1bfb 2243 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2244 break;
411e1bfb 2245 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2246 break;
411e1bfb 2247 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2248 break;
411e1bfb 2249 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2250 break;
411e1bfb 2251 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2252 break;
411e1bfb 2253 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2254 break;
411e1bfb 2255 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2256 break;
411e1bfb 2257 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2258 break;
411e1bfb 2259 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2260 break;
411e1bfb 2261 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2262 break;
411e1bfb 2263 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2264 break;
411e1bfb 2265 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2266 break;
411e1bfb 2267 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2268 break;
411e1bfb 2269 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2270 break;
411e1bfb 2271 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2272 break;
411e1bfb 2273 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2274 break;
411e1bfb 2275 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2276 break;
411e1bfb 2277 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2278 break;
411e1bfb 2279 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2280 break;
411e1bfb 2281 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2282 break;
411e1bfb 2283 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2284 break;
411e1bfb 2285 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2286 break;
411e1bfb 2287 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2288 break;
411e1bfb 2289 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2290 break;
411e1bfb 2291 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2292 break;
411e1bfb 2293 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2294 break;
411e1bfb 2295 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2296 break;
411e1bfb 2297 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2298 break;
411e1bfb 2299 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2300 break;
411e1bfb 2301 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2302 break;
411e1bfb 2303 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2304 break;
411e1bfb 2305 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2306 break;
411e1bfb 2307 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2308 break;
411e1bfb 2309 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2310 break;
411e1bfb 2311 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2312 break;
411e1bfb 2313 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2314 break;
411e1bfb 2315 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2316 break;
727fc41e
AM
2317 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2318 break;
2319 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2320 break;
411e1bfb 2321 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2322 break;
411e1bfb 2323 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2324 break;
411e1bfb 2325 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2326 break;
411e1bfb 2327 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2328 break;
f9c6b907
AM
2329 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2330 break;
411e1bfb 2331 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2332 break;
f9c6b907
AM
2333 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2334 break;
411e1bfb 2335 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2336 break;
411e1bfb
AM
2337 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2338 break;
2339 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2340 break;
2341 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2342 break;
f9c6b907
AM
2343 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2344 break;
411e1bfb
AM
2345 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2346 break;
f9c6b907
AM
2347 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2348 break;
411e1bfb
AM
2349 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2350 break;
2351 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2352 break;
2353 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2354 break;
2355 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2356 break;
2357 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2358 break;
2359 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2360 break;
2361 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2362 break;
2363 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2364 break;
2365 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2366 break;
2367 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2368 break;
2369 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2370 break;
2371 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2372 break;
2373 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2374 break;
2375 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2376 break;
2377 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2378 break;
2379 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2380 break;
2381 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2382 break;
2383 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2384 break;
2385 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2386 break;
2387 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2388 break;
2389 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2390 break;
2391 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2392 break;
2393 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2394 break;
2395 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2396 break;
2397 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2398 break;
2399 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2400 break;
2401 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2402 break;
2403 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2404 break;
2405 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2406 break;
25f23106
AM
2407 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2408 break;
2409 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2410 break;
2411 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2412 break;
2413 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2414 break;
45965137
AM
2415 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2416 break;
411e1bfb
AM
2417 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2418 break;
2419 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2420 break;
2421 }
2422
4ce794b7 2423 return ppc64_elf_howto_table[r];
5bd4f169
AM
2424};
2425
157090f7
AM
2426static reloc_howto_type *
2427ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2428 const char *r_name)
2429{
2430 unsigned int i;
2431
a4b6fadd 2432 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2433 if (ppc64_elf_howto_raw[i].name != NULL
2434 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2435 return &ppc64_elf_howto_raw[i];
2436
2437 return NULL;
2438}
2439
5bd4f169
AM
2440/* Set the howto pointer for a PowerPC ELF reloc. */
2441
2442static void
4ce794b7
AM
2443ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2444 Elf_Internal_Rela *dst)
5bd4f169 2445{
65f38f15
AM
2446 unsigned int type;
2447
ef60b7ff 2448 /* Initialize howto table if needed. */
5bd4f169 2449 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2450 ppc_howto_init ();
2451
65f38f15 2452 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2453 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d
JJ
2454 {
2455 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2456 abfd, (int) type);
2457 type = R_PPC64_NONE;
d0fb9a8d 2458 }
65f38f15 2459 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2460}
2461
04c9666a 2462/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2463
2464static bfd_reloc_status_type
4ce794b7
AM
2465ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2466 void *data, asection *input_section,
2467 bfd *output_bfd, char **error_message)
5bd4f169 2468{
805fc799
AM
2469 /* If this is a relocatable link (output_bfd test tells us), just
2470 call the generic function. Any adjustment will be done at final
2471 link time. */
2472 if (output_bfd != NULL)
cedb70c5 2473 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2474 input_section, output_bfd, error_message);
2475
2476 /* Adjust the addend for sign extension of the low 16 bits.
2477 We won't actually be using the low 16 bits, so trashing them
2478 doesn't matter. */
2479 reloc_entry->addend += 0x8000;
2480 return bfd_reloc_continue;
2481}
5bd4f169 2482
2441e016
AM
2483static bfd_reloc_status_type
2484ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2485 void *data, asection *input_section,
2486 bfd *output_bfd, char **error_message)
2487{
2488 if (output_bfd != NULL)
2489 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2490 input_section, output_bfd, error_message);
2491
699733f6
AM
2492 if (strcmp (symbol->section->name, ".opd") == 0
2493 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2494 {
2495 bfd_vma dest = opd_entry_value (symbol->section,
2496 symbol->value + reloc_entry->addend,
aef36ac1 2497 NULL, NULL, FALSE);
2441e016
AM
2498 if (dest != (bfd_vma) -1)
2499 reloc_entry->addend = dest - (symbol->value
2500 + symbol->section->output_section->vma
2501 + symbol->section->output_offset);
2502 }
810d4e75
AM
2503 else
2504 {
2505 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2506
2507 if (symbol->section->owner != abfd
2508 && abiversion (symbol->section->owner) >= 2)
2509 {
2510 unsigned int i;
2511
2512 for (i = 0; i < symbol->section->owner->symcount; ++i)
2513 {
2514 asymbol *symdef = symbol->section->owner->outsymbols[i];
2515
2516 if (strcmp (symdef->name, symbol->name) == 0)
2517 {
2518 elfsym = (elf_symbol_type *) symdef;
2519 break;
2520 }
2521 }
2522 }
2523 reloc_entry->addend
2524 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2525 }
2441e016
AM
2526 return bfd_reloc_continue;
2527}
2528
805fc799 2529static bfd_reloc_status_type
4ce794b7
AM
2530ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2531 void *data, asection *input_section,
2532 bfd *output_bfd, char **error_message)
805fc799
AM
2533{
2534 long insn;
04c9666a 2535 enum elf_ppc64_reloc_type r_type;
805fc799 2536 bfd_size_type octets;
794e51c0
AM
2537 /* Assume 'at' branch hints. */
2538 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2539
2540 /* If this is a relocatable link (output_bfd test tells us), just
2541 call the generic function. Any adjustment will be done at final
2542 link time. */
5bd4f169 2543 if (output_bfd != NULL)
cedb70c5 2544 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2545 input_section, output_bfd, error_message);
2546
2547 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2548 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2549 insn &= ~(0x01 << 21);
4ce794b7 2550 r_type = reloc_entry->howto->type;
805fc799
AM
2551 if (r_type == R_PPC64_ADDR14_BRTAKEN
2552 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2553 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2554
794e51c0 2555 if (is_isa_v2)
5bd4f169 2556 {
805fc799
AM
2557 /* Set 'a' bit. This is 0b00010 in BO field for branch
2558 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2559 for branch on CTR insns (BO == 1a00t or 1a01t). */
2560 if ((insn & (0x14 << 21)) == (0x04 << 21))
2561 insn |= 0x02 << 21;
2562 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2563 insn |= 0x08 << 21;
2564 else
2441e016 2565 goto out;
5bd4f169 2566 }
805fc799
AM
2567 else
2568 {
2569 bfd_vma target = 0;
2570 bfd_vma from;
5bd4f169 2571
805fc799
AM
2572 if (!bfd_is_com_section (symbol->section))
2573 target = symbol->value;
2574 target += symbol->section->output_section->vma;
2575 target += symbol->section->output_offset;
2576 target += reloc_entry->addend;
5bd4f169 2577
805fc799
AM
2578 from = (reloc_entry->address
2579 + input_section->output_offset
2580 + input_section->output_section->vma);
5bd4f169 2581
805fc799
AM
2582 /* Invert 'y' bit if not the default. */
2583 if ((bfd_signed_vma) (target - from) < 0)
2584 insn ^= 0x01 << 21;
2585 }
4ce794b7 2586 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2587 out:
2588 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2589 input_section, output_bfd, error_message);
805fc799 2590}
5bd4f169 2591
805fc799 2592static bfd_reloc_status_type
4ce794b7
AM
2593ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2594 void *data, asection *input_section,
2595 bfd *output_bfd, char **error_message)
805fc799
AM
2596{
2597 /* If this is a relocatable link (output_bfd test tells us), just
2598 call the generic function. Any adjustment will be done at final
2599 link time. */
2600 if (output_bfd != NULL)
cedb70c5 2601 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2602 input_section, output_bfd, error_message);
5bd4f169 2603
805fc799
AM
2604 /* Subtract the symbol section base address. */
2605 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2606 return bfd_reloc_continue;
2607}
2608
805fc799 2609static bfd_reloc_status_type
4ce794b7
AM
2610ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2611 void *data, asection *input_section,
2612 bfd *output_bfd, char **error_message)
805fc799
AM
2613{
2614 /* If this is a relocatable link (output_bfd test tells us), just
2615 call the generic function. Any adjustment will be done at final
2616 link time. */
2617 if (output_bfd != NULL)
cedb70c5 2618 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2619 input_section, output_bfd, error_message);
2620
2621 /* Subtract the symbol section base address. */
2622 reloc_entry->addend -= symbol->section->output_section->vma;
2623
2624 /* Adjust the addend for sign extension of the low 16 bits. */
2625 reloc_entry->addend += 0x8000;
2626 return bfd_reloc_continue;
2627}
2628
2629static bfd_reloc_status_type
4ce794b7
AM
2630ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2631 void *data, asection *input_section,
2632 bfd *output_bfd, char **error_message)
805fc799
AM
2633{
2634 bfd_vma TOCstart;
2635
2636 /* If this is a relocatable link (output_bfd test tells us), just
2637 call the generic function. Any adjustment will be done at final
2638 link time. */
2639 if (output_bfd != NULL)
cedb70c5 2640 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2641 input_section, output_bfd, error_message);
2642
2643 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2644 if (TOCstart == 0)
1c865ab2 2645 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2646
2647 /* Subtract the TOC base address. */
2648 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2649 return bfd_reloc_continue;
2650}
2651
2652static bfd_reloc_status_type
4ce794b7
AM
2653ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2654 void *data, asection *input_section,
2655 bfd *output_bfd, char **error_message)
805fc799
AM
2656{
2657 bfd_vma TOCstart;
2658
2659 /* If this is a relocatable link (output_bfd test tells us), just
2660 call the generic function. Any adjustment will be done at final
2661 link time. */
2662 if (output_bfd != NULL)
cedb70c5 2663 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2664 input_section, output_bfd, error_message);
2665
2666 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2667 if (TOCstart == 0)
1c865ab2 2668 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2669
2670 /* Subtract the TOC base address. */
2671 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2672
2673 /* Adjust the addend for sign extension of the low 16 bits. */
2674 reloc_entry->addend += 0x8000;
2675 return bfd_reloc_continue;
2676}
2677
2678static bfd_reloc_status_type
4ce794b7
AM
2679ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2680 void *data, asection *input_section,
2681 bfd *output_bfd, char **error_message)
805fc799
AM
2682{
2683 bfd_vma TOCstart;
2684 bfd_size_type octets;
2685
2686 /* If this is a relocatable link (output_bfd test tells us), just
2687 call the generic function. Any adjustment will be done at final
2688 link time. */
2689 if (output_bfd != NULL)
cedb70c5 2690 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2691 input_section, output_bfd, error_message);
2692
2693 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2694 if (TOCstart == 0)
1c865ab2 2695 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2696
2697 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2698 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2699 return bfd_reloc_ok;
2700}
2701
2702static bfd_reloc_status_type
4ce794b7
AM
2703ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2704 void *data, asection *input_section,
2705 bfd *output_bfd, char **error_message)
805fc799
AM
2706{
2707 /* If this is a relocatable link (output_bfd test tells us), just
2708 call the generic function. Any adjustment will be done at final
2709 link time. */
2710 if (output_bfd != NULL)
cedb70c5 2711 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2712 input_section, output_bfd, error_message);
2713
2714 if (error_message != NULL)
2715 {
2716 static char buf[60];
2717 sprintf (buf, "generic linker can't handle %s",
2718 reloc_entry->howto->name);
2719 *error_message = buf;
2720 }
2721 return bfd_reloc_dangerous;
2722}
2723
927be08e
AM
2724/* Track GOT entries needed for a given symbol. We might need more
2725 than one got entry per symbol. */
2726struct got_entry
2727{
2728 struct got_entry *next;
2729
2730 /* The symbol addend that we'll be placing in the GOT. */
2731 bfd_vma addend;
2732
2733 /* Unlike other ELF targets, we use separate GOT entries for the same
2734 symbol referenced from different input files. This is to support
2735 automatic multiple TOC/GOT sections, where the TOC base can vary
2736 from one input file to another. After partitioning into TOC groups
2737 we merge entries within the group.
2738
2739 Point to the BFD owning this GOT entry. */
2740 bfd *owner;
2741
2742 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2743 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2744 unsigned char tls_type;
927be08e
AM
2745
2746 /* Non-zero if got.ent points to real entry. */
f961d9dd 2747 unsigned char is_indirect;
927be08e
AM
2748
2749 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2750 union
2751 {
2752 bfd_signed_vma refcount;
2753 bfd_vma offset;
2754 struct got_entry *ent;
2755 } got;
2756};
2757
2758/* The same for PLT. */
2759struct plt_entry
2760{
2761 struct plt_entry *next;
2762
2763 bfd_vma addend;
2764
2765 union
2766 {
2767 bfd_signed_vma refcount;
2768 bfd_vma offset;
2769 } plt;
2770};
2771
e717da7e
AM
2772struct ppc64_elf_obj_tdata
2773{
2774 struct elf_obj_tdata elf;
2775
2776 /* Shortcuts to dynamic linker sections. */
2777 asection *got;
2778 asection *relgot;
2779
b3fac117
AM
2780 /* Used during garbage collection. We attach global symbols defined
2781 on removed .opd entries to this section so that the sym is removed. */
2782 asection *deleted_section;
81688140 2783
927be08e 2784 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2785 sections means we potentially need one of these for each input bfd. */
927be08e 2786 struct got_entry tlsld_got;
8860955f 2787
729eabd5
AM
2788 union {
2789 /* A copy of relocs before they are modified for --emit-relocs. */
2790 Elf_Internal_Rela *relocs;
2791
2792 /* Section contents. */
2793 bfd_byte *contents;
2794 } opd;
d77c8a4b
AM
2795
2796 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2797 the reloc to be in the range -32768 to 32767. */
98528052
AM
2798 unsigned int has_small_toc_reloc : 1;
2799
560c8763
AM
2800 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2801 instruction not one we handle. */
2802 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2803};
2804
2805#define ppc64_elf_tdata(bfd) \
2806 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2807
2808#define ppc64_tlsld_got(bfd) \
2809 (&ppc64_elf_tdata (bfd)->tlsld_got)
2810
0c8d6e5c
AM
2811#define is_ppc64_elf(bfd) \
2812 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2813 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2814
e717da7e
AM
2815/* Override the generic function because we store some extras. */
2816
2817static bfd_boolean
2818ppc64_elf_mkobject (bfd *abfd)
2819{
0ffa91dd 2820 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2821 PPC64_ELF_DATA);
e717da7e
AM
2822}
2823
feee612b
AM
2824/* Fix bad default arch selected for a 64 bit input bfd when the
2825 default is 32 bit. */
2826
b34976b6 2827static bfd_boolean
4ce794b7 2828ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2829{
2830 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2831 {
2832 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2833
2834 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2835 {
2836 /* Relies on arch after 32 bit default being 64 bit default. */
2837 abfd->arch_info = abfd->arch_info->next;
2838 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2839 }
2840 }
b34976b6 2841 return TRUE;
feee612b
AM
2842}
2843
d37c89e5
AM
2844/* Support for core dump NOTE sections. */
2845
2846static bfd_boolean
2847ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2848{
eea6121a 2849 size_t offset, size;
d37c89e5
AM
2850
2851 if (note->descsz != 504)
2852 return FALSE;
2853
2854 /* pr_cursig */
228e534f 2855 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2856
2857 /* pr_pid */
228e534f 2858 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2859
2860 /* pr_reg */
2861 offset = 112;
eea6121a 2862 size = 384;
d37c89e5
AM
2863
2864 /* Make a ".reg/999" section. */
2865 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2866 size, note->descpos + offset);
d37c89e5
AM
2867}
2868
2869static bfd_boolean
2870ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2871{
2872 if (note->descsz != 136)
2873 return FALSE;
2874
228e534f 2875 elf_tdata (abfd)->core->pid
bc989cdc 2876 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2877 elf_tdata (abfd)->core->program
d37c89e5 2878 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2879 elf_tdata (abfd)->core->command
d37c89e5
AM
2880 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2881
2882 return TRUE;
2883}
2884
183e98be
AM
2885static char *
2886ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2887 ...)
2888{
2889 switch (note_type)
2890 {
2891 default:
2892 return NULL;
2893
2894 case NT_PRPSINFO:
2895 {
2896 char data[136];
2897 va_list ap;
2898
2899 va_start (ap, note_type);
75cd47ed 2900 memset (data, 0, sizeof (data));
183e98be
AM
2901 strncpy (data + 40, va_arg (ap, const char *), 16);
2902 strncpy (data + 56, va_arg (ap, const char *), 80);
2903 va_end (ap);
2904 return elfcore_write_note (abfd, buf, bufsiz,
2905 "CORE", note_type, data, sizeof (data));
2906 }
2907
2908 case NT_PRSTATUS:
2909 {
2910 char data[504];
2911 va_list ap;
2912 long pid;
2913 int cursig;
2914 const void *greg;
2915
2916 va_start (ap, note_type);
2917 memset (data, 0, 112);
2918 pid = va_arg (ap, long);
2919 bfd_put_32 (abfd, pid, data + 32);
2920 cursig = va_arg (ap, int);
2921 bfd_put_16 (abfd, cursig, data + 12);
2922 greg = va_arg (ap, const void *);
2923 memcpy (data + 112, greg, 384);
2924 memset (data + 496, 0, 8);
2925 va_end (ap);
2926 return elfcore_write_note (abfd, buf, bufsiz,
2927 "CORE", note_type, data, sizeof (data));
2928 }
2929 }
2930}
2931
5d35169e
AM
2932/* Add extra PPC sections. */
2933
b35d266b 2934static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2935{
0112cd26
NC
2936 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2937 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2938 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2939 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2940 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2941 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2942 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2943};
2944
7c8fe5c4
AM
2945enum _ppc64_sec_type {
2946 sec_normal = 0,
2947 sec_opd = 1,
2948 sec_toc = 2
2949};
2950
f0abc2a1
AM
2951struct _ppc64_elf_section_data
2952{
2953 struct bfd_elf_section_data elf;
411e1bfb 2954
f0abc2a1
AM
2955 union
2956 {
51aecdc5
AM
2957 /* An array with one entry for each opd function descriptor,
2958 and some spares since opd entries may be either 16 or 24 bytes. */
2959#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
2960 struct _opd_sec_data
2961 {
2962 /* Points to the function code section for local opd entries. */
2963 asection **func_sec;
2964
2965 /* After editing .opd, adjust references to opd local syms. */
2966 long *adjust;
2967 } opd;
7c8fe5c4 2968
3a71aa26
AM
2969 /* An array for toc sections, indexed by offset/8. */
2970 struct _toc_sec_data
2971 {
2972 /* Specifies the relocation symbol index used at a given toc offset. */
2973 unsigned *symndx;
2974
2975 /* And the relocation addend. */
2976 bfd_vma *add;
2977 } toc;
7c8fe5c4
AM
2978 } u;
2979
2980 enum _ppc64_sec_type sec_type:2;
411e1bfb 2981
7c8fe5c4
AM
2982 /* Flag set when small branches are detected. Used to
2983 select suitable defaults for the stub group size. */
2984 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2985};
2986
2987#define ppc64_elf_section_data(sec) \
411e1bfb 2988 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2989
2990static bfd_boolean
4ce794b7 2991ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2992{
f592407e
AM
2993 if (!sec->used_by_bfd)
2994 {
2995 struct _ppc64_elf_section_data *sdata;
2996 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2997
f592407e
AM
2998 sdata = bfd_zalloc (abfd, amt);
2999 if (sdata == NULL)
3000 return FALSE;
3001 sec->used_by_bfd = sdata;
3002 }
f0abc2a1
AM
3003
3004 return _bfd_elf_new_section_hook (abfd, sec);
3005}
4025353c 3006
74f0fb50 3007static struct _opd_sec_data *
4025353c
AM
3008get_opd_info (asection * sec)
3009{
3010 if (sec != NULL
3011 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3012 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3013 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3014 return NULL;
3015}
90e3cdf2
JJ
3016\f
3017/* Parameters for the qsort hook. */
90e3cdf2
JJ
3018static bfd_boolean synthetic_relocatable;
3019
699733f6 3020/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3021
3022static int
3023compare_symbols (const void *ap, const void *bp)
3024{
3025 const asymbol *a = * (const asymbol **) ap;
3026 const asymbol *b = * (const asymbol **) bp;
3027
699733f6
AM
3028 /* Section symbols first. */
3029 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3030 return -1;
699733f6 3031 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3032 return 1;
3033
699733f6 3034 /* then .opd symbols. */
ffcfec52
AM
3035 if (strcmp (a->section->name, ".opd") == 0
3036 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 3037 return -1;
ffcfec52
AM
3038 if (strcmp (a->section->name, ".opd") != 0
3039 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
3040 return 1;
3041
699733f6 3042 /* then other code symbols. */
90e3cdf2
JJ
3043 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3044 == (SEC_CODE | SEC_ALLOC)
3045 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3046 != (SEC_CODE | SEC_ALLOC))
3047 return -1;
3048
3049 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3050 != (SEC_CODE | SEC_ALLOC)
3051 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3052 == (SEC_CODE | SEC_ALLOC))
3053 return 1;
3054
3055 if (synthetic_relocatable)
3056 {
3057 if (a->section->id < b->section->id)
3058 return -1;
3059
3060 if (a->section->id > b->section->id)
3061 return 1;
3062 }
3063
3064 if (a->value + a->section->vma < b->value + b->section->vma)
3065 return -1;
3066
3067 if (a->value + a->section->vma > b->value + b->section->vma)
3068 return 1;
3069
4d35a0aa
AM
3070 /* For syms with the same value, prefer strong dynamic global function
3071 syms over other syms. */
3072 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3073 return -1;
3074
3075 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3076 return 1;
3077
3078 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3079 return -1;
3080
3081 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3082 return 1;
3083
3084 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3085 return -1;
3086
3087 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3088 return 1;
3089
3090 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3091 return -1;
3092
3093 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3094 return 1;
3095
90e3cdf2
JJ
3096 return 0;
3097}
3098
699733f6 3099/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3100
699733f6 3101static asymbol *
7292b3ac 3102sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3103{
699733f6 3104 long mid;
90e3cdf2 3105
7292b3ac 3106 if (id == (unsigned) -1)
699733f6
AM
3107 {
3108 while (lo < hi)
3109 {
3110 mid = (lo + hi) >> 1;
3111 if (syms[mid]->value + syms[mid]->section->vma < value)
3112 lo = mid + 1;
3113 else if (syms[mid]->value + syms[mid]->section->vma > value)
3114 hi = mid;
3115 else
3116 return syms[mid];
3117 }
3118 }
3119 else
3120 {
3121 while (lo < hi)
3122 {
3123 mid = (lo + hi) >> 1;
3124 if (syms[mid]->section->id < id)
3125 lo = mid + 1;
3126 else if (syms[mid]->section->id > id)
3127 hi = mid;
3128 else if (syms[mid]->value < value)
3129 lo = mid + 1;
3130 else if (syms[mid]->value > value)
3131 hi = mid;
3132 else
3133 return syms[mid];
3134 }
3135 }
3136 return NULL;
90e3cdf2
JJ
3137}
3138
468392fb
AM
3139static bfd_boolean
3140section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3141{
3142 bfd_vma vma = *(bfd_vma *) ptr;
3143 return ((section->flags & SEC_ALLOC) != 0
3144 && section->vma <= vma
3145 && vma < section->vma + section->size);
3146}
3147
699733f6 3148/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3149 entry syms. Also generate @plt symbols for the glink branch table.
3150 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3151
3152static long
a7535cf3
AM
3153ppc64_elf_get_synthetic_symtab (bfd *abfd,
3154 long static_count, asymbol **static_syms,
3155 long dyn_count, asymbol **dyn_syms,
c9727e01 3156 asymbol **ret)
90e3cdf2
JJ
3157{
3158 asymbol *s;
699733f6
AM
3159 long i;
3160 long count;
90e3cdf2 3161 char *names;
a7535cf3 3162 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3163 asection *opd = NULL;
90e3cdf2 3164 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3165 asymbol **syms;
ee67d69a 3166 int abi = abiversion (abfd);
90e3cdf2
JJ
3167
3168 *ret = NULL;
3169
ee67d69a
AM
3170 if (abi < 2)
3171 {
3172 opd = bfd_get_section_by_name (abfd, ".opd");
3173 if (opd == NULL && abi == 1)
3174 return 0;
3175 }
90e3cdf2 3176
a7535cf3 3177 symcount = static_count;
c9727e01 3178 if (!relocatable)
a7535cf3 3179 symcount += dyn_count;
90e3cdf2 3180 if (symcount == 0)
c9727e01 3181 return 0;
90e3cdf2 3182
a7535cf3
AM
3183 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3184 if (syms == NULL)
7356fed5 3185 return -1;
a7535cf3
AM
3186
3187 if (!relocatable && static_count != 0 && dyn_count != 0)
3188 {
3189 /* Use both symbol tables. */
3190 memcpy (syms, static_syms, static_count * sizeof (*syms));
3191 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3192 }
3193 else if (!relocatable && static_count == 0)
3194 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3195 else
3196 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3197
90e3cdf2 3198 synthetic_relocatable = relocatable;
595da8c5 3199 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3200
c9727e01
AM
3201 if (!relocatable && symcount > 1)
3202 {
3203 long j;
3204 /* Trim duplicate syms, since we may have merged the normal and
3205 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3206 different values, so trim any with the same value. */
c9727e01
AM
3207 for (i = 1, j = 1; i < symcount; ++i)
3208 if (syms[i - 1]->value + syms[i - 1]->section->vma
3209 != syms[i]->value + syms[i]->section->vma)
3210 syms[j++] = syms[i];
3211 symcount = j;
3212 }
3213
699733f6 3214 i = 0;
ffcfec52 3215 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3216 ++i;
3217 codesecsym = i;
90e3cdf2 3218
699733f6
AM
3219 for (; i < symcount; ++i)
3220 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3221 != (SEC_CODE | SEC_ALLOC))
3222 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3223 break;
3224 codesecsymend = i;
90e3cdf2 3225
699733f6
AM
3226 for (; i < symcount; ++i)
3227 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3228 break;
3229 secsymend = i;
90e3cdf2 3230
699733f6 3231 for (; i < symcount; ++i)
ffcfec52 3232 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3233 break;
3234 opdsymend = i;
90e3cdf2 3235
699733f6
AM
3236 for (; i < symcount; ++i)
3237 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3238 != (SEC_CODE | SEC_ALLOC))
3239 break;
3240 symcount = i;
3241
c9727e01 3242 count = 0;
90e3cdf2 3243
699733f6 3244 if (relocatable)
90e3cdf2 3245 {
699733f6
AM
3246 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3247 arelent *r;
3248 size_t size;
3249 long relcount;
90e3cdf2 3250
468392fb
AM
3251 if (opdsymend == secsymend)
3252 goto done;
3253
699733f6 3254 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3255 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3256 if (relcount == 0)
c9727e01 3257 goto done;
90e3cdf2 3258
7356fed5
AM
3259 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3260 {
3261 count = -1;
3262 goto done;
3263 }
3264
699733f6 3265 size = 0;
595da8c5 3266 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3267 {
3268 asymbol *sym;
90e3cdf2 3269
595da8c5 3270 while (r < opd->relocation + relcount
699733f6
AM
3271 && r->address < syms[i]->value + opd->vma)
3272 ++r;
90e3cdf2 3273
595da8c5 3274 if (r == opd->relocation + relcount)
699733f6 3275 break;
90e3cdf2 3276
699733f6
AM
3277 if (r->address != syms[i]->value + opd->vma)
3278 continue;
90e3cdf2 3279
699733f6
AM
3280 if (r->howto->type != R_PPC64_ADDR64)
3281 continue;
90e3cdf2 3282
699733f6
AM
3283 sym = *r->sym_ptr_ptr;
3284 if (!sym_exists_at (syms, opdsymend, symcount,
3285 sym->section->id, sym->value + r->addend))
3286 {
3287 ++count;
3288 size += sizeof (asymbol);
3289 size += strlen (syms[i]->name) + 2;
3290 }
3291 }
90e3cdf2 3292
c4b0b099
AM
3293 if (size == 0)
3294 goto done;
699733f6
AM
3295 s = *ret = bfd_malloc (size);
3296 if (s == NULL)
3297 {
7356fed5 3298 count = -1;
c9727e01 3299 goto done;
699733f6 3300 }
90e3cdf2 3301
699733f6 3302 names = (char *) (s + count);
90e3cdf2 3303
595da8c5 3304 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3305 {
699733f6 3306 asymbol *sym;
90e3cdf2 3307
595da8c5 3308 while (r < opd->relocation + relcount
699733f6
AM
3309 && r->address < syms[i]->value + opd->vma)
3310 ++r;
90e3cdf2 3311
595da8c5 3312 if (r == opd->relocation + relcount)
699733f6
AM
3313 break;
3314
3315 if (r->address != syms[i]->value + opd->vma)
3316 continue;
3317
3318 if (r->howto->type != R_PPC64_ADDR64)
3319 continue;
90e3cdf2 3320
699733f6
AM
3321 sym = *r->sym_ptr_ptr;
3322 if (!sym_exists_at (syms, opdsymend, symcount,
3323 sym->section->id, sym->value + r->addend))
3324 {
3325 size_t len;
3326
3327 *s = *syms[i];
6ba2a415 3328 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3329 s->section = sym->section;
3330 s->value = sym->value + r->addend;
3331 s->name = names;
3332 *names++ = '.';
3333 len = strlen (syms[i]->name);
3334 memcpy (names, syms[i]->name, len + 1);
3335 names += len + 1;
6f610d07
UW
3336 /* Have udata.p point back to the original symbol this
3337 synthetic symbol was derived from. */
3338 s->udata.p = syms[i];
699733f6
AM
3339 s++;
3340 }
3341 }
3342 }
3343 else
90e3cdf2 3344 {
468392fb 3345 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3346 bfd_byte *contents = NULL;
699733f6 3347 size_t size;
468392fb
AM
3348 long plt_count = 0;
3349 bfd_vma glink_vma = 0, resolv_vma = 0;
3350 asection *dynamic, *glink = NULL, *relplt = NULL;
3351 arelent *p;
90e3cdf2 3352
ee67d69a 3353 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3354 {
c4b0b099
AM
3355 free_contents_and_exit_err:
3356 count = -1;
ee67d69a 3357 free_contents_and_exit:
699733f6 3358 if (contents)
ee67d69a 3359 free (contents);
c9727e01 3360 goto done;
699733f6 3361 }
90e3cdf2 3362
699733f6
AM
3363 size = 0;
3364 for (i = secsymend; i < opdsymend; ++i)
3365 {
3366 bfd_vma ent;
90e3cdf2 3367
5ef11c02
AM
3368 /* Ignore bogus symbols. */
3369 if (syms[i]->value > opd->size - 8)
3370 continue;
3371
699733f6
AM
3372 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3373 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3374 {
3375 ++count;
3376 size += sizeof (asymbol);
3377 size += strlen (syms[i]->name) + 2;
3378 }
3379 }
90e3cdf2 3380
468392fb 3381 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3382 if (dyn_count != 0
3383 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3384 {
3385 bfd_byte *dynbuf, *extdyn, *extdynend;
3386 size_t extdynsize;
3387 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3388
3389 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3390 goto free_contents_and_exit_err;
468392fb
AM
3391
3392 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3393 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3394
3395 extdyn = dynbuf;
3396 extdynend = extdyn + dynamic->size;
3397 for (; extdyn < extdynend; extdyn += extdynsize)
3398 {
3399 Elf_Internal_Dyn dyn;
3400 (*swap_dyn_in) (abfd, extdyn, &dyn);
3401
3402 if (dyn.d_tag == DT_NULL)
3403 break;
3404
3405 if (dyn.d_tag == DT_PPC64_GLINK)
3406 {
b9e5796b
AM
3407 /* The first glink stub starts at offset 32; see
3408 comment in ppc64_elf_finish_dynamic_sections. */
3409 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3410 /* The .glink section usually does not survive the final
3411 link; search for the section (usually .text) where the
3412 glink stubs now reside. */
3413 glink = bfd_sections_find_if (abfd, section_covers_vma,
3414 &glink_vma);
3415 break;
3416 }
3417 }
3418
3419 free (dynbuf);
3420 }
3421
3422 if (glink != NULL)
3423 {
3424 /* Determine __glink trampoline by reading the relative branch
3425 from the first glink stub. */
3426 bfd_byte buf[4];
b9e5796b
AM
3427 unsigned int off = 0;
3428
3429 while (bfd_get_section_contents (abfd, glink, buf,
3430 glink_vma + off - glink->vma, 4))
468392fb
AM
3431 {
3432 unsigned int insn = bfd_get_32 (abfd, buf);
3433 insn ^= B_DOT;
3434 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3435 {
3436 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3437 break;
3438 }
3439 off += 4;
3440 if (off > 4)
3441 break;
468392fb
AM
3442 }
3443
3444 if (resolv_vma)
3445 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3446
066ee829
AM
3447 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3448 if (relplt != NULL)
3449 {
3450 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3451 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3452 goto free_contents_and_exit_err;
68ffbac6 3453
066ee829
AM
3454 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3455 size += plt_count * sizeof (asymbol);
468392fb 3456
066ee829
AM
3457 p = relplt->relocation;
3458 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3459 {
3460 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3461 if (p->addend != 0)
3462 size += sizeof ("+0x") - 1 + 16;
3463 }
066ee829 3464 }
468392fb
AM
3465 }
3466
c4b0b099
AM
3467 if (size == 0)
3468 goto free_contents_and_exit;
699733f6
AM
3469 s = *ret = bfd_malloc (size);
3470 if (s == NULL)
c4b0b099 3471 goto free_contents_and_exit_err;
90e3cdf2 3472
468392fb 3473 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3474
699733f6 3475 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3476 {
699733f6 3477 bfd_vma ent;
90e3cdf2 3478
5ef11c02
AM
3479 if (syms[i]->value > opd->size - 8)
3480 continue;
3481
699733f6
AM
3482 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3483 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3484 {
c9727e01 3485 long lo, hi;
699733f6 3486 size_t len;
c9727e01 3487 asection *sec = abfd->sections;
90e3cdf2 3488
699733f6
AM
3489 *s = *syms[i];
3490 lo = codesecsym;
3491 hi = codesecsymend;
3492 while (lo < hi)
3493 {
c9727e01 3494 long mid = (lo + hi) >> 1;
699733f6
AM
3495 if (syms[mid]->section->vma < ent)
3496 lo = mid + 1;
3497 else if (syms[mid]->section->vma > ent)
3498 hi = mid;
3499 else
c9727e01
AM
3500 {
3501 sec = syms[mid]->section;
3502 break;
3503 }
699733f6
AM
3504 }
3505
c9727e01 3506 if (lo >= hi && lo > codesecsym)
699733f6 3507 sec = syms[lo - 1]->section;
699733f6
AM
3508
3509 for (; sec != NULL; sec = sec->next)
3510 {
3511 if (sec->vma > ent)
3512 break;
63524580
JK
3513 /* SEC_LOAD may not be set if SEC is from a separate debug
3514 info file. */
3515 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3516 break;
3517 if ((sec->flags & SEC_CODE) != 0)
3518 s->section = sec;
3519 }
6ba2a415 3520 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3521 s->value = ent - s->section->vma;
3522 s->name = names;
3523 *names++ = '.';
3524 len = strlen (syms[i]->name);
3525 memcpy (names, syms[i]->name, len + 1);
3526 names += len + 1;
6f610d07
UW
3527 /* Have udata.p point back to the original symbol this
3528 synthetic symbol was derived from. */
3529 s->udata.p = syms[i];
699733f6 3530 s++;
90e3cdf2 3531 }
90e3cdf2 3532 }
699733f6 3533 free (contents);
468392fb
AM
3534
3535 if (glink != NULL && relplt != NULL)
3536 {
3537 if (resolv_vma)
3538 {
3539 /* Add a symbol for the main glink trampoline. */
86a4952b 3540 memset (s, 0, sizeof *s);
468392fb 3541 s->the_bfd = abfd;
6ba2a415 3542 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3543 s->section = glink;
3544 s->value = resolv_vma - glink->vma;
3545 s->name = names;
3546 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3547 names += sizeof ("__glink_PLTresolve");
3548 s++;
3549 count++;
3550 }
3551
3552 /* FIXME: It would be very much nicer to put sym@plt on the
3553 stub rather than on the glink branch table entry. The
3554 objdump disassembler would then use a sensible symbol
3555 name on plt calls. The difficulty in doing so is
3556 a) finding the stubs, and,
3557 b) matching stubs against plt entries, and,
3558 c) there can be multiple stubs for a given plt entry.
3559
3560 Solving (a) could be done by code scanning, but older
3561 ppc64 binaries used different stubs to current code.
3562 (b) is the tricky one since you need to known the toc
3563 pointer for at least one function that uses a pic stub to
3564 be able to calculate the plt address referenced.
3565 (c) means gdb would need to set multiple breakpoints (or
3566 find the glink branch itself) when setting breakpoints
3567 for pending shared library loads. */
3568 p = relplt->relocation;
3569 for (i = 0; i < plt_count; i++, p++)
3570 {
3571 size_t len;
3572
3573 *s = **p->sym_ptr_ptr;
3574 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3575 we are defining a symbol, ensure one of them is set. */
3576 if ((s->flags & BSF_LOCAL) == 0)
3577 s->flags |= BSF_GLOBAL;
6ba2a415 3578 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3579 s->section = glink;
3580 s->value = glink_vma - glink->vma;
3581 s->name = names;
3582 s->udata.p = NULL;
3583 len = strlen ((*p->sym_ptr_ptr)->name);
3584 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3585 names += len;
e054468f
AM
3586 if (p->addend != 0)
3587 {
3588 memcpy (names, "+0x", sizeof ("+0x") - 1);
3589 names += sizeof ("+0x") - 1;
3590 bfd_sprintf_vma (abfd, names, p->addend);
3591 names += strlen (names);
3592 }
468392fb
AM
3593 memcpy (names, "@plt", sizeof ("@plt"));
3594 names += sizeof ("@plt");
3595 s++;
b9e5796b
AM
3596 if (abi < 2)
3597 {
3598 glink_vma += 8;
3599 if (i >= 0x8000)
3600 glink_vma += 4;
3601 }
3602 else
468392fb
AM
3603 glink_vma += 4;
3604 }
3605 count += plt_count;
3606 }
90e3cdf2
JJ
3607 }
3608
c9727e01 3609 done:
a7535cf3 3610 free (syms);
90e3cdf2
JJ
3611 return count;
3612}
5bd4f169 3613\f
65f38f15
AM
3614/* The following functions are specific to the ELF linker, while
3615 functions above are used generally. Those named ppc64_elf_* are
3616 called by the main ELF linker code. They appear in this file more
3617 or less in the order in which they are called. eg.
3618 ppc64_elf_check_relocs is called early in the link process,
3619 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3620 called.
3621
3622 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3623 functions have both a function code symbol and a function descriptor
3624 symbol. A call to foo in a relocatable object file looks like:
3625
3626 . .text
3627 . x:
3628 . bl .foo
3629 . nop
3630
3631 The function definition in another object file might be:
3632
3633 . .section .opd
3634 . foo: .quad .foo
3635 . .quad .TOC.@tocbase
3636 . .quad 0
3637 .
3638 . .text
3639 . .foo: blr
3640
3641 When the linker resolves the call during a static link, the branch
3642 unsurprisingly just goes to .foo and the .opd information is unused.
3643 If the function definition is in a shared library, things are a little
3644 different: The call goes via a plt call stub, the opd information gets
3645 copied to the plt, and the linker patches the nop.
3646
3647 . x:
3648 . bl .foo_stub
3649 . ld 2,40(1)
3650 .
3651 .
3652 . .foo_stub:
71a39c98
AM
3653 . std 2,40(1) # in practice, the call stub
3654 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3655 . addi 11,11,Lfoo@toc@l # this is the general idea
3656 . ld 12,0(11)
3657 . ld 2,8(11)
3658 . mtctr 12
3659 . ld 11,16(11)
e86ce104
AM
3660 . bctr
3661 .
3662 . .section .plt
3663 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3664
3665 The "reloc ()" notation is supposed to indicate that the linker emits
3666 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3667 copying.
3668
3669 What are the difficulties here? Well, firstly, the relocations
3670 examined by the linker in check_relocs are against the function code
3671 sym .foo, while the dynamic relocation in the plt is emitted against
3672 the function descriptor symbol, foo. Somewhere along the line, we need
3673 to carefully copy dynamic link information from one symbol to the other.
3674 Secondly, the generic part of the elf linker will make .foo a dynamic
3675 symbol as is normal for most other backends. We need foo dynamic
3676 instead, at least for an application final link. However, when
3677 creating a shared library containing foo, we need to have both symbols
3678 dynamic so that references to .foo are satisfied during the early
3679 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3680 definition from some other object, eg. a static library.
3681
3682 Update: As of August 2004, we support a new convention. Function
3683 calls may use the function descriptor symbol, ie. "bl foo". This
3684 behaves exactly as "bl .foo". */
65f38f15 3685
1d483afe 3686/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3687 selects those that must be copied when linking a shared library,
3688 even when the symbol is local. */
65f38f15 3689
1d483afe
AM
3690static int
3691must_be_dyn_reloc (struct bfd_link_info *info,
3692 enum elf_ppc64_reloc_type r_type)
3693{
3694 switch (r_type)
3695 {
3696 default:
3697 return 1;
3698
3699 case R_PPC64_REL32:
3700 case R_PPC64_REL64:
3701 case R_PPC64_REL30:
3702 return 0;
3703
3704 case R_PPC64_TPREL16:
3705 case R_PPC64_TPREL16_LO:
3706 case R_PPC64_TPREL16_HI:
3707 case R_PPC64_TPREL16_HA:
3708 case R_PPC64_TPREL16_DS:
3709 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3710 case R_PPC64_TPREL16_HIGH:
3711 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3712 case R_PPC64_TPREL16_HIGHER:
3713 case R_PPC64_TPREL16_HIGHERA:
3714 case R_PPC64_TPREL16_HIGHEST:
3715 case R_PPC64_TPREL16_HIGHESTA:
3716 case R_PPC64_TPREL64:
0e1862bb 3717 return !bfd_link_executable (info);
1d483afe
AM
3718 }
3719}
65f38f15 3720
f4656909
AM
3721/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3722 copying dynamic variables from a shared lib into an app's dynbss
3723 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3724 shared lib. With code that gcc generates, it's vital that this be
3725 enabled; In the PowerPC64 ABI, the address of a function is actually
3726 the address of a function descriptor, which resides in the .opd
3727 section. gcc uses the descriptor directly rather than going via the
3728 GOT as some other ABI's do, which means that initialized function
3729 pointers must reference the descriptor. Thus, a function pointer
3730 initialized to the address of a function in a shared library will
3731 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3732 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3733 presents a problem as a plt entry for that function is also
3734 initialized from the function descriptor symbol and the copy reloc
3735 may not be initialized first. */
a23b6845 3736#define ELIMINATE_COPY_RELOCS 1
f4656909 3737
721956f4 3738/* Section name for stubs is the associated section name plus this
29942be8
NC
3739 string. */
3740#define STUB_SUFFIX ".stub"
721956f4
AM
3741
3742/* Linker stubs.
3743 ppc_stub_long_branch:
3744 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3745 destination, but a 24 bit branch in a stub section will reach.
3746 . b dest
3747
3748 ppc_stub_plt_branch:
3749 Similar to the above, but a 24 bit branch in the stub section won't
3750 reach its destination.
71a39c98
AM
3751 . addis %r11,%r2,xxx@toc@ha
3752 . ld %r12,xxx@toc@l(%r11)
3753 . mtctr %r12
721956f4
AM
3754 . bctr
3755
3756 ppc_stub_plt_call:
2c66dc6c
AM
3757 Used to call a function in a shared library. If it so happens that
3758 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3759 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3760 . std %r2,40(%r1)
71a39c98
AM
3761 . addis %r11,%r2,xxx@toc@ha
3762 . ld %r12,xxx+0@toc@l(%r11)
3763 . mtctr %r12
3764 . ld %r2,xxx+8@toc@l(%r11)
3765 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3766 . bctr
ad8e1ba5
AM
3767
3768 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3769 code to adjust the value and save r2 to support multiple toc sections.
3770 A ppc_stub_long_branch with an r2 offset looks like:
3771 . std %r2,40(%r1)
3772 . addis %r2,%r2,off@ha
3773 . addi %r2,%r2,off@l
3774 . b dest
3775
3776 A ppc_stub_plt_branch with an r2 offset looks like:
3777 . std %r2,40(%r1)
71a39c98
AM
3778 . addis %r11,%r2,xxx@toc@ha
3779 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3780 . addis %r2,%r2,off@ha
3781 . addi %r2,%r2,off@l
71a39c98 3782 . mtctr %r12
ad8e1ba5 3783 . bctr
ac2df442
AM
3784
3785 In cases where the "addis" instruction would add zero, the "addis" is
3786 omitted and following instructions modified slightly in some cases.
721956f4
AM
3787*/
3788
3789enum ppc_stub_type {
3790 ppc_stub_none,
3791 ppc_stub_long_branch,
ad8e1ba5 3792 ppc_stub_long_branch_r2off,
721956f4 3793 ppc_stub_plt_branch,
ad8e1ba5 3794 ppc_stub_plt_branch_r2off,
794e51c0 3795 ppc_stub_plt_call,
7341d5e2 3796 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3797 ppc_stub_global_entry,
3798 ppc_stub_save_res
721956f4
AM
3799};
3800
6f20ed8a
AM
3801/* Information on stub grouping. */
3802struct map_stub
3803{
3804 /* The stub section. */
3805 asection *stub_sec;
3806 /* This is the section to which stubs in the group will be attached. */
3807 asection *link_sec;
a4b6fadd
AM
3808 /* Next group. */
3809 struct map_stub *next;
3810 /* Whether to emit a copy of register save/restore functions in this
3811 group. */
3812 int needs_save_res;
6f20ed8a
AM
3813};
3814
721956f4
AM
3815struct ppc_stub_hash_entry {
3816
3817 /* Base hash table entry structure. */
3818 struct bfd_hash_entry root;
3819
ad8e1ba5
AM
3820 enum ppc_stub_type stub_type;
3821
6f20ed8a
AM
3822 /* Group information. */
3823 struct map_stub *group;
721956f4
AM
3824
3825 /* Offset within stub_sec of the beginning of this stub. */
3826 bfd_vma stub_offset;
3827
3828 /* Given the symbol's value and its section we can determine its final
3829 value when building the stubs (so the stub knows where to jump. */
3830 bfd_vma target_value;
3831 asection *target_section;
3832
721956f4
AM
3833 /* The symbol table entry, if any, that this was derived from. */
3834 struct ppc_link_hash_entry *h;
e054468f 3835 struct plt_entry *plt_ent;
721956f4 3836
6911b7dc
AM
3837 /* Symbol st_other. */
3838 unsigned char other;
721956f4
AM
3839};
3840
3841struct ppc_branch_hash_entry {
3842
3843 /* Base hash table entry structure. */
3844 struct bfd_hash_entry root;
3845
c456f082 3846 /* Offset within branch lookup table. */
721956f4
AM
3847 unsigned int offset;
3848
3849 /* Generation marker. */
3850 unsigned int iter;
3851};
65f38f15 3852
19e08130
AM
3853/* Used to track dynamic relocations for local symbols. */
3854struct ppc_dyn_relocs
3855{
3856 struct ppc_dyn_relocs *next;
3857
3858 /* The input section of the reloc. */
3859 asection *sec;
3860
3861 /* Total number of relocs copied for the input section. */
3862 unsigned int count : 31;
3863
3864 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3865 unsigned int ifunc : 1;
3866};
3867
65f38f15
AM
3868struct ppc_link_hash_entry
3869{
3870 struct elf_link_hash_entry elf;
3871
b3fac117
AM
3872 union {
3873 /* A pointer to the most recently used stub hash entry against this
3874 symbol. */
3875 struct ppc_stub_hash_entry *stub_cache;
3876
3877 /* A pointer to the next symbol starting with a '.' */
3878 struct ppc_link_hash_entry *next_dot_sym;
3879 } u;
721956f4 3880
65f38f15 3881 /* Track dynamic relocs copied for this symbol. */
6061a67d 3882 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3883
721956f4 3884 /* Link between function code and descriptor symbols. */
34814b9f 3885 struct ppc_link_hash_entry *oh;
721956f4 3886
e86ce104
AM
3887 /* Flag function code and descriptor symbols. */
3888 unsigned int is_func:1;
3889 unsigned int is_func_descriptor:1;
908b32fc 3890 unsigned int fake:1;
411e1bfb 3891
c5614fa4
AM
3892 /* Whether global opd/toc sym has been adjusted or not.
3893 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3894 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3895 unsigned int adjust_done:1;
3896
99877b66
AM
3897 /* Set if we twiddled this symbol to weak at some stage. */
3898 unsigned int was_undefined:1;
3899
a4b6fadd
AM
3900 /* Set if this is an out-of-line register save/restore function,
3901 with non-standard calling convention. */
3902 unsigned int save_res:1;
3903
411e1bfb 3904 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3905 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3906 corresponding relocs are encountered during check_relocs.
3907 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3908 indicate the corresponding GOT entry type is not needed.
3909 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3910 a TPREL one. We use a separate flag rather than setting TPREL
3911 just for convenience in distinguishing the two cases. */
3912#define TLS_GD 1 /* GD reloc. */
3913#define TLS_LD 2 /* LD reloc. */
3914#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3915#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3916#define TLS_TLS 16 /* Any TLS reloc. */
3917#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3918#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3919#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3920 unsigned char tls_mask;
65f38f15
AM
3921};
3922
3923/* ppc64 ELF linker hash table. */
3924
3925struct ppc_link_hash_table
3926{
3927 struct elf_link_hash_table elf;
3928
721956f4
AM
3929 /* The stub hash table. */
3930 struct bfd_hash_table stub_hash_table;
3931
3932 /* Another hash table for plt_branch stubs. */
3933 struct bfd_hash_table branch_hash_table;
3934
3b421ab3
AM
3935 /* Hash table for function prologue tocsave. */
3936 htab_t tocsave_htab;
3937
e7d1c40c
AM
3938 /* Various options and other info passed from the linker. */
3939 struct ppc64_elf_params *params;
721956f4 3940
6f20ed8a
AM
3941 /* The size of sec_info below. */
3942 unsigned int sec_info_arr_size;
3943
3944 /* Per-section array of extra section info. Done this way rather
3945 than as part of ppc64_elf_section_data so we have the info for
3946 non-ppc64 sections. */
3947 struct
3948 {
3949 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3950 bfd_vma toc_off;
6f20ed8a
AM
3951
3952 union
3953 {
3954 /* The section group that this section belongs to. */
3955 struct map_stub *group;
3956 /* A temp section list pointer. */
3957 asection *list;
3958 } u;
3959 } *sec_info;
721956f4 3960
a4b6fadd
AM
3961 /* Linked list of groups. */
3962 struct map_stub *group;
3963
ad8e1ba5
AM
3964 /* Temp used when calculating TOC pointers. */
3965 bfd_vma toc_curr;
bf102f86
AM
3966 bfd *toc_bfd;
3967 asection *toc_first_sec;
ad8e1ba5 3968
b3fac117
AM
3969 /* Used when adding symbols. */
3970 struct ppc_link_hash_entry *dot_syms;
3971
33e44f2e 3972 /* Shortcuts to get to dynamic linker sections. */
4ce794b7
AM
3973 asection *dynbss;
3974 asection *relbss;
3975 asection *glink;
82bd7b59 3976 asection *sfpr;
4ce794b7
AM
3977 asection *brlt;
3978 asection *relbrlt;
58d180e8 3979 asection *glink_eh_frame;
ec338859 3980
8387904d
AM
3981 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3982 struct ppc_link_hash_entry *tls_get_addr;
3983 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3984
927be08e
AM
3985 /* The size of reliplt used by got entry relocs. */
3986 bfd_size_type got_reli_size;
3987
9b5ecbd0 3988 /* Statistics. */
7341d5e2 3989 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3990
ee75fd95
AM
3991 /* Number of stubs against global syms. */
3992 unsigned long stub_globals;
3993
ee67d69a
AM
3994 /* Set if we're linking code with function descriptors. */
3995 unsigned int opd_abi:1;
3996
4c52953f 3997 /* Support for multiple toc sections. */
33c0ec9d 3998 unsigned int do_multi_toc:1;
4c52953f 3999 unsigned int multi_toc_needed:1;
927be08e 4000 unsigned int second_toc_pass:1;
67f0cbdb 4001 unsigned int do_toc_opt:1;
4c52953f 4002
5d1634d7 4003 /* Set on error. */
99877b66 4004 unsigned int stub_error:1;
721956f4 4005
f6c7c3e8 4006 /* Temp used by ppc64_elf_before_check_relocs. */
99877b66 4007 unsigned int twiddled_syms:1;
721956f4
AM
4008
4009 /* Incremented every time we size stubs. */
4010 unsigned int stub_iteration;
5d1634d7 4011
87d72d41
AM
4012 /* Small local sym cache. */
4013 struct sym_cache sym_cache;
65f38f15
AM
4014};
4015
4c52953f
AM
4016/* Rename some of the generic section flags to better document how they
4017 are used here. */
b0dddeec
AM
4018
4019/* Nonzero if this section has TLS related relocations. */
4020#define has_tls_reloc sec_flg0
4021
4022/* Nonzero if this section has a call to __tls_get_addr. */
4023#define has_tls_get_addr_call sec_flg1
4024
4025/* Nonzero if this section has any toc or got relocs. */
4026#define has_toc_reloc sec_flg2
4027
4028/* Nonzero if this section has a call to another section that uses
4029 the toc or got. */
d77c8a4b 4030#define makes_toc_func_call sec_flg3
b0dddeec
AM
4031
4032/* Recursion protection when determining above flag. */
d77c8a4b 4033#define call_check_in_progress sec_flg4
70cc837d 4034#define call_check_done sec_flg5
4c52953f 4035
65f38f15
AM
4036/* Get the ppc64 ELF linker hash table from a link_info structure. */
4037
4038#define ppc_hash_table(p) \
4dfe6ac6
NC
4039 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4040 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4041
721956f4
AM
4042#define ppc_stub_hash_lookup(table, string, create, copy) \
4043 ((struct ppc_stub_hash_entry *) \
4044 bfd_hash_lookup ((table), (string), (create), (copy)))
4045
4046#define ppc_branch_hash_lookup(table, string, create, copy) \
4047 ((struct ppc_branch_hash_entry *) \
4048 bfd_hash_lookup ((table), (string), (create), (copy)))
4049
4050/* Create an entry in the stub hash table. */
4051
4052static struct bfd_hash_entry *
4ce794b7
AM
4053stub_hash_newfunc (struct bfd_hash_entry *entry,
4054 struct bfd_hash_table *table,
4055 const char *string)
721956f4
AM
4056{
4057 /* Allocate the structure if it has not already been allocated by a
4058 subclass. */
4059 if (entry == NULL)
4060 {
4061 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4062 if (entry == NULL)
4063 return entry;
4064 }
4065
4066 /* Call the allocation method of the superclass. */
4067 entry = bfd_hash_newfunc (entry, table, string);
4068 if (entry != NULL)
4069 {
4070 struct ppc_stub_hash_entry *eh;
4071
4072 /* Initialize the local fields. */
4073 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4074 eh->stub_type = ppc_stub_none;
6f20ed8a 4075 eh->group = NULL;
721956f4
AM
4076 eh->stub_offset = 0;
4077 eh->target_value = 0;
4078 eh->target_section = NULL;
721956f4 4079 eh->h = NULL;
6911b7dc 4080 eh->plt_ent = NULL;
6911b7dc 4081 eh->other = 0;
721956f4
AM
4082 }
4083
4084 return entry;
4085}
4086
4087/* Create an entry in the branch hash table. */
4088
4089static struct bfd_hash_entry *
4ce794b7
AM
4090branch_hash_newfunc (struct bfd_hash_entry *entry,
4091 struct bfd_hash_table *table,
4092 const char *string)
721956f4
AM
4093{
4094 /* Allocate the structure if it has not already been allocated by a
4095 subclass. */
4096 if (entry == NULL)
4097 {
4098 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4099 if (entry == NULL)
4100 return entry;
4101 }
4102
4103 /* Call the allocation method of the superclass. */
4104 entry = bfd_hash_newfunc (entry, table, string);
4105 if (entry != NULL)
4106 {
4107 struct ppc_branch_hash_entry *eh;
4108
4109 /* Initialize the local fields. */
4110 eh = (struct ppc_branch_hash_entry *) entry;
4111 eh->offset = 0;
4112 eh->iter = 0;
4113 }
4114
4115 return entry;
4116}
4117
65f38f15
AM
4118/* Create an entry in a ppc64 ELF linker hash table. */
4119
4120static struct bfd_hash_entry *
4ce794b7
AM
4121link_hash_newfunc (struct bfd_hash_entry *entry,
4122 struct bfd_hash_table *table,
4123 const char *string)
65f38f15
AM
4124{
4125 /* Allocate the structure if it has not already been allocated by a
4126 subclass. */
4127 if (entry == NULL)
4128 {
4129 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4130 if (entry == NULL)
4131 return entry;
4132 }
4133
4134 /* Call the allocation method of the superclass. */
4135 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4136 if (entry != NULL)
4137 {
4138 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4139
b3fac117 4140 memset (&eh->u.stub_cache, 0,
908b32fc 4141 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4142 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4143
4144 /* When making function calls, old ABI code references function entry
4145 points (dot symbols), while new ABI code references the function
4146 descriptor symbol. We need to make any combination of reference and
4147 definition work together, without breaking archive linking.
4148
4149 For a defined function "foo" and an undefined call to "bar":
4150 An old object defines "foo" and ".foo", references ".bar" (possibly
4151 "bar" too).
4152 A new object defines "foo" and references "bar".
4153
4154 A new object thus has no problem with its undefined symbols being
4155 satisfied by definitions in an old object. On the other hand, the
4156 old object won't have ".bar" satisfied by a new object.
4157
4158 Keep a list of newly added dot-symbols. */
4159
4160 if (string[0] == '.')
4161 {
4162 struct ppc_link_hash_table *htab;
4163
4164 htab = (struct ppc_link_hash_table *) table;
4165 eh->u.next_dot_sym = htab->dot_syms;
4166 htab->dot_syms = eh;
4167 }
65f38f15
AM
4168 }
4169
4170 return entry;
4171}
4172
3b421ab3
AM
4173struct tocsave_entry {
4174 asection *sec;
4175 bfd_vma offset;
4176};
4177
4178static hashval_t
4179tocsave_htab_hash (const void *p)
4180{
4181 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4182 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4183}
4184
4185static int
4186tocsave_htab_eq (const void *p1, const void *p2)
4187{
4188 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4189 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4190 return e1->sec == e2->sec && e1->offset == e2->offset;
4191}
4192
68faa637
AM
4193/* Destroy a ppc64 ELF linker hash table. */
4194
4195static void
d495ab0d 4196ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4197{
d495ab0d 4198 struct ppc_link_hash_table *htab;
68faa637 4199
d495ab0d 4200 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4201 if (htab->tocsave_htab)
4202 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4203 bfd_hash_table_free (&htab->branch_hash_table);
4204 bfd_hash_table_free (&htab->stub_hash_table);
4205 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4206}
4207
65f38f15
AM
4208/* Create a ppc64 ELF linker hash table. */
4209
4210static struct bfd_link_hash_table *
4ce794b7 4211ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4212{
4213 struct ppc_link_hash_table *htab;
4214 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4215
4ce794b7 4216 htab = bfd_zmalloc (amt);
65f38f15
AM
4217 if (htab == NULL)
4218 return NULL;
4219
66eb6687 4220 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4221 sizeof (struct ppc_link_hash_entry),
4222 PPC64_ELF_DATA))
65f38f15 4223 {
e2d34d7d 4224 free (htab);
65f38f15
AM
4225 return NULL;
4226 }
4227
721956f4 4228 /* Init the stub hash table too. */
66eb6687
AM
4229 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4230 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4231 {
d495ab0d 4232 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4233 return NULL;
4234 }
721956f4
AM
4235
4236 /* And the branch hash table. */
66eb6687
AM
4237 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4238 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4239 {
4240 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4241 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4242 return NULL;
4243 }
721956f4 4244
3b421ab3
AM
4245 htab->tocsave_htab = htab_try_create (1024,
4246 tocsave_htab_hash,
4247 tocsave_htab_eq,
4248 NULL);
4249 if (htab->tocsave_htab == NULL)
2915c55b 4250 {
d495ab0d 4251 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4252 return NULL;
4253 }
d495ab0d 4254 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4255
3254fd24
AM
4256 /* Initializing two fields of the union is just cosmetic. We really
4257 only care about glist, but when compiled on a 32-bit host the
4258 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4259 debugger inspection of these fields look nicer. */
a6aa5195
AM
4260 htab->elf.init_got_refcount.refcount = 0;
4261 htab->elf.init_got_refcount.glist = NULL;
4262 htab->elf.init_plt_refcount.refcount = 0;
4263 htab->elf.init_plt_refcount.glist = NULL;
4264 htab->elf.init_got_offset.offset = 0;
4265 htab->elf.init_got_offset.glist = NULL;
4266 htab->elf.init_plt_offset.offset = 0;
4267 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4268
65f38f15
AM
4269 return &htab->elf.root;
4270}
4271
bfeb4a28
AM
4272/* Create sections for linker generated code. */
4273
4274static bfd_boolean
4275create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4276{
4277 struct ppc_link_hash_table *htab;
4278 flagword flags;
4279
4280 htab = ppc_hash_table (info);
4281
4282 /* Create .sfpr for code to save and restore fp regs. */
4283 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4284 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4285 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4286 flags);
4287 if (htab->sfpr == NULL
4288 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4289 return FALSE;
4290
4291 /* Create .glink for lazy dynamic linking support. */
4292 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4293 flags);
4294 if (htab->glink == NULL
4295 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4296 return FALSE;
4297
4298 if (!info->no_ld_generated_unwind_info)
4299 {
4300 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4301 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4302 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4303 ".eh_frame",
4304 flags);
4305 if (htab->glink_eh_frame == NULL
4306 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4307 return FALSE;
4308 }
4309
4310 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4311 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4312 if (htab->elf.iplt == NULL
4313 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4314 return FALSE;
4315
4316 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4317 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4318 htab->elf.irelplt
4319 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4320 if (htab->elf.irelplt == NULL
4321 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4322 return FALSE;
4323
4324 /* Create branch lookup table for plt_branch stubs. */
4325 flags = (SEC_ALLOC | SEC_LOAD
4326 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4327 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4328 flags);
4329 if (htab->brlt == NULL
4330 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4331 return FALSE;
4332
0e1862bb 4333 if (!bfd_link_pic (info))
bfeb4a28
AM
4334 return TRUE;
4335
4336 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4337 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4338 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4339 ".rela.branch_lt",
4340 flags);
4341 if (htab->relbrlt == NULL
4342 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4343 return FALSE;
4344
4345 return TRUE;
4346}
4347
e717da7e
AM
4348/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4349
bfeb4a28 4350bfd_boolean
e7d1c40c
AM
4351ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4352 struct ppc64_elf_params *params)
e717da7e
AM
4353{
4354 struct ppc_link_hash_table *htab;
4355
e7d1c40c 4356 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4357
4358/* Always hook our dynamic sections into the first bfd, which is the
4359 linker created stub bfd. This ensures that the GOT header is at
4360 the start of the output TOC section. */
4361 htab = ppc_hash_table (info);
4dfe6ac6 4362 if (htab == NULL)
bfeb4a28 4363 return FALSE;
e7d1c40c
AM
4364 htab->elf.dynobj = params->stub_bfd;
4365 htab->params = params;
bfeb4a28 4366
0e1862bb 4367 if (bfd_link_relocatable (info))
bfeb4a28
AM
4368 return TRUE;
4369
4370 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4371}
4372
721956f4
AM
4373/* Build a name for an entry in the stub hash table. */
4374
4375static char *
4ce794b7
AM
4376ppc_stub_name (const asection *input_section,
4377 const asection *sym_sec,
4378 const struct ppc_link_hash_entry *h,
4379 const Elf_Internal_Rela *rel)
721956f4
AM
4380{
4381 char *stub_name;
bcaa2f82 4382 ssize_t len;
721956f4
AM
4383
4384 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4385 offsets from a sym as a branch target? In fact, we could
4386 probably assume the addend is always zero. */
4387 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4388
4389 if (h)
4390 {
4391 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4392 stub_name = bfd_malloc (len);
46de2a7c
AM
4393 if (stub_name == NULL)
4394 return stub_name;
4395
bcaa2f82
AM
4396 len = sprintf (stub_name, "%08x.%s+%x",
4397 input_section->id & 0xffffffff,
4398 h->elf.root.root.string,
4399 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4400 }
4401 else
4402 {
ad8e1ba5 4403 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4404 stub_name = bfd_malloc (len);
46de2a7c
AM
4405 if (stub_name == NULL)
4406 return stub_name;
4407
bcaa2f82
AM
4408 len = sprintf (stub_name, "%08x.%x:%x+%x",
4409 input_section->id & 0xffffffff,
4410 sym_sec->id & 0xffffffff,
4411 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4412 (int) rel->r_addend & 0xffffffff);
721956f4 4413 }
bcaa2f82 4414 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4415 stub_name[len - 2] = 0;
721956f4
AM
4416 return stub_name;
4417}
4418
4419/* Look up an entry in the stub hash. Stub entries are cached because
4420 creating the stub name takes a bit of time. */
4421
4422static struct ppc_stub_hash_entry *
4ce794b7
AM
4423ppc_get_stub_entry (const asection *input_section,
4424 const asection *sym_sec,
039b3fef 4425 struct ppc_link_hash_entry *h,
4ce794b7
AM
4426 const Elf_Internal_Rela *rel,
4427 struct ppc_link_hash_table *htab)
721956f4
AM
4428{
4429 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4430 struct map_stub *group;
721956f4
AM
4431
4432 /* If this input section is part of a group of sections sharing one
4433 stub section, then use the id of the first section in the group.
4434 Stub names need to include a section id, as there may well be
4435 more than one stub used to reach say, printf, and we need to
4436 distinguish between them. */
6f20ed8a 4437 group = htab->sec_info[input_section->id].u.group;
721956f4 4438
b3fac117
AM
4439 if (h != NULL && h->u.stub_cache != NULL
4440 && h->u.stub_cache->h == h
6f20ed8a 4441 && h->u.stub_cache->group == group)
721956f4 4442 {
b3fac117 4443 stub_entry = h->u.stub_cache;
721956f4
AM
4444 }
4445 else
4446 {
4447 char *stub_name;
4448
6f20ed8a 4449 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4450 if (stub_name == NULL)
4451 return NULL;
4452
4453 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4454 stub_name, FALSE, FALSE);
721956f4 4455 if (h != NULL)
b3fac117 4456 h->u.stub_cache = stub_entry;
721956f4
AM
4457
4458 free (stub_name);
4459 }
4460
4461 return stub_entry;
4462}
4463
4464/* Add a new stub entry to the stub hash. Not all fields of the new
4465 stub entry are initialised. */
4466
4467static struct ppc_stub_hash_entry *
4ce794b7
AM
4468ppc_add_stub (const char *stub_name,
4469 asection *section,
25f53a85 4470 struct bfd_link_info *info)
721956f4 4471{
25f53a85 4472 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4473 struct map_stub *group;
721956f4
AM
4474 asection *link_sec;
4475 asection *stub_sec;
4476 struct ppc_stub_hash_entry *stub_entry;
4477
6f20ed8a
AM
4478 group = htab->sec_info[section->id].u.group;
4479 link_sec = group->link_sec;
4480 stub_sec = group->stub_sec;
721956f4
AM
4481 if (stub_sec == NULL)
4482 {
6f20ed8a
AM
4483 size_t namelen;
4484 bfd_size_type len;
4485 char *s_name;
721956f4 4486
6f20ed8a
AM
4487 namelen = strlen (link_sec->name);
4488 len = namelen + sizeof (STUB_SUFFIX);
4489 s_name = bfd_alloc (htab->params->stub_bfd, len);
4490 if (s_name == NULL)
4491 return NULL;
721956f4 4492
6f20ed8a
AM
4493 memcpy (s_name, link_sec->name, namelen);
4494 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4495 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4496 if (stub_sec == NULL)
4497 return NULL;
4498 group->stub_sec = stub_sec;
721956f4
AM
4499 }
4500
4501 /* Enter this entry into the linker stub hash table. */
4502 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4503 TRUE, FALSE);
721956f4
AM
4504 if (stub_entry == NULL)
4505 {
8de848d8 4506 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4507 section->owner, stub_name);
721956f4
AM
4508 return NULL;
4509 }
4510
6f20ed8a 4511 stub_entry->group = group;
721956f4 4512 stub_entry->stub_offset = 0;
721956f4
AM
4513 return stub_entry;
4514}
4515
e717da7e
AM
4516/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4517 not already done. */
65f38f15 4518
b34976b6 4519static bfd_boolean
e717da7e 4520create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4521{
e717da7e
AM
4522 asection *got, *relgot;
4523 flagword flags;
4524 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4525
0c8d6e5c 4526 if (!is_ppc64_elf (abfd))
0ffa91dd 4527 return FALSE;
4dfe6ac6
NC
4528 if (htab == NULL)
4529 return FALSE;
0ffa91dd 4530
33e44f2e
AM
4531 if (!htab->elf.sgot
4532 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4533 return FALSE;
e717da7e
AM
4534
4535 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4536 | SEC_LINKER_CREATED);
4537
c456f082 4538 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4539 if (!got
e717da7e
AM
4540 || !bfd_set_section_alignment (abfd, got, 3))
4541 return FALSE;
65f38f15 4542
c456f082
AM
4543 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4544 flags | SEC_READONLY);
e717da7e 4545 if (!relgot
e717da7e 4546 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4547 return FALSE;
e717da7e
AM
4548
4549 ppc64_elf_tdata (abfd)->got = got;
4550 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4551 return TRUE;
65f38f15 4552}
5bd4f169 4553
82bd7b59 4554/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4555
b34976b6 4556static bfd_boolean
4ce794b7 4557ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4558{
65f38f15 4559 struct ppc_link_hash_table *htab;
5bd4f169 4560
65f38f15 4561 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4562 return FALSE;
65f38f15 4563
e717da7e 4564 htab = ppc_hash_table (info);
4dfe6ac6
NC
4565 if (htab == NULL)
4566 return FALSE;
4567
3d4d4302 4568 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
0e1862bb 4569 if (!bfd_link_pic (info))
3d4d4302 4570 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4571
33e44f2e 4572 if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
0e1862bb 4573 || (!bfd_link_pic (info) && !htab->relbss))
65f38f15
AM
4574 abort ();
4575
b34976b6 4576 return TRUE;
5bd4f169
AM
4577}
4578
b31867b6
AM
4579/* Follow indirect and warning symbol links. */
4580
4581static inline struct bfd_link_hash_entry *
4582follow_link (struct bfd_link_hash_entry *h)
4583{
4584 while (h->type == bfd_link_hash_indirect
4585 || h->type == bfd_link_hash_warning)
4586 h = h->u.i.link;
4587 return h;
4588}
4589
4590static inline struct elf_link_hash_entry *
4591elf_follow_link (struct elf_link_hash_entry *h)
4592{
4593 return (struct elf_link_hash_entry *) follow_link (&h->root);
4594}
4595
4596static inline struct ppc_link_hash_entry *
4597ppc_follow_link (struct ppc_link_hash_entry *h)
4598{
4599 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4600}
4601
40d16e0b
AM
4602/* Merge PLT info on FROM with that on TO. */
4603
4604static void
4605move_plt_plist (struct ppc_link_hash_entry *from,
4606 struct ppc_link_hash_entry *to)
4607{
4608 if (from->elf.plt.plist != NULL)
4609 {
4610 if (to->elf.plt.plist != NULL)
4611 {
4612 struct plt_entry **entp;
4613 struct plt_entry *ent;
4614
4615 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4616 {
4617 struct plt_entry *dent;
4618
4619 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4620 if (dent->addend == ent->addend)
4621 {
4622 dent->plt.refcount += ent->plt.refcount;
4623 *entp = ent->next;
4624 break;
4625 }
4626 if (dent == NULL)
4627 entp = &ent->next;
4628 }
4629 *entp = to->elf.plt.plist;
4630 }
4631
4632 to->elf.plt.plist = from->elf.plt.plist;
4633 from->elf.plt.plist = NULL;
4634 }
4635}
4636
65f38f15
AM
4637/* Copy the extra info we tack onto an elf_link_hash_entry. */
4638
4639static void
fcfa13d2
AM
4640ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4641 struct elf_link_hash_entry *dir,
4642 struct elf_link_hash_entry *ind)
65f38f15
AM
4643{
4644 struct ppc_link_hash_entry *edir, *eind;
4645
4646 edir = (struct ppc_link_hash_entry *) dir;
4647 eind = (struct ppc_link_hash_entry *) ind;
4648
c79d6685
AM
4649 edir->is_func |= eind->is_func;
4650 edir->is_func_descriptor |= eind->is_func_descriptor;
4651 edir->tls_mask |= eind->tls_mask;
4652 if (eind->oh != NULL)
4653 edir->oh = ppc_follow_link (eind->oh);
4654
4655 /* If called to transfer flags for a weakdef during processing
4656 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4657 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4658 if (!(ELIMINATE_COPY_RELOCS
4659 && eind->elf.root.type != bfd_link_hash_indirect
4660 && edir->elf.dynamic_adjusted))
4661 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4662
4663 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4664 edir->elf.ref_regular |= eind->elf.ref_regular;
4665 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4666 edir->elf.needs_plt |= eind->elf.needs_plt;
a345bc8d 4667 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4668
411e1bfb 4669 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4670 if (eind->dyn_relocs != NULL)
65f38f15 4671 {
bbd7ec4a
AM
4672 if (edir->dyn_relocs != NULL)
4673 {
6061a67d
AM
4674 struct elf_dyn_relocs **pp;
4675 struct elf_dyn_relocs *p;
bbd7ec4a 4676
fcfa13d2 4677 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4678 list. Merge any entries against the same section. */
4679 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4680 {
6061a67d 4681 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4682
4683 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4684 if (q->sec == p->sec)
4685 {
4686 q->pc_count += p->pc_count;
4687 q->count += p->count;
4688 *pp = p->next;
4689 break;
4690 }
4691 if (q == NULL)
4692 pp = &p->next;
4693 }
4694 *pp = edir->dyn_relocs;
4695 }
4696
65f38f15
AM
4697 edir->dyn_relocs = eind->dyn_relocs;
4698 eind->dyn_relocs = NULL;
4699 }
65f38f15 4700
68ba6d40
AM
4701 /* If we were called to copy over info for a weak sym, that's all.
4702 You might think dyn_relocs need not be copied over; After all,
4703 both syms will be dynamic or both non-dynamic so we're just
68ffbac6 4704 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
68ba6d40
AM
4705 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4706 dyn_relocs in read-only sections, and it does so on what is the
4707 DIR sym here. */
4708 if (eind->elf.root.type != bfd_link_hash_indirect)
4709 return;
4710
81848ca0
AM
4711 /* Copy over got entries that we may have already seen to the
4712 symbol which just became indirect. */
411e1bfb
AM
4713 if (eind->elf.got.glist != NULL)
4714 {
4715 if (edir->elf.got.glist != NULL)
4716 {
4717 struct got_entry **entp;
4718 struct got_entry *ent;
4719
4720 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4721 {
4722 struct got_entry *dent;
4723
4724 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4725 if (dent->addend == ent->addend
e717da7e 4726 && dent->owner == ent->owner
411e1bfb
AM
4727 && dent->tls_type == ent->tls_type)
4728 {
4729 dent->got.refcount += ent->got.refcount;
4730 *entp = ent->next;
4731 break;
4732 }
4733 if (dent == NULL)
4734 entp = &ent->next;
4735 }
4736 *entp = edir->elf.got.glist;
4737 }
4738
4739 edir->elf.got.glist = eind->elf.got.glist;
4740 eind->elf.got.glist = NULL;
4741 }
4742
4743 /* And plt entries. */
40d16e0b 4744 move_plt_plist (eind, edir);
411e1bfb 4745
fcfa13d2 4746 if (eind->elf.dynindx != -1)
411e1bfb 4747 {
fcfa13d2
AM
4748 if (edir->elf.dynindx != -1)
4749 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4750 edir->elf.dynstr_index);
411e1bfb
AM
4751 edir->elf.dynindx = eind->elf.dynindx;
4752 edir->elf.dynstr_index = eind->elf.dynstr_index;
4753 eind->elf.dynindx = -1;
4754 eind->elf.dynstr_index = 0;
4755 }
411e1bfb
AM
4756}
4757
8387904d
AM
4758/* Find the function descriptor hash entry from the given function code
4759 hash entry FH. Link the entries via their OH fields. */
4760
4761static struct ppc_link_hash_entry *
b31867b6 4762lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4763{
4764 struct ppc_link_hash_entry *fdh = fh->oh;
4765
4766 if (fdh == NULL)
4767 {
4768 const char *fd_name = fh->elf.root.root.string + 1;
4769
4770 fdh = (struct ppc_link_hash_entry *)
4771 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4772 if (fdh == NULL)
4773 return fdh;
4774
4775 fdh->is_func_descriptor = 1;
4776 fdh->oh = fh;
4777 fh->is_func = 1;
4778 fh->oh = fdh;
8387904d
AM
4779 }
4780
b31867b6 4781 return ppc_follow_link (fdh);
8387904d
AM
4782}
4783
bb700d78
AM
4784/* Make a fake function descriptor sym for the code sym FH. */
4785
4786static struct ppc_link_hash_entry *
4787make_fdh (struct bfd_link_info *info,
908b32fc 4788 struct ppc_link_hash_entry *fh)
bb700d78
AM
4789{
4790 bfd *abfd;
4791 asymbol *newsym;
4792 struct bfd_link_hash_entry *bh;
4793 struct ppc_link_hash_entry *fdh;
4794
4795 abfd = fh->elf.root.u.undef.abfd;
4796 newsym = bfd_make_empty_symbol (abfd);
4797 newsym->name = fh->elf.root.root.string + 1;
4798 newsym->section = bfd_und_section_ptr;
4799 newsym->value = 0;
908b32fc 4800 newsym->flags = BSF_WEAK;
bb700d78
AM
4801
4802 bh = NULL;
4803 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4804 newsym->flags, newsym->section,
4805 newsym->value, NULL, FALSE, FALSE,
4806 &bh))
4807 return NULL;
4808
4809 fdh = (struct ppc_link_hash_entry *) bh;
4810 fdh->elf.non_elf = 0;
908b32fc
AM
4811 fdh->fake = 1;
4812 fdh->is_func_descriptor = 1;
4813 fdh->oh = fh;
4814 fh->is_func = 1;
4815 fh->oh = fdh;
bb700d78
AM
4816 return fdh;
4817}
4818
8387904d
AM
4819/* Fix function descriptor symbols defined in .opd sections to be
4820 function type. */
555cd476
AM
4821
4822static bfd_boolean
c16153ae 4823ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4824 struct bfd_link_info *info,
555cd476 4825 Elf_Internal_Sym *isym,
6911b7dc 4826 const char **name,
555cd476
AM
4827 flagword *flags ATTRIBUTE_UNUSED,
4828 asection **sec,
b53dfeb2 4829 bfd_vma *value)
555cd476 4830{
f1885d1e
AM
4831 if ((ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4832 || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4833 && (ibfd->flags & DYNAMIC) == 0
4834 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
13a2df29 4835 elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
f64b2e8d 4836
b53dfeb2 4837 if (*sec != NULL
f1885d1e 4838 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4839 {
4840 asection *code_sec;
4841
4842 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4843 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4844 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4845
4846 /* If the symbol is a function defined in .opd, and the function
4847 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4848 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4849 && (*sec)->reloc_count != 0
4850 && opd_entry_value (*sec, *value, &code_sec, NULL,
4851 FALSE) != (bfd_vma) -1
4852 && discarded_section (code_sec))
4853 {
4854 *sec = bfd_und_section_ptr;
4855 isym->st_shndx = SHN_UNDEF;
4856 }
4857 }
dbd1e97e
AM
4858 else if (*sec != NULL
4859 && strcmp ((*sec)->name, ".toc") == 0
4860 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4861 {
4862 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4863 if (htab != NULL)
4864 htab->params->object_in_toc = 1;
4865 }
433817dd 4866
6911b7dc
AM
4867 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4868 {
4869 if (abiversion (ibfd) == 0)
4870 set_abiversion (ibfd, 2);
4871 else if (abiversion (ibfd) == 1)
4872 {
4873 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4874 " for ABI version 1\n"), name);
4875 bfd_set_error (bfd_error_bad_value);
4876 return FALSE;
4877 }
4878 }
4879
555cd476
AM
4880 return TRUE;
4881}
4882
6911b7dc
AM
4883/* Merge non-visibility st_other attributes: local entry point. */
4884
4885static void
4886ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4887 const Elf_Internal_Sym *isym,
4888 bfd_boolean definition,
4889 bfd_boolean dynamic)
4890{
4891 if (definition && !dynamic)
4892 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4893 | ELF_ST_VISIBILITY (h->other));
4894}
4895
8387904d 4896/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4897 inclusion of a new ABI object archive that defines "bar".
4898 NAME is a symbol defined in an archive. Return a symbol in the hash
4899 table that might be satisfied by the archive symbols. */
8387904d
AM
4900
4901static struct elf_link_hash_entry *
4902ppc64_elf_archive_symbol_lookup (bfd *abfd,
4903 struct bfd_link_info *info,
4904 const char *name)
4905{
4906 struct elf_link_hash_entry *h;
4907 char *dot_name;
4908 size_t len;
4909
4910 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4911 if (h != NULL
4912 /* Don't return this sym if it is a fake function descriptor
4913 created by add_symbol_adjust. */
4914 && !(h->root.type == bfd_link_hash_undefweak
4915 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4916 return h;
4917
4918 if (name[0] == '.')
4919 return h;
4920
4921 len = strlen (name);
4922 dot_name = bfd_alloc (abfd, len + 2);
4923 if (dot_name == NULL)
4924 return (struct elf_link_hash_entry *) 0 - 1;
4925 dot_name[0] = '.';
4926 memcpy (dot_name + 1, name, len + 1);
4927 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4928 bfd_release (abfd, dot_name);
4929 return h;
4930}
4931
4932/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4933 new ABI object defines "bar". Well, at least, undefined dot symbols
4934 are made weak. This stops later archive searches from including an
4935 object if we already have a function descriptor definition. It also
35b0ce59
AM
4936 prevents the linker complaining about undefined symbols.
4937 We also check and correct mismatched symbol visibility here. The
4938 most restrictive visibility of the function descriptor and the
4939 function entry symbol is used. */
8387904d
AM
4940
4941static bfd_boolean
b3fac117 4942add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4943{
8387904d
AM
4944 struct ppc_link_hash_table *htab;
4945 struct ppc_link_hash_entry *fdh;
4946
b3fac117 4947 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4948 return TRUE;
4949
b3fac117
AM
4950 if (eh->elf.root.type == bfd_link_hash_warning)
4951 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4952
b3fac117
AM
4953 if (eh->elf.root.root.string[0] != '.')
4954 abort ();
8387904d 4955
b3fac117 4956 htab = ppc_hash_table (info);
4dfe6ac6
NC
4957 if (htab == NULL)
4958 return FALSE;
4959
b31867b6
AM
4960 fdh = lookup_fdh (eh, htab);
4961 if (fdh == NULL)
4962 {
0e1862bb 4963 if (!bfd_link_relocatable (info)
b31867b6
AM
4964 && (eh->elf.root.type == bfd_link_hash_undefined
4965 || eh->elf.root.type == bfd_link_hash_undefweak)
4966 && eh->elf.ref_regular)
4967 {
4968 /* Make an undefweak function descriptor sym, which is enough to
4969 pull in an --as-needed shared lib, but won't cause link
4970 errors. Archives are handled elsewhere. */
4971 fdh = make_fdh (info, eh);
4972 if (fdh == NULL)
4973 return FALSE;
4974 fdh->elf.ref_regular = 1;
4975 }
bb700d78 4976 }
b31867b6 4977 else
8387904d 4978 {
35b0ce59
AM
4979 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4980 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4981 if (entry_vis < descr_vis)
4982 fdh->elf.other += entry_vis - descr_vis;
4983 else if (entry_vis > descr_vis)
4984 eh->elf.other += descr_vis - entry_vis;
4985
e87d886e
AM
4986 if ((fdh->elf.root.type == bfd_link_hash_defined
4987 || fdh->elf.root.type == bfd_link_hash_defweak)
4988 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4989 {
4990 eh->elf.root.type = bfd_link_hash_undefweak;
4991 eh->was_undefined = 1;
4992 htab->twiddled_syms = 1;
4993 }
8387904d 4994 }
99877b66 4995
8387904d
AM
4996 return TRUE;
4997}
4998
f6c7c3e8
AM
4999/* Set up opd section info and abiversion for IBFD, and process list
5000 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5001
8387904d 5002static bfd_boolean
f6c7c3e8 5003ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5004{
99877b66 5005 struct ppc_link_hash_table *htab;
b3fac117 5006 struct ppc_link_hash_entry **p, *eh;
459609d6 5007 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5008
459609d6 5009 if (opd != NULL && opd->size != 0)
b3fac117 5010 {
459609d6
AM
5011 if (abiversion (ibfd) == 0)
5012 set_abiversion (ibfd, 1);
5013 else if (abiversion (ibfd) == 2)
f6c7c3e8 5014 {
459609d6
AM
5015 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5016 " version %d\n"),
5017 ibfd, abiversion (ibfd));
5018 bfd_set_error (bfd_error_bad_value);
5019 return FALSE;
f6c7c3e8
AM
5020 }
5021
459609d6
AM
5022 if ((ibfd->flags & DYNAMIC) == 0
5023 && (opd->flags & SEC_RELOC) != 0
5024 && opd->reloc_count != 0
5025 && !bfd_is_abs_section (opd->output_section))
b3fac117 5026 {
459609d6
AM
5027 /* Garbage collection needs some extra help with .opd sections.
5028 We don't want to necessarily keep everything referenced by
5029 relocs in .opd, as that would keep all functions. Instead,
5030 if we reference an .opd symbol (a function descriptor), we
5031 want to keep the function code symbol's section. This is
5032 easy for global symbols, but for local syms we need to keep
5033 information about the associated function section. */
5034 bfd_size_type amt;
5035 asection **opd_sym_map;
5036
51aecdc5 5037 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5038 opd_sym_map = bfd_zalloc (ibfd, amt);
5039 if (opd_sym_map == NULL)
b3fac117 5040 return FALSE;
459609d6
AM
5041 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5042 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5043 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5044 }
5045 }
5046
459609d6
AM
5047 if (!is_ppc64_elf (info->output_bfd))
5048 return TRUE;
5049 htab = ppc_hash_table (info);
5050 if (htab == NULL)
5051 return FALSE;
5052
5053 /* For input files without an explicit abiversion in e_flags
5054 we should have flagged any with symbol st_other bits set
5055 as ELFv1 and above flagged those with .opd as ELFv2.
5056 Set the output abiversion if not yet set, and for any input
5057 still ambiguous, take its abiversion from the output.
5058 Differences in ABI are reported later. */
5059 if (abiversion (info->output_bfd) == 0)
5060 set_abiversion (info->output_bfd, abiversion (ibfd));
5061 else if (abiversion (ibfd) == 0)
5062 set_abiversion (ibfd, abiversion (info->output_bfd));
5063
5064 p = &htab->dot_syms;
5065 while ((eh = *p) != NULL)
5066 {
5067 *p = NULL;
5068 if (&eh->elf == htab->elf.hgot)
5069 ;
5070 else if (htab->elf.hgot == NULL
5071 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5072 htab->elf.hgot = &eh->elf;
5073 else if (!add_symbol_adjust (eh, info))
5074 return FALSE;
5075 p = &eh->u.next_dot_sym;
5076 }
5077
b3fac117
AM
5078 /* Clear the list for non-ppc64 input files. */
5079 p = &htab->dot_syms;
5080 while ((eh = *p) != NULL)
5081 {
5082 *p = NULL;
5083 p = &eh->u.next_dot_sym;
5084 }
99877b66
AM
5085
5086 /* We need to fix the undefs list for any syms we have twiddled to
5087 undef_weak. */
5088 if (htab->twiddled_syms)
5089 {
77cfaee6 5090 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
5091 htab->twiddled_syms = 0;
5092 }
b3fac117 5093 return TRUE;
8387904d
AM
5094}
5095
97fed1c9
JJ
5096/* Undo hash table changes when an --as-needed input file is determined
5097 not to be needed. */
5098
5099static bfd_boolean
e5034e59
AM
5100ppc64_elf_notice_as_needed (bfd *ibfd,
5101 struct bfd_link_info *info,
5102 enum notice_asneeded_action act)
97fed1c9 5103{
e5034e59
AM
5104 if (act == notice_not_needed)
5105 {
5106 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5107
e5034e59
AM
5108 if (htab == NULL)
5109 return FALSE;
4dfe6ac6 5110
e5034e59
AM
5111 htab->dot_syms = NULL;
5112 }
5113 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5114}
5115
aa374f67
AM
5116/* If --just-symbols against a final linked binary, then assume we need
5117 toc adjusting stubs when calling functions defined there. */
5118
5119static void
5120ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5121{
5122 if ((sec->flags & SEC_CODE) != 0
5123 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5124 && is_ppc64_elf (sec->owner))
5125 {
2c3f079f
AM
5126 if (abiversion (sec->owner) >= 2
5127 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5128 sec->has_toc_reloc = 1;
5129 }
5130 _bfd_elf_link_just_syms (sec, info);
5131}
5132
e054468f 5133static struct plt_entry **
4ce794b7
AM
5134update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5135 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5136{
5137 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5138 struct plt_entry **local_plt;
f961d9dd 5139 unsigned char *local_got_tls_masks;
411e1bfb
AM
5140
5141 if (local_got_ents == NULL)
5142 {
5143 bfd_size_type size = symtab_hdr->sh_info;
5144
e054468f
AM
5145 size *= (sizeof (*local_got_ents)
5146 + sizeof (*local_plt)
5147 + sizeof (*local_got_tls_masks));
4ce794b7 5148 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5149 if (local_got_ents == NULL)
e054468f 5150 return NULL;
411e1bfb
AM
5151 elf_local_got_ents (abfd) = local_got_ents;
5152 }
5153
e054468f 5154 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5155 {
5156 struct got_entry *ent;
5157
5158 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5159 if (ent->addend == r_addend
5160 && ent->owner == abfd
5161 && ent->tls_type == tls_type)
411e1bfb
AM
5162 break;
5163 if (ent == NULL)
5164 {
5165 bfd_size_type amt = sizeof (*ent);
4ce794b7 5166 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5167 if (ent == NULL)
5168 return FALSE;
5169 ent->next = local_got_ents[r_symndx];
5170 ent->addend = r_addend;
e717da7e 5171 ent->owner = abfd;
411e1bfb 5172 ent->tls_type = tls_type;
927be08e 5173 ent->is_indirect = FALSE;
411e1bfb
AM
5174 ent->got.refcount = 0;
5175 local_got_ents[r_symndx] = ent;
5176 }
5177 ent->got.refcount += 1;
5178 }
5179
e054468f 5180 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5181 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5182 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5183
5184 return local_plt + r_symndx;
65f38f15
AM
5185}
5186
411e1bfb 5187static bfd_boolean
e054468f 5188update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5189{
411e1bfb 5190 struct plt_entry *ent;
1e2f5b6e 5191
e054468f 5192 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5193 if (ent->addend == addend)
5194 break;
5195 if (ent == NULL)
1e2f5b6e 5196 {
411e1bfb 5197 bfd_size_type amt = sizeof (*ent);
4ce794b7 5198 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5199 if (ent == NULL)
5200 return FALSE;
e054468f 5201 ent->next = *plist;
411e1bfb
AM
5202 ent->addend = addend;
5203 ent->plt.refcount = 0;
e054468f 5204 *plist = ent;
1e2f5b6e 5205 }
411e1bfb 5206 ent->plt.refcount += 1;
b34976b6 5207 return TRUE;
1e2f5b6e
AM
5208}
5209
e054468f
AM
5210static bfd_boolean
5211is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5212{
5213 return (r_type == R_PPC64_REL24
5214 || r_type == R_PPC64_REL14
5215 || r_type == R_PPC64_REL14_BRTAKEN
5216 || r_type == R_PPC64_REL14_BRNTAKEN
5217 || r_type == R_PPC64_ADDR24
5218 || r_type == R_PPC64_ADDR14
5219 || r_type == R_PPC64_ADDR14_BRTAKEN
5220 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5221}
5222
5bd4f169 5223/* Look through the relocs for a section during the first phase, and
65f38f15 5224 calculate needed space in the global offset table, procedure
5d1634d7 5225 linkage table, and dynamic reloc sections. */
5bd4f169 5226
b34976b6 5227static bfd_boolean
4ce794b7
AM
5228ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5229 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5230{
65f38f15 5231 struct ppc_link_hash_table *htab;
5bd4f169 5232 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5233 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5234 const Elf_Internal_Rela *rel;
5235 const Elf_Internal_Rela *rel_end;
5bd4f169 5236 asection *sreloc;
1e2f5b6e 5237 asection **opd_sym_map;
3a71aa26 5238 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5239
0e1862bb 5240 if (bfd_link_relocatable (info))
b34976b6 5241 return TRUE;
5bd4f169 5242
680a3378
AM
5243 /* Don't do anything special with non-loaded, non-alloced sections.
5244 In particular, any relocs in such sections should not affect GOT
5245 and PLT reference counting (ie. we don't allow them to create GOT
5246 or PLT entries), there's no possibility or desire to optimize TLS
5247 relocs, and there's not much point in propagating relocs to shared
5248 libs that the dynamic linker won't relocate. */
5249 if ((sec->flags & SEC_ALLOC) == 0)
5250 return TRUE;
5251
0c8d6e5c 5252 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5253
65f38f15 5254 htab = ppc_hash_table (info);
4dfe6ac6
NC
5255 if (htab == NULL)
5256 return FALSE;
5257
3a71aa26
AM
5258 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5259 FALSE, FALSE, TRUE);
5260 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5261 FALSE, FALSE, TRUE);
0ffa91dd 5262 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5263 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5264 sreloc = NULL;
1e2f5b6e 5265 opd_sym_map = NULL;
f6c7c3e8
AM
5266 if (ppc64_elf_section_data (sec) != NULL
5267 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5268 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5269
5270 rel_end = relocs + sec->reloc_count;
5271 for (rel = relocs; rel < rel_end; rel++)
5272 {
5273 unsigned long r_symndx;
5274 struct elf_link_hash_entry *h;
04c9666a 5275 enum elf_ppc64_reloc_type r_type;
727fc41e 5276 int tls_type;
7c8fe5c4 5277 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 5278 struct plt_entry **ifunc;
5bd4f169
AM
5279
5280 r_symndx = ELF64_R_SYM (rel->r_info);
5281 if (r_symndx < symtab_hdr->sh_info)
5282 h = NULL;
5283 else
973a3492
L
5284 {
5285 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5286 h = elf_follow_link (h);
81fbe831
AM
5287
5288 /* PR15323, ref flags aren't set for references in the same
5289 object. */
5290 h->root.non_ir_ref = 1;
1c865ab2
AM
5291
5292 if (h == htab->elf.hgot)
5293 sec->has_toc_reloc = 1;
973a3492 5294 }
5bd4f169 5295
727fc41e 5296 tls_type = 0;
e054468f 5297 ifunc = NULL;
25f23106
AM
5298 if (h != NULL)
5299 {
5300 if (h->type == STT_GNU_IFUNC)
5301 {
5302 h->needs_plt = 1;
5303 ifunc = &h->plt.plist;
5304 }
5305 }
5306 else
5307 {
5308 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5309 abfd, r_symndx);
5310 if (isym == NULL)
5311 return FALSE;
5312
5313 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5314 {
5315 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5316 rel->r_addend, PLT_IFUNC);
5317 if (ifunc == NULL)
5318 return FALSE;
5319 }
5320 }
4ce794b7 5321 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
5322 if (is_branch_reloc (r_type))
5323 {
5324 if (h != NULL && (h == tga || h == dottga))
5325 {
5326 if (rel != relocs
5327 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5328 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5329 /* We have a new-style __tls_get_addr call with a marker
5330 reloc. */
5331 ;
5332 else
5333 /* Mark this section as having an old-style call. */
5334 sec->has_tls_get_addr_call = 1;
5335 }
727fc41e 5336
e054468f 5337 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5338 if (ifunc != NULL
5339 && !update_plt_info (abfd, ifunc, rel->r_addend))
5340 return FALSE;
5341 }
727fc41e 5342
a33d1f77 5343 switch (r_type)
5bd4f169 5344 {
727fc41e
AM
5345 case R_PPC64_TLSGD:
5346 case R_PPC64_TLSLD:
5347 /* These special tls relocs tie a call to __tls_get_addr with
5348 its parameter symbol. */
5349 break;
5350
411e1bfb
AM
5351 case R_PPC64_GOT_TLSLD16:
5352 case R_PPC64_GOT_TLSLD16_LO:
5353 case R_PPC64_GOT_TLSLD16_HI:
5354 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5355 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5356 goto dogottls;
5357
5358 case R_PPC64_GOT_TLSGD16:
5359 case R_PPC64_GOT_TLSGD16_LO:
5360 case R_PPC64_GOT_TLSGD16_HI:
5361 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5362 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5363 goto dogottls;
5364
5365 case R_PPC64_GOT_TPREL16_DS:
5366 case R_PPC64_GOT_TPREL16_LO_DS:
5367 case R_PPC64_GOT_TPREL16_HI:
5368 case R_PPC64_GOT_TPREL16_HA:
0e1862bb 5369 if (bfd_link_pic (info))
411e1bfb
AM
5370 info->flags |= DF_STATIC_TLS;
5371 tls_type = TLS_TLS | TLS_TPREL;
5372 goto dogottls;
5373
5374 case R_PPC64_GOT_DTPREL16_DS:
5375 case R_PPC64_GOT_DTPREL16_LO_DS:
5376 case R_PPC64_GOT_DTPREL16_HI:
5377 case R_PPC64_GOT_DTPREL16_HA:
5378 tls_type = TLS_TLS | TLS_DTPREL;
5379 dogottls:
5380 sec->has_tls_reloc = 1;
5381 /* Fall thru */
5382
5bd4f169 5383 case R_PPC64_GOT16:
5bd4f169 5384 case R_PPC64_GOT16_DS:
65f38f15
AM
5385 case R_PPC64_GOT16_HA:
5386 case R_PPC64_GOT16_HI:
5387 case R_PPC64_GOT16_LO:
5bd4f169 5388 case R_PPC64_GOT16_LO_DS:
65f38f15 5389 /* This symbol requires a global offset table entry. */
4c52953f 5390 sec->has_toc_reloc = 1;
33c0ec9d
AM
5391 if (r_type == R_PPC64_GOT_TLSLD16
5392 || r_type == R_PPC64_GOT_TLSGD16
5393 || r_type == R_PPC64_GOT_TPREL16_DS
5394 || r_type == R_PPC64_GOT_DTPREL16_DS
5395 || r_type == R_PPC64_GOT16
5396 || r_type == R_PPC64_GOT16_DS)
5397 {
5398 htab->do_multi_toc = 1;
d77c8a4b 5399 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5400 }
5401
e717da7e
AM
5402 if (ppc64_elf_tdata (abfd)->got == NULL
5403 && !create_got_section (abfd, info))
b34976b6 5404 return FALSE;
5bd4f169
AM
5405
5406 if (h != NULL)
5407 {
411e1bfb
AM
5408 struct ppc_link_hash_entry *eh;
5409 struct got_entry *ent;
65f38f15 5410
411e1bfb
AM
5411 eh = (struct ppc_link_hash_entry *) h;
5412 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5413 if (ent->addend == rel->r_addend
e717da7e 5414 && ent->owner == abfd
411e1bfb
AM
5415 && ent->tls_type == tls_type)
5416 break;
5417 if (ent == NULL)
5bd4f169 5418 {
411e1bfb 5419 bfd_size_type amt = sizeof (*ent);
4ce794b7 5420 ent = bfd_alloc (abfd, amt);
411e1bfb 5421 if (ent == NULL)
b34976b6 5422 return FALSE;
411e1bfb
AM
5423 ent->next = eh->elf.got.glist;
5424 ent->addend = rel->r_addend;
e717da7e 5425 ent->owner = abfd;
411e1bfb 5426 ent->tls_type = tls_type;
927be08e 5427 ent->is_indirect = FALSE;
411e1bfb
AM
5428 ent->got.refcount = 0;
5429 eh->elf.got.glist = ent;
5bd4f169 5430 }
411e1bfb 5431 ent->got.refcount += 1;
e7b938ca 5432 eh->tls_mask |= tls_type;
5bd4f169 5433 }
411e1bfb
AM
5434 else
5435 /* This is a global offset table entry for a local symbol. */
5436 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5437 rel->r_addend, tls_type))
5438 return FALSE;
a345bc8d
AM
5439
5440 /* We may also need a plt entry if the symbol turns out to be
5441 an ifunc. */
0e1862bb 5442 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5443 {
5444 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5445 return FALSE;
5446 }
5bd4f169
AM
5447 break;
5448
5bd4f169 5449 case R_PPC64_PLT16_HA:
65f38f15
AM
5450 case R_PPC64_PLT16_HI:
5451 case R_PPC64_PLT16_LO:
5452 case R_PPC64_PLT32:
5453 case R_PPC64_PLT64:
5bd4f169 5454 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5455 actually build the entry in adjust_dynamic_symbol,
5456 because this might be a case of linking PIC code without
5457 linking in any dynamic objects, in which case we don't
5458 need to generate a procedure linkage table after all. */
5bd4f169
AM
5459 if (h == NULL)
5460 {
5461 /* It does not make sense to have a procedure linkage
3fad3c7c 5462 table entry for a local symbol. */
5bd4f169 5463 bfd_set_error (bfd_error_bad_value);
b34976b6 5464 return FALSE;
5bd4f169 5465 }
411e1bfb 5466 else
e054468f
AM
5467 {
5468 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5469 return FALSE;
5470 h->needs_plt = 1;
5471 if (h->root.root.string[0] == '.'
5472 && h->root.root.string[1] != '\0')
5473 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5474 }
5bd4f169
AM
5475 break;
5476
5477 /* The following relocations don't need to propagate the
5478 relocation if linking a shared object since they are
5479 section relative. */
5480 case R_PPC64_SECTOFF:
5481 case R_PPC64_SECTOFF_LO:
5482 case R_PPC64_SECTOFF_HI:
5483 case R_PPC64_SECTOFF_HA:
5484 case R_PPC64_SECTOFF_DS:
5485 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5486 case R_PPC64_DTPREL16:
5487 case R_PPC64_DTPREL16_LO:
5488 case R_PPC64_DTPREL16_HI:
5489 case R_PPC64_DTPREL16_HA:
5490 case R_PPC64_DTPREL16_DS:
5491 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5492 case R_PPC64_DTPREL16_HIGH:
5493 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5494 case R_PPC64_DTPREL16_HIGHER:
5495 case R_PPC64_DTPREL16_HIGHERA:
5496 case R_PPC64_DTPREL16_HIGHEST:
5497 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5498 break;
5499
ad8e1ba5 5500 /* Nor do these. */
25f23106
AM
5501 case R_PPC64_REL16:
5502 case R_PPC64_REL16_LO:
5503 case R_PPC64_REL16_HI:
5504 case R_PPC64_REL16_HA:
5505 break;
5506
45965137
AM
5507 /* Not supported as a dynamic relocation. */
5508 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5509 if (bfd_link_pic (info))
45965137
AM
5510 {
5511 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5512 ppc_howto_init ();
5513 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5514 "in shared libraries and PIEs.\n"),
5515 abfd, sec, rel->r_offset,
5516 ppc64_elf_howto_table[r_type]->name);
5517 bfd_set_error (bfd_error_bad_value);
5518 return FALSE;
5519 }
5520 break;
5521
ad8e1ba5 5522 case R_PPC64_TOC16:
33c0ec9d
AM
5523 case R_PPC64_TOC16_DS:
5524 htab->do_multi_toc = 1;
d77c8a4b 5525 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5526 case R_PPC64_TOC16_LO:
5527 case R_PPC64_TOC16_HI:
5528 case R_PPC64_TOC16_HA:
ad8e1ba5 5529 case R_PPC64_TOC16_LO_DS:
4c52953f 5530 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5531 break;
5532
5bd4f169
AM
5533 /* This relocation describes the C++ object vtable hierarchy.
5534 Reconstruct it for later use during GC. */
5535 case R_PPC64_GNU_VTINHERIT:
c152c796 5536 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5537 return FALSE;
5bd4f169
AM
5538 break;
5539
5540 /* This relocation describes which C++ vtable entries are actually
5541 used. Record for later use during GC. */
5542 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5543 BFD_ASSERT (h != NULL);
5544 if (h != NULL
5545 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5546 return FALSE;
5bd4f169
AM
5547 break;
5548
721956f4
AM
5549 case R_PPC64_REL14:
5550 case R_PPC64_REL14_BRTAKEN:
5551 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5552 {
5553 asection *dest = NULL;
5554
5555 /* Heuristic: If jumping outside our section, chances are
5556 we are going to need a stub. */
5557 if (h != NULL)
5558 {
5559 /* If the sym is weak it may be overridden later, so
5560 don't assume we know where a weak sym lives. */
5561 if (h->root.type == bfd_link_hash_defined)
5562 dest = h->root.u.def.section;
5563 }
5564 else
87d72d41
AM
5565 {
5566 Elf_Internal_Sym *isym;
5567
5568 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5569 abfd, r_symndx);
5570 if (isym == NULL)
5571 return FALSE;
5572
5573 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5574 }
5575
220c76dd 5576 if (dest != sec)
7c8fe5c4 5577 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5578 }
721956f4
AM
5579 /* Fall through. */
5580
5d1634d7 5581 case R_PPC64_REL24:
e054468f 5582 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5583 {
5584 /* We may need a .plt entry if the function this reloc
5585 refers to is in a shared lib. */
e054468f 5586 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5587 return FALSE;
e054468f
AM
5588 h->needs_plt = 1;
5589 if (h->root.root.string[0] == '.'
5590 && h->root.root.string[1] != '\0')
5591 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5592 if (h == tga || h == dottga)
411e1bfb 5593 sec->has_tls_reloc = 1;
411e1bfb
AM
5594 }
5595 break;
5596
5597 case R_PPC64_TPREL64:
5598 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
0e1862bb 5599 if (bfd_link_pic (info))
411e1bfb
AM
5600 info->flags |= DF_STATIC_TLS;
5601 goto dotlstoc;
5602
5603 case R_PPC64_DTPMOD64:
5604 if (rel + 1 < rel_end
5605 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5606 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5607 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5608 else
951fd09b 5609 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5610 goto dotlstoc;
5611
5612 case R_PPC64_DTPREL64:
5613 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5614 if (rel != relocs
5615 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5616 && rel[-1].r_offset == rel->r_offset - 8)
5617 /* This is the second reloc of a dtpmod, dtprel pair.
5618 Don't mark with TLS_DTPREL. */
5619 goto dodyn;
5620
5621 dotlstoc:
5622 sec->has_tls_reloc = 1;
5623 if (h != NULL)
5624 {
5625 struct ppc_link_hash_entry *eh;
5626 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5627 eh->tls_mask |= tls_type;
411e1bfb
AM
5628 }
5629 else
5630 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5631 rel->r_addend, tls_type))
5632 return FALSE;
5633
7c8fe5c4
AM
5634 ppc64_sec = ppc64_elf_section_data (sec);
5635 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5636 {
3a71aa26
AM
5637 bfd_size_type amt;
5638
e7b938ca 5639 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5640 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5641 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5642 if (ppc64_sec->u.toc.symndx == NULL)
5643 return FALSE;
5644 amt = sec->size * sizeof (bfd_vma) / 8;
5645 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5646 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5647 return FALSE;
7c8fe5c4
AM
5648 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5649 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5650 }
5651 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5652 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5653 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5654
5655 /* Mark the second slot of a GD or LD entry.
5656 -1 to indicate GD and -2 to indicate LD. */
5657 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5658 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5659 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5660 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5661 goto dodyn;
5662
5663 case R_PPC64_TPREL16:
5664 case R_PPC64_TPREL16_LO:
5665 case R_PPC64_TPREL16_HI:
5666 case R_PPC64_TPREL16_HA:
5667 case R_PPC64_TPREL16_DS:
5668 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5669 case R_PPC64_TPREL16_HIGH:
5670 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5671 case R_PPC64_TPREL16_HIGHER:
5672 case R_PPC64_TPREL16_HIGHERA:
5673 case R_PPC64_TPREL16_HIGHEST:
5674 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 5675 if (bfd_link_pic (info))
411e1bfb 5676 {
afb93314 5677 info->flags |= DF_STATIC_TLS;
411e1bfb 5678 goto dodyn;
5d1634d7
AM
5679 }
5680 break;
5681
e86ce104 5682 case R_PPC64_ADDR64:
1e2f5b6e 5683 if (opd_sym_map != NULL
1e2f5b6e 5684 && rel + 1 < rel_end
4ce794b7 5685 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5686 {
8387904d
AM
5687 if (h != NULL)
5688 {
5689 if (h->root.root.string[0] == '.'
5690 && h->root.root.string[1] != 0
b31867b6 5691 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5692 ;
5693 else
5694 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5695 }
5696 else
5697 {
5698 asection *s;
87d72d41 5699 Elf_Internal_Sym *isym;
1e2f5b6e 5700
87d72d41
AM
5701 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5702 abfd, r_symndx);
5703 if (isym == NULL)
8387904d 5704 return FALSE;
87d72d41
AM
5705
5706 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5707 if (s != NULL && s != sec)
51aecdc5 5708 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5709 }
1e2f5b6e 5710 }
e86ce104
AM
5711 /* Fall through. */
5712
65f38f15
AM
5713 case R_PPC64_ADDR16:
5714 case R_PPC64_ADDR16_DS:
5715 case R_PPC64_ADDR16_HA:
5716 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5717 case R_PPC64_ADDR16_HIGH:
5718 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5719 case R_PPC64_ADDR16_HIGHER:
5720 case R_PPC64_ADDR16_HIGHERA:
5721 case R_PPC64_ADDR16_HIGHEST:
5722 case R_PPC64_ADDR16_HIGHESTA:
5723 case R_PPC64_ADDR16_LO:
5724 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5725 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5726 && rel->r_addend == 0)
5727 {
5728 /* We may need a .plt entry if this reloc refers to a
5729 function in a shared lib. */
5730 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5731 return FALSE;
5732 h->pointer_equality_needed = 1;
5733 }
5734 /* Fall through. */
5735
5736 case R_PPC64_REL30:
5737 case R_PPC64_REL32:
5738 case R_PPC64_REL64:
5739 case R_PPC64_ADDR14:
5740 case R_PPC64_ADDR14_BRNTAKEN:
5741 case R_PPC64_ADDR14_BRTAKEN:
65f38f15 5742 case R_PPC64_ADDR24:
65f38f15 5743 case R_PPC64_ADDR32:
65f38f15
AM
5744 case R_PPC64_UADDR16:
5745 case R_PPC64_UADDR32:
5746 case R_PPC64_UADDR64:
5bd4f169 5747 case R_PPC64_TOC:
0e1862bb 5748 if (h != NULL && !bfd_link_pic (info))
81848ca0 5749 /* We may need a copy reloc. */
f5385ebf 5750 h->non_got_ref = 1;
81848ca0 5751
41bd81ab 5752 /* Don't propagate .opd relocs. */
1e2f5b6e 5753 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5754 break;
e86ce104 5755
65f38f15
AM
5756 /* If we are creating a shared library, and this is a reloc
5757 against a global symbol, or a non PC relative reloc
5758 against a local symbol, then we need to copy the reloc
5759 into the shared library. However, if we are linking with
5760 -Bsymbolic, we do not need to copy a reloc against a
5761 global symbol which is defined in an object we are
5762 including in the link (i.e., DEF_REGULAR is set). At
5763 this point we have not seen all the input files, so it is
5764 possible that DEF_REGULAR is not set now but will be set
5765 later (it is never cleared). In case of a weak definition,
5766 DEF_REGULAR may be cleared later by a strong definition in
5767 a shared library. We account for that possibility below by
f4656909 5768 storing information in the dyn_relocs field of the hash
65f38f15
AM
5769 table entry. A similar situation occurs when creating
5770 shared libraries and symbol visibility changes render the
5771 symbol local.
5772
5773 If on the other hand, we are creating an executable, we
5774 may need to keep relocations for symbols satisfied by a
5775 dynamic library if we manage to avoid copy relocs for the
5776 symbol. */
411e1bfb 5777 dodyn:
0e1862bb 5778 if ((bfd_link_pic (info)
1d483afe 5779 && (must_be_dyn_reloc (info, r_type)
65f38f15 5780 || (h != NULL
198f1157 5781 && (!SYMBOLIC_BIND (info, h)
65f38f15 5782 || h->root.type == bfd_link_hash_defweak
f5385ebf 5783 || !h->def_regular))))
f4656909 5784 || (ELIMINATE_COPY_RELOCS
0e1862bb 5785 && !bfd_link_pic (info)
65f38f15
AM
5786 && h != NULL
5787 && (h->root.type == bfd_link_hash_defweak
25f23106 5788 || !h->def_regular))
0e1862bb 5789 || (!bfd_link_pic (info)
25f23106 5790 && ifunc != NULL))
5bd4f169 5791 {
65f38f15
AM
5792 /* We must copy these reloc types into the output file.
5793 Create a reloc section in dynobj and make room for
5794 this reloc. */
5bd4f169
AM
5795 if (sreloc == NULL)
5796 {
83bac4b0
NC
5797 sreloc = _bfd_elf_make_dynamic_reloc_section
5798 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5799
5bd4f169 5800 if (sreloc == NULL)
83bac4b0 5801 return FALSE;
5bd4f169
AM
5802 }
5803
65f38f15
AM
5804 /* If this is a global symbol, we count the number of
5805 relocations we need for this symbol. */
5806 if (h != NULL)
5807 {
19e08130
AM
5808 struct elf_dyn_relocs *p;
5809 struct elf_dyn_relocs **head;
5810
ec338859 5811 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5812 p = *head;
5813 if (p == NULL || p->sec != sec)
5814 {
5815 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5816 if (p == NULL)
5817 return FALSE;
5818 p->next = *head;
5819 *head = p;
5820 p->sec = sec;
5821 p->count = 0;
5822 p->pc_count = 0;
5823 }
5824 p->count += 1;
5825 if (!must_be_dyn_reloc (info, r_type))
5826 p->pc_count += 1;
65f38f15
AM
5827 }
5828 else
5829 {
ec338859
AM
5830 /* Track dynamic relocs needed for local syms too.
5831 We really need local syms available to do this
5832 easily. Oh well. */
19e08130
AM
5833 struct ppc_dyn_relocs *p;
5834 struct ppc_dyn_relocs **head;
5835 bfd_boolean is_ifunc;
ec338859 5836 asection *s;
6edfbbad 5837 void *vpp;
87d72d41 5838 Elf_Internal_Sym *isym;
6edfbbad 5839
87d72d41
AM
5840 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5841 abfd, r_symndx);
5842 if (isym == NULL)
b34976b6 5843 return FALSE;
ec338859 5844
87d72d41
AM
5845 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5846 if (s == NULL)
5847 s = sec;
5848
6edfbbad 5849 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5850 head = (struct ppc_dyn_relocs **) vpp;
5851 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5852 p = *head;
5853 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5854 p = p->next;
5855 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5856 {
5857 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5858 if (p == NULL)
5859 return FALSE;
5860 p->next = *head;
5861 *head = p;
5862 p->sec = sec;
5863 p->ifunc = is_ifunc;
5864 p->count = 0;
5865 }
5866 p->count += 1;
ec338859 5867 }
65f38f15 5868 }
5bd4f169 5869 break;
65f38f15
AM
5870
5871 default:
96e0dda4 5872 break;
5bd4f169
AM
5873 }
5874 }
5875
b34976b6 5876 return TRUE;
5bd4f169
AM
5877}
5878
ee67d69a
AM
5879/* Merge backend specific data from an object file to the output
5880 object file when linking. */
5881
5882static bfd_boolean
5883ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5884{
5885 unsigned long iflags, oflags;
5886
5887 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5888 return TRUE;
5889
5890 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5891 return TRUE;
5892
5893 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5894 return FALSE;
5895
5896 iflags = elf_elfheader (ibfd)->e_flags;
5897 oflags = elf_elfheader (obfd)->e_flags;
5898
f6c7c3e8 5899 if (iflags & ~EF_PPC64_ABI)
ee67d69a
AM
5900 {
5901 (*_bfd_error_handler)
5902 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5903 bfd_set_error (bfd_error_bad_value);
5904 return FALSE;
5905 }
f6c7c3e8 5906 else if (iflags != oflags && iflags != 0)
ee67d69a
AM
5907 {
5908 (*_bfd_error_handler)
5909 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5910 ibfd, iflags, oflags);
5911 bfd_set_error (bfd_error_bad_value);
5912 return FALSE;
5913 }
5914
5915 /* Merge Tag_compatibility attributes and any common GNU ones. */
5916 _bfd_elf_merge_object_attributes (ibfd, obfd);
5917
5918 return TRUE;
5919}
5920
5921static bfd_boolean
5922ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5923{
5924 /* Print normal ELF private data. */
5925 _bfd_elf_print_private_bfd_data (abfd, ptr);
5926
5927 if (elf_elfheader (abfd)->e_flags != 0)
5928 {
5929 FILE *file = ptr;
5930
5931 /* xgettext:c-format */
5932 fprintf (file, _("private flags = 0x%lx:"),
5933 elf_elfheader (abfd)->e_flags);
5934
5935 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5936 fprintf (file, _(" [abiv%ld]"),
5937 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5938 fputc ('\n', file);
5939 }
5940
5941 return TRUE;
5942}
5943
8387904d 5944/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5945 of the code entry point, and its section, which must be in the same
5946 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5947
5948static bfd_vma
5949opd_entry_value (asection *opd_sec,
5950 bfd_vma offset,
5951 asection **code_sec,
aef36ac1
AM
5952 bfd_vma *code_off,
5953 bfd_boolean in_code_sec)
8387904d
AM
5954{
5955 bfd *opd_bfd = opd_sec->owner;
8860955f 5956 Elf_Internal_Rela *relocs;
8387904d 5957 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5958 bfd_vma val;
8387904d 5959
9f296da3
AM
5960 /* No relocs implies we are linking a --just-symbols object, or looking
5961 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5962 if (opd_sec->reloc_count == 0)
5963 {
729eabd5 5964 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5965
729eabd5
AM
5966 if (contents == NULL)
5967 {
5968 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5969 return (bfd_vma) -1;
5970 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5971 }
ee1e4ede 5972
dbb3fbbb 5973 /* PR 17512: file: 64b9dfbb. */
451dfd38 5974 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5975 return (bfd_vma) -1;
5976
729eabd5 5977 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5978 if (code_sec != NULL)
5979 {
5980 asection *sec, *likely = NULL;
ee1e4ede 5981
aef36ac1 5982 if (in_code_sec)
4b85d634 5983 {
aef36ac1
AM
5984 sec = *code_sec;
5985 if (sec->vma <= val
5986 && val < sec->vma + sec->size)
5987 likely = sec;
5988 else
5989 val = -1;
5990 }
5991 else
5992 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5993 if (sec->vma <= val
5994 && (sec->flags & SEC_LOAD) != 0
5995 && (sec->flags & SEC_ALLOC) != 0)
5996 likely = sec;
5997 if (likely != NULL)
5998 {
5999 *code_sec = likely;
6000 if (code_off != NULL)
6001 *code_off = val - likely->vma;
4b85d634
AM
6002 }
6003 }
aef36ac1 6004 return val;
4b85d634
AM
6005 }
6006
0c8d6e5c 6007 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6008
729eabd5 6009 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6010 if (relocs == NULL)
6011 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6012 /* PR 17512: file: df8e1fd6. */
6013 if (relocs == NULL)
6014 return (bfd_vma) -1;
645ea6a9 6015
8387904d 6016 /* Go find the opd reloc at the sym address. */
8860955f 6017 lo = relocs;
8387904d 6018 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6019 val = (bfd_vma) -1;
8387904d
AM
6020 while (lo < hi)
6021 {
6022 look = lo + (hi - lo) / 2;
6023 if (look->r_offset < offset)
6024 lo = look + 1;
6025 else if (look->r_offset > offset)
6026 hi = look;
6027 else
6028 {
0ffa91dd
NC
6029 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6030
8387904d
AM
6031 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6032 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6033 {
6034 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6035 asection *sec = NULL;
8387904d 6036
b53dfeb2
AM
6037 if (symndx >= symtab_hdr->sh_info
6038 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6039 {
6040 struct elf_link_hash_entry **sym_hashes;
6041 struct elf_link_hash_entry *rh;
6042
6043 sym_hashes = elf_sym_hashes (opd_bfd);
6044 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6045 if (rh != NULL)
6046 {
6047 rh = elf_follow_link (rh);
bb854a36
AM
6048 if (rh->root.type != bfd_link_hash_defined
6049 && rh->root.type != bfd_link_hash_defweak)
6050 break;
6051 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6052 {
bb854a36
AM
6053 val = rh->root.u.def.value;
6054 sec = rh->root.u.def.section;
b53dfeb2
AM
6055 }
6056 }
6057 }
6058
6059 if (sec == NULL)
6060 {
6061 Elf_Internal_Sym *sym;
6062
6063 if (symndx < symtab_hdr->sh_info)
6064 {
6065 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6066 if (sym == NULL)
6067 {
6068 size_t symcnt = symtab_hdr->sh_info;
6069 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6070 symcnt, 0,
6071 NULL, NULL, NULL);
6072 if (sym == NULL)
6073 break;
6074 symtab_hdr->contents = (bfd_byte *) sym;
6075 }
6076 sym += symndx;
128205bb
AM
6077 }
6078 else
6079 {
b53dfeb2
AM
6080 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6081 1, symndx,
6082 NULL, NULL, NULL);
128205bb
AM
6083 if (sym == NULL)
6084 break;
128205bb 6085 }
b53dfeb2
AM
6086 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6087 if (sec == NULL)
6088 break;
6089 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6090 val = sym->st_value;
8387904d 6091 }
b53dfeb2 6092
8387904d
AM
6093 val += look->r_addend;
6094 if (code_off != NULL)
6095 *code_off = val;
6096 if (code_sec != NULL)
aef36ac1
AM
6097 {
6098 if (in_code_sec && *code_sec != sec)
6099 return -1;
6100 else
6101 *code_sec = sec;
6102 }
b53dfeb2 6103 if (sec->output_section != NULL)
8387904d 6104 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6105 }
6106 break;
6107 }
6108 }
645ea6a9 6109
645ea6a9 6110 return val;
8387904d
AM
6111}
6112
aef36ac1
AM
6113/* If the ELF symbol SYM might be a function in SEC, return the
6114 function size and set *CODE_OFF to the function's entry point,
6115 otherwise return zero. */
9f296da3 6116
aef36ac1
AM
6117static bfd_size_type
6118ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6119 bfd_vma *code_off)
9f296da3 6120{
aef36ac1
AM
6121 bfd_size_type size;
6122
6123 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6124 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6125 return 0;
6126
6127 size = 0;
6128 if (!(sym->flags & BSF_SYNTHETIC))
6129 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6130
6131 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6132 {
b07bca4e
AM
6133 struct _opd_sec_data *opd = get_opd_info (sym->section);
6134 bfd_vma symval = sym->value;
6135
6136 if (opd != NULL
6137 && opd->adjust != NULL
6138 && elf_section_data (sym->section)->relocs != NULL)
6139 {
6140 /* opd_entry_value will use cached relocs that have been
6141 adjusted, but with raw symbols. That means both local
6142 and global symbols need adjusting. */
6143 long adjust = opd->adjust[OPD_NDX (symval)];
6144 if (adjust == -1)
6145 return 0;
6146 symval += adjust;
6147 }
6148
6149 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6150 &sec, code_off, TRUE) == (bfd_vma) -1)
6151 return 0;
6152 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6153 symbol. This size has nothing to do with the code size of the
6154 function, which is what we're supposed to return, but the
6155 code size isn't available without looking up the dot-sym.
6156 However, doing that would be a waste of time particularly
6157 since elf_find_function will look at the dot-sym anyway.
6158 Now, elf_find_function will keep the largest size of any
6159 function sym found at the code address of interest, so return
6160 1 here to avoid it incorrectly caching a larger function size
6161 for a small function. This does mean we return the wrong
6162 size for a new-ABI function of size 24, but all that does is
6163 disable caching for such functions. */
6164 if (size == 24)
6165 size = 1;
9f296da3 6166 }
aef36ac1
AM
6167 else
6168 {
6169 if (sym->section != sec)
6170 return 0;
6171 *code_off = sym->value;
6172 }
6173 if (size == 0)
6174 size = 1;
6175 return size;
9f296da3
AM
6176}
6177
854b41e7
AM
6178/* Return true if symbol is defined in a regular object file. */
6179
6180static bfd_boolean
6181is_static_defined (struct elf_link_hash_entry *h)
6182{
6183 return ((h->root.type == bfd_link_hash_defined
6184 || h->root.type == bfd_link_hash_defweak)
6185 && h->root.u.def.section != NULL
6186 && h->root.u.def.section->output_section != NULL);
6187}
6188
b31867b6
AM
6189/* If FDH is a function descriptor symbol, return the associated code
6190 entry symbol if it is defined. Return NULL otherwise. */
6191
6192static struct ppc_link_hash_entry *
6193defined_code_entry (struct ppc_link_hash_entry *fdh)
6194{
6195 if (fdh->is_func_descriptor)
6196 {
6197 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6198 if (fh->elf.root.type == bfd_link_hash_defined
6199 || fh->elf.root.type == bfd_link_hash_defweak)
6200 return fh;
6201 }
6202 return NULL;
6203}
6204
6205/* If FH is a function code entry symbol, return the associated
6206 function descriptor symbol if it is defined. Return NULL otherwise. */
6207
6208static struct ppc_link_hash_entry *
6209defined_func_desc (struct ppc_link_hash_entry *fh)
6210{
6211 if (fh->oh != NULL
6212 && fh->oh->is_func_descriptor)
6213 {
6214 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6215 if (fdh->elf.root.type == bfd_link_hash_defined
6216 || fdh->elf.root.type == bfd_link_hash_defweak)
6217 return fdh;
6218 }
6219 return NULL;
6220}
6221
74f0fb50
AM
6222/* Mark all our entry sym sections, both opd and code section. */
6223
6224static void
6225ppc64_elf_gc_keep (struct bfd_link_info *info)
6226{
6227 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6228 struct bfd_sym_chain *sym;
6229
4dfe6ac6
NC
6230 if (htab == NULL)
6231 return;
6232
74f0fb50
AM
6233 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6234 {
b31867b6 6235 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6236 asection *sec;
6237
6238 eh = (struct ppc_link_hash_entry *)
b31867b6 6239 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6240 if (eh == NULL)
6241 continue;
6242 if (eh->elf.root.type != bfd_link_hash_defined
6243 && eh->elf.root.type != bfd_link_hash_defweak)
6244 continue;
6245
b31867b6
AM
6246 fh = defined_code_entry (eh);
6247 if (fh != NULL)
74f0fb50 6248 {
b31867b6 6249 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6250 sec->flags |= SEC_KEEP;
6251 }
6252 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6253 && opd_entry_value (eh->elf.root.u.def.section,
6254 eh->elf.root.u.def.value,
aef36ac1 6255 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6256 sec->flags |= SEC_KEEP;
6257
6258 sec = eh->elf.root.u.def.section;
6259 sec->flags |= SEC_KEEP;
6260 }
6261}
6262
64d03ab5
AM
6263/* Mark sections containing dynamically referenced symbols. When
6264 building shared libraries, we must assume that any visible symbol is
6265 referenced. */
6266
6267static bfd_boolean
6268ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6269{
6270 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6271 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6272 struct ppc_link_hash_entry *fdh;
b407645f 6273 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6274
64d03ab5 6275 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6276 fdh = defined_func_desc (eh);
6277 if (fdh != NULL)
6278 eh = fdh;
64d03ab5
AM
6279
6280 if ((eh->elf.root.type == bfd_link_hash_defined
6281 || eh->elf.root.type == bfd_link_hash_defweak)
6282 && (eh->elf.ref_dynamic
1c9177d9 6283 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6284 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6285 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6286 && (!bfd_link_executable (info)
b407645f
AM
6287 || info->export_dynamic
6288 || (eh->elf.dynamic
6289 && d != NULL
6290 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
4c58e0d8
AM
6291 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6292 || !bfd_hide_sym_by_version (info->version_info,
6293 eh->elf.root.root.string)))))
64d03ab5
AM
6294 {
6295 asection *code_sec;
b31867b6 6296 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6297
6298 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6299
6300 /* Function descriptor syms cause the associated
6301 function code sym section to be marked. */
b31867b6
AM
6302 fh = defined_code_entry (eh);
6303 if (fh != NULL)
6304 {
6305 code_sec = fh->elf.root.u.def.section;
6306 code_sec->flags |= SEC_KEEP;
6307 }
64d03ab5
AM
6308 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6309 && opd_entry_value (eh->elf.root.u.def.section,
6310 eh->elf.root.u.def.value,
aef36ac1 6311 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6312 code_sec->flags |= SEC_KEEP;
6313 }
6314
6315 return TRUE;
6316}
6317
5bd4f169
AM
6318/* Return the section that should be marked against GC for a given
6319 relocation. */
6320
6321static asection *
4ce794b7 6322ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6323 struct bfd_link_info *info,
4ce794b7
AM
6324 Elf_Internal_Rela *rel,
6325 struct elf_link_hash_entry *h,
6326 Elf_Internal_Sym *sym)
5bd4f169 6327{
ccfa59ea
AM
6328 asection *rsec;
6329
ccfa59ea
AM
6330 /* Syms return NULL if we're marking .opd, so we avoid marking all
6331 function sections, as all functions are referenced in .opd. */
6332 rsec = NULL;
6333 if (get_opd_info (sec) != NULL)
6334 return rsec;
1e2f5b6e 6335
5bd4f169
AM
6336 if (h != NULL)
6337 {
04c9666a 6338 enum elf_ppc64_reloc_type r_type;
b31867b6 6339 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6340
4ce794b7 6341 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6342 switch (r_type)
5bd4f169
AM
6343 {
6344 case R_PPC64_GNU_VTINHERIT:
6345 case R_PPC64_GNU_VTENTRY:
6346 break;
6347
6348 default:
6349 switch (h->root.type)
6350 {
6351 case bfd_link_hash_defined:
6352 case bfd_link_hash_defweak:
ccfa59ea 6353 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6354 fdh = defined_func_desc (eh);
6355 if (fdh != NULL)
6356 eh = fdh;
1e2f5b6e
AM
6357
6358 /* Function descriptor syms cause the associated
6359 function code sym section to be marked. */
b31867b6
AM
6360 fh = defined_code_entry (eh);
6361 if (fh != NULL)
ccfa59ea
AM
6362 {
6363 /* They also mark their opd section. */
74f0fb50 6364 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6365
b31867b6 6366 rsec = fh->elf.root.u.def.section;
ccfa59ea 6367 }
8387904d
AM
6368 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6369 && opd_entry_value (eh->elf.root.u.def.section,
6370 eh->elf.root.u.def.value,
aef36ac1 6371 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6372 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6373 else
1e2f5b6e
AM
6374 rsec = h->root.u.def.section;
6375 break;
5bd4f169
AM
6376
6377 case bfd_link_hash_common:
1e2f5b6e
AM
6378 rsec = h->root.u.c.p->section;
6379 break;
5bd4f169
AM
6380
6381 default:
fb34365b 6382 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6383 }
6384 }
6385 }
6386 else
6387 {
74f0fb50 6388 struct _opd_sec_data *opd;
1e2f5b6e
AM
6389
6390 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6391 opd = get_opd_info (rsec);
6392 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6393 {
74f0fb50 6394 rsec->gc_mark = 1;
ccfa59ea 6395
51aecdc5 6396 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6397 }
5bd4f169
AM
6398 }
6399
1e2f5b6e 6400 return rsec;
5bd4f169
AM
6401}
6402
65f38f15
AM
6403/* Update the .got, .plt. and dynamic reloc reference counts for the
6404 section being removed. */
5bd4f169 6405
b34976b6 6406static bfd_boolean
4ce794b7
AM
6407ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6408 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6409{
411e1bfb 6410 struct ppc_link_hash_table *htab;
5bd4f169
AM
6411 Elf_Internal_Shdr *symtab_hdr;
6412 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6413 struct got_entry **local_got_ents;
5bd4f169 6414 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6415
0e1862bb 6416 if (bfd_link_relocatable (info))
7dda2462
TG
6417 return TRUE;
6418
680a3378
AM
6419 if ((sec->flags & SEC_ALLOC) == 0)
6420 return TRUE;
6421
ec338859
AM
6422 elf_section_data (sec)->local_dynrel = NULL;
6423
411e1bfb 6424 htab = ppc_hash_table (info);
4dfe6ac6
NC
6425 if (htab == NULL)
6426 return FALSE;
6427
0ffa91dd 6428 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6429 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6430 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6431
6432 relend = relocs + sec->reloc_count;
6433 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6434 {
6435 unsigned long r_symndx;
04c9666a 6436 enum elf_ppc64_reloc_type r_type;
58ac9f71 6437 struct elf_link_hash_entry *h = NULL;
f961d9dd 6438 unsigned char tls_type = 0;
5bd4f169 6439
a33d1f77 6440 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6441 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6442 if (r_symndx >= symtab_hdr->sh_info)
6443 {
6444 struct ppc_link_hash_entry *eh;
6061a67d
AM
6445 struct elf_dyn_relocs **pp;
6446 struct elf_dyn_relocs *p;
58ac9f71
AM
6447
6448 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6449 h = elf_follow_link (h);
58ac9f71
AM
6450 eh = (struct ppc_link_hash_entry *) h;
6451
6452 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6453 if (p->sec == sec)
6454 {
6455 /* Everything must go for SEC. */
6456 *pp = p->next;
6457 break;
6458 }
6459 }
6460
e054468f
AM
6461 if (is_branch_reloc (r_type))
6462 {
6463 struct plt_entry **ifunc = NULL;
6464 if (h != NULL)
6465 {
6466 if (h->type == STT_GNU_IFUNC)
6467 ifunc = &h->plt.plist;
6468 }
6469 else if (local_got_ents != NULL)
6470 {
6471 struct plt_entry **local_plt = (struct plt_entry **)
6472 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 6473 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
6474 (local_plt + symtab_hdr->sh_info);
6475 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6476 ifunc = local_plt + r_symndx;
6477 }
6478 if (ifunc != NULL)
6479 {
6480 struct plt_entry *ent;
6481
6482 for (ent = *ifunc; ent != NULL; ent = ent->next)
6483 if (ent->addend == rel->r_addend)
6484 break;
6485 if (ent == NULL)
6486 abort ();
6487 if (ent->plt.refcount > 0)
6488 ent->plt.refcount -= 1;
6489 continue;
6490 }
6491 }
6492
a33d1f77
AM
6493 switch (r_type)
6494 {
411e1bfb
AM
6495 case R_PPC64_GOT_TLSLD16:
6496 case R_PPC64_GOT_TLSLD16_LO:
6497 case R_PPC64_GOT_TLSLD16_HI:
6498 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6499 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6500 goto dogot;
6501
6502 case R_PPC64_GOT_TLSGD16:
6503 case R_PPC64_GOT_TLSGD16_LO:
6504 case R_PPC64_GOT_TLSGD16_HI:
6505 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6506 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6507 goto dogot;
6508
6509 case R_PPC64_GOT_TPREL16_DS:
6510 case R_PPC64_GOT_TPREL16_LO_DS:
6511 case R_PPC64_GOT_TPREL16_HI:
6512 case R_PPC64_GOT_TPREL16_HA:
6513 tls_type = TLS_TLS | TLS_TPREL;
6514 goto dogot;
6515
6516 case R_PPC64_GOT_DTPREL16_DS:
6517 case R_PPC64_GOT_DTPREL16_LO_DS:
6518 case R_PPC64_GOT_DTPREL16_HI:
6519 case R_PPC64_GOT_DTPREL16_HA:
6520 tls_type = TLS_TLS | TLS_DTPREL;
6521 goto dogot;
6522
a33d1f77
AM
6523 case R_PPC64_GOT16:
6524 case R_PPC64_GOT16_DS:
6525 case R_PPC64_GOT16_HA:
6526 case R_PPC64_GOT16_HI:
6527 case R_PPC64_GOT16_LO:
6528 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6529 dogot:
6530 {
6531 struct got_entry *ent;
6532
58ac9f71
AM
6533 if (h != NULL)
6534 ent = h->got.glist;
411e1bfb
AM
6535 else
6536 ent = local_got_ents[r_symndx];
6537
6538 for (; ent != NULL; ent = ent->next)
6539 if (ent->addend == rel->r_addend
e717da7e 6540 && ent->owner == abfd
411e1bfb
AM
6541 && ent->tls_type == tls_type)
6542 break;
6543 if (ent == NULL)
6544 abort ();
6545 if (ent->got.refcount > 0)
6546 ent->got.refcount -= 1;
6547 }
a33d1f77 6548 break;
65f38f15 6549
a33d1f77
AM
6550 case R_PPC64_PLT16_HA:
6551 case R_PPC64_PLT16_HI:
6552 case R_PPC64_PLT16_LO:
6553 case R_PPC64_PLT32:
6554 case R_PPC64_PLT64:
721956f4
AM
6555 case R_PPC64_REL14:
6556 case R_PPC64_REL14_BRNTAKEN:
6557 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6558 case R_PPC64_REL24:
58ac9f71 6559 if (h != NULL)
5d1634d7 6560 {
411e1bfb
AM
6561 struct plt_entry *ent;
6562
411e1bfb
AM
6563 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6564 if (ent->addend == rel->r_addend)
6565 break;
7c6c1722 6566 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6567 ent->plt.refcount -= 1;
5d1634d7 6568 }
e86ce104 6569 break;
5d1634d7 6570
a33d1f77
AM
6571 default:
6572 break;
6573 }
6574 }
b34976b6 6575 return TRUE;
5bd4f169
AM
6576}
6577
deb0e272
AM
6578/* The maximum size of .sfpr. */
6579#define SFPR_MAX (218*4)
6580
6581struct sfpr_def_parms
6582{
699733f6
AM
6583 const char name[12];
6584 unsigned char lo, hi;
deb0e272
AM
6585 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6586 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6587};
6588
a4b6fadd
AM
6589/* Auto-generate _save*, _rest* functions in .sfpr.
6590 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6591 instead. */
deb0e272 6592
4dfe6ac6 6593static bfd_boolean
a4b6fadd
AM
6594sfpr_define (struct bfd_link_info *info,
6595 const struct sfpr_def_parms *parm,
6596 asection *stub_sec)
deb0e272
AM
6597{
6598 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6599 unsigned int i;
6600 size_t len = strlen (parm->name);
6601 bfd_boolean writing = FALSE;
699733f6 6602 char sym[16];
deb0e272 6603
4dfe6ac6
NC
6604 if (htab == NULL)
6605 return FALSE;
6606
deb0e272
AM
6607 memcpy (sym, parm->name, len);
6608 sym[len + 2] = 0;
6609
6610 for (i = parm->lo; i <= parm->hi; i++)
6611 {
a4b6fadd 6612 struct ppc_link_hash_entry *h;
deb0e272
AM
6613
6614 sym[len + 0] = i / 10 + '0';
6615 sym[len + 1] = i % 10 + '0';
a4b6fadd
AM
6616 h = (struct ppc_link_hash_entry *)
6617 elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6618 if (stub_sec != NULL)
deb0e272 6619 {
a4b6fadd
AM
6620 if (h != NULL
6621 && h->elf.root.type == bfd_link_hash_defined
6622 && h->elf.root.u.def.section == htab->sfpr)
6623 {
6624 struct elf_link_hash_entry *s;
6625 char buf[32];
6626 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6627 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6628 if (s == NULL)
6629 return FALSE;
6630 if (s->root.type == bfd_link_hash_new
6631 || (s->root.type = bfd_link_hash_defined
6632 && s->root.u.def.section == stub_sec))
6633 {
6634 s->root.type = bfd_link_hash_defined;
6635 s->root.u.def.section = stub_sec;
6636 s->root.u.def.value = (stub_sec->size
6637 + h->elf.root.u.def.value);
6638 s->ref_regular = 1;
6639 s->def_regular = 1;
6640 s->ref_regular_nonweak = 1;
6641 s->forced_local = 1;
6642 s->non_elf = 0;
6643 s->root.linker_def = 1;
6644 }
6645 }
6646 continue;
6647 }
6648 if (h != NULL)
6649 {
6650 h->save_res = 1;
6651 if (!h->elf.def_regular)
deb0e272 6652 {
a4b6fadd
AM
6653 h->elf.root.type = bfd_link_hash_defined;
6654 h->elf.root.u.def.section = htab->sfpr;
6655 h->elf.root.u.def.value = htab->sfpr->size;
6656 h->elf.type = STT_FUNC;
6657 h->elf.def_regular = 1;
6658 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6659 writing = TRUE;
deb0e272 6660 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6661 {
6662 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6663 if (htab->sfpr->contents == NULL)
6664 return FALSE;
6665 }
deb0e272
AM
6666 }
6667 }
6668 if (writing)
6669 {
6670 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6671 if (i != parm->hi)
6672 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6673 else
6674 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6675 htab->sfpr->size = p - htab->sfpr->contents;
6676 }
6677 }
6678
6679 return TRUE;
6680}
6681
6682static bfd_byte *
6683savegpr0 (bfd *abfd, bfd_byte *p, int r)
6684{
6685 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6686 return p + 4;
6687}
6688
6689static bfd_byte *
6690savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6691{
6692 p = savegpr0 (abfd, p, r);
a078d95a 6693 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6694 p = p + 4;
6695 bfd_put_32 (abfd, BLR, p);
6696 return p + 4;
6697}
6698
6699static bfd_byte *
6700restgpr0 (bfd *abfd, bfd_byte *p, int r)
6701{
6702 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6703 return p + 4;
6704}
6705
6706static bfd_byte *
6707restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6708{
a078d95a 6709 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6710 p = p + 4;
6711 p = restgpr0 (abfd, p, r);
6712 bfd_put_32 (abfd, MTLR_R0, p);
6713 p = p + 4;
6714 if (r == 29)
6715 {
6716 p = restgpr0 (abfd, p, 30);
6717 p = restgpr0 (abfd, p, 31);
6718 }
6719 bfd_put_32 (abfd, BLR, p);
6720 return p + 4;
6721}
6722
6723static bfd_byte *
6724savegpr1 (bfd *abfd, bfd_byte *p, int r)
6725{
6726 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6727 return p + 4;
6728}
6729
6730static bfd_byte *
6731savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6732{
6733 p = savegpr1 (abfd, p, r);
6734 bfd_put_32 (abfd, BLR, p);
6735 return p + 4;
6736}
6737
6738static bfd_byte *
6739restgpr1 (bfd *abfd, bfd_byte *p, int r)
6740{
6741 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6742 return p + 4;
6743}
6744
6745static bfd_byte *
6746restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6747{
6748 p = restgpr1 (abfd, p, r);
6749 bfd_put_32 (abfd, BLR, p);
6750 return p + 4;
6751}
6752
6753static bfd_byte *
6754savefpr (bfd *abfd, bfd_byte *p, int r)
6755{
6756 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6757 return p + 4;
6758}
6759
6760static bfd_byte *
6761savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6762{
6763 p = savefpr (abfd, p, r);
a078d95a 6764 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6765 p = p + 4;
6766 bfd_put_32 (abfd, BLR, p);
6767 return p + 4;
6768}
6769
6770static bfd_byte *
6771restfpr (bfd *abfd, bfd_byte *p, int r)
6772{
6773 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6774 return p + 4;
6775}
6776
6777static bfd_byte *
6778restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6779{
a078d95a 6780 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6781 p = p + 4;
6782 p = restfpr (abfd, p, r);
6783 bfd_put_32 (abfd, MTLR_R0, p);
6784 p = p + 4;
6785 if (r == 29)
6786 {
6787 p = restfpr (abfd, p, 30);
6788 p = restfpr (abfd, p, 31);
6789 }
6790 bfd_put_32 (abfd, BLR, p);
6791 return p + 4;
6792}
6793
6794static bfd_byte *
6795savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6796{
6797 p = savefpr (abfd, p, r);
6798 bfd_put_32 (abfd, BLR, p);
6799 return p + 4;
6800}
6801
6802static bfd_byte *
6803restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6804{
6805 p = restfpr (abfd, p, r);
6806 bfd_put_32 (abfd, BLR, p);
6807 return p + 4;
6808}
6809
6810static bfd_byte *
6811savevr (bfd *abfd, bfd_byte *p, int r)
6812{
6813 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6814 p = p + 4;
6815 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6816 return p + 4;
6817}
6818
6819static bfd_byte *
6820savevr_tail (bfd *abfd, bfd_byte *p, int r)
6821{
6822 p = savevr (abfd, p, r);
6823 bfd_put_32 (abfd, BLR, p);
6824 return p + 4;
6825}
6826
6827static bfd_byte *
6828restvr (bfd *abfd, bfd_byte *p, int r)
6829{
6830 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6831 p = p + 4;
6832 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6833 return p + 4;
6834}
6835
6836static bfd_byte *
6837restvr_tail (bfd *abfd, bfd_byte *p, int r)
6838{
6839 p = restvr (abfd, p, r);
6840 bfd_put_32 (abfd, BLR, p);
6841 return p + 4;
6842}
6843
e86ce104
AM
6844/* Called via elf_link_hash_traverse to transfer dynamic linking
6845 information on function code symbol entries to their corresponding
6846 function descriptor symbol entries. */
deb0e272 6847
b34976b6 6848static bfd_boolean
4ce794b7 6849func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6850{
e86ce104 6851 struct bfd_link_info *info;
65f38f15 6852 struct ppc_link_hash_table *htab;
411e1bfb 6853 struct plt_entry *ent;
50bc7936
AM
6854 struct ppc_link_hash_entry *fh;
6855 struct ppc_link_hash_entry *fdh;
6856 bfd_boolean force_local;
5bd4f169 6857
50bc7936
AM
6858 fh = (struct ppc_link_hash_entry *) h;
6859 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6860 return TRUE;
e86ce104 6861
4ce794b7 6862 info = inf;
65f38f15 6863 htab = ppc_hash_table (info);
4dfe6ac6
NC
6864 if (htab == NULL)
6865 return FALSE;
5bd4f169 6866
c09bdfe5
AM
6867 /* Resolve undefined references to dot-symbols as the value
6868 in the function descriptor, if we have one in a regular object.
6869 This is to satisfy cases like ".quad .foo". Calls to functions
6870 in dynamic objects are handled elsewhere. */
6871 if (fh->elf.root.type == bfd_link_hash_undefweak
6872 && fh->was_undefined
b31867b6
AM
6873 && (fdh = defined_func_desc (fh)) != NULL
6874 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6875 && opd_entry_value (fdh->elf.root.u.def.section,
6876 fdh->elf.root.u.def.value,
c09bdfe5 6877 &fh->elf.root.u.def.section,
aef36ac1 6878 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6879 {
b31867b6 6880 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6881 fh->elf.forced_local = 1;
b31867b6
AM
6882 fh->elf.def_regular = fdh->elf.def_regular;
6883 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6884 }
6885
e86ce104
AM
6886 /* If this is a function code symbol, transfer dynamic linking
6887 information to the function descriptor symbol. */
50bc7936 6888 if (!fh->is_func)
b34976b6 6889 return TRUE;
e86ce104 6890
50bc7936 6891 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6892 if (ent->plt.refcount > 0)
6893 break;
50bc7936
AM
6894 if (ent == NULL
6895 || fh->elf.root.root.string[0] != '.'
6896 || fh->elf.root.root.string[1] == '\0')
6897 return TRUE;
5bd4f169 6898
50bc7936
AM
6899 /* Find the corresponding function descriptor symbol. Create it
6900 as undefined if necessary. */
5bd4f169 6901
b31867b6 6902 fdh = lookup_fdh (fh, htab);
50bc7936 6903 if (fdh == NULL
0e1862bb 6904 && !bfd_link_executable (info)
50bc7936
AM
6905 && (fh->elf.root.type == bfd_link_hash_undefined
6906 || fh->elf.root.type == bfd_link_hash_undefweak))
6907 {
908b32fc 6908 fdh = make_fdh (info, fh);
bb700d78
AM
6909 if (fdh == NULL)
6910 return FALSE;
50bc7936 6911 }
648cca2c 6912
908b32fc 6913 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6914 code symbol is strong undefined, make the fake sym the same.
6915 If the function code symbol is defined, then force the fake
6916 descriptor local; We can't support overriding of symbols in a
6917 shared library on a fake descriptor. */
908b32fc
AM
6918
6919 if (fdh != NULL
6920 && fdh->fake
433817dd 6921 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6922 {
433817dd
AM
6923 if (fh->elf.root.type == bfd_link_hash_undefined)
6924 {
6925 fdh->elf.root.type = bfd_link_hash_undefined;
6926 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6927 }
6928 else if (fh->elf.root.type == bfd_link_hash_defined
6929 || fh->elf.root.type == bfd_link_hash_defweak)
6930 {
6931 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6932 }
908b32fc
AM
6933 }
6934
50bc7936 6935 if (fdh != NULL
f5385ebf 6936 && !fdh->elf.forced_local
0e1862bb 6937 && (!bfd_link_executable (info)
f5385ebf
AM
6938 || fdh->elf.def_dynamic
6939 || fdh->elf.ref_dynamic
50bc7936
AM
6940 || (fdh->elf.root.type == bfd_link_hash_undefweak
6941 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6942 {
6943 if (fdh->elf.dynindx == -1)
c152c796 6944 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6945 return FALSE;
f5385ebf
AM
6946 fdh->elf.ref_regular |= fh->elf.ref_regular;
6947 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6948 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6949 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6950 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6951 {
40d16e0b 6952 move_plt_plist (fh, fdh);
f5385ebf 6953 fdh->elf.needs_plt = 1;
e86ce104 6954 }
50bc7936 6955 fdh->is_func_descriptor = 1;
34814b9f
AM
6956 fdh->oh = fh;
6957 fh->oh = fdh;
e86ce104
AM
6958 }
6959
50bc7936
AM
6960 /* Now that the info is on the function descriptor, clear the
6961 function code sym info. Any function code syms for which we
6962 don't have a definition in a regular file, we force local.
6963 This prevents a shared library from exporting syms that have
6964 been imported from another library. Function code syms that
6965 are really in the library we must leave global to prevent the
6966 linker dragging in a definition from a static library. */
93f3fa99
AM
6967 force_local = (!fh->elf.def_regular
6968 || fdh == NULL
6969 || !fdh->elf.def_regular
6970 || fdh->elf.forced_local);
50bc7936
AM
6971 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6972
b34976b6 6973 return TRUE;
e86ce104 6974}
40b8271b 6975
a4b6fadd
AM
6976static const struct sfpr_def_parms save_res_funcs[] =
6977 {
6978 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6979 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6980 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6981 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6982 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6983 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6984 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6985 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6986 { "._savef", 14, 31, savefpr, savefpr1_tail },
6987 { "._restf", 14, 31, restfpr, restfpr1_tail },
6988 { "_savevr_", 20, 31, savevr, savevr_tail },
6989 { "_restvr_", 20, 31, restvr, restvr_tail }
6990 };
6991
e86ce104 6992/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6993 this hook to a) provide some gcc support functions, and b) transfer
6994 dynamic linking information gathered so far on function code symbol
6995 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6996
b34976b6 6997static bfd_boolean
4ce794b7
AM
6998ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6999 struct bfd_link_info *info)
e86ce104
AM
7000{
7001 struct ppc_link_hash_table *htab;
82bd7b59 7002 unsigned int i;
e86ce104
AM
7003
7004 htab = ppc_hash_table (info);
4dfe6ac6
NC
7005 if (htab == NULL)
7006 return FALSE;
7007
0e1862bb 7008 if (!bfd_link_relocatable (info)
5295321c 7009 && htab->elf.hgot != NULL)
dba6fa9b
AM
7010 {
7011 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7012 /* Make .TOC. defined so as to prevent it being made dynamic.
7013 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7014 if (!htab->elf.hgot->def_regular
7015 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7016 {
7017 htab->elf.hgot->root.type = bfd_link_hash_defined;
7018 htab->elf.hgot->root.u.def.value = 0;
7019 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7020 htab->elf.hgot->def_regular = 1;
7021 htab->elf.hgot->root.linker_def = 1;
7022 }
dba6fa9b 7023 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7024 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7025 | STV_HIDDEN);
7026 }
c66bb0ee 7027
82bd7b59
AM
7028 if (htab->sfpr == NULL)
7029 /* We don't have any relocs. */
b34976b6 7030 return TRUE;
82bd7b59 7031
deb0e272
AM
7032 /* Provide any missing _save* and _rest* functions. */
7033 htab->sfpr->size = 0;
7d4c687d 7034 if (htab->params->save_restore_funcs)
a4b6fadd
AM
7035 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7036 if (!sfpr_define (info, &save_res_funcs[i], NULL))
27fc25a1 7037 return FALSE;
82bd7b59 7038
4ce794b7 7039 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 7040
eea6121a 7041 if (htab->sfpr->size == 0)
8423293d 7042 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 7043
b34976b6 7044 return TRUE;
e86ce104
AM
7045}
7046
a345bc8d
AM
7047/* Return true if we have dynamic relocs that apply to read-only sections. */
7048
7049static bfd_boolean
7050readonly_dynrelocs (struct elf_link_hash_entry *h)
7051{
7052 struct ppc_link_hash_entry *eh;
7053 struct elf_dyn_relocs *p;
7054
7055 eh = (struct ppc_link_hash_entry *) h;
7056 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7057 {
7058 asection *s = p->sec->output_section;
7059
7060 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7061 return TRUE;
7062 }
7063 return FALSE;
7064}
7065
e86ce104
AM
7066/* Adjust a symbol defined by a dynamic object and referenced by a
7067 regular object. The current definition is in some section of the
7068 dynamic object, but we're not including those sections. We have to
7069 change the definition to something the rest of the link can
7070 understand. */
7071
b34976b6 7072static bfd_boolean
4ce794b7
AM
7073ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7074 struct elf_link_hash_entry *h)
e86ce104
AM
7075{
7076 struct ppc_link_hash_table *htab;
e86ce104 7077 asection *s;
e86ce104
AM
7078
7079 htab = ppc_hash_table (info);
4dfe6ac6
NC
7080 if (htab == NULL)
7081 return FALSE;
e86ce104
AM
7082
7083 /* Deal with function syms. */
7084 if (h->type == STT_FUNC
e054468f 7085 || h->type == STT_GNU_IFUNC
f5385ebf 7086 || h->needs_plt)
e86ce104
AM
7087 {
7088 /* Clear procedure linkage table information for any symbol that
7089 won't need a .plt entry. */
411e1bfb
AM
7090 struct plt_entry *ent;
7091 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7092 if (ent->plt.refcount > 0)
7093 break;
8387904d 7094 if (ent == NULL
e054468f
AM
7095 || (h->type != STT_GNU_IFUNC
7096 && (SYMBOL_CALLS_LOCAL (info, h)
7097 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
a4b6fadd
AM
7098 && h->root.type == bfd_link_hash_undefweak)))
7099 || ((struct ppc_link_hash_entry *) h)->save_res)
40b8271b 7100 {
411e1bfb 7101 h->plt.plist = NULL;
f5385ebf 7102 h->needs_plt = 0;
d1eca1e4 7103 h->pointer_equality_needed = 0;
40b8271b 7104 }
a345bc8d
AM
7105 else if (abiversion (info->output_bfd) == 2)
7106 {
d1eca1e4
AM
7107 /* Taking a function's address in a read/write section
7108 doesn't require us to define the function symbol in the
7109 executable on a global entry stub. A dynamic reloc can
7110 be used instead. */
7111 if (h->pointer_equality_needed
1009ef28 7112 && h->type != STT_GNU_IFUNC
d1eca1e4
AM
7113 && !readonly_dynrelocs (h))
7114 {
7115 h->pointer_equality_needed = 0;
7116 h->non_got_ref = 0;
7117 }
7118
a345bc8d
AM
7119 /* After adjust_dynamic_symbol, non_got_ref set in the
7120 non-shared case means that we have allocated space in
7121 .dynbss for the symbol and thus dyn_relocs for this
7122 symbol should be discarded.
7123 If we get here we know we are making a PLT entry for this
7124 symbol, and in an executable we'd normally resolve
7125 relocations against this symbol to the PLT entry. Allow
7126 dynamic relocs if the reference is weak, and the dynamic
7127 relocs will not cause text relocation. */
d1eca1e4
AM
7128 else if (!h->ref_regular_nonweak
7129 && h->non_got_ref
7130 && h->type != STT_GNU_IFUNC
7131 && !readonly_dynrelocs (h))
a345bc8d
AM
7132 h->non_got_ref = 0;
7133
7134 /* If making a plt entry, then we don't need copy relocs. */
7135 return TRUE;
7136 }
5bd4f169 7137 }
bbd7ec4a 7138 else
411e1bfb 7139 h->plt.plist = NULL;
5bd4f169
AM
7140
7141 /* If this is a weak symbol, and there is a real definition, the
7142 processor independent code will have arranged for us to see the
7143 real definition first, and we can just use the same value. */
f6e332e6 7144 if (h->u.weakdef != NULL)
5bd4f169 7145 {
f6e332e6
AM
7146 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7147 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7148 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7149 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7150 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7151 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7152 return TRUE;
5bd4f169
AM
7153 }
7154
5bd4f169
AM
7155 /* If we are creating a shared library, we must presume that the
7156 only references to the symbol are via the global offset table.
7157 For such cases we need not do anything here; the relocations will
7158 be handled correctly by relocate_section. */
0e1862bb 7159 if (bfd_link_pic (info))
b34976b6 7160 return TRUE;
5bd4f169 7161
65f38f15
AM
7162 /* If there are no references to this symbol that do not use the
7163 GOT, we don't need to generate a copy reloc. */
f5385ebf 7164 if (!h->non_got_ref)
b34976b6 7165 return TRUE;
65f38f15 7166
b186458a
JJ
7167 /* Don't generate a copy reloc for symbols defined in the executable. */
7168 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
7169 return TRUE;
7170
a127494f
AM
7171 /* If -z nocopyreloc was given, don't generate them either. */
7172 if (info->nocopyreloc)
7173 {
7174 h->non_got_ref = 0;
7175 return TRUE;
7176 }
7177
a345bc8d
AM
7178 /* If we didn't find any dynamic relocs in read-only sections, then
7179 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7180 if (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
65f38f15 7181 {
a345bc8d
AM
7182 h->non_got_ref = 0;
7183 return TRUE;
65f38f15
AM
7184 }
7185
a127494f
AM
7186 /* Protected variables do not work with .dynbss. The copy in
7187 .dynbss won't be used by the shared library with the protected
7188 definition for the variable. Text relocations are preferable
7189 to an incorrect program. */
7190 if (h->protected_def)
7191 {
7192 h->non_got_ref = 0;
7193 return TRUE;
7194 }
7195
5d35169e 7196 if (h->plt.plist != NULL)
97b639ba
AM
7197 {
7198 /* We should never get here, but unfortunately there are versions
7199 of gcc out there that improperly (for this ABI) put initialized
7200 function pointers, vtable refs and suchlike in read-only
7201 sections. Allow them to proceed, but warn that this might
7202 break at runtime. */
25f53a85 7203 info->callbacks->einfo
bc30df16 7204 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7205 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7206 h->root.root.string);
7207 }
5d35169e
AM
7208
7209 /* This is a reference to a symbol defined by a dynamic object which
7210 is not a function. */
7211
5bd4f169
AM
7212 /* We must allocate the symbol in our .dynbss section, which will
7213 become part of the .bss section of the executable. There will be
7214 an entry for this symbol in the .dynsym section. The dynamic
7215 object will contain position independent code, so all references
7216 from the dynamic object to this symbol will go through the global
7217 offset table. The dynamic linker will use the .dynsym entry to
7218 determine the address it must put in the global offset table, so
7219 both the dynamic object and the regular object will refer to the
7220 same memory location for the variable. */
5bd4f169 7221
04c9666a
AM
7222 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7223 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7224 runtime process image. We need to remember the offset into the
7225 .rela.bss section we are going to use. */
1d7e9d18 7226 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7227 {
eea6121a 7228 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 7229 h->needs_copy = 1;
5bd4f169
AM
7230 }
7231
4ce794b7 7232 s = htab->dynbss;
5bd4f169 7233
6cabe1ea 7234 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7235}
7236
e86ce104
AM
7237/* If given a function descriptor symbol, hide both the function code
7238 sym and the descriptor. */
7239static void
4ce794b7
AM
7240ppc64_elf_hide_symbol (struct bfd_link_info *info,
7241 struct elf_link_hash_entry *h,
7242 bfd_boolean force_local)
e86ce104 7243{
34814b9f 7244 struct ppc_link_hash_entry *eh;
e86ce104
AM
7245 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7246
34814b9f
AM
7247 eh = (struct ppc_link_hash_entry *) h;
7248 if (eh->is_func_descriptor)
e86ce104 7249 {
34814b9f 7250 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7251
721956f4 7252 if (fh == NULL)
d1329ca3
AM
7253 {
7254 const char *p, *q;
7255 struct ppc_link_hash_table *htab;
7256 char save;
7257
7258 /* We aren't supposed to use alloca in BFD because on
7259 systems which do not have alloca the version in libiberty
7260 calls xmalloc, which might cause the program to crash
7261 when it runs out of memory. This function doesn't have a
7262 return status, so there's no way to gracefully return an
7263 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7264 accessed; It's either a string in an ELF string table,
7265 or allocated in an objalloc structure. */
d1329ca3 7266
34814b9f 7267 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7268 save = *p;
7269 *(char *) p = '.';
7270 htab = ppc_hash_table (info);
4dfe6ac6
NC
7271 if (htab == NULL)
7272 return;
7273
34814b9f
AM
7274 fh = (struct ppc_link_hash_entry *)
7275 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7276 *(char *) p = save;
7277
7278 /* Unfortunately, if it so happens that the string we were
7279 looking for was allocated immediately before this string,
7280 then we overwrote the string terminator. That's the only
7281 reason the lookup should fail. */
7282 if (fh == NULL)
7283 {
34814b9f
AM
7284 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7285 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7286 --q, --p;
34814b9f
AM
7287 if (q < eh->elf.root.root.string && *p == '.')
7288 fh = (struct ppc_link_hash_entry *)
7289 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7290 }
7291 if (fh != NULL)
7292 {
34814b9f
AM
7293 eh->oh = fh;
7294 fh->oh = eh;
d1329ca3
AM
7295 }
7296 }
e86ce104 7297 if (fh != NULL)
34814b9f 7298 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7299 }
7300}
7301
411e1bfb 7302static bfd_boolean
8843416a
AM
7303get_sym_h (struct elf_link_hash_entry **hp,
7304 Elf_Internal_Sym **symp,
7305 asection **symsecp,
f961d9dd 7306 unsigned char **tls_maskp,
8843416a
AM
7307 Elf_Internal_Sym **locsymsp,
7308 unsigned long r_symndx,
7309 bfd *ibfd)
411e1bfb 7310{
0ffa91dd 7311 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7312
7313 if (r_symndx >= symtab_hdr->sh_info)
7314 {
7315 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7316 struct elf_link_hash_entry *h;
7317
7318 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7319 h = elf_follow_link (h);
411e1bfb
AM
7320
7321 if (hp != NULL)
7322 *hp = h;
7323
7324 if (symp != NULL)
7325 *symp = NULL;
7326
7327 if (symsecp != NULL)
7328 {
7329 asection *symsec = NULL;
7330 if (h->root.type == bfd_link_hash_defined
7331 || h->root.type == bfd_link_hash_defweak)
7332 symsec = h->root.u.def.section;
7333 *symsecp = symsec;
7334 }
7335
e7b938ca 7336 if (tls_maskp != NULL)
411e1bfb
AM
7337 {
7338 struct ppc_link_hash_entry *eh;
7339
7340 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7341 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7342 }
7343 }
7344 else
7345 {
7346 Elf_Internal_Sym *sym;
7347 Elf_Internal_Sym *locsyms = *locsymsp;
7348
7349 if (locsyms == NULL)
7350 {
7351 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7352 if (locsyms == NULL)
7353 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7354 symtab_hdr->sh_info,
7355 0, NULL, NULL, NULL);
7356 if (locsyms == NULL)
7357 return FALSE;
7358 *locsymsp = locsyms;
7359 }
7360 sym = locsyms + r_symndx;
7361
7362 if (hp != NULL)
7363 *hp = NULL;
7364
7365 if (symp != NULL)
7366 *symp = sym;
7367
7368 if (symsecp != NULL)
cb33740c 7369 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7370
e7b938ca 7371 if (tls_maskp != NULL)
411e1bfb
AM
7372 {
7373 struct got_entry **lgot_ents;
f961d9dd 7374 unsigned char *tls_mask;
411e1bfb 7375
e7b938ca 7376 tls_mask = NULL;
411e1bfb
AM
7377 lgot_ents = elf_local_got_ents (ibfd);
7378 if (lgot_ents != NULL)
7379 {
e054468f
AM
7380 struct plt_entry **local_plt = (struct plt_entry **)
7381 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7382 unsigned char *lgot_masks = (unsigned char *)
e054468f 7383 (local_plt + symtab_hdr->sh_info);
e7b938ca 7384 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7385 }
e7b938ca 7386 *tls_maskp = tls_mask;
411e1bfb
AM
7387 }
7388 }
7389 return TRUE;
7390}
7391
e7b938ca 7392/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7393 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7394 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7395
7396static int
f961d9dd 7397get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7398 unsigned long *toc_symndx,
7399 bfd_vma *toc_addend,
0d4792f7 7400 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7401 const Elf_Internal_Rela *rel,
7402 bfd *ibfd)
411e1bfb
AM
7403{
7404 unsigned long r_symndx;
0d4792f7 7405 int next_r;
411e1bfb
AM
7406 struct elf_link_hash_entry *h;
7407 Elf_Internal_Sym *sym;
7408 asection *sec;
7409 bfd_vma off;
7410
7411 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7412 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7413 return 0;
411e1bfb 7414
e7b938ca 7415 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7416 || sec == NULL
6bee8834 7417 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7418 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7419 return 1;
411e1bfb
AM
7420
7421 /* Look inside a TOC section too. */
7422 if (h != NULL)
7423 {
7424 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7425 off = h->root.u.def.value;
7426 }
7427 else
7428 off = sym->st_value;
7429 off += rel->r_addend;
7430 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7431 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7432 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7433 if (toc_symndx != NULL)
7434 *toc_symndx = r_symndx;
3a71aa26
AM
7435 if (toc_addend != NULL)
7436 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7437 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7438 return 0;
854b41e7 7439 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7440 && (next_r == -1 || next_r == -2))
7441 return 1 - next_r;
951fd09b 7442 return 1;
411e1bfb
AM
7443}
7444
3b421ab3
AM
7445/* Find (or create) an entry in the tocsave hash table. */
7446
7447static struct tocsave_entry *
7448tocsave_find (struct ppc_link_hash_table *htab,
7449 enum insert_option insert,
7450 Elf_Internal_Sym **local_syms,
7451 const Elf_Internal_Rela *irela,
7452 bfd *ibfd)
7453{
7454 unsigned long r_indx;
7455 struct elf_link_hash_entry *h;
7456 Elf_Internal_Sym *sym;
7457 struct tocsave_entry ent, *p;
7458 hashval_t hash;
7459 struct tocsave_entry **slot;
7460
7461 r_indx = ELF64_R_SYM (irela->r_info);
7462 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7463 return NULL;
7464 if (ent.sec == NULL || ent.sec->output_section == NULL)
7465 {
7466 (*_bfd_error_handler)
7467 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7468 return NULL;
7469 }
7470
7471 if (h != NULL)
7472 ent.offset = h->root.u.def.value;
7473 else
7474 ent.offset = sym->st_value;
7475 ent.offset += irela->r_addend;
7476
7477 hash = tocsave_htab_hash (&ent);
7478 slot = ((struct tocsave_entry **)
7479 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7480 if (slot == NULL)
7481 return NULL;
7482
7483 if (*slot == NULL)
7484 {
7485 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7486 if (p == NULL)
7487 return NULL;
7488 *p = ent;
7489 *slot = p;
7490 }
7491 return *slot;
7492}
7493
754021d0 7494/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7495 code for the old ABI, these will already have been done. */
754021d0
AM
7496
7497static bfd_boolean
7498adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7499{
7500 struct ppc_link_hash_entry *eh;
7501 asection *sym_sec;
74f0fb50 7502 struct _opd_sec_data *opd;
754021d0
AM
7503
7504 if (h->root.type == bfd_link_hash_indirect)
7505 return TRUE;
7506
754021d0
AM
7507 if (h->root.type != bfd_link_hash_defined
7508 && h->root.type != bfd_link_hash_defweak)
7509 return TRUE;
7510
7511 eh = (struct ppc_link_hash_entry *) h;
7512 if (eh->adjust_done)
7513 return TRUE;
7514
7515 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7516 opd = get_opd_info (sym_sec);
7517 if (opd != NULL && opd->adjust != NULL)
754021d0 7518 {
51aecdc5 7519 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7520 if (adjust == -1)
7521 {
7522 /* This entry has been deleted. */
b3fac117 7523 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7524 if (dsec == NULL)
7525 {
7526 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7527 if (discarded_section (dsec))
81688140 7528 {
b3fac117 7529 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7530 break;
7531 }
7532 }
4025353c 7533 eh->elf.root.u.def.value = 0;
81688140 7534 eh->elf.root.u.def.section = dsec;
4025353c
AM
7535 }
7536 else
7537 eh->elf.root.u.def.value += adjust;
754021d0
AM
7538 eh->adjust_done = 1;
7539 }
7540 return TRUE;
7541}
7542
8c1d1bb8 7543/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7544 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7545 have already been determined. */
7546
7547static bfd_boolean
7548dec_dynrel_count (bfd_vma r_info,
7549 asection *sec,
7550 struct bfd_link_info *info,
7551 Elf_Internal_Sym **local_syms,
7552 struct elf_link_hash_entry *h,
19e08130 7553 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7554{
7555 enum elf_ppc64_reloc_type r_type;
19e08130 7556 asection *sym_sec = NULL;
8c1d1bb8
AM
7557
7558 /* Can this reloc be dynamic? This switch, and later tests here
7559 should be kept in sync with the code in check_relocs. */
7560 r_type = ELF64_R_TYPE (r_info);
7561 switch (r_type)
7562 {
7563 default:
7564 return TRUE;
7565
7566 case R_PPC64_TPREL16:
7567 case R_PPC64_TPREL16_LO:
7568 case R_PPC64_TPREL16_HI:
7569 case R_PPC64_TPREL16_HA:
7570 case R_PPC64_TPREL16_DS:
7571 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7572 case R_PPC64_TPREL16_HIGH:
7573 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7574 case R_PPC64_TPREL16_HIGHER:
7575 case R_PPC64_TPREL16_HIGHERA:
7576 case R_PPC64_TPREL16_HIGHEST:
7577 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 7578 if (!bfd_link_pic (info))
8c1d1bb8
AM
7579 return TRUE;
7580
7581 case R_PPC64_TPREL64:
7582 case R_PPC64_DTPMOD64:
7583 case R_PPC64_DTPREL64:
7584 case R_PPC64_ADDR64:
7585 case R_PPC64_REL30:
7586 case R_PPC64_REL32:
7587 case R_PPC64_REL64:
7588 case R_PPC64_ADDR14:
7589 case R_PPC64_ADDR14_BRNTAKEN:
7590 case R_PPC64_ADDR14_BRTAKEN:
7591 case R_PPC64_ADDR16:
7592 case R_PPC64_ADDR16_DS:
7593 case R_PPC64_ADDR16_HA:
7594 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7595 case R_PPC64_ADDR16_HIGH:
7596 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7597 case R_PPC64_ADDR16_HIGHER:
7598 case R_PPC64_ADDR16_HIGHERA:
7599 case R_PPC64_ADDR16_HIGHEST:
7600 case R_PPC64_ADDR16_HIGHESTA:
7601 case R_PPC64_ADDR16_LO:
7602 case R_PPC64_ADDR16_LO_DS:
7603 case R_PPC64_ADDR24:
7604 case R_PPC64_ADDR32:
7605 case R_PPC64_UADDR16:
7606 case R_PPC64_UADDR32:
7607 case R_PPC64_UADDR64:
7608 case R_PPC64_TOC:
7609 break;
7610 }
7611
7612 if (local_syms != NULL)
7613 {
7614 unsigned long r_symndx;
8c1d1bb8
AM
7615 bfd *ibfd = sec->owner;
7616
7617 r_symndx = ELF64_R_SYM (r_info);
7618 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7619 return FALSE;
7620 }
7621
0e1862bb 7622 if ((bfd_link_pic (info)
1d483afe 7623 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7624 || (h != NULL
198f1157 7625 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7626 || h->root.type == bfd_link_hash_defweak
7627 || !h->def_regular))))
7628 || (ELIMINATE_COPY_RELOCS
0e1862bb 7629 && !bfd_link_pic (info)
8c1d1bb8
AM
7630 && h != NULL
7631 && (h->root.type == bfd_link_hash_defweak
7632 || !h->def_regular)))
7633 ;
7634 else
7635 return TRUE;
7636
7637 if (h != NULL)
6edfbbad 7638 {
19e08130
AM
7639 struct elf_dyn_relocs *p;
7640 struct elf_dyn_relocs **pp;
7641 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7642
7643 /* elf_gc_sweep may have already removed all dyn relocs associated
7644 with local syms for a given section. Also, symbol flags are
7645 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7646 report a dynreloc miscount. */
7647 if (*pp == NULL && info->gc_sections)
7648 return TRUE;
7649
7650 while ((p = *pp) != NULL)
60124e18 7651 {
19e08130
AM
7652 if (p->sec == sec)
7653 {
7654 if (!must_be_dyn_reloc (info, r_type))
7655 p->pc_count -= 1;
7656 p->count -= 1;
7657 if (p->count == 0)
7658 *pp = p->next;
7659 return TRUE;
7660 }
7661 pp = &p->next;
60124e18 7662 }
6edfbbad 7663 }
19e08130
AM
7664 else
7665 {
7666 struct ppc_dyn_relocs *p;
7667 struct ppc_dyn_relocs **pp;
7668 void *vpp;
7669 bfd_boolean is_ifunc;
8c1d1bb8 7670
19e08130
AM
7671 if (local_syms == NULL)
7672 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7673 if (sym_sec == NULL)
7674 sym_sec = sec;
c57da1a7 7675
19e08130
AM
7676 vpp = &elf_section_data (sym_sec)->local_dynrel;
7677 pp = (struct ppc_dyn_relocs **) vpp;
7678
7679 if (*pp == NULL && info->gc_sections)
7680 return TRUE;
7681
7682 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7683 while ((p = *pp) != NULL)
8c1d1bb8 7684 {
19e08130
AM
7685 if (p->sec == sec && p->ifunc == is_ifunc)
7686 {
7687 p->count -= 1;
7688 if (p->count == 0)
7689 *pp = p->next;
7690 return TRUE;
7691 }
7692 pp = &p->next;
8c1d1bb8 7693 }
8c1d1bb8
AM
7694 }
7695
8de848d8 7696 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7697 sec->owner, sec);
8c1d1bb8
AM
7698 bfd_set_error (bfd_error_bad_value);
7699 return FALSE;
7700}
7701
754021d0
AM
7702/* Remove unused Official Procedure Descriptor entries. Currently we
7703 only remove those associated with functions in discarded link-once
7704 sections, or weakly defined functions that have been overridden. It
7705 would be possible to remove many more entries for statically linked
7706 applications. */
7707
b34976b6 7708bfd_boolean
e7d1c40c 7709ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7710{
7711 bfd *ibfd;
754021d0 7712 bfd_boolean some_edited = FALSE;
3f764659 7713 asection *need_pad = NULL;
e7d1c40c
AM
7714 struct ppc_link_hash_table *htab;
7715
7716 htab = ppc_hash_table (info);
7717 if (htab == NULL)
7718 return FALSE;
1e2f5b6e 7719
c72f2fb2 7720 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7721 {
7722 asection *sec;
7723 Elf_Internal_Rela *relstart, *rel, *relend;
7724 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7725 Elf_Internal_Sym *local_syms;
74f0fb50 7726 struct _opd_sec_data *opd;
51aecdc5 7727 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7728 bfd_size_type cnt_16b = 0;
1e2f5b6e 7729
854b41e7
AM
7730 if (!is_ppc64_elf (ibfd))
7731 continue;
7732
1e2f5b6e 7733 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7734 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7735 continue;
7736
dbaa2011 7737 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7738 continue;
7739
1e2f5b6e
AM
7740 if (sec->output_section == bfd_abs_section_ptr)
7741 continue;
7742
7743 /* Look through the section relocs. */
7744 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7745 continue;
7746
6cdc0ccc 7747 local_syms = NULL;
0ffa91dd 7748 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7749
7750 /* Read the relocations. */
4ce794b7 7751 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7752 info->keep_memory);
1e2f5b6e 7753 if (relstart == NULL)
b34976b6 7754 return FALSE;
1e2f5b6e
AM
7755
7756 /* First run through the relocs to check they are sane, and to
7757 determine whether we need to edit this opd section. */
b34976b6 7758 need_edit = FALSE;
51aecdc5 7759 broken = FALSE;
3f764659 7760 need_pad = sec;
1e2f5b6e 7761 relend = relstart + sec->reloc_count;
50bc7936 7762 for (rel = relstart; rel < relend; )
1e2f5b6e 7763 {
04c9666a 7764 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7765 unsigned long r_symndx;
7766 asection *sym_sec;
7767 struct elf_link_hash_entry *h;
7768 Elf_Internal_Sym *sym;
51aecdc5 7769 bfd_vma offset;
1e2f5b6e 7770
51aecdc5 7771 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7772 only interested in the reloc pointing to a function entry
7773 point. */
51aecdc5
AM
7774 offset = rel->r_offset;
7775 if (rel + 1 == relend
7776 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7777 {
7778 /* If someone messes with .opd alignment then after a
7779 "ld -r" we might have padding in the middle of .opd.
7780 Also, there's nothing to prevent someone putting
7781 something silly in .opd with the assembler. No .opd
b34976b6 7782 optimization for them! */
3f764659 7783 broken_opd:
1e2f5b6e 7784 (*_bfd_error_handler)
d003868e 7785 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7786 broken = TRUE;
1e2f5b6e
AM
7787 break;
7788 }
7789
50bc7936
AM
7790 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7791 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7792 {
7793 (*_bfd_error_handler)
d003868e
AM
7794 (_("%B: unexpected reloc type %u in .opd section"),
7795 ibfd, r_type);
51aecdc5 7796 broken = TRUE;
50bc7936
AM
7797 break;
7798 }
7799
1e2f5b6e 7800 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7801 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7802 r_symndx, ibfd))
50bc7936 7803 goto error_ret;
1e2f5b6e
AM
7804
7805 if (sym_sec == NULL || sym_sec->owner == NULL)
7806 {
411e1bfb
AM
7807 const char *sym_name;
7808 if (h != NULL)
7809 sym_name = h->root.root.string;
7810 else
26c61ae5
L
7811 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7812 sym_sec);
411e1bfb 7813
1e2f5b6e 7814 (*_bfd_error_handler)
d003868e
AM
7815 (_("%B: undefined sym `%s' in .opd section"),
7816 ibfd, sym_name);
51aecdc5 7817 broken = TRUE;
1e2f5b6e
AM
7818 break;
7819 }
7820
51020317
AM
7821 /* opd entries are always for functions defined in the
7822 current input bfd. If the symbol isn't defined in the
7823 input bfd, then we won't be using the function in this
7824 bfd; It must be defined in a linkonce section in another
7825 bfd, or is weak. It's also possible that we are
7826 discarding the function due to a linker script /DISCARD/,
7827 which we test for via the output_section. */
7828 if (sym_sec->owner != ibfd
7829 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7830 need_edit = TRUE;
1e2f5b6e 7831
50bc7936 7832 rel += 2;
51aecdc5
AM
7833 if (rel + 1 == relend
7834 || (rel + 2 < relend
7835 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7836 ++rel;
7837
7838 if (rel == relend)
3f764659
JJ
7839 {
7840 if (sec->size == offset + 24)
7841 {
7842 need_pad = NULL;
7843 break;
7844 }
51aecdc5 7845 if (sec->size == offset + 16)
3f764659
JJ
7846 {
7847 cnt_16b++;
7848 break;
7849 }
7850 goto broken_opd;
7851 }
3f764659
JJ
7852 else if (rel + 1 < relend
7853 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7854 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7855 {
51aecdc5
AM
7856 if (rel[0].r_offset == offset + 16)
7857 cnt_16b++;
7858 else if (rel[0].r_offset != offset + 24)
7859 goto broken_opd;
3f764659
JJ
7860 }
7861 else
7862 goto broken_opd;
1e2f5b6e
AM
7863 }
7864
e7d1c40c 7865 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7866
51aecdc5 7867 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7868 {
7869 Elf_Internal_Rela *write_rel;
d4730f92 7870 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7871 bfd_byte *rptr, *wptr;
983bddc8 7872 bfd_byte *new_contents;
74f0fb50
AM
7873 bfd_size_type amt;
7874
983bddc8 7875 new_contents = NULL;
51aecdc5 7876 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7877 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7878 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7879 if (opd->adjust == NULL)
7880 return FALSE;
7881 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7882
7883 /* This seems a waste of time as input .opd sections are all
7884 zeros as generated by gcc, but I suppose there's no reason
7885 this will always be so. We might start putting something in
7886 the third word of .opd entries. */
7887 if ((sec->flags & SEC_IN_MEMORY) == 0)
7888 {
eea6121a
AM
7889 bfd_byte *loc;
7890 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7891 {
eea6121a
AM
7892 if (loc != NULL)
7893 free (loc);
50bc7936 7894 error_ret:
6cdc0ccc
AM
7895 if (local_syms != NULL
7896 && symtab_hdr->contents != (unsigned char *) local_syms)
7897 free (local_syms);
6cdc0ccc
AM
7898 if (elf_section_data (sec)->relocs != relstart)
7899 free (relstart);
b34976b6 7900 return FALSE;
6cdc0ccc 7901 }
1e2f5b6e
AM
7902 sec->contents = loc;
7903 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7904 }
7905
7906 elf_section_data (sec)->relocs = relstart;
7907
3f764659 7908 new_contents = sec->contents;
3f764659
JJ
7909 if (add_aux_fields)
7910 {
7911 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7912 if (new_contents == NULL)
7913 return FALSE;
51aecdc5 7914 need_pad = NULL;
3f764659 7915 }
b4f4e59f
AM
7916 wptr = new_contents;
7917 rptr = sec->contents;
1e2f5b6e 7918 write_rel = relstart;
51aecdc5 7919 for (rel = relstart; rel < relend; )
1e2f5b6e 7920 {
50bc7936
AM
7921 unsigned long r_symndx;
7922 asection *sym_sec;
7923 struct elf_link_hash_entry *h;
51aecdc5 7924 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7925 Elf_Internal_Sym *sym;
51aecdc5
AM
7926 long opd_ent_size;
7927 Elf_Internal_Rela *next_rel;
7928 bfd_boolean skip;
50bc7936
AM
7929
7930 r_symndx = ELF64_R_SYM (rel->r_info);
7931 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7932 r_symndx, ibfd))
50bc7936
AM
7933 goto error_ret;
7934
51aecdc5
AM
7935 next_rel = rel + 2;
7936 if (next_rel + 1 == relend
7937 || (next_rel + 2 < relend
7938 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7939 ++next_rel;
7940
7941 /* See if the .opd entry is full 24 byte or
7942 16 byte (with fd_aux entry overlapped with next
7943 fd_func). */
7944 opd_ent_size = 24;
7945 if (next_rel == relend)
1e2f5b6e 7946 {
51aecdc5 7947 if (sec->size == rel->r_offset + 16)
3f764659 7948 opd_ent_size = 16;
51aecdc5
AM
7949 }
7950 else if (next_rel->r_offset == rel->r_offset + 16)
7951 opd_ent_size = 16;
3f764659 7952
51aecdc5
AM
7953 if (h != NULL
7954 && h->root.root.string[0] == '.')
7955 {
7956 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
7957 if (fdh != NULL
7958 && fdh->elf.root.type != bfd_link_hash_defined
7959 && fdh->elf.root.type != bfd_link_hash_defweak)
7960 fdh = NULL;
7961 }
1e2f5b6e 7962
51aecdc5
AM
7963 skip = (sym_sec->owner != ibfd
7964 || sym_sec->output_section == bfd_abs_section_ptr);
7965 if (skip)
7966 {
7967 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7968 {
51aecdc5
AM
7969 /* Arrange for the function descriptor sym
7970 to be dropped. */
7971 fdh->elf.root.u.def.value = 0;
7972 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7973 }
51aecdc5 7974 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7975
0e1862bb 7976 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7977 rel = next_rel;
7978 else
7979 while (1)
7980 {
7981 if (!dec_dynrel_count (rel->r_info, sec, info,
7982 NULL, h, sym))
7983 goto error_ret;
754021d0 7984
51aecdc5
AM
7985 if (++rel == next_rel)
7986 break;
1e2f5b6e 7987
51aecdc5
AM
7988 r_symndx = ELF64_R_SYM (rel->r_info);
7989 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7990 r_symndx, ibfd))
7991 goto error_ret;
7992 }
50bc7936
AM
7993 }
7994 else
1e2f5b6e 7995 {
51aecdc5
AM
7996 /* We'll be keeping this opd entry. */
7997 long adjust;
7998
7999 if (fdh != NULL)
8000 {
8001 /* Redefine the function descriptor symbol to
8002 this location in the opd section. It is
8003 necessary to update the value here rather
8004 than using an array of adjustments as we do
8005 for local symbols, because various places
8006 in the generic ELF code use the value
8007 stored in u.def.value. */
8008 fdh->elf.root.u.def.value = wptr - new_contents;
8009 fdh->adjust_done = 1;
8010 }
8011
8012 /* Local syms are a bit tricky. We could
8013 tweak them as they can be cached, but
8014 we'd need to look through the local syms
8015 for the function descriptor sym which we
8016 don't have at the moment. So keep an
8017 array of adjustments. */
8018 adjust = (wptr - new_contents) - (rptr - sec->contents);
8019 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8020
8021 if (wptr != rptr)
8022 memcpy (wptr, rptr, opd_ent_size);
8023 wptr += opd_ent_size;
8024 if (add_aux_fields && opd_ent_size == 16)
8025 {
8026 memset (wptr, '\0', 8);
8027 wptr += 8;
8028 }
8029
50bc7936 8030 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8031 new opd entries. */
8032 for ( ; rel != next_rel; ++rel)
8033 {
8034 rel->r_offset += adjust;
8035 if (write_rel != rel)
8036 memcpy (write_rel, rel, sizeof (*rel));
8037 ++write_rel;
8038 }
1e2f5b6e 8039 }
51aecdc5
AM
8040
8041 rptr += opd_ent_size;
1e2f5b6e
AM
8042 }
8043
3f764659 8044 sec->size = wptr - new_contents;
1e2f5b6e 8045 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8046 if (add_aux_fields)
8047 {
8048 free (sec->contents);
8049 sec->contents = new_contents;
8050 }
8051
05bf9422 8052 /* Fudge the header size too, as this is used later in
cdcf6e38 8053 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8054 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8055 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8056 some_edited = TRUE;
1e2f5b6e 8057 }
6cdc0ccc 8058 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8059 free (relstart);
6cdc0ccc 8060
411e1bfb
AM
8061 if (local_syms != NULL
8062 && symtab_hdr->contents != (unsigned char *) local_syms)
8063 {
8064 if (!info->keep_memory)
8065 free (local_syms);
8066 else
8067 symtab_hdr->contents = (unsigned char *) local_syms;
8068 }
8069 }
8070
754021d0
AM
8071 if (some_edited)
8072 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8073
3f764659
JJ
8074 /* If we are doing a final link and the last .opd entry is just 16 byte
8075 long, add a 8 byte padding after it. */
0e1862bb 8076 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8077 {
8078 bfd_byte *p;
8079
8080 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8081 {
8082 BFD_ASSERT (need_pad->size > 0);
8083
8084 p = bfd_malloc (need_pad->size + 8);
8085 if (p == NULL)
8086 return FALSE;
699733f6 8087
3f764659
JJ
8088 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8089 p, 0, need_pad->size))
8090 return FALSE;
8091
8092 need_pad->contents = p;
8093 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8094 }
8095 else
8096 {
8097 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8098 if (p == NULL)
8099 return FALSE;
8100
8101 need_pad->contents = p;
8102 }
8103
8104 memset (need_pad->contents + need_pad->size, 0, 8);
8105 need_pad->size += 8;
8106 }
8107
411e1bfb
AM
8108 return TRUE;
8109}
8110
e1918d23 8111/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8112
e1918d23 8113asection *
e7d1c40c 8114ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8115{
411e1bfb
AM
8116 struct ppc_link_hash_table *htab;
8117
411e1bfb 8118 htab = ppc_hash_table (info);
4dfe6ac6
NC
8119 if (htab == NULL)
8120 return NULL;
8121
ee67d69a
AM
8122 if (abiversion (info->output_bfd) == 1)
8123 htab->opd_abi = 1;
8124
e7d1c40c 8125 if (htab->params->no_multi_toc)
33c0ec9d
AM
8126 htab->do_multi_toc = 0;
8127 else if (!htab->do_multi_toc)
e7d1c40c 8128 htab->params->no_multi_toc = 1;
33c0ec9d 8129
3a71aa26
AM
8130 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8131 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8132 FALSE, FALSE, TRUE));
a7f2871e
AM
8133 /* Move dynamic linking info to the function descriptor sym. */
8134 if (htab->tls_get_addr != NULL)
8135 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8136 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8137 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8138 FALSE, FALSE, TRUE));
7c9cf415 8139 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8140 {
8141 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8142
8143 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8144 FALSE, FALSE, TRUE);
8145 if (opt != NULL)
8146 func_desc_adjust (opt, info);
8147 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8148 FALSE, FALSE, TRUE);
8149 if (opt_fd != NULL
8150 && (opt_fd->root.type == bfd_link_hash_defined
8151 || opt_fd->root.type == bfd_link_hash_defweak))
8152 {
8153 /* If glibc supports an optimized __tls_get_addr call stub,
8154 signalled by the presence of __tls_get_addr_opt, and we'll
8155 be calling __tls_get_addr via a plt call stub, then
8156 make __tls_get_addr point to __tls_get_addr_opt. */
8157 tga_fd = &htab->tls_get_addr_fd->elf;
8158 if (htab->elf.dynamic_sections_created
8159 && tga_fd != NULL
8160 && (tga_fd->type == STT_FUNC
8161 || tga_fd->needs_plt)
8162 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8163 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8164 && tga_fd->root.type == bfd_link_hash_undefweak)))
8165 {
8166 struct plt_entry *ent;
8167
8168 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8169 if (ent->plt.refcount > 0)
8170 break;
8171 if (ent != NULL)
8172 {
8173 tga_fd->root.type = bfd_link_hash_indirect;
8174 tga_fd->root.u.i.link = &opt_fd->root;
8175 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8176 if (opt_fd->dynindx != -1)
8177 {
8178 /* Use __tls_get_addr_opt in dynamic relocations. */
8179 opt_fd->dynindx = -1;
8180 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8181 opt_fd->dynstr_index);
8182 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8183 return NULL;
a7f2871e
AM
8184 }
8185 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8186 tga = &htab->tls_get_addr->elf;
8187 if (opt != NULL && tga != NULL)
8188 {
8189 tga->root.type = bfd_link_hash_indirect;
8190 tga->root.u.i.link = &opt->root;
8191 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8192 _bfd_elf_link_hash_hide_symbol (info, opt,
8193 tga->forced_local);
8194 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8195 }
8196 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8197 htab->tls_get_addr_fd->is_func_descriptor = 1;
8198 if (htab->tls_get_addr != NULL)
8199 {
8200 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8201 htab->tls_get_addr->is_func = 1;
8202 }
8203 }
8204 }
8205 }
7c9cf415
AM
8206 else if (htab->params->tls_get_addr_opt < 0)
8207 htab->params->tls_get_addr_opt = 0;
a7f2871e 8208 }
33c0ec9d 8209 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8210}
8387904d 8211
3a71aa26
AM
8212/* Return TRUE iff REL is a branch reloc with a global symbol matching
8213 HASH1 or HASH2. */
8387904d 8214
3a71aa26
AM
8215static bfd_boolean
8216branch_reloc_hash_match (const bfd *ibfd,
8217 const Elf_Internal_Rela *rel,
8218 const struct ppc_link_hash_entry *hash1,
8219 const struct ppc_link_hash_entry *hash2)
8220{
8221 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8222 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8223 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8224
e054468f 8225 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8226 {
3a71aa26
AM
8227 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8228 struct elf_link_hash_entry *h;
8387904d 8229
3a71aa26 8230 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8231 h = elf_follow_link (h);
3a71aa26
AM
8232 if (h == &hash1->elf || h == &hash2->elf)
8233 return TRUE;
a48ebf4d 8234 }
3a71aa26 8235 return FALSE;
951fd09b 8236}
411e1bfb 8237
951fd09b
AM
8238/* Run through all the TLS relocs looking for optimization
8239 opportunities. The linker has been hacked (see ppc64elf.em) to do
8240 a preliminary section layout so that we know the TLS segment
8241 offsets. We can't optimize earlier because some optimizations need
8242 to know the tp offset, and we need to optimize before allocating
8243 dynamic relocations. */
8244
8245bfd_boolean
33c0ec9d 8246ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8247{
8248 bfd *ibfd;
8249 asection *sec;
8250 struct ppc_link_hash_table *htab;
663a1470 8251 unsigned char *toc_ref;
102890f0 8252 int pass;
951fd09b 8253
3cbc1e5e 8254 if (!bfd_link_executable (info))
411e1bfb
AM
8255 return TRUE;
8256
951fd09b 8257 htab = ppc_hash_table (info);
4dfe6ac6
NC
8258 if (htab == NULL)
8259 return FALSE;
8260
663a1470
AM
8261 /* Make two passes over the relocs. On the first pass, mark toc
8262 entries involved with tls relocs, and check that tls relocs
8263 involved in setting up a tls_get_addr call are indeed followed by
8264 such a call. If they are not, we can't do any tls optimization.
8265 On the second pass twiddle tls_mask flags to notify
8266 relocate_section that optimization can be done, and adjust got
8267 and plt refcounts. */
8268 toc_ref = NULL;
8269 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8270 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8271 {
8272 Elf_Internal_Sym *locsyms = NULL;
8273 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8274
102890f0
AM
8275 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8276 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8277 {
8278 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8279 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8280
102890f0
AM
8281 /* Read the relocations. */
8282 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8283 info->keep_memory);
8284 if (relstart == NULL)
2915c55b
JK
8285 {
8286 free (toc_ref);
8287 return FALSE;
8288 }
411e1bfb 8289
102890f0
AM
8290 relend = relstart + sec->reloc_count;
8291 for (rel = relstart; rel < relend; rel++)
8292 {
8293 enum elf_ppc64_reloc_type r_type;
8294 unsigned long r_symndx;
8295 struct elf_link_hash_entry *h;
8296 Elf_Internal_Sym *sym;
8297 asection *sym_sec;
f961d9dd
AM
8298 unsigned char *tls_mask;
8299 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8300 bfd_vma value;
8301 bfd_boolean ok_tprel, is_local;
8302 long toc_ref_index = 0;
8303 int expecting_tls_get_addr = 0;
663a1470 8304 bfd_boolean ret = FALSE;
411e1bfb 8305
102890f0
AM
8306 r_symndx = ELF64_R_SYM (rel->r_info);
8307 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8308 r_symndx, ibfd))
8309 {
8310 err_free_rel:
8311 if (elf_section_data (sec)->relocs != relstart)
8312 free (relstart);
8313 if (toc_ref != NULL)
8314 free (toc_ref);
8315 if (locsyms != NULL
0ffa91dd 8316 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8317 != (unsigned char *) locsyms))
8318 free (locsyms);
663a1470 8319 return ret;
102890f0 8320 }
411e1bfb 8321
102890f0
AM
8322 if (h != NULL)
8323 {
766bc656
AM
8324 if (h->root.type == bfd_link_hash_defined
8325 || h->root.type == bfd_link_hash_defweak)
8326 value = h->root.u.def.value;
8327 else if (h->root.type == bfd_link_hash_undefweak)
8328 value = 0;
8329 else
663a1470
AM
8330 {
8331 found_tls_get_addr_arg = 0;
8332 continue;
8333 }
102890f0
AM
8334 }
8335 else
8336 /* Symbols referenced by TLS relocs must be of type
8337 STT_TLS. So no need for .opd local sym adjust. */
8338 value = sym->st_value;
8339
8340 ok_tprel = FALSE;
8341 is_local = FALSE;
8342 if (h == NULL
8343 || !h->def_dynamic)
8344 {
8345 is_local = TRUE;
766bc656
AM
8346 if (h != NULL
8347 && h->root.type == bfd_link_hash_undefweak)
8348 ok_tprel = TRUE;
8349 else
8350 {
8351 value += sym_sec->output_offset;
8352 value += sym_sec->output_section->vma;
8353 value -= htab->elf.tls_sec->vma;
8354 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8355 < (bfd_vma) 1 << 32);
8356 }
102890f0 8357 }
951fd09b 8358
102890f0 8359 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8360 /* If this section has old-style __tls_get_addr calls
8361 without marker relocs, then check that each
8362 __tls_get_addr call reloc is preceded by a reloc
8363 that conceivably belongs to the __tls_get_addr arg
8364 setup insn. If we don't find matching arg setup
8365 relocs, don't do any tls optimization. */
8366 if (pass == 0
8367 && sec->has_tls_get_addr_call
8368 && h != NULL
8369 && (h == &htab->tls_get_addr->elf
8370 || h == &htab->tls_get_addr_fd->elf)
8371 && !found_tls_get_addr_arg
8372 && is_branch_reloc (r_type))
8373 {
25f53a85 8374 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8375 "TLS optimization disabled\n"),
8376 ibfd, sec, rel->r_offset);
8377 ret = TRUE;
8378 goto err_free_rel;
8379 }
8380
8381 found_tls_get_addr_arg = 0;
102890f0
AM
8382 switch (r_type)
8383 {
8384 case R_PPC64_GOT_TLSLD16:
8385 case R_PPC64_GOT_TLSLD16_LO:
8386 expecting_tls_get_addr = 1;
663a1470 8387 found_tls_get_addr_arg = 1;
102890f0
AM
8388 /* Fall thru */
8389
8390 case R_PPC64_GOT_TLSLD16_HI:
8391 case R_PPC64_GOT_TLSLD16_HA:
8392 /* These relocs should never be against a symbol
8393 defined in a shared lib. Leave them alone if
8394 that turns out to be the case. */
8395 if (!is_local)
8396 continue;
411e1bfb 8397
102890f0 8398 /* LD -> LE */
411e1bfb 8399 tls_set = 0;
102890f0
AM
8400 tls_clear = TLS_LD;
8401 tls_type = TLS_TLS | TLS_LD;
8402 break;
411e1bfb 8403
102890f0
AM
8404 case R_PPC64_GOT_TLSGD16:
8405 case R_PPC64_GOT_TLSGD16_LO:
8406 expecting_tls_get_addr = 1;
663a1470 8407 found_tls_get_addr_arg = 1;
102890f0
AM
8408 /* Fall thru */
8409
8410 case R_PPC64_GOT_TLSGD16_HI:
8411 case R_PPC64_GOT_TLSGD16_HA:
8412 if (ok_tprel)
8413 /* GD -> LE */
411e1bfb 8414 tls_set = 0;
102890f0
AM
8415 else
8416 /* GD -> IE */
8417 tls_set = TLS_TLS | TLS_TPRELGD;
8418 tls_clear = TLS_GD;
8419 tls_type = TLS_TLS | TLS_GD;
8420 break;
8421
8422 case R_PPC64_GOT_TPREL16_DS:
8423 case R_PPC64_GOT_TPREL16_LO_DS:
8424 case R_PPC64_GOT_TPREL16_HI:
8425 case R_PPC64_GOT_TPREL16_HA:
8426 if (ok_tprel)
8427 {
8428 /* IE -> LE */
8429 tls_set = 0;
8430 tls_clear = TLS_TPREL;
8431 tls_type = TLS_TLS | TLS_TPREL;
8432 break;
8433 }
411e1bfb
AM
8434 continue;
8435
727fc41e
AM
8436 case R_PPC64_TLSGD:
8437 case R_PPC64_TLSLD:
663a1470
AM
8438 found_tls_get_addr_arg = 1;
8439 /* Fall thru */
8440
8441 case R_PPC64_TLS:
8442 case R_PPC64_TOC16:
8443 case R_PPC64_TOC16_LO:
102890f0
AM
8444 if (sym_sec == NULL || sym_sec != toc)
8445 continue;
8446
8447 /* Mark this toc entry as referenced by a TLS
8448 code sequence. We can do that now in the
8449 case of R_PPC64_TLS, and after checking for
8450 tls_get_addr for the TOC16 relocs. */
8451 if (toc_ref == NULL)
663a1470
AM
8452 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8453 if (toc_ref == NULL)
8454 goto err_free_rel;
8455
102890f0
AM
8456 if (h != NULL)
8457 value = h->root.u.def.value;
8458 else
8459 value = sym->st_value;
8460 value += rel->r_addend;
73242275
AM
8461 if (value % 8 != 0)
8462 continue;
8463 BFD_ASSERT (value < toc->size
8464 && toc->output_offset % 8 == 0);
663a1470 8465 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8466 if (r_type == R_PPC64_TLS
8467 || r_type == R_PPC64_TLSGD
8468 || r_type == R_PPC64_TLSLD)
102890f0
AM
8469 {
8470 toc_ref[toc_ref_index] = 1;
8471 continue;
8472 }
8473
8474 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8475 continue;
8476
8477 tls_set = 0;
8478 tls_clear = 0;
8479 expecting_tls_get_addr = 2;
8480 break;
8481
8482 case R_PPC64_TPREL64:
8483 if (pass == 0
8484 || sec != toc
8485 || toc_ref == NULL
663a1470 8486 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8487 continue;
8488 if (ok_tprel)
8489 {
8490 /* IE -> LE */
8491 tls_set = TLS_EXPLICIT;
8492 tls_clear = TLS_TPREL;
8493 break;
8494 }
8495 continue;
8496
8497 case R_PPC64_DTPMOD64:
8498 if (pass == 0
8499 || sec != toc
8500 || toc_ref == NULL
663a1470 8501 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8502 continue;
8503 if (rel + 1 < relend
8504 && (rel[1].r_info
8505 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8506 && rel[1].r_offset == rel->r_offset + 8)
8507 {
8508 if (ok_tprel)
8509 /* GD -> LE */
8510 tls_set = TLS_EXPLICIT | TLS_GD;
8511 else
8512 /* GD -> IE */
8513 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8514 tls_clear = TLS_GD;
8515 }
8516 else
8517 {
8518 if (!is_local)
8519 continue;
8520
8521 /* LD -> LE */
8522 tls_set = TLS_EXPLICIT;
8523 tls_clear = TLS_LD;
8524 }
8525 break;
8526
8527 default:
8528 continue;
8529 }
8530
8531 if (pass == 0)
8532 {
727fc41e
AM
8533 if (!expecting_tls_get_addr
8534 || !sec->has_tls_get_addr_call)
102890f0
AM
8535 continue;
8536
3a71aa26
AM
8537 if (rel + 1 < relend
8538 && branch_reloc_hash_match (ibfd, rel + 1,
8539 htab->tls_get_addr,
8540 htab->tls_get_addr_fd))
102890f0 8541 {
3a71aa26 8542 if (expecting_tls_get_addr == 2)
102890f0 8543 {
3a71aa26 8544 /* Check for toc tls entries. */
f961d9dd 8545 unsigned char *toc_tls;
3a71aa26
AM
8546 int retval;
8547
8548 retval = get_tls_mask (&toc_tls, NULL, NULL,
8549 &locsyms,
8550 rel, ibfd);
8551 if (retval == 0)
8552 goto err_free_rel;
663a1470
AM
8553 if (toc_tls != NULL)
8554 {
8555 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8556 found_tls_get_addr_arg = 1;
8557 if (retval > 1)
8558 toc_ref[toc_ref_index] = 1;
8559 }
102890f0 8560 }
3a71aa26 8561 continue;
102890f0
AM
8562 }
8563
8564 if (expecting_tls_get_addr != 1)
8565 continue;
8566
8567 /* Uh oh, we didn't find the expected call. We
8568 could just mark this symbol to exclude it
8569 from tls optimization but it's safer to skip
663a1470 8570 the entire optimization. */
25f53a85 8571 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8572 "TLS optimization disabled\n"),
8573 ibfd, sec, rel->r_offset);
8574 ret = TRUE;
8575 goto err_free_rel;
102890f0
AM
8576 }
8577
85f7a9cb 8578 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8579 {
8580 struct plt_entry *ent;
8581 for (ent = htab->tls_get_addr->elf.plt.plist;
8582 ent != NULL;
8583 ent = ent->next)
8584 if (ent->addend == 0)
411e1bfb 8585 {
102890f0 8586 if (ent->plt.refcount > 0)
30038c59 8587 {
102890f0
AM
8588 ent->plt.refcount -= 1;
8589 expecting_tls_get_addr = 0;
30038c59 8590 }
102890f0 8591 break;
411e1bfb 8592 }
102890f0 8593 }
411e1bfb 8594
85f7a9cb 8595 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8596 {
8597 struct plt_entry *ent;
8598 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8599 ent != NULL;
8600 ent = ent->next)
8601 if (ent->addend == 0)
411e1bfb 8602 {
102890f0
AM
8603 if (ent->plt.refcount > 0)
8604 ent->plt.refcount -= 1;
8605 break;
411e1bfb 8606 }
102890f0 8607 }
411e1bfb 8608
102890f0 8609 if (tls_clear == 0)
30038c59
AM
8610 continue;
8611
102890f0
AM
8612 if ((tls_set & TLS_EXPLICIT) == 0)
8613 {
8614 struct got_entry *ent;
411e1bfb 8615
102890f0
AM
8616 /* Adjust got entry for this reloc. */
8617 if (h != NULL)
8618 ent = h->got.glist;
8619 else
8620 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8621
102890f0
AM
8622 for (; ent != NULL; ent = ent->next)
8623 if (ent->addend == rel->r_addend
8624 && ent->owner == ibfd
8625 && ent->tls_type == tls_type)
8626 break;
8627 if (ent == NULL)
8628 abort ();
411e1bfb 8629
102890f0
AM
8630 if (tls_set == 0)
8631 {
8632 /* We managed to get rid of a got entry. */
8633 if (ent->got.refcount > 0)
8634 ent->got.refcount -= 1;
8635 }
8636 }
8637 else
8638 {
8639 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8640 we'll lose one or two dyn relocs. */
8641 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8642 NULL, h, sym))
102890f0 8643 return FALSE;
411e1bfb 8644
102890f0
AM
8645 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8646 {
8647 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8648 NULL, h, sym))
102890f0
AM
8649 return FALSE;
8650 }
8651 }
411e1bfb 8652
102890f0
AM
8653 *tls_mask |= tls_set;
8654 *tls_mask &= ~tls_clear;
8655 }
8c1d1bb8 8656
102890f0
AM
8657 if (elf_section_data (sec)->relocs != relstart)
8658 free (relstart);
8659 }
411e1bfb 8660
663a1470
AM
8661 if (locsyms != NULL
8662 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8663 {
8664 if (!info->keep_memory)
8665 free (locsyms);
8666 else
8667 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8668 }
8669 }
411e1bfb 8670
663a1470
AM
8671 if (toc_ref != NULL)
8672 free (toc_ref);
b34976b6 8673 return TRUE;
1e2f5b6e 8674}
b34976b6 8675
c5614fa4
AM
8676/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8677 the values of any global symbols in a toc section that has been
8678 edited. Globals in toc sections should be a rarity, so this function
8679 sets a flag if any are found in toc sections other than the one just
8680 edited, so that futher hash table traversals can be avoided. */
8681
8682struct adjust_toc_info
8683{
8684 asection *toc;
8685 unsigned long *skip;
8686 bfd_boolean global_toc_syms;
8687};
8688
ba761f19
AM
8689enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8690
c5614fa4
AM
8691static bfd_boolean
8692adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8693{
8694 struct ppc_link_hash_entry *eh;
8695 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8696 unsigned long i;
c5614fa4 8697
c5614fa4
AM
8698 if (h->root.type != bfd_link_hash_defined
8699 && h->root.type != bfd_link_hash_defweak)
8700 return TRUE;
8701
8702 eh = (struct ppc_link_hash_entry *) h;
8703 if (eh->adjust_done)
8704 return TRUE;
8705
8706 if (eh->elf.root.u.def.section == toc_inf->toc)
8707 {
854b41e7
AM
8708 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8709 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8710 else
854b41e7
AM
8711 i = eh->elf.root.u.def.value >> 3;
8712
ba761f19 8713 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8714 {
8715 (*_bfd_error_handler)
854b41e7
AM
8716 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8717 do
8718 ++i;
ba761f19 8719 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8720 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8721 }
854b41e7
AM
8722
8723 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8724 eh->adjust_done = 1;
8725 }
8726 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8727 toc_inf->global_toc_syms = TRUE;
8728
8729 return TRUE;
8730}
8731
560c8763
AM
8732/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8733
8734static bfd_boolean
8735ok_lo_toc_insn (unsigned int insn)
8736{
8737 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8738 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8739 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8740 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8741 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8742 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8743 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8744 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8745 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8746 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8747 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8748 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8749 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8750 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8751 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8752 && (insn & 3) != 1)
8753 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8754 && ((insn & 3) == 0 || (insn & 3) == 3))
8755 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8756}
8757
c5614fa4
AM
8758/* Examine all relocs referencing .toc sections in order to remove
8759 unused .toc entries. */
8760
8761bfd_boolean
33c0ec9d 8762ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8763{
8764 bfd *ibfd;
8765 struct adjust_toc_info toc_inf;
67f0cbdb 8766 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8767
67f0cbdb 8768 htab->do_toc_opt = 1;
c5614fa4 8769 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8770 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8771 {
8772 asection *toc, *sec;
8773 Elf_Internal_Shdr *symtab_hdr;
8774 Elf_Internal_Sym *local_syms;
425b145b 8775 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8776 unsigned long *skip, *drop;
8777 unsigned char *used;
8778 unsigned char *keep, last, some_unused;
8779
854b41e7
AM
8780 if (!is_ppc64_elf (ibfd))
8781 continue;
8782
c5614fa4
AM
8783 toc = bfd_get_section_by_name (ibfd, ".toc");
8784 if (toc == NULL
92b7a70f 8785 || toc->size == 0
dbaa2011
AM
8786 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8787 || discarded_section (toc))
c5614fa4
AM
8788 continue;
8789
425b145b 8790 toc_relocs = NULL;
c5614fa4 8791 local_syms = NULL;
0ffa91dd 8792 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8793
8794 /* Look at sections dropped from the final link. */
8795 skip = NULL;
8796 relstart = NULL;
8797 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8798 {
8799 if (sec->reloc_count == 0
dbaa2011 8800 || !discarded_section (sec)
c5614fa4
AM
8801 || get_opd_info (sec)
8802 || (sec->flags & SEC_ALLOC) == 0
8803 || (sec->flags & SEC_DEBUGGING) != 0)
8804 continue;
8805
8806 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8807 if (relstart == NULL)
8808 goto error_ret;
8809
8810 /* Run through the relocs to see which toc entries might be
8811 unused. */
8812 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8813 {
8814 enum elf_ppc64_reloc_type r_type;
8815 unsigned long r_symndx;
8816 asection *sym_sec;
8817 struct elf_link_hash_entry *h;
8818 Elf_Internal_Sym *sym;
8819 bfd_vma val;
8820
8821 r_type = ELF64_R_TYPE (rel->r_info);
8822 switch (r_type)
8823 {
8824 default:
8825 continue;
8826
8827 case R_PPC64_TOC16:
8828 case R_PPC64_TOC16_LO:
8829 case R_PPC64_TOC16_HI:
8830 case R_PPC64_TOC16_HA:
8831 case R_PPC64_TOC16_DS:
8832 case R_PPC64_TOC16_LO_DS:
8833 break;
8834 }
8835
8836 r_symndx = ELF64_R_SYM (rel->r_info);
8837 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8838 r_symndx, ibfd))
8839 goto error_ret;
8840
8841 if (sym_sec != toc)
8842 continue;
8843
8844 if (h != NULL)
8845 val = h->root.u.def.value;
8846 else
8847 val = sym->st_value;
8848 val += rel->r_addend;
8849
8850 if (val >= toc->size)
8851 continue;
8852
8853 /* Anything in the toc ought to be aligned to 8 bytes.
8854 If not, don't mark as unused. */
8855 if (val & 7)
8856 continue;
8857
8858 if (skip == NULL)
8859 {
854b41e7 8860 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8861 if (skip == NULL)
8862 goto error_ret;
8863 }
8864
ba761f19 8865 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8866 }
8867
8868 if (elf_section_data (sec)->relocs != relstart)
8869 free (relstart);
8870 }
8871
ba761f19
AM
8872 /* For largetoc loads of address constants, we can convert
8873 . addis rx,2,addr@got@ha
8874 . ld ry,addr@got@l(rx)
8875 to
8876 . addis rx,2,addr@toc@ha
8877 . addi ry,rx,addr@toc@l
8878 when addr is within 2G of the toc pointer. This then means
8879 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8880
ba761f19
AM
8881 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8882 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8883 && toc->reloc_count != 0)
8884 {
8885 /* Read toc relocs. */
425b145b
AM
8886 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8887 info->keep_memory);
8888 if (toc_relocs == NULL)
ba761f19
AM
8889 goto error_ret;
8890
425b145b 8891 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8892 {
8893 enum elf_ppc64_reloc_type r_type;
8894 unsigned long r_symndx;
8895 asection *sym_sec;
8896 struct elf_link_hash_entry *h;
8897 Elf_Internal_Sym *sym;
8898 bfd_vma val, addr;
8899
8900 r_type = ELF64_R_TYPE (rel->r_info);
8901 if (r_type != R_PPC64_ADDR64)
8902 continue;
8903
8904 r_symndx = ELF64_R_SYM (rel->r_info);
8905 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8906 r_symndx, ibfd))
8907 goto error_ret;
8908
425b145b 8909 if (sym_sec == NULL
dbaa2011 8910 || discarded_section (sym_sec))
425b145b
AM
8911 continue;
8912
afe397ea 8913 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8914 continue;
8915
8916 if (h != NULL)
bddc25c9
AM
8917 {
8918 if (h->type == STT_GNU_IFUNC)
8919 continue;
8920 val = h->root.u.def.value;
8921 }
ba761f19 8922 else
bddc25c9
AM
8923 {
8924 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8925 continue;
8926 val = sym->st_value;
8927 }
ba761f19
AM
8928 val += rel->r_addend;
8929 val += sym_sec->output_section->vma + sym_sec->output_offset;
8930
8931 /* We don't yet know the exact toc pointer value, but we
8932 know it will be somewhere in the toc section. Don't
8933 optimize if the difference from any possible toc
8934 pointer is outside [ff..f80008000, 7fff7fff]. */
8935 addr = toc->output_section->vma + TOC_BASE_OFF;
8936 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8937 continue;
8938
8939 addr = toc->output_section->vma + toc->output_section->rawsize;
8940 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8941 continue;
8942
8943 if (skip == NULL)
8944 {
8945 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8946 if (skip == NULL)
8947 goto error_ret;
8948 }
8949
8950 skip[rel->r_offset >> 3]
425b145b 8951 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8952 }
ba761f19
AM
8953 }
8954
c5614fa4
AM
8955 if (skip == NULL)
8956 continue;
8957
8958 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8959 if (used == NULL)
8960 {
8961 error_ret:
8962 if (local_syms != NULL
8963 && symtab_hdr->contents != (unsigned char *) local_syms)
8964 free (local_syms);
8965 if (sec != NULL
8966 && relstart != NULL
8967 && elf_section_data (sec)->relocs != relstart)
8968 free (relstart);
425b145b
AM
8969 if (toc_relocs != NULL
8970 && elf_section_data (toc)->relocs != toc_relocs)
8971 free (toc_relocs);
c5614fa4
AM
8972 if (skip != NULL)
8973 free (skip);
8974 return FALSE;
8975 }
8976
30038c59
AM
8977 /* Now check all kept sections that might reference the toc.
8978 Check the toc itself last. */
8979 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8980 : ibfd->sections);
c5614fa4 8981 sec != NULL;
c5614fa4 8982 sec = (sec == toc ? NULL
c5614fa4 8983 : sec->next == NULL ? toc
30038c59 8984 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8985 : sec->next))
8986 {
8987 int repeat;
8988
8989 if (sec->reloc_count == 0
dbaa2011 8990 || discarded_section (sec)
c5614fa4
AM
8991 || get_opd_info (sec)
8992 || (sec->flags & SEC_ALLOC) == 0
8993 || (sec->flags & SEC_DEBUGGING) != 0)
8994 continue;
8995
854b41e7
AM
8996 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8997 info->keep_memory);
c5614fa4 8998 if (relstart == NULL)
2915c55b
JK
8999 {
9000 free (used);
9001 goto error_ret;
9002 }
c5614fa4
AM
9003
9004 /* Mark toc entries referenced as used. */
c5614fa4 9005 do
d4f1ee75
AM
9006 {
9007 repeat = 0;
9008 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9009 {
9010 enum elf_ppc64_reloc_type r_type;
9011 unsigned long r_symndx;
9012 asection *sym_sec;
9013 struct elf_link_hash_entry *h;
9014 Elf_Internal_Sym *sym;
9015 bfd_vma val;
9016 enum {no_check, check_lo, check_ha} insn_check;
98528052 9017
d4f1ee75
AM
9018 r_type = ELF64_R_TYPE (rel->r_info);
9019 switch (r_type)
9020 {
9021 default:
9022 insn_check = no_check;
9023 break;
98528052 9024
d4f1ee75
AM
9025 case R_PPC64_GOT_TLSLD16_HA:
9026 case R_PPC64_GOT_TLSGD16_HA:
9027 case R_PPC64_GOT_TPREL16_HA:
9028 case R_PPC64_GOT_DTPREL16_HA:
9029 case R_PPC64_GOT16_HA:
9030 case R_PPC64_TOC16_HA:
9031 insn_check = check_ha;
9032 break;
98528052 9033
d4f1ee75
AM
9034 case R_PPC64_GOT_TLSLD16_LO:
9035 case R_PPC64_GOT_TLSGD16_LO:
9036 case R_PPC64_GOT_TPREL16_LO_DS:
9037 case R_PPC64_GOT_DTPREL16_LO_DS:
9038 case R_PPC64_GOT16_LO:
9039 case R_PPC64_GOT16_LO_DS:
9040 case R_PPC64_TOC16_LO:
9041 case R_PPC64_TOC16_LO_DS:
9042 insn_check = check_lo;
9043 break;
9044 }
560c8763 9045
d4f1ee75
AM
9046 if (insn_check != no_check)
9047 {
9048 bfd_vma off = rel->r_offset & ~3;
9049 unsigned char buf[4];
9050 unsigned int insn;
c5614fa4 9051
d4f1ee75
AM
9052 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9053 {
9054 free (used);
9055 goto error_ret;
9056 }
9057 insn = bfd_get_32 (ibfd, buf);
9058 if (insn_check == check_lo
9059 ? !ok_lo_toc_insn (insn)
9060 : ((insn & ((0x3f << 26) | 0x1f << 16))
9061 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9062 {
9063 char str[12];
9064
9065 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9066 sprintf (str, "%#08x", insn);
9067 info->callbacks->einfo
9068 (_("%P: %H: toc optimization is not supported for"
9069 " %s instruction.\n"),
9070 ibfd, sec, rel->r_offset & ~3, str);
9071 }
9072 }
c5614fa4 9073
d4f1ee75
AM
9074 switch (r_type)
9075 {
9076 case R_PPC64_TOC16:
9077 case R_PPC64_TOC16_LO:
9078 case R_PPC64_TOC16_HI:
9079 case R_PPC64_TOC16_HA:
9080 case R_PPC64_TOC16_DS:
9081 case R_PPC64_TOC16_LO_DS:
9082 /* In case we're taking addresses of toc entries. */
9083 case R_PPC64_ADDR64:
9084 break;
c5614fa4 9085
d4f1ee75
AM
9086 default:
9087 continue;
9088 }
c5614fa4 9089
d4f1ee75
AM
9090 r_symndx = ELF64_R_SYM (rel->r_info);
9091 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9092 r_symndx, ibfd))
9093 {
9094 free (used);
9095 goto error_ret;
9096 }
c5614fa4 9097
d4f1ee75
AM
9098 if (sym_sec != toc)
9099 continue;
c5614fa4 9100
d4f1ee75
AM
9101 if (h != NULL)
9102 val = h->root.u.def.value;
9103 else
9104 val = sym->st_value;
9105 val += rel->r_addend;
ba761f19 9106
d4f1ee75
AM
9107 if (val >= toc->size)
9108 continue;
ba761f19 9109
d4f1ee75
AM
9110 if ((skip[val >> 3] & can_optimize) != 0)
9111 {
9112 bfd_vma off;
9113 unsigned char opc;
9114
9115 switch (r_type)
9116 {
9117 case R_PPC64_TOC16_HA:
ba761f19 9118 break;
ba761f19 9119
d4f1ee75
AM
9120 case R_PPC64_TOC16_LO_DS:
9121 off = rel->r_offset;
9122 off += (bfd_big_endian (ibfd) ? -2 : 3);
9123 if (!bfd_get_section_contents (ibfd, sec, &opc,
9124 off, 1))
9125 {
9126 free (used);
9127 goto error_ret;
9128 }
9129 if ((opc & (0x3f << 2)) == (58u << 2))
9130 break;
9131 /* Fall thru */
ba761f19 9132
d4f1ee75
AM
9133 default:
9134 /* Wrong sort of reloc, or not a ld. We may
9135 as well clear ref_from_discarded too. */
9136 skip[val >> 3] = 0;
9137 }
9138 }
9139
9140 if (sec != toc)
9141 used[val >> 3] = 1;
9142 /* For the toc section, we only mark as used if this
9143 entry itself isn't unused. */
9144 else if ((used[rel->r_offset >> 3]
9145 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9146 && !used[val >> 3])
9147 {
9148 /* Do all the relocs again, to catch reference
9149 chains. */
9150 repeat = 1;
9151 used[val >> 3] = 1;
9152 }
9153 }
9154 }
c5614fa4 9155 while (repeat);
854b41e7
AM
9156
9157 if (elf_section_data (sec)->relocs != relstart)
9158 free (relstart);
c5614fa4
AM
9159 }
9160
9161 /* Merge the used and skip arrays. Assume that TOC
9162 doublewords not appearing as either used or unused belong
9163 to to an entry more than one doubleword in size. */
9164 for (drop = skip, keep = used, last = 0, some_unused = 0;
9165 drop < skip + (toc->size + 7) / 8;
9166 ++drop, ++keep)
9167 {
9168 if (*keep)
9169 {
ba761f19
AM
9170 *drop &= ~ref_from_discarded;
9171 if ((*drop & can_optimize) != 0)
9172 some_unused = 1;
c5614fa4
AM
9173 last = 0;
9174 }
b140b010 9175 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9176 {
9177 some_unused = 1;
ba761f19 9178 last = ref_from_discarded;
c5614fa4
AM
9179 }
9180 else
9181 *drop = last;
9182 }
9183
9184 free (used);
9185
9186 if (some_unused)
9187 {
9188 bfd_byte *contents, *src;
9189 unsigned long off;
d62b3684 9190 Elf_Internal_Sym *sym;
ba761f19 9191 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9192
9193 /* Shuffle the toc contents, and at the same time convert the
9194 skip array from booleans into offsets. */
9195 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9196 goto error_ret;
9197
9198 elf_section_data (toc)->this_hdr.contents = contents;
9199
9200 for (src = contents, off = 0, drop = skip;
9201 src < contents + toc->size;
9202 src += 8, ++drop)
9203 {
ba761f19
AM
9204 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9205 off += 8;
c5614fa4
AM
9206 else if (off != 0)
9207 {
9208 *drop = off;
9209 memcpy (src - off, src, 8);
9210 }
9211 }
854b41e7 9212 *drop = off;
c5614fa4
AM
9213 toc->rawsize = toc->size;
9214 toc->size = src - contents - off;
9215
ba761f19
AM
9216 /* Adjust addends for relocs against the toc section sym,
9217 and optimize any accesses we can. */
c5614fa4
AM
9218 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9219 {
9220 if (sec->reloc_count == 0
dbaa2011 9221 || discarded_section (sec))
c5614fa4
AM
9222 continue;
9223
9224 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9225 info->keep_memory);
c5614fa4
AM
9226 if (relstart == NULL)
9227 goto error_ret;
9228
9229 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9230 {
9231 enum elf_ppc64_reloc_type r_type;
9232 unsigned long r_symndx;
9233 asection *sym_sec;
9234 struct elf_link_hash_entry *h;
854b41e7 9235 bfd_vma val;
c5614fa4
AM
9236
9237 r_type = ELF64_R_TYPE (rel->r_info);
9238 switch (r_type)
9239 {
9240 default:
9241 continue;
9242
9243 case R_PPC64_TOC16:
9244 case R_PPC64_TOC16_LO:
9245 case R_PPC64_TOC16_HI:
9246 case R_PPC64_TOC16_HA:
9247 case R_PPC64_TOC16_DS:
9248 case R_PPC64_TOC16_LO_DS:
9249 case R_PPC64_ADDR64:
9250 break;
9251 }
9252
9253 r_symndx = ELF64_R_SYM (rel->r_info);
9254 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9255 r_symndx, ibfd))
9256 goto error_ret;
9257
ba761f19 9258 if (sym_sec != toc)
c5614fa4
AM
9259 continue;
9260
ba761f19
AM
9261 if (h != NULL)
9262 val = h->root.u.def.value;
9263 else
9264 {
9265 val = sym->st_value;
9266 if (val != 0)
9267 local_toc_syms = TRUE;
9268 }
9269
9270 val += rel->r_addend;
854b41e7
AM
9271
9272 if (val > toc->rawsize)
9273 val = toc->rawsize;
ba761f19
AM
9274 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9275 continue;
9276 else if ((skip[val >> 3] & can_optimize) != 0)
9277 {
9278 Elf_Internal_Rela *tocrel
425b145b 9279 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9280 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9281
9282 switch (r_type)
9283 {
9284 case R_PPC64_TOC16_HA:
9285 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9286 break;
9287
9288 case R_PPC64_TOC16_LO_DS:
9289 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9290 break;
9291
9292 default:
28942f62
AM
9293 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9294 ppc_howto_init ();
b140b010 9295 info->callbacks->einfo
bc30df16 9296 (_("%P: %H: %s references "
b140b010
AM
9297 "optimized away TOC entry\n"),
9298 ibfd, sec, rel->r_offset,
9299 ppc64_elf_howto_table[r_type]->name);
9300 bfd_set_error (bfd_error_bad_value);
9301 goto error_ret;
ba761f19
AM
9302 }
9303 rel->r_addend = tocrel->r_addend;
9304 elf_section_data (sec)->relocs = relstart;
9305 continue;
9306 }
9307
9308 if (h != NULL || sym->st_value != 0)
9309 continue;
854b41e7
AM
9310
9311 rel->r_addend -= skip[val >> 3];
9312 elf_section_data (sec)->relocs = relstart;
c5614fa4 9313 }
854b41e7
AM
9314
9315 if (elf_section_data (sec)->relocs != relstart)
9316 free (relstart);
c5614fa4
AM
9317 }
9318
9319 /* We shouldn't have local or global symbols defined in the TOC,
9320 but handle them anyway. */
df22d223
AM
9321 if (local_syms != NULL)
9322 for (sym = local_syms;
9323 sym < local_syms + symtab_hdr->sh_info;
9324 ++sym)
9325 if (sym->st_value != 0
9326 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9327 {
9328 unsigned long i;
854b41e7 9329
df22d223
AM
9330 if (sym->st_value > toc->rawsize)
9331 i = toc->rawsize >> 3;
9332 else
9333 i = sym->st_value >> 3;
854b41e7 9334
df22d223
AM
9335 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9336 {
9337 if (local_toc_syms)
9338 (*_bfd_error_handler)
9339 (_("%s defined on removed toc entry"),
9340 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9341 do
9342 ++i;
9343 while ((skip[i] & (ref_from_discarded | can_optimize)));
9344 sym->st_value = (bfd_vma) i << 3;
9345 }
d62b3684 9346
df22d223
AM
9347 sym->st_value -= skip[i];
9348 symtab_hdr->contents = (unsigned char *) local_syms;
9349 }
c5614fa4 9350
854b41e7 9351 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9352 if (toc_inf.global_toc_syms)
9353 {
9354 toc_inf.toc = toc;
9355 toc_inf.skip = skip;
9356 toc_inf.global_toc_syms = FALSE;
9357 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9358 &toc_inf);
9359 }
854b41e7
AM
9360
9361 if (toc->reloc_count != 0)
9362 {
d4730f92 9363 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9364 Elf_Internal_Rela *wrel;
9365 bfd_size_type sz;
9366
854b41e7 9367 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9368 if (toc_relocs == NULL)
9369 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9370 info->keep_memory);
9371 if (toc_relocs == NULL)
9372 goto error_ret;
9373
425b145b
AM
9374 wrel = toc_relocs;
9375 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9376 if ((skip[rel->r_offset >> 3]
9377 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9378 {
9379 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9380 wrel->r_info = rel->r_info;
9381 wrel->r_addend = rel->r_addend;
9382 ++wrel;
9383 }
9384 else if (!dec_dynrel_count (rel->r_info, toc, info,
9385 &local_syms, NULL, NULL))
9386 goto error_ret;
9387
425b145b
AM
9388 elf_section_data (toc)->relocs = toc_relocs;
9389 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9390 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9391 sz = rel_hdr->sh_entsize;
9392 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9393 }
c5614fa4 9394 }
28be611c
AM
9395 else if (toc_relocs != NULL
9396 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9397 free (toc_relocs);
c5614fa4
AM
9398
9399 if (local_syms != NULL
9400 && symtab_hdr->contents != (unsigned char *) local_syms)
9401 {
9402 if (!info->keep_memory)
9403 free (local_syms);
9404 else
9405 symtab_hdr->contents = (unsigned char *) local_syms;
9406 }
9407 free (skip);
9408 }
9409
9410 return TRUE;
9411}
9412
1bbe0902
AM
9413/* Return true iff input section I references the TOC using
9414 instructions limited to +/-32k offsets. */
9415
9416bfd_boolean
9417ppc64_elf_has_small_toc_reloc (asection *i)
9418{
9419 return (is_ppc64_elf (i->owner)
9420 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9421}
9422
927be08e
AM
9423/* Allocate space for one GOT entry. */
9424
9425static void
9426allocate_got (struct elf_link_hash_entry *h,
9427 struct bfd_link_info *info,
9428 struct got_entry *gent)
9429{
9430 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9431 bfd_boolean dyn;
9432 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9433 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9434 ? 16 : 8);
9435 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9436 ? 2 : 1) * sizeof (Elf64_External_Rela);
9437 asection *got = ppc64_elf_tdata (gent->owner)->got;
9438
9439 gent->got.offset = got->size;
9440 got->size += entsize;
9441
9442 dyn = htab->elf.dynamic_sections_created;
19e08130 9443 if (h->type == STT_GNU_IFUNC)
927be08e 9444 {
33e44f2e 9445 htab->elf.irelplt->size += rentsize;
19e08130 9446 htab->got_reli_size += rentsize;
927be08e 9447 }
0e1862bb 9448 else if ((bfd_link_pic (info)
19e08130
AM
9449 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9450 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9451 || h->root.type != bfd_link_hash_undefweak))
927be08e 9452 {
19e08130 9453 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9454 relgot->size += rentsize;
927be08e
AM
9455 }
9456}
9457
7865406b
AM
9458/* This function merges got entries in the same toc group. */
9459
9460static void
9461merge_got_entries (struct got_entry **pent)
9462{
9463 struct got_entry *ent, *ent2;
9464
9465 for (ent = *pent; ent != NULL; ent = ent->next)
9466 if (!ent->is_indirect)
9467 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9468 if (!ent2->is_indirect
9469 && ent2->addend == ent->addend
9470 && ent2->tls_type == ent->tls_type
9471 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9472 {
9473 ent2->is_indirect = TRUE;
9474 ent2->got.ent = ent;
9475 }
9476}
9477
65f38f15
AM
9478/* Allocate space in .plt, .got and associated reloc sections for
9479 dynamic relocs. */
5bd4f169 9480
b34976b6 9481static bfd_boolean
4ce794b7 9482allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9483{
65f38f15
AM
9484 struct bfd_link_info *info;
9485 struct ppc_link_hash_table *htab;
5bd4f169 9486 asection *s;
65f38f15 9487 struct ppc_link_hash_entry *eh;
6061a67d 9488 struct elf_dyn_relocs *p;
0b8bcf0d 9489 struct got_entry **pgent, *gent;
5bd4f169 9490
e92d460e 9491 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9492 return TRUE;
5bd4f169 9493
65f38f15
AM
9494 info = (struct bfd_link_info *) inf;
9495 htab = ppc_hash_table (info);
4dfe6ac6
NC
9496 if (htab == NULL)
9497 return FALSE;
5bd4f169 9498
e054468f
AM
9499 if ((htab->elf.dynamic_sections_created
9500 && h->dynindx != -1
0e1862bb 9501 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
e054468f 9502 || h->type == STT_GNU_IFUNC)
5bd4f169 9503 {
411e1bfb
AM
9504 struct plt_entry *pent;
9505 bfd_boolean doneone = FALSE;
9506 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9507 if (pent->plt.refcount > 0)
9508 {
25f23106
AM
9509 if (!htab->elf.dynamic_sections_created
9510 || h->dynindx == -1)
e054468f 9511 {
33e44f2e 9512 s = htab->elf.iplt;
e054468f 9513 pent->plt.offset = s->size;
b9e5796b 9514 s->size += PLT_ENTRY_SIZE (htab);
33e44f2e 9515 s = htab->elf.irelplt;
e054468f
AM
9516 }
9517 else
9518 {
9519 /* If this is the first .plt entry, make room for the special
9520 first entry. */
33e44f2e 9521 s = htab->elf.splt;
e054468f 9522 if (s->size == 0)
b9e5796b 9523 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
e054468f
AM
9524
9525 pent->plt.offset = s->size;
9526
9527 /* Make room for this entry. */
b9e5796b 9528 s->size += PLT_ENTRY_SIZE (htab);
e054468f
AM
9529
9530 /* Make room for the .glink code. */
9531 s = htab->glink;
9532 if (s->size == 0)
9533 s->size += GLINK_CALL_STUB_SIZE;
b9e5796b
AM
9534 if (htab->opd_abi)
9535 {
9536 /* We need bigger stubs past index 32767. */
9537 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9538 s->size += 4;
9539 s->size += 2*4;
9540 }
9541 else
e054468f 9542 s->size += 4;
e054468f
AM
9543
9544 /* We also need to make an entry in the .rela.plt section. */
33e44f2e 9545 s = htab->elf.srelplt;
e054468f 9546 }
eea6121a 9547 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
9548 doneone = TRUE;
9549 }
9550 else
9551 pent->plt.offset = (bfd_vma) -1;
9552 if (!doneone)
65f38f15 9553 {
411e1bfb 9554 h->plt.plist = NULL;
f5385ebf 9555 h->needs_plt = 0;
65f38f15
AM
9556 }
9557 }
9558 else
9559 {
411e1bfb 9560 h->plt.plist = NULL;
f5385ebf 9561 h->needs_plt = 0;
65f38f15
AM
9562 }
9563
951fd09b
AM
9564 eh = (struct ppc_link_hash_entry *) h;
9565 /* Run through the TLS GD got entries first if we're changing them
9566 to TPREL. */
e7b938ca 9567 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9568 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9569 if (gent->got.refcount > 0
9570 && (gent->tls_type & TLS_GD) != 0)
9571 {
9572 /* This was a GD entry that has been converted to TPREL. If
9573 there happens to be a TPREL entry we can use that one. */
9574 struct got_entry *ent;
9575 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9576 if (ent->got.refcount > 0
9577 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9578 && ent->addend == gent->addend
9579 && ent->owner == gent->owner)
951fd09b
AM
9580 {
9581 gent->got.refcount = 0;
9582 break;
9583 }
9584
9585 /* If not, then we'll be using our own TPREL entry. */
9586 if (gent->got.refcount != 0)
9587 gent->tls_type = TLS_TLS | TLS_TPREL;
9588 }
9589
7865406b
AM
9590 /* Remove any list entry that won't generate a word in the GOT before
9591 we call merge_got_entries. Otherwise we risk merging to empty
9592 entries. */
0b8bcf0d
AM
9593 pgent = &h->got.glist;
9594 while ((gent = *pgent) != NULL)
411e1bfb 9595 if (gent->got.refcount > 0)
7865406b
AM
9596 {
9597 if ((gent->tls_type & TLS_LD) != 0
9598 && !h->def_dynamic)
9599 {
9600 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9601 *pgent = gent->next;
9602 }
9603 else
9604 pgent = &gent->next;
9605 }
9606 else
9607 *pgent = gent->next;
9608
9609 if (!htab->do_multi_toc)
9610 merge_got_entries (&h->got.glist);
9611
9612 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9613 if (!gent->is_indirect)
411e1bfb
AM
9614 {
9615 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9616 Undefined weak syms won't yet be marked as dynamic,
9617 nor will all TLS symbols. */
411e1bfb 9618 if (h->dynindx == -1
b099ab9f 9619 && !h->forced_local
25f23106 9620 && h->type != STT_GNU_IFUNC
b099ab9f 9621 && htab->elf.dynamic_sections_created)
411e1bfb 9622 {
c152c796 9623 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9624 return FALSE;
9625 }
65f38f15 9626
0c8d6e5c 9627 if (!is_ppc64_elf (gent->owner))
927be08e 9628 abort ();
0ffa91dd 9629
927be08e 9630 allocate_got (h, info, gent);
411e1bfb 9631 }
65f38f15 9632
b099ab9f 9633 if (eh->dyn_relocs == NULL
25f23106 9634 || (!htab->elf.dynamic_sections_created
14b5f73f 9635 && h->type != STT_GNU_IFUNC))
b34976b6 9636 return TRUE;
65f38f15
AM
9637
9638 /* In the shared -Bsymbolic case, discard space allocated for
9639 dynamic pc-relative relocs against symbols which turn out to be
9640 defined in regular objects. For the normal shared case, discard
9641 space for relocs that have become local due to symbol visibility
9642 changes. */
9643
0e1862bb 9644 if (bfd_link_pic (info))
65f38f15 9645 {
9c7a29a3 9646 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 9647 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
9648 generated via assembly. We want calls to protected symbols to
9649 resolve directly to the function rather than going via the plt.
9650 If people want function pointer comparisons to work as expected
9651 then they should avoid writing weird assembly. */
09695f56 9652 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 9653 {
6061a67d 9654 struct elf_dyn_relocs **pp;
65f38f15
AM
9655
9656 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 9657 {
65f38f15
AM
9658 p->count -= p->pc_count;
9659 p->pc_count = 0;
9660 if (p->count == 0)
9661 *pp = p->next;
9662 else
9663 pp = &p->next;
5bd4f169 9664 }
65f38f15 9665 }
4e795f50
AM
9666
9667 /* Also discard relocs on undefined weak syms with non-default
9668 visibility. */
cab87ef9
AM
9669 if (eh->dyn_relocs != NULL
9670 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
9671 {
9672 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9673 eh->dyn_relocs = NULL;
9674
9675 /* Make sure this symbol is output as a dynamic symbol.
9676 Undefined weak syms won't yet be marked as dynamic. */
9677 else if (h->dynindx == -1
9678 && !h->forced_local)
9679 {
9680 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9681 return FALSE;
9682 }
9683 }
65f38f15 9684 }
25f23106
AM
9685 else if (h->type == STT_GNU_IFUNC)
9686 {
9687 if (!h->non_got_ref)
9688 eh->dyn_relocs = NULL;
9689 }
f4656909 9690 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
9691 {
9692 /* For the non-shared case, discard space for relocs against
9693 symbols which turn out to need copy relocs or are not
9694 dynamic. */
9695
f5385ebf 9696 if (!h->non_got_ref
f5385ebf 9697 && !h->def_regular)
65f38f15
AM
9698 {
9699 /* Make sure this symbol is output as a dynamic symbol.
9700 Undefined weak syms won't yet be marked as dynamic. */
9701 if (h->dynindx == -1
f5385ebf 9702 && !h->forced_local)
65f38f15 9703 {
c152c796 9704 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 9705 return FALSE;
65f38f15
AM
9706 }
9707
9708 /* If that succeeded, we know we'll be keeping all the
9709 relocs. */
9710 if (h->dynindx != -1)
9711 goto keep;
9712 }
9713
9714 eh->dyn_relocs = NULL;
9715
ec338859 9716 keep: ;
65f38f15
AM
9717 }
9718
9719 /* Finally, allocate space. */
9720 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9721 {
9722 asection *sreloc = elf_section_data (p->sec)->sreloc;
19e08130 9723 if (eh->elf.type == STT_GNU_IFUNC)
33e44f2e 9724 sreloc = htab->elf.irelplt;
eea6121a 9725 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9726 }
9727
b34976b6 9728 return TRUE;
65f38f15
AM
9729}
9730
a345bc8d
AM
9731/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9732 to set up space for global entry stubs. These are put in glink,
9733 after the branch table. */
65f38f15 9734
b34976b6 9735static bfd_boolean
a345bc8d 9736size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9737{
a345bc8d
AM
9738 struct bfd_link_info *info;
9739 struct ppc_link_hash_table *htab;
9740 struct plt_entry *pent;
9741 asection *s;
65f38f15 9742
a345bc8d
AM
9743 if (h->root.type == bfd_link_hash_indirect)
9744 return TRUE;
65f38f15 9745
a345bc8d
AM
9746 if (!h->pointer_equality_needed)
9747 return TRUE;
65f38f15 9748
a345bc8d
AM
9749 if (h->def_regular)
9750 return TRUE;
65f38f15 9751
a345bc8d
AM
9752 info = inf;
9753 htab = ppc_hash_table (info);
9754 if (htab == NULL)
9755 return FALSE;
9756
9757 s = htab->glink;
9758 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9759 if (pent->plt.offset != (bfd_vma) -1
9760 && pent->addend == 0)
9761 {
afe397ea
AM
9762 /* For ELFv2, if this symbol is not defined in a regular file
9763 and we are not generating a shared library or pie, then we
9764 need to define the symbol in the executable on a call stub.
9765 This is to avoid text relocations. */
a345bc8d 9766 s->size = (s->size + 15) & -16;
afe397ea
AM
9767 h->root.u.def.section = s;
9768 h->root.u.def.value = s->size;
a345bc8d
AM
9769 s->size += 16;
9770 break;
9771 }
9772 return TRUE;
9773}
9774
9775/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9776 read-only sections. */
9777
9778static bfd_boolean
9779maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9780{
9781 if (h->root.type == bfd_link_hash_indirect)
9782 return TRUE;
9783
9784 if (readonly_dynrelocs (h))
9785 {
9786 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9787
9788 /* Not an error, just cut short the traversal. */
9789 return FALSE;
65f38f15 9790 }
b34976b6 9791 return TRUE;
65f38f15
AM
9792}
9793
9794/* Set the sizes of the dynamic sections. */
9795
b34976b6 9796static bfd_boolean
ee67d69a 9797ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9798 struct bfd_link_info *info)
65f38f15
AM
9799{
9800 struct ppc_link_hash_table *htab;
9801 bfd *dynobj;
9802 asection *s;
b34976b6 9803 bfd_boolean relocs;
65f38f15 9804 bfd *ibfd;
7865406b 9805 struct got_entry *first_tlsld;
65f38f15
AM
9806
9807 htab = ppc_hash_table (info);
4dfe6ac6
NC
9808 if (htab == NULL)
9809 return FALSE;
9810
65f38f15
AM
9811 dynobj = htab->elf.dynobj;
9812 if (dynobj == NULL)
9813 abort ();
9814
9815 if (htab->elf.dynamic_sections_created)
9816 {
9817 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9818 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9819 {
3d4d4302 9820 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9821 if (s == NULL)
9822 abort ();
eea6121a 9823 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9824 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9825 }
9826 }
9827
9828 /* Set up .got offsets for local syms, and space for local dynamic
9829 relocs. */
c72f2fb2 9830 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9831 {
411e1bfb
AM
9832 struct got_entry **lgot_ents;
9833 struct got_entry **end_lgot_ents;
e054468f
AM
9834 struct plt_entry **local_plt;
9835 struct plt_entry **end_local_plt;
f961d9dd 9836 unsigned char *lgot_masks;
65f38f15
AM
9837 bfd_size_type locsymcount;
9838 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9839
0c8d6e5c 9840 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9841 continue;
9842
9843 for (s = ibfd->sections; s != NULL; s = s->next)
9844 {
19e08130 9845 struct ppc_dyn_relocs *p;
65f38f15 9846
6edfbbad 9847 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9848 {
ec338859
AM
9849 if (!bfd_is_abs_section (p->sec)
9850 && bfd_is_abs_section (p->sec->output_section))
9851 {
9852 /* Input section has been discarded, either because
9853 it is a copy of a linkonce section or due to
9854 linker script /DISCARD/, so we'll be discarding
9855 the relocs too. */
9856 }
248866a8 9857 else if (p->count != 0)
ec338859 9858 {
19e08130
AM
9859 asection *srel = elf_section_data (p->sec)->sreloc;
9860 if (p->ifunc)
33e44f2e 9861 srel = htab->elf.irelplt;
eea6121a 9862 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9863 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9864 info->flags |= DF_TEXTREL;
ec338859 9865 }
65f38f15
AM
9866 }
9867 }
9868
411e1bfb
AM
9869 lgot_ents = elf_local_got_ents (ibfd);
9870 if (!lgot_ents)
65f38f15
AM
9871 continue;
9872
0ffa91dd 9873 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9874 locsymcount = symtab_hdr->sh_info;
411e1bfb 9875 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9876 local_plt = (struct plt_entry **) end_lgot_ents;
9877 end_local_plt = local_plt + locsymcount;
f961d9dd 9878 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9879 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9880 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9881 {
0b8bcf0d 9882 struct got_entry **pent, *ent;
411e1bfb 9883
0b8bcf0d
AM
9884 pent = lgot_ents;
9885 while ((ent = *pent) != NULL)
411e1bfb
AM
9886 if (ent->got.refcount > 0)
9887 {
e7b938ca 9888 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9889 {
927be08e 9890 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9891 *pent = ent->next;
411e1bfb
AM
9892 }
9893 else
9894 {
19e08130
AM
9895 unsigned int ent_size = 8;
9896 unsigned int rel_size = sizeof (Elf64_External_Rela);
9897
eea6121a 9898 ent->got.offset = s->size;
e7b938ca 9899 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9900 {
19e08130
AM
9901 ent_size *= 2;
9902 rel_size *= 2;
9903 }
9904 s->size += ent_size;
9905 if ((*lgot_masks & PLT_IFUNC) != 0)
9906 {
33e44f2e 9907 htab->elf.irelplt->size += rel_size;
19e08130
AM
9908 htab->got_reli_size += rel_size;
9909 }
0e1862bb 9910 else if (bfd_link_pic (info))
19e08130
AM
9911 {
9912 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9913 srel->size += rel_size;
927be08e 9914 }
0b8bcf0d 9915 pent = &ent->next;
411e1bfb
AM
9916 }
9917 }
9918 else
0b8bcf0d 9919 *pent = ent->next;
65f38f15 9920 }
e054468f
AM
9921
9922 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9923 for (; local_plt < end_local_plt; ++local_plt)
9924 {
9925 struct plt_entry *ent;
9926
9927 for (ent = *local_plt; ent != NULL; ent = ent->next)
9928 if (ent->plt.refcount > 0)
9929 {
33e44f2e 9930 s = htab->elf.iplt;
e054468f 9931 ent->plt.offset = s->size;
b9e5796b 9932 s->size += PLT_ENTRY_SIZE (htab);
e054468f 9933
33e44f2e 9934 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9935 }
9936 else
9937 ent->plt.offset = (bfd_vma) -1;
9938 }
65f38f15
AM
9939 }
9940
9941 /* Allocate global sym .plt and .got entries, and space for global
9942 sym dynamic relocs. */
4ce794b7 9943 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
9944 /* Stash the end of glink branch table. */
9945 if (htab->glink != NULL)
9946 htab->glink->rawsize = htab->glink->size;
9947
0e1862bb 9948 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9949 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9950
7865406b 9951 first_tlsld = NULL;
c72f2fb2 9952 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9953 {
7865406b
AM
9954 struct got_entry *ent;
9955
0c8d6e5c 9956 if (!is_ppc64_elf (ibfd))
102890f0
AM
9957 continue;
9958
7865406b
AM
9959 ent = ppc64_tlsld_got (ibfd);
9960 if (ent->got.refcount > 0)
102890f0 9961 {
7865406b 9962 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9963 {
7865406b
AM
9964 ent->is_indirect = TRUE;
9965 ent->got.ent = first_tlsld;
9966 }
9967 else
9968 {
9969 if (first_tlsld == NULL)
9970 first_tlsld = ent;
9971 s = ppc64_elf_tdata (ibfd)->got;
9972 ent->got.offset = s->size;
9973 ent->owner = ibfd;
9974 s->size += 16;
0e1862bb 9975 if (bfd_link_pic (info))
7865406b
AM
9976 {
9977 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9978 srel->size += sizeof (Elf64_External_Rela);
9979 }
102890f0
AM
9980 }
9981 }
9982 else
7865406b 9983 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9984 }
9985
65f38f15
AM
9986 /* We now have determined the sizes of the various dynamic sections.
9987 Allocate memory for them. */
b34976b6 9988 relocs = FALSE;
65f38f15
AM
9989 for (s = dynobj->sections; s != NULL; s = s->next)
9990 {
9991 if ((s->flags & SEC_LINKER_CREATED) == 0)
9992 continue;
9993
4ce794b7 9994 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9995 /* These haven't been allocated yet; don't strip. */
9996 continue;
33e44f2e
AM
9997 else if (s == htab->elf.sgot
9998 || s == htab->elf.splt
9999 || s == htab->elf.iplt
c456f082
AM
10000 || s == htab->glink
10001 || s == htab->dynbss)
65f38f15
AM
10002 {
10003 /* Strip this section if we don't need it; see the
10004 comment below. */
5bd4f169 10005 }
58d180e8
AM
10006 else if (s == htab->glink_eh_frame)
10007 {
10008 if (!bfd_is_abs_section (s->output_section))
10009 /* Not sized yet. */
10010 continue;
10011 }
70cc837d 10012 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10013 {
c456f082 10014 if (s->size != 0)
5bd4f169 10015 {
33e44f2e 10016 if (s != htab->elf.srelplt)
b34976b6 10017 relocs = TRUE;
5bd4f169
AM
10018
10019 /* We use the reloc_count field as a counter if we need
10020 to copy relocs into the output file. */
10021 s->reloc_count = 0;
10022 }
10023 }
65f38f15 10024 else
5bd4f169
AM
10025 {
10026 /* It's not one of our sections, so don't allocate space. */
10027 continue;
10028 }
10029
eea6121a 10030 if (s->size == 0)
5bd4f169 10031 {
c456f082
AM
10032 /* If we don't need this section, strip it from the
10033 output file. This is mostly to handle .rela.bss and
10034 .rela.plt. We must create both sections in
10035 create_dynamic_sections, because they must be created
10036 before the linker maps input sections to output
10037 sections. The linker does that before
10038 adjust_dynamic_symbol is called, and it is that
10039 function which decides whether anything needs to go
10040 into these sections. */
8423293d 10041 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10042 continue;
10043 }
10044
c456f082 10045 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10046 continue;
10047
65f38f15
AM
10048 /* Allocate memory for the section contents. We use bfd_zalloc
10049 here in case unused entries are not reclaimed before the
10050 section's contents are written out. This should not happen,
411e1bfb
AM
10051 but this way if it does we get a R_PPC64_NONE reloc in .rela
10052 sections instead of garbage.
10053 We also rely on the section contents being zero when writing
10054 the GOT. */
eea6121a 10055 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10056 if (s->contents == NULL)
b34976b6 10057 return FALSE;
5bd4f169
AM
10058 }
10059
c72f2fb2 10060 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10061 {
0c8d6e5c 10062 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10063 continue;
10064
e717da7e 10065 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10066 if (s != NULL && s != htab->elf.sgot)
e717da7e 10067 {
eea6121a 10068 if (s->size == 0)
8423293d 10069 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10070 else
10071 {
eea6121a 10072 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10073 if (s->contents == NULL)
10074 return FALSE;
10075 }
10076 }
10077 s = ppc64_elf_tdata (ibfd)->relgot;
10078 if (s != NULL)
10079 {
eea6121a 10080 if (s->size == 0)
8423293d 10081 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10082 else
10083 {
eea6121a 10084 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10085 if (s->contents == NULL)
10086 return FALSE;
10087 relocs = TRUE;
10088 s->reloc_count = 0;
10089 }
10090 }
10091 }
10092
e86ce104 10093 if (htab->elf.dynamic_sections_created)
5bd4f169 10094 {
e8910a83
AM
10095 bfd_boolean tls_opt;
10096
5bd4f169
AM
10097 /* Add some entries to the .dynamic section. We fill in the
10098 values later, in ppc64_elf_finish_dynamic_sections, but we
10099 must add the entries now so that we get the correct size for
10100 the .dynamic section. The DT_DEBUG entry is filled in by the
10101 dynamic linker and used by the debugger. */
dc810e39 10102#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10103 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10104
0e1862bb 10105 if (bfd_link_executable (info))
5bd4f169 10106 {
dc810e39 10107 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10108 return FALSE;
5bd4f169
AM
10109 }
10110
33e44f2e 10111 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10112 {
dc810e39
AM
10113 if (!add_dynamic_entry (DT_PLTGOT, 0)
10114 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10115 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10116 || !add_dynamic_entry (DT_JMPREL, 0)
10117 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10118 return FALSE;
5bd4f169
AM
10119 }
10120
ee67d69a 10121 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10122 {
10123 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10124 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10125 return FALSE;
19397422
AM
10126 }
10127
7c9cf415 10128 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10129 && htab->tls_get_addr_fd != NULL
10130 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10131 if (tls_opt || !htab->opd_abi)
10132 {
10133 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10134 return FALSE;
10135 }
a7f2871e 10136
5bd4f169
AM
10137 if (relocs)
10138 {
dc810e39
AM
10139 if (!add_dynamic_entry (DT_RELA, 0)
10140 || !add_dynamic_entry (DT_RELASZ, 0)
10141 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10142 return FALSE;
5bd4f169 10143
65f38f15
AM
10144 /* If any dynamic relocs apply to a read-only section,
10145 then we need a DT_TEXTREL entry. */
248866a8 10146 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10147 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10148
65f38f15 10149 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10150 {
65f38f15 10151 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10152 return FALSE;
5bd4f169 10153 }
5bd4f169 10154 }
5bd4f169 10155 }
65f38f15 10156#undef add_dynamic_entry
5bd4f169 10157
b34976b6 10158 return TRUE;
5bd4f169
AM
10159}
10160
a345bc8d
AM
10161/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10162
10163static bfd_boolean
10164ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10165{
10166 if (h->plt.plist != NULL
10167 && !h->def_regular
10168 && !h->pointer_equality_needed)
10169 return FALSE;
10170
10171 return _bfd_elf_hash_symbol (h);
10172}
10173
721956f4 10174/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10175
4ce794b7
AM
10176static inline enum ppc_stub_type
10177ppc_type_of_stub (asection *input_sec,
10178 const Elf_Internal_Rela *rel,
10179 struct ppc_link_hash_entry **hash,
e054468f 10180 struct plt_entry **plt_ent,
6911b7dc
AM
10181 bfd_vma destination,
10182 unsigned long local_off)
5bd4f169 10183{
721956f4
AM
10184 struct ppc_link_hash_entry *h = *hash;
10185 bfd_vma location;
10186 bfd_vma branch_offset;
10187 bfd_vma max_branch_offset;
4ce794b7 10188 enum elf_ppc64_reloc_type r_type;
5bd4f169 10189
721956f4
AM
10190 if (h != NULL)
10191 {
e054468f 10192 struct plt_entry *ent;
7fe2b9a6 10193 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10194 if (h->oh != NULL
10195 && h->oh->is_func_descriptor)
7b8f6675
AM
10196 {
10197 fdh = ppc_follow_link (h->oh);
10198 *hash = fdh;
10199 }
8387904d 10200
e054468f
AM
10201 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10202 if (ent->addend == rel->r_addend
10203 && ent->plt.offset != (bfd_vma) -1)
10204 {
e054468f
AM
10205 *plt_ent = ent;
10206 return ppc_stub_plt_call;
10207 }
5bd4f169 10208
7fe2b9a6
AM
10209 /* Here, we know we don't have a plt entry. If we don't have a
10210 either a defined function descriptor or a defined entry symbol
10211 in a regular object file, then it is pointless trying to make
10212 any other type of stub. */
854b41e7
AM
10213 if (!is_static_defined (&fdh->elf)
10214 && !is_static_defined (&h->elf))
721956f4 10215 return ppc_stub_none;
5d1634d7 10216 }
e054468f
AM
10217 else if (elf_local_got_ents (input_sec->owner) != NULL)
10218 {
10219 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10220 struct plt_entry **local_plt = (struct plt_entry **)
10221 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10222 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10223
10224 if (local_plt[r_symndx] != NULL)
10225 {
10226 struct plt_entry *ent;
10227
10228 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10229 if (ent->addend == rel->r_addend
10230 && ent->plt.offset != (bfd_vma) -1)
10231 {
10232 *plt_ent = ent;
10233 return ppc_stub_plt_call;
10234 }
10235 }
10236 }
5d1634d7 10237
721956f4
AM
10238 /* Determine where the call point is. */
10239 location = (input_sec->output_offset
10240 + input_sec->output_section->vma
10241 + rel->r_offset);
5d1634d7 10242
721956f4
AM
10243 branch_offset = destination - location;
10244 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10245
721956f4
AM
10246 /* Determine if a long branch stub is needed. */
10247 max_branch_offset = 1 << 25;
4ce794b7 10248 if (r_type != R_PPC64_REL24)
721956f4 10249 max_branch_offset = 1 << 15;
5d1634d7 10250
6911b7dc 10251 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10252 /* We need a stub. Figure out whether a long_branch or plt_branch
10253 is needed later. */
10254 return ppc_stub_long_branch;
5d1634d7 10255
721956f4 10256 return ppc_stub_none;
5d1634d7
AM
10257}
10258
794e51c0
AM
10259/* With power7 weakly ordered memory model, it is possible for ld.so
10260 to update a plt entry in one thread and have another thread see a
10261 stale zero toc entry. To avoid this we need some sort of acquire
10262 barrier in the call stub. One solution is to make the load of the
10263 toc word seem to appear to depend on the load of the function entry
10264 word. Another solution is to test for r2 being zero, and branch to
10265 the appropriate glink entry if so.
10266
10267 . fake dep barrier compare
71a39c98
AM
10268 . ld 12,xxx(2) ld 12,xxx(2)
10269 . mtctr 12 mtctr 12
10270 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10271 . add 2,2,11 cmpldi 2,0
10272 . ld 2,xxx+8(2) bnectr+
10273 . bctr b <glink_entry>
10274
10275 The solution involving the compare turns out to be faster, so
10276 that's what we use unless the branch won't reach. */
10277
10278#define ALWAYS_USE_FAKE_DEP 0
10279#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10280
5d1634d7
AM
10281#define PPC_LO(v) ((v) & 0xffff)
10282#define PPC_HI(v) (((v) >> 16) & 0xffff)
10283#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10284
794e51c0
AM
10285static inline unsigned int
10286plt_stub_size (struct ppc_link_hash_table *htab,
10287 struct ppc_stub_hash_entry *stub_entry,
10288 bfd_vma off)
10289{
b9e5796b
AM
10290 unsigned size = 12;
10291
10292 if (ALWAYS_EMIT_R2SAVE
10293 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10294 size += 4;
10295 if (PPC_HA (off) != 0)
794e51c0 10296 size += 4;
b9e5796b
AM
10297 if (htab->opd_abi)
10298 {
10299 size += 4;
e7d1c40c 10300 if (htab->params->plt_static_chain)
b9e5796b 10301 size += 4;
bd4d2eaa
AM
10302 if (htab->params->plt_thread_safe
10303 && htab->elf.dynamic_sections_created
10304 && stub_entry->h != NULL
10305 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10306 size += 8;
e7d1c40c 10307 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10308 size += 4;
10309 }
794e51c0
AM
10310 if (stub_entry->h != NULL
10311 && (stub_entry->h == htab->tls_get_addr_fd
10312 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10313 && htab->params->tls_get_addr_opt)
794e51c0
AM
10314 size += 13 * 4;
10315 return size;
10316}
10317
10318/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10319 then return the padding needed to do so. */
10320static inline unsigned int
10321plt_stub_pad (struct ppc_link_hash_table *htab,
10322 struct ppc_stub_hash_entry *stub_entry,
10323 bfd_vma plt_off)
10324{
e7d1c40c 10325 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0 10326 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10327 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0
AM
10328
10329 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10330 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10331 return stub_align - (stub_off & (stub_align - 1));
10332 return 0;
10333}
10334
10335/* Build a .plt call stub. */
10336
10337static inline bfd_byte *
10338build_plt_stub (struct ppc_link_hash_table *htab,
10339 struct ppc_stub_hash_entry *stub_entry,
10340 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10341{
e7d1c40c 10342 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10343 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10344 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10345 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10346 && htab->elf.dynamic_sections_created
10347 && stub_entry->h != NULL
10348 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10349 bfd_boolean use_fake_dep = plt_thread_safe;
10350 bfd_vma cmp_branch_off = 0;
10351
10352 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10353 && plt_load_toc
794e51c0 10354 && plt_thread_safe
bd4d2eaa
AM
10355 && !((stub_entry->h == htab->tls_get_addr_fd
10356 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10357 && htab->params->tls_get_addr_opt))
794e51c0
AM
10358 {
10359 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10360 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10361 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10362 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10363 bfd_vma to, from;
10364
68d62958
AM
10365 if (pltindex > 32768)
10366 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10367 to = (glinkoff
10368 + htab->glink->output_offset
10369 + htab->glink->output_section->vma);
6f20ed8a 10370 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10371 + 4 * (ALWAYS_EMIT_R2SAVE
10372 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10373 + 4 * (PPC_HA (offset) != 0)
10374 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10375 != PPC_HA (offset))
10376 + 4 * (plt_static_chain != 0)
10377 + 20
6f20ed8a
AM
10378 + stub_entry->group->stub_sec->output_offset
10379 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10380 cmp_branch_off = to - from;
10381 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10382 }
10383
ac2df442
AM
10384 if (PPC_HA (offset) != 0)
10385 {
176a0d42
AM
10386 if (r != NULL)
10387 {
794e51c0
AM
10388 if (ALWAYS_EMIT_R2SAVE
10389 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10390 r[0].r_offset += 4;
176a0d42 10391 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10392 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10393 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10394 r[1].r_addend = r[0].r_addend;
b9e5796b 10395 if (plt_load_toc)
176a0d42 10396 {
b9e5796b 10397 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10398 {
b9e5796b
AM
10399 r[2].r_offset = r[1].r_offset + 4;
10400 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10401 r[2].r_addend = r[0].r_addend;
10402 }
10403 else
10404 {
10405 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10406 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10407 r[2].r_addend = r[0].r_addend + 8;
10408 if (plt_static_chain)
10409 {
10410 r[3].r_offset = r[2].r_offset + 4;
10411 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10412 r[3].r_addend = r[0].r_addend + 16;
10413 }
c7131b65 10414 }
176a0d42
AM
10415 }
10416 }
794e51c0
AM
10417 if (ALWAYS_EMIT_R2SAVE
10418 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10419 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10420 if (plt_load_toc)
10421 {
10422 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10423 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10424 }
10425 else
10426 {
10427 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10428 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10429 }
b9e5796b
AM
10430 if (plt_load_toc
10431 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10432 {
71a39c98 10433 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10434 offset = 0;
10435 }
71a39c98 10436 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10437 if (plt_load_toc)
794e51c0 10438 {
b9e5796b
AM
10439 if (use_fake_dep)
10440 {
10441 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10442 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10443 }
10444 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10445 if (plt_static_chain)
10446 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10447 }
ac2df442
AM
10448 }
10449 else
10450 {
176a0d42
AM
10451 if (r != NULL)
10452 {
794e51c0
AM
10453 if (ALWAYS_EMIT_R2SAVE
10454 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10455 r[0].r_offset += 4;
176a0d42 10456 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10457 if (plt_load_toc)
176a0d42 10458 {
b9e5796b 10459 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10460 {
b9e5796b
AM
10461 r[1].r_offset = r[0].r_offset + 4;
10462 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10463 r[1].r_addend = r[0].r_addend;
10464 }
10465 else
10466 {
10467 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10468 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10469 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10470 if (plt_static_chain)
10471 {
10472 r[2].r_offset = r[1].r_offset + 4;
10473 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10474 r[2].r_addend = r[0].r_addend + 8;
10475 }
c7131b65 10476 }
176a0d42
AM
10477 }
10478 }
794e51c0
AM
10479 if (ALWAYS_EMIT_R2SAVE
10480 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10481 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10482 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10483 if (plt_load_toc
10484 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10485 {
10486 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10487 offset = 0;
10488 }
71a39c98 10489 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10490 if (plt_load_toc)
794e51c0 10491 {
b9e5796b
AM
10492 if (use_fake_dep)
10493 {
10494 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10495 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10496 }
10497 if (plt_static_chain)
10498 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10499 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10500 }
ac2df442 10501 }
b9e5796b 10502 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10503 {
10504 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10505 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10506 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10507 }
10508 else
10509 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10510 return p;
10511}
10512
a7f2871e
AM
10513/* Build a special .plt call stub for __tls_get_addr. */
10514
10515#define LD_R11_0R3 0xe9630000
10516#define LD_R12_0R3 0xe9830000
10517#define MR_R0_R3 0x7c601b78
10518#define CMPDI_R11_0 0x2c2b0000
10519#define ADD_R3_R12_R13 0x7c6c6a14
10520#define BEQLR 0x4d820020
10521#define MR_R3_R0 0x7c030378
a7f2871e
AM
10522#define STD_R11_0R1 0xf9610000
10523#define BCTRL 0x4e800421
10524#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10525#define MTLR_R11 0x7d6803a6
10526
10527static inline bfd_byte *
794e51c0
AM
10528build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10529 struct ppc_stub_hash_entry *stub_entry,
10530 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10531{
e7d1c40c 10532 bfd *obfd = htab->params->stub_bfd;
794e51c0 10533
a7f2871e
AM
10534 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10535 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10536 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10537 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10538 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10539 bfd_put_32 (obfd, BEQLR, p), p += 4;
10540 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10541 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10542 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10543
10544 if (r != NULL)
10545 r[0].r_offset += 9 * 4;
794e51c0 10546 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10547 bfd_put_32 (obfd, BCTRL, p - 4);
10548
a078d95a 10549 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10550 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10551 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10552 bfd_put_32 (obfd, BLR, p), p += 4;
10553
10554 return p;
10555}
10556
176a0d42
AM
10557static Elf_Internal_Rela *
10558get_relocs (asection *sec, int count)
10559{
10560 Elf_Internal_Rela *relocs;
10561 struct bfd_elf_section_data *elfsec_data;
10562
10563 elfsec_data = elf_section_data (sec);
10564 relocs = elfsec_data->relocs;
10565 if (relocs == NULL)
10566 {
10567 bfd_size_type relsize;
10568 relsize = sec->reloc_count * sizeof (*relocs);
10569 relocs = bfd_alloc (sec->owner, relsize);
10570 if (relocs == NULL)
10571 return NULL;
10572 elfsec_data->relocs = relocs;
d4730f92
BS
10573 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10574 sizeof (Elf_Internal_Shdr));
10575 if (elfsec_data->rela.hdr == NULL)
10576 return NULL;
10577 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10578 * sizeof (Elf64_External_Rela));
10579 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10580 sec->reloc_count = 0;
10581 }
10582 relocs += sec->reloc_count;
10583 sec->reloc_count += count;
10584 return relocs;
10585}
10586
aa374f67 10587static bfd_vma
25f53a85 10588get_r2off (struct bfd_link_info *info,
aa374f67
AM
10589 struct ppc_stub_hash_entry *stub_entry)
10590{
25f53a85 10591 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10592 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10593
10594 if (r2off == 0)
10595 {
10596 /* Support linking -R objects. Get the toc pointer from the
10597 opd entry. */
10598 char buf[8];
b9e5796b
AM
10599 if (!htab->opd_abi)
10600 return r2off;
aa374f67
AM
10601 asection *opd = stub_entry->h->elf.root.u.def.section;
10602 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10603
10604 if (strcmp (opd->name, ".opd") != 0
10605 || opd->reloc_count != 0)
10606 {
bc30df16 10607 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10608 stub_entry->h->elf.root.root.string);
aa374f67 10609 bfd_set_error (bfd_error_bad_value);
a7c49797 10610 return (bfd_vma) -1;
aa374f67
AM
10611 }
10612 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10613 return (bfd_vma) -1;
aa374f67 10614 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10615 r2off -= elf_gp (info->output_bfd);
aa374f67 10616 }
6f20ed8a 10617 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10618 return r2off;
10619}
10620
b34976b6 10621static bfd_boolean
4ce794b7 10622ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10623{
721956f4
AM
10624 struct ppc_stub_hash_entry *stub_entry;
10625 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10626 struct bfd_link_info *info;
10627 struct ppc_link_hash_table *htab;
721956f4
AM
10628 bfd_byte *loc;
10629 bfd_byte *p;
ee75fd95 10630 bfd_vma dest, off;
721956f4 10631 int size;
176a0d42 10632 Elf_Internal_Rela *r;
e054468f 10633 asection *plt;
5d1634d7 10634
721956f4
AM
10635 /* Massage our args to the form they really have. */
10636 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10637 info = in_arg;
5d1634d7 10638
5d1634d7 10639 htab = ppc_hash_table (info);
4dfe6ac6
NC
10640 if (htab == NULL)
10641 return FALSE;
5d1634d7 10642
721956f4 10643 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10644 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10645 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10646
4ce794b7 10647 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10648 switch (stub_entry->stub_type)
5d1634d7 10649 {
721956f4 10650 case ppc_stub_long_branch:
ad8e1ba5 10651 case ppc_stub_long_branch_r2off:
721956f4 10652 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10653 dest = (stub_entry->target_value
10654 + stub_entry->target_section->output_offset
10655 + stub_entry->target_section->output_section->vma);
10656 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10657 off = dest;
5d1634d7 10658
721956f4
AM
10659 /* And this is where we are coming from. */
10660 off -= (stub_entry->stub_offset
6f20ed8a
AM
10661 + stub_entry->group->stub_sec->output_offset
10662 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10663
ac2df442
AM
10664 size = 4;
10665 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10666 {
25f53a85 10667 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10668
a7c49797 10669 if (r2off == (bfd_vma) -1)
aa374f67
AM
10670 {
10671 htab->stub_error = TRUE;
10672 return FALSE;
10673 }
e7d1c40c 10674 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10675 loc += 4;
a7c49797 10676 size = 8;
ac2df442
AM
10677 if (PPC_HA (r2off) != 0)
10678 {
e7d1c40c
AM
10679 bfd_put_32 (htab->params->stub_bfd,
10680 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442 10681 loc += 4;
a7c49797
AM
10682 size += 4;
10683 }
10684 if (PPC_LO (r2off) != 0)
10685 {
10686 bfd_put_32 (htab->params->stub_bfd,
10687 ADDI_R2_R2 | PPC_LO (r2off), loc);
10688 loc += 4;
10689 size += 4;
ac2df442 10690 }
ac2df442 10691 off -= size - 4;
ad8e1ba5 10692 }
e7d1c40c 10693 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10694
5c3dead3
AM
10695 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10696 {
bc30df16
AM
10697 info->callbacks->einfo
10698 (_("%P: long branch stub `%s' offset overflow\n"),
10699 stub_entry->root.string);
5c3dead3
AM
10700 htab->stub_error = TRUE;
10701 return FALSE;
10702 }
ee75fd95
AM
10703
10704 if (info->emitrelocations)
10705 {
6f20ed8a 10706 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10707 if (r == NULL)
10708 return FALSE;
6f20ed8a 10709 r->r_offset = loc - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10710 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10711 r->r_addend = dest;
10712 if (stub_entry->h != NULL)
10713 {
10714 struct elf_link_hash_entry **hashes;
10715 unsigned long symndx;
10716 struct ppc_link_hash_entry *h;
10717
e7d1c40c 10718 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10719 if (hashes == NULL)
10720 {
10721 bfd_size_type hsize;
10722
10723 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10724 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10725 if (hashes == NULL)
10726 return FALSE;
e7d1c40c 10727 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10728 htab->stub_globals = 1;
10729 }
10730 symndx = htab->stub_globals++;
10731 h = stub_entry->h;
10732 hashes[symndx] = &h->elf;
10733 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10734 if (h->oh != NULL && h->oh->is_func)
b31867b6 10735 h = ppc_follow_link (h->oh);
ee75fd95
AM
10736 if (h->elf.root.u.def.section != stub_entry->target_section)
10737 /* H is an opd symbol. The addend must be zero. */
10738 r->r_addend = 0;
10739 else
10740 {
10741 off = (h->elf.root.u.def.value
10742 + h->elf.root.u.def.section->output_offset
10743 + h->elf.root.u.def.section->output_section->vma);
10744 r->r_addend -= off;
10745 }
10746 }
10747 }
721956f4 10748 break;
e86ce104 10749
721956f4 10750 case ppc_stub_plt_branch:
ad8e1ba5 10751 case ppc_stub_plt_branch_r2off:
721956f4
AM
10752 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10753 stub_entry->root.string + 9,
b34976b6 10754 FALSE, FALSE);
721956f4
AM
10755 if (br_entry == NULL)
10756 {
8de848d8 10757 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10758 stub_entry->root.string);
b34976b6
AM
10759 htab->stub_error = TRUE;
10760 return FALSE;
721956f4
AM
10761 }
10762
176a0d42
AM
10763 dest = (stub_entry->target_value
10764 + stub_entry->target_section->output_offset
10765 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10766 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10767 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10768
176a0d42 10769 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10770 htab->brlt->contents + br_entry->offset);
721956f4 10771
f94498ff 10772 if (br_entry->iter == htab->stub_iteration)
721956f4 10773 {
f94498ff 10774 br_entry->iter = 0;
84f5d08e 10775
f94498ff 10776 if (htab->relbrlt != NULL)
84f5d08e 10777 {
f94498ff
AM
10778 /* Create a reloc for the branch lookup table entry. */
10779 Elf_Internal_Rela rela;
10780 bfd_byte *rl;
10781
10782 rela.r_offset = (br_entry->offset
10783 + htab->brlt->output_offset
10784 + htab->brlt->output_section->vma);
10785 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10786 rela.r_addend = dest;
f94498ff
AM
10787
10788 rl = htab->relbrlt->contents;
10789 rl += (htab->relbrlt->reloc_count++
10790 * sizeof (Elf64_External_Rela));
10791 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10792 }
10793 else if (info->emitrelocations)
10794 {
176a0d42
AM
10795 r = get_relocs (htab->brlt, 1);
10796 if (r == NULL)
10797 return FALSE;
10798 /* brlt, being SEC_LINKER_CREATED does not go through the
10799 normal reloc processing. Symbols and offsets are not
10800 translated from input file to output file form, so
10801 set up the offset per the output file. */
f94498ff
AM
10802 r->r_offset = (br_entry->offset
10803 + htab->brlt->output_offset
10804 + htab->brlt->output_section->vma);
10805 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10806 r->r_addend = dest;
84f5d08e 10807 }
84f5d08e 10808 }
721956f4 10809
176a0d42
AM
10810 dest = (br_entry->offset
10811 + htab->brlt->output_offset
10812 + htab->brlt->output_section->vma);
10813
10814 off = (dest
4ce794b7 10815 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 10816 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 10817
ad8e1ba5 10818 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10819 {
25f53a85 10820 info->callbacks->einfo
bc30df16 10821 (_("%P: linkage table error against `%T'\n"),
721956f4 10822 stub_entry->root.string);
5d1634d7 10823 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10824 htab->stub_error = TRUE;
10825 return FALSE;
5d1634d7 10826 }
41bd81ab 10827
176a0d42
AM
10828 if (info->emitrelocations)
10829 {
6f20ed8a 10830 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
10831 if (r == NULL)
10832 return FALSE;
6f20ed8a 10833 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10834 if (bfd_big_endian (info->output_bfd))
10835 r[0].r_offset += 2;
00f412ee 10836 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
10837 r[0].r_offset += 4;
10838 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10839 r[0].r_addend = dest;
10840 if (PPC_HA (off) != 0)
10841 {
10842 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10843 r[1].r_offset = r[0].r_offset + 4;
10844 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10845 r[1].r_addend = r[0].r_addend;
10846 }
10847 }
10848
00f412ee 10849 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 10850 {
176a0d42 10851 if (PPC_HA (off) != 0)
ac2df442
AM
10852 {
10853 size = 16;
e7d1c40c 10854 bfd_put_32 (htab->params->stub_bfd,
397998fc 10855 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10856 loc += 4;
e7d1c40c 10857 bfd_put_32 (htab->params->stub_bfd,
397998fc 10858 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
10859 }
10860 else
10861 {
10862 size = 12;
e7d1c40c
AM
10863 bfd_put_32 (htab->params->stub_bfd,
10864 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 10865 }
ad8e1ba5
AM
10866 }
10867 else
10868 {
25f53a85 10869 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 10870
a7c49797 10871 if (r2off == (bfd_vma) -1)
aa374f67
AM
10872 {
10873 htab->stub_error = TRUE;
10874 return FALSE;
10875 }
ad8e1ba5 10876
e7d1c40c 10877 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10878 loc += 4;
00f412ee 10879 size = 16;
176a0d42 10880 if (PPC_HA (off) != 0)
ac2df442
AM
10881 {
10882 size += 4;
e7d1c40c 10883 bfd_put_32 (htab->params->stub_bfd,
397998fc 10884 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10885 loc += 4;
e7d1c40c 10886 bfd_put_32 (htab->params->stub_bfd,
397998fc 10887 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
10888 }
10889 else
e7d1c40c 10890 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
10891
10892 if (PPC_HA (r2off) != 0)
10893 {
10894 size += 4;
00f412ee 10895 loc += 4;
e7d1c40c
AM
10896 bfd_put_32 (htab->params->stub_bfd,
10897 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
10898 }
10899 if (PPC_LO (r2off) != 0)
10900 {
10901 size += 4;
ac2df442 10902 loc += 4;
e7d1c40c
AM
10903 bfd_put_32 (htab->params->stub_bfd,
10904 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 10905 }
ad8e1ba5
AM
10906 }
10907 loc += 4;
e7d1c40c 10908 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 10909 loc += 4;
e7d1c40c 10910 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
721956f4 10911 break;
5d1634d7 10912
721956f4 10913 case ppc_stub_plt_call:
794e51c0 10914 case ppc_stub_plt_call_r2save:
e054468f 10915 if (stub_entry->h != NULL
b31867b6
AM
10916 && stub_entry->h->is_func_descriptor
10917 && stub_entry->h->oh != NULL)
c862ae31 10918 {
b31867b6
AM
10919 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10920
10921 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 10922 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
b31867b6
AM
10923 if (fh->elf.root.type == bfd_link_hash_undefined)
10924 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
10925 /* Stop undo_symbol_twiddle changing it back to undefined. */
10926 fh->was_undefined = 0;
c862ae31
AM
10927 }
10928
721956f4 10929 /* Now build the stub. */
e054468f 10930 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 10931 if (dest >= (bfd_vma) -2)
721956f4
AM
10932 abort ();
10933
33e44f2e 10934 plt = htab->elf.splt;
25f23106
AM
10935 if (!htab->elf.dynamic_sections_created
10936 || stub_entry->h == NULL
10937 || stub_entry->h->elf.dynindx == -1)
33e44f2e 10938 plt = htab->elf.iplt;
e054468f
AM
10939
10940 dest += plt->output_offset + plt->output_section->vma;
10941
10942 if (stub_entry->h == NULL
10943 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10944 {
10945 Elf_Internal_Rela rela;
10946 bfd_byte *rl;
10947
10948 rela.r_offset = dest;
ee67d69a
AM
10949 if (htab->opd_abi)
10950 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10951 else
10952 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
10953 rela.r_addend = (stub_entry->target_value
10954 + stub_entry->target_section->output_offset
10955 + stub_entry->target_section->output_section->vma);
10956
33e44f2e
AM
10957 rl = (htab->elf.irelplt->contents
10958 + (htab->elf.irelplt->reloc_count++
25f23106
AM
10959 * sizeof (Elf64_External_Rela)));
10960 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
10961 stub_entry->plt_ent->plt.offset |= 1;
10962 }
176a0d42
AM
10963
10964 off = (dest
e054468f 10965 - elf_gp (plt->output_section->owner)
6f20ed8a 10966 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 10967
ad8e1ba5 10968 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10969 {
25f53a85 10970 info->callbacks->einfo
bc30df16 10971 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
10972 stub_entry->h != NULL
10973 ? stub_entry->h->elf.root.root.string
10974 : "<local sym>");
721956f4 10975 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10976 htab->stub_error = TRUE;
10977 return FALSE;
721956f4
AM
10978 }
10979
e7d1c40c 10980 if (htab->params->plt_stub_align != 0)
794e51c0
AM
10981 {
10982 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10983
6f20ed8a
AM
10984 stub_entry->group->stub_sec->size += pad;
10985 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
10986 loc += pad;
10987 }
10988
176a0d42
AM
10989 r = NULL;
10990 if (info->emitrelocations)
10991 {
6f20ed8a 10992 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
10993 ((PPC_HA (off) != 0)
10994 + (htab->opd_abi
e7d1c40c 10995 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
10996 && PPC_HA (off + 16) == PPC_HA (off))
10997 : 1)));
176a0d42
AM
10998 if (r == NULL)
10999 return FALSE;
6f20ed8a 11000 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11001 if (bfd_big_endian (info->output_bfd))
11002 r[0].r_offset += 2;
176a0d42
AM
11003 r[0].r_addend = dest;
11004 }
a7f2871e
AM
11005 if (stub_entry->h != NULL
11006 && (stub_entry->h == htab->tls_get_addr_fd
11007 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11008 && htab->params->tls_get_addr_opt)
794e51c0 11009 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11010 else
794e51c0 11011 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11012 size = p - loc;
11013 break;
11014
a4b6fadd
AM
11015 case ppc_stub_save_res:
11016 return TRUE;
11017
721956f4
AM
11018 default:
11019 BFD_FAIL ();
b34976b6 11020 return FALSE;
721956f4
AM
11021 }
11022
6f20ed8a 11023 stub_entry->group->stub_sec->size += size;
97b639ba 11024
e7d1c40c 11025 if (htab->params->emit_stub_syms)
97b639ba
AM
11026 {
11027 struct elf_link_hash_entry *h;
ee75fd95
AM
11028 size_t len1, len2;
11029 char *name;
11030 const char *const stub_str[] = { "long_branch",
11031 "long_branch_r2off",
11032 "plt_branch",
11033 "plt_branch_r2off",
794e51c0 11034 "plt_call",
ee75fd95
AM
11035 "plt_call" };
11036
11037 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11038 len2 = strlen (stub_entry->root.string);
11039 name = bfd_malloc (len1 + len2 + 2);
11040 if (name == NULL)
11041 return FALSE;
11042 memcpy (name, stub_entry->root.string, 9);
11043 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11044 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11045 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11046 if (h == NULL)
11047 return FALSE;
11048 if (h->root.type == bfd_link_hash_new)
11049 {
11050 h->root.type = bfd_link_hash_defined;
6f20ed8a 11051 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11052 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11053 h->ref_regular = 1;
11054 h->def_regular = 1;
11055 h->ref_regular_nonweak = 1;
11056 h->forced_local = 1;
11057 h->non_elf = 0;
2ec55de3 11058 h->root.linker_def = 1;
97b639ba
AM
11059 }
11060 }
11061
b34976b6 11062 return TRUE;
721956f4
AM
11063}
11064
11065/* As above, but don't actually build the stub. Just bump offset so
11066 we know stub section sizes, and select plt_branch stubs where
11067 long_branch stubs won't do. */
11068
b34976b6 11069static bfd_boolean
4ce794b7 11070ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11071{
11072 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11073 struct bfd_link_info *info;
721956f4
AM
11074 struct ppc_link_hash_table *htab;
11075 bfd_vma off;
11076 int size;
11077
11078 /* Massage our args to the form they really have. */
11079 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11080 info = in_arg;
11081
11082 htab = ppc_hash_table (info);
4dfe6ac6
NC
11083 if (htab == NULL)
11084 return FALSE;
721956f4 11085
a4b6fadd
AM
11086 if (stub_entry->h != NULL
11087 && stub_entry->h->save_res
11088 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11089 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11090 {
11091 /* Don't make stubs to out-of-line register save/restore
11092 functions. Instead, emit copies of the functions. */
11093 stub_entry->group->needs_save_res = 1;
11094 stub_entry->stub_type = ppc_stub_save_res;
11095 return TRUE;
11096 }
11097
794e51c0
AM
11098 if (stub_entry->stub_type == ppc_stub_plt_call
11099 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11100 {
e054468f
AM
11101 asection *plt;
11102 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11103 if (off >= (bfd_vma) -2)
411e1bfb 11104 abort ();
33e44f2e 11105 plt = htab->elf.splt;
25f23106
AM
11106 if (!htab->elf.dynamic_sections_created
11107 || stub_entry->h == NULL
11108 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11109 plt = htab->elf.iplt;
e054468f
AM
11110 off += (plt->output_offset
11111 + plt->output_section->vma
11112 - elf_gp (plt->output_section->owner)
6f20ed8a 11113 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11114
794e51c0 11115 size = plt_stub_size (htab, stub_entry, off);
e7d1c40c 11116 if (htab->params->plt_stub_align)
794e51c0 11117 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11118 if (info->emitrelocations)
11119 {
6f20ed8a 11120 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11121 += ((PPC_HA (off) != 0)
11122 + (htab->opd_abi
e7d1c40c 11123 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11124 && PPC_HA (off + 16) == PPC_HA (off))
11125 : 1));
6f20ed8a 11126 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11127 }
721956f4
AM
11128 }
11129 else
11130 {
ad8e1ba5
AM
11131 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11132 variants. */
ac2df442 11133 bfd_vma r2off = 0;
6911b7dc 11134 bfd_vma local_off = 0;
ac2df442 11135
721956f4
AM
11136 off = (stub_entry->target_value
11137 + stub_entry->target_section->output_offset
11138 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11139 off -= (stub_entry->group->stub_sec->size
11140 + stub_entry->group->stub_sec->output_offset
11141 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11142
ad8e1ba5
AM
11143 /* Reset the stub type from the plt variant in case we now
11144 can reach with a shorter stub. */
11145 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11146 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11147
11148 size = 4;
11149 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11150 {
25f53a85 11151 r2off = get_r2off (info, stub_entry);
a7c49797 11152 if (r2off == (bfd_vma) -1)
aa374f67
AM
11153 {
11154 htab->stub_error = TRUE;
11155 return FALSE;
11156 }
a7c49797 11157 size = 8;
ac2df442 11158 if (PPC_HA (r2off) != 0)
a7c49797
AM
11159 size += 4;
11160 if (PPC_LO (r2off) != 0)
11161 size += 4;
ac2df442 11162 off -= size - 4;
ad8e1ba5
AM
11163 }
11164
6911b7dc
AM
11165 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11166
b9e5796b
AM
11167 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11168 Do the same for -R objects without function descriptors. */
11169 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11170 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11171 && r2off == 0
11172 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11173 {
11174 struct ppc_branch_hash_entry *br_entry;
11175
11176 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11177 stub_entry->root.string + 9,
b34976b6 11178 TRUE, FALSE);
721956f4
AM
11179 if (br_entry == NULL)
11180 {
8de848d8 11181 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11182 stub_entry->root.string);
b34976b6
AM
11183 htab->stub_error = TRUE;
11184 return FALSE;
721956f4
AM
11185 }
11186
11187 if (br_entry->iter != htab->stub_iteration)
11188 {
11189 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11190 br_entry->offset = htab->brlt->size;
11191 htab->brlt->size += 8;
63bc6f6c 11192
ee75fd95 11193 if (htab->relbrlt != NULL)
eea6121a 11194 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11195 else if (info->emitrelocations)
11196 {
11197 htab->brlt->reloc_count += 1;
11198 htab->brlt->flags |= SEC_RELOC;
11199 }
721956f4 11200 }
ad8e1ba5
AM
11201
11202 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11203 off = (br_entry->offset
11204 + htab->brlt->output_offset
11205 + htab->brlt->output_section->vma
11206 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11207 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11208
176a0d42
AM
11209 if (info->emitrelocations)
11210 {
6f20ed8a
AM
11211 stub_entry->group->stub_sec->reloc_count
11212 += 1 + (PPC_HA (off) != 0);
11213 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11214 }
11215
00f412ee 11216 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11217 {
11218 size = 12;
176a0d42 11219 if (PPC_HA (off) != 0)
ac2df442
AM
11220 size = 16;
11221 }
11222 else
11223 {
00f412ee 11224 size = 16;
176a0d42 11225 if (PPC_HA (off) != 0)
ac2df442
AM
11226 size += 4;
11227
11228 if (PPC_HA (r2off) != 0)
11229 size += 4;
00f412ee
AM
11230 if (PPC_LO (r2off) != 0)
11231 size += 4;
ac2df442 11232 }
721956f4 11233 }
84f5d08e
AM
11234 else if (info->emitrelocations)
11235 {
6f20ed8a
AM
11236 stub_entry->group->stub_sec->reloc_count += 1;
11237 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11238 }
721956f4
AM
11239 }
11240
6f20ed8a 11241 stub_entry->group->stub_sec->size += size;
b34976b6 11242 return TRUE;
721956f4
AM
11243}
11244
11245/* Set up various things so that we can make a list of input sections
11246 for each output section included in the link. Returns -1 on error,
cedb70c5 11247 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11248
11249int
e7d1c40c 11250ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11251{
6f20ed8a 11252 unsigned int id;
721956f4
AM
11253 bfd_size_type amt;
11254 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11255
4dfe6ac6
NC
11256 if (htab == NULL)
11257 return -1;
4c52953f 11258
6f20ed8a
AM
11259 htab->sec_info_arr_size = bfd_get_next_section_id ();
11260 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11261 htab->sec_info = bfd_zmalloc (amt);
11262 if (htab->sec_info == NULL)
721956f4
AM
11263 return -1;
11264
3d6f9012
AM
11265 /* Set toc_off for com, und, abs and ind sections. */
11266 for (id = 0; id < 3; id++)
6f20ed8a 11267 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11268
721956f4
AM
11269 return 1;
11270}
11271
927be08e
AM
11272/* Set up for first pass at multitoc partitioning. */
11273
11274void
11275ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11276{
11277 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11278
1c865ab2 11279 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11280 htab->toc_bfd = NULL;
11281 htab->toc_first_sec = NULL;
11282}
11283
e717da7e
AM
11284/* The linker repeatedly calls this function for each TOC input section
11285 and linker generated GOT section. Group input bfds such that the toc
927be08e 11286 within a group is less than 64k in size. */
ad8e1ba5 11287
927be08e 11288bfd_boolean
4ce794b7 11289ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11290{
11291 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11292 bfd_vma addr, off, limit;
ad8e1ba5 11293
4dfe6ac6
NC
11294 if (htab == NULL)
11295 return FALSE;
11296
927be08e 11297 if (!htab->second_toc_pass)
4c52953f 11298 {
927be08e 11299 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11300 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11301
11302 if (new_bfd)
bf102f86
AM
11303 {
11304 htab->toc_bfd = isec->owner;
11305 htab->toc_first_sec = isec;
11306 }
927be08e 11307
bf102f86
AM
11308 addr = isec->output_offset + isec->output_section->vma;
11309 off = addr - htab->toc_curr;
d77c8a4b
AM
11310 limit = 0x80008000;
11311 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11312 limit = 0x10000;
11313 if (off + isec->size > limit)
bf102f86
AM
11314 {
11315 addr = (htab->toc_first_sec->output_offset
11316 + htab->toc_first_sec->output_section->vma);
11317 htab->toc_curr = addr;
a27e685f 11318 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11319 }
99877b66 11320
927be08e
AM
11321 /* toc_curr is the base address of this toc group. Set elf_gp
11322 for the input section to be the offset relative to the
11323 output toc base plus 0x8000. Making the input elf_gp an
11324 offset allows us to move the toc as a whole without
11325 recalculating input elf_gp. */
11326 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11327 off += TOC_BASE_OFF;
11328
11329 /* Die if someone uses a linker script that doesn't keep input
11330 file .toc and .got together. */
a4fd3de5
AM
11331 if (new_bfd
11332 && elf_gp (isec->owner) != 0
927be08e
AM
11333 && elf_gp (isec->owner) != off)
11334 return FALSE;
11335
11336 elf_gp (isec->owner) = off;
11337 return TRUE;
4c52953f 11338 }
927be08e
AM
11339
11340 /* During the second pass toc_first_sec points to the start of
11341 a toc group, and toc_curr is used to track the old elf_gp.
11342 We use toc_bfd to ensure we only look at each bfd once. */
11343 if (htab->toc_bfd == isec->owner)
11344 return TRUE;
11345 htab->toc_bfd = isec->owner;
11346
11347 if (htab->toc_first_sec == NULL
11348 || htab->toc_curr != elf_gp (isec->owner))
11349 {
11350 htab->toc_curr = elf_gp (isec->owner);
11351 htab->toc_first_sec = isec;
11352 }
11353 addr = (htab->toc_first_sec->output_offset
11354 + htab->toc_first_sec->output_section->vma);
11355 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11356 elf_gp (isec->owner) = off;
11357
11358 return TRUE;
ad8e1ba5
AM
11359}
11360
927be08e
AM
11361/* Called via elf_link_hash_traverse to merge GOT entries for global
11362 symbol H. */
11363
11364static bfd_boolean
11365merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11366{
11367 if (h->root.type == bfd_link_hash_indirect)
11368 return TRUE;
11369
927be08e
AM
11370 merge_got_entries (&h->got.glist);
11371
11372 return TRUE;
11373}
11374
11375/* Called via elf_link_hash_traverse to allocate GOT entries for global
11376 symbol H. */
11377
11378static bfd_boolean
11379reallocate_got (struct elf_link_hash_entry *h, void *inf)
11380{
11381 struct got_entry *gent;
11382
11383 if (h->root.type == bfd_link_hash_indirect)
11384 return TRUE;
11385
927be08e
AM
11386 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11387 if (!gent->is_indirect)
11388 allocate_got (h, (struct bfd_link_info *) inf, gent);
11389 return TRUE;
11390}
11391
11392/* Called on the first multitoc pass after the last call to
11393 ppc64_elf_next_toc_section. This function removes duplicate GOT
11394 entries. */
11395
11396bfd_boolean
11397ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11398{
11399 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11400 struct bfd *ibfd, *ibfd2;
11401 bfd_boolean done_something;
11402
11403 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11404
7865406b
AM
11405 if (!htab->do_multi_toc)
11406 return FALSE;
11407
d0fae19d 11408 /* Merge global sym got entries within a toc group. */
927be08e
AM
11409 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11410
11411 /* And tlsld_got. */
c72f2fb2 11412 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11413 {
11414 struct got_entry *ent, *ent2;
11415
11416 if (!is_ppc64_elf (ibfd))
11417 continue;
11418
11419 ent = ppc64_tlsld_got (ibfd);
11420 if (!ent->is_indirect
11421 && ent->got.offset != (bfd_vma) -1)
11422 {
c72f2fb2 11423 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11424 {
11425 if (!is_ppc64_elf (ibfd2))
11426 continue;
11427
11428 ent2 = ppc64_tlsld_got (ibfd2);
11429 if (!ent2->is_indirect
11430 && ent2->got.offset != (bfd_vma) -1
11431 && elf_gp (ibfd2) == elf_gp (ibfd))
11432 {
11433 ent2->is_indirect = TRUE;
11434 ent2->got.ent = ent;
11435 }
11436 }
11437 }
11438 }
11439
11440 /* Zap sizes of got sections. */
33e44f2e
AM
11441 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11442 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11443 htab->got_reli_size = 0;
11444
c72f2fb2 11445 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11446 {
11447 asection *got, *relgot;
11448
11449 if (!is_ppc64_elf (ibfd))
11450 continue;
11451
11452 got = ppc64_elf_tdata (ibfd)->got;
11453 if (got != NULL)
11454 {
11455 got->rawsize = got->size;
11456 got->size = 0;
11457 relgot = ppc64_elf_tdata (ibfd)->relgot;
11458 relgot->rawsize = relgot->size;
11459 relgot->size = 0;
11460 }
11461 }
11462
11463 /* Now reallocate the got, local syms first. We don't need to
11464 allocate section contents again since we never increase size. */
c72f2fb2 11465 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11466 {
11467 struct got_entry **lgot_ents;
11468 struct got_entry **end_lgot_ents;
11469 struct plt_entry **local_plt;
11470 struct plt_entry **end_local_plt;
f961d9dd 11471 unsigned char *lgot_masks;
927be08e
AM
11472 bfd_size_type locsymcount;
11473 Elf_Internal_Shdr *symtab_hdr;
19e08130 11474 asection *s;
927be08e
AM
11475
11476 if (!is_ppc64_elf (ibfd))
11477 continue;
11478
11479 lgot_ents = elf_local_got_ents (ibfd);
11480 if (!lgot_ents)
11481 continue;
11482
11483 symtab_hdr = &elf_symtab_hdr (ibfd);
11484 locsymcount = symtab_hdr->sh_info;
11485 end_lgot_ents = lgot_ents + locsymcount;
11486 local_plt = (struct plt_entry **) end_lgot_ents;
11487 end_local_plt = local_plt + locsymcount;
f961d9dd 11488 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11489 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11490 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11491 {
11492 struct got_entry *ent;
11493
11494 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11495 {
19e08130
AM
11496 unsigned int ent_size = 8;
11497 unsigned int rel_size = sizeof (Elf64_External_Rela);
11498
d0fae19d
AM
11499 ent->got.offset = s->size;
11500 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11501 {
19e08130
AM
11502 ent_size *= 2;
11503 rel_size *= 2;
11504 }
11505 s->size += ent_size;
11506 if ((*lgot_masks & PLT_IFUNC) != 0)
11507 {
33e44f2e 11508 htab->elf.irelplt->size += rel_size;
19e08130
AM
11509 htab->got_reli_size += rel_size;
11510 }
0e1862bb 11511 else if (bfd_link_pic (info))
19e08130
AM
11512 {
11513 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11514 srel->size += rel_size;
d0fae19d
AM
11515 }
11516 }
927be08e
AM
11517 }
11518 }
11519
11520 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11521
c72f2fb2 11522 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11523 {
11524 struct got_entry *ent;
11525
11526 if (!is_ppc64_elf (ibfd))
11527 continue;
11528
11529 ent = ppc64_tlsld_got (ibfd);
11530 if (!ent->is_indirect
11531 && ent->got.offset != (bfd_vma) -1)
11532 {
11533 asection *s = ppc64_elf_tdata (ibfd)->got;
11534 ent->got.offset = s->size;
11535 s->size += 16;
0e1862bb 11536 if (bfd_link_pic (info))
927be08e
AM
11537 {
11538 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11539 srel->size += sizeof (Elf64_External_Rela);
11540 }
11541 }
11542 }
11543
33e44f2e 11544 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11545 if (!done_something)
c72f2fb2 11546 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11547 {
11548 asection *got;
11549
11550 if (!is_ppc64_elf (ibfd))
11551 continue;
11552
11553 got = ppc64_elf_tdata (ibfd)->got;
11554 if (got != NULL)
11555 {
11556 done_something = got->rawsize != got->size;
11557 if (done_something)
11558 break;
11559 }
11560 }
11561
11562 if (done_something)
e7d1c40c 11563 (*htab->params->layout_sections_again) ();
927be08e
AM
11564
11565 /* Set up for second pass over toc sections to recalculate elf_gp
11566 on input sections. */
11567 htab->toc_bfd = NULL;
11568 htab->toc_first_sec = NULL;
11569 htab->second_toc_pass = TRUE;
11570 return done_something;
11571}
11572
11573/* Called after second pass of multitoc partitioning. */
11574
11575void
11576ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11577{
11578 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11579
11580 /* After the second pass, toc_curr tracks the TOC offset used
11581 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11582 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11583}
11584
9b5ecbd0
AM
11585/* No toc references were found in ISEC. If the code in ISEC makes no
11586 calls, then there's no need to use toc adjusting stubs when branching
11587 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11588 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11589 needed, and 2 if a cyclical call-graph was found but no other reason
11590 for a stub was detected. If called from the top level, a return of
11591 2 means the same as a return of 0. */
9b5ecbd0
AM
11592
11593static int
4ce794b7 11594toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11595{
9b5ecbd0 11596 int ret;
70cc837d
AM
11597
11598 /* Mark this section as checked. */
11599 isec->call_check_done = 1;
9b5ecbd0 11600
772119ce
AM
11601 /* We know none of our code bearing sections will need toc stubs. */
11602 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11603 return 0;
11604
eea6121a 11605 if (isec->size == 0)
082c50f8
AM
11606 return 0;
11607
4c52953f
AM
11608 if (isec->output_section == NULL)
11609 return 0;
11610
4c52953f 11611 ret = 0;
70cc837d 11612 if (isec->reloc_count != 0)
9b5ecbd0 11613 {
70cc837d
AM
11614 Elf_Internal_Rela *relstart, *rel;
11615 Elf_Internal_Sym *local_syms;
11616 struct ppc_link_hash_table *htab;
2917689a 11617
70cc837d
AM
11618 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11619 info->keep_memory);
11620 if (relstart == NULL)
11621 return -1;
90aecf7a 11622
70cc837d
AM
11623 /* Look for branches to outside of this section. */
11624 local_syms = NULL;
11625 htab = ppc_hash_table (info);
11626 if (htab == NULL)
11627 return -1;
4c52953f 11628
70cc837d 11629 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11630 {
70cc837d
AM
11631 enum elf_ppc64_reloc_type r_type;
11632 unsigned long r_symndx;
11633 struct elf_link_hash_entry *h;
11634 struct ppc_link_hash_entry *eh;
11635 Elf_Internal_Sym *sym;
11636 asection *sym_sec;
11637 struct _opd_sec_data *opd;
11638 bfd_vma sym_value;
11639 bfd_vma dest;
11640
11641 r_type = ELF64_R_TYPE (rel->r_info);
11642 if (r_type != R_PPC64_REL24
11643 && r_type != R_PPC64_REL14
11644 && r_type != R_PPC64_REL14_BRTAKEN
11645 && r_type != R_PPC64_REL14_BRNTAKEN)
11646 continue;
4c52953f 11647
70cc837d
AM
11648 r_symndx = ELF64_R_SYM (rel->r_info);
11649 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11650 isec->owner))
4c52953f 11651 {
70cc837d
AM
11652 ret = -1;
11653 break;
11654 }
4c52953f 11655
70cc837d
AM
11656 /* Calls to dynamic lib functions go through a plt call stub
11657 that uses r2. */
11658 eh = (struct ppc_link_hash_entry *) h;
11659 if (eh != NULL
11660 && (eh->elf.plt.plist != NULL
11661 || (eh->oh != NULL
11662 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11663 {
11664 ret = 1;
11665 break;
4c52953f
AM
11666 }
11667
70cc837d
AM
11668 if (sym_sec == NULL)
11669 /* Ignore other undefined symbols. */
4c52953f 11670 continue;
4c52953f 11671
70cc837d
AM
11672 /* Assume branches to other sections not included in the
11673 link need stubs too, to cover -R and absolute syms. */
11674 if (sym_sec->output_section == NULL)
11675 {
11676 ret = 1;
11677 break;
11678 }
4c52953f 11679
70cc837d
AM
11680 if (h == NULL)
11681 sym_value = sym->st_value;
11682 else
11683 {
11684 if (h->root.type != bfd_link_hash_defined
11685 && h->root.type != bfd_link_hash_defweak)
11686 abort ();
11687 sym_value = h->root.u.def.value;
11688 }
11689 sym_value += rel->r_addend;
4c52953f 11690
70cc837d
AM
11691 /* If this branch reloc uses an opd sym, find the code section. */
11692 opd = get_opd_info (sym_sec);
11693 if (opd != NULL)
11694 {
11695 if (h == NULL && opd->adjust != NULL)
11696 {
11697 long adjust;
4c52953f 11698
92a9c616 11699 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11700 if (adjust == -1)
11701 /* Assume deleted functions won't ever be called. */
11702 continue;
11703 sym_value += adjust;
11704 }
4c52953f 11705
aef36ac1
AM
11706 dest = opd_entry_value (sym_sec, sym_value,
11707 &sym_sec, NULL, FALSE);
70cc837d
AM
11708 if (dest == (bfd_vma) -1)
11709 continue;
11710 }
11711 else
11712 dest = (sym_value
11713 + sym_sec->output_offset
11714 + sym_sec->output_section->vma);
4c52953f 11715
70cc837d
AM
11716 /* Ignore branch to self. */
11717 if (sym_sec == isec)
11718 continue;
4c52953f 11719
70cc837d
AM
11720 /* If the called function uses the toc, we need a stub. */
11721 if (sym_sec->has_toc_reloc
11722 || sym_sec->makes_toc_func_call)
4c52953f 11723 {
70cc837d 11724 ret = 1;
4c52953f
AM
11725 break;
11726 }
70cc837d
AM
11727
11728 /* Assume any branch that needs a long branch stub might in fact
11729 need a plt_branch stub. A plt_branch stub uses r2. */
11730 else if (dest - (isec->output_offset
11731 + isec->output_section->vma
6911b7dc
AM
11732 + rel->r_offset) + (1 << 25)
11733 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11734 ? h->other
11735 : sym->st_other))
4c52953f 11736 {
70cc837d
AM
11737 ret = 1;
11738 break;
11739 }
11740
11741 /* If calling back to a section in the process of being
11742 tested, we can't say for sure that no toc adjusting stubs
11743 are needed, so don't return zero. */
11744 else if (sym_sec->call_check_in_progress)
11745 ret = 2;
11746
11747 /* Branches to another section that itself doesn't have any TOC
11748 references are OK. Recursively call ourselves to check. */
11749 else if (!sym_sec->call_check_done)
11750 {
11751 int recur;
11752
11753 /* Mark current section as indeterminate, so that other
11754 sections that call back to current won't be marked as
11755 known. */
11756 isec->call_check_in_progress = 1;
11757 recur = toc_adjusting_stub_needed (info, sym_sec);
11758 isec->call_check_in_progress = 0;
11759
4c52953f
AM
11760 if (recur != 0)
11761 {
70cc837d
AM
11762 ret = recur;
11763 if (recur != 2)
11764 break;
4c52953f
AM
11765 }
11766 }
4c52953f 11767 }
70cc837d
AM
11768
11769 if (local_syms != NULL
11770 && (elf_symtab_hdr (isec->owner).contents
11771 != (unsigned char *) local_syms))
11772 free (local_syms);
11773 if (elf_section_data (isec)->relocs != relstart)
11774 free (relstart);
9b5ecbd0
AM
11775 }
11776
70cc837d
AM
11777 if ((ret & 1) == 0
11778 && isec->map_head.s != NULL
11779 && (strcmp (isec->output_section->name, ".init") == 0
11780 || strcmp (isec->output_section->name, ".fini") == 0))
11781 {
11782 if (isec->map_head.s->has_toc_reloc
11783 || isec->map_head.s->makes_toc_func_call)
11784 ret = 1;
11785 else if (!isec->map_head.s->call_check_done)
11786 {
11787 int recur;
11788 isec->call_check_in_progress = 1;
11789 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11790 isec->call_check_in_progress = 0;
11791 if (recur != 0)
11792 ret = recur;
11793 }
11794 }
11795
11796 if (ret == 1)
11797 isec->makes_toc_func_call = 1;
4c52953f 11798
9b5ecbd0
AM
11799 return ret;
11800}
11801
721956f4
AM
11802/* The linker repeatedly calls this function for each input section,
11803 in the order that input sections are linked into output sections.
11804 Build lists of input sections to determine groupings between which
11805 we may insert linker stubs. */
11806
9b5ecbd0 11807bfd_boolean
4ce794b7 11808ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11809{
11810 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11811
4dfe6ac6
NC
11812 if (htab == NULL)
11813 return FALSE;
11814
734b6cf9 11815 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11816 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11817 {
3d6f9012
AM
11818 /* This happens to make the list in reverse order,
11819 which is what we want. */
6f20ed8a
AM
11820 htab->sec_info[isec->id].u.list
11821 = htab->sec_info[isec->output_section->id].u.list;
11822 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 11823 }
ad8e1ba5 11824
4c52953f 11825 if (htab->multi_toc_needed)
9b5ecbd0 11826 {
8b974ba3
AM
11827 /* Analyse sections that aren't already flagged as needing a
11828 valid toc pointer. Exclude .fixup for the linux kernel.
11829 .fixup contains branches, but only back to the function that
11830 hit an exception. */
11831 if (!(isec->has_toc_reloc
11832 || (isec->flags & SEC_CODE) == 0
11833 || strcmp (isec->name, ".fixup") == 0
11834 || isec->call_check_done))
11835 {
11836 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 11837 return FALSE;
8b974ba3
AM
11838 }
11839 /* Make all sections use the TOC assigned for this object file.
11840 This will be wrong for pasted sections; We fix that in
11841 check_pasted_section(). */
11842 if (elf_gp (isec->owner) != 0)
11843 htab->toc_curr = elf_gp (isec->owner);
11844 }
11845
6f20ed8a 11846 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11847 return TRUE;
721956f4
AM
11848}
11849
70cc837d
AM
11850/* Check that all .init and .fini sections use the same toc, if they
11851 have toc relocs. */
11852
11853static bfd_boolean
11854check_pasted_section (struct bfd_link_info *info, const char *name)
11855{
11856 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11857
11858 if (o != NULL)
11859 {
11860 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11861 bfd_vma toc_off = 0;
11862 asection *i;
11863
11864 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11865 if (i->has_toc_reloc)
11866 {
11867 if (toc_off == 0)
6f20ed8a
AM
11868 toc_off = htab->sec_info[i->id].toc_off;
11869 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
11870 return FALSE;
11871 }
6683a28d
AM
11872
11873 if (toc_off == 0)
11874 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11875 if (i->makes_toc_func_call)
11876 {
6f20ed8a 11877 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
11878 break;
11879 }
11880
70cc837d
AM
11881 /* Make sure the whole pasted function uses the same toc offset. */
11882 if (toc_off != 0)
11883 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 11884 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
11885 }
11886 return TRUE;
11887}
11888
11889bfd_boolean
11890ppc64_elf_check_init_fini (struct bfd_link_info *info)
11891{
11892 return (check_pasted_section (info, ".init")
11893 & check_pasted_section (info, ".fini"));
11894}
11895
721956f4
AM
11896/* See whether we can group stub sections together. Grouping stub
11897 sections may result in fewer stubs. More importantly, we need to
11898 put all .init* and .fini* stubs at the beginning of the .init or
11899 .fini output sections respectively, because glibc splits the
11900 _init and _fini functions into multiple parts. Putting a stub in
11901 the middle of a function is not a good idea. */
11902
6f20ed8a
AM
11903static bfd_boolean
11904group_sections (struct bfd_link_info *info,
4ce794b7
AM
11905 bfd_size_type stub_group_size,
11906 bfd_boolean stubs_always_before_branch)
721956f4 11907{
6f20ed8a
AM
11908 struct ppc_link_hash_table *htab;
11909 asection *osec;
7c8fe5c4
AM
11910 bfd_size_type stub14_group_size;
11911 bfd_boolean suppress_size_errors;
11912
6f20ed8a
AM
11913 htab = ppc_hash_table (info);
11914 if (htab == NULL)
11915 return FALSE;
11916
7c8fe5c4 11917 suppress_size_errors = FALSE;
0cfb0717 11918 stub14_group_size = stub_group_size >> 10;
7c8fe5c4
AM
11919 if (stub_group_size == 1)
11920 {
11921 /* Default values. */
11922 if (stubs_always_before_branch)
11923 {
11924 stub_group_size = 0x1e00000;
11925 stub14_group_size = 0x7800;
11926 }
11927 else
11928 {
11929 stub_group_size = 0x1c00000;
11930 stub14_group_size = 0x7000;
11931 }
11932 suppress_size_errors = TRUE;
11933 }
11934
6f20ed8a 11935 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 11936 {
6f20ed8a
AM
11937 asection *tail;
11938
11939 if (osec->id >= htab->sec_info_arr_size)
11940 continue;
11941
11942 tail = htab->sec_info[osec->id].u.list;
734b6cf9 11943 while (tail != NULL)
721956f4 11944 {
734b6cf9
AM
11945 asection *curr;
11946 asection *prev;
11947 bfd_size_type total;
11948 bfd_boolean big_sec;
11949 bfd_vma curr_toc;
6f20ed8a 11950 struct map_stub *group;
734b6cf9
AM
11951
11952 curr = tail;
eea6121a 11953 total = tail->size;
6bee8834
AM
11954 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11955 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
11956 ? stub14_group_size : stub_group_size);
11957 if (big_sec && !suppress_size_errors)
5c3dead3
AM
11958 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11959 tail->owner, tail);
6f20ed8a 11960 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 11961
6f20ed8a 11962 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 11963 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11964 < (ppc64_elf_section_data (prev) != NULL
11965 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11966 ? stub14_group_size : stub_group_size))
6f20ed8a 11967 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
11968 curr = prev;
11969
11970 /* OK, the size from the start of CURR to the end is less
11971 than stub_group_size and thus can be handled by one stub
11972 section. (or the tail section is itself larger than
11973 stub_group_size, in which case we may be toast.) We
11974 should really be keeping track of the total size of stubs
11975 added here, as stubs contribute to the final output
11976 section size. That's a little tricky, and this way will
11977 only break if stubs added make the total size more than
11978 2^25, ie. for the default stub_group_size, if stubs total
11979 more than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
11980 group = bfd_alloc (curr->owner, sizeof (*group));
11981 if (group == NULL)
11982 return FALSE;
11983 group->link_sec = curr;
11984 group->stub_sec = NULL;
a4b6fadd
AM
11985 group->needs_save_res = 0;
11986 group->next = htab->group;
11987 htab->group = group;
734b6cf9 11988 do
721956f4 11989 {
6f20ed8a 11990 prev = htab->sec_info[tail->id].u.list;
734b6cf9 11991 /* Set up this stub group. */
6f20ed8a 11992 htab->sec_info[tail->id].u.group = group;
721956f4 11993 }
734b6cf9
AM
11994 while (tail != curr && (tail = prev) != NULL);
11995
11996 /* But wait, there's more! Input sections up to stub_group_size
11997 bytes before the stub section can be handled by it too.
11998 Don't do this if we have a really large section after the
11999 stubs, as adding more stubs increases the chance that
12000 branches may not reach into the stub section. */
12001 if (!stubs_always_before_branch && !big_sec)
12002 {
12003 total = 0;
12004 while (prev != NULL
12005 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12006 < (ppc64_elf_section_data (prev) != NULL
12007 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 12008 ? stub14_group_size : stub_group_size))
6f20ed8a 12009 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12010 {
12011 tail = prev;
6f20ed8a
AM
12012 prev = htab->sec_info[tail->id].u.list;
12013 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12014 }
12015 }
12016 tail = prev;
721956f4
AM
12017 }
12018 }
6f20ed8a 12019 return TRUE;
721956f4
AM
12020}
12021
58d180e8
AM
12022static const unsigned char glink_eh_frame_cie[] =
12023{
12024 0, 0, 0, 16, /* length. */
12025 0, 0, 0, 0, /* id. */
12026 1, /* CIE version. */
12027 'z', 'R', 0, /* Augmentation string. */
12028 4, /* Code alignment. */
12029 0x78, /* Data alignment. */
12030 65, /* RA reg. */
12031 1, /* Augmentation size. */
12032 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
da44f4e5
AM
12033 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12034 0, 0, 0, 0
58d180e8
AM
12035};
12036
d969d15f
AM
12037/* Stripping output sections is normally done before dynamic section
12038 symbols have been allocated. This function is called later, and
12039 handles cases like htab->brlt which is mapped to its own output
12040 section. */
12041
12042static void
12043maybe_strip_output (struct bfd_link_info *info, asection *isec)
12044{
12045 if (isec->size == 0
12046 && isec->output_section->size == 0
53d8967a 12047 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12048 && !bfd_section_removed_from_list (info->output_bfd,
12049 isec->output_section)
12050 && elf_section_data (isec->output_section)->dynindx == 0)
12051 {
12052 isec->output_section->flags |= SEC_EXCLUDE;
12053 bfd_section_list_remove (info->output_bfd, isec->output_section);
12054 info->output_bfd->section_count--;
12055 }
12056}
12057
721956f4
AM
12058/* Determine and set the size of the stub section for a final link.
12059
12060 The basic idea here is to examine all the relocations looking for
12061 PC-relative calls to a target that is unreachable with a "bl"
12062 instruction. */
12063
b34976b6 12064bfd_boolean
e7d1c40c 12065ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12066{
12067 bfd_size_type stub_group_size;
b34976b6 12068 bfd_boolean stubs_always_before_branch;
721956f4
AM
12069 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12070
4dfe6ac6
NC
12071 if (htab == NULL)
12072 return FALSE;
12073
0e1862bb 12074 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12075 htab->params->plt_thread_safe = 1;
b9e5796b 12076 if (!htab->opd_abi)
e7d1c40c
AM
12077 htab->params->plt_thread_safe = 0;
12078 else if (htab->params->plt_thread_safe == -1)
794e51c0 12079 {
e2458743 12080 static const char *const thread_starter[] =
794e51c0
AM
12081 {
12082 "pthread_create",
12083 /* libstdc++ */
12084 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12085 /* librt */
12086 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12087 "mq_notify", "create_timer",
12088 /* libanl */
12089 "getaddrinfo_a",
12090 /* libgomp */
2300b5a1 12091 "GOMP_parallel",
794e51c0 12092 "GOMP_parallel_start",
2300b5a1 12093 "GOMP_parallel_loop_static",
794e51c0 12094 "GOMP_parallel_loop_static_start",
2300b5a1 12095 "GOMP_parallel_loop_dynamic",
794e51c0 12096 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12097 "GOMP_parallel_loop_guided",
794e51c0 12098 "GOMP_parallel_loop_guided_start",
2300b5a1 12099 "GOMP_parallel_loop_runtime",
794e51c0 12100 "GOMP_parallel_loop_runtime_start",
2300b5a1 12101 "GOMP_parallel_sections",
68ffbac6 12102 "GOMP_parallel_sections_start",
f9dffbf0
AM
12103 /* libgo */
12104 "__go_go",
794e51c0
AM
12105 };
12106 unsigned i;
12107
a4b6fadd 12108 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12109 {
12110 struct elf_link_hash_entry *h;
12111 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12112 FALSE, FALSE, TRUE);
e7d1c40c
AM
12113 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12114 if (htab->params->plt_thread_safe)
794e51c0
AM
12115 break;
12116 }
12117 }
e7d1c40c
AM
12118 stubs_always_before_branch = htab->params->group_size < 0;
12119 if (htab->params->group_size < 0)
12120 stub_group_size = -htab->params->group_size;
721956f4 12121 else
e7d1c40c 12122 stub_group_size = htab->params->group_size;
721956f4 12123
6f20ed8a
AM
12124 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12125 return FALSE;
721956f4 12126
721956f4
AM
12127 while (1)
12128 {
12129 bfd *input_bfd;
12130 unsigned int bfd_indx;
a4b6fadd 12131 struct map_stub *group;
721956f4 12132 asection *stub_sec;
721956f4
AM
12133
12134 htab->stub_iteration += 1;
721956f4
AM
12135
12136 for (input_bfd = info->input_bfds, bfd_indx = 0;
12137 input_bfd != NULL;
c72f2fb2 12138 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12139 {
12140 Elf_Internal_Shdr *symtab_hdr;
12141 asection *section;
6cdc0ccc 12142 Elf_Internal_Sym *local_syms = NULL;
721956f4 12143
0c8d6e5c 12144 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12145 continue;
12146
721956f4 12147 /* We'll need the symbol table in a second. */
0ffa91dd 12148 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12149 if (symtab_hdr->sh_info == 0)
12150 continue;
12151
721956f4
AM
12152 /* Walk over each section attached to the input bfd. */
12153 for (section = input_bfd->sections;
12154 section != NULL;
12155 section = section->next)
12156 {
721956f4 12157 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12158
12159 /* If there aren't any relocs, then there's nothing more
12160 to do. */
12161 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12162 || (section->flags & SEC_ALLOC) == 0
12163 || (section->flags & SEC_LOAD) == 0
12164 || (section->flags & SEC_CODE) == 0
721956f4
AM
12165 || section->reloc_count == 0)
12166 continue;
12167
12168 /* If this section is a link-once section that will be
12169 discarded, then don't create any stubs. */
12170 if (section->output_section == NULL
927be08e 12171 || section->output_section->owner != info->output_bfd)
721956f4
AM
12172 continue;
12173
1e2f5b6e
AM
12174 /* Get the relocs. */
12175 internal_relocs
4ce794b7 12176 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12177 info->keep_memory);
721956f4 12178 if (internal_relocs == NULL)
1e2f5b6e 12179 goto error_ret_free_local;
721956f4
AM
12180
12181 /* Now examine each relocation. */
12182 irela = internal_relocs;
12183 irelaend = irela + section->reloc_count;
12184 for (; irela < irelaend; irela++)
12185 {
4ce794b7
AM
12186 enum elf_ppc64_reloc_type r_type;
12187 unsigned int r_indx;
721956f4
AM
12188 enum ppc_stub_type stub_type;
12189 struct ppc_stub_hash_entry *stub_entry;
8387904d 12190 asection *sym_sec, *code_sec;
e054468f 12191 bfd_vma sym_value, code_value;
721956f4 12192 bfd_vma destination;
6911b7dc 12193 unsigned long local_off;
8843416a 12194 bfd_boolean ok_dest;
721956f4 12195 struct ppc_link_hash_entry *hash;
8387904d 12196 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12197 struct elf_link_hash_entry *h;
12198 Elf_Internal_Sym *sym;
721956f4
AM
12199 char *stub_name;
12200 const asection *id_sec;
74f0fb50 12201 struct _opd_sec_data *opd;
e054468f 12202 struct plt_entry *plt_ent;
721956f4
AM
12203
12204 r_type = ELF64_R_TYPE (irela->r_info);
12205 r_indx = ELF64_R_SYM (irela->r_info);
12206
4ce794b7 12207 if (r_type >= R_PPC64_max)
721956f4
AM
12208 {
12209 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12210 goto error_ret_free_internal;
721956f4
AM
12211 }
12212
12213 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12214 if (r_type != R_PPC64_REL24
12215 && r_type != R_PPC64_REL14
12216 && r_type != R_PPC64_REL14_BRTAKEN
12217 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12218 continue;
12219
12220 /* Now determine the call target, its name, value,
12221 section. */
411e1bfb
AM
12222 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12223 r_indx, input_bfd))
12224 goto error_ret_free_internal;
12225 hash = (struct ppc_link_hash_entry *) h;
12226
8843416a 12227 ok_dest = FALSE;
8387904d 12228 fdh = NULL;
7fe2b9a6 12229 sym_value = 0;
411e1bfb 12230 if (hash == NULL)
721956f4 12231 {
411e1bfb 12232 sym_value = sym->st_value;
8843416a 12233 ok_dest = TRUE;
721956f4 12234 }
7fe2b9a6
AM
12235 else if (hash->elf.root.type == bfd_link_hash_defined
12236 || hash->elf.root.type == bfd_link_hash_defweak)
12237 {
12238 sym_value = hash->elf.root.u.def.value;
12239 if (sym_sec->output_section != NULL)
12240 ok_dest = TRUE;
12241 }
12242 else if (hash->elf.root.type == bfd_link_hash_undefweak
12243 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12244 {
99877b66 12245 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12246 use the func descriptor sym instead if it is
12247 defined. */
ceb1f1ef 12248 if (hash->elf.root.root.string[0] == '.'
b31867b6 12249 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 12250 {
8387904d
AM
12251 if (fdh->elf.root.type == bfd_link_hash_defined
12252 || fdh->elf.root.type == bfd_link_hash_defweak)
12253 {
12254 sym_sec = fdh->elf.root.u.def.section;
12255 sym_value = fdh->elf.root.u.def.value;
12256 if (sym_sec->output_section != NULL)
12257 ok_dest = TRUE;
12258 }
99877b66
AM
12259 else
12260 fdh = NULL;
8387904d 12261 }
7fe2b9a6
AM
12262 }
12263 else
12264 {
12265 bfd_set_error (bfd_error_bad_value);
12266 goto error_ret_free_internal;
721956f4
AM
12267 }
12268
8843416a 12269 destination = 0;
6911b7dc 12270 local_off = 0;
8843416a
AM
12271 if (ok_dest)
12272 {
12273 sym_value += irela->r_addend;
12274 destination = (sym_value
12275 + sym_sec->output_offset
12276 + sym_sec->output_section->vma);
6911b7dc
AM
12277 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12278 ? hash->elf.other
12279 : sym->st_other);
8843416a
AM
12280 }
12281
8387904d 12282 code_sec = sym_sec;
e054468f 12283 code_value = sym_value;
74f0fb50
AM
12284 opd = get_opd_info (sym_sec);
12285 if (opd != NULL)
8387904d
AM
12286 {
12287 bfd_vma dest;
12288
74f0fb50 12289 if (hash == NULL && opd->adjust != NULL)
8387904d 12290 {
51aecdc5 12291 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12292 if (adjust == -1)
12293 continue;
e054468f 12294 code_value += adjust;
8387904d
AM
12295 sym_value += adjust;
12296 }
12297 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12298 &code_sec, &code_value, FALSE);
8387904d
AM
12299 if (dest != (bfd_vma) -1)
12300 {
12301 destination = dest;
12302 if (fdh != NULL)
12303 {
12304 /* Fixup old ABI sym to point at code
12305 entry. */
99877b66 12306 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12307 hash->elf.root.u.def.section = code_sec;
e054468f 12308 hash->elf.root.u.def.value = code_value;
8387904d
AM
12309 }
12310 }
12311 }
12312
721956f4 12313 /* Determine what (if any) linker stub is needed. */
e054468f 12314 plt_ent = NULL;
721956f4 12315 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12316 &plt_ent, destination,
12317 local_off);
ad8e1ba5
AM
12318
12319 if (stub_type != ppc_stub_plt_call)
12320 {
12321 /* Check whether we need a TOC adjusting stub.
12322 Since the linker pastes together pieces from
12323 different object files when creating the
12324 _init and _fini functions, it may be that a
12325 call to what looks like a local sym is in
12326 fact a call needing a TOC adjustment. */
8387904d
AM
12327 if (code_sec != NULL
12328 && code_sec->output_section != NULL
6f20ed8a
AM
12329 && (htab->sec_info[code_sec->id].toc_off
12330 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12331 && (code_sec->has_toc_reloc
12332 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12333 stub_type = ppc_stub_long_branch_r2off;
12334 }
12335
721956f4
AM
12336 if (stub_type == ppc_stub_none)
12337 continue;
12338
411e1bfb
AM
12339 /* __tls_get_addr calls might be eliminated. */
12340 if (stub_type != ppc_stub_plt_call
12341 && hash != NULL
8387904d
AM
12342 && (hash == htab->tls_get_addr
12343 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12344 && section->has_tls_reloc
12345 && irela != internal_relocs)
12346 {
12347 /* Get tls info. */
f961d9dd 12348 unsigned char *tls_mask;
411e1bfb 12349
3a71aa26 12350 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12351 irela - 1, input_bfd))
12352 goto error_ret_free_internal;
e7b938ca 12353 if (*tls_mask != 0)
411e1bfb
AM
12354 continue;
12355 }
12356
3b421ab3
AM
12357 if (stub_type == ppc_stub_plt_call
12358 && irela + 1 < irelaend
12359 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12360 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12361 {
12362 if (!tocsave_find (htab, INSERT,
12363 &local_syms, irela + 1, input_bfd))
12364 goto error_ret_free_internal;
12365 }
12366 else if (stub_type == ppc_stub_plt_call)
12367 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12368
721956f4 12369 /* Support for grouping stub sections. */
6f20ed8a 12370 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12371
12372 /* Get the name of this stub. */
12373 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12374 if (!stub_name)
12375 goto error_ret_free_internal;
12376
12377 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12378 stub_name, FALSE, FALSE);
721956f4
AM
12379 if (stub_entry != NULL)
12380 {
12381 /* The proper stub has already been created. */
12382 free (stub_name);
794e51c0
AM
12383 if (stub_type == ppc_stub_plt_call_r2save)
12384 stub_entry->stub_type = stub_type;
721956f4
AM
12385 continue;
12386 }
12387
25f53a85 12388 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12389 if (stub_entry == NULL)
12390 {
12391 free (stub_name);
6cdc0ccc
AM
12392 error_ret_free_internal:
12393 if (elf_section_data (section)->relocs == NULL)
12394 free (internal_relocs);
12395 error_ret_free_local:
12396 if (local_syms != NULL
12397 && (symtab_hdr->contents
12398 != (unsigned char *) local_syms))
12399 free (local_syms);
b34976b6 12400 return FALSE;
721956f4
AM
12401 }
12402
ad8e1ba5 12403 stub_entry->stub_type = stub_type;
794e51c0
AM
12404 if (stub_type != ppc_stub_plt_call
12405 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12406 {
12407 stub_entry->target_value = code_value;
12408 stub_entry->target_section = code_sec;
12409 }
12410 else
12411 {
12412 stub_entry->target_value = sym_value;
12413 stub_entry->target_section = sym_sec;
12414 }
721956f4 12415 stub_entry->h = hash;
e054468f 12416 stub_entry->plt_ent = plt_ent;
6911b7dc 12417 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12418
12419 if (stub_entry->h != NULL)
12420 htab->stub_globals += 1;
721956f4
AM
12421 }
12422
12423 /* We're done with the internal relocs, free them. */
6cdc0ccc 12424 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12425 free (internal_relocs);
721956f4 12426 }
6cdc0ccc
AM
12427
12428 if (local_syms != NULL
12429 && symtab_hdr->contents != (unsigned char *) local_syms)
12430 {
12431 if (!info->keep_memory)
12432 free (local_syms);
12433 else
12434 symtab_hdr->contents = (unsigned char *) local_syms;
12435 }
721956f4
AM
12436 }
12437
5c3dead3 12438 /* We may have added some stubs. Find out the new size of the
721956f4 12439 stub sections. */
e7d1c40c 12440 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12441 stub_sec != NULL;
12442 stub_sec = stub_sec->next)
e717da7e 12443 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12444 {
5c3dead3 12445 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12446 stub_sec->size = 0;
12447 stub_sec->reloc_count = 0;
84f5d08e 12448 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12449 }
eea6121a
AM
12450
12451 htab->brlt->size = 0;
84f5d08e
AM
12452 htab->brlt->reloc_count = 0;
12453 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12454 if (htab->relbrlt != NULL)
eea6121a 12455 htab->relbrlt->size = 0;
721956f4 12456
63bc6f6c 12457 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12458
a4b6fadd
AM
12459 for (group = htab->group; group != NULL; group = group->next)
12460 if (group->needs_save_res)
12461 group->stub_sec->size += htab->sfpr->size;
12462
176a0d42
AM
12463 if (info->emitrelocations
12464 && htab->glink != NULL && htab->glink->size != 0)
12465 {
12466 htab->glink->reloc_count = 1;
12467 htab->glink->flags |= SEC_RELOC;
12468 }
12469
58d180e8
AM
12470 if (htab->glink_eh_frame != NULL
12471 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12472 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12473 {
4bbe044a 12474 size_t size = 0, align;
58d180e8 12475
e7d1c40c 12476 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12477 stub_sec != NULL;
12478 stub_sec = stub_sec->next)
12479 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
da44f4e5 12480 size += 24;
58d180e8
AM
12481 if (htab->glink != NULL && htab->glink->size != 0)
12482 size += 24;
12483 if (size != 0)
12484 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12485 align = 1;
12486 align <<= htab->glink_eh_frame->output_section->alignment_power;
12487 align -= 1;
12488 size = (size + align) & ~align;
58d180e8
AM
12489 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12490 htab->glink_eh_frame->size = size;
12491 }
12492
e7d1c40c
AM
12493 if (htab->params->plt_stub_align != 0)
12494 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12495 stub_sec != NULL;
12496 stub_sec = stub_sec->next)
12497 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12498 stub_sec->size = ((stub_sec->size
12499 + (1 << htab->params->plt_stub_align) - 1)
29f628db 12500 & -(1 << htab->params->plt_stub_align));
794e51c0 12501
e7d1c40c 12502 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12503 stub_sec != NULL;
12504 stub_sec = stub_sec->next)
12505 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12506 && stub_sec->rawsize != stub_sec->size)
12507 break;
12508
12509 /* Exit from this loop when no stubs have been added, and no stubs
12510 have changed size. */
58d180e8
AM
12511 if (stub_sec == NULL
12512 && (htab->glink_eh_frame == NULL
12513 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12514 break;
12515
721956f4 12516 /* Ask the linker to do its stuff. */
e7d1c40c 12517 (*htab->params->layout_sections_again) ();
721956f4
AM
12518 }
12519
da44f4e5
AM
12520 if (htab->glink_eh_frame != NULL
12521 && htab->glink_eh_frame->size != 0)
12522 {
12523 bfd_vma val;
12524 bfd_byte *p, *last_fde;
12525 size_t last_fde_len, size, align, pad;
12526 asection *stub_sec;
12527
12528 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12529 if (p == NULL)
12530 return FALSE;
12531 htab->glink_eh_frame->contents = p;
12532 last_fde = p;
12533
12534 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12535 /* CIE length (rewrite in case little-endian). */
12536 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12537 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12538 p += sizeof (glink_eh_frame_cie);
12539
12540 for (stub_sec = htab->params->stub_bfd->sections;
12541 stub_sec != NULL;
12542 stub_sec = stub_sec->next)
12543 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12544 {
12545 last_fde = p;
12546 last_fde_len = 20;
12547 /* FDE length. */
12548 bfd_put_32 (htab->elf.dynobj, 20, p);
12549 p += 4;
12550 /* CIE pointer. */
12551 val = p - htab->glink_eh_frame->contents;
12552 bfd_put_32 (htab->elf.dynobj, val, p);
12553 p += 4;
12554 /* Offset to stub section, written later. */
12555 p += 4;
12556 /* stub section size. */
12557 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12558 p += 4;
12559 /* Augmentation. */
12560 p += 1;
12561 /* Pad. */
12562 p += 7;
12563 }
12564 if (htab->glink != NULL && htab->glink->size != 0)
12565 {
12566 last_fde = p;
12567 last_fde_len = 20;
12568 /* FDE length. */
12569 bfd_put_32 (htab->elf.dynobj, 20, p);
12570 p += 4;
12571 /* CIE pointer. */
12572 val = p - htab->glink_eh_frame->contents;
12573 bfd_put_32 (htab->elf.dynobj, val, p);
12574 p += 4;
12575 /* Offset to .glink, written later. */
12576 p += 4;
12577 /* .glink size. */
12578 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12579 p += 4;
12580 /* Augmentation. */
12581 p += 1;
12582
12583 *p++ = DW_CFA_advance_loc + 1;
12584 *p++ = DW_CFA_register;
12585 *p++ = 65;
9f08fa5c 12586 *p++ = htab->opd_abi ? 12 : 0;
da44f4e5
AM
12587 *p++ = DW_CFA_advance_loc + 4;
12588 *p++ = DW_CFA_restore_extended;
12589 *p++ = 65;
12590 }
12591 /* Subsume any padding into the last FDE if user .eh_frame
12592 sections are aligned more than glink_eh_frame. Otherwise any
12593 zero padding will be seen as a terminator. */
12594 size = p - htab->glink_eh_frame->contents;
12595 align = 1;
12596 align <<= htab->glink_eh_frame->output_section->alignment_power;
12597 align -= 1;
12598 pad = ((size + align) & ~align) - size;
12599 htab->glink_eh_frame->size = size + pad;
12600 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12601 }
12602
d969d15f
AM
12603 maybe_strip_output (info, htab->brlt);
12604 if (htab->glink_eh_frame != NULL)
12605 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12606
b34976b6 12607 return TRUE;
721956f4
AM
12608}
12609
12610/* Called after we have determined section placement. If sections
805fc799 12611 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12612
805fc799 12613bfd_vma
1c865ab2 12614ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12615{
805fc799 12616 asection *s;
a27e685f 12617 bfd_vma TOCstart, adjust;
721956f4 12618
43417696
AM
12619 if (info != NULL)
12620 {
12621 struct elf_link_hash_entry *h;
12622 struct elf_link_hash_table *htab = elf_hash_table (info);
12623
12624 if (is_elf_hash_table (htab)
12625 && htab->hgot != NULL)
12626 h = htab->hgot;
12627 else
12628 {
12629 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12630 if (is_elf_hash_table (htab))
12631 htab->hgot = h;
12632 }
12633 if (h != NULL
12634 && h->root.type == bfd_link_hash_defined
12635 && !h->root.linker_def
12636 && (!is_elf_hash_table (htab)
12637 || h->def_regular))
12638 {
12639 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12640 + h->root.u.def.section->output_offset
12641 + h->root.u.def.section->output_section->vma);
12642 _bfd_set_gp_value (obfd, TOCstart);
12643 return TOCstart;
12644 }
12645 }
12646
805fc799
AM
12647 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12648 order. The TOC starts where the first of these sections starts. */
12649 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12650 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12651 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12652 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12653 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12654 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12655 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12656 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12657 {
12658 /* This may happen for
12659 o references to TOC base (SYM@toc / TOC[tc0]) without a
12660 .toc directive
12661 o bad linker script
12662 o --gc-sections and empty TOC sections
12663
12664 FIXME: Warn user? */
12665
12666 /* Look for a likely section. We probably won't even be
12667 using TOCstart. */
12668 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12669 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12670 | SEC_EXCLUDE))
805fc799
AM
12671 == (SEC_ALLOC | SEC_SMALL_DATA))
12672 break;
721956f4 12673 if (s == NULL)
805fc799 12674 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12675 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12676 == (SEC_ALLOC | SEC_SMALL_DATA))
12677 break;
721956f4 12678 if (s == NULL)
805fc799 12679 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12680 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12681 == SEC_ALLOC)
805fc799 12682 break;
721956f4 12683 if (s == NULL)
805fc799 12684 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12685 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12686 break;
12687 }
721956f4 12688
805fc799
AM
12689 TOCstart = 0;
12690 if (s != NULL)
12691 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12692
a27e685f
AM
12693 /* Force alignment. */
12694 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12695 TOCstart -= adjust;
1c865ab2
AM
12696 _bfd_set_gp_value (obfd, TOCstart);
12697
810d4e75 12698 if (info != NULL && s != NULL)
1c865ab2
AM
12699 {
12700 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12701
810d4e75
AM
12702 if (htab != NULL)
12703 {
12704 if (htab->elf.hgot != NULL)
12705 {
a27e685f 12706 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12707 htab->elf.hgot->root.u.def.section = s;
12708 }
12709 }
12710 else
1c865ab2 12711 {
810d4e75
AM
12712 struct bfd_link_hash_entry *bh = NULL;
12713 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12714 s, TOC_BASE_OFF - adjust,
12715 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12716 }
12717 }
805fc799 12718 return TOCstart;
721956f4
AM
12719}
12720
a345bc8d
AM
12721/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12722 write out any global entry stubs. */
12723
12724static bfd_boolean
12725build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12726{
12727 struct bfd_link_info *info;
12728 struct ppc_link_hash_table *htab;
12729 struct plt_entry *pent;
12730 asection *s;
12731
12732 if (h->root.type == bfd_link_hash_indirect)
12733 return TRUE;
12734
12735 if (!h->pointer_equality_needed)
12736 return TRUE;
12737
12738 if (h->def_regular)
12739 return TRUE;
12740
12741 info = inf;
12742 htab = ppc_hash_table (info);
12743 if (htab == NULL)
12744 return FALSE;
12745
12746 s = htab->glink;
12747 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12748 if (pent->plt.offset != (bfd_vma) -1
12749 && pent->addend == 0)
12750 {
12751 bfd_byte *p;
12752 asection *plt;
12753 bfd_vma off;
12754
a345bc8d 12755 p = s->contents + h->root.u.def.value;
33e44f2e 12756 plt = htab->elf.splt;
a345bc8d
AM
12757 if (!htab->elf.dynamic_sections_created
12758 || h->dynindx == -1)
33e44f2e 12759 plt = htab->elf.iplt;
a345bc8d
AM
12760 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12761 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12762
12763 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12764 {
12765 info->callbacks->einfo
12766 (_("%P: linkage table error against `%T'\n"),
12767 h->root.root.string);
12768 bfd_set_error (bfd_error_bad_value);
12769 htab->stub_error = TRUE;
12770 }
12771
7341d5e2
AM
12772 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12773 if (htab->params->emit_stub_syms)
12774 {
12775 size_t len = strlen (h->root.root.string);
12776 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12777
12778 if (name == NULL)
12779 return FALSE;
12780
12781 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12782 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12783 if (h == NULL)
12784 return FALSE;
12785 if (h->root.type == bfd_link_hash_new)
12786 {
12787 h->root.type = bfd_link_hash_defined;
12788 h->root.u.def.section = s;
12789 h->root.u.def.value = p - s->contents;
12790 h->ref_regular = 1;
12791 h->def_regular = 1;
12792 h->ref_regular_nonweak = 1;
12793 h->forced_local = 1;
12794 h->non_elf = 0;
2ec55de3 12795 h->root.linker_def = 1;
7341d5e2
AM
12796 }
12797 }
12798
a345bc8d
AM
12799 if (PPC_HA (off) != 0)
12800 {
12801 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12802 p += 4;
12803 }
12804 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12805 p += 4;
12806 bfd_put_32 (s->owner, MTCTR_R12, p);
12807 p += 4;
12808 bfd_put_32 (s->owner, BCTR, p);
12809 break;
12810 }
12811 return TRUE;
12812}
12813
721956f4
AM
12814/* Build all the stubs associated with the current output file.
12815 The stubs are kept in a hash table attached to the main linker
12816 hash table. This function is called via gldelf64ppc_finish. */
12817
b34976b6 12818bfd_boolean
e7d1c40c 12819ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 12820 char **stats)
5d1634d7
AM
12821{
12822 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 12823 struct map_stub *group;
721956f4 12824 asection *stub_sec;
5d1634d7 12825 bfd_byte *p;
e717da7e 12826 int stub_sec_count = 0;
5d1634d7 12827
4dfe6ac6
NC
12828 if (htab == NULL)
12829 return FALSE;
12830
eea6121a 12831 /* Allocate memory to hold the linker stubs. */
e7d1c40c 12832 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12833 stub_sec != NULL;
12834 stub_sec = stub_sec->next)
eea6121a
AM
12835 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12836 && stub_sec->size != 0)
e717da7e 12837 {
e7d1c40c 12838 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
12839 if (stub_sec->contents == NULL)
12840 return FALSE;
12841 /* We want to check that built size is the same as calculated
12842 size. rawsize is a convenient location to use. */
12843 stub_sec->rawsize = stub_sec->size;
12844 stub_sec->size = 0;
e717da7e 12845 }
5d1634d7 12846
23eb7e01 12847 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12848 {
9f951329 12849 unsigned int indx;
ad8e1ba5 12850 bfd_vma plt0;
9f951329 12851
721956f4 12852 /* Build the .glink plt call stub. */
e7d1c40c 12853 if (htab->params->emit_stub_syms)
97b639ba
AM
12854 {
12855 struct elf_link_hash_entry *h;
468392fb
AM
12856 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12857 TRUE, FALSE, FALSE);
97b639ba
AM
12858 if (h == NULL)
12859 return FALSE;
12860 if (h->root.type == bfd_link_hash_new)
12861 {
12862 h->root.type = bfd_link_hash_defined;
12863 h->root.u.def.section = htab->glink;
ee4bf8d2 12864 h->root.u.def.value = 8;
f5385ebf
AM
12865 h->ref_regular = 1;
12866 h->def_regular = 1;
12867 h->ref_regular_nonweak = 1;
12868 h->forced_local = 1;
12869 h->non_elf = 0;
2ec55de3 12870 h->root.linker_def = 1;
97b639ba
AM
12871 }
12872 }
33e44f2e
AM
12873 plt0 = (htab->elf.splt->output_section->vma
12874 + htab->elf.splt->output_offset
12875 - 16);
176a0d42
AM
12876 if (info->emitrelocations)
12877 {
12878 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12879 if (r == NULL)
12880 return FALSE;
12881 r->r_offset = (htab->glink->output_offset
12882 + htab->glink->output_section->vma);
12883 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12884 r->r_addend = plt0;
12885 }
4ce794b7 12886 p = htab->glink->contents;
176a0d42 12887 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
12888 bfd_put_64 (htab->glink->owner, plt0, p);
12889 p += 8;
b9e5796b
AM
12890 if (htab->opd_abi)
12891 {
12892 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12893 p += 4;
12894 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12895 p += 4;
12896 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12897 p += 4;
12898 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12899 p += 4;
12900 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12901 p += 4;
12902 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12903 p += 4;
12904 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12905 p += 4;
12906 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12907 p += 4;
12908 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12909 p += 4;
12910 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12911 p += 4;
12912 }
12913 else
12914 {
12915 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12916 p += 4;
12917 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12918 p += 4;
12919 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12920 p += 4;
12921 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12922 p += 4;
12923 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12924 p += 4;
12925 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12926 p += 4;
12927 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12928 p += 4;
12929 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12930 p += 4;
12931 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12932 p += 4;
12933 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12934 p += 4;
12935 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12936 p += 4;
12937 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12938 p += 4;
12939 }
4ce794b7 12940 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 12941 p += 4;
ee4bf8d2
AM
12942 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12943 {
12944 bfd_put_32 (htab->glink->owner, NOP, p);
12945 p += 4;
12946 }
ad8e1ba5 12947
9f951329
AM
12948 /* Build the .glink lazy link call stubs. */
12949 indx = 0;
a345bc8d 12950 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 12951 {
b9e5796b 12952 if (htab->opd_abi)
9f951329 12953 {
b9e5796b
AM
12954 if (indx < 0x8000)
12955 {
12956 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12957 p += 4;
12958 }
12959 else
12960 {
12961 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12962 p += 4;
12963 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12964 p);
12965 p += 4;
12966 }
9f951329 12967 }
4ce794b7 12968 bfd_put_32 (htab->glink->owner,
ee4bf8d2 12969 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 12970 indx++;
9f951329
AM
12971 p += 4;
12972 }
a345bc8d
AM
12973
12974 /* Build .glink global entry stubs. */
12975 if (htab->glink->size > htab->glink->rawsize)
afe397ea 12976 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 12977 }
5d1634d7 12978
7341d5e2 12979 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 12980 {
4ce794b7 12981 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 12982 htab->brlt->size);
4ce794b7 12983 if (htab->brlt->contents == NULL)
b34976b6 12984 return FALSE;
721956f4 12985 }
ee75fd95 12986 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
12987 {
12988 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 12989 htab->relbrlt->size);
63bc6f6c
AM
12990 if (htab->relbrlt->contents == NULL)
12991 return FALSE;
12992 }
5d1634d7 12993
721956f4
AM
12994 /* Build the stubs as directed by the stub hash table. */
12995 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 12996
a4b6fadd
AM
12997 for (group = htab->group; group != NULL; group = group->next)
12998 if (group->needs_save_res)
12999 {
13000 stub_sec = group->stub_sec;
13001 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13002 htab->sfpr->size);
13003 if (htab->params->emit_stub_syms)
13004 {
13005 unsigned int i;
13006
13007 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13008 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13009 return FALSE;
13010 }
13011 stub_sec->size += htab->sfpr->size;
13012 }
13013
aa8a7074
AM
13014 if (htab->relbrlt != NULL)
13015 htab->relbrlt->reloc_count = 0;
13016
e7d1c40c
AM
13017 if (htab->params->plt_stub_align != 0)
13018 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
13019 stub_sec != NULL;
13020 stub_sec = stub_sec->next)
13021 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
13022 stub_sec->size = ((stub_sec->size
13023 + (1 << htab->params->plt_stub_align) - 1)
29f628db 13024 & -(1 << htab->params->plt_stub_align));
794e51c0 13025
e7d1c40c 13026 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13027 stub_sec != NULL;
13028 stub_sec = stub_sec->next)
e717da7e
AM
13029 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13030 {
13031 stub_sec_count += 1;
eea6121a 13032 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
13033 break;
13034 }
5d1634d7 13035
da44f4e5
AM
13036 /* Note that the glink_eh_frame check here is not only testing that
13037 the generated size matched the calculated size but also that
13038 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 13039 if (stub_sec != NULL
58d180e8
AM
13040 || (htab->glink_eh_frame != NULL
13041 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 13042 {
b34976b6 13043 htab->stub_error = TRUE;
8de848d8 13044 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13045 }
721956f4 13046
d2a300cf
AM
13047 if (htab->stub_error)
13048 return FALSE;
13049
13050 if (stats != NULL)
13051 {
13052 *stats = bfd_malloc (500);
13053 if (*stats == NULL)
13054 return FALSE;
13055
ee75fd95 13056 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
13057 " branch %lu\n"
13058 " toc adjust %lu\n"
13059 " long branch %lu\n"
13060 " long toc adj %lu\n"
794e51c0 13061 " plt call %lu\n"
7341d5e2
AM
13062 " plt call toc %lu\n"
13063 " global entry %lu"),
e717da7e 13064 stub_sec_count,
ee75fd95 13065 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
13066 htab->stub_count[ppc_stub_long_branch - 1],
13067 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13068 htab->stub_count[ppc_stub_plt_branch - 1],
13069 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13070 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13071 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13072 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13073 }
13074 return TRUE;
5bd4f169
AM
13075}
13076
99877b66
AM
13077/* This function undoes the changes made by add_symbol_adjust. */
13078
13079static bfd_boolean
13080undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
13081{
13082 struct ppc_link_hash_entry *eh;
13083
13084 if (h->root.type == bfd_link_hash_indirect)
13085 return TRUE;
13086
99877b66
AM
13087 eh = (struct ppc_link_hash_entry *) h;
13088 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
13089 return TRUE;
13090
13091 eh->elf.root.type = bfd_link_hash_undefined;
13092 return TRUE;
13093}
13094
13095void
13096ppc64_elf_restore_symbols (struct bfd_link_info *info)
13097{
13098 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
13099
13100 if (htab != NULL)
13101 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
13102}
13103
60124e18
AM
13104/* What to do when ld finds relocations against symbols defined in
13105 discarded sections. */
13106
13107static unsigned int
13108ppc64_elf_action_discarded (asection *sec)
13109{
13110 if (strcmp (".opd", sec->name) == 0)
13111 return 0;
13112
13113 if (strcmp (".toc", sec->name) == 0)
13114 return 0;
13115
bce50a28
JJ
13116 if (strcmp (".toc1", sec->name) == 0)
13117 return 0;
13118
60124e18
AM
13119 return _bfd_elf_default_action_discarded (sec);
13120}
13121
5bd4f169
AM
13122/* The RELOCATE_SECTION function is called by the ELF backend linker
13123 to handle the relocations for a section.
13124
13125 The relocs are always passed as Rela structures; if the section
13126 actually uses Rel structures, the r_addend field will always be
13127 zero.
13128
13129 This function is responsible for adjust the section contents as
13130 necessary, and (if using Rela relocs and generating a
1049f94e 13131 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13132 necessary.
13133
13134 This function does not have to worry about setting the reloc
13135 address or the reloc symbol index.
13136
13137 LOCAL_SYMS is a pointer to the swapped in local symbols.
13138
13139 LOCAL_SECTIONS is an array giving the section in the input file
13140 corresponding to the st_shndx field of each local symbol.
13141
13142 The global hash table entry for the global symbols can be found
13143 via elf_sym_hashes (input_bfd).
13144
1049f94e 13145 When generating relocatable output, this function must handle
5bd4f169
AM
13146 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13147 going to be the section symbol corresponding to the output
13148 section, which means that the addend must be adjusted
13149 accordingly. */
13150
b34976b6 13151static bfd_boolean
4ce794b7
AM
13152ppc64_elf_relocate_section (bfd *output_bfd,
13153 struct bfd_link_info *info,
13154 bfd *input_bfd,
13155 asection *input_section,
13156 bfd_byte *contents,
13157 Elf_Internal_Rela *relocs,
13158 Elf_Internal_Sym *local_syms,
13159 asection **local_sections)
5bd4f169 13160{
65f38f15 13161 struct ppc_link_hash_table *htab;
5bd4f169
AM
13162 Elf_Internal_Shdr *symtab_hdr;
13163 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13164 Elf_Internal_Rela *rel;
c316a17c 13165 Elf_Internal_Rela *wrel;
5bd4f169 13166 Elf_Internal_Rela *relend;
411e1bfb
AM
13167 Elf_Internal_Rela outrel;
13168 bfd_byte *loc;
411e1bfb 13169 struct got_entry **local_got_ents;
5bd4f169 13170 bfd_vma TOCstart;
b34976b6
AM
13171 bfd_boolean ret = TRUE;
13172 bfd_boolean is_opd;
794e51c0
AM
13173 /* Assume 'at' branch hints. */
13174 bfd_boolean is_isa_v2 = TRUE;
4fe5ca5b 13175 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 13176
65f38f15 13177 /* Initialize howto table if needed. */
5bd4f169 13178 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13179 ppc_howto_init ();
13180
65f38f15 13181 htab = ppc_hash_table (info);
4dfe6ac6
NC
13182 if (htab == NULL)
13183 return FALSE;
ee75fd95
AM
13184
13185 /* Don't relocate stub sections. */
e7d1c40c 13186 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13187 return TRUE;
13188
0c8d6e5c 13189 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13190
411e1bfb 13191 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13192 TOCstart = elf_gp (output_bfd);
0ffa91dd 13193 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13194 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13195 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13196
c316a17c 13197 rel = wrel = relocs;
5bd4f169 13198 relend = relocs + input_section->reloc_count;
c316a17c 13199 for (; rel < relend; wrel++, rel++)
5bd4f169 13200 {
04c9666a 13201 enum elf_ppc64_reloc_type r_type;
31c76678 13202 bfd_vma addend;
5bd4f169
AM
13203 bfd_reloc_status_type r;
13204 Elf_Internal_Sym *sym;
13205 asection *sec;
039b3fef
AM
13206 struct elf_link_hash_entry *h_elf;
13207 struct ppc_link_hash_entry *h;
13208 struct ppc_link_hash_entry *fdh;
5bd4f169 13209 const char *sym_name;
0d4792f7 13210 unsigned long r_symndx, toc_symndx;
3a71aa26 13211 bfd_vma toc_addend;
f961d9dd
AM
13212 unsigned char tls_mask, tls_gd, tls_type;
13213 unsigned char sym_type;
5bd4f169 13214 bfd_vma relocation;
b34976b6
AM
13215 bfd_boolean unresolved_reloc;
13216 bfd_boolean warned;
bc30df16 13217 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13218 unsigned int insn;
e11840f9 13219 unsigned int mask;
721956f4
AM
13220 struct ppc_stub_hash_entry *stub_entry;
13221 bfd_vma max_br_offset;
13222 bfd_vma from;
c316a17c 13223 Elf_Internal_Rela orig_rel;
b80eed39
AM
13224 reloc_howto_type *howto;
13225 struct reloc_howto_struct alt_howto;
5bd4f169 13226
c316a17c
AM
13227 again:
13228 orig_rel = *rel;
13229
4ce794b7 13230 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13231 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13232
13233 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13234 symbol of the previous ADDR64 reloc. The symbol gives us the
13235 proper TOC base to use. */
13236 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13237 && wrel != relocs
13238 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13239 && is_opd)
c316a17c 13240 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13241
4ce794b7
AM
13242 sym = NULL;
13243 sec = NULL;
039b3fef 13244 h_elf = NULL;
4ce794b7 13245 sym_name = NULL;
b34976b6
AM
13246 unresolved_reloc = FALSE;
13247 warned = FALSE;
65f38f15 13248
0b13192e 13249 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13250 {
13251 /* It's a local symbol. */
74f0fb50 13252 struct _opd_sec_data *opd;
4025353c 13253
5bd4f169
AM
13254 sym = local_syms + r_symndx;
13255 sec = local_sections[r_symndx];
26c61ae5 13256 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13257 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13258 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13259 opd = get_opd_info (sec);
13260 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13261 {
51aecdc5
AM
13262 long adjust = opd->adjust[OPD_NDX (sym->st_value
13263 + rel->r_addend)];
4025353c
AM
13264 if (adjust == -1)
13265 relocation = 0;
13266 else
4cc603a5
AM
13267 {
13268 /* If this is a relocation against the opd section sym
13269 and we have edited .opd, adjust the reloc addend so
13270 that ld -r and ld --emit-relocs output is correct.
13271 If it is a reloc against some other .opd symbol,
13272 then the symbol value will be adjusted later. */
13273 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13274 rel->r_addend += adjust;
13275 else
13276 relocation += adjust;
13277 }
1e2f5b6e 13278 }
5bd4f169
AM
13279 }
13280 else
13281 {
62d887d4
L
13282 bfd_boolean ignored;
13283
b2a8e766
AM
13284 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13285 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13286 h_elf, sec, relocation,
62d887d4 13287 unresolved_reloc, warned, ignored);
039b3fef
AM
13288 sym_name = h_elf->root.root.string;
13289 sym_type = h_elf->type;
b69fdb4e
AM
13290 if (sec != NULL
13291 && sec->owner == output_bfd
13292 && strcmp (sec->name, ".opd") == 0)
13293 {
13294 /* This is a symbol defined in a linker script. All
13295 such are defined in output sections, even those
13296 defined by simple assignment from a symbol defined in
13297 an input section. Transfer the symbol to an
13298 appropriate input .opd section, so that a branch to
13299 this symbol will be mapped to the location specified
13300 by the opd entry. */
13301 struct bfd_link_order *lo;
13302 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13303 if (lo->type == bfd_indirect_link_order)
13304 {
13305 asection *isec = lo->u.indirect.section;
13306 if (h_elf->root.u.def.value >= isec->output_offset
13307 && h_elf->root.u.def.value < (isec->output_offset
13308 + isec->size))
13309 {
13310 h_elf->root.u.def.value -= isec->output_offset;
13311 h_elf->root.u.def.section = isec;
13312 sec = isec;
13313 break;
13314 }
13315 }
13316 }
5bd4f169 13317 }
039b3fef 13318 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13319
dbaa2011 13320 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13321 {
13322 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13323 input_bfd, input_section,
13324 contents + rel->r_offset);
13325 wrel->r_offset = rel->r_offset;
13326 wrel->r_info = 0;
13327 wrel->r_addend = 0;
13328
13329 /* For ld -r, remove relocations in debug sections against
13330 sections defined in discarded sections. Not done for
13331 non-debug to preserve relocs in .eh_frame which the
13332 eh_frame editing code expects to be present. */
13333 if (bfd_link_relocatable (info)
13334 && (input_section->flags & SEC_DEBUGGING))
13335 wrel--;
13336
13337 continue;
13338 }
ab96bf03 13339
0e1862bb 13340 if (bfd_link_relocatable (info))
c316a17c 13341 goto copy_reloc;
ab96bf03 13342
f40da81b
AM
13343 if (h != NULL && &h->elf == htab->elf.hgot)
13344 {
6f20ed8a 13345 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13346 sec = bfd_abs_section_ptr;
13347 unresolved_reloc = FALSE;
13348 }
13349
951fd09b
AM
13350 /* TLS optimizations. Replace instruction sequences and relocs
13351 based on information we collected in tls_optimize. We edit
13352 RELOCS so that --emit-relocs will output something sensible
13353 for the final instruction stream. */
13354 tls_mask = 0;
13355 tls_gd = 0;
0d4792f7 13356 toc_symndx = 0;
727fc41e
AM
13357 if (h != NULL)
13358 tls_mask = h->tls_mask;
13359 else if (local_got_ents != NULL)
411e1bfb 13360 {
e054468f
AM
13361 struct plt_entry **local_plt = (struct plt_entry **)
13362 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13363 unsigned char *lgot_masks = (unsigned char *)
e054468f 13364 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13365 tls_mask = lgot_masks[r_symndx];
13366 }
13367 if (tls_mask == 0
13368 && (r_type == R_PPC64_TLS
13369 || r_type == R_PPC64_TLSGD
13370 || r_type == R_PPC64_TLSLD))
13371 {
13372 /* Check for toc tls entries. */
f961d9dd 13373 unsigned char *toc_tls;
0d4792f7 13374
727fc41e
AM
13375 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13376 &local_syms, rel, input_bfd))
13377 return FALSE;
0d4792f7 13378
727fc41e
AM
13379 if (toc_tls)
13380 tls_mask = *toc_tls;
0d4792f7
AM
13381 }
13382
13383 /* Check that tls relocs are used with tls syms, and non-tls
13384 relocs are used with non-tls syms. */
cf35638d 13385 if (r_symndx != STN_UNDEF
0d4792f7
AM
13386 && r_type != R_PPC64_NONE
13387 && (h == NULL
039b3fef
AM
13388 || h->elf.root.type == bfd_link_hash_defined
13389 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13390 && (IS_PPC64_TLS_RELOC (r_type)
13391 != (sym_type == STT_TLS
13392 || (sym_type == STT_SECTION
13393 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13394 {
727fc41e
AM
13395 if (tls_mask != 0
13396 && (r_type == R_PPC64_TLS
13397 || r_type == R_PPC64_TLSGD
13398 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13399 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13400 ;
13401 else
25f53a85 13402 info->callbacks->einfo
1d483afe 13403 (!IS_PPC64_TLS_RELOC (r_type)
bc30df16
AM
13404 ? _("%P: %H: %s used with TLS symbol `%T'\n")
13405 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13406 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13407 ppc64_elf_howto_table[r_type]->name,
13408 sym_name);
411e1bfb
AM
13409 }
13410
13411 /* Ensure reloc mapping code below stays sane. */
13412 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13413 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13414 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13415 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13416 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13417 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13418 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13419 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13420 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13421 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13422 abort ();
0d4792f7 13423
411e1bfb
AM
13424 switch (r_type)
13425 {
13426 default:
411e1bfb
AM
13427 break;
13428
ba761f19
AM
13429 case R_PPC64_LO_DS_OPT:
13430 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13431 if ((insn & (0x3f << 26)) != 58u << 26)
13432 abort ();
13433 insn += (14u << 26) - (58u << 26);
13434 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13435 r_type = R_PPC64_TOC16_LO;
13436 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13437 break;
13438
411e1bfb
AM
13439 case R_PPC64_TOC16:
13440 case R_PPC64_TOC16_LO:
13441 case R_PPC64_TOC16_DS:
13442 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13443 {
13444 /* Check for toc tls entries. */
f961d9dd 13445 unsigned char *toc_tls;
951fd09b 13446 int retval;
411e1bfb 13447
3a71aa26
AM
13448 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13449 &local_syms, rel, input_bfd);
951fd09b 13450 if (retval == 0)
411e1bfb
AM
13451 return FALSE;
13452
13453 if (toc_tls)
13454 {
951fd09b 13455 tls_mask = *toc_tls;
411e1bfb
AM
13456 if (r_type == R_PPC64_TOC16_DS
13457 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13458 {
13459 if (tls_mask != 0
13460 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13461 goto toctprel;
13462 }
411e1bfb 13463 else
951fd09b
AM
13464 {
13465 /* If we found a GD reloc pair, then we might be
13466 doing a GD->IE transition. */
13467 if (retval == 2)
13468 {
13469 tls_gd = TLS_TPRELGD;
13470 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13471 goto tls_ldgd_opt;
951fd09b
AM
13472 }
13473 else if (retval == 3)
13474 {
13475 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13476 goto tls_ldgd_opt;
951fd09b
AM
13477 }
13478 }
411e1bfb
AM
13479 }
13480 }
13481 break;
13482
9d6ded02
AM
13483 case R_PPC64_GOT_TPREL16_HI:
13484 case R_PPC64_GOT_TPREL16_HA:
13485 if (tls_mask != 0
13486 && (tls_mask & TLS_TPREL) == 0)
13487 {
13488 rel->r_offset -= d_offset;
13489 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13490 r_type = R_PPC64_NONE;
13491 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13492 }
13493 break;
13494
411e1bfb
AM
13495 case R_PPC64_GOT_TPREL16_DS:
13496 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13497 if (tls_mask != 0
13498 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13499 {
81407a69 13500 toctprel:
c316a17c
AM
13501 insn = bfd_get_32 (output_bfd,
13502 contents + rel->r_offset - d_offset);
411e1bfb
AM
13503 insn &= 31 << 21;
13504 insn |= 0x3c0d0000; /* addis 0,13,0 */
c316a17c
AM
13505 bfd_put_32 (output_bfd, insn,
13506 contents + rel->r_offset - d_offset);
411e1bfb 13507 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13508 if (toc_symndx != 0)
13509 {
13510 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13511 rel->r_addend = toc_addend;
0d4792f7
AM
13512 /* We changed the symbol. Start over in order to
13513 get h, sym, sec etc. right. */
c316a17c 13514 goto again;
0d4792f7
AM
13515 }
13516 else
13517 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13518 }
13519 break;
13520
13521 case R_PPC64_TLS:
951fd09b
AM
13522 if (tls_mask != 0
13523 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13524 {
411e1bfb 13525 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
13526 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13527 if (insn == 0)
411e1bfb 13528 abort ();
411e1bfb 13529 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 13530 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13531 PPC64_TPREL16_LO which is at low-order half-word. */
13532 rel->r_offset += d_offset;
0d4792f7
AM
13533 r_type = R_PPC64_TPREL16_LO;
13534 if (toc_symndx != 0)
13535 {
13536 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13537 rel->r_addend = toc_addend;
0d4792f7
AM
13538 /* We changed the symbol. Start over in order to
13539 get h, sym, sec etc. right. */
c316a17c 13540 goto again;
0d4792f7
AM
13541 }
13542 else
13543 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13544 }
13545 break;
13546
411e1bfb
AM
13547 case R_PPC64_GOT_TLSGD16_HI:
13548 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13549 tls_gd = TLS_TPRELGD;
13550 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13551 goto tls_gdld_hi;
13552 break;
13553
411e1bfb
AM
13554 case R_PPC64_GOT_TLSLD16_HI:
13555 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13556 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13557 {
951fd09b
AM
13558 tls_gdld_hi:
13559 if ((tls_mask & tls_gd) != 0)
13560 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13561 + R_PPC64_GOT_TPREL16_DS);
13562 else
411e1bfb 13563 {
4fe5ca5b 13564 rel->r_offset -= d_offset;
727ac201 13565 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 13566 r_type = R_PPC64_NONE;
411e1bfb 13567 }
951fd09b 13568 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13569 }
13570 break;
13571
951fd09b
AM
13572 case R_PPC64_GOT_TLSGD16:
13573 case R_PPC64_GOT_TLSGD16_LO:
13574 tls_gd = TLS_TPRELGD;
13575 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13576 goto tls_ldgd_opt;
951fd09b 13577 break;
411e1bfb 13578
951fd09b
AM
13579 case R_PPC64_GOT_TLSLD16:
13580 case R_PPC64_GOT_TLSLD16_LO:
13581 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13582 {
3a71aa26 13583 unsigned int insn1, insn2, insn3;
102890f0
AM
13584 bfd_vma offset;
13585
13586 tls_ldgd_opt:
727fc41e
AM
13587 offset = (bfd_vma) -1;
13588 /* If not using the newer R_PPC64_TLSGD/LD to mark
13589 __tls_get_addr calls, we must trust that the call
13590 stays with its arg setup insns, ie. that the next
13591 reloc is the __tls_get_addr call associated with
13592 the current reloc. Edit both insns. */
13593 if (input_section->has_tls_get_addr_call
13594 && rel + 1 < relend
13595 && branch_reloc_hash_match (input_bfd, rel + 1,
13596 htab->tls_get_addr,
13597 htab->tls_get_addr_fd))
13598 offset = rel[1].r_offset;
b86ac8e3
AM
13599 /* We read the low GOT_TLS (or TOC16) insn because we
13600 need to keep the destination reg. It may be
13601 something other than the usual r3, and moved to r3
13602 before the call by intervening code. */
13603 insn1 = bfd_get_32 (output_bfd,
13604 contents + rel->r_offset - d_offset);
102890f0 13605 if ((tls_mask & tls_gd) != 0)
411e1bfb 13606 {
102890f0 13607 /* IE */
b86ac8e3 13608 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13609 insn1 |= 58 << 26; /* ld */
13610 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13611 if (offset != (bfd_vma) -1)
f58d5a2d 13612 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13613 if ((tls_mask & TLS_EXPLICIT) == 0)
13614 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13615 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13616 else
102890f0
AM
13617 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13618 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13619 }
13620 else
13621 {
13622 /* LE */
b86ac8e3
AM
13623 insn1 &= 0x1f << 21;
13624 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13625 insn2 = 0x38630000; /* addi 3,3,0 */
13626 if (tls_gd == 0)
951fd09b 13627 {
102890f0 13628 /* Was an LD reloc. */
1d483afe
AM
13629 if (toc_symndx)
13630 sec = local_sections[toc_symndx];
13631 for (r_symndx = 0;
13632 r_symndx < symtab_hdr->sh_info;
13633 r_symndx++)
13634 if (local_sections[r_symndx] == sec)
13635 break;
13636 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13637 r_symndx = STN_UNDEF;
102890f0 13638 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13639 if (r_symndx != STN_UNDEF)
1d483afe
AM
13640 rel->r_addend -= (local_syms[r_symndx].st_value
13641 + sec->output_offset
13642 + sec->output_section->vma);
951fd09b 13643 }
102890f0 13644 else if (toc_symndx != 0)
3a71aa26
AM
13645 {
13646 r_symndx = toc_symndx;
13647 rel->r_addend = toc_addend;
13648 }
102890f0
AM
13649 r_type = R_PPC64_TPREL16_HA;
13650 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13651 if (offset != (bfd_vma) -1)
13652 {
13653 rel[1].r_info = ELF64_R_INFO (r_symndx,
13654 R_PPC64_TPREL16_LO);
13655 rel[1].r_offset = offset + d_offset;
13656 rel[1].r_addend = rel->r_addend;
13657 }
102890f0 13658 }
3a71aa26
AM
13659 bfd_put_32 (output_bfd, insn1,
13660 contents + rel->r_offset - d_offset);
727fc41e
AM
13661 if (offset != (bfd_vma) -1)
13662 {
13663 insn3 = bfd_get_32 (output_bfd,
13664 contents + offset + 4);
13665 if (insn3 == NOP
13666 || insn3 == CROR_151515 || insn3 == CROR_313131)
13667 {
13668 rel[1].r_offset += 4;
13669 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13670 insn2 = NOP;
13671 }
13672 bfd_put_32 (output_bfd, insn2, contents + offset);
13673 }
13674 if ((tls_mask & tls_gd) == 0
13675 && (tls_gd == 0 || toc_symndx != 0))
13676 {
13677 /* We changed the symbol. Start over in order
13678 to get h, sym, sec etc. right. */
c316a17c 13679 goto again;
727fc41e
AM
13680 }
13681 }
13682 break;
13683
13684 case R_PPC64_TLSGD:
13685 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13686 {
13687 unsigned int insn2, insn3;
13688 bfd_vma offset = rel->r_offset;
13689
13690 if ((tls_mask & TLS_TPRELGD) != 0)
13691 {
13692 /* IE */
13693 r_type = R_PPC64_NONE;
13694 insn2 = 0x7c636a14; /* add 3,3,13 */
13695 }
13696 else
13697 {
13698 /* LE */
13699 if (toc_symndx != 0)
13700 {
13701 r_symndx = toc_symndx;
13702 rel->r_addend = toc_addend;
13703 }
13704 r_type = R_PPC64_TPREL16_LO;
13705 rel->r_offset = offset + d_offset;
13706 insn2 = 0x38630000; /* addi 3,3,0 */
13707 }
13708 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13709 /* Zap the reloc on the _tls_get_addr call too. */
13710 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13711 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
13712 insn3 = bfd_get_32 (output_bfd,
13713 contents + offset + 4);
102890f0
AM
13714 if (insn3 == NOP
13715 || insn3 == CROR_151515 || insn3 == CROR_313131)
13716 {
727fc41e 13717 rel->r_offset += 4;
3a71aa26
AM
13718 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13719 insn2 = NOP;
102890f0 13720 }
102890f0 13721 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 13722 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13723 goto again;
411e1bfb 13724 }
411e1bfb
AM
13725 break;
13726
727fc41e
AM
13727 case R_PPC64_TLSLD:
13728 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13729 {
13730 unsigned int insn2, insn3;
13731 bfd_vma offset = rel->r_offset;
13732
13733 if (toc_symndx)
13734 sec = local_sections[toc_symndx];
13735 for (r_symndx = 0;
13736 r_symndx < symtab_hdr->sh_info;
13737 r_symndx++)
13738 if (local_sections[r_symndx] == sec)
13739 break;
13740 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13741 r_symndx = STN_UNDEF;
727fc41e 13742 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13743 if (r_symndx != STN_UNDEF)
727fc41e
AM
13744 rel->r_addend -= (local_syms[r_symndx].st_value
13745 + sec->output_offset
13746 + sec->output_section->vma);
13747
13748 r_type = R_PPC64_TPREL16_LO;
13749 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13750 rel->r_offset = offset + d_offset;
13751 /* Zap the reloc on the _tls_get_addr call too. */
13752 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13753 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
13754 insn2 = 0x38630000; /* addi 3,3,0 */
13755 insn3 = bfd_get_32 (output_bfd,
13756 contents + offset + 4);
13757 if (insn3 == NOP
13758 || insn3 == CROR_151515 || insn3 == CROR_313131)
13759 {
13760 rel->r_offset += 4;
13761 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13762 insn2 = NOP;
13763 }
13764 bfd_put_32 (output_bfd, insn2, contents + offset);
c316a17c 13765 goto again;
727fc41e
AM
13766 }
13767 break;
13768
411e1bfb 13769 case R_PPC64_DTPMOD64:
951fd09b
AM
13770 if (rel + 1 < relend
13771 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13772 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13773 {
951fd09b
AM
13774 if ((tls_mask & TLS_GD) == 0)
13775 {
13776 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13777 if ((tls_mask & TLS_TPRELGD) != 0)
13778 r_type = R_PPC64_TPREL64;
13779 else
13780 {
4ce794b7 13781 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13782 r_type = R_PPC64_NONE;
13783 }
13784 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13785 }
13786 }
13787 else
13788 {
13789 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13790 {
4ce794b7 13791 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13792 r_type = R_PPC64_NONE;
951fd09b 13793 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13794 }
411e1bfb
AM
13795 }
13796 break;
13797
13798 case R_PPC64_TPREL64:
951fd09b 13799 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13800 {
13801 r_type = R_PPC64_NONE;
13802 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13803 }
13804 break;
52a82034
AM
13805
13806 case R_PPC64_REL16_HA:
13807 /* If we are generating a non-PIC executable, edit
13808 . 0: addis 2,12,.TOC.-0b@ha
13809 . addi 2,2,.TOC.-0b@l
13810 used by ELFv2 global entry points to set up r2, to
13811 . lis 2,.TOC.@ha
13812 . addi 2,2,.TOC.@l
13813 if .TOC. is in range. */
0e1862bb 13814 if (!bfd_link_pic (info)
810d4e75 13815 && !info->traditional_format
52a82034
AM
13816 && h != NULL && &h->elf == htab->elf.hgot
13817 && rel + 1 < relend
13818 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13819 && rel[1].r_offset == rel->r_offset + 4
13820 && rel[1].r_addend == rel->r_addend + 4
13821 && relocation + 0x80008000 <= 0xffffffff)
13822 {
13823 unsigned int insn1, insn2;
13824 bfd_vma offset = rel->r_offset - d_offset;
13825 insn1 = bfd_get_32 (output_bfd, contents + offset);
13826 insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
13827 if ((insn1 & 0xffff0000) == 0x3c4c0000 /* addis 2,12 */
13828 && (insn2 & 0xffff0000) == 0x38420000 /* addi 2,2 */)
13829 {
13830 r_type = R_PPC64_ADDR16_HA;
13831 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13832 rel->r_addend -= d_offset;
13833 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13834 rel[1].r_addend -= d_offset + 4;
13835 bfd_put_32 (output_bfd, 0x3c400000, contents + offset);
13836 }
13837 }
13838 break;
411e1bfb
AM
13839 }
13840
13841 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 13842 insn = 0;
b25116a9
AM
13843 max_br_offset = 1 << 25;
13844 addend = rel->r_addend;
bc30df16 13845 reloc_dest = DEST_NORMAL;
65f38f15 13846 switch (r_type)
5bd4f169
AM
13847 {
13848 default:
65f38f15 13849 break;
5bd4f169 13850
3b421ab3
AM
13851 case R_PPC64_TOCSAVE:
13852 if (relocation + addend == (rel->r_offset
13853 + input_section->output_offset
13854 + input_section->output_section->vma)
13855 && tocsave_find (htab, NO_INSERT,
13856 &local_syms, rel, input_bfd))
13857 {
13858 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13859 if (insn == NOP
13860 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
13861 bfd_put_32 (input_bfd,
13862 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
13863 contents + rel->r_offset);
13864 }
13865 break;
13866
65f38f15
AM
13867 /* Branch taken prediction relocations. */
13868 case R_PPC64_ADDR14_BRTAKEN:
13869 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
13870 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13871 /* Fall thru. */
65f38f15 13872
86c76c7b 13873 /* Branch not taken prediction relocations. */
65f38f15
AM
13874 case R_PPC64_ADDR14_BRNTAKEN:
13875 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
13876 insn |= bfd_get_32 (output_bfd,
13877 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 13878 /* Fall thru. */
86c76c7b 13879
b25116a9
AM
13880 case R_PPC64_REL14:
13881 max_br_offset = 1 << 15;
13882 /* Fall thru. */
5bd4f169 13883
65f38f15 13884 case R_PPC64_REL24:
ad8e1ba5
AM
13885 /* Calls to functions with a different TOC, such as calls to
13886 shared objects, need to alter the TOC pointer. This is
13887 done using a linkage stub. A REL24 branching to these
13888 linkage stubs needs to be followed by a nop, as the nop
13889 will be replaced with an instruction to restore the TOC
13890 base pointer. */
8387904d 13891 fdh = h;
b31867b6
AM
13892 if (h != NULL
13893 && h->oh != NULL
13894 && h->oh->is_func_descriptor)
13895 fdh = ppc_follow_link (h->oh);
31c76678
DK
13896 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13897 htab);
6abec6bc 13898 if (stub_entry != NULL
ad8e1ba5 13899 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 13900 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
13901 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13902 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 13903 {
b25116a9 13904 bfd_boolean can_plt_call = FALSE;
721956f4 13905
ba8ca3e7
AM
13906 /* All of these stubs will modify r2, so there must be a
13907 branch and link followed by a nop. The nop is
13908 replaced by an insn to restore r2. */
eea6121a 13909 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 13910 {
ba8ca3e7
AM
13911 unsigned long br;
13912
13913 br = bfd_get_32 (input_bfd,
13914 contents + rel->r_offset);
13915 if ((br & 1) != 0)
41bd81ab 13916 {
ba8ca3e7
AM
13917 unsigned long nop;
13918
13919 nop = bfd_get_32 (input_bfd,
13920 contents + rel->r_offset + 4);
13921 if (nop == NOP
13922 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 13923 {
ba8ca3e7
AM
13924 if (h != NULL
13925 && (h == htab->tls_get_addr_fd
13926 || h == htab->tls_get_addr)
7c9cf415 13927 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
13928 {
13929 /* Special stub used, leave nop alone. */
13930 }
13931 else
a078d95a
AM
13932 bfd_put_32 (input_bfd,
13933 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
13934 contents + rel->r_offset + 4);
13935 can_plt_call = TRUE;
a7f2871e 13936 }
41bd81ab 13937 }
5bd4f169 13938 }
721956f4 13939
ba8ca3e7 13940 if (!can_plt_call && h != NULL)
721956f4 13941 {
ba8ca3e7
AM
13942 const char *name = h->elf.root.root.string;
13943
13944 if (*name == '.')
13945 ++name;
13946
13947 if (strncmp (name, "__libc_start_main", 17) == 0
13948 && (name[17] == 0 || name[17] == '@'))
6ab189d5 13949 {
ba8ca3e7
AM
13950 /* Allow crt1 branch to go via a toc adjusting
13951 stub. Other calls that never return could do
13952 the same, if we could detect such. */
b25116a9 13953 can_plt_call = TRUE;
6ab189d5 13954 }
ba8ca3e7
AM
13955 }
13956
13957 if (!can_plt_call)
13958 {
13959 /* g++ as of 20130507 emits self-calls without a
13960 following nop. This is arguably wrong since we
13961 have conflicting information. On the one hand a
13962 global symbol and on the other a local call
13963 sequence, but don't error for this special case.
13964 It isn't possible to cheaply verify we have
13965 exactly such a call. Allow all calls to the same
13966 section. */
13967 asection *code_sec = sec;
13968
13969 if (get_opd_info (sec) != NULL)
ad8e1ba5 13970 {
ba8ca3e7
AM
13971 bfd_vma off = (relocation + addend
13972 - sec->output_section->vma
13973 - sec->output_offset);
bc30df16 13974
ba8ca3e7 13975 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 13976 }
ba8ca3e7
AM
13977 if (code_sec == input_section)
13978 can_plt_call = TRUE;
13979 }
13980
13981 if (!can_plt_call)
13982 {
4805fc55
AM
13983 if (stub_entry->stub_type == ppc_stub_plt_call
13984 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13985 info->callbacks->einfo
13986 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13987 "recompile with -fPIC\n"),
13988 input_bfd, input_section, rel->r_offset, sym_name);
13989 else
13990 info->callbacks->einfo
13991 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13992 "(-mcmodel=small toc adjust stub)\n"),
13993 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
13994
13995 bfd_set_error (bfd_error_bad_value);
13996 ret = FALSE;
721956f4
AM
13997 }
13998
b25116a9 13999 if (can_plt_call
794e51c0
AM
14000 && (stub_entry->stub_type == ppc_stub_plt_call
14001 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14002 unresolved_reloc = FALSE;
14003 }
14004
6abec6bc
AM
14005 if ((stub_entry == NULL
14006 || stub_entry->stub_type == ppc_stub_long_branch
14007 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14008 && get_opd_info (sec) != NULL)
14009 {
14010 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14011 bfd_vma off = (relocation + addend
14012 - sec->output_section->vma
14013 - sec->output_offset);
aef36ac1 14014 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14015 if (dest != (bfd_vma) -1)
14016 {
14017 relocation = dest;
14018 addend = 0;
bc30df16 14019 reloc_dest = DEST_OPD;
8387904d
AM
14020 }
14021 }
14022
b25116a9
AM
14023 /* If the branch is out of reach we ought to have a long
14024 branch stub. */
14025 from = (rel->r_offset
14026 + input_section->output_offset
14027 + input_section->output_section->vma);
14028
6911b7dc
AM
14029 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14030 ? fdh->elf.other
14031 : sym->st_other);
14032
6abec6bc
AM
14033 if (stub_entry != NULL
14034 && (stub_entry->stub_type == ppc_stub_long_branch
14035 || stub_entry->stub_type == ppc_stub_plt_branch)
14036 && (r_type == R_PPC64_ADDR14_BRTAKEN
14037 || r_type == R_PPC64_ADDR14_BRNTAKEN
14038 || (relocation + addend - from + max_br_offset
14039 < 2 * max_br_offset)))
14040 /* Don't use the stub if this branch is in range. */
14041 stub_entry = NULL;
b25116a9
AM
14042
14043 if (stub_entry != NULL)
14044 {
14045 /* Munge up the value and addend so that we call the stub
14046 rather than the procedure directly. */
a4b6fadd
AM
14047 asection *stub_sec = stub_entry->group->stub_sec;
14048
14049 if (stub_entry->stub_type == ppc_stub_save_res)
14050 relocation += (stub_sec->output_offset
14051 + stub_sec->output_section->vma
14052 + stub_sec->size - htab->sfpr->size
14053 - htab->sfpr->output_offset
14054 - htab->sfpr->output_section->vma);
14055 else
14056 relocation = (stub_entry->stub_offset
14057 + stub_sec->output_offset
14058 + stub_sec->output_section->vma);
b25116a9 14059 addend = 0;
bc30df16 14060 reloc_dest = DEST_STUB;
3b421ab3 14061
794e51c0
AM
14062 if ((stub_entry->stub_type == ppc_stub_plt_call
14063 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14064 && (ALWAYS_EMIT_R2SAVE
14065 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14066 && rel + 1 < relend
14067 && rel[1].r_offset == rel->r_offset + 4
14068 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14069 relocation += 4;
b25116a9
AM
14070 }
14071
14072 if (insn != 0)
14073 {
794e51c0 14074 if (is_isa_v2)
721956f4 14075 {
b25116a9
AM
14076 /* Set 'a' bit. This is 0b00010 in BO field for branch
14077 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14078 for branch on CTR insns (BO == 1a00t or 1a01t). */
14079 if ((insn & (0x14 << 21)) == (0x04 << 21))
14080 insn |= 0x02 << 21;
14081 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14082 insn |= 0x08 << 21;
14083 else
14084 break;
14085 }
14086 else
14087 {
14088 /* Invert 'y' bit if not the default. */
4cc603a5 14089 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14090 insn ^= 0x01 << 21;
721956f4 14091 }
b25116a9
AM
14092
14093 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 14094 }
e86ce104 14095
06da1e8e
AM
14096 /* NOP out calls to undefined weak functions.
14097 We can thus call a weak function without first
14098 checking whether the function is defined. */
b25116a9 14099 else if (h != NULL
039b3fef 14100 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14101 && h->elf.dynindx == -1
b25116a9
AM
14102 && r_type == R_PPC64_REL24
14103 && relocation == 0
4cc603a5 14104 && addend == 0)
e86ce104 14105 {
06da1e8e 14106 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
c316a17c 14107 goto copy_reloc;
e86ce104 14108 }
65f38f15
AM
14109 break;
14110 }
5bd4f169 14111
65f38f15 14112 /* Set `addend'. */
411e1bfb 14113 tls_type = 0;
65f38f15
AM
14114 switch (r_type)
14115 {
14116 default:
25f53a85 14117 info->callbacks->einfo
bc30df16 14118 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14119 input_bfd, (int) r_type, sym_name);
5bd4f169 14120
65f38f15 14121 bfd_set_error (bfd_error_bad_value);
b34976b6 14122 ret = FALSE;
c316a17c 14123 goto copy_reloc;
5bd4f169 14124
65f38f15 14125 case R_PPC64_NONE:
411e1bfb 14126 case R_PPC64_TLS:
727fc41e
AM
14127 case R_PPC64_TLSGD:
14128 case R_PPC64_TLSLD:
3b421ab3 14129 case R_PPC64_TOCSAVE:
04c9666a
AM
14130 case R_PPC64_GNU_VTINHERIT:
14131 case R_PPC64_GNU_VTENTRY:
c316a17c 14132 goto copy_reloc;
5bd4f169
AM
14133
14134 /* GOT16 relocations. Like an ADDR16 using the symbol's
14135 address in the GOT as relocation value instead of the
411e1bfb 14136 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14137 symbol and put the symbol value there. */
411e1bfb
AM
14138 case R_PPC64_GOT_TLSGD16:
14139 case R_PPC64_GOT_TLSGD16_LO:
14140 case R_PPC64_GOT_TLSGD16_HI:
14141 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14142 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14143 goto dogot;
14144
14145 case R_PPC64_GOT_TLSLD16:
14146 case R_PPC64_GOT_TLSLD16_LO:
14147 case R_PPC64_GOT_TLSLD16_HI:
14148 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14149 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14150 goto dogot;
14151
14152 case R_PPC64_GOT_TPREL16_DS:
14153 case R_PPC64_GOT_TPREL16_LO_DS:
14154 case R_PPC64_GOT_TPREL16_HI:
14155 case R_PPC64_GOT_TPREL16_HA:
14156 tls_type = TLS_TLS | TLS_TPREL;
14157 goto dogot;
14158
14159 case R_PPC64_GOT_DTPREL16_DS:
14160 case R_PPC64_GOT_DTPREL16_LO_DS:
14161 case R_PPC64_GOT_DTPREL16_HI:
14162 case R_PPC64_GOT_DTPREL16_HA:
14163 tls_type = TLS_TLS | TLS_DTPREL;
14164 goto dogot;
14165
65f38f15
AM
14166 case R_PPC64_GOT16:
14167 case R_PPC64_GOT16_LO:
14168 case R_PPC64_GOT16_HI:
14169 case R_PPC64_GOT16_HA:
14170 case R_PPC64_GOT16_DS:
14171 case R_PPC64_GOT16_LO_DS:
411e1bfb 14172 dogot:
5bd4f169
AM
14173 {
14174 /* Relocation is to the entry for this symbol in the global
14175 offset table. */
e717da7e 14176 asection *got;
d881513a 14177 bfd_vma *offp;
5bd4f169 14178 bfd_vma off;
d881513a 14179 unsigned long indx = 0;
927be08e 14180 struct got_entry *ent;
65f38f15 14181
d881513a
AM
14182 if (tls_type == (TLS_TLS | TLS_LD)
14183 && (h == NULL
f5385ebf 14184 || !h->elf.def_dynamic))
927be08e 14185 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14186 else
5bd4f169 14187 {
5bd4f169 14188
d881513a
AM
14189 if (h != NULL)
14190 {
14191 bfd_boolean dyn = htab->elf.dynamic_sections_created;
0e1862bb 14192 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
039b3fef 14193 &h->elf)
0e1862bb 14194 || (bfd_link_pic (info)
afe397ea 14195 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
14196 /* This is actually a static link, or it is a
14197 -Bsymbolic link and the symbol is defined
14198 locally, or the symbol was forced to be local
14199 because of a version file. */
14200 ;
14201 else
14202 {
14acf4dc 14203 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 14204 indx = h->elf.dynindx;
d881513a
AM
14205 unresolved_reloc = FALSE;
14206 }
039b3fef 14207 ent = h->elf.got.glist;
d881513a 14208 }
411e1bfb 14209 else
5bd4f169 14210 {
d881513a
AM
14211 if (local_got_ents == NULL)
14212 abort ();
14213 ent = local_got_ents[r_symndx];
5bd4f169 14214 }
d881513a
AM
14215
14216 for (; ent != NULL; ent = ent->next)
31c76678 14217 if (ent->addend == orig_rel.r_addend
e717da7e 14218 && ent->owner == input_bfd
d881513a
AM
14219 && ent->tls_type == tls_type)
14220 break;
5bd4f169 14221 }
411e1bfb 14222
927be08e
AM
14223 if (ent == NULL)
14224 abort ();
14225 if (ent->is_indirect)
14226 ent = ent->got.ent;
14227 offp = &ent->got.offset;
14228 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14229 if (got == NULL)
14230 abort ();
14231
411e1bfb
AM
14232 /* The offset must always be a multiple of 8. We use the
14233 least significant bit to record whether we have already
14234 processed this entry. */
d881513a 14235 off = *offp;
411e1bfb
AM
14236 if ((off & 1) != 0)
14237 off &= ~1;
5bd4f169
AM
14238 else
14239 {
411e1bfb
AM
14240 /* Generate relocs for the dynamic linker, except in
14241 the case of TLSLD where we'll use one entry per
14242 module. */
25f23106
AM
14243 asection *relgot;
14244 bfd_boolean ifunc;
e717da7e 14245
d881513a 14246 *offp = off | 1;
25f23106
AM
14247 relgot = NULL;
14248 ifunc = (h != NULL
14249 ? h->elf.type == STT_GNU_IFUNC
14250 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14251 if (ifunc)
33e44f2e 14252 relgot = htab->elf.irelplt;
0e1862bb 14253 else if ((bfd_link_pic (info) || indx != 0)
19e08130
AM
14254 && (h == NULL
14255 || (tls_type == (TLS_TLS | TLS_LD)
14256 && !h->elf.def_dynamic)
14257 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14258 || h->elf.root.type != bfd_link_hash_undefweak))
14259 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14260 if (relgot != NULL)
5bd4f169 14261 {
e717da7e
AM
14262 outrel.r_offset = (got->output_section->vma
14263 + got->output_offset
411e1bfb 14264 + off);
4cc603a5 14265 outrel.r_addend = addend;
d881513a 14266 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14267 {
411e1bfb 14268 outrel.r_addend = 0;
e515b051 14269 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14270 if (tls_type == (TLS_TLS | TLS_GD))
14271 {
e717da7e
AM
14272 loc = relgot->contents;
14273 loc += (relgot->reloc_count++
d881513a
AM
14274 * sizeof (Elf64_External_Rela));
14275 bfd_elf64_swap_reloca_out (output_bfd,
14276 &outrel, loc);
e515b051 14277 outrel.r_offset += 8;
4cc603a5 14278 outrel.r_addend = addend;
d881513a
AM
14279 outrel.r_info
14280 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14281 }
411e1bfb 14282 }
951fd09b 14283 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14284 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14285 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14286 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14287 else if (indx != 0)
14288 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14289 else
81407a69 14290 {
25f23106
AM
14291 if (ifunc)
14292 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14293 else
14294 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14295
14296 /* Write the .got section contents for the sake
14297 of prelink. */
e717da7e 14298 loc = got->contents + off;
23fbd6fa
JJ
14299 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14300 loc);
81407a69 14301 }
81407a69
AM
14302
14303 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14304 {
14305 outrel.r_addend += relocation;
14306 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14307 {
14308 if (htab->elf.tls_sec == NULL)
14309 outrel.r_addend = 0;
14310 else
14311 outrel.r_addend -= htab->elf.tls_sec->vma;
14312 }
e515b051 14313 }
e717da7e
AM
14314 loc = relgot->contents;
14315 loc += (relgot->reloc_count++
411e1bfb
AM
14316 * sizeof (Elf64_External_Rela));
14317 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14318 }
14319
ad8e1ba5 14320 /* Init the .got section contents here if we're not
81407a69 14321 emitting a reloc. */
d881513a 14322 else
411e1bfb 14323 {
4cc603a5 14324 relocation += addend;
7b609f53
AM
14325 if (tls_type == (TLS_TLS | TLS_LD))
14326 relocation = 1;
14327 else if (tls_type != 0)
411e1bfb 14328 {
989f9879
AM
14329 if (htab->elf.tls_sec == NULL)
14330 relocation = 0;
14331 else
14332 {
14333 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14334 if (tls_type == (TLS_TLS | TLS_TPREL))
14335 relocation += DTP_OFFSET - TP_OFFSET;
14336 }
5bd4f169 14337
7b609f53
AM
14338 if (tls_type == (TLS_TLS | TLS_GD))
14339 {
14340 bfd_put_64 (output_bfd, relocation,
e717da7e 14341 got->contents + off + 8);
7b609f53
AM
14342 relocation = 1;
14343 }
411e1bfb 14344 }
7b609f53 14345
411e1bfb 14346 bfd_put_64 (output_bfd, relocation,
e717da7e 14347 got->contents + off);
5bd4f169
AM
14348 }
14349 }
14350
65f38f15
AM
14351 if (off >= (bfd_vma) -2)
14352 abort ();
14353
bf102f86 14354 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14355 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14356 }
65f38f15
AM
14357 break;
14358
14359 case R_PPC64_PLT16_HA:
14360 case R_PPC64_PLT16_HI:
14361 case R_PPC64_PLT16_LO:
14362 case R_PPC64_PLT32:
14363 case R_PPC64_PLT64:
14364 /* Relocation is to the entry for this symbol in the
14365 procedure linkage table. */
14366
14367 /* Resolve a PLT reloc against a local symbol directly,
14368 without using the procedure linkage table. */
14369 if (h == NULL)
14370 break;
14371
411e1bfb
AM
14372 /* It's possible that we didn't make a PLT entry for this
14373 symbol. This happens when statically linking PIC code,
14374 or when using -Bsymbolic. Go find a match if there is a
14375 PLT entry. */
33e44f2e 14376 if (htab->elf.splt != NULL)
65f38f15 14377 {
411e1bfb 14378 struct plt_entry *ent;
039b3fef 14379 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
a345bc8d
AM
14380 if (ent->plt.offset != (bfd_vma) -1
14381 && ent->addend == orig_rel.r_addend)
411e1bfb 14382 {
33e44f2e
AM
14383 relocation = (htab->elf.splt->output_section->vma
14384 + htab->elf.splt->output_offset
411e1bfb
AM
14385 + ent->plt.offset);
14386 unresolved_reloc = FALSE;
a345bc8d 14387 break;
411e1bfb 14388 }
65f38f15 14389 }
65f38f15 14390 break;
5bd4f169 14391
0b13192e
AM
14392 case R_PPC64_TOC:
14393 /* Relocation value is TOC base. */
14394 relocation = TOCstart;
cf35638d 14395 if (r_symndx == STN_UNDEF)
6f20ed8a 14396 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14397 else if (unresolved_reloc)
14398 ;
6f20ed8a
AM
14399 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14400 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14401 else
14402 unresolved_reloc = TRUE;
ab96bf03 14403 goto dodyn;
0b13192e 14404
5bd4f169
AM
14405 /* TOC16 relocs. We want the offset relative to the TOC base,
14406 which is the address of the start of the TOC plus 0x8000.
14407 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14408 in this order. */
65f38f15
AM
14409 case R_PPC64_TOC16:
14410 case R_PPC64_TOC16_LO:
14411 case R_PPC64_TOC16_HI:
14412 case R_PPC64_TOC16_DS:
14413 case R_PPC64_TOC16_LO_DS:
14414 case R_PPC64_TOC16_HA:
6f20ed8a 14415 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14416 break;
14417
14418 /* Relocate against the beginning of the section. */
65f38f15
AM
14419 case R_PPC64_SECTOFF:
14420 case R_PPC64_SECTOFF_LO:
14421 case R_PPC64_SECTOFF_HI:
14422 case R_PPC64_SECTOFF_DS:
14423 case R_PPC64_SECTOFF_LO_DS:
14424 case R_PPC64_SECTOFF_HA:
4ce794b7 14425 if (sec != NULL)
65f38f15 14426 addend -= sec->output_section->vma;
5bd4f169
AM
14427 break;
14428
25f23106
AM
14429 case R_PPC64_REL16:
14430 case R_PPC64_REL16_LO:
14431 case R_PPC64_REL16_HI:
14432 case R_PPC64_REL16_HA:
14433 break;
14434
721956f4
AM
14435 case R_PPC64_REL14:
14436 case R_PPC64_REL14_BRNTAKEN:
14437 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14438 case R_PPC64_REL24:
14439 break;
14440
411e1bfb
AM
14441 case R_PPC64_TPREL16:
14442 case R_PPC64_TPREL16_LO:
14443 case R_PPC64_TPREL16_HI:
14444 case R_PPC64_TPREL16_HA:
14445 case R_PPC64_TPREL16_DS:
14446 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14447 case R_PPC64_TPREL16_HIGH:
14448 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14449 case R_PPC64_TPREL16_HIGHER:
14450 case R_PPC64_TPREL16_HIGHERA:
14451 case R_PPC64_TPREL16_HIGHEST:
14452 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14453 if (h != NULL
14454 && h->elf.root.type == bfd_link_hash_undefweak
14455 && h->elf.dynindx == -1)
14456 {
14457 /* Make this relocation against an undefined weak symbol
14458 resolve to zero. This is really just a tweak, since
14459 code using weak externs ought to check that they are
14460 defined before using them. */
14461 bfd_byte *p = contents + rel->r_offset - d_offset;
14462
14463 insn = bfd_get_32 (output_bfd, p);
14464 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14465 if (insn != 0)
14466 bfd_put_32 (output_bfd, insn, p);
14467 break;
14468 }
989f9879
AM
14469 if (htab->elf.tls_sec != NULL)
14470 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
0e1862bb 14471 if (bfd_link_pic (info))
411e1bfb
AM
14472 /* The TPREL16 relocs shouldn't really be used in shared
14473 libs as they will result in DT_TEXTREL being set, but
14474 support them anyway. */
14475 goto dodyn;
14476 break;
14477
14478 case R_PPC64_DTPREL16:
14479 case R_PPC64_DTPREL16_LO:
14480 case R_PPC64_DTPREL16_HI:
14481 case R_PPC64_DTPREL16_HA:
14482 case R_PPC64_DTPREL16_DS:
14483 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14484 case R_PPC64_DTPREL16_HIGH:
14485 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14486 case R_PPC64_DTPREL16_HIGHER:
14487 case R_PPC64_DTPREL16_HIGHERA:
14488 case R_PPC64_DTPREL16_HIGHEST:
14489 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14490 if (htab->elf.tls_sec != NULL)
14491 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14492 break;
14493
45965137
AM
14494 case R_PPC64_ADDR64_LOCAL:
14495 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14496 ? h->elf.other
14497 : sym->st_other);
14498 break;
14499
e515b051
AM
14500 case R_PPC64_DTPMOD64:
14501 relocation = 1;
14502 addend = 0;
14503 goto dodyn;
14504
411e1bfb 14505 case R_PPC64_TPREL64:
989f9879
AM
14506 if (htab->elf.tls_sec != NULL)
14507 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14508 goto dodyn;
14509
14510 case R_PPC64_DTPREL64:
989f9879
AM
14511 if (htab->elf.tls_sec != NULL)
14512 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14513 /* Fall thru */
14514
65f38f15
AM
14515 /* Relocations that may need to be propagated if this is a
14516 dynamic object. */
04c9666a 14517 case R_PPC64_REL30:
65f38f15
AM
14518 case R_PPC64_REL32:
14519 case R_PPC64_REL64:
14520 case R_PPC64_ADDR14:
14521 case R_PPC64_ADDR14_BRNTAKEN:
14522 case R_PPC64_ADDR14_BRTAKEN:
14523 case R_PPC64_ADDR16:
14524 case R_PPC64_ADDR16_DS:
14525 case R_PPC64_ADDR16_HA:
14526 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14527 case R_PPC64_ADDR16_HIGH:
14528 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14529 case R_PPC64_ADDR16_HIGHER:
14530 case R_PPC64_ADDR16_HIGHERA:
14531 case R_PPC64_ADDR16_HIGHEST:
14532 case R_PPC64_ADDR16_HIGHESTA:
14533 case R_PPC64_ADDR16_LO:
14534 case R_PPC64_ADDR16_LO_DS:
14535 case R_PPC64_ADDR24:
65f38f15
AM
14536 case R_PPC64_ADDR32:
14537 case R_PPC64_ADDR64:
14538 case R_PPC64_UADDR16:
14539 case R_PPC64_UADDR32:
14540 case R_PPC64_UADDR64:
411e1bfb 14541 dodyn:
5d1634d7 14542 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14543 break;
14544
41bd81ab
AM
14545 if (NO_OPD_RELOCS && is_opd)
14546 break;
14547
0e1862bb 14548 if ((bfd_link_pic (info)
4e795f50 14549 && (h == NULL
039b3fef
AM
14550 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14551 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 14552 && (must_be_dyn_reloc (info, r_type)
039b3fef 14553 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909 14554 || (ELIMINATE_COPY_RELOCS
0e1862bb 14555 && !bfd_link_pic (info)
65f38f15 14556 && h != NULL
039b3fef 14557 && h->elf.dynindx != -1
f5385ebf 14558 && !h->elf.non_got_ref
25f23106 14559 && !h->elf.def_regular)
0e1862bb 14560 || (!bfd_link_pic (info)
25f23106
AM
14561 && (h != NULL
14562 ? h->elf.type == STT_GNU_IFUNC
14563 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 14564 {
b34976b6 14565 bfd_boolean skip, relocate;
65f38f15 14566 asection *sreloc;
1cf1f670 14567 bfd_vma out_off;
65f38f15
AM
14568
14569 /* When generating a dynamic object, these relocations
14570 are copied into the output file to be resolved at run
14571 time. */
14572
b34976b6
AM
14573 skip = FALSE;
14574 relocate = FALSE;
65f38f15 14575
1cf1f670
AM
14576 out_off = _bfd_elf_section_offset (output_bfd, info,
14577 input_section, rel->r_offset);
14578 if (out_off == (bfd_vma) -1)
b34976b6 14579 skip = TRUE;
1cf1f670 14580 else if (out_off == (bfd_vma) -2)
b34976b6 14581 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14582 out_off += (input_section->output_section->vma
14583 + input_section->output_offset);
14584 outrel.r_offset = out_off;
411e1bfb 14585 outrel.r_addend = rel->r_addend;
65f38f15 14586
1cf1f670
AM
14587 /* Optimize unaligned reloc use. */
14588 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14589 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14590 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14591 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14592 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14593 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14594 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14595 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14596 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14597
65f38f15 14598 if (skip)
0bb2d96a 14599 memset (&outrel, 0, sizeof outrel);
afe397ea 14600 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14601 && !is_opd
14602 && r_type != R_PPC64_TOC)
14acf4dc
MR
14603 {
14604 BFD_ASSERT (h->elf.dynindx != -1);
14605 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14606 }
65f38f15
AM
14607 else
14608 {
41bd81ab
AM
14609 /* This symbol is local, or marked to become local,
14610 or this is an opd section reloc which must point
14611 at a local function. */
65f38f15 14612 outrel.r_addend += relocation;
e86ce104 14613 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14614 {
3fad3c7c 14615 if (is_opd && h != NULL)
afbe61cf
AM
14616 {
14617 /* Lie about opd entries. This case occurs
14618 when building shared libraries and we
14619 reference a function in another shared
3fad3c7c
AM
14620 lib. The same thing happens for a weak
14621 definition in an application that's
14622 overridden by a strong definition in a
14623 shared lib. (I believe this is a generic
14624 bug in binutils handling of weak syms.)
14625 In these cases we won't use the opd
1e2f5b6e 14626 entry in this lib. */
b34976b6 14627 unresolved_reloc = FALSE;
afbe61cf 14628 }
25f23106
AM
14629 if (!is_opd
14630 && r_type == R_PPC64_ADDR64
14631 && (h != NULL
14632 ? h->elf.type == STT_GNU_IFUNC
14633 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14634 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14635 else
14636 {
14637 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14638
25f23106
AM
14639 /* We need to relocate .opd contents for ld.so.
14640 Prelink also wants simple and consistent rules
14641 for relocs. This make all RELATIVE relocs have
14642 *r_offset equal to r_addend. */
14643 relocate = TRUE;
14644 }
65f38f15
AM
14645 }
14646 else
14647 {
14648 long indx = 0;
14649
25f23106
AM
14650 if (h != NULL
14651 ? h->elf.type == STT_GNU_IFUNC
14652 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14653 {
25f53a85 14654 info->callbacks->einfo
bc30df16
AM
14655 (_("%P: %H: %s for indirect "
14656 "function `%T' unsupported\n"),
25f53a85 14657 input_bfd, input_section, rel->r_offset,
25f23106
AM
14658 ppc64_elf_howto_table[r_type]->name,
14659 sym_name);
14660 ret = FALSE;
14661 }
cf35638d 14662 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14663 ;
14664 else if (sec == NULL || sec->owner == NULL)
14665 {
14666 bfd_set_error (bfd_error_bad_value);
b34976b6 14667 return FALSE;
65f38f15
AM
14668 }
14669 else
14670 {
14671 asection *osec;
14672
14673 osec = sec->output_section;
14674 indx = elf_section_data (osec)->dynindx;
14675
74541ad4
AM
14676 if (indx == 0)
14677 {
14678 if ((osec->flags & SEC_READONLY) == 0
14679 && htab->elf.data_index_section != NULL)
14680 osec = htab->elf.data_index_section;
14681 else
14682 osec = htab->elf.text_index_section;
14683 indx = elf_section_data (osec)->dynindx;
14684 }
14685 BFD_ASSERT (indx != 0);
14686
65f38f15
AM
14687 /* We are turning this relocation into one
14688 against a section symbol, so subtract out
14689 the output section's address but not the
14690 offset of the input section in the output
14691 section. */
14692 outrel.r_addend -= osec->vma;
14693 }
14694
14695 outrel.r_info = ELF64_R_INFO (indx, r_type);
14696 }
14697 }
14698
14699 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14700 if (h != NULL
14701 ? h->elf.type == STT_GNU_IFUNC
14702 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
33e44f2e 14703 sreloc = htab->elf.irelplt;
65f38f15
AM
14704 if (sreloc == NULL)
14705 abort ();
14706
dfbb6ac9
AM
14707 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14708 >= sreloc->size)
14709 abort ();
947216bf
AM
14710 loc = sreloc->contents;
14711 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14712 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14713
14714 /* If this reloc is against an external symbol, it will
14715 be computed at runtime, so there's no need to do
81407a69
AM
14716 anything now. However, for the sake of prelink ensure
14717 that the section contents are a known value. */
65f38f15 14718 if (! relocate)
81407a69
AM
14719 {
14720 unresolved_reloc = FALSE;
14721 /* The value chosen here is quite arbitrary as ld.so
14722 ignores section contents except for the special
14723 case of .opd where the contents might be accessed
14724 before relocation. Choose zero, as that won't
14725 cause reloc overflow. */
14726 relocation = 0;
14727 addend = 0;
14728 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14729 to improve backward compatibility with older
14730 versions of ld. */
14731 if (r_type == R_PPC64_ADDR64)
14732 addend = outrel.r_addend;
14733 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14734 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14735 addend = (input_section->output_section->vma
14736 + input_section->output_offset
14737 + rel->r_offset);
81407a69 14738 }
65f38f15 14739 }
5bd4f169
AM
14740 break;
14741
65f38f15
AM
14742 case R_PPC64_COPY:
14743 case R_PPC64_GLOB_DAT:
14744 case R_PPC64_JMP_SLOT:
25f23106 14745 case R_PPC64_JMP_IREL:
65f38f15
AM
14746 case R_PPC64_RELATIVE:
14747 /* We shouldn't ever see these dynamic relocs in relocatable
14748 files. */
ae9a127f 14749 /* Fall through. */
65f38f15
AM
14750
14751 case R_PPC64_PLTGOT16:
14752 case R_PPC64_PLTGOT16_DS:
14753 case R_PPC64_PLTGOT16_HA:
14754 case R_PPC64_PLTGOT16_HI:
14755 case R_PPC64_PLTGOT16_LO:
14756 case R_PPC64_PLTGOT16_LO_DS:
14757 case R_PPC64_PLTREL32:
14758 case R_PPC64_PLTREL64:
14759 /* These ones haven't been implemented yet. */
14760
25f53a85 14761 info->callbacks->einfo
bc30df16 14762 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14763 input_bfd,
4ce794b7 14764 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14765
14766 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14767 ret = FALSE;
c316a17c 14768 goto copy_reloc;
65f38f15 14769 }
5bd4f169 14770
67f0cbdb
AM
14771 /* Multi-instruction sequences that access the TOC can be
14772 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14773 to nop; addi rb,r2,x; */
14774 switch (r_type)
14775 {
14776 default:
14777 break;
14778
14779 case R_PPC64_GOT_TLSLD16_HI:
14780 case R_PPC64_GOT_TLSGD16_HI:
14781 case R_PPC64_GOT_TPREL16_HI:
14782 case R_PPC64_GOT_DTPREL16_HI:
14783 case R_PPC64_GOT16_HI:
14784 case R_PPC64_TOC16_HI:
14785 /* These relocs would only be useful if building up an
14786 offset to later add to r2, perhaps in an indexed
14787 addressing mode instruction. Don't try to optimize.
14788 Unfortunately, the possibility of someone building up an
14789 offset like this or even with the HA relocs, means that
14790 we need to check the high insn when optimizing the low
14791 insn. */
14792 break;
14793
14794 case R_PPC64_GOT_TLSLD16_HA:
14795 case R_PPC64_GOT_TLSGD16_HA:
14796 case R_PPC64_GOT_TPREL16_HA:
14797 case R_PPC64_GOT_DTPREL16_HA:
14798 case R_PPC64_GOT16_HA:
14799 case R_PPC64_TOC16_HA:
98528052 14800 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14801 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
14802 {
14803 bfd_byte *p = contents + (rel->r_offset & ~3);
14804 bfd_put_32 (input_bfd, NOP, p);
14805 }
67f0cbdb
AM
14806 break;
14807
14808 case R_PPC64_GOT_TLSLD16_LO:
14809 case R_PPC64_GOT_TLSGD16_LO:
14810 case R_PPC64_GOT_TPREL16_LO_DS:
14811 case R_PPC64_GOT_DTPREL16_LO_DS:
14812 case R_PPC64_GOT16_LO:
14813 case R_PPC64_GOT16_LO_DS:
14814 case R_PPC64_TOC16_LO:
14815 case R_PPC64_TOC16_LO_DS:
98528052 14816 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14817 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
14818 {
14819 bfd_byte *p = contents + (rel->r_offset & ~3);
14820 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
14821 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14822 {
14823 /* Transform addic to addi when we change reg. */
14824 insn &= ~((0x3f << 26) | (0x1f << 16));
14825 insn |= (14u << 26) | (2 << 16);
14826 }
14827 else
67f0cbdb 14828 {
98528052
AM
14829 insn &= ~(0x1f << 16);
14830 insn |= 2 << 16;
67f0cbdb 14831 }
560c8763 14832 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
14833 }
14834 break;
14835 }
14836
65f38f15 14837 /* Do any further special processing. */
b80eed39 14838 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
14839 switch (r_type)
14840 {
14841 default:
14842 break;
14843
25f23106 14844 case R_PPC64_REL16_HA:
f9c6b907
AM
14845 case R_PPC64_ADDR16_HA:
14846 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14847 case R_PPC64_ADDR16_HIGHERA:
14848 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
14849 case R_PPC64_TOC16_HA:
14850 case R_PPC64_SECTOFF_HA:
411e1bfb 14851 case R_PPC64_TPREL16_HA:
f9c6b907 14852 case R_PPC64_TPREL16_HIGHA:
411e1bfb 14853 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 14854 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
14855 case R_PPC64_DTPREL16_HA:
14856 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 14857 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 14858 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
14859 /* It's just possible that this symbol is a weak symbol
14860 that's not actually defined anywhere. In that case,
14861 'sec' would be NULL, and we should leave the symbol
14862 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
14863 if (sec == NULL)
14864 break;
14865 /* Fall thru */
14866
14867 case R_PPC64_GOT16_HA:
14868 case R_PPC64_PLTGOT16_HA:
14869 case R_PPC64_PLT16_HA:
14870 case R_PPC64_GOT_TLSGD16_HA:
14871 case R_PPC64_GOT_TLSLD16_HA:
14872 case R_PPC64_GOT_TPREL16_HA:
14873 case R_PPC64_GOT_DTPREL16_HA:
14874 /* Add 0x10000 if sign bit in 0:15 is set.
14875 Bits 0:15 are not used. */
14876 addend += 0x8000;
65f38f15
AM
14877 break;
14878
14879 case R_PPC64_ADDR16_DS:
14880 case R_PPC64_ADDR16_LO_DS:
14881 case R_PPC64_GOT16_DS:
14882 case R_PPC64_GOT16_LO_DS:
14883 case R_PPC64_PLT16_LO_DS:
14884 case R_PPC64_SECTOFF_DS:
14885 case R_PPC64_SECTOFF_LO_DS:
14886 case R_PPC64_TOC16_DS:
14887 case R_PPC64_TOC16_LO_DS:
14888 case R_PPC64_PLTGOT16_DS:
14889 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
14890 case R_PPC64_GOT_TPREL16_DS:
14891 case R_PPC64_GOT_TPREL16_LO_DS:
14892 case R_PPC64_GOT_DTPREL16_DS:
14893 case R_PPC64_GOT_DTPREL16_LO_DS:
14894 case R_PPC64_TPREL16_DS:
14895 case R_PPC64_TPREL16_LO_DS:
14896 case R_PPC64_DTPREL16_DS:
14897 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
14898 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14899 mask = 3;
14900 /* If this reloc is against an lq insn, then the value must be
14901 a multiple of 16. This is somewhat of a hack, but the
14902 "correct" way to do this by defining _DQ forms of all the
14903 _DS relocs bloats all reloc switches in this file. It
14904 doesn't seem to make much sense to use any of these relocs
14905 in data, so testing the insn should be safe. */
494dac0c 14906 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
14907 mask = 15;
14908 if (((relocation + addend) & mask) != 0)
65f38f15 14909 {
25f53a85 14910 info->callbacks->einfo
8de848d8 14911 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 14912 input_bfd, input_section, rel->r_offset,
b80eed39 14913 howto->name,
adadcc0c 14914 mask + 1);
65f38f15 14915 bfd_set_error (bfd_error_bad_value);
b34976b6 14916 ret = FALSE;
c316a17c 14917 goto copy_reloc;
65f38f15
AM
14918 }
14919 break;
5bd4f169
AM
14920 }
14921
239e1f3a
AM
14922 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14923 because such sections are not SEC_ALLOC and thus ld.so will
14924 not process them. */
65f38f15 14925 if (unresolved_reloc
239e1f3a 14926 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
14927 && h->elf.def_dynamic)
14928 && _bfd_elf_section_offset (output_bfd, info, input_section,
14929 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 14930 {
25f53a85 14931 info->callbacks->einfo
bc30df16 14932 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 14933 input_bfd, input_section, rel->r_offset,
b80eed39 14934 howto->name,
039b3fef 14935 h->elf.root.root.string);
b34976b6 14936 ret = FALSE;
9c07fe7c 14937 }
5bd4f169 14938
b80eed39
AM
14939 /* 16-bit fields in insns mostly have signed values, but a
14940 few insns have 16-bit unsigned values. Really, we should
14941 have different reloc types. */
14942 if (howto->complain_on_overflow != complain_overflow_dont
14943 && howto->dst_mask == 0xffff
14944 && (input_section->flags & SEC_CODE) != 0)
14945 {
14946 enum complain_overflow complain = complain_overflow_signed;
14947
14948 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
14949 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
14950 complain = complain_overflow_bitfield;
14951 else if (howto->rightshift == 0
14952 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
14953 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
14954 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
14955 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
14956 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
14957 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
14958 complain = complain_overflow_unsigned;
14959 if (howto->complain_on_overflow != complain)
14960 {
14961 alt_howto = *howto;
14962 alt_howto.complain_on_overflow = complain;
14963 howto = &alt_howto;
14964 }
14965 }
14966
14967 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
14968 rel->r_offset, relocation, addend);
5bd4f169 14969
ef60b7ff 14970 if (r != bfd_reloc_ok)
5bd4f169 14971 {
bc30df16 14972 char *more_info = NULL;
b80eed39 14973 const char *reloc_name = howto->name;
bc30df16
AM
14974
14975 if (reloc_dest != DEST_NORMAL)
14976 {
14977 more_info = bfd_malloc (strlen (reloc_name) + 8);
14978 if (more_info != NULL)
14979 {
14980 strcpy (more_info, reloc_name);
14981 strcat (more_info, (reloc_dest == DEST_OPD
14982 ? " (OPD)" : " (stub)"));
14983 reloc_name = more_info;
14984 }
14985 }
14986
cd27b276 14987 if (r == bfd_reloc_overflow)
5bd4f169 14988 {
8131c122
AM
14989 /* On code like "if (foo) foo();" don't report overflow
14990 on a branch to zero when foo is undefined. */
14991 if (!warned
14992 && (reloc_dest == DEST_STUB
14993 || !(h != NULL
14994 && (h->elf.root.type == bfd_link_hash_undefweak
14995 || h->elf.root.type == bfd_link_hash_undefined)
14996 && is_branch_reloc (r_type))))
5bd4f169 14997 {
8131c122
AM
14998 if (!((*info->callbacks->reloc_overflow)
14999 (info, &h->elf.root, sym_name,
15000 reloc_name, orig_rel.r_addend,
15001 input_bfd, input_section, rel->r_offset)))
15002 return FALSE;
5bd4f169 15003 }
ef60b7ff
AM
15004 }
15005 else
15006 {
25f53a85 15007 info->callbacks->einfo
bc30df16 15008 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 15009 input_bfd, input_section, rel->r_offset,
bc30df16 15010 reloc_name, sym_name, (int) r);
b34976b6 15011 ret = FALSE;
ef60b7ff 15012 }
bc30df16
AM
15013 if (more_info != NULL)
15014 free (more_info);
5bd4f169 15015 }
c316a17c
AM
15016 copy_reloc:
15017 if (wrel != rel)
15018 *wrel = *rel;
15019 }
15020
15021 if (wrel != rel)
15022 {
15023 Elf_Internal_Shdr *rel_hdr;
15024 size_t deleted = rel - wrel;
15025
15026 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15027 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15028 if (rel_hdr->sh_size == 0)
15029 {
15030 /* It is too late to remove an empty reloc section. Leave
15031 one NONE reloc.
15032 ??? What is wrong with an empty section??? */
15033 rel_hdr->sh_size = rel_hdr->sh_entsize;
15034 deleted -= 1;
15035 }
15036 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15037 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15038 input_section->reloc_count -= deleted;
5bd4f169
AM
15039 }
15040
645ea6a9
AM
15041 /* If we're emitting relocations, then shortly after this function
15042 returns, reloc offsets and addends for this section will be
15043 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15044 file rather than the input. Save a copy of the relocs for
15045 opd_entry_value. */
0e1862bb 15046 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15047 {
15048 bfd_size_type amt;
15049 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15050 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15051 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15052 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15053 if (rel == NULL)
15054 return FALSE;
15055 memcpy (rel, relocs, amt);
15056 }
5bd4f169
AM
15057 return ret;
15058}
15059
754021d0
AM
15060/* Adjust the value of any local symbols in opd sections. */
15061
6e0b88f1 15062static int
754021d0
AM
15063ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15064 const char *name ATTRIBUTE_UNUSED,
15065 Elf_Internal_Sym *elfsym,
15066 asection *input_sec,
15067 struct elf_link_hash_entry *h)
15068{
74f0fb50
AM
15069 struct _opd_sec_data *opd;
15070 long adjust;
754021d0
AM
15071 bfd_vma value;
15072
4025353c 15073 if (h != NULL)
6e0b88f1 15074 return 1;
4025353c 15075
74f0fb50
AM
15076 opd = get_opd_info (input_sec);
15077 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15078 return 1;
754021d0
AM
15079
15080 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15081 if (!bfd_link_relocatable (info))
754021d0
AM
15082 value -= input_sec->output_section->vma;
15083
51aecdc5 15084 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15085 if (adjust == -1)
6e0b88f1
AM
15086 return 2;
15087
15088 elfsym->st_value += adjust;
15089 return 1;
754021d0
AM
15090}
15091
5bd4f169
AM
15092/* Finish up dynamic symbol handling. We set the contents of various
15093 dynamic sections here. */
15094
b34976b6 15095static bfd_boolean
4ce794b7
AM
15096ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15097 struct bfd_link_info *info,
15098 struct elf_link_hash_entry *h,
ab6dce23 15099 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 15100{
65f38f15 15101 struct ppc_link_hash_table *htab;
8387904d
AM
15102 struct plt_entry *ent;
15103 Elf_Internal_Rela rela;
15104 bfd_byte *loc;
5bd4f169 15105
65f38f15 15106 htab = ppc_hash_table (info);
4dfe6ac6
NC
15107 if (htab == NULL)
15108 return FALSE;
5bd4f169 15109
8387904d
AM
15110 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15111 if (ent->plt.offset != (bfd_vma) -1)
15112 {
15113 /* This symbol has an entry in the procedure linkage
15114 table. Set it up. */
e054468f
AM
15115 if (!htab->elf.dynamic_sections_created
15116 || h->dynindx == -1)
15117 {
15118 BFD_ASSERT (h->type == STT_GNU_IFUNC
15119 && h->def_regular
15120 && (h->root.type == bfd_link_hash_defined
15121 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15122 rela.r_offset = (htab->elf.iplt->output_section->vma
15123 + htab->elf.iplt->output_offset
25f23106 15124 + ent->plt.offset);
ee67d69a
AM
15125 if (htab->opd_abi)
15126 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15127 else
15128 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15129 rela.r_addend = (h->root.u.def.value
15130 + h->root.u.def.section->output_offset
15131 + h->root.u.def.section->output_section->vma
15132 + ent->addend);
33e44f2e
AM
15133 loc = (htab->elf.irelplt->contents
15134 + (htab->elf.irelplt->reloc_count++
25f23106 15135 * sizeof (Elf64_External_Rela)));
e054468f
AM
15136 }
15137 else
15138 {
33e44f2e
AM
15139 rela.r_offset = (htab->elf.splt->output_section->vma
15140 + htab->elf.splt->output_offset
25f23106 15141 + ent->plt.offset);
e054468f
AM
15142 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15143 rela.r_addend = ent->addend;
33e44f2e 15144 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15145 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15146 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 15147 }
8387904d 15148 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15149
15150 if (!htab->opd_abi)
15151 {
15152 if (!h->def_regular)
15153 {
15154 /* Mark the symbol as undefined, rather than as
15155 defined in glink. Leave the value if there were
15156 any relocations where pointer equality matters
15157 (this is a clue for the dynamic linker, to make
15158 function pointer comparisons work between an
15159 application and shared library), otherwise set it
15160 to zero. */
15161 sym->st_shndx = SHN_UNDEF;
15162 if (!h->pointer_equality_needed)
15163 sym->st_value = 0;
15164 else if (!h->ref_regular_nonweak)
15165 {
15166 /* This breaks function pointer comparisons, but
15167 that is better than breaking tests for a NULL
15168 function pointer. */
15169 sym->st_value = 0;
15170 }
15171 }
15172 }
8387904d 15173 }
5bd4f169 15174
f5385ebf 15175 if (h->needs_copy)
5bd4f169 15176 {
65f38f15 15177 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 15178
65f38f15
AM
15179 if (h->dynindx == -1
15180 || (h->root.type != bfd_link_hash_defined
15181 && h->root.type != bfd_link_hash_defweak)
4ce794b7 15182 || htab->relbss == NULL)
65f38f15 15183 abort ();
5bd4f169
AM
15184
15185 rela.r_offset = (h->root.u.def.value
15186 + h->root.u.def.section->output_section->vma
15187 + h->root.u.def.section->output_offset);
15188 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15189 rela.r_addend = 0;
4ce794b7
AM
15190 loc = htab->relbss->contents;
15191 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15192 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15193 }
15194
b34976b6 15195 return TRUE;
5bd4f169
AM
15196}
15197
65f38f15
AM
15198/* Used to decide how to sort relocs in an optimal manner for the
15199 dynamic linker, before writing them out. */
15200
15201static enum elf_reloc_type_class
7e612e98
AM
15202ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15203 const asection *rel_sec,
15204 const Elf_Internal_Rela *rela)
65f38f15 15205{
04c9666a 15206 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15207 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15208
33e44f2e 15209 if (rel_sec == htab->elf.irelplt)
7e612e98 15210 return reloc_class_ifunc;
a33d1f77 15211
4ce794b7 15212 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15213 switch (r_type)
65f38f15
AM
15214 {
15215 case R_PPC64_RELATIVE:
15216 return reloc_class_relative;
15217 case R_PPC64_JMP_SLOT:
15218 return reloc_class_plt;
15219 case R_PPC64_COPY:
15220 return reloc_class_copy;
15221 default:
15222 return reloc_class_normal;
15223 }
15224}
15225
5bd4f169
AM
15226/* Finish up the dynamic sections. */
15227
b34976b6 15228static bfd_boolean
4ce794b7
AM
15229ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15230 struct bfd_link_info *info)
5bd4f169 15231{
65f38f15
AM
15232 struct ppc_link_hash_table *htab;
15233 bfd *dynobj;
5bd4f169 15234 asection *sdyn;
5bd4f169 15235
65f38f15 15236 htab = ppc_hash_table (info);
4dfe6ac6
NC
15237 if (htab == NULL)
15238 return FALSE;
15239
65f38f15 15240 dynobj = htab->elf.dynobj;
3d4d4302 15241 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15242
65f38f15 15243 if (htab->elf.dynamic_sections_created)
5bd4f169 15244 {
5bd4f169
AM
15245 Elf64_External_Dyn *dyncon, *dynconend;
15246
33e44f2e 15247 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15248 abort ();
5bd4f169
AM
15249
15250 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15251 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15252 for (; dyncon < dynconend; dyncon++)
15253 {
15254 Elf_Internal_Dyn dyn;
19397422 15255 asection *s;
5bd4f169
AM
15256
15257 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15258
15259 switch (dyn.d_tag)
15260 {
65f38f15
AM
15261 default:
15262 continue;
5bd4f169 15263
5d1634d7 15264 case DT_PPC64_GLINK:
4ce794b7 15265 s = htab->glink;
6348e046 15266 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15267 /* We stupidly defined DT_PPC64_GLINK to be the start
15268 of glink rather than the first entry point, which is
15269 what ld.so needs, and now have a bigger stub to
15270 support automatic multiple TOCs. */
b9e5796b 15271 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15272 break;
15273
19397422
AM
15274 case DT_PPC64_OPD:
15275 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15276 if (s == NULL)
15277 continue;
15278 dyn.d_un.d_ptr = s->vma;
19397422
AM
15279 break;
15280
e8910a83
AM
15281 case DT_PPC64_OPT:
15282 if (htab->do_multi_toc && htab->multi_toc_needed)
15283 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15284 break;
15285
19397422
AM
15286 case DT_PPC64_OPDSZ:
15287 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15288 if (s == NULL)
15289 continue;
eea6121a 15290 dyn.d_un.d_val = s->size;
19397422
AM
15291 break;
15292
65f38f15 15293 case DT_PLTGOT:
33e44f2e 15294 s = htab->elf.splt;
6348e046 15295 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15296 break;
15297
15298 case DT_JMPREL:
33e44f2e 15299 s = htab->elf.srelplt;
6348e046 15300 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15301 break;
5bd4f169 15302
65f38f15 15303 case DT_PLTRELSZ:
33e44f2e 15304 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7
AM
15305 break;
15306
15307 case DT_RELASZ:
15308 /* Don't count procedure linkage table relocs in the
15309 overall reloc count. */
33e44f2e 15310 s = htab->elf.srelplt;
6348e046
AM
15311 if (s == NULL)
15312 continue;
eea6121a 15313 dyn.d_un.d_val -= s->size;
6348e046
AM
15314 break;
15315
15316 case DT_RELA:
15317 /* We may not be using the standard ELF linker script.
15318 If .rela.plt is the first .rela section, we adjust
15319 DT_RELA to not include it. */
33e44f2e 15320 s = htab->elf.srelplt;
6348e046
AM
15321 if (s == NULL)
15322 continue;
15323 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
15324 continue;
eea6121a 15325 dyn.d_un.d_ptr += s->size;
65f38f15 15326 break;
5bd4f169 15327 }
5bd4f169 15328
65f38f15 15329 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15330 }
5bd4f169
AM
15331 }
15332
33e44f2e 15333 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
5d1634d7
AM
15334 {
15335 /* Fill in the first entry in the global offset table.
15336 We use it to hold the link-time TOCbase. */
15337 bfd_put_64 (output_bfd,
60ee0d4a 15338 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15339 htab->elf.sgot->contents);
5d1634d7
AM
15340
15341 /* Set .got entry size. */
33e44f2e 15342 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15343 }
15344
33e44f2e 15345 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5d1634d7
AM
15346 {
15347 /* Set .plt entry size. */
33e44f2e 15348 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15349 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15350 }
15351
84f5d08e
AM
15352 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15353 brlt ourselves if emitrelocations. */
15354 if (htab->brlt != NULL
15355 && htab->brlt->reloc_count != 0
15356 && !_bfd_elf_link_output_relocs (output_bfd,
15357 htab->brlt,
d4730f92 15358 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15359 elf_section_data (htab->brlt)->relocs,
15360 NULL))
15361 return FALSE;
15362
176a0d42
AM
15363 if (htab->glink != NULL
15364 && htab->glink->reloc_count != 0
15365 && !_bfd_elf_link_output_relocs (output_bfd,
15366 htab->glink,
d4730f92 15367 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15368 elf_section_data (htab->glink)->relocs,
15369 NULL))
15370 return FALSE;
15371
58d180e8 15372 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15373 && htab->glink_eh_frame->size != 0)
15374 {
15375 bfd_vma val;
15376 bfd_byte *p;
15377 asection *stub_sec;
15378
15379 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15380 for (stub_sec = htab->params->stub_bfd->sections;
15381 stub_sec != NULL;
15382 stub_sec = stub_sec->next)
15383 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15384 {
15385 /* FDE length. */
15386 p += 4;
15387 /* CIE pointer. */
15388 p += 4;
15389 /* Offset to stub section. */
15390 val = (stub_sec->output_section->vma
15391 + stub_sec->output_offset);
15392 val -= (htab->glink_eh_frame->output_section->vma
15393 + htab->glink_eh_frame->output_offset
15394 + (p - htab->glink_eh_frame->contents));
15395 if (val + 0x80000000 > 0xffffffff)
15396 {
15397 info->callbacks->einfo
15398 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15399 stub_sec->name);
15400 return FALSE;
15401 }
15402 bfd_put_32 (dynobj, val, p);
15403 p += 4;
15404 /* stub section size. */
15405 p += 4;
15406 /* Augmentation. */
15407 p += 1;
15408 /* Pad. */
15409 p += 7;
15410 }
15411 if (htab->glink != NULL && htab->glink->size != 0)
15412 {
15413 /* FDE length. */
15414 p += 4;
15415 /* CIE pointer. */
15416 p += 4;
15417 /* Offset to .glink. */
15418 val = (htab->glink->output_section->vma
15419 + htab->glink->output_offset
15420 + 8);
15421 val -= (htab->glink_eh_frame->output_section->vma
15422 + htab->glink_eh_frame->output_offset
15423 + (p - htab->glink_eh_frame->contents));
15424 if (val + 0x80000000 > 0xffffffff)
15425 {
15426 info->callbacks->einfo
15427 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15428 htab->glink->name);
15429 return FALSE;
15430 }
15431 bfd_put_32 (dynobj, val, p);
15432 p += 4;
15433 /* .glink size. */
15434 p += 4;
15435 /* Augmentation. */
15436 p += 1;
15437 /* Ops. */
15438 p += 7;
15439 }
15440
15441 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15442 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15443 htab->glink_eh_frame,
15444 htab->glink_eh_frame->contents))
15445 return FALSE;
15446 }
58d180e8 15447
e717da7e 15448 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15449 since we didn't add them to DYNOBJ. We know dynobj is the first
15450 bfd. */
c72f2fb2 15451 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15452 {
15453 asection *s;
7b53ace3 15454
0c8d6e5c 15455 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15456 continue;
15457
e717da7e
AM
15458 s = ppc64_elf_tdata (dynobj)->got;
15459 if (s != NULL
eea6121a 15460 && s->size != 0
e717da7e
AM
15461 && s->output_section != bfd_abs_section_ptr
15462 && !bfd_set_section_contents (output_bfd, s->output_section,
15463 s->contents, s->output_offset,
eea6121a 15464 s->size))
e717da7e
AM
15465 return FALSE;
15466 s = ppc64_elf_tdata (dynobj)->relgot;
15467 if (s != NULL
eea6121a 15468 && s->size != 0
e717da7e
AM
15469 && s->output_section != bfd_abs_section_ptr
15470 && !bfd_set_section_contents (output_bfd, s->output_section,
15471 s->contents, s->output_offset,
eea6121a 15472 s->size))
e717da7e
AM
15473 return FALSE;
15474 }
f6c52c13 15475
b34976b6 15476 return TRUE;
5bd4f169
AM
15477}
15478
5bd4f169 15479#include "elf64-target.h"
7b8e7dad
AM
15480
15481/* FreeBSD support */
15482
15483#undef TARGET_LITTLE_SYM
15484#undef TARGET_LITTLE_NAME
15485
15486#undef TARGET_BIG_SYM
6d00b590 15487#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15488#undef TARGET_BIG_NAME
15489#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15490
15491#undef ELF_OSABI
15492#define ELF_OSABI ELFOSABI_FREEBSD
15493
15494#undef elf64_bed
15495#define elf64_bed elf64_powerpc_fbsd_bed
15496
15497#include "elf64-target.h"
15498