]> 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
AM
240
241static inline int
242abiversion (bfd *abfd)
243{
244 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
245}
246
247static inline void
248set_abiversion (bfd *abfd, int ver)
249{
250 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
251 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
252}
5bd4f169 253\f
f5e87a1d 254#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 255
5bd4f169 256/* Relocation HOWTO's. */
04c9666a 257static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
258
259static reloc_howto_type ppc64_elf_howto_raw[] = {
260 /* This reloc does nothing. */
261 HOWTO (R_PPC64_NONE, /* type */
262 0, /* rightshift */
6346d5ca
AM
263 3, /* size (0 = byte, 1 = short, 2 = long) */
264 0, /* bitsize */
b34976b6 265 FALSE, /* pc_relative */
5bd4f169 266 0, /* bitpos */
f5e87a1d 267 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
268 bfd_elf_generic_reloc, /* special_function */
269 "R_PPC64_NONE", /* name */
b34976b6 270 FALSE, /* partial_inplace */
d006db6c 271 0, /* src_mask */
5bd4f169 272 0, /* dst_mask */
b34976b6 273 FALSE), /* pcrel_offset */
5bd4f169
AM
274
275 /* A standard 32 bit relocation. */
276 HOWTO (R_PPC64_ADDR32, /* type */
277 0, /* rightshift */
278 2, /* size (0 = byte, 1 = short, 2 = long) */
279 32, /* bitsize */
b34976b6 280 FALSE, /* pc_relative */
5bd4f169
AM
281 0, /* bitpos */
282 complain_overflow_bitfield, /* complain_on_overflow */
283 bfd_elf_generic_reloc, /* special_function */
284 "R_PPC64_ADDR32", /* name */
b34976b6 285 FALSE, /* partial_inplace */
5bd4f169
AM
286 0, /* src_mask */
287 0xffffffff, /* dst_mask */
b34976b6 288 FALSE), /* pcrel_offset */
5bd4f169
AM
289
290 /* An absolute 26 bit branch; the lower two bits must be zero.
291 FIXME: we don't check that, we just clear them. */
292 HOWTO (R_PPC64_ADDR24, /* type */
293 0, /* rightshift */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
295 26, /* bitsize */
b34976b6 296 FALSE, /* pc_relative */
5bd4f169
AM
297 0, /* bitpos */
298 complain_overflow_bitfield, /* complain_on_overflow */
299 bfd_elf_generic_reloc, /* special_function */
300 "R_PPC64_ADDR24", /* name */
b34976b6 301 FALSE, /* partial_inplace */
d006db6c 302 0, /* src_mask */
f5e87a1d 303 0x03fffffc, /* dst_mask */
b34976b6 304 FALSE), /* pcrel_offset */
5bd4f169
AM
305
306 /* A standard 16 bit relocation. */
307 HOWTO (R_PPC64_ADDR16, /* type */
308 0, /* rightshift */
309 1, /* size (0 = byte, 1 = short, 2 = long) */
310 16, /* bitsize */
b34976b6 311 FALSE, /* pc_relative */
5bd4f169
AM
312 0, /* bitpos */
313 complain_overflow_bitfield, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_PPC64_ADDR16", /* name */
b34976b6 316 FALSE, /* partial_inplace */
5bd4f169
AM
317 0, /* src_mask */
318 0xffff, /* dst_mask */
b34976b6 319 FALSE), /* pcrel_offset */
5bd4f169
AM
320
321 /* A 16 bit relocation without overflow. */
322 HOWTO (R_PPC64_ADDR16_LO, /* type */
323 0, /* rightshift */
324 1, /* size (0 = byte, 1 = short, 2 = long) */
325 16, /* bitsize */
b34976b6 326 FALSE, /* pc_relative */
5bd4f169
AM
327 0, /* bitpos */
328 complain_overflow_dont,/* complain_on_overflow */
329 bfd_elf_generic_reloc, /* special_function */
330 "R_PPC64_ADDR16_LO", /* name */
b34976b6 331 FALSE, /* partial_inplace */
5bd4f169
AM
332 0, /* src_mask */
333 0xffff, /* dst_mask */
b34976b6 334 FALSE), /* pcrel_offset */
5bd4f169
AM
335
336 /* Bits 16-31 of an address. */
337 HOWTO (R_PPC64_ADDR16_HI, /* type */
338 16, /* rightshift */
339 1, /* size (0 = byte, 1 = short, 2 = long) */
340 16, /* bitsize */
b34976b6 341 FALSE, /* pc_relative */
5bd4f169 342 0, /* bitpos */
f9c6b907 343 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
344 bfd_elf_generic_reloc, /* special_function */
345 "R_PPC64_ADDR16_HI", /* name */
b34976b6 346 FALSE, /* partial_inplace */
5bd4f169
AM
347 0, /* src_mask */
348 0xffff, /* dst_mask */
b34976b6 349 FALSE), /* pcrel_offset */
5bd4f169
AM
350
351 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
352 bits, treated as a signed number, is negative. */
353 HOWTO (R_PPC64_ADDR16_HA, /* type */
354 16, /* rightshift */
355 1, /* size (0 = byte, 1 = short, 2 = long) */
356 16, /* bitsize */
b34976b6 357 FALSE, /* pc_relative */
5bd4f169 358 0, /* bitpos */
f9c6b907 359 complain_overflow_signed, /* complain_on_overflow */
805fc799 360 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 361 "R_PPC64_ADDR16_HA", /* name */
b34976b6 362 FALSE, /* partial_inplace */
5bd4f169
AM
363 0, /* src_mask */
364 0xffff, /* dst_mask */
b34976b6 365 FALSE), /* pcrel_offset */
5bd4f169
AM
366
367 /* An absolute 16 bit branch; the lower two bits must be zero.
368 FIXME: we don't check that, we just clear them. */
369 HOWTO (R_PPC64_ADDR14, /* type */
370 0, /* rightshift */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
372 16, /* bitsize */
b34976b6 373 FALSE, /* pc_relative */
5bd4f169 374 0, /* bitpos */
b80eed39 375 complain_overflow_signed, /* complain_on_overflow */
2441e016 376 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 377 "R_PPC64_ADDR14", /* name */
b34976b6 378 FALSE, /* partial_inplace */
d006db6c 379 0, /* src_mask */
f5e87a1d 380 0x0000fffc, /* dst_mask */
b34976b6 381 FALSE), /* pcrel_offset */
5bd4f169
AM
382
383 /* An absolute 16 bit branch, for which bit 10 should be set to
384 indicate that the branch is expected to be taken. The lower two
385 bits must be zero. */
386 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 16, /* bitsize */
b34976b6 390 FALSE, /* pc_relative */
5bd4f169 391 0, /* bitpos */
b80eed39 392 complain_overflow_signed, /* complain_on_overflow */
805fc799 393 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 394 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 395 FALSE, /* partial_inplace */
d006db6c 396 0, /* src_mask */
f5e87a1d 397 0x0000fffc, /* dst_mask */
b34976b6 398 FALSE), /* pcrel_offset */
5bd4f169
AM
399
400 /* An absolute 16 bit branch, for which bit 10 should be set to
401 indicate that the branch is not expected to be taken. The lower
402 two bits must be zero. */
403 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
404 0, /* rightshift */
405 2, /* size (0 = byte, 1 = short, 2 = long) */
406 16, /* bitsize */
b34976b6 407 FALSE, /* pc_relative */
5bd4f169 408 0, /* bitpos */
b80eed39 409 complain_overflow_signed, /* complain_on_overflow */
805fc799 410 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 411 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 412 FALSE, /* partial_inplace */
d006db6c 413 0, /* src_mask */
f5e87a1d 414 0x0000fffc, /* dst_mask */
b34976b6 415 FALSE), /* pcrel_offset */
5bd4f169
AM
416
417 /* A relative 26 bit branch; the lower two bits must be zero. */
418 HOWTO (R_PPC64_REL24, /* type */
419 0, /* rightshift */
420 2, /* size (0 = byte, 1 = short, 2 = long) */
421 26, /* bitsize */
b34976b6 422 TRUE, /* pc_relative */
5bd4f169
AM
423 0, /* bitpos */
424 complain_overflow_signed, /* complain_on_overflow */
2441e016 425 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 426 "R_PPC64_REL24", /* name */
b34976b6 427 FALSE, /* partial_inplace */
d006db6c 428 0, /* src_mask */
f5e87a1d 429 0x03fffffc, /* dst_mask */
b34976b6 430 TRUE), /* pcrel_offset */
5bd4f169
AM
431
432 /* A relative 16 bit branch; the lower two bits must be zero. */
433 HOWTO (R_PPC64_REL14, /* type */
434 0, /* rightshift */
435 2, /* size (0 = byte, 1 = short, 2 = long) */
436 16, /* bitsize */
b34976b6 437 TRUE, /* pc_relative */
5bd4f169
AM
438 0, /* bitpos */
439 complain_overflow_signed, /* complain_on_overflow */
2441e016 440 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 441 "R_PPC64_REL14", /* name */
b34976b6 442 FALSE, /* partial_inplace */
d006db6c 443 0, /* src_mask */
f5e87a1d 444 0x0000fffc, /* dst_mask */
b34976b6 445 TRUE), /* pcrel_offset */
5bd4f169
AM
446
447 /* A relative 16 bit branch. Bit 10 should be set to indicate that
448 the branch is expected to be taken. The lower two bits must be
449 zero. */
450 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
451 0, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 16, /* bitsize */
b34976b6 454 TRUE, /* pc_relative */
5bd4f169
AM
455 0, /* bitpos */
456 complain_overflow_signed, /* complain_on_overflow */
805fc799 457 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 458 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 459 FALSE, /* partial_inplace */
d006db6c 460 0, /* src_mask */
f5e87a1d 461 0x0000fffc, /* dst_mask */
b34976b6 462 TRUE), /* pcrel_offset */
5bd4f169
AM
463
464 /* A relative 16 bit branch. Bit 10 should be set to indicate that
465 the branch is not expected to be taken. The lower two bits must
466 be zero. */
467 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
468 0, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 16, /* bitsize */
b34976b6 471 TRUE, /* pc_relative */
5bd4f169
AM
472 0, /* bitpos */
473 complain_overflow_signed, /* complain_on_overflow */
805fc799 474 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 475 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 476 FALSE, /* partial_inplace */
d006db6c 477 0, /* src_mask */
f5e87a1d 478 0x0000fffc, /* dst_mask */
b34976b6 479 TRUE), /* pcrel_offset */
5bd4f169
AM
480
481 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
482 symbol. */
483 HOWTO (R_PPC64_GOT16, /* type */
484 0, /* rightshift */
485 1, /* size (0 = byte, 1 = short, 2 = long) */
486 16, /* bitsize */
b34976b6 487 FALSE, /* pc_relative */
5bd4f169
AM
488 0, /* bitpos */
489 complain_overflow_signed, /* complain_on_overflow */
805fc799 490 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 491 "R_PPC64_GOT16", /* name */
b34976b6 492 FALSE, /* partial_inplace */
5bd4f169
AM
493 0, /* src_mask */
494 0xffff, /* dst_mask */
b34976b6 495 FALSE), /* pcrel_offset */
5bd4f169
AM
496
497 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
498 the symbol. */
499 HOWTO (R_PPC64_GOT16_LO, /* type */
500 0, /* rightshift */
501 1, /* size (0 = byte, 1 = short, 2 = long) */
502 16, /* bitsize */
b34976b6 503 FALSE, /* pc_relative */
5bd4f169
AM
504 0, /* bitpos */
505 complain_overflow_dont, /* complain_on_overflow */
805fc799 506 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 507 "R_PPC64_GOT16_LO", /* name */
b34976b6 508 FALSE, /* partial_inplace */
5bd4f169
AM
509 0, /* src_mask */
510 0xffff, /* dst_mask */
b34976b6 511 FALSE), /* pcrel_offset */
5bd4f169
AM
512
513 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
514 the symbol. */
515 HOWTO (R_PPC64_GOT16_HI, /* type */
516 16, /* rightshift */
517 1, /* size (0 = byte, 1 = short, 2 = long) */
518 16, /* bitsize */
b34976b6 519 FALSE, /* pc_relative */
5bd4f169 520 0, /* bitpos */
f9c6b907 521 complain_overflow_signed,/* complain_on_overflow */
805fc799 522 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 523 "R_PPC64_GOT16_HI", /* name */
b34976b6 524 FALSE, /* partial_inplace */
5bd4f169
AM
525 0, /* src_mask */
526 0xffff, /* dst_mask */
b34976b6 527 FALSE), /* pcrel_offset */
5bd4f169
AM
528
529 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
530 the symbol. */
531 HOWTO (R_PPC64_GOT16_HA, /* type */
532 16, /* rightshift */
533 1, /* size (0 = byte, 1 = short, 2 = long) */
534 16, /* bitsize */
b34976b6 535 FALSE, /* pc_relative */
5bd4f169 536 0, /* bitpos */
f9c6b907 537 complain_overflow_signed,/* complain_on_overflow */
805fc799 538 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 539 "R_PPC64_GOT16_HA", /* name */
b34976b6 540 FALSE, /* partial_inplace */
5bd4f169
AM
541 0, /* src_mask */
542 0xffff, /* dst_mask */
b34976b6 543 FALSE), /* pcrel_offset */
5bd4f169
AM
544
545 /* This is used only by the dynamic linker. The symbol should exist
546 both in the object being run and in some shared library. The
547 dynamic linker copies the data addressed by the symbol from the
548 shared library into the object, because the object being
549 run has to have the data at some particular address. */
550 HOWTO (R_PPC64_COPY, /* type */
551 0, /* rightshift */
f5e87a1d
AM
552 0, /* this one is variable size */
553 0, /* bitsize */
b34976b6 554 FALSE, /* pc_relative */
5bd4f169 555 0, /* bitpos */
f5e87a1d
AM
556 complain_overflow_dont, /* complain_on_overflow */
557 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 558 "R_PPC64_COPY", /* name */
b34976b6 559 FALSE, /* partial_inplace */
5bd4f169
AM
560 0, /* src_mask */
561 0, /* dst_mask */
b34976b6 562 FALSE), /* pcrel_offset */
5bd4f169
AM
563
564 /* Like R_PPC64_ADDR64, but used when setting global offset table
565 entries. */
566 HOWTO (R_PPC64_GLOB_DAT, /* type */
567 0, /* rightshift */
568 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
569 64, /* bitsize */
b34976b6 570 FALSE, /* pc_relative */
5bd4f169
AM
571 0, /* bitpos */
572 complain_overflow_dont, /* complain_on_overflow */
805fc799 573 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 574 "R_PPC64_GLOB_DAT", /* name */
b34976b6 575 FALSE, /* partial_inplace */
5bd4f169 576 0, /* src_mask */
f5e87a1d 577 ONES (64), /* dst_mask */
b34976b6 578 FALSE), /* pcrel_offset */
5bd4f169
AM
579
580 /* Created by the link editor. Marks a procedure linkage table
581 entry for a symbol. */
582 HOWTO (R_PPC64_JMP_SLOT, /* type */
583 0, /* rightshift */
584 0, /* size (0 = byte, 1 = short, 2 = long) */
585 0, /* bitsize */
b34976b6 586 FALSE, /* pc_relative */
5bd4f169
AM
587 0, /* bitpos */
588 complain_overflow_dont, /* complain_on_overflow */
805fc799 589 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 590 "R_PPC64_JMP_SLOT", /* name */
b34976b6 591 FALSE, /* partial_inplace */
5bd4f169
AM
592 0, /* src_mask */
593 0, /* dst_mask */
b34976b6 594 FALSE), /* pcrel_offset */
5bd4f169
AM
595
596 /* Used only by the dynamic linker. When the object is run, this
597 doubleword64 is set to the load address of the object, plus the
598 addend. */
599 HOWTO (R_PPC64_RELATIVE, /* type */
600 0, /* rightshift */
601 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
602 64, /* bitsize */
b34976b6 603 FALSE, /* pc_relative */
5bd4f169
AM
604 0, /* bitpos */
605 complain_overflow_dont, /* complain_on_overflow */
606 bfd_elf_generic_reloc, /* special_function */
607 "R_PPC64_RELATIVE", /* name */
b34976b6 608 FALSE, /* partial_inplace */
5bd4f169 609 0, /* src_mask */
f5e87a1d 610 ONES (64), /* dst_mask */
b34976b6 611 FALSE), /* pcrel_offset */
5bd4f169
AM
612
613 /* Like R_PPC64_ADDR32, but may be unaligned. */
614 HOWTO (R_PPC64_UADDR32, /* type */
615 0, /* rightshift */
616 2, /* size (0 = byte, 1 = short, 2 = long) */
617 32, /* bitsize */
b34976b6 618 FALSE, /* pc_relative */
5bd4f169
AM
619 0, /* bitpos */
620 complain_overflow_bitfield, /* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_PPC64_UADDR32", /* name */
b34976b6 623 FALSE, /* partial_inplace */
5bd4f169
AM
624 0, /* src_mask */
625 0xffffffff, /* dst_mask */
b34976b6 626 FALSE), /* pcrel_offset */
5bd4f169
AM
627
628 /* Like R_PPC64_ADDR16, but may be unaligned. */
629 HOWTO (R_PPC64_UADDR16, /* type */
630 0, /* rightshift */
631 1, /* size (0 = byte, 1 = short, 2 = long) */
632 16, /* bitsize */
b34976b6 633 FALSE, /* pc_relative */
5bd4f169
AM
634 0, /* bitpos */
635 complain_overflow_bitfield, /* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_PPC64_UADDR16", /* name */
b34976b6 638 FALSE, /* partial_inplace */
5bd4f169
AM
639 0, /* src_mask */
640 0xffff, /* dst_mask */
b34976b6 641 FALSE), /* pcrel_offset */
5bd4f169
AM
642
643 /* 32-bit PC relative. */
644 HOWTO (R_PPC64_REL32, /* type */
645 0, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 32, /* bitsize */
b34976b6 648 TRUE, /* pc_relative */
5bd4f169 649 0, /* bitpos */
5bd4f169
AM
650 complain_overflow_signed, /* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_PPC64_REL32", /* name */
b34976b6 653 FALSE, /* partial_inplace */
5bd4f169
AM
654 0, /* src_mask */
655 0xffffffff, /* dst_mask */
b34976b6 656 TRUE), /* pcrel_offset */
5bd4f169 657
10ed1bba 658 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
659 HOWTO (R_PPC64_PLT32, /* type */
660 0, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 32, /* bitsize */
b34976b6 663 FALSE, /* pc_relative */
5bd4f169
AM
664 0, /* bitpos */
665 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 666 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 667 "R_PPC64_PLT32", /* name */
b34976b6 668 FALSE, /* partial_inplace */
5bd4f169 669 0, /* src_mask */
f5e87a1d 670 0xffffffff, /* dst_mask */
b34976b6 671 FALSE), /* pcrel_offset */
5bd4f169
AM
672
673 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
674 FIXME: R_PPC64_PLTREL32 not supported. */
675 HOWTO (R_PPC64_PLTREL32, /* type */
676 0, /* rightshift */
677 2, /* size (0 = byte, 1 = short, 2 = long) */
678 32, /* bitsize */
b34976b6 679 TRUE, /* pc_relative */
5bd4f169
AM
680 0, /* bitpos */
681 complain_overflow_signed, /* complain_on_overflow */
682 bfd_elf_generic_reloc, /* special_function */
683 "R_PPC64_PLTREL32", /* name */
b34976b6 684 FALSE, /* partial_inplace */
5bd4f169 685 0, /* src_mask */
f5e87a1d 686 0xffffffff, /* dst_mask */
b34976b6 687 TRUE), /* pcrel_offset */
5bd4f169
AM
688
689 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
690 the symbol. */
691 HOWTO (R_PPC64_PLT16_LO, /* type */
692 0, /* rightshift */
693 1, /* size (0 = byte, 1 = short, 2 = long) */
694 16, /* bitsize */
b34976b6 695 FALSE, /* pc_relative */
5bd4f169
AM
696 0, /* bitpos */
697 complain_overflow_dont, /* complain_on_overflow */
805fc799 698 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 699 "R_PPC64_PLT16_LO", /* name */
b34976b6 700 FALSE, /* partial_inplace */
5bd4f169
AM
701 0, /* src_mask */
702 0xffff, /* dst_mask */
b34976b6 703 FALSE), /* pcrel_offset */
5bd4f169
AM
704
705 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
706 the symbol. */
707 HOWTO (R_PPC64_PLT16_HI, /* type */
708 16, /* rightshift */
709 1, /* size (0 = byte, 1 = short, 2 = long) */
710 16, /* bitsize */
b34976b6 711 FALSE, /* pc_relative */
5bd4f169 712 0, /* bitpos */
f9c6b907 713 complain_overflow_signed, /* complain_on_overflow */
805fc799 714 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 715 "R_PPC64_PLT16_HI", /* name */
b34976b6 716 FALSE, /* partial_inplace */
5bd4f169
AM
717 0, /* src_mask */
718 0xffff, /* dst_mask */
b34976b6 719 FALSE), /* pcrel_offset */
5bd4f169
AM
720
721 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
722 the symbol. */
723 HOWTO (R_PPC64_PLT16_HA, /* type */
724 16, /* rightshift */
725 1, /* size (0 = byte, 1 = short, 2 = long) */
726 16, /* bitsize */
b34976b6 727 FALSE, /* pc_relative */
5bd4f169 728 0, /* bitpos */
f9c6b907 729 complain_overflow_signed, /* complain_on_overflow */
805fc799 730 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 731 "R_PPC64_PLT16_HA", /* name */
b34976b6 732 FALSE, /* partial_inplace */
5bd4f169
AM
733 0, /* src_mask */
734 0xffff, /* dst_mask */
b34976b6 735 FALSE), /* pcrel_offset */
5bd4f169 736
c061c2d8 737 /* 16-bit section relative relocation. */
5bd4f169
AM
738 HOWTO (R_PPC64_SECTOFF, /* type */
739 0, /* rightshift */
c061c2d8
AM
740 1, /* size (0 = byte, 1 = short, 2 = long) */
741 16, /* bitsize */
b34976b6 742 FALSE, /* pc_relative */
5bd4f169 743 0, /* bitpos */
b80eed39 744 complain_overflow_signed, /* complain_on_overflow */
805fc799 745 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 746 "R_PPC64_SECTOFF", /* name */
b34976b6 747 FALSE, /* partial_inplace */
5bd4f169 748 0, /* src_mask */
c061c2d8 749 0xffff, /* dst_mask */
b34976b6 750 FALSE), /* pcrel_offset */
5bd4f169 751
c061c2d8 752 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
753 HOWTO (R_PPC64_SECTOFF_LO, /* type */
754 0, /* rightshift */
755 1, /* size (0 = byte, 1 = short, 2 = long) */
756 16, /* bitsize */
b34976b6 757 FALSE, /* pc_relative */
5bd4f169
AM
758 0, /* bitpos */
759 complain_overflow_dont, /* complain_on_overflow */
805fc799 760 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 761 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 762 FALSE, /* partial_inplace */
5bd4f169
AM
763 0, /* src_mask */
764 0xffff, /* dst_mask */
b34976b6 765 FALSE), /* pcrel_offset */
5bd4f169
AM
766
767 /* 16-bit upper half section relative relocation. */
768 HOWTO (R_PPC64_SECTOFF_HI, /* type */
769 16, /* rightshift */
770 1, /* size (0 = byte, 1 = short, 2 = long) */
771 16, /* bitsize */
b34976b6 772 FALSE, /* pc_relative */
5bd4f169 773 0, /* bitpos */
f9c6b907 774 complain_overflow_signed, /* complain_on_overflow */
805fc799 775 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 776 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 777 FALSE, /* partial_inplace */
5bd4f169
AM
778 0, /* src_mask */
779 0xffff, /* dst_mask */
b34976b6 780 FALSE), /* pcrel_offset */
5bd4f169
AM
781
782 /* 16-bit upper half adjusted section relative relocation. */
783 HOWTO (R_PPC64_SECTOFF_HA, /* type */
784 16, /* rightshift */
785 1, /* size (0 = byte, 1 = short, 2 = long) */
786 16, /* bitsize */
b34976b6 787 FALSE, /* pc_relative */
5bd4f169 788 0, /* bitpos */
f9c6b907 789 complain_overflow_signed, /* complain_on_overflow */
805fc799 790 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 791 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 792 FALSE, /* partial_inplace */
5bd4f169
AM
793 0, /* src_mask */
794 0xffff, /* dst_mask */
b34976b6 795 FALSE), /* pcrel_offset */
5bd4f169 796
04c9666a
AM
797 /* Like R_PPC64_REL24 without touching the two least significant bits. */
798 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
799 2, /* rightshift */
800 2, /* size (0 = byte, 1 = short, 2 = long) */
801 30, /* bitsize */
b34976b6 802 TRUE, /* pc_relative */
5bd4f169
AM
803 0, /* bitpos */
804 complain_overflow_dont, /* complain_on_overflow */
805 bfd_elf_generic_reloc, /* special_function */
04c9666a 806 "R_PPC64_REL30", /* name */
b34976b6 807 FALSE, /* partial_inplace */
d006db6c 808 0, /* src_mask */
5bd4f169 809 0xfffffffc, /* dst_mask */
b34976b6 810 TRUE), /* pcrel_offset */
5bd4f169
AM
811
812 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
813
814 /* A standard 64-bit relocation. */
815 HOWTO (R_PPC64_ADDR64, /* type */
816 0, /* rightshift */
817 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
818 64, /* bitsize */
b34976b6 819 FALSE, /* pc_relative */
5bd4f169
AM
820 0, /* bitpos */
821 complain_overflow_dont, /* complain_on_overflow */
822 bfd_elf_generic_reloc, /* special_function */
823 "R_PPC64_ADDR64", /* name */
b34976b6 824 FALSE, /* partial_inplace */
5bd4f169 825 0, /* src_mask */
f5e87a1d 826 ONES (64), /* dst_mask */
b34976b6 827 FALSE), /* pcrel_offset */
5bd4f169
AM
828
829 /* The bits 32-47 of an address. */
830 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
831 32, /* rightshift */
832 1, /* size (0 = byte, 1 = short, 2 = long) */
833 16, /* bitsize */
b34976b6 834 FALSE, /* pc_relative */
5bd4f169
AM
835 0, /* bitpos */
836 complain_overflow_dont, /* complain_on_overflow */
837 bfd_elf_generic_reloc, /* special_function */
838 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 839 FALSE, /* partial_inplace */
5bd4f169
AM
840 0, /* src_mask */
841 0xffff, /* dst_mask */
b34976b6 842 FALSE), /* pcrel_offset */
5bd4f169
AM
843
844 /* The bits 32-47 of an address, plus 1 if the contents of the low
845 16 bits, treated as a signed number, is negative. */
846 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
847 32, /* rightshift */
848 1, /* size (0 = byte, 1 = short, 2 = long) */
849 16, /* bitsize */
b34976b6 850 FALSE, /* pc_relative */
5bd4f169
AM
851 0, /* bitpos */
852 complain_overflow_dont, /* complain_on_overflow */
805fc799 853 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 854 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 855 FALSE, /* partial_inplace */
5bd4f169
AM
856 0, /* src_mask */
857 0xffff, /* dst_mask */
b34976b6 858 FALSE), /* pcrel_offset */
5bd4f169
AM
859
860 /* The bits 48-63 of an address. */
861 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
862 48, /* rightshift */
863 1, /* size (0 = byte, 1 = short, 2 = long) */
864 16, /* bitsize */
b34976b6 865 FALSE, /* pc_relative */
5bd4f169
AM
866 0, /* bitpos */
867 complain_overflow_dont, /* complain_on_overflow */
868 bfd_elf_generic_reloc, /* special_function */
869 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 870 FALSE, /* partial_inplace */
5bd4f169
AM
871 0, /* src_mask */
872 0xffff, /* dst_mask */
b34976b6 873 FALSE), /* pcrel_offset */
5bd4f169
AM
874
875 /* The bits 48-63 of an address, plus 1 if the contents of the low
876 16 bits, treated as a signed number, is negative. */
877 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
878 48, /* rightshift */
879 1, /* size (0 = byte, 1 = short, 2 = long) */
880 16, /* bitsize */
b34976b6 881 FALSE, /* pc_relative */
5bd4f169
AM
882 0, /* bitpos */
883 complain_overflow_dont, /* complain_on_overflow */
805fc799 884 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 885 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 886 FALSE, /* partial_inplace */
5bd4f169
AM
887 0, /* src_mask */
888 0xffff, /* dst_mask */
b34976b6 889 FALSE), /* pcrel_offset */
5bd4f169
AM
890
891 /* Like ADDR64, but may be unaligned. */
892 HOWTO (R_PPC64_UADDR64, /* type */
893 0, /* rightshift */
894 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
895 64, /* bitsize */
b34976b6 896 FALSE, /* pc_relative */
5bd4f169
AM
897 0, /* bitpos */
898 complain_overflow_dont, /* complain_on_overflow */
899 bfd_elf_generic_reloc, /* special_function */
900 "R_PPC64_UADDR64", /* name */
b34976b6 901 FALSE, /* partial_inplace */
5bd4f169 902 0, /* src_mask */
f5e87a1d 903 ONES (64), /* dst_mask */
b34976b6 904 FALSE), /* pcrel_offset */
5bd4f169
AM
905
906 /* 64-bit relative relocation. */
907 HOWTO (R_PPC64_REL64, /* type */
908 0, /* rightshift */
909 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
910 64, /* bitsize */
b34976b6 911 TRUE, /* pc_relative */
5bd4f169
AM
912 0, /* bitpos */
913 complain_overflow_dont, /* complain_on_overflow */
914 bfd_elf_generic_reloc, /* special_function */
915 "R_PPC64_REL64", /* name */
b34976b6 916 FALSE, /* partial_inplace */
5bd4f169 917 0, /* src_mask */
f5e87a1d 918 ONES (64), /* dst_mask */
b34976b6 919 TRUE), /* pcrel_offset */
5bd4f169 920
cedb70c5 921 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
922 HOWTO (R_PPC64_PLT64, /* type */
923 0, /* rightshift */
924 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
925 64, /* bitsize */
b34976b6 926 FALSE, /* pc_relative */
5bd4f169
AM
927 0, /* bitpos */
928 complain_overflow_dont, /* complain_on_overflow */
805fc799 929 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 930 "R_PPC64_PLT64", /* name */
b34976b6 931 FALSE, /* partial_inplace */
5bd4f169 932 0, /* src_mask */
f5e87a1d 933 ONES (64), /* dst_mask */
b34976b6 934 FALSE), /* pcrel_offset */
5bd4f169
AM
935
936 /* 64-bit PC relative relocation to the symbol's procedure linkage
937 table. */
938 /* FIXME: R_PPC64_PLTREL64 not supported. */
939 HOWTO (R_PPC64_PLTREL64, /* type */
940 0, /* rightshift */
941 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
942 64, /* bitsize */
b34976b6 943 TRUE, /* pc_relative */
5bd4f169
AM
944 0, /* bitpos */
945 complain_overflow_dont, /* complain_on_overflow */
805fc799 946 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 947 "R_PPC64_PLTREL64", /* name */
b34976b6 948 FALSE, /* partial_inplace */
5bd4f169 949 0, /* src_mask */
f5e87a1d 950 ONES (64), /* dst_mask */
b34976b6 951 TRUE), /* pcrel_offset */
5bd4f169
AM
952
953 /* 16 bit TOC-relative relocation. */
954
955 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
956 HOWTO (R_PPC64_TOC16, /* type */
957 0, /* rightshift */
958 1, /* size (0 = byte, 1 = short, 2 = long) */
959 16, /* bitsize */
b34976b6 960 FALSE, /* pc_relative */
5bd4f169
AM
961 0, /* bitpos */
962 complain_overflow_signed, /* complain_on_overflow */
805fc799 963 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 964 "R_PPC64_TOC16", /* name */
b34976b6 965 FALSE, /* partial_inplace */
5bd4f169
AM
966 0, /* src_mask */
967 0xffff, /* dst_mask */
b34976b6 968 FALSE), /* pcrel_offset */
5bd4f169
AM
969
970 /* 16 bit TOC-relative relocation without overflow. */
971
972 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
973 HOWTO (R_PPC64_TOC16_LO, /* type */
974 0, /* rightshift */
975 1, /* size (0 = byte, 1 = short, 2 = long) */
976 16, /* bitsize */
b34976b6 977 FALSE, /* pc_relative */
5bd4f169
AM
978 0, /* bitpos */
979 complain_overflow_dont, /* complain_on_overflow */
805fc799 980 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 981 "R_PPC64_TOC16_LO", /* name */
b34976b6 982 FALSE, /* partial_inplace */
5bd4f169
AM
983 0, /* src_mask */
984 0xffff, /* dst_mask */
b34976b6 985 FALSE), /* pcrel_offset */
5bd4f169
AM
986
987 /* 16 bit TOC-relative relocation, high 16 bits. */
988
989 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
990 HOWTO (R_PPC64_TOC16_HI, /* type */
991 16, /* rightshift */
992 1, /* size (0 = byte, 1 = short, 2 = long) */
993 16, /* bitsize */
b34976b6 994 FALSE, /* pc_relative */
5bd4f169 995 0, /* bitpos */
f9c6b907 996 complain_overflow_signed, /* complain_on_overflow */
805fc799 997 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 998 "R_PPC64_TOC16_HI", /* name */
b34976b6 999 FALSE, /* partial_inplace */
5bd4f169
AM
1000 0, /* src_mask */
1001 0xffff, /* dst_mask */
b34976b6 1002 FALSE), /* pcrel_offset */
5bd4f169
AM
1003
1004 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1005 contents of the low 16 bits, treated as a signed number, is
1006 negative. */
1007
1008 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1009 HOWTO (R_PPC64_TOC16_HA, /* type */
1010 16, /* rightshift */
1011 1, /* size (0 = byte, 1 = short, 2 = long) */
1012 16, /* bitsize */
b34976b6 1013 FALSE, /* pc_relative */
5bd4f169 1014 0, /* bitpos */
f9c6b907 1015 complain_overflow_signed, /* complain_on_overflow */
805fc799 1016 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1017 "R_PPC64_TOC16_HA", /* name */
b34976b6 1018 FALSE, /* partial_inplace */
5bd4f169
AM
1019 0, /* src_mask */
1020 0xffff, /* dst_mask */
b34976b6 1021 FALSE), /* pcrel_offset */
5bd4f169
AM
1022
1023 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1024
1025 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1026 HOWTO (R_PPC64_TOC, /* type */
1027 0, /* rightshift */
1028 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1029 64, /* bitsize */
b34976b6 1030 FALSE, /* pc_relative */
5bd4f169 1031 0, /* bitpos */
b80eed39 1032 complain_overflow_dont, /* complain_on_overflow */
805fc799 1033 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1034 "R_PPC64_TOC", /* name */
b34976b6 1035 FALSE, /* partial_inplace */
5bd4f169 1036 0, /* src_mask */
f5e87a1d 1037 ONES (64), /* dst_mask */
b34976b6 1038 FALSE), /* pcrel_offset */
5bd4f169
AM
1039
1040 /* Like R_PPC64_GOT16, but also informs the link editor that the
1041 value to relocate may (!) refer to a PLT entry which the link
1042 editor (a) may replace with the symbol value. If the link editor
1043 is unable to fully resolve the symbol, it may (b) create a PLT
1044 entry and store the address to the new PLT entry in the GOT.
1045 This permits lazy resolution of function symbols at run time.
1046 The link editor may also skip all of this and just (c) emit a
1047 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1048 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1049 HOWTO (R_PPC64_PLTGOT16, /* type */
1050 0, /* rightshift */
1051 1, /* size (0 = byte, 1 = short, 2 = long) */
1052 16, /* bitsize */
b34976b6 1053 FALSE, /* pc_relative */
5bd4f169
AM
1054 0, /* bitpos */
1055 complain_overflow_signed, /* complain_on_overflow */
805fc799 1056 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1057 "R_PPC64_PLTGOT16", /* name */
1058 FALSE, /* partial_inplace */
1059 0, /* src_mask */
1060 0xffff, /* dst_mask */
1061 FALSE), /* pcrel_offset */
1062
1063 /* Like R_PPC64_PLTGOT16, but without overflow. */
1064 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1065 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1066 0, /* rightshift */
1067 1, /* size (0 = byte, 1 = short, 2 = long) */
1068 16, /* bitsize */
1069 FALSE, /* pc_relative */
1070 0, /* bitpos */
1071 complain_overflow_dont, /* complain_on_overflow */
1072 ppc64_elf_unhandled_reloc, /* special_function */
1073 "R_PPC64_PLTGOT16_LO", /* name */
1074 FALSE, /* partial_inplace */
1075 0, /* src_mask */
1076 0xffff, /* dst_mask */
1077 FALSE), /* pcrel_offset */
1078
1079 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1080 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1081 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1082 16, /* rightshift */
1083 1, /* size (0 = byte, 1 = short, 2 = long) */
1084 16, /* bitsize */
1085 FALSE, /* pc_relative */
1086 0, /* bitpos */
f9c6b907 1087 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1088 ppc64_elf_unhandled_reloc, /* special_function */
1089 "R_PPC64_PLTGOT16_HI", /* name */
1090 FALSE, /* partial_inplace */
1091 0, /* src_mask */
1092 0xffff, /* dst_mask */
1093 FALSE), /* pcrel_offset */
1094
1095 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1096 1 if the contents of the low 16 bits, treated as a signed number,
1097 is negative. */
1098 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1099 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1100 16, /* rightshift */
1101 1, /* size (0 = byte, 1 = short, 2 = long) */
1102 16, /* bitsize */
1103 FALSE, /* pc_relative */
1104 0, /* bitpos */
f9c6b907 1105 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1106 ppc64_elf_unhandled_reloc, /* special_function */
1107 "R_PPC64_PLTGOT16_HA", /* name */
1108 FALSE, /* partial_inplace */
1109 0, /* src_mask */
1110 0xffff, /* dst_mask */
1111 FALSE), /* pcrel_offset */
1112
1113 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1114 HOWTO (R_PPC64_ADDR16_DS, /* type */
1115 0, /* rightshift */
1116 1, /* size (0 = byte, 1 = short, 2 = long) */
1117 16, /* bitsize */
1118 FALSE, /* pc_relative */
1119 0, /* bitpos */
b80eed39 1120 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1121 bfd_elf_generic_reloc, /* special_function */
1122 "R_PPC64_ADDR16_DS", /* name */
1123 FALSE, /* partial_inplace */
1124 0, /* src_mask */
1125 0xfffc, /* dst_mask */
1126 FALSE), /* pcrel_offset */
1127
1128 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1129 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1130 0, /* rightshift */
1131 1, /* size (0 = byte, 1 = short, 2 = long) */
1132 16, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_dont,/* complain_on_overflow */
1136 bfd_elf_generic_reloc, /* special_function */
1137 "R_PPC64_ADDR16_LO_DS",/* name */
1138 FALSE, /* partial_inplace */
1139 0, /* src_mask */
1140 0xfffc, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1142
1143 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1144 HOWTO (R_PPC64_GOT16_DS, /* type */
1145 0, /* rightshift */
1146 1, /* size (0 = byte, 1 = short, 2 = long) */
1147 16, /* bitsize */
1148 FALSE, /* pc_relative */
1149 0, /* bitpos */
1150 complain_overflow_signed, /* complain_on_overflow */
1151 ppc64_elf_unhandled_reloc, /* special_function */
1152 "R_PPC64_GOT16_DS", /* name */
1153 FALSE, /* partial_inplace */
1154 0, /* src_mask */
1155 0xfffc, /* dst_mask */
1156 FALSE), /* pcrel_offset */
1157
1158 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1159 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1160 0, /* rightshift */
1161 1, /* size (0 = byte, 1 = short, 2 = long) */
1162 16, /* bitsize */
1163 FALSE, /* pc_relative */
1164 0, /* bitpos */
1165 complain_overflow_dont, /* complain_on_overflow */
1166 ppc64_elf_unhandled_reloc, /* special_function */
1167 "R_PPC64_GOT16_LO_DS", /* name */
1168 FALSE, /* partial_inplace */
1169 0, /* src_mask */
1170 0xfffc, /* dst_mask */
1171 FALSE), /* pcrel_offset */
1172
1173 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1174 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1175 0, /* rightshift */
1176 1, /* size (0 = byte, 1 = short, 2 = long) */
1177 16, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_dont, /* complain_on_overflow */
1181 ppc64_elf_unhandled_reloc, /* special_function */
1182 "R_PPC64_PLT16_LO_DS", /* name */
1183 FALSE, /* partial_inplace */
1184 0, /* src_mask */
1185 0xfffc, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1187
1188 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1189 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1190 0, /* rightshift */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 16, /* bitsize */
1193 FALSE, /* pc_relative */
1194 0, /* bitpos */
b80eed39 1195 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1196 ppc64_elf_sectoff_reloc, /* special_function */
1197 "R_PPC64_SECTOFF_DS", /* name */
1198 FALSE, /* partial_inplace */
1199 0, /* src_mask */
1200 0xfffc, /* dst_mask */
1201 FALSE), /* pcrel_offset */
1202
1203 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1204 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1205 0, /* rightshift */
1206 1, /* size (0 = byte, 1 = short, 2 = long) */
1207 16, /* bitsize */
1208 FALSE, /* pc_relative */
1209 0, /* bitpos */
1210 complain_overflow_dont, /* complain_on_overflow */
1211 ppc64_elf_sectoff_reloc, /* special_function */
1212 "R_PPC64_SECTOFF_LO_DS",/* name */
1213 FALSE, /* partial_inplace */
1214 0, /* src_mask */
1215 0xfffc, /* dst_mask */
1216 FALSE), /* pcrel_offset */
1217
1218 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1219 HOWTO (R_PPC64_TOC16_DS, /* type */
1220 0, /* rightshift */
1221 1, /* size (0 = byte, 1 = short, 2 = long) */
1222 16, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_signed, /* complain_on_overflow */
1226 ppc64_elf_toc_reloc, /* special_function */
1227 "R_PPC64_TOC16_DS", /* name */
1228 FALSE, /* partial_inplace */
1229 0, /* src_mask */
1230 0xfffc, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1232
1233 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1234 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1235 0, /* rightshift */
1236 1, /* size (0 = byte, 1 = short, 2 = long) */
1237 16, /* bitsize */
1238 FALSE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_dont, /* complain_on_overflow */
1241 ppc64_elf_toc_reloc, /* special_function */
1242 "R_PPC64_TOC16_LO_DS", /* name */
1243 FALSE, /* partial_inplace */
1244 0, /* src_mask */
1245 0xfffc, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1247
1248 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1249 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1250 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1251 0, /* rightshift */
1252 1, /* size (0 = byte, 1 = short, 2 = long) */
1253 16, /* bitsize */
1254 FALSE, /* pc_relative */
1255 0, /* bitpos */
1256 complain_overflow_signed, /* complain_on_overflow */
1257 ppc64_elf_unhandled_reloc, /* special_function */
1258 "R_PPC64_PLTGOT16_DS", /* name */
1259 FALSE, /* partial_inplace */
1260 0, /* src_mask */
1261 0xfffc, /* dst_mask */
1262 FALSE), /* pcrel_offset */
1263
1264 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1265 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1266 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1267 0, /* rightshift */
1268 1, /* size (0 = byte, 1 = short, 2 = long) */
1269 16, /* bitsize */
1270 FALSE, /* pc_relative */
1271 0, /* bitpos */
1272 complain_overflow_dont, /* complain_on_overflow */
1273 ppc64_elf_unhandled_reloc, /* special_function */
1274 "R_PPC64_PLTGOT16_LO_DS",/* name */
1275 FALSE, /* partial_inplace */
1276 0, /* src_mask */
1277 0xfffc, /* dst_mask */
1278 FALSE), /* pcrel_offset */
1279
727fc41e 1280 /* Marker relocs for TLS. */
411e1bfb
AM
1281 HOWTO (R_PPC64_TLS,
1282 0, /* rightshift */
1283 2, /* size (0 = byte, 1 = short, 2 = long) */
1284 32, /* bitsize */
1285 FALSE, /* pc_relative */
1286 0, /* bitpos */
1287 complain_overflow_dont, /* complain_on_overflow */
1288 bfd_elf_generic_reloc, /* special_function */
1289 "R_PPC64_TLS", /* name */
1290 FALSE, /* partial_inplace */
1291 0, /* src_mask */
1292 0, /* dst_mask */
1293 FALSE), /* pcrel_offset */
1294
727fc41e
AM
1295 HOWTO (R_PPC64_TLSGD,
1296 0, /* rightshift */
1297 2, /* size (0 = byte, 1 = short, 2 = long) */
1298 32, /* bitsize */
1299 FALSE, /* pc_relative */
1300 0, /* bitpos */
1301 complain_overflow_dont, /* complain_on_overflow */
1302 bfd_elf_generic_reloc, /* special_function */
1303 "R_PPC64_TLSGD", /* name */
1304 FALSE, /* partial_inplace */
1305 0, /* src_mask */
1306 0, /* dst_mask */
1307 FALSE), /* pcrel_offset */
1308
1309 HOWTO (R_PPC64_TLSLD,
1310 0, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 32, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_dont, /* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_PPC64_TLSLD", /* name */
1318 FALSE, /* partial_inplace */
1319 0, /* src_mask */
1320 0, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1322
3b421ab3
AM
1323 HOWTO (R_PPC64_TOCSAVE,
1324 0, /* rightshift */
1325 2, /* size (0 = byte, 1 = short, 2 = long) */
1326 32, /* bitsize */
1327 FALSE, /* pc_relative */
1328 0, /* bitpos */
1329 complain_overflow_dont, /* complain_on_overflow */
1330 bfd_elf_generic_reloc, /* special_function */
1331 "R_PPC64_TOCSAVE", /* name */
1332 FALSE, /* partial_inplace */
1333 0, /* src_mask */
1334 0, /* dst_mask */
1335 FALSE), /* pcrel_offset */
1336
411e1bfb
AM
1337 /* Computes the load module index of the load module that contains the
1338 definition of its TLS sym. */
1339 HOWTO (R_PPC64_DTPMOD64,
1340 0, /* rightshift */
1341 4, /* size (0 = byte, 1 = short, 2 = long) */
1342 64, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont, /* complain_on_overflow */
1346 ppc64_elf_unhandled_reloc, /* special_function */
1347 "R_PPC64_DTPMOD64", /* name */
1348 FALSE, /* partial_inplace */
1349 0, /* src_mask */
1350 ONES (64), /* dst_mask */
1351 FALSE), /* pcrel_offset */
1352
1353 /* Computes a dtv-relative displacement, the difference between the value
1354 of sym+add and the base address of the thread-local storage block that
1355 contains the definition of sym, minus 0x8000. */
1356 HOWTO (R_PPC64_DTPREL64,
1357 0, /* rightshift */
1358 4, /* size (0 = byte, 1 = short, 2 = long) */
1359 64, /* bitsize */
1360 FALSE, /* pc_relative */
1361 0, /* bitpos */
1362 complain_overflow_dont, /* complain_on_overflow */
1363 ppc64_elf_unhandled_reloc, /* special_function */
1364 "R_PPC64_DTPREL64", /* name */
1365 FALSE, /* partial_inplace */
1366 0, /* src_mask */
1367 ONES (64), /* dst_mask */
1368 FALSE), /* pcrel_offset */
1369
1370 /* A 16 bit dtprel reloc. */
1371 HOWTO (R_PPC64_DTPREL16,
1372 0, /* rightshift */
1373 1, /* size (0 = byte, 1 = short, 2 = long) */
1374 16, /* bitsize */
1375 FALSE, /* pc_relative */
1376 0, /* bitpos */
1377 complain_overflow_signed, /* complain_on_overflow */
1378 ppc64_elf_unhandled_reloc, /* special_function */
1379 "R_PPC64_DTPREL16", /* name */
1380 FALSE, /* partial_inplace */
1381 0, /* src_mask */
1382 0xffff, /* dst_mask */
1383 FALSE), /* pcrel_offset */
1384
1385 /* Like DTPREL16, but no overflow. */
1386 HOWTO (R_PPC64_DTPREL16_LO,
1387 0, /* rightshift */
1388 1, /* size (0 = byte, 1 = short, 2 = long) */
1389 16, /* bitsize */
1390 FALSE, /* pc_relative */
1391 0, /* bitpos */
1392 complain_overflow_dont, /* complain_on_overflow */
1393 ppc64_elf_unhandled_reloc, /* special_function */
1394 "R_PPC64_DTPREL16_LO", /* name */
1395 FALSE, /* partial_inplace */
1396 0, /* src_mask */
1397 0xffff, /* dst_mask */
1398 FALSE), /* pcrel_offset */
1399
1400 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1401 HOWTO (R_PPC64_DTPREL16_HI,
1402 16, /* rightshift */
1403 1, /* size (0 = byte, 1 = short, 2 = long) */
1404 16, /* bitsize */
1405 FALSE, /* pc_relative */
1406 0, /* bitpos */
f9c6b907 1407 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1408 ppc64_elf_unhandled_reloc, /* special_function */
1409 "R_PPC64_DTPREL16_HI", /* name */
1410 FALSE, /* partial_inplace */
1411 0, /* src_mask */
1412 0xffff, /* dst_mask */
1413 FALSE), /* pcrel_offset */
1414
1415 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1416 HOWTO (R_PPC64_DTPREL16_HA,
1417 16, /* rightshift */
1418 1, /* size (0 = byte, 1 = short, 2 = long) */
1419 16, /* bitsize */
1420 FALSE, /* pc_relative */
1421 0, /* bitpos */
f9c6b907 1422 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1423 ppc64_elf_unhandled_reloc, /* special_function */
1424 "R_PPC64_DTPREL16_HA", /* name */
1425 FALSE, /* partial_inplace */
1426 0, /* src_mask */
1427 0xffff, /* dst_mask */
1428 FALSE), /* pcrel_offset */
1429
1430 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1431 HOWTO (R_PPC64_DTPREL16_HIGHER,
1432 32, /* rightshift */
1433 1, /* size (0 = byte, 1 = short, 2 = long) */
1434 16, /* bitsize */
1435 FALSE, /* pc_relative */
1436 0, /* bitpos */
1437 complain_overflow_dont, /* complain_on_overflow */
1438 ppc64_elf_unhandled_reloc, /* special_function */
1439 "R_PPC64_DTPREL16_HIGHER", /* name */
1440 FALSE, /* partial_inplace */
1441 0, /* src_mask */
1442 0xffff, /* dst_mask */
1443 FALSE), /* pcrel_offset */
1444
1445 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1446 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1447 32, /* rightshift */
1448 1, /* size (0 = byte, 1 = short, 2 = long) */
1449 16, /* bitsize */
1450 FALSE, /* pc_relative */
1451 0, /* bitpos */
1452 complain_overflow_dont, /* complain_on_overflow */
1453 ppc64_elf_unhandled_reloc, /* special_function */
1454 "R_PPC64_DTPREL16_HIGHERA", /* name */
1455 FALSE, /* partial_inplace */
1456 0, /* src_mask */
1457 0xffff, /* dst_mask */
1458 FALSE), /* pcrel_offset */
1459
1460 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1461 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1462 48, /* rightshift */
1463 1, /* size (0 = byte, 1 = short, 2 = long) */
1464 16, /* bitsize */
1465 FALSE, /* pc_relative */
1466 0, /* bitpos */
1467 complain_overflow_dont, /* complain_on_overflow */
1468 ppc64_elf_unhandled_reloc, /* special_function */
1469 "R_PPC64_DTPREL16_HIGHEST", /* name */
1470 FALSE, /* partial_inplace */
1471 0, /* src_mask */
1472 0xffff, /* dst_mask */
1473 FALSE), /* pcrel_offset */
1474
1475 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1476 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1477 48, /* rightshift */
1478 1, /* size (0 = byte, 1 = short, 2 = long) */
1479 16, /* bitsize */
1480 FALSE, /* pc_relative */
1481 0, /* bitpos */
1482 complain_overflow_dont, /* complain_on_overflow */
1483 ppc64_elf_unhandled_reloc, /* special_function */
1484 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1485 FALSE, /* partial_inplace */
1486 0, /* src_mask */
1487 0xffff, /* dst_mask */
1488 FALSE), /* pcrel_offset */
1489
1490 /* Like DTPREL16, but for insns with a DS field. */
1491 HOWTO (R_PPC64_DTPREL16_DS,
1492 0, /* rightshift */
1493 1, /* size (0 = byte, 1 = short, 2 = long) */
1494 16, /* bitsize */
1495 FALSE, /* pc_relative */
1496 0, /* bitpos */
1497 complain_overflow_signed, /* complain_on_overflow */
1498 ppc64_elf_unhandled_reloc, /* special_function */
1499 "R_PPC64_DTPREL16_DS", /* name */
1500 FALSE, /* partial_inplace */
1501 0, /* src_mask */
1502 0xfffc, /* dst_mask */
1503 FALSE), /* pcrel_offset */
1504
1505 /* Like DTPREL16_DS, but no overflow. */
1506 HOWTO (R_PPC64_DTPREL16_LO_DS,
1507 0, /* rightshift */
1508 1, /* size (0 = byte, 1 = short, 2 = long) */
1509 16, /* bitsize */
1510 FALSE, /* pc_relative */
1511 0, /* bitpos */
1512 complain_overflow_dont, /* complain_on_overflow */
1513 ppc64_elf_unhandled_reloc, /* special_function */
1514 "R_PPC64_DTPREL16_LO_DS", /* name */
1515 FALSE, /* partial_inplace */
1516 0, /* src_mask */
1517 0xfffc, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519
1520 /* Computes a tp-relative displacement, the difference between the value of
1521 sym+add and the value of the thread pointer (r13). */
1522 HOWTO (R_PPC64_TPREL64,
1523 0, /* rightshift */
1524 4, /* size (0 = byte, 1 = short, 2 = long) */
1525 64, /* bitsize */
1526 FALSE, /* pc_relative */
1527 0, /* bitpos */
1528 complain_overflow_dont, /* complain_on_overflow */
1529 ppc64_elf_unhandled_reloc, /* special_function */
1530 "R_PPC64_TPREL64", /* name */
1531 FALSE, /* partial_inplace */
1532 0, /* src_mask */
1533 ONES (64), /* dst_mask */
1534 FALSE), /* pcrel_offset */
1535
1536 /* A 16 bit tprel reloc. */
1537 HOWTO (R_PPC64_TPREL16,
1538 0, /* rightshift */
1539 1, /* size (0 = byte, 1 = short, 2 = long) */
1540 16, /* bitsize */
1541 FALSE, /* pc_relative */
1542 0, /* bitpos */
1543 complain_overflow_signed, /* complain_on_overflow */
1544 ppc64_elf_unhandled_reloc, /* special_function */
1545 "R_PPC64_TPREL16", /* name */
1546 FALSE, /* partial_inplace */
1547 0, /* src_mask */
1548 0xffff, /* dst_mask */
1549 FALSE), /* pcrel_offset */
1550
1551 /* Like TPREL16, but no overflow. */
1552 HOWTO (R_PPC64_TPREL16_LO,
1553 0, /* rightshift */
1554 1, /* size (0 = byte, 1 = short, 2 = long) */
1555 16, /* bitsize */
1556 FALSE, /* pc_relative */
1557 0, /* bitpos */
1558 complain_overflow_dont, /* complain_on_overflow */
1559 ppc64_elf_unhandled_reloc, /* special_function */
1560 "R_PPC64_TPREL16_LO", /* name */
1561 FALSE, /* partial_inplace */
1562 0, /* src_mask */
1563 0xffff, /* dst_mask */
1564 FALSE), /* pcrel_offset */
1565
1566 /* Like TPREL16_LO, but next higher group of 16 bits. */
1567 HOWTO (R_PPC64_TPREL16_HI,
1568 16, /* rightshift */
1569 1, /* size (0 = byte, 1 = short, 2 = long) */
1570 16, /* bitsize */
1571 FALSE, /* pc_relative */
1572 0, /* bitpos */
f9c6b907 1573 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1574 ppc64_elf_unhandled_reloc, /* special_function */
1575 "R_PPC64_TPREL16_HI", /* name */
1576 FALSE, /* partial_inplace */
1577 0, /* src_mask */
1578 0xffff, /* dst_mask */
1579 FALSE), /* pcrel_offset */
1580
1581 /* Like TPREL16_HI, but adjust for low 16 bits. */
1582 HOWTO (R_PPC64_TPREL16_HA,
1583 16, /* rightshift */
1584 1, /* size (0 = byte, 1 = short, 2 = long) */
1585 16, /* bitsize */
1586 FALSE, /* pc_relative */
1587 0, /* bitpos */
f9c6b907 1588 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1589 ppc64_elf_unhandled_reloc, /* special_function */
1590 "R_PPC64_TPREL16_HA", /* name */
1591 FALSE, /* partial_inplace */
1592 0, /* src_mask */
1593 0xffff, /* dst_mask */
1594 FALSE), /* pcrel_offset */
1595
1596 /* Like TPREL16_HI, but next higher group of 16 bits. */
1597 HOWTO (R_PPC64_TPREL16_HIGHER,
1598 32, /* rightshift */
1599 1, /* size (0 = byte, 1 = short, 2 = long) */
1600 16, /* bitsize */
1601 FALSE, /* pc_relative */
1602 0, /* bitpos */
1603 complain_overflow_dont, /* complain_on_overflow */
1604 ppc64_elf_unhandled_reloc, /* special_function */
1605 "R_PPC64_TPREL16_HIGHER", /* name */
1606 FALSE, /* partial_inplace */
1607 0, /* src_mask */
1608 0xffff, /* dst_mask */
1609 FALSE), /* pcrel_offset */
1610
1611 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1612 HOWTO (R_PPC64_TPREL16_HIGHERA,
1613 32, /* rightshift */
1614 1, /* size (0 = byte, 1 = short, 2 = long) */
1615 16, /* bitsize */
1616 FALSE, /* pc_relative */
1617 0, /* bitpos */
1618 complain_overflow_dont, /* complain_on_overflow */
1619 ppc64_elf_unhandled_reloc, /* special_function */
1620 "R_PPC64_TPREL16_HIGHERA", /* name */
1621 FALSE, /* partial_inplace */
1622 0, /* src_mask */
1623 0xffff, /* dst_mask */
1624 FALSE), /* pcrel_offset */
1625
1626 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1627 HOWTO (R_PPC64_TPREL16_HIGHEST,
1628 48, /* rightshift */
1629 1, /* size (0 = byte, 1 = short, 2 = long) */
1630 16, /* bitsize */
1631 FALSE, /* pc_relative */
1632 0, /* bitpos */
1633 complain_overflow_dont, /* complain_on_overflow */
1634 ppc64_elf_unhandled_reloc, /* special_function */
1635 "R_PPC64_TPREL16_HIGHEST", /* name */
1636 FALSE, /* partial_inplace */
1637 0, /* src_mask */
1638 0xffff, /* dst_mask */
1639 FALSE), /* pcrel_offset */
1640
1641 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1642 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1643 48, /* rightshift */
1644 1, /* size (0 = byte, 1 = short, 2 = long) */
1645 16, /* bitsize */
1646 FALSE, /* pc_relative */
1647 0, /* bitpos */
1648 complain_overflow_dont, /* complain_on_overflow */
1649 ppc64_elf_unhandled_reloc, /* special_function */
1650 "R_PPC64_TPREL16_HIGHESTA", /* name */
1651 FALSE, /* partial_inplace */
1652 0, /* src_mask */
1653 0xffff, /* dst_mask */
1654 FALSE), /* pcrel_offset */
1655
1656 /* Like TPREL16, but for insns with a DS field. */
1657 HOWTO (R_PPC64_TPREL16_DS,
1658 0, /* rightshift */
1659 1, /* size (0 = byte, 1 = short, 2 = long) */
1660 16, /* bitsize */
1661 FALSE, /* pc_relative */
1662 0, /* bitpos */
1663 complain_overflow_signed, /* complain_on_overflow */
1664 ppc64_elf_unhandled_reloc, /* special_function */
1665 "R_PPC64_TPREL16_DS", /* name */
1666 FALSE, /* partial_inplace */
1667 0, /* src_mask */
1668 0xfffc, /* dst_mask */
1669 FALSE), /* pcrel_offset */
1670
1671 /* Like TPREL16_DS, but no overflow. */
1672 HOWTO (R_PPC64_TPREL16_LO_DS,
1673 0, /* rightshift */
1674 1, /* size (0 = byte, 1 = short, 2 = long) */
1675 16, /* bitsize */
1676 FALSE, /* pc_relative */
1677 0, /* bitpos */
1678 complain_overflow_dont, /* complain_on_overflow */
1679 ppc64_elf_unhandled_reloc, /* special_function */
1680 "R_PPC64_TPREL16_LO_DS", /* name */
1681 FALSE, /* partial_inplace */
1682 0, /* src_mask */
1683 0xfffc, /* dst_mask */
1684 FALSE), /* pcrel_offset */
1685
1686 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1687 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1688 to the first entry relative to the TOC base (r2). */
1689 HOWTO (R_PPC64_GOT_TLSGD16,
1690 0, /* rightshift */
1691 1, /* size (0 = byte, 1 = short, 2 = long) */
1692 16, /* bitsize */
1693 FALSE, /* pc_relative */
1694 0, /* bitpos */
1695 complain_overflow_signed, /* complain_on_overflow */
1696 ppc64_elf_unhandled_reloc, /* special_function */
1697 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1698 FALSE, /* partial_inplace */
5bd4f169
AM
1699 0, /* src_mask */
1700 0xffff, /* dst_mask */
b34976b6 1701 FALSE), /* pcrel_offset */
5bd4f169 1702
411e1bfb
AM
1703 /* Like GOT_TLSGD16, but no overflow. */
1704 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1705 0, /* rightshift */
1706 1, /* size (0 = byte, 1 = short, 2 = long) */
1707 16, /* bitsize */
b34976b6 1708 FALSE, /* pc_relative */
5bd4f169
AM
1709 0, /* bitpos */
1710 complain_overflow_dont, /* complain_on_overflow */
805fc799 1711 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1712 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1713 FALSE, /* partial_inplace */
5bd4f169
AM
1714 0, /* src_mask */
1715 0xffff, /* dst_mask */
b34976b6 1716 FALSE), /* pcrel_offset */
5bd4f169 1717
411e1bfb
AM
1718 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1719 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1720 16, /* rightshift */
1721 1, /* size (0 = byte, 1 = short, 2 = long) */
1722 16, /* bitsize */
b34976b6 1723 FALSE, /* pc_relative */
5bd4f169 1724 0, /* bitpos */
f9c6b907 1725 complain_overflow_signed, /* complain_on_overflow */
805fc799 1726 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1727 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1728 FALSE, /* partial_inplace */
5bd4f169
AM
1729 0, /* src_mask */
1730 0xffff, /* dst_mask */
b34976b6 1731 FALSE), /* pcrel_offset */
5bd4f169 1732
411e1bfb
AM
1733 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1734 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1735 16, /* rightshift */
1736 1, /* size (0 = byte, 1 = short, 2 = long) */
1737 16, /* bitsize */
b34976b6 1738 FALSE, /* pc_relative */
5bd4f169 1739 0, /* bitpos */
f9c6b907 1740 complain_overflow_signed, /* complain_on_overflow */
805fc799 1741 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1742 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1743 FALSE, /* partial_inplace */
5bd4f169
AM
1744 0, /* src_mask */
1745 0xffff, /* dst_mask */
b34976b6 1746 FALSE), /* pcrel_offset */
5bd4f169 1747
411e1bfb
AM
1748 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1749 with values (sym+add)@dtpmod and zero, and computes the offset to the
1750 first entry relative to the TOC base (r2). */
1751 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1752 0, /* rightshift */
1753 1, /* size (0 = byte, 1 = short, 2 = long) */
1754 16, /* bitsize */
b34976b6 1755 FALSE, /* pc_relative */
5bd4f169 1756 0, /* bitpos */
411e1bfb
AM
1757 complain_overflow_signed, /* complain_on_overflow */
1758 ppc64_elf_unhandled_reloc, /* special_function */
1759 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1760 FALSE, /* partial_inplace */
d006db6c 1761 0, /* src_mask */
411e1bfb 1762 0xffff, /* dst_mask */
b34976b6 1763 FALSE), /* pcrel_offset */
5bd4f169 1764
411e1bfb
AM
1765 /* Like GOT_TLSLD16, but no overflow. */
1766 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1767 0, /* rightshift */
1768 1, /* size (0 = byte, 1 = short, 2 = long) */
1769 16, /* bitsize */
b34976b6 1770 FALSE, /* pc_relative */
5bd4f169 1771 0, /* bitpos */
411e1bfb
AM
1772 complain_overflow_dont, /* complain_on_overflow */
1773 ppc64_elf_unhandled_reloc, /* special_function */
1774 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1775 FALSE, /* partial_inplace */
d006db6c 1776 0, /* src_mask */
411e1bfb 1777 0xffff, /* dst_mask */
b34976b6 1778 FALSE), /* pcrel_offset */
5bd4f169 1779
411e1bfb
AM
1780 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1781 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1782 16, /* rightshift */
5bd4f169
AM
1783 1, /* size (0 = byte, 1 = short, 2 = long) */
1784 16, /* bitsize */
b34976b6 1785 FALSE, /* pc_relative */
5bd4f169 1786 0, /* bitpos */
f9c6b907 1787 complain_overflow_signed, /* complain_on_overflow */
805fc799 1788 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1789 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1790 FALSE, /* partial_inplace */
d006db6c 1791 0, /* src_mask */
411e1bfb 1792 0xffff, /* dst_mask */
b34976b6 1793 FALSE), /* pcrel_offset */
5bd4f169 1794
411e1bfb
AM
1795 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1796 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1797 16, /* rightshift */
5bd4f169
AM
1798 1, /* size (0 = byte, 1 = short, 2 = long) */
1799 16, /* bitsize */
b34976b6 1800 FALSE, /* pc_relative */
5bd4f169 1801 0, /* bitpos */
f9c6b907 1802 complain_overflow_signed, /* complain_on_overflow */
805fc799 1803 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1804 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1805 FALSE, /* partial_inplace */
d006db6c 1806 0, /* src_mask */
411e1bfb 1807 0xffff, /* dst_mask */
b34976b6 1808 FALSE), /* pcrel_offset */
5bd4f169 1809
411e1bfb
AM
1810 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1811 the offset to the entry relative to the TOC base (r2). */
1812 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1813 0, /* rightshift */
1814 1, /* size (0 = byte, 1 = short, 2 = long) */
1815 16, /* bitsize */
b34976b6 1816 FALSE, /* pc_relative */
5bd4f169 1817 0, /* bitpos */
411e1bfb 1818 complain_overflow_signed, /* complain_on_overflow */
805fc799 1819 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1820 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1821 FALSE, /* partial_inplace */
d006db6c 1822 0, /* src_mask */
5bd4f169 1823 0xfffc, /* dst_mask */
b34976b6 1824 FALSE), /* pcrel_offset */
5bd4f169 1825
411e1bfb
AM
1826 /* Like GOT_DTPREL16_DS, but no overflow. */
1827 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1828 0, /* rightshift */
c061c2d8
AM
1829 1, /* size (0 = byte, 1 = short, 2 = long) */
1830 16, /* bitsize */
b34976b6 1831 FALSE, /* pc_relative */
5bd4f169 1832 0, /* bitpos */
411e1bfb
AM
1833 complain_overflow_dont, /* complain_on_overflow */
1834 ppc64_elf_unhandled_reloc, /* special_function */
1835 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1836 FALSE, /* partial_inplace */
d006db6c 1837 0, /* src_mask */
c061c2d8 1838 0xfffc, /* dst_mask */
b34976b6 1839 FALSE), /* pcrel_offset */
5bd4f169 1840
411e1bfb
AM
1841 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1842 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1843 16, /* rightshift */
5bd4f169
AM
1844 1, /* size (0 = byte, 1 = short, 2 = long) */
1845 16, /* bitsize */
b34976b6 1846 FALSE, /* pc_relative */
5bd4f169 1847 0, /* bitpos */
f9c6b907 1848 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1849 ppc64_elf_unhandled_reloc, /* special_function */
1850 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1851 FALSE, /* partial_inplace */
d006db6c 1852 0, /* src_mask */
411e1bfb 1853 0xffff, /* dst_mask */
b34976b6 1854 FALSE), /* pcrel_offset */
5bd4f169 1855
411e1bfb
AM
1856 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1857 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1858 16, /* rightshift */
1859 1, /* size (0 = byte, 1 = short, 2 = long) */
1860 16, /* bitsize */
1861 FALSE, /* pc_relative */
1862 0, /* bitpos */
f9c6b907 1863 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1864 ppc64_elf_unhandled_reloc, /* special_function */
1865 "R_PPC64_GOT_DTPREL16_HA", /* name */
1866 FALSE, /* partial_inplace */
1867 0, /* src_mask */
1868 0xffff, /* dst_mask */
1869 FALSE), /* pcrel_offset */
1870
1871 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1872 offset to the entry relative to the TOC base (r2). */
1873 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1874 0, /* rightshift */
1875 1, /* size (0 = byte, 1 = short, 2 = long) */
1876 16, /* bitsize */
b34976b6 1877 FALSE, /* pc_relative */
5bd4f169
AM
1878 0, /* bitpos */
1879 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1880 ppc64_elf_unhandled_reloc, /* special_function */
1881 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1882 FALSE, /* partial_inplace */
d006db6c 1883 0, /* src_mask */
ad8e1ba5 1884 0xfffc, /* dst_mask */
b34976b6 1885 FALSE), /* pcrel_offset */
5bd4f169 1886
411e1bfb
AM
1887 /* Like GOT_TPREL16_DS, but no overflow. */
1888 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1889 0, /* rightshift */
1890 1, /* size (0 = byte, 1 = short, 2 = long) */
1891 16, /* bitsize */
b34976b6 1892 FALSE, /* pc_relative */
5bd4f169
AM
1893 0, /* bitpos */
1894 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1895 ppc64_elf_unhandled_reloc, /* special_function */
1896 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1897 FALSE, /* partial_inplace */
d006db6c 1898 0, /* src_mask */
ad8e1ba5 1899 0xfffc, /* dst_mask */
b34976b6 1900 FALSE), /* pcrel_offset */
5bd4f169 1901
411e1bfb
AM
1902 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1903 HOWTO (R_PPC64_GOT_TPREL16_HI,
1904 16, /* rightshift */
5bd4f169
AM
1905 1, /* size (0 = byte, 1 = short, 2 = long) */
1906 16, /* bitsize */
b34976b6 1907 FALSE, /* pc_relative */
5bd4f169 1908 0, /* bitpos */
f9c6b907 1909 complain_overflow_signed, /* complain_on_overflow */
805fc799 1910 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1911 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1912 FALSE, /* partial_inplace */
d006db6c 1913 0, /* src_mask */
411e1bfb 1914 0xffff, /* dst_mask */
b34976b6 1915 FALSE), /* pcrel_offset */
5bd4f169 1916
411e1bfb
AM
1917 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1918 HOWTO (R_PPC64_GOT_TPREL16_HA,
1919 16, /* rightshift */
5bd4f169
AM
1920 1, /* size (0 = byte, 1 = short, 2 = long) */
1921 16, /* bitsize */
b34976b6 1922 FALSE, /* pc_relative */
5bd4f169 1923 0, /* bitpos */
f9c6b907 1924 complain_overflow_signed, /* complain_on_overflow */
805fc799 1925 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1926 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1927 FALSE, /* partial_inplace */
d006db6c 1928 0, /* src_mask */
411e1bfb 1929 0xffff, /* dst_mask */
b34976b6 1930 FALSE), /* pcrel_offset */
5bd4f169 1931
25f23106
AM
1932 HOWTO (R_PPC64_JMP_IREL, /* type */
1933 0, /* rightshift */
1934 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1935 0, /* bitsize */
1936 FALSE, /* pc_relative */
1937 0, /* bitpos */
1938 complain_overflow_dont, /* complain_on_overflow */
1939 ppc64_elf_unhandled_reloc, /* special_function */
1940 "R_PPC64_JMP_IREL", /* name */
1941 FALSE, /* partial_inplace */
1942 0, /* src_mask */
1943 0, /* dst_mask */
1944 FALSE), /* pcrel_offset */
1945
e054468f
AM
1946 HOWTO (R_PPC64_IRELATIVE, /* type */
1947 0, /* rightshift */
1948 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1949 64, /* bitsize */
1950 FALSE, /* pc_relative */
1951 0, /* bitpos */
1952 complain_overflow_dont, /* complain_on_overflow */
1953 bfd_elf_generic_reloc, /* special_function */
1954 "R_PPC64_IRELATIVE", /* name */
1955 FALSE, /* partial_inplace */
1956 0, /* src_mask */
1957 ONES (64), /* dst_mask */
1958 FALSE), /* pcrel_offset */
1959
25f23106
AM
1960 /* A 16 bit relative relocation. */
1961 HOWTO (R_PPC64_REL16, /* type */
1962 0, /* rightshift */
1963 1, /* size (0 = byte, 1 = short, 2 = long) */
1964 16, /* bitsize */
1965 TRUE, /* pc_relative */
1966 0, /* bitpos */
b80eed39 1967 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1968 bfd_elf_generic_reloc, /* special_function */
1969 "R_PPC64_REL16", /* name */
1970 FALSE, /* partial_inplace */
1971 0, /* src_mask */
1972 0xffff, /* dst_mask */
1973 TRUE), /* pcrel_offset */
1974
1975 /* A 16 bit relative relocation without overflow. */
1976 HOWTO (R_PPC64_REL16_LO, /* type */
1977 0, /* rightshift */
1978 1, /* size (0 = byte, 1 = short, 2 = long) */
1979 16, /* bitsize */
1980 TRUE, /* pc_relative */
1981 0, /* bitpos */
1982 complain_overflow_dont,/* complain_on_overflow */
1983 bfd_elf_generic_reloc, /* special_function */
1984 "R_PPC64_REL16_LO", /* name */
1985 FALSE, /* partial_inplace */
1986 0, /* src_mask */
1987 0xffff, /* dst_mask */
1988 TRUE), /* pcrel_offset */
1989
1990 /* The high order 16 bits of a relative address. */
1991 HOWTO (R_PPC64_REL16_HI, /* type */
1992 16, /* rightshift */
1993 1, /* size (0 = byte, 1 = short, 2 = long) */
1994 16, /* bitsize */
1995 TRUE, /* pc_relative */
1996 0, /* bitpos */
f9c6b907 1997 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1998 bfd_elf_generic_reloc, /* special_function */
1999 "R_PPC64_REL16_HI", /* name */
2000 FALSE, /* partial_inplace */
2001 0, /* src_mask */
2002 0xffff, /* dst_mask */
2003 TRUE), /* pcrel_offset */
2004
2005 /* The high order 16 bits of a relative address, plus 1 if the contents of
2006 the low 16 bits, treated as a signed number, is negative. */
2007 HOWTO (R_PPC64_REL16_HA, /* type */
2008 16, /* rightshift */
2009 1, /* size (0 = byte, 1 = short, 2 = long) */
2010 16, /* bitsize */
2011 TRUE, /* pc_relative */
2012 0, /* bitpos */
f9c6b907 2013 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2014 ppc64_elf_ha_reloc, /* special_function */
2015 "R_PPC64_REL16_HA", /* name */
2016 FALSE, /* partial_inplace */
2017 0, /* src_mask */
2018 0xffff, /* dst_mask */
2019 TRUE), /* pcrel_offset */
2020
f9c6b907
AM
2021 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2022 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2023 16, /* rightshift */
2024 1, /* size (0 = byte, 1 = short, 2 = long) */
2025 16, /* bitsize */
2026 FALSE, /* pc_relative */
2027 0, /* bitpos */
2028 complain_overflow_dont, /* complain_on_overflow */
2029 bfd_elf_generic_reloc, /* special_function */
2030 "R_PPC64_ADDR16_HIGH", /* name */
2031 FALSE, /* partial_inplace */
2032 0, /* src_mask */
2033 0xffff, /* dst_mask */
2034 FALSE), /* pcrel_offset */
2035
2036 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2037 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2038 16, /* rightshift */
2039 1, /* size (0 = byte, 1 = short, 2 = long) */
2040 16, /* bitsize */
2041 FALSE, /* pc_relative */
2042 0, /* bitpos */
2043 complain_overflow_dont, /* complain_on_overflow */
2044 ppc64_elf_ha_reloc, /* special_function */
2045 "R_PPC64_ADDR16_HIGHA", /* name */
2046 FALSE, /* partial_inplace */
2047 0, /* src_mask */
2048 0xffff, /* dst_mask */
2049 FALSE), /* pcrel_offset */
2050
2051 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2052 HOWTO (R_PPC64_DTPREL16_HIGH,
2053 16, /* rightshift */
2054 1, /* size (0 = byte, 1 = short, 2 = long) */
2055 16, /* bitsize */
2056 FALSE, /* pc_relative */
2057 0, /* bitpos */
2058 complain_overflow_dont, /* complain_on_overflow */
2059 ppc64_elf_unhandled_reloc, /* special_function */
2060 "R_PPC64_DTPREL16_HIGH", /* name */
2061 FALSE, /* partial_inplace */
2062 0, /* src_mask */
2063 0xffff, /* dst_mask */
2064 FALSE), /* pcrel_offset */
2065
2066 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2067 HOWTO (R_PPC64_DTPREL16_HIGHA,
2068 16, /* rightshift */
2069 1, /* size (0 = byte, 1 = short, 2 = long) */
2070 16, /* bitsize */
2071 FALSE, /* pc_relative */
2072 0, /* bitpos */
2073 complain_overflow_dont, /* complain_on_overflow */
2074 ppc64_elf_unhandled_reloc, /* special_function */
2075 "R_PPC64_DTPREL16_HIGHA", /* name */
2076 FALSE, /* partial_inplace */
2077 0, /* src_mask */
2078 0xffff, /* dst_mask */
2079 FALSE), /* pcrel_offset */
2080
2081 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2082 HOWTO (R_PPC64_TPREL16_HIGH,
2083 16, /* rightshift */
2084 1, /* size (0 = byte, 1 = short, 2 = long) */
2085 16, /* bitsize */
2086 FALSE, /* pc_relative */
2087 0, /* bitpos */
2088 complain_overflow_dont, /* complain_on_overflow */
2089 ppc64_elf_unhandled_reloc, /* special_function */
2090 "R_PPC64_TPREL16_HIGH", /* name */
2091 FALSE, /* partial_inplace */
2092 0, /* src_mask */
2093 0xffff, /* dst_mask */
2094 FALSE), /* pcrel_offset */
2095
2096 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2097 HOWTO (R_PPC64_TPREL16_HIGHA,
2098 16, /* rightshift */
2099 1, /* size (0 = byte, 1 = short, 2 = long) */
2100 16, /* bitsize */
2101 FALSE, /* pc_relative */
2102 0, /* bitpos */
2103 complain_overflow_dont, /* complain_on_overflow */
2104 ppc64_elf_unhandled_reloc, /* special_function */
2105 "R_PPC64_TPREL16_HIGHA", /* name */
2106 FALSE, /* partial_inplace */
2107 0, /* src_mask */
2108 0xffff, /* dst_mask */
2109 FALSE), /* pcrel_offset */
2110
45965137
AM
2111 /* Like ADDR64, but use local entry point of function. */
2112 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2113 0, /* rightshift */
2114 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2115 64, /* bitsize */
2116 FALSE, /* pc_relative */
2117 0, /* bitpos */
2118 complain_overflow_dont, /* complain_on_overflow */
2119 bfd_elf_generic_reloc, /* special_function */
2120 "R_PPC64_ADDR64_LOCAL", /* name */
2121 FALSE, /* partial_inplace */
2122 0, /* src_mask */
2123 ONES (64), /* dst_mask */
2124 FALSE), /* pcrel_offset */
2125
5bd4f169
AM
2126 /* GNU extension to record C++ vtable hierarchy. */
2127 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2128 0, /* rightshift */
2129 0, /* size (0 = byte, 1 = short, 2 = long) */
2130 0, /* bitsize */
b34976b6 2131 FALSE, /* pc_relative */
5bd4f169
AM
2132 0, /* bitpos */
2133 complain_overflow_dont, /* complain_on_overflow */
2134 NULL, /* special_function */
2135 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2136 FALSE, /* partial_inplace */
5bd4f169
AM
2137 0, /* src_mask */
2138 0, /* dst_mask */
b34976b6 2139 FALSE), /* pcrel_offset */
5bd4f169
AM
2140
2141 /* GNU extension to record C++ vtable member usage. */
2142 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2143 0, /* rightshift */
2144 0, /* size (0 = byte, 1 = short, 2 = long) */
2145 0, /* bitsize */
b34976b6 2146 FALSE, /* pc_relative */
5bd4f169
AM
2147 0, /* bitpos */
2148 complain_overflow_dont, /* complain_on_overflow */
2149 NULL, /* special_function */
2150 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2151 FALSE, /* partial_inplace */
5bd4f169
AM
2152 0, /* src_mask */
2153 0, /* dst_mask */
b34976b6 2154 FALSE), /* pcrel_offset */
5bd4f169
AM
2155};
2156
2157\f
2158/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2159 be done. */
2160
2161static void
4ce794b7 2162ppc_howto_init (void)
5bd4f169
AM
2163{
2164 unsigned int i, type;
2165
2166 for (i = 0;
2167 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2168 i++)
2169 {
2170 type = ppc64_elf_howto_raw[i].type;
2171 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2172 / sizeof (ppc64_elf_howto_table[0])));
2173 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2174 }
2175}
2176
2177static reloc_howto_type *
4ce794b7
AM
2178ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2179 bfd_reloc_code_real_type code)
5bd4f169 2180{
411e1bfb 2181 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2182
2183 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2184 /* Initialize howto table if needed. */
2185 ppc_howto_init ();
2186
4ce794b7 2187 switch (code)
5bd4f169
AM
2188 {
2189 default:
4ce794b7 2190 return NULL;
5bd4f169 2191
411e1bfb
AM
2192 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2193 break;
2194 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2195 break;
2196 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2197 break;
2198 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2199 break;
2200 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2201 break;
2202 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2203 break;
f9c6b907
AM
2204 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2205 break;
411e1bfb 2206 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2207 break;
f9c6b907
AM
2208 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2209 break;
411e1bfb 2210 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2211 break;
411e1bfb 2212 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2213 break;
411e1bfb 2214 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2215 break;
411e1bfb 2216 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2217 break;
411e1bfb 2218 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2219 break;
411e1bfb 2220 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2221 break;
411e1bfb 2222 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2223 break;
411e1bfb 2224 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2225 break;
411e1bfb 2226 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2227 break;
411e1bfb 2228 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2229 break;
411e1bfb 2230 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2231 break;
411e1bfb 2232 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2233 break;
411e1bfb 2234 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2235 break;
411e1bfb 2236 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2237 break;
411e1bfb 2238 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2239 break;
411e1bfb 2240 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2241 break;
411e1bfb 2242 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2243 break;
411e1bfb 2244 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2245 break;
411e1bfb 2246 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2247 break;
411e1bfb 2248 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2249 break;
411e1bfb 2250 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2251 break;
411e1bfb 2252 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2253 break;
411e1bfb 2254 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2255 break;
411e1bfb 2256 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2257 break;
411e1bfb 2258 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2259 break;
411e1bfb 2260 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2261 break;
411e1bfb 2262 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2263 break;
411e1bfb 2264 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2265 break;
411e1bfb 2266 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2267 break;
411e1bfb 2268 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2269 break;
411e1bfb 2270 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2271 break;
411e1bfb 2272 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2273 break;
411e1bfb 2274 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2275 break;
411e1bfb 2276 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2277 break;
411e1bfb 2278 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2279 break;
411e1bfb 2280 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2281 break;
411e1bfb 2282 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2283 break;
411e1bfb 2284 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2285 break;
411e1bfb 2286 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2287 break;
411e1bfb 2288 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2289 break;
411e1bfb 2290 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2291 break;
411e1bfb 2292 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2293 break;
411e1bfb 2294 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2295 break;
411e1bfb 2296 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2297 break;
411e1bfb 2298 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2299 break;
411e1bfb 2300 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2301 break;
411e1bfb 2302 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2303 break;
411e1bfb 2304 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2305 break;
411e1bfb 2306 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2307 break;
411e1bfb 2308 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2309 break;
411e1bfb 2310 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2311 break;
411e1bfb 2312 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2313 break;
411e1bfb 2314 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2315 break;
727fc41e
AM
2316 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2317 break;
2318 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2319 break;
411e1bfb 2320 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2321 break;
411e1bfb 2322 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2323 break;
411e1bfb 2324 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2325 break;
411e1bfb 2326 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2327 break;
f9c6b907
AM
2328 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2329 break;
411e1bfb 2330 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2331 break;
f9c6b907
AM
2332 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2333 break;
411e1bfb 2334 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2335 break;
411e1bfb
AM
2336 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2337 break;
2338 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2339 break;
2340 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2341 break;
f9c6b907
AM
2342 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2343 break;
411e1bfb
AM
2344 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2345 break;
f9c6b907
AM
2346 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2347 break;
411e1bfb
AM
2348 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2349 break;
2350 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2351 break;
2352 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2353 break;
2354 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2355 break;
2356 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2357 break;
2358 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2359 break;
2360 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2361 break;
2362 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2363 break;
2364 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2365 break;
2366 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2367 break;
2368 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2369 break;
2370 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2371 break;
2372 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2373 break;
2374 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2375 break;
2376 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2377 break;
2378 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2379 break;
2380 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2381 break;
2382 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2383 break;
2384 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2385 break;
2386 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2387 break;
2388 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2389 break;
2390 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2391 break;
2392 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2393 break;
2394 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2395 break;
2396 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2397 break;
2398 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2399 break;
2400 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2401 break;
2402 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2403 break;
2404 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2405 break;
25f23106
AM
2406 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2407 break;
2408 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2409 break;
2410 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2411 break;
2412 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2413 break;
45965137
AM
2414 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2415 break;
411e1bfb
AM
2416 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2417 break;
2418 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2419 break;
2420 }
2421
4ce794b7 2422 return ppc64_elf_howto_table[r];
5bd4f169
AM
2423};
2424
157090f7
AM
2425static reloc_howto_type *
2426ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2427 const char *r_name)
2428{
2429 unsigned int i;
2430
2431 for (i = 0;
2432 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2433 i++)
2434 if (ppc64_elf_howto_raw[i].name != NULL
2435 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2436 return &ppc64_elf_howto_raw[i];
2437
2438 return NULL;
2439}
2440
5bd4f169
AM
2441/* Set the howto pointer for a PowerPC ELF reloc. */
2442
2443static void
4ce794b7
AM
2444ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2445 Elf_Internal_Rela *dst)
5bd4f169 2446{
65f38f15
AM
2447 unsigned int type;
2448
ef60b7ff 2449 /* Initialize howto table if needed. */
5bd4f169 2450 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2451 ppc_howto_init ();
2452
65f38f15 2453 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2454 if (type >= (sizeof (ppc64_elf_howto_table)
2455 / sizeof (ppc64_elf_howto_table[0])))
2456 {
2457 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2458 abfd, (int) type);
2459 type = R_PPC64_NONE;
d0fb9a8d 2460 }
65f38f15 2461 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2462}
2463
04c9666a 2464/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2465
2466static bfd_reloc_status_type
4ce794b7
AM
2467ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2468 void *data, asection *input_section,
2469 bfd *output_bfd, char **error_message)
5bd4f169 2470{
805fc799
AM
2471 /* If this is a relocatable link (output_bfd test tells us), just
2472 call the generic function. Any adjustment will be done at final
2473 link time. */
2474 if (output_bfd != NULL)
cedb70c5 2475 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2476 input_section, output_bfd, error_message);
2477
2478 /* Adjust the addend for sign extension of the low 16 bits.
2479 We won't actually be using the low 16 bits, so trashing them
2480 doesn't matter. */
2481 reloc_entry->addend += 0x8000;
2482 return bfd_reloc_continue;
2483}
5bd4f169 2484
2441e016
AM
2485static bfd_reloc_status_type
2486ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487 void *data, asection *input_section,
2488 bfd *output_bfd, char **error_message)
2489{
2490 if (output_bfd != NULL)
2491 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2492 input_section, output_bfd, error_message);
2493
699733f6
AM
2494 if (strcmp (symbol->section->name, ".opd") == 0
2495 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2496 {
2497 bfd_vma dest = opd_entry_value (symbol->section,
2498 symbol->value + reloc_entry->addend,
aef36ac1 2499 NULL, NULL, FALSE);
2441e016
AM
2500 if (dest != (bfd_vma) -1)
2501 reloc_entry->addend = dest - (symbol->value
2502 + symbol->section->output_section->vma
2503 + symbol->section->output_offset);
2504 }
810d4e75
AM
2505 else
2506 {
2507 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2508
2509 if (symbol->section->owner != abfd
2510 && abiversion (symbol->section->owner) >= 2)
2511 {
2512 unsigned int i;
2513
2514 for (i = 0; i < symbol->section->owner->symcount; ++i)
2515 {
2516 asymbol *symdef = symbol->section->owner->outsymbols[i];
2517
2518 if (strcmp (symdef->name, symbol->name) == 0)
2519 {
2520 elfsym = (elf_symbol_type *) symdef;
2521 break;
2522 }
2523 }
2524 }
2525 reloc_entry->addend
2526 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2527 }
2441e016
AM
2528 return bfd_reloc_continue;
2529}
2530
805fc799 2531static bfd_reloc_status_type
4ce794b7
AM
2532ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2533 void *data, asection *input_section,
2534 bfd *output_bfd, char **error_message)
805fc799
AM
2535{
2536 long insn;
04c9666a 2537 enum elf_ppc64_reloc_type r_type;
805fc799 2538 bfd_size_type octets;
794e51c0
AM
2539 /* Assume 'at' branch hints. */
2540 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2541
2542 /* If this is a relocatable link (output_bfd test tells us), just
2543 call the generic function. Any adjustment will be done at final
2544 link time. */
5bd4f169 2545 if (output_bfd != NULL)
cedb70c5 2546 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2547 input_section, output_bfd, error_message);
2548
2549 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2550 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2551 insn &= ~(0x01 << 21);
4ce794b7 2552 r_type = reloc_entry->howto->type;
805fc799
AM
2553 if (r_type == R_PPC64_ADDR14_BRTAKEN
2554 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2555 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2556
794e51c0 2557 if (is_isa_v2)
5bd4f169 2558 {
805fc799
AM
2559 /* Set 'a' bit. This is 0b00010 in BO field for branch
2560 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2561 for branch on CTR insns (BO == 1a00t or 1a01t). */
2562 if ((insn & (0x14 << 21)) == (0x04 << 21))
2563 insn |= 0x02 << 21;
2564 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2565 insn |= 0x08 << 21;
2566 else
2441e016 2567 goto out;
5bd4f169 2568 }
805fc799
AM
2569 else
2570 {
2571 bfd_vma target = 0;
2572 bfd_vma from;
5bd4f169 2573
805fc799
AM
2574 if (!bfd_is_com_section (symbol->section))
2575 target = symbol->value;
2576 target += symbol->section->output_section->vma;
2577 target += symbol->section->output_offset;
2578 target += reloc_entry->addend;
5bd4f169 2579
805fc799
AM
2580 from = (reloc_entry->address
2581 + input_section->output_offset
2582 + input_section->output_section->vma);
5bd4f169 2583
805fc799
AM
2584 /* Invert 'y' bit if not the default. */
2585 if ((bfd_signed_vma) (target - from) < 0)
2586 insn ^= 0x01 << 21;
2587 }
4ce794b7 2588 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2589 out:
2590 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2591 input_section, output_bfd, error_message);
805fc799 2592}
5bd4f169 2593
805fc799 2594static bfd_reloc_status_type
4ce794b7
AM
2595ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2596 void *data, asection *input_section,
2597 bfd *output_bfd, char **error_message)
805fc799
AM
2598{
2599 /* If this is a relocatable link (output_bfd test tells us), just
2600 call the generic function. Any adjustment will be done at final
2601 link time. */
2602 if (output_bfd != NULL)
cedb70c5 2603 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2604 input_section, output_bfd, error_message);
5bd4f169 2605
805fc799
AM
2606 /* Subtract the symbol section base address. */
2607 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2608 return bfd_reloc_continue;
2609}
2610
805fc799 2611static bfd_reloc_status_type
4ce794b7
AM
2612ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2613 void *data, asection *input_section,
2614 bfd *output_bfd, char **error_message)
805fc799
AM
2615{
2616 /* If this is a relocatable link (output_bfd test tells us), just
2617 call the generic function. Any adjustment will be done at final
2618 link time. */
2619 if (output_bfd != NULL)
cedb70c5 2620 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2621 input_section, output_bfd, error_message);
2622
2623 /* Subtract the symbol section base address. */
2624 reloc_entry->addend -= symbol->section->output_section->vma;
2625
2626 /* Adjust the addend for sign extension of the low 16 bits. */
2627 reloc_entry->addend += 0x8000;
2628 return bfd_reloc_continue;
2629}
2630
2631static bfd_reloc_status_type
4ce794b7
AM
2632ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2633 void *data, asection *input_section,
2634 bfd *output_bfd, char **error_message)
805fc799
AM
2635{
2636 bfd_vma TOCstart;
2637
2638 /* If this is a relocatable link (output_bfd test tells us), just
2639 call the generic function. Any adjustment will be done at final
2640 link time. */
2641 if (output_bfd != NULL)
cedb70c5 2642 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2643 input_section, output_bfd, error_message);
2644
2645 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2646 if (TOCstart == 0)
1c865ab2 2647 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2648
2649 /* Subtract the TOC base address. */
2650 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2651 return bfd_reloc_continue;
2652}
2653
2654static bfd_reloc_status_type
4ce794b7
AM
2655ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2656 void *data, asection *input_section,
2657 bfd *output_bfd, char **error_message)
805fc799
AM
2658{
2659 bfd_vma TOCstart;
2660
2661 /* If this is a relocatable link (output_bfd test tells us), just
2662 call the generic function. Any adjustment will be done at final
2663 link time. */
2664 if (output_bfd != NULL)
cedb70c5 2665 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2666 input_section, output_bfd, error_message);
2667
2668 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2669 if (TOCstart == 0)
1c865ab2 2670 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2671
2672 /* Subtract the TOC base address. */
2673 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2674
2675 /* Adjust the addend for sign extension of the low 16 bits. */
2676 reloc_entry->addend += 0x8000;
2677 return bfd_reloc_continue;
2678}
2679
2680static bfd_reloc_status_type
4ce794b7
AM
2681ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2682 void *data, asection *input_section,
2683 bfd *output_bfd, char **error_message)
805fc799
AM
2684{
2685 bfd_vma TOCstart;
2686 bfd_size_type octets;
2687
2688 /* If this is a relocatable link (output_bfd test tells us), just
2689 call the generic function. Any adjustment will be done at final
2690 link time. */
2691 if (output_bfd != NULL)
cedb70c5 2692 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2693 input_section, output_bfd, error_message);
2694
2695 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2696 if (TOCstart == 0)
1c865ab2 2697 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2698
2699 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2700 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2701 return bfd_reloc_ok;
2702}
2703
2704static bfd_reloc_status_type
4ce794b7
AM
2705ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2706 void *data, asection *input_section,
2707 bfd *output_bfd, char **error_message)
805fc799
AM
2708{
2709 /* If this is a relocatable link (output_bfd test tells us), just
2710 call the generic function. Any adjustment will be done at final
2711 link time. */
2712 if (output_bfd != NULL)
cedb70c5 2713 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2714 input_section, output_bfd, error_message);
2715
2716 if (error_message != NULL)
2717 {
2718 static char buf[60];
2719 sprintf (buf, "generic linker can't handle %s",
2720 reloc_entry->howto->name);
2721 *error_message = buf;
2722 }
2723 return bfd_reloc_dangerous;
2724}
2725
927be08e
AM
2726/* Track GOT entries needed for a given symbol. We might need more
2727 than one got entry per symbol. */
2728struct got_entry
2729{
2730 struct got_entry *next;
2731
2732 /* The symbol addend that we'll be placing in the GOT. */
2733 bfd_vma addend;
2734
2735 /* Unlike other ELF targets, we use separate GOT entries for the same
2736 symbol referenced from different input files. This is to support
2737 automatic multiple TOC/GOT sections, where the TOC base can vary
2738 from one input file to another. After partitioning into TOC groups
2739 we merge entries within the group.
2740
2741 Point to the BFD owning this GOT entry. */
2742 bfd *owner;
2743
2744 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2745 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2746 unsigned char tls_type;
927be08e
AM
2747
2748 /* Non-zero if got.ent points to real entry. */
f961d9dd 2749 unsigned char is_indirect;
927be08e
AM
2750
2751 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2752 union
2753 {
2754 bfd_signed_vma refcount;
2755 bfd_vma offset;
2756 struct got_entry *ent;
2757 } got;
2758};
2759
2760/* The same for PLT. */
2761struct plt_entry
2762{
2763 struct plt_entry *next;
2764
2765 bfd_vma addend;
2766
2767 union
2768 {
2769 bfd_signed_vma refcount;
2770 bfd_vma offset;
2771 } plt;
2772};
2773
e717da7e
AM
2774struct ppc64_elf_obj_tdata
2775{
2776 struct elf_obj_tdata elf;
2777
2778 /* Shortcuts to dynamic linker sections. */
2779 asection *got;
2780 asection *relgot;
2781
b3fac117
AM
2782 /* Used during garbage collection. We attach global symbols defined
2783 on removed .opd entries to this section so that the sym is removed. */
2784 asection *deleted_section;
81688140 2785
927be08e 2786 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2787 sections means we potentially need one of these for each input bfd. */
927be08e 2788 struct got_entry tlsld_got;
8860955f 2789
729eabd5
AM
2790 union {
2791 /* A copy of relocs before they are modified for --emit-relocs. */
2792 Elf_Internal_Rela *relocs;
2793
2794 /* Section contents. */
2795 bfd_byte *contents;
2796 } opd;
d77c8a4b
AM
2797
2798 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2799 the reloc to be in the range -32768 to 32767. */
98528052
AM
2800 unsigned int has_small_toc_reloc : 1;
2801
560c8763
AM
2802 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2803 instruction not one we handle. */
2804 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2805};
2806
2807#define ppc64_elf_tdata(bfd) \
2808 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2809
2810#define ppc64_tlsld_got(bfd) \
2811 (&ppc64_elf_tdata (bfd)->tlsld_got)
2812
0c8d6e5c
AM
2813#define is_ppc64_elf(bfd) \
2814 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2815 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2816
e717da7e
AM
2817/* Override the generic function because we store some extras. */
2818
2819static bfd_boolean
2820ppc64_elf_mkobject (bfd *abfd)
2821{
0ffa91dd 2822 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2823 PPC64_ELF_DATA);
e717da7e
AM
2824}
2825
feee612b
AM
2826/* Fix bad default arch selected for a 64 bit input bfd when the
2827 default is 32 bit. */
2828
b34976b6 2829static bfd_boolean
4ce794b7 2830ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2831{
2832 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2833 {
2834 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2835
2836 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2837 {
2838 /* Relies on arch after 32 bit default being 64 bit default. */
2839 abfd->arch_info = abfd->arch_info->next;
2840 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2841 }
2842 }
b34976b6 2843 return TRUE;
feee612b
AM
2844}
2845
d37c89e5
AM
2846/* Support for core dump NOTE sections. */
2847
2848static bfd_boolean
2849ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2850{
eea6121a 2851 size_t offset, size;
d37c89e5
AM
2852
2853 if (note->descsz != 504)
2854 return FALSE;
2855
2856 /* pr_cursig */
228e534f 2857 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2858
2859 /* pr_pid */
228e534f 2860 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2861
2862 /* pr_reg */
2863 offset = 112;
eea6121a 2864 size = 384;
d37c89e5
AM
2865
2866 /* Make a ".reg/999" section. */
2867 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2868 size, note->descpos + offset);
d37c89e5
AM
2869}
2870
2871static bfd_boolean
2872ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2873{
2874 if (note->descsz != 136)
2875 return FALSE;
2876
228e534f 2877 elf_tdata (abfd)->core->pid
bc989cdc 2878 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2879 elf_tdata (abfd)->core->program
d37c89e5 2880 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2881 elf_tdata (abfd)->core->command
d37c89e5
AM
2882 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2883
2884 return TRUE;
2885}
2886
183e98be
AM
2887static char *
2888ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2889 ...)
2890{
2891 switch (note_type)
2892 {
2893 default:
2894 return NULL;
2895
2896 case NT_PRPSINFO:
2897 {
2898 char data[136];
2899 va_list ap;
2900
2901 va_start (ap, note_type);
75cd47ed 2902 memset (data, 0, sizeof (data));
183e98be
AM
2903 strncpy (data + 40, va_arg (ap, const char *), 16);
2904 strncpy (data + 56, va_arg (ap, const char *), 80);
2905 va_end (ap);
2906 return elfcore_write_note (abfd, buf, bufsiz,
2907 "CORE", note_type, data, sizeof (data));
2908 }
2909
2910 case NT_PRSTATUS:
2911 {
2912 char data[504];
2913 va_list ap;
2914 long pid;
2915 int cursig;
2916 const void *greg;
2917
2918 va_start (ap, note_type);
2919 memset (data, 0, 112);
2920 pid = va_arg (ap, long);
2921 bfd_put_32 (abfd, pid, data + 32);
2922 cursig = va_arg (ap, int);
2923 bfd_put_16 (abfd, cursig, data + 12);
2924 greg = va_arg (ap, const void *);
2925 memcpy (data + 112, greg, 384);
2926 memset (data + 496, 0, 8);
2927 va_end (ap);
2928 return elfcore_write_note (abfd, buf, bufsiz,
2929 "CORE", note_type, data, sizeof (data));
2930 }
2931 }
2932}
2933
5d35169e
AM
2934/* Add extra PPC sections. */
2935
b35d266b 2936static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2937{
0112cd26
NC
2938 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2939 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2940 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2941 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2942 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2943 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2944 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2945};
2946
7c8fe5c4
AM
2947enum _ppc64_sec_type {
2948 sec_normal = 0,
2949 sec_opd = 1,
2950 sec_toc = 2
2951};
2952
f0abc2a1
AM
2953struct _ppc64_elf_section_data
2954{
2955 struct bfd_elf_section_data elf;
411e1bfb 2956
f0abc2a1
AM
2957 union
2958 {
51aecdc5
AM
2959 /* An array with one entry for each opd function descriptor,
2960 and some spares since opd entries may be either 16 or 24 bytes. */
2961#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
2962 struct _opd_sec_data
2963 {
2964 /* Points to the function code section for local opd entries. */
2965 asection **func_sec;
2966
2967 /* After editing .opd, adjust references to opd local syms. */
2968 long *adjust;
2969 } opd;
7c8fe5c4 2970
3a71aa26
AM
2971 /* An array for toc sections, indexed by offset/8. */
2972 struct _toc_sec_data
2973 {
2974 /* Specifies the relocation symbol index used at a given toc offset. */
2975 unsigned *symndx;
2976
2977 /* And the relocation addend. */
2978 bfd_vma *add;
2979 } toc;
7c8fe5c4
AM
2980 } u;
2981
2982 enum _ppc64_sec_type sec_type:2;
411e1bfb 2983
7c8fe5c4
AM
2984 /* Flag set when small branches are detected. Used to
2985 select suitable defaults for the stub group size. */
2986 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2987};
2988
2989#define ppc64_elf_section_data(sec) \
411e1bfb 2990 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2991
2992static bfd_boolean
4ce794b7 2993ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2994{
f592407e
AM
2995 if (!sec->used_by_bfd)
2996 {
2997 struct _ppc64_elf_section_data *sdata;
2998 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2999
f592407e
AM
3000 sdata = bfd_zalloc (abfd, amt);
3001 if (sdata == NULL)
3002 return FALSE;
3003 sec->used_by_bfd = sdata;
3004 }
f0abc2a1
AM
3005
3006 return _bfd_elf_new_section_hook (abfd, sec);
3007}
4025353c 3008
74f0fb50 3009static struct _opd_sec_data *
4025353c
AM
3010get_opd_info (asection * sec)
3011{
3012 if (sec != NULL
3013 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3014 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3015 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3016 return NULL;
3017}
90e3cdf2
JJ
3018\f
3019/* Parameters for the qsort hook. */
90e3cdf2
JJ
3020static bfd_boolean synthetic_relocatable;
3021
699733f6 3022/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3023
3024static int
3025compare_symbols (const void *ap, const void *bp)
3026{
3027 const asymbol *a = * (const asymbol **) ap;
3028 const asymbol *b = * (const asymbol **) bp;
3029
699733f6
AM
3030 /* Section symbols first. */
3031 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3032 return -1;
699733f6 3033 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3034 return 1;
3035
699733f6 3036 /* then .opd symbols. */
ffcfec52
AM
3037 if (strcmp (a->section->name, ".opd") == 0
3038 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 3039 return -1;
ffcfec52
AM
3040 if (strcmp (a->section->name, ".opd") != 0
3041 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
3042 return 1;
3043
699733f6 3044 /* then other code symbols. */
90e3cdf2
JJ
3045 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3046 == (SEC_CODE | SEC_ALLOC)
3047 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3048 != (SEC_CODE | SEC_ALLOC))
3049 return -1;
3050
3051 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3052 != (SEC_CODE | SEC_ALLOC)
3053 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3054 == (SEC_CODE | SEC_ALLOC))
3055 return 1;
3056
3057 if (synthetic_relocatable)
3058 {
3059 if (a->section->id < b->section->id)
3060 return -1;
3061
3062 if (a->section->id > b->section->id)
3063 return 1;
3064 }
3065
3066 if (a->value + a->section->vma < b->value + b->section->vma)
3067 return -1;
3068
3069 if (a->value + a->section->vma > b->value + b->section->vma)
3070 return 1;
3071
4d35a0aa
AM
3072 /* For syms with the same value, prefer strong dynamic global function
3073 syms over other syms. */
3074 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3075 return -1;
3076
3077 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3078 return 1;
3079
3080 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3081 return -1;
3082
3083 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3084 return 1;
3085
3086 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3087 return -1;
3088
3089 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3090 return 1;
3091
3092 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3093 return -1;
3094
3095 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3096 return 1;
3097
90e3cdf2
JJ
3098 return 0;
3099}
3100
699733f6 3101/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3102
699733f6
AM
3103static asymbol *
3104sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 3105{
699733f6 3106 long mid;
90e3cdf2 3107
699733f6
AM
3108 if (id == -1)
3109 {
3110 while (lo < hi)
3111 {
3112 mid = (lo + hi) >> 1;
3113 if (syms[mid]->value + syms[mid]->section->vma < value)
3114 lo = mid + 1;
3115 else if (syms[mid]->value + syms[mid]->section->vma > value)
3116 hi = mid;
3117 else
3118 return syms[mid];
3119 }
3120 }
3121 else
3122 {
3123 while (lo < hi)
3124 {
3125 mid = (lo + hi) >> 1;
3126 if (syms[mid]->section->id < id)
3127 lo = mid + 1;
3128 else if (syms[mid]->section->id > id)
3129 hi = mid;
3130 else if (syms[mid]->value < value)
3131 lo = mid + 1;
3132 else if (syms[mid]->value > value)
3133 hi = mid;
3134 else
3135 return syms[mid];
3136 }
3137 }
3138 return NULL;
90e3cdf2
JJ
3139}
3140
468392fb
AM
3141static bfd_boolean
3142section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3143{
3144 bfd_vma vma = *(bfd_vma *) ptr;
3145 return ((section->flags & SEC_ALLOC) != 0
3146 && section->vma <= vma
3147 && vma < section->vma + section->size);
3148}
3149
699733f6 3150/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3151 entry syms. Also generate @plt symbols for the glink branch table.
3152 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3153
3154static long
a7535cf3
AM
3155ppc64_elf_get_synthetic_symtab (bfd *abfd,
3156 long static_count, asymbol **static_syms,
3157 long dyn_count, asymbol **dyn_syms,
c9727e01 3158 asymbol **ret)
90e3cdf2
JJ
3159{
3160 asymbol *s;
699733f6
AM
3161 long i;
3162 long count;
90e3cdf2 3163 char *names;
a7535cf3 3164 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3165 asection *opd = NULL;
90e3cdf2 3166 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3167 asymbol **syms;
ee67d69a 3168 int abi = abiversion (abfd);
90e3cdf2
JJ
3169
3170 *ret = NULL;
3171
ee67d69a
AM
3172 if (abi < 2)
3173 {
3174 opd = bfd_get_section_by_name (abfd, ".opd");
3175 if (opd == NULL && abi == 1)
3176 return 0;
3177 }
90e3cdf2 3178
a7535cf3 3179 symcount = static_count;
c9727e01 3180 if (!relocatable)
a7535cf3 3181 symcount += dyn_count;
90e3cdf2 3182 if (symcount == 0)
c9727e01 3183 return 0;
90e3cdf2 3184
a7535cf3
AM
3185 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3186 if (syms == NULL)
7356fed5 3187 return -1;
a7535cf3
AM
3188
3189 if (!relocatable && static_count != 0 && dyn_count != 0)
3190 {
3191 /* Use both symbol tables. */
3192 memcpy (syms, static_syms, static_count * sizeof (*syms));
3193 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3194 }
3195 else if (!relocatable && static_count == 0)
3196 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3197 else
3198 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3199
90e3cdf2 3200 synthetic_relocatable = relocatable;
595da8c5 3201 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3202
c9727e01
AM
3203 if (!relocatable && symcount > 1)
3204 {
3205 long j;
3206 /* Trim duplicate syms, since we may have merged the normal and
3207 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3208 different values, so trim any with the same value. */
c9727e01
AM
3209 for (i = 1, j = 1; i < symcount; ++i)
3210 if (syms[i - 1]->value + syms[i - 1]->section->vma
3211 != syms[i]->value + syms[i]->section->vma)
3212 syms[j++] = syms[i];
3213 symcount = j;
3214 }
3215
699733f6 3216 i = 0;
ffcfec52 3217 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3218 ++i;
3219 codesecsym = i;
90e3cdf2 3220
699733f6
AM
3221 for (; i < symcount; ++i)
3222 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3223 != (SEC_CODE | SEC_ALLOC))
3224 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3225 break;
3226 codesecsymend = i;
90e3cdf2 3227
699733f6
AM
3228 for (; i < symcount; ++i)
3229 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3230 break;
3231 secsymend = i;
90e3cdf2 3232
699733f6 3233 for (; i < symcount; ++i)
ffcfec52 3234 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3235 break;
3236 opdsymend = i;
90e3cdf2 3237
699733f6
AM
3238 for (; i < symcount; ++i)
3239 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3240 != (SEC_CODE | SEC_ALLOC))
3241 break;
3242 symcount = i;
3243
c9727e01 3244 count = 0;
90e3cdf2 3245
699733f6 3246 if (relocatable)
90e3cdf2 3247 {
699733f6
AM
3248 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3249 arelent *r;
3250 size_t size;
3251 long relcount;
90e3cdf2 3252
468392fb
AM
3253 if (opdsymend == secsymend)
3254 goto done;
3255
699733f6 3256 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3257 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3258 if (relcount == 0)
c9727e01 3259 goto done;
90e3cdf2 3260
7356fed5
AM
3261 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3262 {
3263 count = -1;
3264 goto done;
3265 }
3266
699733f6 3267 size = 0;
595da8c5 3268 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3269 {
3270 asymbol *sym;
90e3cdf2 3271
595da8c5 3272 while (r < opd->relocation + relcount
699733f6
AM
3273 && r->address < syms[i]->value + opd->vma)
3274 ++r;
90e3cdf2 3275
595da8c5 3276 if (r == opd->relocation + relcount)
699733f6 3277 break;
90e3cdf2 3278
699733f6
AM
3279 if (r->address != syms[i]->value + opd->vma)
3280 continue;
90e3cdf2 3281
699733f6
AM
3282 if (r->howto->type != R_PPC64_ADDR64)
3283 continue;
90e3cdf2 3284
699733f6
AM
3285 sym = *r->sym_ptr_ptr;
3286 if (!sym_exists_at (syms, opdsymend, symcount,
3287 sym->section->id, sym->value + r->addend))
3288 {
3289 ++count;
3290 size += sizeof (asymbol);
3291 size += strlen (syms[i]->name) + 2;
3292 }
3293 }
90e3cdf2 3294
c4b0b099
AM
3295 if (size == 0)
3296 goto done;
699733f6
AM
3297 s = *ret = bfd_malloc (size);
3298 if (s == NULL)
3299 {
7356fed5 3300 count = -1;
c9727e01 3301 goto done;
699733f6 3302 }
90e3cdf2 3303
699733f6 3304 names = (char *) (s + count);
90e3cdf2 3305
595da8c5 3306 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3307 {
699733f6 3308 asymbol *sym;
90e3cdf2 3309
595da8c5 3310 while (r < opd->relocation + relcount
699733f6
AM
3311 && r->address < syms[i]->value + opd->vma)
3312 ++r;
90e3cdf2 3313
595da8c5 3314 if (r == opd->relocation + relcount)
699733f6
AM
3315 break;
3316
3317 if (r->address != syms[i]->value + opd->vma)
3318 continue;
3319
3320 if (r->howto->type != R_PPC64_ADDR64)
3321 continue;
90e3cdf2 3322
699733f6
AM
3323 sym = *r->sym_ptr_ptr;
3324 if (!sym_exists_at (syms, opdsymend, symcount,
3325 sym->section->id, sym->value + r->addend))
3326 {
3327 size_t len;
3328
3329 *s = *syms[i];
6ba2a415 3330 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3331 s->section = sym->section;
3332 s->value = sym->value + r->addend;
3333 s->name = names;
3334 *names++ = '.';
3335 len = strlen (syms[i]->name);
3336 memcpy (names, syms[i]->name, len + 1);
3337 names += len + 1;
6f610d07
UW
3338 /* Have udata.p point back to the original symbol this
3339 synthetic symbol was derived from. */
3340 s->udata.p = syms[i];
699733f6
AM
3341 s++;
3342 }
3343 }
3344 }
3345 else
90e3cdf2 3346 {
468392fb 3347 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3348 bfd_byte *contents = NULL;
699733f6 3349 size_t size;
468392fb
AM
3350 long plt_count = 0;
3351 bfd_vma glink_vma = 0, resolv_vma = 0;
3352 asection *dynamic, *glink = NULL, *relplt = NULL;
3353 arelent *p;
90e3cdf2 3354
ee67d69a 3355 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3356 {
c4b0b099
AM
3357 free_contents_and_exit_err:
3358 count = -1;
ee67d69a 3359 free_contents_and_exit:
699733f6 3360 if (contents)
ee67d69a 3361 free (contents);
c9727e01 3362 goto done;
699733f6 3363 }
90e3cdf2 3364
699733f6
AM
3365 size = 0;
3366 for (i = secsymend; i < opdsymend; ++i)
3367 {
3368 bfd_vma ent;
90e3cdf2 3369
5ef11c02
AM
3370 /* Ignore bogus symbols. */
3371 if (syms[i]->value > opd->size - 8)
3372 continue;
3373
699733f6
AM
3374 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3375 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3376 {
3377 ++count;
3378 size += sizeof (asymbol);
3379 size += strlen (syms[i]->name) + 2;
3380 }
3381 }
90e3cdf2 3382
468392fb 3383 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3384 if (dyn_count != 0
3385 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3386 {
3387 bfd_byte *dynbuf, *extdyn, *extdynend;
3388 size_t extdynsize;
3389 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3390
3391 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3392 goto free_contents_and_exit_err;
468392fb
AM
3393
3394 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3395 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3396
3397 extdyn = dynbuf;
3398 extdynend = extdyn + dynamic->size;
3399 for (; extdyn < extdynend; extdyn += extdynsize)
3400 {
3401 Elf_Internal_Dyn dyn;
3402 (*swap_dyn_in) (abfd, extdyn, &dyn);
3403
3404 if (dyn.d_tag == DT_NULL)
3405 break;
3406
3407 if (dyn.d_tag == DT_PPC64_GLINK)
3408 {
b9e5796b
AM
3409 /* The first glink stub starts at offset 32; see
3410 comment in ppc64_elf_finish_dynamic_sections. */
3411 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3412 /* The .glink section usually does not survive the final
3413 link; search for the section (usually .text) where the
3414 glink stubs now reside. */
3415 glink = bfd_sections_find_if (abfd, section_covers_vma,
3416 &glink_vma);
3417 break;
3418 }
3419 }
3420
3421 free (dynbuf);
3422 }
3423
3424 if (glink != NULL)
3425 {
3426 /* Determine __glink trampoline by reading the relative branch
3427 from the first glink stub. */
3428 bfd_byte buf[4];
b9e5796b
AM
3429 unsigned int off = 0;
3430
3431 while (bfd_get_section_contents (abfd, glink, buf,
3432 glink_vma + off - glink->vma, 4))
468392fb
AM
3433 {
3434 unsigned int insn = bfd_get_32 (abfd, buf);
3435 insn ^= B_DOT;
3436 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3437 {
3438 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3439 break;
3440 }
3441 off += 4;
3442 if (off > 4)
3443 break;
468392fb
AM
3444 }
3445
3446 if (resolv_vma)
3447 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3448
066ee829
AM
3449 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3450 if (relplt != NULL)
3451 {
3452 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3453 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3454 goto free_contents_and_exit_err;
68ffbac6 3455
066ee829
AM
3456 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3457 size += plt_count * sizeof (asymbol);
468392fb 3458
066ee829
AM
3459 p = relplt->relocation;
3460 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3461 {
3462 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3463 if (p->addend != 0)
3464 size += sizeof ("+0x") - 1 + 16;
3465 }
066ee829 3466 }
468392fb
AM
3467 }
3468
c4b0b099
AM
3469 if (size == 0)
3470 goto free_contents_and_exit;
699733f6
AM
3471 s = *ret = bfd_malloc (size);
3472 if (s == NULL)
c4b0b099 3473 goto free_contents_and_exit_err;
90e3cdf2 3474
468392fb 3475 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3476
699733f6 3477 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3478 {
699733f6 3479 bfd_vma ent;
90e3cdf2 3480
5ef11c02
AM
3481 if (syms[i]->value > opd->size - 8)
3482 continue;
3483
699733f6
AM
3484 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3485 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3486 {
c9727e01 3487 long lo, hi;
699733f6 3488 size_t len;
c9727e01 3489 asection *sec = abfd->sections;
90e3cdf2 3490
699733f6
AM
3491 *s = *syms[i];
3492 lo = codesecsym;
3493 hi = codesecsymend;
3494 while (lo < hi)
3495 {
c9727e01 3496 long mid = (lo + hi) >> 1;
699733f6
AM
3497 if (syms[mid]->section->vma < ent)
3498 lo = mid + 1;
3499 else if (syms[mid]->section->vma > ent)
3500 hi = mid;
3501 else
c9727e01
AM
3502 {
3503 sec = syms[mid]->section;
3504 break;
3505 }
699733f6
AM
3506 }
3507
c9727e01 3508 if (lo >= hi && lo > codesecsym)
699733f6 3509 sec = syms[lo - 1]->section;
699733f6
AM
3510
3511 for (; sec != NULL; sec = sec->next)
3512 {
3513 if (sec->vma > ent)
3514 break;
63524580
JK
3515 /* SEC_LOAD may not be set if SEC is from a separate debug
3516 info file. */
3517 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3518 break;
3519 if ((sec->flags & SEC_CODE) != 0)
3520 s->section = sec;
3521 }
6ba2a415 3522 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3523 s->value = ent - s->section->vma;
3524 s->name = names;
3525 *names++ = '.';
3526 len = strlen (syms[i]->name);
3527 memcpy (names, syms[i]->name, len + 1);
3528 names += len + 1;
6f610d07
UW
3529 /* Have udata.p point back to the original symbol this
3530 synthetic symbol was derived from. */
3531 s->udata.p = syms[i];
699733f6 3532 s++;
90e3cdf2 3533 }
90e3cdf2 3534 }
699733f6 3535 free (contents);
468392fb
AM
3536
3537 if (glink != NULL && relplt != NULL)
3538 {
3539 if (resolv_vma)
3540 {
3541 /* Add a symbol for the main glink trampoline. */
86a4952b 3542 memset (s, 0, sizeof *s);
468392fb 3543 s->the_bfd = abfd;
6ba2a415 3544 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3545 s->section = glink;
3546 s->value = resolv_vma - glink->vma;
3547 s->name = names;
3548 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3549 names += sizeof ("__glink_PLTresolve");
3550 s++;
3551 count++;
3552 }
3553
3554 /* FIXME: It would be very much nicer to put sym@plt on the
3555 stub rather than on the glink branch table entry. The
3556 objdump disassembler would then use a sensible symbol
3557 name on plt calls. The difficulty in doing so is
3558 a) finding the stubs, and,
3559 b) matching stubs against plt entries, and,
3560 c) there can be multiple stubs for a given plt entry.
3561
3562 Solving (a) could be done by code scanning, but older
3563 ppc64 binaries used different stubs to current code.
3564 (b) is the tricky one since you need to known the toc
3565 pointer for at least one function that uses a pic stub to
3566 be able to calculate the plt address referenced.
3567 (c) means gdb would need to set multiple breakpoints (or
3568 find the glink branch itself) when setting breakpoints
3569 for pending shared library loads. */
3570 p = relplt->relocation;
3571 for (i = 0; i < plt_count; i++, p++)
3572 {
3573 size_t len;
3574
3575 *s = **p->sym_ptr_ptr;
3576 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3577 we are defining a symbol, ensure one of them is set. */
3578 if ((s->flags & BSF_LOCAL) == 0)
3579 s->flags |= BSF_GLOBAL;
6ba2a415 3580 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3581 s->section = glink;
3582 s->value = glink_vma - glink->vma;
3583 s->name = names;
3584 s->udata.p = NULL;
3585 len = strlen ((*p->sym_ptr_ptr)->name);
3586 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3587 names += len;
e054468f
AM
3588 if (p->addend != 0)
3589 {
3590 memcpy (names, "+0x", sizeof ("+0x") - 1);
3591 names += sizeof ("+0x") - 1;
3592 bfd_sprintf_vma (abfd, names, p->addend);
3593 names += strlen (names);
3594 }
468392fb
AM
3595 memcpy (names, "@plt", sizeof ("@plt"));
3596 names += sizeof ("@plt");
3597 s++;
b9e5796b
AM
3598 if (abi < 2)
3599 {
3600 glink_vma += 8;
3601 if (i >= 0x8000)
3602 glink_vma += 4;
3603 }
3604 else
468392fb
AM
3605 glink_vma += 4;
3606 }
3607 count += plt_count;
3608 }
90e3cdf2
JJ
3609 }
3610
c9727e01 3611 done:
a7535cf3 3612 free (syms);
90e3cdf2
JJ
3613 return count;
3614}
5bd4f169 3615\f
65f38f15
AM
3616/* The following functions are specific to the ELF linker, while
3617 functions above are used generally. Those named ppc64_elf_* are
3618 called by the main ELF linker code. They appear in this file more
3619 or less in the order in which they are called. eg.
3620 ppc64_elf_check_relocs is called early in the link process,
3621 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3622 called.
3623
3624 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3625 functions have both a function code symbol and a function descriptor
3626 symbol. A call to foo in a relocatable object file looks like:
3627
3628 . .text
3629 . x:
3630 . bl .foo
3631 . nop
3632
3633 The function definition in another object file might be:
3634
3635 . .section .opd
3636 . foo: .quad .foo
3637 . .quad .TOC.@tocbase
3638 . .quad 0
3639 .
3640 . .text
3641 . .foo: blr
3642
3643 When the linker resolves the call during a static link, the branch
3644 unsurprisingly just goes to .foo and the .opd information is unused.
3645 If the function definition is in a shared library, things are a little
3646 different: The call goes via a plt call stub, the opd information gets
3647 copied to the plt, and the linker patches the nop.
3648
3649 . x:
3650 . bl .foo_stub
3651 . ld 2,40(1)
3652 .
3653 .
3654 . .foo_stub:
71a39c98
AM
3655 . std 2,40(1) # in practice, the call stub
3656 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3657 . addi 11,11,Lfoo@toc@l # this is the general idea
3658 . ld 12,0(11)
3659 . ld 2,8(11)
3660 . mtctr 12
3661 . ld 11,16(11)
e86ce104
AM
3662 . bctr
3663 .
3664 . .section .plt
3665 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3666
3667 The "reloc ()" notation is supposed to indicate that the linker emits
3668 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3669 copying.
3670
3671 What are the difficulties here? Well, firstly, the relocations
3672 examined by the linker in check_relocs are against the function code
3673 sym .foo, while the dynamic relocation in the plt is emitted against
3674 the function descriptor symbol, foo. Somewhere along the line, we need
3675 to carefully copy dynamic link information from one symbol to the other.
3676 Secondly, the generic part of the elf linker will make .foo a dynamic
3677 symbol as is normal for most other backends. We need foo dynamic
3678 instead, at least for an application final link. However, when
3679 creating a shared library containing foo, we need to have both symbols
3680 dynamic so that references to .foo are satisfied during the early
3681 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3682 definition from some other object, eg. a static library.
3683
3684 Update: As of August 2004, we support a new convention. Function
3685 calls may use the function descriptor symbol, ie. "bl foo". This
3686 behaves exactly as "bl .foo". */
65f38f15 3687
1d483afe 3688/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3689 selects those that must be copied when linking a shared library,
3690 even when the symbol is local. */
65f38f15 3691
1d483afe
AM
3692static int
3693must_be_dyn_reloc (struct bfd_link_info *info,
3694 enum elf_ppc64_reloc_type r_type)
3695{
3696 switch (r_type)
3697 {
3698 default:
3699 return 1;
3700
3701 case R_PPC64_REL32:
3702 case R_PPC64_REL64:
3703 case R_PPC64_REL30:
3704 return 0;
3705
3706 case R_PPC64_TPREL16:
3707 case R_PPC64_TPREL16_LO:
3708 case R_PPC64_TPREL16_HI:
3709 case R_PPC64_TPREL16_HA:
3710 case R_PPC64_TPREL16_DS:
3711 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3712 case R_PPC64_TPREL16_HIGH:
3713 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3714 case R_PPC64_TPREL16_HIGHER:
3715 case R_PPC64_TPREL16_HIGHERA:
3716 case R_PPC64_TPREL16_HIGHEST:
3717 case R_PPC64_TPREL16_HIGHESTA:
3718 case R_PPC64_TPREL64:
3719 return !info->executable;
3720 }
3721}
65f38f15 3722
f4656909
AM
3723/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3724 copying dynamic variables from a shared lib into an app's dynbss
3725 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3726 shared lib. With code that gcc generates, it's vital that this be
3727 enabled; In the PowerPC64 ABI, the address of a function is actually
3728 the address of a function descriptor, which resides in the .opd
3729 section. gcc uses the descriptor directly rather than going via the
3730 GOT as some other ABI's do, which means that initialized function
3731 pointers must reference the descriptor. Thus, a function pointer
3732 initialized to the address of a function in a shared library will
3733 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3734 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3735 presents a problem as a plt entry for that function is also
3736 initialized from the function descriptor symbol and the copy reloc
3737 may not be initialized first. */
a23b6845 3738#define ELIMINATE_COPY_RELOCS 1
f4656909 3739
721956f4 3740/* Section name for stubs is the associated section name plus this
29942be8
NC
3741 string. */
3742#define STUB_SUFFIX ".stub"
721956f4
AM
3743
3744/* Linker stubs.
3745 ppc_stub_long_branch:
3746 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3747 destination, but a 24 bit branch in a stub section will reach.
3748 . b dest
3749
3750 ppc_stub_plt_branch:
3751 Similar to the above, but a 24 bit branch in the stub section won't
3752 reach its destination.
71a39c98
AM
3753 . addis %r11,%r2,xxx@toc@ha
3754 . ld %r12,xxx@toc@l(%r11)
3755 . mtctr %r12
721956f4
AM
3756 . bctr
3757
3758 ppc_stub_plt_call:
2c66dc6c
AM
3759 Used to call a function in a shared library. If it so happens that
3760 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3761 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3762 . std %r2,40(%r1)
71a39c98
AM
3763 . addis %r11,%r2,xxx@toc@ha
3764 . ld %r12,xxx+0@toc@l(%r11)
3765 . mtctr %r12
3766 . ld %r2,xxx+8@toc@l(%r11)
3767 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3768 . bctr
ad8e1ba5
AM
3769
3770 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3771 code to adjust the value and save r2 to support multiple toc sections.
3772 A ppc_stub_long_branch with an r2 offset looks like:
3773 . std %r2,40(%r1)
3774 . addis %r2,%r2,off@ha
3775 . addi %r2,%r2,off@l
3776 . b dest
3777
3778 A ppc_stub_plt_branch with an r2 offset looks like:
3779 . std %r2,40(%r1)
71a39c98
AM
3780 . addis %r11,%r2,xxx@toc@ha
3781 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3782 . addis %r2,%r2,off@ha
3783 . addi %r2,%r2,off@l
71a39c98 3784 . mtctr %r12
ad8e1ba5 3785 . bctr
ac2df442
AM
3786
3787 In cases where the "addis" instruction would add zero, the "addis" is
3788 omitted and following instructions modified slightly in some cases.
721956f4
AM
3789*/
3790
3791enum ppc_stub_type {
3792 ppc_stub_none,
3793 ppc_stub_long_branch,
ad8e1ba5 3794 ppc_stub_long_branch_r2off,
721956f4 3795 ppc_stub_plt_branch,
ad8e1ba5 3796 ppc_stub_plt_branch_r2off,
794e51c0 3797 ppc_stub_plt_call,
7341d5e2
AM
3798 ppc_stub_plt_call_r2save,
3799 ppc_stub_global_entry
721956f4
AM
3800};
3801
3802struct ppc_stub_hash_entry {
3803
3804 /* Base hash table entry structure. */
3805 struct bfd_hash_entry root;
3806
ad8e1ba5
AM
3807 enum ppc_stub_type stub_type;
3808
721956f4
AM
3809 /* The stub section. */
3810 asection *stub_sec;
3811
3812 /* Offset within stub_sec of the beginning of this stub. */
3813 bfd_vma stub_offset;
3814
3815 /* Given the symbol's value and its section we can determine its final
3816 value when building the stubs (so the stub knows where to jump. */
3817 bfd_vma target_value;
3818 asection *target_section;
3819
721956f4
AM
3820 /* The symbol table entry, if any, that this was derived from. */
3821 struct ppc_link_hash_entry *h;
e054468f 3822 struct plt_entry *plt_ent;
721956f4
AM
3823
3824 /* Where this stub is being called from, or, in the case of combined
3825 stub sections, the first input section in the group. */
3826 asection *id_sec;
6911b7dc
AM
3827
3828 /* Symbol st_other. */
3829 unsigned char other;
721956f4
AM
3830};
3831
3832struct ppc_branch_hash_entry {
3833
3834 /* Base hash table entry structure. */
3835 struct bfd_hash_entry root;
3836
c456f082 3837 /* Offset within branch lookup table. */
721956f4
AM
3838 unsigned int offset;
3839
3840 /* Generation marker. */
3841 unsigned int iter;
3842};
65f38f15 3843
19e08130
AM
3844/* Used to track dynamic relocations for local symbols. */
3845struct ppc_dyn_relocs
3846{
3847 struct ppc_dyn_relocs *next;
3848
3849 /* The input section of the reloc. */
3850 asection *sec;
3851
3852 /* Total number of relocs copied for the input section. */
3853 unsigned int count : 31;
3854
3855 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3856 unsigned int ifunc : 1;
3857};
3858
65f38f15
AM
3859struct ppc_link_hash_entry
3860{
3861 struct elf_link_hash_entry elf;
3862
b3fac117
AM
3863 union {
3864 /* A pointer to the most recently used stub hash entry against this
3865 symbol. */
3866 struct ppc_stub_hash_entry *stub_cache;
3867
3868 /* A pointer to the next symbol starting with a '.' */
3869 struct ppc_link_hash_entry *next_dot_sym;
3870 } u;
721956f4 3871
65f38f15 3872 /* Track dynamic relocs copied for this symbol. */
6061a67d 3873 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3874
721956f4 3875 /* Link between function code and descriptor symbols. */
34814b9f 3876 struct ppc_link_hash_entry *oh;
721956f4 3877
e86ce104
AM
3878 /* Flag function code and descriptor symbols. */
3879 unsigned int is_func:1;
3880 unsigned int is_func_descriptor:1;
908b32fc 3881 unsigned int fake:1;
411e1bfb 3882
c5614fa4
AM
3883 /* Whether global opd/toc sym has been adjusted or not.
3884 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3885 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3886 unsigned int adjust_done:1;
3887
99877b66
AM
3888 /* Set if we twiddled this symbol to weak at some stage. */
3889 unsigned int was_undefined:1;
3890
411e1bfb 3891 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3892 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3893 corresponding relocs are encountered during check_relocs.
3894 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3895 indicate the corresponding GOT entry type is not needed.
3896 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3897 a TPREL one. We use a separate flag rather than setting TPREL
3898 just for convenience in distinguishing the two cases. */
3899#define TLS_GD 1 /* GD reloc. */
3900#define TLS_LD 2 /* LD reloc. */
3901#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3902#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3903#define TLS_TLS 16 /* Any TLS reloc. */
3904#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3905#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3906#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3907 unsigned char tls_mask;
65f38f15
AM
3908};
3909
3910/* ppc64 ELF linker hash table. */
3911
3912struct ppc_link_hash_table
3913{
3914 struct elf_link_hash_table elf;
3915
721956f4
AM
3916 /* The stub hash table. */
3917 struct bfd_hash_table stub_hash_table;
3918
3919 /* Another hash table for plt_branch stubs. */
3920 struct bfd_hash_table branch_hash_table;
3921
3b421ab3
AM
3922 /* Hash table for function prologue tocsave. */
3923 htab_t tocsave_htab;
3924
e7d1c40c
AM
3925 /* Various options and other info passed from the linker. */
3926 struct ppc64_elf_params *params;
721956f4
AM
3927
3928 /* Array to keep track of which stub sections have been created, and
3929 information on stub grouping. */
3930 struct map_stub {
3931 /* This is the section to which stubs in the group will be attached. */
3932 asection *link_sec;
3933 /* The stub section. */
3934 asection *stub_sec;
ad8e1ba5
AM
3935 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3936 bfd_vma toc_off;
721956f4
AM
3937 } *stub_group;
3938
ad8e1ba5
AM
3939 /* Temp used when calculating TOC pointers. */
3940 bfd_vma toc_curr;
bf102f86
AM
3941 bfd *toc_bfd;
3942 asection *toc_first_sec;
ad8e1ba5 3943
8f3bab57
AM
3944 /* Highest input section id. */
3945 int top_id;
3946
734b6cf9
AM
3947 /* Highest output section index. */
3948 int top_index;
3949
b3fac117
AM
3950 /* Used when adding symbols. */
3951 struct ppc_link_hash_entry *dot_syms;
3952
734b6cf9
AM
3953 /* List of input sections for each output section. */
3954 asection **input_list;
721956f4 3955
33e44f2e 3956 /* Shortcuts to get to dynamic linker sections. */
4ce794b7
AM
3957 asection *dynbss;
3958 asection *relbss;
3959 asection *glink;
82bd7b59 3960 asection *sfpr;
4ce794b7
AM
3961 asection *brlt;
3962 asection *relbrlt;
58d180e8 3963 asection *glink_eh_frame;
ec338859 3964
8387904d
AM
3965 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3966 struct ppc_link_hash_entry *tls_get_addr;
3967 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3968
927be08e
AM
3969 /* The size of reliplt used by got entry relocs. */
3970 bfd_size_type got_reli_size;
3971
9b5ecbd0 3972 /* Statistics. */
7341d5e2 3973 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3974
ee75fd95
AM
3975 /* Number of stubs against global syms. */
3976 unsigned long stub_globals;
3977
ee67d69a
AM
3978 /* Set if we're linking code with function descriptors. */
3979 unsigned int opd_abi:1;
3980
4c52953f 3981 /* Support for multiple toc sections. */
33c0ec9d 3982 unsigned int do_multi_toc:1;
4c52953f 3983 unsigned int multi_toc_needed:1;
927be08e 3984 unsigned int second_toc_pass:1;
67f0cbdb 3985 unsigned int do_toc_opt:1;
4c52953f 3986
5d1634d7 3987 /* Set on error. */
99877b66 3988 unsigned int stub_error:1;
721956f4 3989
f6c7c3e8 3990 /* Temp used by ppc64_elf_before_check_relocs. */
99877b66 3991 unsigned int twiddled_syms:1;
721956f4
AM
3992
3993 /* Incremented every time we size stubs. */
3994 unsigned int stub_iteration;
5d1634d7 3995
87d72d41
AM
3996 /* Small local sym cache. */
3997 struct sym_cache sym_cache;
65f38f15
AM
3998};
3999
4c52953f
AM
4000/* Rename some of the generic section flags to better document how they
4001 are used here. */
b0dddeec
AM
4002
4003/* Nonzero if this section has TLS related relocations. */
4004#define has_tls_reloc sec_flg0
4005
4006/* Nonzero if this section has a call to __tls_get_addr. */
4007#define has_tls_get_addr_call sec_flg1
4008
4009/* Nonzero if this section has any toc or got relocs. */
4010#define has_toc_reloc sec_flg2
4011
4012/* Nonzero if this section has a call to another section that uses
4013 the toc or got. */
d77c8a4b 4014#define makes_toc_func_call sec_flg3
b0dddeec
AM
4015
4016/* Recursion protection when determining above flag. */
d77c8a4b 4017#define call_check_in_progress sec_flg4
70cc837d 4018#define call_check_done sec_flg5
4c52953f 4019
65f38f15
AM
4020/* Get the ppc64 ELF linker hash table from a link_info structure. */
4021
4022#define ppc_hash_table(p) \
4dfe6ac6
NC
4023 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4024 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4025
721956f4
AM
4026#define ppc_stub_hash_lookup(table, string, create, copy) \
4027 ((struct ppc_stub_hash_entry *) \
4028 bfd_hash_lookup ((table), (string), (create), (copy)))
4029
4030#define ppc_branch_hash_lookup(table, string, create, copy) \
4031 ((struct ppc_branch_hash_entry *) \
4032 bfd_hash_lookup ((table), (string), (create), (copy)))
4033
4034/* Create an entry in the stub hash table. */
4035
4036static struct bfd_hash_entry *
4ce794b7
AM
4037stub_hash_newfunc (struct bfd_hash_entry *entry,
4038 struct bfd_hash_table *table,
4039 const char *string)
721956f4
AM
4040{
4041 /* Allocate the structure if it has not already been allocated by a
4042 subclass. */
4043 if (entry == NULL)
4044 {
4045 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4046 if (entry == NULL)
4047 return entry;
4048 }
4049
4050 /* Call the allocation method of the superclass. */
4051 entry = bfd_hash_newfunc (entry, table, string);
4052 if (entry != NULL)
4053 {
4054 struct ppc_stub_hash_entry *eh;
4055
4056 /* Initialize the local fields. */
4057 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4058 eh->stub_type = ppc_stub_none;
721956f4
AM
4059 eh->stub_sec = NULL;
4060 eh->stub_offset = 0;
4061 eh->target_value = 0;
4062 eh->target_section = NULL;
721956f4 4063 eh->h = NULL;
6911b7dc 4064 eh->plt_ent = NULL;
721956f4 4065 eh->id_sec = NULL;
6911b7dc 4066 eh->other = 0;
721956f4
AM
4067 }
4068
4069 return entry;
4070}
4071
4072/* Create an entry in the branch hash table. */
4073
4074static struct bfd_hash_entry *
4ce794b7
AM
4075branch_hash_newfunc (struct bfd_hash_entry *entry,
4076 struct bfd_hash_table *table,
4077 const char *string)
721956f4
AM
4078{
4079 /* Allocate the structure if it has not already been allocated by a
4080 subclass. */
4081 if (entry == NULL)
4082 {
4083 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4084 if (entry == NULL)
4085 return entry;
4086 }
4087
4088 /* Call the allocation method of the superclass. */
4089 entry = bfd_hash_newfunc (entry, table, string);
4090 if (entry != NULL)
4091 {
4092 struct ppc_branch_hash_entry *eh;
4093
4094 /* Initialize the local fields. */
4095 eh = (struct ppc_branch_hash_entry *) entry;
4096 eh->offset = 0;
4097 eh->iter = 0;
4098 }
4099
4100 return entry;
4101}
4102
65f38f15
AM
4103/* Create an entry in a ppc64 ELF linker hash table. */
4104
4105static struct bfd_hash_entry *
4ce794b7
AM
4106link_hash_newfunc (struct bfd_hash_entry *entry,
4107 struct bfd_hash_table *table,
4108 const char *string)
65f38f15
AM
4109{
4110 /* Allocate the structure if it has not already been allocated by a
4111 subclass. */
4112 if (entry == NULL)
4113 {
4114 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4115 if (entry == NULL)
4116 return entry;
4117 }
4118
4119 /* Call the allocation method of the superclass. */
4120 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4121 if (entry != NULL)
4122 {
4123 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4124
b3fac117 4125 memset (&eh->u.stub_cache, 0,
908b32fc 4126 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4127 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4128
4129 /* When making function calls, old ABI code references function entry
4130 points (dot symbols), while new ABI code references the function
4131 descriptor symbol. We need to make any combination of reference and
4132 definition work together, without breaking archive linking.
4133
4134 For a defined function "foo" and an undefined call to "bar":
4135 An old object defines "foo" and ".foo", references ".bar" (possibly
4136 "bar" too).
4137 A new object defines "foo" and references "bar".
4138
4139 A new object thus has no problem with its undefined symbols being
4140 satisfied by definitions in an old object. On the other hand, the
4141 old object won't have ".bar" satisfied by a new object.
4142
4143 Keep a list of newly added dot-symbols. */
4144
4145 if (string[0] == '.')
4146 {
4147 struct ppc_link_hash_table *htab;
4148
4149 htab = (struct ppc_link_hash_table *) table;
4150 eh->u.next_dot_sym = htab->dot_syms;
4151 htab->dot_syms = eh;
4152 }
65f38f15
AM
4153 }
4154
4155 return entry;
4156}
4157
3b421ab3
AM
4158struct tocsave_entry {
4159 asection *sec;
4160 bfd_vma offset;
4161};
4162
4163static hashval_t
4164tocsave_htab_hash (const void *p)
4165{
4166 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4167 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4168}
4169
4170static int
4171tocsave_htab_eq (const void *p1, const void *p2)
4172{
4173 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4174 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4175 return e1->sec == e2->sec && e1->offset == e2->offset;
4176}
4177
68faa637
AM
4178/* Destroy a ppc64 ELF linker hash table. */
4179
4180static void
d495ab0d 4181ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4182{
d495ab0d 4183 struct ppc_link_hash_table *htab;
68faa637 4184
d495ab0d 4185 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4186 if (htab->tocsave_htab)
4187 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4188 bfd_hash_table_free (&htab->branch_hash_table);
4189 bfd_hash_table_free (&htab->stub_hash_table);
4190 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4191}
4192
65f38f15
AM
4193/* Create a ppc64 ELF linker hash table. */
4194
4195static struct bfd_link_hash_table *
4ce794b7 4196ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4197{
4198 struct ppc_link_hash_table *htab;
4199 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4200
4ce794b7 4201 htab = bfd_zmalloc (amt);
65f38f15
AM
4202 if (htab == NULL)
4203 return NULL;
4204
66eb6687 4205 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4206 sizeof (struct ppc_link_hash_entry),
4207 PPC64_ELF_DATA))
65f38f15 4208 {
e2d34d7d 4209 free (htab);
65f38f15
AM
4210 return NULL;
4211 }
4212
721956f4 4213 /* Init the stub hash table too. */
66eb6687
AM
4214 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4215 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4216 {
d495ab0d 4217 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4218 return NULL;
4219 }
721956f4
AM
4220
4221 /* And the branch hash table. */
66eb6687
AM
4222 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4223 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4224 {
4225 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4226 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4227 return NULL;
4228 }
721956f4 4229
3b421ab3
AM
4230 htab->tocsave_htab = htab_try_create (1024,
4231 tocsave_htab_hash,
4232 tocsave_htab_eq,
4233 NULL);
4234 if (htab->tocsave_htab == NULL)
2915c55b 4235 {
d495ab0d 4236 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4237 return NULL;
4238 }
d495ab0d 4239 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4240
3254fd24
AM
4241 /* Initializing two fields of the union is just cosmetic. We really
4242 only care about glist, but when compiled on a 32-bit host the
4243 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4244 debugger inspection of these fields look nicer. */
a6aa5195
AM
4245 htab->elf.init_got_refcount.refcount = 0;
4246 htab->elf.init_got_refcount.glist = NULL;
4247 htab->elf.init_plt_refcount.refcount = 0;
4248 htab->elf.init_plt_refcount.glist = NULL;
4249 htab->elf.init_got_offset.offset = 0;
4250 htab->elf.init_got_offset.glist = NULL;
4251 htab->elf.init_plt_offset.offset = 0;
4252 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4253
65f38f15
AM
4254 return &htab->elf.root;
4255}
4256
bfeb4a28
AM
4257/* Create sections for linker generated code. */
4258
4259static bfd_boolean
4260create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4261{
4262 struct ppc_link_hash_table *htab;
4263 flagword flags;
4264
4265 htab = ppc_hash_table (info);
4266
4267 /* Create .sfpr for code to save and restore fp regs. */
4268 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4269 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4270 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4271 flags);
4272 if (htab->sfpr == NULL
4273 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4274 return FALSE;
4275
4276 /* Create .glink for lazy dynamic linking support. */
4277 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4278 flags);
4279 if (htab->glink == NULL
4280 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4281 return FALSE;
4282
4283 if (!info->no_ld_generated_unwind_info)
4284 {
4285 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4286 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4287 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4288 ".eh_frame",
4289 flags);
4290 if (htab->glink_eh_frame == NULL
4291 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4292 return FALSE;
4293 }
4294
4295 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4296 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4297 if (htab->elf.iplt == NULL
4298 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4299 return FALSE;
4300
4301 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4302 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4303 htab->elf.irelplt
4304 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4305 if (htab->elf.irelplt == NULL
4306 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4307 return FALSE;
4308
4309 /* Create branch lookup table for plt_branch stubs. */
4310 flags = (SEC_ALLOC | SEC_LOAD
4311 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4312 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4313 flags);
4314 if (htab->brlt == NULL
4315 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4316 return FALSE;
4317
4318 if (!info->shared)
4319 return TRUE;
4320
4321 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4322 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4323 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4324 ".rela.branch_lt",
4325 flags);
4326 if (htab->relbrlt == NULL
4327 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4328 return FALSE;
4329
4330 return TRUE;
4331}
4332
e717da7e
AM
4333/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4334
bfeb4a28 4335bfd_boolean
e7d1c40c
AM
4336ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4337 struct ppc64_elf_params *params)
e717da7e
AM
4338{
4339 struct ppc_link_hash_table *htab;
4340
e7d1c40c 4341 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4342
4343/* Always hook our dynamic sections into the first bfd, which is the
4344 linker created stub bfd. This ensures that the GOT header is at
4345 the start of the output TOC section. */
4346 htab = ppc_hash_table (info);
4dfe6ac6 4347 if (htab == NULL)
bfeb4a28 4348 return FALSE;
e7d1c40c
AM
4349 htab->elf.dynobj = params->stub_bfd;
4350 htab->params = params;
bfeb4a28
AM
4351
4352 if (info->relocatable)
4353 return TRUE;
4354
4355 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4356}
4357
721956f4
AM
4358/* Build a name for an entry in the stub hash table. */
4359
4360static char *
4ce794b7
AM
4361ppc_stub_name (const asection *input_section,
4362 const asection *sym_sec,
4363 const struct ppc_link_hash_entry *h,
4364 const Elf_Internal_Rela *rel)
721956f4
AM
4365{
4366 char *stub_name;
bcaa2f82 4367 ssize_t len;
721956f4
AM
4368
4369 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4370 offsets from a sym as a branch target? In fact, we could
4371 probably assume the addend is always zero. */
4372 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4373
4374 if (h)
4375 {
4376 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4377 stub_name = bfd_malloc (len);
46de2a7c
AM
4378 if (stub_name == NULL)
4379 return stub_name;
4380
bcaa2f82
AM
4381 len = sprintf (stub_name, "%08x.%s+%x",
4382 input_section->id & 0xffffffff,
4383 h->elf.root.root.string,
4384 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4385 }
4386 else
4387 {
ad8e1ba5 4388 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4389 stub_name = bfd_malloc (len);
46de2a7c
AM
4390 if (stub_name == NULL)
4391 return stub_name;
4392
bcaa2f82
AM
4393 len = sprintf (stub_name, "%08x.%x:%x+%x",
4394 input_section->id & 0xffffffff,
4395 sym_sec->id & 0xffffffff,
4396 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4397 (int) rel->r_addend & 0xffffffff);
721956f4 4398 }
bcaa2f82 4399 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4400 stub_name[len - 2] = 0;
721956f4
AM
4401 return stub_name;
4402}
4403
4404/* Look up an entry in the stub hash. Stub entries are cached because
4405 creating the stub name takes a bit of time. */
4406
4407static struct ppc_stub_hash_entry *
4ce794b7
AM
4408ppc_get_stub_entry (const asection *input_section,
4409 const asection *sym_sec,
039b3fef 4410 struct ppc_link_hash_entry *h,
4ce794b7
AM
4411 const Elf_Internal_Rela *rel,
4412 struct ppc_link_hash_table *htab)
721956f4
AM
4413{
4414 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4415 const asection *id_sec;
4416
4417 /* If this input section is part of a group of sections sharing one
4418 stub section, then use the id of the first section in the group.
4419 Stub names need to include a section id, as there may well be
4420 more than one stub used to reach say, printf, and we need to
4421 distinguish between them. */
4422 id_sec = htab->stub_group[input_section->id].link_sec;
4423
b3fac117
AM
4424 if (h != NULL && h->u.stub_cache != NULL
4425 && h->u.stub_cache->h == h
4426 && h->u.stub_cache->id_sec == id_sec)
721956f4 4427 {
b3fac117 4428 stub_entry = h->u.stub_cache;
721956f4
AM
4429 }
4430 else
4431 {
4432 char *stub_name;
4433
4434 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4435 if (stub_name == NULL)
4436 return NULL;
4437
4438 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4439 stub_name, FALSE, FALSE);
721956f4 4440 if (h != NULL)
b3fac117 4441 h->u.stub_cache = stub_entry;
721956f4
AM
4442
4443 free (stub_name);
4444 }
4445
4446 return stub_entry;
4447}
4448
4449/* Add a new stub entry to the stub hash. Not all fields of the new
4450 stub entry are initialised. */
4451
4452static struct ppc_stub_hash_entry *
4ce794b7
AM
4453ppc_add_stub (const char *stub_name,
4454 asection *section,
25f53a85 4455 struct bfd_link_info *info)
721956f4 4456{
25f53a85 4457 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4458 asection *link_sec;
4459 asection *stub_sec;
4460 struct ppc_stub_hash_entry *stub_entry;
4461
4462 link_sec = htab->stub_group[section->id].link_sec;
4463 stub_sec = htab->stub_group[section->id].stub_sec;
4464 if (stub_sec == NULL)
4465 {
4466 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4467 if (stub_sec == NULL)
4468 {
d4c88bbb 4469 size_t namelen;
721956f4
AM
4470 bfd_size_type len;
4471 char *s_name;
4472
d4c88bbb
AM
4473 namelen = strlen (link_sec->name);
4474 len = namelen + sizeof (STUB_SUFFIX);
e7d1c40c 4475 s_name = bfd_alloc (htab->params->stub_bfd, len);
721956f4
AM
4476 if (s_name == NULL)
4477 return NULL;
4478
d4c88bbb
AM
4479 memcpy (s_name, link_sec->name, namelen);
4480 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
e7d1c40c 4481 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
721956f4
AM
4482 if (stub_sec == NULL)
4483 return NULL;
4484 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4485 }
4486 htab->stub_group[section->id].stub_sec = stub_sec;
4487 }
4488
4489 /* Enter this entry into the linker stub hash table. */
4490 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4491 TRUE, FALSE);
721956f4
AM
4492 if (stub_entry == NULL)
4493 {
8de848d8 4494 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4495 section->owner, stub_name);
721956f4
AM
4496 return NULL;
4497 }
4498
4499 stub_entry->stub_sec = stub_sec;
4500 stub_entry->stub_offset = 0;
4501 stub_entry->id_sec = link_sec;
4502 return stub_entry;
4503}
4504
e717da7e
AM
4505/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4506 not already done. */
65f38f15 4507
b34976b6 4508static bfd_boolean
e717da7e 4509create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4510{
e717da7e
AM
4511 asection *got, *relgot;
4512 flagword flags;
4513 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4514
0c8d6e5c 4515 if (!is_ppc64_elf (abfd))
0ffa91dd 4516 return FALSE;
4dfe6ac6
NC
4517 if (htab == NULL)
4518 return FALSE;
0ffa91dd 4519
33e44f2e
AM
4520 if (!htab->elf.sgot
4521 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4522 return FALSE;
e717da7e
AM
4523
4524 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4525 | SEC_LINKER_CREATED);
4526
c456f082 4527 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4528 if (!got
e717da7e
AM
4529 || !bfd_set_section_alignment (abfd, got, 3))
4530 return FALSE;
65f38f15 4531
c456f082
AM
4532 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4533 flags | SEC_READONLY);
e717da7e 4534 if (!relgot
e717da7e 4535 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4536 return FALSE;
e717da7e
AM
4537
4538 ppc64_elf_tdata (abfd)->got = got;
4539 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4540 return TRUE;
65f38f15 4541}
5bd4f169 4542
82bd7b59 4543/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4544
b34976b6 4545static bfd_boolean
4ce794b7 4546ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4547{
65f38f15 4548 struct ppc_link_hash_table *htab;
5bd4f169 4549
65f38f15 4550 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4551 return FALSE;
65f38f15 4552
e717da7e 4553 htab = ppc_hash_table (info);
4dfe6ac6
NC
4554 if (htab == NULL)
4555 return FALSE;
4556
3d4d4302 4557 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
65f38f15 4558 if (!info->shared)
3d4d4302 4559 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4560
33e44f2e 4561 if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
4ce794b7 4562 || (!info->shared && !htab->relbss))
65f38f15
AM
4563 abort ();
4564
b34976b6 4565 return TRUE;
5bd4f169
AM
4566}
4567
b31867b6
AM
4568/* Follow indirect and warning symbol links. */
4569
4570static inline struct bfd_link_hash_entry *
4571follow_link (struct bfd_link_hash_entry *h)
4572{
4573 while (h->type == bfd_link_hash_indirect
4574 || h->type == bfd_link_hash_warning)
4575 h = h->u.i.link;
4576 return h;
4577}
4578
4579static inline struct elf_link_hash_entry *
4580elf_follow_link (struct elf_link_hash_entry *h)
4581{
4582 return (struct elf_link_hash_entry *) follow_link (&h->root);
4583}
4584
4585static inline struct ppc_link_hash_entry *
4586ppc_follow_link (struct ppc_link_hash_entry *h)
4587{
4588 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4589}
4590
40d16e0b
AM
4591/* Merge PLT info on FROM with that on TO. */
4592
4593static void
4594move_plt_plist (struct ppc_link_hash_entry *from,
4595 struct ppc_link_hash_entry *to)
4596{
4597 if (from->elf.plt.plist != NULL)
4598 {
4599 if (to->elf.plt.plist != NULL)
4600 {
4601 struct plt_entry **entp;
4602 struct plt_entry *ent;
4603
4604 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4605 {
4606 struct plt_entry *dent;
4607
4608 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4609 if (dent->addend == ent->addend)
4610 {
4611 dent->plt.refcount += ent->plt.refcount;
4612 *entp = ent->next;
4613 break;
4614 }
4615 if (dent == NULL)
4616 entp = &ent->next;
4617 }
4618 *entp = to->elf.plt.plist;
4619 }
4620
4621 to->elf.plt.plist = from->elf.plt.plist;
4622 from->elf.plt.plist = NULL;
4623 }
4624}
4625
65f38f15
AM
4626/* Copy the extra info we tack onto an elf_link_hash_entry. */
4627
4628static void
fcfa13d2
AM
4629ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4630 struct elf_link_hash_entry *dir,
4631 struct elf_link_hash_entry *ind)
65f38f15
AM
4632{
4633 struct ppc_link_hash_entry *edir, *eind;
4634
4635 edir = (struct ppc_link_hash_entry *) dir;
4636 eind = (struct ppc_link_hash_entry *) ind;
4637
c79d6685
AM
4638 edir->is_func |= eind->is_func;
4639 edir->is_func_descriptor |= eind->is_func_descriptor;
4640 edir->tls_mask |= eind->tls_mask;
4641 if (eind->oh != NULL)
4642 edir->oh = ppc_follow_link (eind->oh);
4643
4644 /* If called to transfer flags for a weakdef during processing
4645 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4646 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4647 if (!(ELIMINATE_COPY_RELOCS
4648 && eind->elf.root.type != bfd_link_hash_indirect
4649 && edir->elf.dynamic_adjusted))
4650 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4651
4652 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4653 edir->elf.ref_regular |= eind->elf.ref_regular;
4654 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4655 edir->elf.needs_plt |= eind->elf.needs_plt;
a345bc8d 4656 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4657
411e1bfb 4658 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4659 if (eind->dyn_relocs != NULL)
65f38f15 4660 {
bbd7ec4a
AM
4661 if (edir->dyn_relocs != NULL)
4662 {
6061a67d
AM
4663 struct elf_dyn_relocs **pp;
4664 struct elf_dyn_relocs *p;
bbd7ec4a 4665
fcfa13d2 4666 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4667 list. Merge any entries against the same section. */
4668 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4669 {
6061a67d 4670 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4671
4672 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4673 if (q->sec == p->sec)
4674 {
4675 q->pc_count += p->pc_count;
4676 q->count += p->count;
4677 *pp = p->next;
4678 break;
4679 }
4680 if (q == NULL)
4681 pp = &p->next;
4682 }
4683 *pp = edir->dyn_relocs;
4684 }
4685
65f38f15
AM
4686 edir->dyn_relocs = eind->dyn_relocs;
4687 eind->dyn_relocs = NULL;
4688 }
65f38f15 4689
68ba6d40
AM
4690 /* If we were called to copy over info for a weak sym, that's all.
4691 You might think dyn_relocs need not be copied over; After all,
4692 both syms will be dynamic or both non-dynamic so we're just
68ffbac6 4693 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
68ba6d40
AM
4694 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4695 dyn_relocs in read-only sections, and it does so on what is the
4696 DIR sym here. */
4697 if (eind->elf.root.type != bfd_link_hash_indirect)
4698 return;
4699
81848ca0
AM
4700 /* Copy over got entries that we may have already seen to the
4701 symbol which just became indirect. */
411e1bfb
AM
4702 if (eind->elf.got.glist != NULL)
4703 {
4704 if (edir->elf.got.glist != NULL)
4705 {
4706 struct got_entry **entp;
4707 struct got_entry *ent;
4708
4709 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4710 {
4711 struct got_entry *dent;
4712
4713 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4714 if (dent->addend == ent->addend
e717da7e 4715 && dent->owner == ent->owner
411e1bfb
AM
4716 && dent->tls_type == ent->tls_type)
4717 {
4718 dent->got.refcount += ent->got.refcount;
4719 *entp = ent->next;
4720 break;
4721 }
4722 if (dent == NULL)
4723 entp = &ent->next;
4724 }
4725 *entp = edir->elf.got.glist;
4726 }
4727
4728 edir->elf.got.glist = eind->elf.got.glist;
4729 eind->elf.got.glist = NULL;
4730 }
4731
4732 /* And plt entries. */
40d16e0b 4733 move_plt_plist (eind, edir);
411e1bfb 4734
fcfa13d2 4735 if (eind->elf.dynindx != -1)
411e1bfb 4736 {
fcfa13d2
AM
4737 if (edir->elf.dynindx != -1)
4738 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4739 edir->elf.dynstr_index);
411e1bfb
AM
4740 edir->elf.dynindx = eind->elf.dynindx;
4741 edir->elf.dynstr_index = eind->elf.dynstr_index;
4742 eind->elf.dynindx = -1;
4743 eind->elf.dynstr_index = 0;
4744 }
411e1bfb
AM
4745}
4746
8387904d
AM
4747/* Find the function descriptor hash entry from the given function code
4748 hash entry FH. Link the entries via their OH fields. */
4749
4750static struct ppc_link_hash_entry *
b31867b6 4751lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4752{
4753 struct ppc_link_hash_entry *fdh = fh->oh;
4754
4755 if (fdh == NULL)
4756 {
4757 const char *fd_name = fh->elf.root.root.string + 1;
4758
4759 fdh = (struct ppc_link_hash_entry *)
4760 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4761 if (fdh == NULL)
4762 return fdh;
4763
4764 fdh->is_func_descriptor = 1;
4765 fdh->oh = fh;
4766 fh->is_func = 1;
4767 fh->oh = fdh;
8387904d
AM
4768 }
4769
b31867b6 4770 return ppc_follow_link (fdh);
8387904d
AM
4771}
4772
bb700d78
AM
4773/* Make a fake function descriptor sym for the code sym FH. */
4774
4775static struct ppc_link_hash_entry *
4776make_fdh (struct bfd_link_info *info,
908b32fc 4777 struct ppc_link_hash_entry *fh)
bb700d78
AM
4778{
4779 bfd *abfd;
4780 asymbol *newsym;
4781 struct bfd_link_hash_entry *bh;
4782 struct ppc_link_hash_entry *fdh;
4783
4784 abfd = fh->elf.root.u.undef.abfd;
4785 newsym = bfd_make_empty_symbol (abfd);
4786 newsym->name = fh->elf.root.root.string + 1;
4787 newsym->section = bfd_und_section_ptr;
4788 newsym->value = 0;
908b32fc 4789 newsym->flags = BSF_WEAK;
bb700d78
AM
4790
4791 bh = NULL;
4792 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4793 newsym->flags, newsym->section,
4794 newsym->value, NULL, FALSE, FALSE,
4795 &bh))
4796 return NULL;
4797
4798 fdh = (struct ppc_link_hash_entry *) bh;
4799 fdh->elf.non_elf = 0;
908b32fc
AM
4800 fdh->fake = 1;
4801 fdh->is_func_descriptor = 1;
4802 fdh->oh = fh;
4803 fh->is_func = 1;
4804 fh->oh = fdh;
bb700d78
AM
4805 return fdh;
4806}
4807
8387904d
AM
4808/* Fix function descriptor symbols defined in .opd sections to be
4809 function type. */
555cd476
AM
4810
4811static bfd_boolean
c16153ae 4812ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4813 struct bfd_link_info *info,
555cd476 4814 Elf_Internal_Sym *isym,
6911b7dc 4815 const char **name,
555cd476
AM
4816 flagword *flags ATTRIBUTE_UNUSED,
4817 asection **sec,
b53dfeb2 4818 bfd_vma *value)
555cd476 4819{
f1885d1e
AM
4820 if ((ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4821 || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4822 && (ibfd->flags & DYNAMIC) == 0
4823 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
f64b2e8d
NC
4824 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4825
b53dfeb2 4826 if (*sec != NULL
f1885d1e 4827 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4828 {
4829 asection *code_sec;
4830
4831 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4832 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4833 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4834
4835 /* If the symbol is a function defined in .opd, and the function
4836 code is in a discarded group, let it appear to be undefined. */
4837 if (!info->relocatable
4838 && (*sec)->reloc_count != 0
4839 && opd_entry_value (*sec, *value, &code_sec, NULL,
4840 FALSE) != (bfd_vma) -1
4841 && discarded_section (code_sec))
4842 {
4843 *sec = bfd_und_section_ptr;
4844 isym->st_shndx = SHN_UNDEF;
4845 }
4846 }
dbd1e97e
AM
4847 else if (*sec != NULL
4848 && strcmp ((*sec)->name, ".toc") == 0
4849 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4850 {
4851 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4852 if (htab != NULL)
4853 htab->params->object_in_toc = 1;
4854 }
433817dd 4855
6911b7dc
AM
4856 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4857 {
4858 if (abiversion (ibfd) == 0)
4859 set_abiversion (ibfd, 2);
4860 else if (abiversion (ibfd) == 1)
4861 {
4862 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4863 " for ABI version 1\n"), name);
4864 bfd_set_error (bfd_error_bad_value);
4865 return FALSE;
4866 }
4867 }
4868
555cd476
AM
4869 return TRUE;
4870}
4871
6911b7dc
AM
4872/* Merge non-visibility st_other attributes: local entry point. */
4873
4874static void
4875ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4876 const Elf_Internal_Sym *isym,
4877 bfd_boolean definition,
4878 bfd_boolean dynamic)
4879{
4880 if (definition && !dynamic)
4881 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4882 | ELF_ST_VISIBILITY (h->other));
4883}
4884
8387904d 4885/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4886 inclusion of a new ABI object archive that defines "bar".
4887 NAME is a symbol defined in an archive. Return a symbol in the hash
4888 table that might be satisfied by the archive symbols. */
8387904d
AM
4889
4890static struct elf_link_hash_entry *
4891ppc64_elf_archive_symbol_lookup (bfd *abfd,
4892 struct bfd_link_info *info,
4893 const char *name)
4894{
4895 struct elf_link_hash_entry *h;
4896 char *dot_name;
4897 size_t len;
4898
4899 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4900 if (h != NULL
4901 /* Don't return this sym if it is a fake function descriptor
4902 created by add_symbol_adjust. */
4903 && !(h->root.type == bfd_link_hash_undefweak
4904 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4905 return h;
4906
4907 if (name[0] == '.')
4908 return h;
4909
4910 len = strlen (name);
4911 dot_name = bfd_alloc (abfd, len + 2);
4912 if (dot_name == NULL)
4913 return (struct elf_link_hash_entry *) 0 - 1;
4914 dot_name[0] = '.';
4915 memcpy (dot_name + 1, name, len + 1);
4916 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4917 bfd_release (abfd, dot_name);
4918 return h;
4919}
4920
4921/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4922 new ABI object defines "bar". Well, at least, undefined dot symbols
4923 are made weak. This stops later archive searches from including an
4924 object if we already have a function descriptor definition. It also
35b0ce59
AM
4925 prevents the linker complaining about undefined symbols.
4926 We also check and correct mismatched symbol visibility here. The
4927 most restrictive visibility of the function descriptor and the
4928 function entry symbol is used. */
8387904d
AM
4929
4930static bfd_boolean
b3fac117 4931add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4932{
8387904d
AM
4933 struct ppc_link_hash_table *htab;
4934 struct ppc_link_hash_entry *fdh;
4935
b3fac117 4936 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4937 return TRUE;
4938
b3fac117
AM
4939 if (eh->elf.root.type == bfd_link_hash_warning)
4940 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4941
b3fac117
AM
4942 if (eh->elf.root.root.string[0] != '.')
4943 abort ();
8387904d 4944
b3fac117 4945 htab = ppc_hash_table (info);
4dfe6ac6
NC
4946 if (htab == NULL)
4947 return FALSE;
4948
b31867b6
AM
4949 fdh = lookup_fdh (eh, htab);
4950 if (fdh == NULL)
4951 {
4952 if (!info->relocatable
4953 && (eh->elf.root.type == bfd_link_hash_undefined
4954 || eh->elf.root.type == bfd_link_hash_undefweak)
4955 && eh->elf.ref_regular)
4956 {
4957 /* Make an undefweak function descriptor sym, which is enough to
4958 pull in an --as-needed shared lib, but won't cause link
4959 errors. Archives are handled elsewhere. */
4960 fdh = make_fdh (info, eh);
4961 if (fdh == NULL)
4962 return FALSE;
4963 fdh->elf.ref_regular = 1;
4964 }
bb700d78 4965 }
b31867b6 4966 else
8387904d 4967 {
35b0ce59
AM
4968 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4969 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4970 if (entry_vis < descr_vis)
4971 fdh->elf.other += entry_vis - descr_vis;
4972 else if (entry_vis > descr_vis)
4973 eh->elf.other += descr_vis - entry_vis;
4974
e87d886e
AM
4975 if ((fdh->elf.root.type == bfd_link_hash_defined
4976 || fdh->elf.root.type == bfd_link_hash_defweak)
4977 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4978 {
4979 eh->elf.root.type = bfd_link_hash_undefweak;
4980 eh->was_undefined = 1;
4981 htab->twiddled_syms = 1;
4982 }
8387904d 4983 }
99877b66 4984
8387904d
AM
4985 return TRUE;
4986}
4987
f6c7c3e8
AM
4988/* Set up opd section info and abiversion for IBFD, and process list
4989 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4990
8387904d 4991static bfd_boolean
f6c7c3e8 4992ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4993{
99877b66 4994 struct ppc_link_hash_table *htab;
b3fac117 4995 struct ppc_link_hash_entry **p, *eh;
459609d6 4996 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4997
459609d6 4998 if (opd != NULL && opd->size != 0)
b3fac117 4999 {
459609d6
AM
5000 if (abiversion (ibfd) == 0)
5001 set_abiversion (ibfd, 1);
5002 else if (abiversion (ibfd) == 2)
f6c7c3e8 5003 {
459609d6
AM
5004 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5005 " version %d\n"),
5006 ibfd, abiversion (ibfd));
5007 bfd_set_error (bfd_error_bad_value);
5008 return FALSE;
f6c7c3e8
AM
5009 }
5010
459609d6
AM
5011 if ((ibfd->flags & DYNAMIC) == 0
5012 && (opd->flags & SEC_RELOC) != 0
5013 && opd->reloc_count != 0
5014 && !bfd_is_abs_section (opd->output_section))
b3fac117 5015 {
459609d6
AM
5016 /* Garbage collection needs some extra help with .opd sections.
5017 We don't want to necessarily keep everything referenced by
5018 relocs in .opd, as that would keep all functions. Instead,
5019 if we reference an .opd symbol (a function descriptor), we
5020 want to keep the function code symbol's section. This is
5021 easy for global symbols, but for local syms we need to keep
5022 information about the associated function section. */
5023 bfd_size_type amt;
5024 asection **opd_sym_map;
5025
51aecdc5 5026 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5027 opd_sym_map = bfd_zalloc (ibfd, amt);
5028 if (opd_sym_map == NULL)
b3fac117 5029 return FALSE;
459609d6
AM
5030 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5031 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5032 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5033 }
5034 }
5035
459609d6
AM
5036 if (!is_ppc64_elf (info->output_bfd))
5037 return TRUE;
5038 htab = ppc_hash_table (info);
5039 if (htab == NULL)
5040 return FALSE;
5041
5042 /* For input files without an explicit abiversion in e_flags
5043 we should have flagged any with symbol st_other bits set
5044 as ELFv1 and above flagged those with .opd as ELFv2.
5045 Set the output abiversion if not yet set, and for any input
5046 still ambiguous, take its abiversion from the output.
5047 Differences in ABI are reported later. */
5048 if (abiversion (info->output_bfd) == 0)
5049 set_abiversion (info->output_bfd, abiversion (ibfd));
5050 else if (abiversion (ibfd) == 0)
5051 set_abiversion (ibfd, abiversion (info->output_bfd));
5052
5053 p = &htab->dot_syms;
5054 while ((eh = *p) != NULL)
5055 {
5056 *p = NULL;
5057 if (&eh->elf == htab->elf.hgot)
5058 ;
5059 else if (htab->elf.hgot == NULL
5060 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5061 htab->elf.hgot = &eh->elf;
5062 else if (!add_symbol_adjust (eh, info))
5063 return FALSE;
5064 p = &eh->u.next_dot_sym;
5065 }
5066
b3fac117
AM
5067 /* Clear the list for non-ppc64 input files. */
5068 p = &htab->dot_syms;
5069 while ((eh = *p) != NULL)
5070 {
5071 *p = NULL;
5072 p = &eh->u.next_dot_sym;
5073 }
99877b66
AM
5074
5075 /* We need to fix the undefs list for any syms we have twiddled to
5076 undef_weak. */
5077 if (htab->twiddled_syms)
5078 {
77cfaee6 5079 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
5080 htab->twiddled_syms = 0;
5081 }
b3fac117 5082 return TRUE;
8387904d
AM
5083}
5084
97fed1c9
JJ
5085/* Undo hash table changes when an --as-needed input file is determined
5086 not to be needed. */
5087
5088static bfd_boolean
e5034e59
AM
5089ppc64_elf_notice_as_needed (bfd *ibfd,
5090 struct bfd_link_info *info,
5091 enum notice_asneeded_action act)
97fed1c9 5092{
e5034e59
AM
5093 if (act == notice_not_needed)
5094 {
5095 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5096
e5034e59
AM
5097 if (htab == NULL)
5098 return FALSE;
4dfe6ac6 5099
e5034e59
AM
5100 htab->dot_syms = NULL;
5101 }
5102 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5103}
5104
aa374f67
AM
5105/* If --just-symbols against a final linked binary, then assume we need
5106 toc adjusting stubs when calling functions defined there. */
5107
5108static void
5109ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5110{
5111 if ((sec->flags & SEC_CODE) != 0
5112 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5113 && is_ppc64_elf (sec->owner))
5114 {
2c3f079f
AM
5115 if (abiversion (sec->owner) >= 2
5116 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5117 sec->has_toc_reloc = 1;
5118 }
5119 _bfd_elf_link_just_syms (sec, info);
5120}
5121
e054468f 5122static struct plt_entry **
4ce794b7
AM
5123update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5124 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5125{
5126 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5127 struct plt_entry **local_plt;
f961d9dd 5128 unsigned char *local_got_tls_masks;
411e1bfb
AM
5129
5130 if (local_got_ents == NULL)
5131 {
5132 bfd_size_type size = symtab_hdr->sh_info;
5133
e054468f
AM
5134 size *= (sizeof (*local_got_ents)
5135 + sizeof (*local_plt)
5136 + sizeof (*local_got_tls_masks));
4ce794b7 5137 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5138 if (local_got_ents == NULL)
e054468f 5139 return NULL;
411e1bfb
AM
5140 elf_local_got_ents (abfd) = local_got_ents;
5141 }
5142
e054468f 5143 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5144 {
5145 struct got_entry *ent;
5146
5147 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5148 if (ent->addend == r_addend
5149 && ent->owner == abfd
5150 && ent->tls_type == tls_type)
411e1bfb
AM
5151 break;
5152 if (ent == NULL)
5153 {
5154 bfd_size_type amt = sizeof (*ent);
4ce794b7 5155 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5156 if (ent == NULL)
5157 return FALSE;
5158 ent->next = local_got_ents[r_symndx];
5159 ent->addend = r_addend;
e717da7e 5160 ent->owner = abfd;
411e1bfb 5161 ent->tls_type = tls_type;
927be08e 5162 ent->is_indirect = FALSE;
411e1bfb
AM
5163 ent->got.refcount = 0;
5164 local_got_ents[r_symndx] = ent;
5165 }
5166 ent->got.refcount += 1;
5167 }
5168
e054468f 5169 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5170 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5171 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5172
5173 return local_plt + r_symndx;
65f38f15
AM
5174}
5175
411e1bfb 5176static bfd_boolean
e054468f 5177update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5178{
411e1bfb 5179 struct plt_entry *ent;
1e2f5b6e 5180
e054468f 5181 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5182 if (ent->addend == addend)
5183 break;
5184 if (ent == NULL)
1e2f5b6e 5185 {
411e1bfb 5186 bfd_size_type amt = sizeof (*ent);
4ce794b7 5187 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5188 if (ent == NULL)
5189 return FALSE;
e054468f 5190 ent->next = *plist;
411e1bfb
AM
5191 ent->addend = addend;
5192 ent->plt.refcount = 0;
e054468f 5193 *plist = ent;
1e2f5b6e 5194 }
411e1bfb 5195 ent->plt.refcount += 1;
b34976b6 5196 return TRUE;
1e2f5b6e
AM
5197}
5198
e054468f
AM
5199static bfd_boolean
5200is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5201{
5202 return (r_type == R_PPC64_REL24
5203 || r_type == R_PPC64_REL14
5204 || r_type == R_PPC64_REL14_BRTAKEN
5205 || r_type == R_PPC64_REL14_BRNTAKEN
5206 || r_type == R_PPC64_ADDR24
5207 || r_type == R_PPC64_ADDR14
5208 || r_type == R_PPC64_ADDR14_BRTAKEN
5209 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5210}
5211
5bd4f169 5212/* Look through the relocs for a section during the first phase, and
65f38f15 5213 calculate needed space in the global offset table, procedure
5d1634d7 5214 linkage table, and dynamic reloc sections. */
5bd4f169 5215
b34976b6 5216static bfd_boolean
4ce794b7
AM
5217ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5218 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5219{
65f38f15 5220 struct ppc_link_hash_table *htab;
5bd4f169 5221 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5222 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5223 const Elf_Internal_Rela *rel;
5224 const Elf_Internal_Rela *rel_end;
5bd4f169 5225 asection *sreloc;
1e2f5b6e 5226 asection **opd_sym_map;
3a71aa26 5227 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5228
1049f94e 5229 if (info->relocatable)
b34976b6 5230 return TRUE;
5bd4f169 5231
680a3378
AM
5232 /* Don't do anything special with non-loaded, non-alloced sections.
5233 In particular, any relocs in such sections should not affect GOT
5234 and PLT reference counting (ie. we don't allow them to create GOT
5235 or PLT entries), there's no possibility or desire to optimize TLS
5236 relocs, and there's not much point in propagating relocs to shared
5237 libs that the dynamic linker won't relocate. */
5238 if ((sec->flags & SEC_ALLOC) == 0)
5239 return TRUE;
5240
0c8d6e5c 5241 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5242
65f38f15 5243 htab = ppc_hash_table (info);
4dfe6ac6
NC
5244 if (htab == NULL)
5245 return FALSE;
5246
3a71aa26
AM
5247 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5248 FALSE, FALSE, TRUE);
5249 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5250 FALSE, FALSE, TRUE);
0ffa91dd 5251 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5252 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5253 sreloc = NULL;
1e2f5b6e 5254 opd_sym_map = NULL;
f6c7c3e8
AM
5255 if (ppc64_elf_section_data (sec) != NULL
5256 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5257 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5258
5259 rel_end = relocs + sec->reloc_count;
5260 for (rel = relocs; rel < rel_end; rel++)
5261 {
5262 unsigned long r_symndx;
5263 struct elf_link_hash_entry *h;
04c9666a 5264 enum elf_ppc64_reloc_type r_type;
727fc41e 5265 int tls_type;
7c8fe5c4 5266 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 5267 struct plt_entry **ifunc;
5bd4f169
AM
5268
5269 r_symndx = ELF64_R_SYM (rel->r_info);
5270 if (r_symndx < symtab_hdr->sh_info)
5271 h = NULL;
5272 else
973a3492
L
5273 {
5274 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5275 h = elf_follow_link (h);
81fbe831
AM
5276
5277 /* PR15323, ref flags aren't set for references in the same
5278 object. */
5279 h->root.non_ir_ref = 1;
1c865ab2
AM
5280
5281 if (h == htab->elf.hgot)
5282 sec->has_toc_reloc = 1;
973a3492 5283 }
5bd4f169 5284
727fc41e 5285 tls_type = 0;
e054468f 5286 ifunc = NULL;
25f23106
AM
5287 if (h != NULL)
5288 {
5289 if (h->type == STT_GNU_IFUNC)
5290 {
5291 h->needs_plt = 1;
5292 ifunc = &h->plt.plist;
5293 }
5294 }
5295 else
5296 {
5297 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5298 abfd, r_symndx);
5299 if (isym == NULL)
5300 return FALSE;
5301
5302 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5303 {
5304 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5305 rel->r_addend, PLT_IFUNC);
5306 if (ifunc == NULL)
5307 return FALSE;
5308 }
5309 }
4ce794b7 5310 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
5311 if (is_branch_reloc (r_type))
5312 {
5313 if (h != NULL && (h == tga || h == dottga))
5314 {
5315 if (rel != relocs
5316 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5317 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5318 /* We have a new-style __tls_get_addr call with a marker
5319 reloc. */
5320 ;
5321 else
5322 /* Mark this section as having an old-style call. */
5323 sec->has_tls_get_addr_call = 1;
5324 }
727fc41e 5325
e054468f 5326 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5327 if (ifunc != NULL
5328 && !update_plt_info (abfd, ifunc, rel->r_addend))
5329 return FALSE;
5330 }
727fc41e 5331
a33d1f77 5332 switch (r_type)
5bd4f169 5333 {
727fc41e
AM
5334 case R_PPC64_TLSGD:
5335 case R_PPC64_TLSLD:
5336 /* These special tls relocs tie a call to __tls_get_addr with
5337 its parameter symbol. */
5338 break;
5339
411e1bfb
AM
5340 case R_PPC64_GOT_TLSLD16:
5341 case R_PPC64_GOT_TLSLD16_LO:
5342 case R_PPC64_GOT_TLSLD16_HI:
5343 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5344 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5345 goto dogottls;
5346
5347 case R_PPC64_GOT_TLSGD16:
5348 case R_PPC64_GOT_TLSGD16_LO:
5349 case R_PPC64_GOT_TLSGD16_HI:
5350 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5351 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5352 goto dogottls;
5353
5354 case R_PPC64_GOT_TPREL16_DS:
5355 case R_PPC64_GOT_TPREL16_LO_DS:
5356 case R_PPC64_GOT_TPREL16_HI:
5357 case R_PPC64_GOT_TPREL16_HA:
afb93314 5358 if (info->shared)
411e1bfb
AM
5359 info->flags |= DF_STATIC_TLS;
5360 tls_type = TLS_TLS | TLS_TPREL;
5361 goto dogottls;
5362
5363 case R_PPC64_GOT_DTPREL16_DS:
5364 case R_PPC64_GOT_DTPREL16_LO_DS:
5365 case R_PPC64_GOT_DTPREL16_HI:
5366 case R_PPC64_GOT_DTPREL16_HA:
5367 tls_type = TLS_TLS | TLS_DTPREL;
5368 dogottls:
5369 sec->has_tls_reloc = 1;
5370 /* Fall thru */
5371
5bd4f169 5372 case R_PPC64_GOT16:
5bd4f169 5373 case R_PPC64_GOT16_DS:
65f38f15
AM
5374 case R_PPC64_GOT16_HA:
5375 case R_PPC64_GOT16_HI:
5376 case R_PPC64_GOT16_LO:
5bd4f169 5377 case R_PPC64_GOT16_LO_DS:
65f38f15 5378 /* This symbol requires a global offset table entry. */
4c52953f 5379 sec->has_toc_reloc = 1;
33c0ec9d
AM
5380 if (r_type == R_PPC64_GOT_TLSLD16
5381 || r_type == R_PPC64_GOT_TLSGD16
5382 || r_type == R_PPC64_GOT_TPREL16_DS
5383 || r_type == R_PPC64_GOT_DTPREL16_DS
5384 || r_type == R_PPC64_GOT16
5385 || r_type == R_PPC64_GOT16_DS)
5386 {
5387 htab->do_multi_toc = 1;
d77c8a4b 5388 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5389 }
5390
e717da7e
AM
5391 if (ppc64_elf_tdata (abfd)->got == NULL
5392 && !create_got_section (abfd, info))
b34976b6 5393 return FALSE;
5bd4f169
AM
5394
5395 if (h != NULL)
5396 {
411e1bfb
AM
5397 struct ppc_link_hash_entry *eh;
5398 struct got_entry *ent;
65f38f15 5399
411e1bfb
AM
5400 eh = (struct ppc_link_hash_entry *) h;
5401 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5402 if (ent->addend == rel->r_addend
e717da7e 5403 && ent->owner == abfd
411e1bfb
AM
5404 && ent->tls_type == tls_type)
5405 break;
5406 if (ent == NULL)
5bd4f169 5407 {
411e1bfb 5408 bfd_size_type amt = sizeof (*ent);
4ce794b7 5409 ent = bfd_alloc (abfd, amt);
411e1bfb 5410 if (ent == NULL)
b34976b6 5411 return FALSE;
411e1bfb
AM
5412 ent->next = eh->elf.got.glist;
5413 ent->addend = rel->r_addend;
e717da7e 5414 ent->owner = abfd;
411e1bfb 5415 ent->tls_type = tls_type;
927be08e 5416 ent->is_indirect = FALSE;
411e1bfb
AM
5417 ent->got.refcount = 0;
5418 eh->elf.got.glist = ent;
5bd4f169 5419 }
411e1bfb 5420 ent->got.refcount += 1;
e7b938ca 5421 eh->tls_mask |= tls_type;
5bd4f169 5422 }
411e1bfb
AM
5423 else
5424 /* This is a global offset table entry for a local symbol. */
5425 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5426 rel->r_addend, tls_type))
5427 return FALSE;
a345bc8d
AM
5428
5429 /* We may also need a plt entry if the symbol turns out to be
5430 an ifunc. */
f6c7c3e8 5431 if (h != NULL && !info->shared && abiversion (abfd) != 1)
a345bc8d
AM
5432 {
5433 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5434 return FALSE;
5435 }
5bd4f169
AM
5436 break;
5437
5bd4f169 5438 case R_PPC64_PLT16_HA:
65f38f15
AM
5439 case R_PPC64_PLT16_HI:
5440 case R_PPC64_PLT16_LO:
5441 case R_PPC64_PLT32:
5442 case R_PPC64_PLT64:
5bd4f169 5443 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5444 actually build the entry in adjust_dynamic_symbol,
5445 because this might be a case of linking PIC code without
5446 linking in any dynamic objects, in which case we don't
5447 need to generate a procedure linkage table after all. */
5bd4f169
AM
5448 if (h == NULL)
5449 {
5450 /* It does not make sense to have a procedure linkage
3fad3c7c 5451 table entry for a local symbol. */
5bd4f169 5452 bfd_set_error (bfd_error_bad_value);
b34976b6 5453 return FALSE;
5bd4f169 5454 }
411e1bfb 5455 else
e054468f
AM
5456 {
5457 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5458 return FALSE;
5459 h->needs_plt = 1;
5460 if (h->root.root.string[0] == '.'
5461 && h->root.root.string[1] != '\0')
5462 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5463 }
5bd4f169
AM
5464 break;
5465
5466 /* The following relocations don't need to propagate the
5467 relocation if linking a shared object since they are
5468 section relative. */
5469 case R_PPC64_SECTOFF:
5470 case R_PPC64_SECTOFF_LO:
5471 case R_PPC64_SECTOFF_HI:
5472 case R_PPC64_SECTOFF_HA:
5473 case R_PPC64_SECTOFF_DS:
5474 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5475 case R_PPC64_DTPREL16:
5476 case R_PPC64_DTPREL16_LO:
5477 case R_PPC64_DTPREL16_HI:
5478 case R_PPC64_DTPREL16_HA:
5479 case R_PPC64_DTPREL16_DS:
5480 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5481 case R_PPC64_DTPREL16_HIGH:
5482 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5483 case R_PPC64_DTPREL16_HIGHER:
5484 case R_PPC64_DTPREL16_HIGHERA:
5485 case R_PPC64_DTPREL16_HIGHEST:
5486 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5487 break;
5488
ad8e1ba5 5489 /* Nor do these. */
25f23106
AM
5490 case R_PPC64_REL16:
5491 case R_PPC64_REL16_LO:
5492 case R_PPC64_REL16_HI:
5493 case R_PPC64_REL16_HA:
5494 break;
5495
45965137
AM
5496 /* Not supported as a dynamic relocation. */
5497 case R_PPC64_ADDR64_LOCAL:
5498 if (info->shared)
5499 {
5500 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5501 ppc_howto_init ();
5502 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5503 "in shared libraries and PIEs.\n"),
5504 abfd, sec, rel->r_offset,
5505 ppc64_elf_howto_table[r_type]->name);
5506 bfd_set_error (bfd_error_bad_value);
5507 return FALSE;
5508 }
5509 break;
5510
ad8e1ba5 5511 case R_PPC64_TOC16:
33c0ec9d
AM
5512 case R_PPC64_TOC16_DS:
5513 htab->do_multi_toc = 1;
d77c8a4b 5514 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5515 case R_PPC64_TOC16_LO:
5516 case R_PPC64_TOC16_HI:
5517 case R_PPC64_TOC16_HA:
ad8e1ba5 5518 case R_PPC64_TOC16_LO_DS:
4c52953f 5519 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5520 break;
5521
5bd4f169
AM
5522 /* This relocation describes the C++ object vtable hierarchy.
5523 Reconstruct it for later use during GC. */
5524 case R_PPC64_GNU_VTINHERIT:
c152c796 5525 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5526 return FALSE;
5bd4f169
AM
5527 break;
5528
5529 /* This relocation describes which C++ vtable entries are actually
5530 used. Record for later use during GC. */
5531 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5532 BFD_ASSERT (h != NULL);
5533 if (h != NULL
5534 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5535 return FALSE;
5bd4f169
AM
5536 break;
5537
721956f4
AM
5538 case R_PPC64_REL14:
5539 case R_PPC64_REL14_BRTAKEN:
5540 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5541 {
5542 asection *dest = NULL;
5543
5544 /* Heuristic: If jumping outside our section, chances are
5545 we are going to need a stub. */
5546 if (h != NULL)
5547 {
5548 /* If the sym is weak it may be overridden later, so
5549 don't assume we know where a weak sym lives. */
5550 if (h->root.type == bfd_link_hash_defined)
5551 dest = h->root.u.def.section;
5552 }
5553 else
87d72d41
AM
5554 {
5555 Elf_Internal_Sym *isym;
5556
5557 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5558 abfd, r_symndx);
5559 if (isym == NULL)
5560 return FALSE;
5561
5562 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5563 }
5564
220c76dd 5565 if (dest != sec)
7c8fe5c4 5566 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5567 }
721956f4
AM
5568 /* Fall through. */
5569
5d1634d7 5570 case R_PPC64_REL24:
e054468f 5571 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5572 {
5573 /* We may need a .plt entry if the function this reloc
5574 refers to is in a shared lib. */
e054468f 5575 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5576 return FALSE;
e054468f
AM
5577 h->needs_plt = 1;
5578 if (h->root.root.string[0] == '.'
5579 && h->root.root.string[1] != '\0')
5580 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5581 if (h == tga || h == dottga)
411e1bfb 5582 sec->has_tls_reloc = 1;
411e1bfb
AM
5583 }
5584 break;
5585
5586 case R_PPC64_TPREL64:
5587 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
afb93314 5588 if (info->shared)
411e1bfb
AM
5589 info->flags |= DF_STATIC_TLS;
5590 goto dotlstoc;
5591
5592 case R_PPC64_DTPMOD64:
5593 if (rel + 1 < rel_end
5594 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5595 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5596 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5597 else
951fd09b 5598 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5599 goto dotlstoc;
5600
5601 case R_PPC64_DTPREL64:
5602 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5603 if (rel != relocs
5604 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5605 && rel[-1].r_offset == rel->r_offset - 8)
5606 /* This is the second reloc of a dtpmod, dtprel pair.
5607 Don't mark with TLS_DTPREL. */
5608 goto dodyn;
5609
5610 dotlstoc:
5611 sec->has_tls_reloc = 1;
5612 if (h != NULL)
5613 {
5614 struct ppc_link_hash_entry *eh;
5615 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5616 eh->tls_mask |= tls_type;
411e1bfb
AM
5617 }
5618 else
5619 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5620 rel->r_addend, tls_type))
5621 return FALSE;
5622
7c8fe5c4
AM
5623 ppc64_sec = ppc64_elf_section_data (sec);
5624 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5625 {
3a71aa26
AM
5626 bfd_size_type amt;
5627
e7b938ca 5628 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5629 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5630 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5631 if (ppc64_sec->u.toc.symndx == NULL)
5632 return FALSE;
5633 amt = sec->size * sizeof (bfd_vma) / 8;
5634 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5635 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5636 return FALSE;
7c8fe5c4
AM
5637 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5638 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5639 }
5640 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5641 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5642 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5643
5644 /* Mark the second slot of a GD or LD entry.
5645 -1 to indicate GD and -2 to indicate LD. */
5646 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5647 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5648 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5649 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5650 goto dodyn;
5651
5652 case R_PPC64_TPREL16:
5653 case R_PPC64_TPREL16_LO:
5654 case R_PPC64_TPREL16_HI:
5655 case R_PPC64_TPREL16_HA:
5656 case R_PPC64_TPREL16_DS:
5657 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5658 case R_PPC64_TPREL16_HIGH:
5659 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5660 case R_PPC64_TPREL16_HIGHER:
5661 case R_PPC64_TPREL16_HIGHERA:
5662 case R_PPC64_TPREL16_HIGHEST:
5663 case R_PPC64_TPREL16_HIGHESTA:
5664 if (info->shared)
5665 {
afb93314 5666 info->flags |= DF_STATIC_TLS;
411e1bfb 5667 goto dodyn;
5d1634d7
AM
5668 }
5669 break;
5670
e86ce104 5671 case R_PPC64_ADDR64:
1e2f5b6e 5672 if (opd_sym_map != NULL
1e2f5b6e 5673 && rel + 1 < rel_end
4ce794b7 5674 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5675 {
8387904d
AM
5676 if (h != NULL)
5677 {
5678 if (h->root.root.string[0] == '.'
5679 && h->root.root.string[1] != 0
b31867b6 5680 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5681 ;
5682 else
5683 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5684 }
5685 else
5686 {
5687 asection *s;
87d72d41 5688 Elf_Internal_Sym *isym;
1e2f5b6e 5689
87d72d41
AM
5690 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5691 abfd, r_symndx);
5692 if (isym == NULL)
8387904d 5693 return FALSE;
87d72d41
AM
5694
5695 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5696 if (s != NULL && s != sec)
51aecdc5 5697 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5698 }
1e2f5b6e 5699 }
e86ce104
AM
5700 /* Fall through. */
5701
65f38f15
AM
5702 case R_PPC64_ADDR16:
5703 case R_PPC64_ADDR16_DS:
5704 case R_PPC64_ADDR16_HA:
5705 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5706 case R_PPC64_ADDR16_HIGH:
5707 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5708 case R_PPC64_ADDR16_HIGHER:
5709 case R_PPC64_ADDR16_HIGHERA:
5710 case R_PPC64_ADDR16_HIGHEST:
5711 case R_PPC64_ADDR16_HIGHESTA:
5712 case R_PPC64_ADDR16_LO:
5713 case R_PPC64_ADDR16_LO_DS:
f6c7c3e8 5714 if (h != NULL && !info->shared && abiversion (abfd) != 1
a345bc8d
AM
5715 && rel->r_addend == 0)
5716 {
5717 /* We may need a .plt entry if this reloc refers to a
5718 function in a shared lib. */
5719 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5720 return FALSE;
5721 h->pointer_equality_needed = 1;
5722 }
5723 /* Fall through. */
5724
5725 case R_PPC64_REL30:
5726 case R_PPC64_REL32:
5727 case R_PPC64_REL64:
5728 case R_PPC64_ADDR14:
5729 case R_PPC64_ADDR14_BRNTAKEN:
5730 case R_PPC64_ADDR14_BRTAKEN:
65f38f15 5731 case R_PPC64_ADDR24:
65f38f15 5732 case R_PPC64_ADDR32:
65f38f15
AM
5733 case R_PPC64_UADDR16:
5734 case R_PPC64_UADDR32:
5735 case R_PPC64_UADDR64:
5bd4f169 5736 case R_PPC64_TOC:
81848ca0
AM
5737 if (h != NULL && !info->shared)
5738 /* We may need a copy reloc. */
f5385ebf 5739 h->non_got_ref = 1;
81848ca0 5740
41bd81ab 5741 /* Don't propagate .opd relocs. */
1e2f5b6e 5742 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5743 break;
e86ce104 5744
65f38f15
AM
5745 /* If we are creating a shared library, and this is a reloc
5746 against a global symbol, or a non PC relative reloc
5747 against a local symbol, then we need to copy the reloc
5748 into the shared library. However, if we are linking with
5749 -Bsymbolic, we do not need to copy a reloc against a
5750 global symbol which is defined in an object we are
5751 including in the link (i.e., DEF_REGULAR is set). At
5752 this point we have not seen all the input files, so it is
5753 possible that DEF_REGULAR is not set now but will be set
5754 later (it is never cleared). In case of a weak definition,
5755 DEF_REGULAR may be cleared later by a strong definition in
5756 a shared library. We account for that possibility below by
f4656909 5757 storing information in the dyn_relocs field of the hash
65f38f15
AM
5758 table entry. A similar situation occurs when creating
5759 shared libraries and symbol visibility changes render the
5760 symbol local.
5761
5762 If on the other hand, we are creating an executable, we
5763 may need to keep relocations for symbols satisfied by a
5764 dynamic library if we manage to avoid copy relocs for the
5765 symbol. */
411e1bfb 5766 dodyn:
65f38f15 5767 if ((info->shared
1d483afe 5768 && (must_be_dyn_reloc (info, r_type)
65f38f15 5769 || (h != NULL
198f1157 5770 && (!SYMBOLIC_BIND (info, h)
65f38f15 5771 || h->root.type == bfd_link_hash_defweak
f5385ebf 5772 || !h->def_regular))))
f4656909
AM
5773 || (ELIMINATE_COPY_RELOCS
5774 && !info->shared
65f38f15
AM
5775 && h != NULL
5776 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5777 || !h->def_regular))
5778 || (!info->shared
5779 && ifunc != NULL))
5bd4f169 5780 {
65f38f15
AM
5781 /* We must copy these reloc types into the output file.
5782 Create a reloc section in dynobj and make room for
5783 this reloc. */
5bd4f169
AM
5784 if (sreloc == NULL)
5785 {
83bac4b0
NC
5786 sreloc = _bfd_elf_make_dynamic_reloc_section
5787 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5788
5bd4f169 5789 if (sreloc == NULL)
83bac4b0 5790 return FALSE;
5bd4f169
AM
5791 }
5792
65f38f15
AM
5793 /* If this is a global symbol, we count the number of
5794 relocations we need for this symbol. */
5795 if (h != NULL)
5796 {
19e08130
AM
5797 struct elf_dyn_relocs *p;
5798 struct elf_dyn_relocs **head;
5799
ec338859 5800 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5801 p = *head;
5802 if (p == NULL || p->sec != sec)
5803 {
5804 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5805 if (p == NULL)
5806 return FALSE;
5807 p->next = *head;
5808 *head = p;
5809 p->sec = sec;
5810 p->count = 0;
5811 p->pc_count = 0;
5812 }
5813 p->count += 1;
5814 if (!must_be_dyn_reloc (info, r_type))
5815 p->pc_count += 1;
65f38f15
AM
5816 }
5817 else
5818 {
ec338859
AM
5819 /* Track dynamic relocs needed for local syms too.
5820 We really need local syms available to do this
5821 easily. Oh well. */
19e08130
AM
5822 struct ppc_dyn_relocs *p;
5823 struct ppc_dyn_relocs **head;
5824 bfd_boolean is_ifunc;
ec338859 5825 asection *s;
6edfbbad 5826 void *vpp;
87d72d41 5827 Elf_Internal_Sym *isym;
6edfbbad 5828
87d72d41
AM
5829 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5830 abfd, r_symndx);
5831 if (isym == NULL)
b34976b6 5832 return FALSE;
ec338859 5833
87d72d41
AM
5834 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5835 if (s == NULL)
5836 s = sec;
5837
6edfbbad 5838 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5839 head = (struct ppc_dyn_relocs **) vpp;
5840 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5841 p = *head;
5842 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5843 p = p->next;
5844 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5845 {
5846 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5847 if (p == NULL)
5848 return FALSE;
5849 p->next = *head;
5850 *head = p;
5851 p->sec = sec;
5852 p->ifunc = is_ifunc;
5853 p->count = 0;
5854 }
5855 p->count += 1;
ec338859 5856 }
65f38f15 5857 }
5bd4f169 5858 break;
65f38f15
AM
5859
5860 default:
96e0dda4 5861 break;
5bd4f169
AM
5862 }
5863 }
5864
b34976b6 5865 return TRUE;
5bd4f169
AM
5866}
5867
ee67d69a
AM
5868/* Merge backend specific data from an object file to the output
5869 object file when linking. */
5870
5871static bfd_boolean
5872ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5873{
5874 unsigned long iflags, oflags;
5875
5876 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5877 return TRUE;
5878
5879 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5880 return TRUE;
5881
5882 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5883 return FALSE;
5884
5885 iflags = elf_elfheader (ibfd)->e_flags;
5886 oflags = elf_elfheader (obfd)->e_flags;
5887
f6c7c3e8 5888 if (iflags & ~EF_PPC64_ABI)
ee67d69a
AM
5889 {
5890 (*_bfd_error_handler)
5891 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5892 bfd_set_error (bfd_error_bad_value);
5893 return FALSE;
5894 }
f6c7c3e8 5895 else if (iflags != oflags && iflags != 0)
ee67d69a
AM
5896 {
5897 (*_bfd_error_handler)
5898 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5899 ibfd, iflags, oflags);
5900 bfd_set_error (bfd_error_bad_value);
5901 return FALSE;
5902 }
5903
5904 /* Merge Tag_compatibility attributes and any common GNU ones. */
5905 _bfd_elf_merge_object_attributes (ibfd, obfd);
5906
5907 return TRUE;
5908}
5909
5910static bfd_boolean
5911ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5912{
5913 /* Print normal ELF private data. */
5914 _bfd_elf_print_private_bfd_data (abfd, ptr);
5915
5916 if (elf_elfheader (abfd)->e_flags != 0)
5917 {
5918 FILE *file = ptr;
5919
5920 /* xgettext:c-format */
5921 fprintf (file, _("private flags = 0x%lx:"),
5922 elf_elfheader (abfd)->e_flags);
5923
5924 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5925 fprintf (file, _(" [abiv%ld]"),
5926 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5927 fputc ('\n', file);
5928 }
5929
5930 return TRUE;
5931}
5932
8387904d 5933/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5934 of the code entry point, and its section, which must be in the same
5935 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5936
5937static bfd_vma
5938opd_entry_value (asection *opd_sec,
5939 bfd_vma offset,
5940 asection **code_sec,
aef36ac1
AM
5941 bfd_vma *code_off,
5942 bfd_boolean in_code_sec)
8387904d
AM
5943{
5944 bfd *opd_bfd = opd_sec->owner;
8860955f 5945 Elf_Internal_Rela *relocs;
8387904d 5946 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5947 bfd_vma val;
8387904d 5948
9f296da3
AM
5949 /* No relocs implies we are linking a --just-symbols object, or looking
5950 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5951 if (opd_sec->reloc_count == 0)
5952 {
729eabd5 5953 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5954
729eabd5
AM
5955 if (contents == NULL)
5956 {
5957 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5958 return (bfd_vma) -1;
5959 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5960 }
ee1e4ede 5961
dbb3fbbb 5962 /* PR 17512: file: 64b9dfbb. */
451dfd38 5963 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5964 return (bfd_vma) -1;
5965
729eabd5 5966 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5967 if (code_sec != NULL)
5968 {
5969 asection *sec, *likely = NULL;
ee1e4ede 5970
aef36ac1 5971 if (in_code_sec)
4b85d634 5972 {
aef36ac1
AM
5973 sec = *code_sec;
5974 if (sec->vma <= val
5975 && val < sec->vma + sec->size)
5976 likely = sec;
5977 else
5978 val = -1;
5979 }
5980 else
5981 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5982 if (sec->vma <= val
5983 && (sec->flags & SEC_LOAD) != 0
5984 && (sec->flags & SEC_ALLOC) != 0)
5985 likely = sec;
5986 if (likely != NULL)
5987 {
5988 *code_sec = likely;
5989 if (code_off != NULL)
5990 *code_off = val - likely->vma;
4b85d634
AM
5991 }
5992 }
aef36ac1 5993 return val;
4b85d634
AM
5994 }
5995
0c8d6e5c 5996 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5997
729eabd5 5998 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5999 if (relocs == NULL)
6000 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6001 /* PR 17512: file: df8e1fd6. */
6002 if (relocs == NULL)
6003 return (bfd_vma) -1;
645ea6a9 6004
8387904d 6005 /* Go find the opd reloc at the sym address. */
8860955f 6006 lo = relocs;
8387904d 6007 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6008 val = (bfd_vma) -1;
8387904d
AM
6009 while (lo < hi)
6010 {
6011 look = lo + (hi - lo) / 2;
6012 if (look->r_offset < offset)
6013 lo = look + 1;
6014 else if (look->r_offset > offset)
6015 hi = look;
6016 else
6017 {
0ffa91dd
NC
6018 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6019
8387904d
AM
6020 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6021 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6022 {
6023 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6024 asection *sec = NULL;
8387904d 6025
b53dfeb2
AM
6026 if (symndx >= symtab_hdr->sh_info
6027 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6028 {
6029 struct elf_link_hash_entry **sym_hashes;
6030 struct elf_link_hash_entry *rh;
6031
6032 sym_hashes = elf_sym_hashes (opd_bfd);
6033 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6034 if (rh != NULL)
6035 {
6036 rh = elf_follow_link (rh);
6037 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
6038 || rh->root.type == bfd_link_hash_defweak);
6039 val = rh->root.u.def.value;
6040 sec = rh->root.u.def.section;
b53dfeb2
AM
6041 if (sec->owner != opd_bfd)
6042 {
6043 sec = NULL;
6044 val = (bfd_vma) -1;
6045 }
6046 }
6047 }
6048
6049 if (sec == NULL)
6050 {
6051 Elf_Internal_Sym *sym;
6052
6053 if (symndx < symtab_hdr->sh_info)
6054 {
6055 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6056 if (sym == NULL)
6057 {
6058 size_t symcnt = symtab_hdr->sh_info;
6059 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6060 symcnt, 0,
6061 NULL, NULL, NULL);
6062 if (sym == NULL)
6063 break;
6064 symtab_hdr->contents = (bfd_byte *) sym;
6065 }
6066 sym += symndx;
128205bb
AM
6067 }
6068 else
6069 {
b53dfeb2
AM
6070 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6071 1, symndx,
6072 NULL, NULL, NULL);
128205bb
AM
6073 if (sym == NULL)
6074 break;
128205bb 6075 }
b53dfeb2
AM
6076 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6077 if (sec == NULL)
6078 break;
6079 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6080 val = sym->st_value;
8387904d 6081 }
b53dfeb2 6082
8387904d
AM
6083 val += look->r_addend;
6084 if (code_off != NULL)
6085 *code_off = val;
6086 if (code_sec != NULL)
aef36ac1
AM
6087 {
6088 if (in_code_sec && *code_sec != sec)
6089 return -1;
6090 else
6091 *code_sec = sec;
6092 }
b53dfeb2 6093 if (sec->output_section != NULL)
8387904d 6094 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6095 }
6096 break;
6097 }
6098 }
645ea6a9 6099
645ea6a9 6100 return val;
8387904d
AM
6101}
6102
aef36ac1
AM
6103/* If the ELF symbol SYM might be a function in SEC, return the
6104 function size and set *CODE_OFF to the function's entry point,
6105 otherwise return zero. */
9f296da3 6106
aef36ac1
AM
6107static bfd_size_type
6108ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6109 bfd_vma *code_off)
9f296da3 6110{
aef36ac1
AM
6111 bfd_size_type size;
6112
6113 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6114 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6115 return 0;
6116
6117 size = 0;
6118 if (!(sym->flags & BSF_SYNTHETIC))
6119 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6120
6121 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6122 {
aef36ac1
AM
6123 if (opd_entry_value (sym->section, sym->value,
6124 &sec, code_off, TRUE) == (bfd_vma) -1)
6125 return 0;
6126 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6127 symbol. This size has nothing to do with the code size of the
6128 function, which is what we're supposed to return, but the
6129 code size isn't available without looking up the dot-sym.
6130 However, doing that would be a waste of time particularly
6131 since elf_find_function will look at the dot-sym anyway.
6132 Now, elf_find_function will keep the largest size of any
6133 function sym found at the code address of interest, so return
6134 1 here to avoid it incorrectly caching a larger function size
6135 for a small function. This does mean we return the wrong
6136 size for a new-ABI function of size 24, but all that does is
6137 disable caching for such functions. */
6138 if (size == 24)
6139 size = 1;
9f296da3 6140 }
aef36ac1
AM
6141 else
6142 {
6143 if (sym->section != sec)
6144 return 0;
6145 *code_off = sym->value;
6146 }
6147 if (size == 0)
6148 size = 1;
6149 return size;
9f296da3
AM
6150}
6151
854b41e7
AM
6152/* Return true if symbol is defined in a regular object file. */
6153
6154static bfd_boolean
6155is_static_defined (struct elf_link_hash_entry *h)
6156{
6157 return ((h->root.type == bfd_link_hash_defined
6158 || h->root.type == bfd_link_hash_defweak)
6159 && h->root.u.def.section != NULL
6160 && h->root.u.def.section->output_section != NULL);
6161}
6162
b31867b6
AM
6163/* If FDH is a function descriptor symbol, return the associated code
6164 entry symbol if it is defined. Return NULL otherwise. */
6165
6166static struct ppc_link_hash_entry *
6167defined_code_entry (struct ppc_link_hash_entry *fdh)
6168{
6169 if (fdh->is_func_descriptor)
6170 {
6171 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6172 if (fh->elf.root.type == bfd_link_hash_defined
6173 || fh->elf.root.type == bfd_link_hash_defweak)
6174 return fh;
6175 }
6176 return NULL;
6177}
6178
6179/* If FH is a function code entry symbol, return the associated
6180 function descriptor symbol if it is defined. Return NULL otherwise. */
6181
6182static struct ppc_link_hash_entry *
6183defined_func_desc (struct ppc_link_hash_entry *fh)
6184{
6185 if (fh->oh != NULL
6186 && fh->oh->is_func_descriptor)
6187 {
6188 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6189 if (fdh->elf.root.type == bfd_link_hash_defined
6190 || fdh->elf.root.type == bfd_link_hash_defweak)
6191 return fdh;
6192 }
6193 return NULL;
6194}
6195
74f0fb50
AM
6196/* Mark all our entry sym sections, both opd and code section. */
6197
6198static void
6199ppc64_elf_gc_keep (struct bfd_link_info *info)
6200{
6201 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6202 struct bfd_sym_chain *sym;
6203
4dfe6ac6
NC
6204 if (htab == NULL)
6205 return;
6206
74f0fb50
AM
6207 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6208 {
b31867b6 6209 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6210 asection *sec;
6211
6212 eh = (struct ppc_link_hash_entry *)
b31867b6 6213 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6214 if (eh == NULL)
6215 continue;
6216 if (eh->elf.root.type != bfd_link_hash_defined
6217 && eh->elf.root.type != bfd_link_hash_defweak)
6218 continue;
6219
b31867b6
AM
6220 fh = defined_code_entry (eh);
6221 if (fh != NULL)
74f0fb50 6222 {
b31867b6 6223 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6224 sec->flags |= SEC_KEEP;
6225 }
6226 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6227 && opd_entry_value (eh->elf.root.u.def.section,
6228 eh->elf.root.u.def.value,
aef36ac1 6229 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6230 sec->flags |= SEC_KEEP;
6231
6232 sec = eh->elf.root.u.def.section;
6233 sec->flags |= SEC_KEEP;
6234 }
6235}
6236
64d03ab5
AM
6237/* Mark sections containing dynamically referenced symbols. When
6238 building shared libraries, we must assume that any visible symbol is
6239 referenced. */
6240
6241static bfd_boolean
6242ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6243{
6244 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6245 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6246 struct ppc_link_hash_entry *fdh;
b407645f 6247 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6248
64d03ab5 6249 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6250 fdh = defined_func_desc (eh);
6251 if (fdh != NULL)
6252 eh = fdh;
64d03ab5
AM
6253
6254 if ((eh->elf.root.type == bfd_link_hash_defined
6255 || eh->elf.root.type == bfd_link_hash_defweak)
6256 && (eh->elf.ref_dynamic
1c9177d9 6257 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6258 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6259 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
b407645f
AM
6260 && (!info->executable
6261 || info->export_dynamic
6262 || (eh->elf.dynamic
6263 && d != NULL
6264 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
4c58e0d8
AM
6265 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6266 || !bfd_hide_sym_by_version (info->version_info,
6267 eh->elf.root.root.string)))))
64d03ab5
AM
6268 {
6269 asection *code_sec;
b31867b6 6270 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6271
6272 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6273
6274 /* Function descriptor syms cause the associated
6275 function code sym section to be marked. */
b31867b6
AM
6276 fh = defined_code_entry (eh);
6277 if (fh != NULL)
6278 {
6279 code_sec = fh->elf.root.u.def.section;
6280 code_sec->flags |= SEC_KEEP;
6281 }
64d03ab5
AM
6282 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6283 && opd_entry_value (eh->elf.root.u.def.section,
6284 eh->elf.root.u.def.value,
aef36ac1 6285 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6286 code_sec->flags |= SEC_KEEP;
6287 }
6288
6289 return TRUE;
6290}
6291
5bd4f169
AM
6292/* Return the section that should be marked against GC for a given
6293 relocation. */
6294
6295static asection *
4ce794b7 6296ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6297 struct bfd_link_info *info,
4ce794b7
AM
6298 Elf_Internal_Rela *rel,
6299 struct elf_link_hash_entry *h,
6300 Elf_Internal_Sym *sym)
5bd4f169 6301{
ccfa59ea
AM
6302 asection *rsec;
6303
ccfa59ea
AM
6304 /* Syms return NULL if we're marking .opd, so we avoid marking all
6305 function sections, as all functions are referenced in .opd. */
6306 rsec = NULL;
6307 if (get_opd_info (sec) != NULL)
6308 return rsec;
1e2f5b6e 6309
5bd4f169
AM
6310 if (h != NULL)
6311 {
04c9666a 6312 enum elf_ppc64_reloc_type r_type;
b31867b6 6313 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6314
4ce794b7 6315 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6316 switch (r_type)
5bd4f169
AM
6317 {
6318 case R_PPC64_GNU_VTINHERIT:
6319 case R_PPC64_GNU_VTENTRY:
6320 break;
6321
6322 default:
6323 switch (h->root.type)
6324 {
6325 case bfd_link_hash_defined:
6326 case bfd_link_hash_defweak:
ccfa59ea 6327 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6328 fdh = defined_func_desc (eh);
6329 if (fdh != NULL)
6330 eh = fdh;
1e2f5b6e
AM
6331
6332 /* Function descriptor syms cause the associated
6333 function code sym section to be marked. */
b31867b6
AM
6334 fh = defined_code_entry (eh);
6335 if (fh != NULL)
ccfa59ea
AM
6336 {
6337 /* They also mark their opd section. */
74f0fb50 6338 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6339
b31867b6 6340 rsec = fh->elf.root.u.def.section;
ccfa59ea 6341 }
8387904d
AM
6342 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6343 && opd_entry_value (eh->elf.root.u.def.section,
6344 eh->elf.root.u.def.value,
aef36ac1 6345 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6346 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6347 else
1e2f5b6e
AM
6348 rsec = h->root.u.def.section;
6349 break;
5bd4f169
AM
6350
6351 case bfd_link_hash_common:
1e2f5b6e
AM
6352 rsec = h->root.u.c.p->section;
6353 break;
5bd4f169
AM
6354
6355 default:
fb34365b 6356 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6357 }
6358 }
6359 }
6360 else
6361 {
74f0fb50 6362 struct _opd_sec_data *opd;
1e2f5b6e
AM
6363
6364 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6365 opd = get_opd_info (rsec);
6366 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6367 {
74f0fb50 6368 rsec->gc_mark = 1;
ccfa59ea 6369
51aecdc5 6370 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6371 }
5bd4f169
AM
6372 }
6373
1e2f5b6e 6374 return rsec;
5bd4f169
AM
6375}
6376
65f38f15
AM
6377/* Update the .got, .plt. and dynamic reloc reference counts for the
6378 section being removed. */
5bd4f169 6379
b34976b6 6380static bfd_boolean
4ce794b7
AM
6381ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6382 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6383{
411e1bfb 6384 struct ppc_link_hash_table *htab;
5bd4f169
AM
6385 Elf_Internal_Shdr *symtab_hdr;
6386 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6387 struct got_entry **local_got_ents;
5bd4f169 6388 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6389
7dda2462
TG
6390 if (info->relocatable)
6391 return TRUE;
6392
680a3378
AM
6393 if ((sec->flags & SEC_ALLOC) == 0)
6394 return TRUE;
6395
ec338859
AM
6396 elf_section_data (sec)->local_dynrel = NULL;
6397
411e1bfb 6398 htab = ppc_hash_table (info);
4dfe6ac6
NC
6399 if (htab == NULL)
6400 return FALSE;
6401
0ffa91dd 6402 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6403 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6404 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6405
6406 relend = relocs + sec->reloc_count;
6407 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6408 {
6409 unsigned long r_symndx;
04c9666a 6410 enum elf_ppc64_reloc_type r_type;
58ac9f71 6411 struct elf_link_hash_entry *h = NULL;
f961d9dd 6412 unsigned char tls_type = 0;
5bd4f169 6413
a33d1f77 6414 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6415 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6416 if (r_symndx >= symtab_hdr->sh_info)
6417 {
6418 struct ppc_link_hash_entry *eh;
6061a67d
AM
6419 struct elf_dyn_relocs **pp;
6420 struct elf_dyn_relocs *p;
58ac9f71
AM
6421
6422 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6423 h = elf_follow_link (h);
58ac9f71
AM
6424 eh = (struct ppc_link_hash_entry *) h;
6425
6426 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6427 if (p->sec == sec)
6428 {
6429 /* Everything must go for SEC. */
6430 *pp = p->next;
6431 break;
6432 }
6433 }
6434
e054468f
AM
6435 if (is_branch_reloc (r_type))
6436 {
6437 struct plt_entry **ifunc = NULL;
6438 if (h != NULL)
6439 {
6440 if (h->type == STT_GNU_IFUNC)
6441 ifunc = &h->plt.plist;
6442 }
6443 else if (local_got_ents != NULL)
6444 {
6445 struct plt_entry **local_plt = (struct plt_entry **)
6446 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 6447 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
6448 (local_plt + symtab_hdr->sh_info);
6449 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6450 ifunc = local_plt + r_symndx;
6451 }
6452 if (ifunc != NULL)
6453 {
6454 struct plt_entry *ent;
6455
6456 for (ent = *ifunc; ent != NULL; ent = ent->next)
6457 if (ent->addend == rel->r_addend)
6458 break;
6459 if (ent == NULL)
6460 abort ();
6461 if (ent->plt.refcount > 0)
6462 ent->plt.refcount -= 1;
6463 continue;
6464 }
6465 }
6466
a33d1f77
AM
6467 switch (r_type)
6468 {
411e1bfb
AM
6469 case R_PPC64_GOT_TLSLD16:
6470 case R_PPC64_GOT_TLSLD16_LO:
6471 case R_PPC64_GOT_TLSLD16_HI:
6472 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6473 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6474 goto dogot;
6475
6476 case R_PPC64_GOT_TLSGD16:
6477 case R_PPC64_GOT_TLSGD16_LO:
6478 case R_PPC64_GOT_TLSGD16_HI:
6479 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6480 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6481 goto dogot;
6482
6483 case R_PPC64_GOT_TPREL16_DS:
6484 case R_PPC64_GOT_TPREL16_LO_DS:
6485 case R_PPC64_GOT_TPREL16_HI:
6486 case R_PPC64_GOT_TPREL16_HA:
6487 tls_type = TLS_TLS | TLS_TPREL;
6488 goto dogot;
6489
6490 case R_PPC64_GOT_DTPREL16_DS:
6491 case R_PPC64_GOT_DTPREL16_LO_DS:
6492 case R_PPC64_GOT_DTPREL16_HI:
6493 case R_PPC64_GOT_DTPREL16_HA:
6494 tls_type = TLS_TLS | TLS_DTPREL;
6495 goto dogot;
6496
a33d1f77
AM
6497 case R_PPC64_GOT16:
6498 case R_PPC64_GOT16_DS:
6499 case R_PPC64_GOT16_HA:
6500 case R_PPC64_GOT16_HI:
6501 case R_PPC64_GOT16_LO:
6502 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6503 dogot:
6504 {
6505 struct got_entry *ent;
6506
58ac9f71
AM
6507 if (h != NULL)
6508 ent = h->got.glist;
411e1bfb
AM
6509 else
6510 ent = local_got_ents[r_symndx];
6511
6512 for (; ent != NULL; ent = ent->next)
6513 if (ent->addend == rel->r_addend
e717da7e 6514 && ent->owner == abfd
411e1bfb
AM
6515 && ent->tls_type == tls_type)
6516 break;
6517 if (ent == NULL)
6518 abort ();
6519 if (ent->got.refcount > 0)
6520 ent->got.refcount -= 1;
6521 }
a33d1f77 6522 break;
65f38f15 6523
a33d1f77
AM
6524 case R_PPC64_PLT16_HA:
6525 case R_PPC64_PLT16_HI:
6526 case R_PPC64_PLT16_LO:
6527 case R_PPC64_PLT32:
6528 case R_PPC64_PLT64:
721956f4
AM
6529 case R_PPC64_REL14:
6530 case R_PPC64_REL14_BRNTAKEN:
6531 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6532 case R_PPC64_REL24:
58ac9f71 6533 if (h != NULL)
5d1634d7 6534 {
411e1bfb
AM
6535 struct plt_entry *ent;
6536
411e1bfb
AM
6537 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6538 if (ent->addend == rel->r_addend)
6539 break;
7c6c1722 6540 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6541 ent->plt.refcount -= 1;
5d1634d7 6542 }
e86ce104 6543 break;
5d1634d7 6544
a33d1f77
AM
6545 default:
6546 break;
6547 }
6548 }
b34976b6 6549 return TRUE;
5bd4f169
AM
6550}
6551
deb0e272
AM
6552/* The maximum size of .sfpr. */
6553#define SFPR_MAX (218*4)
6554
6555struct sfpr_def_parms
6556{
699733f6
AM
6557 const char name[12];
6558 unsigned char lo, hi;
deb0e272
AM
6559 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6560 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6561};
6562
6563/* Auto-generate _save*, _rest* functions in .sfpr. */
6564
4dfe6ac6 6565static bfd_boolean
deb0e272
AM
6566sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6567{
6568 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6569 unsigned int i;
6570 size_t len = strlen (parm->name);
6571 bfd_boolean writing = FALSE;
699733f6 6572 char sym[16];
deb0e272 6573
4dfe6ac6
NC
6574 if (htab == NULL)
6575 return FALSE;
6576
deb0e272
AM
6577 memcpy (sym, parm->name, len);
6578 sym[len + 2] = 0;
6579
6580 for (i = parm->lo; i <= parm->hi; i++)
6581 {
6582 struct elf_link_hash_entry *h;
6583
6584 sym[len + 0] = i / 10 + '0';
6585 sym[len + 1] = i % 10 + '0';
6586 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6587 if (h != NULL
f5385ebf 6588 && !h->def_regular)
deb0e272
AM
6589 {
6590 h->root.type = bfd_link_hash_defined;
6591 h->root.u.def.section = htab->sfpr;
6592 h->root.u.def.value = htab->sfpr->size;
6593 h->type = STT_FUNC;
f5385ebf 6594 h->def_regular = 1;
deb0e272
AM
6595 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6596 writing = TRUE;
6597 if (htab->sfpr->contents == NULL)
6598 {
6599 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6600 if (htab->sfpr->contents == NULL)
6601 return FALSE;
6602 }
6603 }
6604 if (writing)
6605 {
6606 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6607 if (i != parm->hi)
6608 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6609 else
6610 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6611 htab->sfpr->size = p - htab->sfpr->contents;
6612 }
6613 }
6614
6615 return TRUE;
6616}
6617
6618static bfd_byte *
6619savegpr0 (bfd *abfd, bfd_byte *p, int r)
6620{
6621 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6622 return p + 4;
6623}
6624
6625static bfd_byte *
6626savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6627{
6628 p = savegpr0 (abfd, p, r);
a078d95a 6629 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6630 p = p + 4;
6631 bfd_put_32 (abfd, BLR, p);
6632 return p + 4;
6633}
6634
6635static bfd_byte *
6636restgpr0 (bfd *abfd, bfd_byte *p, int r)
6637{
6638 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6639 return p + 4;
6640}
6641
6642static bfd_byte *
6643restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6644{
a078d95a 6645 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6646 p = p + 4;
6647 p = restgpr0 (abfd, p, r);
6648 bfd_put_32 (abfd, MTLR_R0, p);
6649 p = p + 4;
6650 if (r == 29)
6651 {
6652 p = restgpr0 (abfd, p, 30);
6653 p = restgpr0 (abfd, p, 31);
6654 }
6655 bfd_put_32 (abfd, BLR, p);
6656 return p + 4;
6657}
6658
6659static bfd_byte *
6660savegpr1 (bfd *abfd, bfd_byte *p, int r)
6661{
6662 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6663 return p + 4;
6664}
6665
6666static bfd_byte *
6667savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6668{
6669 p = savegpr1 (abfd, p, r);
6670 bfd_put_32 (abfd, BLR, p);
6671 return p + 4;
6672}
6673
6674static bfd_byte *
6675restgpr1 (bfd *abfd, bfd_byte *p, int r)
6676{
6677 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6678 return p + 4;
6679}
6680
6681static bfd_byte *
6682restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6683{
6684 p = restgpr1 (abfd, p, r);
6685 bfd_put_32 (abfd, BLR, p);
6686 return p + 4;
6687}
6688
6689static bfd_byte *
6690savefpr (bfd *abfd, bfd_byte *p, int r)
6691{
6692 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6693 return p + 4;
6694}
6695
6696static bfd_byte *
6697savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6698{
6699 p = savefpr (abfd, p, r);
a078d95a 6700 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6701 p = p + 4;
6702 bfd_put_32 (abfd, BLR, p);
6703 return p + 4;
6704}
6705
6706static bfd_byte *
6707restfpr (bfd *abfd, bfd_byte *p, int r)
6708{
6709 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6710 return p + 4;
6711}
6712
6713static bfd_byte *
6714restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6715{
a078d95a 6716 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6717 p = p + 4;
6718 p = restfpr (abfd, p, r);
6719 bfd_put_32 (abfd, MTLR_R0, p);
6720 p = p + 4;
6721 if (r == 29)
6722 {
6723 p = restfpr (abfd, p, 30);
6724 p = restfpr (abfd, p, 31);
6725 }
6726 bfd_put_32 (abfd, BLR, p);
6727 return p + 4;
6728}
6729
6730static bfd_byte *
6731savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6732{
6733 p = savefpr (abfd, p, r);
6734 bfd_put_32 (abfd, BLR, p);
6735 return p + 4;
6736}
6737
6738static bfd_byte *
6739restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6740{
6741 p = restfpr (abfd, p, r);
6742 bfd_put_32 (abfd, BLR, p);
6743 return p + 4;
6744}
6745
6746static bfd_byte *
6747savevr (bfd *abfd, bfd_byte *p, int r)
6748{
6749 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6750 p = p + 4;
6751 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6752 return p + 4;
6753}
6754
6755static bfd_byte *
6756savevr_tail (bfd *abfd, bfd_byte *p, int r)
6757{
6758 p = savevr (abfd, p, r);
6759 bfd_put_32 (abfd, BLR, p);
6760 return p + 4;
6761}
6762
6763static bfd_byte *
6764restvr (bfd *abfd, bfd_byte *p, int r)
6765{
6766 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6767 p = p + 4;
6768 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6769 return p + 4;
6770}
6771
6772static bfd_byte *
6773restvr_tail (bfd *abfd, bfd_byte *p, int r)
6774{
6775 p = restvr (abfd, p, r);
6776 bfd_put_32 (abfd, BLR, p);
6777 return p + 4;
6778}
6779
e86ce104
AM
6780/* Called via elf_link_hash_traverse to transfer dynamic linking
6781 information on function code symbol entries to their corresponding
6782 function descriptor symbol entries. */
deb0e272 6783
b34976b6 6784static bfd_boolean
4ce794b7 6785func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6786{
e86ce104 6787 struct bfd_link_info *info;
65f38f15 6788 struct ppc_link_hash_table *htab;
411e1bfb 6789 struct plt_entry *ent;
50bc7936
AM
6790 struct ppc_link_hash_entry *fh;
6791 struct ppc_link_hash_entry *fdh;
6792 bfd_boolean force_local;
5bd4f169 6793
50bc7936
AM
6794 fh = (struct ppc_link_hash_entry *) h;
6795 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6796 return TRUE;
e86ce104 6797
4ce794b7 6798 info = inf;
65f38f15 6799 htab = ppc_hash_table (info);
4dfe6ac6
NC
6800 if (htab == NULL)
6801 return FALSE;
5bd4f169 6802
c09bdfe5
AM
6803 /* Resolve undefined references to dot-symbols as the value
6804 in the function descriptor, if we have one in a regular object.
6805 This is to satisfy cases like ".quad .foo". Calls to functions
6806 in dynamic objects are handled elsewhere. */
6807 if (fh->elf.root.type == bfd_link_hash_undefweak
6808 && fh->was_undefined
b31867b6
AM
6809 && (fdh = defined_func_desc (fh)) != NULL
6810 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6811 && opd_entry_value (fdh->elf.root.u.def.section,
6812 fdh->elf.root.u.def.value,
c09bdfe5 6813 &fh->elf.root.u.def.section,
aef36ac1 6814 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6815 {
b31867b6 6816 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6817 fh->elf.forced_local = 1;
b31867b6
AM
6818 fh->elf.def_regular = fdh->elf.def_regular;
6819 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6820 }
6821
e86ce104
AM
6822 /* If this is a function code symbol, transfer dynamic linking
6823 information to the function descriptor symbol. */
50bc7936 6824 if (!fh->is_func)
b34976b6 6825 return TRUE;
e86ce104 6826
50bc7936 6827 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6828 if (ent->plt.refcount > 0)
6829 break;
50bc7936
AM
6830 if (ent == NULL
6831 || fh->elf.root.root.string[0] != '.'
6832 || fh->elf.root.root.string[1] == '\0')
6833 return TRUE;
5bd4f169 6834
50bc7936
AM
6835 /* Find the corresponding function descriptor symbol. Create it
6836 as undefined if necessary. */
5bd4f169 6837
b31867b6 6838 fdh = lookup_fdh (fh, htab);
50bc7936 6839 if (fdh == NULL
df131623 6840 && !info->executable
50bc7936
AM
6841 && (fh->elf.root.type == bfd_link_hash_undefined
6842 || fh->elf.root.type == bfd_link_hash_undefweak))
6843 {
908b32fc 6844 fdh = make_fdh (info, fh);
bb700d78
AM
6845 if (fdh == NULL)
6846 return FALSE;
50bc7936 6847 }
648cca2c 6848
908b32fc 6849 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6850 code symbol is strong undefined, make the fake sym the same.
6851 If the function code symbol is defined, then force the fake
6852 descriptor local; We can't support overriding of symbols in a
6853 shared library on a fake descriptor. */
908b32fc
AM
6854
6855 if (fdh != NULL
6856 && fdh->fake
433817dd 6857 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6858 {
433817dd
AM
6859 if (fh->elf.root.type == bfd_link_hash_undefined)
6860 {
6861 fdh->elf.root.type = bfd_link_hash_undefined;
6862 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6863 }
6864 else if (fh->elf.root.type == bfd_link_hash_defined
6865 || fh->elf.root.type == bfd_link_hash_defweak)
6866 {
6867 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6868 }
908b32fc
AM
6869 }
6870
50bc7936 6871 if (fdh != NULL
f5385ebf 6872 && !fdh->elf.forced_local
df131623 6873 && (!info->executable
f5385ebf
AM
6874 || fdh->elf.def_dynamic
6875 || fdh->elf.ref_dynamic
50bc7936
AM
6876 || (fdh->elf.root.type == bfd_link_hash_undefweak
6877 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6878 {
6879 if (fdh->elf.dynindx == -1)
c152c796 6880 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6881 return FALSE;
f5385ebf
AM
6882 fdh->elf.ref_regular |= fh->elf.ref_regular;
6883 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6884 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6885 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6886 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6887 {
40d16e0b 6888 move_plt_plist (fh, fdh);
f5385ebf 6889 fdh->elf.needs_plt = 1;
e86ce104 6890 }
50bc7936 6891 fdh->is_func_descriptor = 1;
34814b9f
AM
6892 fdh->oh = fh;
6893 fh->oh = fdh;
e86ce104
AM
6894 }
6895
50bc7936
AM
6896 /* Now that the info is on the function descriptor, clear the
6897 function code sym info. Any function code syms for which we
6898 don't have a definition in a regular file, we force local.
6899 This prevents a shared library from exporting syms that have
6900 been imported from another library. Function code syms that
6901 are really in the library we must leave global to prevent the
6902 linker dragging in a definition from a static library. */
93f3fa99
AM
6903 force_local = (!fh->elf.def_regular
6904 || fdh == NULL
6905 || !fdh->elf.def_regular
6906 || fdh->elf.forced_local);
50bc7936
AM
6907 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6908
b34976b6 6909 return TRUE;
e86ce104 6910}
40b8271b 6911
e86ce104 6912/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6913 this hook to a) provide some gcc support functions, and b) transfer
6914 dynamic linking information gathered so far on function code symbol
6915 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6916
b34976b6 6917static bfd_boolean
4ce794b7
AM
6918ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6919 struct bfd_link_info *info)
e86ce104
AM
6920{
6921 struct ppc_link_hash_table *htab;
82bd7b59 6922 unsigned int i;
27fc25a1 6923 static const struct sfpr_def_parms funcs[] =
deb0e272
AM
6924 {
6925 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6926 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6927 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6928 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6929 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6930 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6931 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6932 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6933 { "._savef", 14, 31, savefpr, savefpr1_tail },
6934 { "._restf", 14, 31, restfpr, restfpr1_tail },
6935 { "_savevr_", 20, 31, savevr, savevr_tail },
6936 { "_restvr_", 20, 31, restvr, restvr_tail }
6937 };
e86ce104
AM
6938
6939 htab = ppc_hash_table (info);
4dfe6ac6
NC
6940 if (htab == NULL)
6941 return FALSE;
6942
5295321c
AM
6943 if (!info->relocatable
6944 && htab->elf.hgot != NULL)
dba6fa9b
AM
6945 {
6946 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6947 /* Make .TOC. defined so as to prevent it being made dynamic.
6948 The wrong value here is fixed later in ppc64_elf_set_toc. */
6949 htab->elf.hgot->type = STT_OBJECT;
6950 htab->elf.hgot->root.type = bfd_link_hash_defined;
6951 htab->elf.hgot->root.u.def.value = 0;
6952 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6953 htab->elf.hgot->def_regular = 1;
6954 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
6955 | STV_HIDDEN);
6956 }
c66bb0ee 6957
82bd7b59
AM
6958 if (htab->sfpr == NULL)
6959 /* We don't have any relocs. */
b34976b6 6960 return TRUE;
82bd7b59 6961
deb0e272
AM
6962 /* Provide any missing _save* and _rest* functions. */
6963 htab->sfpr->size = 0;
7d4c687d 6964 if (htab->params->save_restore_funcs)
27fc25a1
AM
6965 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6966 if (!sfpr_define (info, &funcs[i]))
6967 return FALSE;
82bd7b59 6968
4ce794b7 6969 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6970
eea6121a 6971 if (htab->sfpr->size == 0)
8423293d 6972 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6973
b34976b6 6974 return TRUE;
e86ce104
AM
6975}
6976
a345bc8d
AM
6977/* Return true if we have dynamic relocs that apply to read-only sections. */
6978
6979static bfd_boolean
6980readonly_dynrelocs (struct elf_link_hash_entry *h)
6981{
6982 struct ppc_link_hash_entry *eh;
6983 struct elf_dyn_relocs *p;
6984
6985 eh = (struct ppc_link_hash_entry *) h;
6986 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6987 {
6988 asection *s = p->sec->output_section;
6989
6990 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6991 return TRUE;
6992 }
6993 return FALSE;
6994}
6995
e86ce104
AM
6996/* Adjust a symbol defined by a dynamic object and referenced by a
6997 regular object. The current definition is in some section of the
6998 dynamic object, but we're not including those sections. We have to
6999 change the definition to something the rest of the link can
7000 understand. */
7001
b34976b6 7002static bfd_boolean
4ce794b7
AM
7003ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7004 struct elf_link_hash_entry *h)
e86ce104
AM
7005{
7006 struct ppc_link_hash_table *htab;
e86ce104 7007 asection *s;
e86ce104
AM
7008
7009 htab = ppc_hash_table (info);
4dfe6ac6
NC
7010 if (htab == NULL)
7011 return FALSE;
e86ce104
AM
7012
7013 /* Deal with function syms. */
7014 if (h->type == STT_FUNC
e054468f 7015 || h->type == STT_GNU_IFUNC
f5385ebf 7016 || h->needs_plt)
e86ce104
AM
7017 {
7018 /* Clear procedure linkage table information for any symbol that
7019 won't need a .plt entry. */
411e1bfb
AM
7020 struct plt_entry *ent;
7021 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7022 if (ent->plt.refcount > 0)
7023 break;
8387904d 7024 if (ent == NULL
e054468f
AM
7025 || (h->type != STT_GNU_IFUNC
7026 && (SYMBOL_CALLS_LOCAL (info, h)
7027 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7028 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 7029 {
411e1bfb 7030 h->plt.plist = NULL;
f5385ebf 7031 h->needs_plt = 0;
d1eca1e4 7032 h->pointer_equality_needed = 0;
40b8271b 7033 }
a345bc8d
AM
7034 else if (abiversion (info->output_bfd) == 2)
7035 {
d1eca1e4
AM
7036 /* Taking a function's address in a read/write section
7037 doesn't require us to define the function symbol in the
7038 executable on a global entry stub. A dynamic reloc can
7039 be used instead. */
7040 if (h->pointer_equality_needed
1009ef28 7041 && h->type != STT_GNU_IFUNC
d1eca1e4
AM
7042 && !readonly_dynrelocs (h))
7043 {
7044 h->pointer_equality_needed = 0;
7045 h->non_got_ref = 0;
7046 }
7047
a345bc8d
AM
7048 /* After adjust_dynamic_symbol, non_got_ref set in the
7049 non-shared case means that we have allocated space in
7050 .dynbss for the symbol and thus dyn_relocs for this
7051 symbol should be discarded.
7052 If we get here we know we are making a PLT entry for this
7053 symbol, and in an executable we'd normally resolve
7054 relocations against this symbol to the PLT entry. Allow
7055 dynamic relocs if the reference is weak, and the dynamic
7056 relocs will not cause text relocation. */
d1eca1e4
AM
7057 else if (!h->ref_regular_nonweak
7058 && h->non_got_ref
7059 && h->type != STT_GNU_IFUNC
7060 && !readonly_dynrelocs (h))
a345bc8d
AM
7061 h->non_got_ref = 0;
7062
7063 /* If making a plt entry, then we don't need copy relocs. */
7064 return TRUE;
7065 }
5bd4f169 7066 }
bbd7ec4a 7067 else
411e1bfb 7068 h->plt.plist = NULL;
5bd4f169
AM
7069
7070 /* If this is a weak symbol, and there is a real definition, the
7071 processor independent code will have arranged for us to see the
7072 real definition first, and we can just use the same value. */
f6e332e6 7073 if (h->u.weakdef != NULL)
5bd4f169 7074 {
f6e332e6
AM
7075 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7076 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7077 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7078 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7079 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7080 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7081 return TRUE;
5bd4f169
AM
7082 }
7083
5bd4f169
AM
7084 /* If we are creating a shared library, we must presume that the
7085 only references to the symbol are via the global offset table.
7086 For such cases we need not do anything here; the relocations will
7087 be handled correctly by relocate_section. */
7088 if (info->shared)
b34976b6 7089 return TRUE;
5bd4f169 7090
65f38f15
AM
7091 /* If there are no references to this symbol that do not use the
7092 GOT, we don't need to generate a copy reloc. */
f5385ebf 7093 if (!h->non_got_ref)
b34976b6 7094 return TRUE;
65f38f15 7095
b186458a
JJ
7096 /* Don't generate a copy reloc for symbols defined in the executable. */
7097 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
7098 return TRUE;
7099
a127494f
AM
7100 /* If -z nocopyreloc was given, don't generate them either. */
7101 if (info->nocopyreloc)
7102 {
7103 h->non_got_ref = 0;
7104 return TRUE;
7105 }
7106
a345bc8d
AM
7107 /* If we didn't find any dynamic relocs in read-only sections, then
7108 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7109 if (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
65f38f15 7110 {
a345bc8d
AM
7111 h->non_got_ref = 0;
7112 return TRUE;
65f38f15
AM
7113 }
7114
a127494f
AM
7115 /* Protected variables do not work with .dynbss. The copy in
7116 .dynbss won't be used by the shared library with the protected
7117 definition for the variable. Text relocations are preferable
7118 to an incorrect program. */
7119 if (h->protected_def)
7120 {
7121 h->non_got_ref = 0;
7122 return TRUE;
7123 }
7124
5d35169e 7125 if (h->plt.plist != NULL)
97b639ba
AM
7126 {
7127 /* We should never get here, but unfortunately there are versions
7128 of gcc out there that improperly (for this ABI) put initialized
7129 function pointers, vtable refs and suchlike in read-only
7130 sections. Allow them to proceed, but warn that this might
7131 break at runtime. */
25f53a85 7132 info->callbacks->einfo
bc30df16 7133 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7134 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7135 h->root.root.string);
7136 }
5d35169e
AM
7137
7138 /* This is a reference to a symbol defined by a dynamic object which
7139 is not a function. */
7140
5bd4f169
AM
7141 /* We must allocate the symbol in our .dynbss section, which will
7142 become part of the .bss section of the executable. There will be
7143 an entry for this symbol in the .dynsym section. The dynamic
7144 object will contain position independent code, so all references
7145 from the dynamic object to this symbol will go through the global
7146 offset table. The dynamic linker will use the .dynsym entry to
7147 determine the address it must put in the global offset table, so
7148 both the dynamic object and the regular object will refer to the
7149 same memory location for the variable. */
5bd4f169 7150
04c9666a
AM
7151 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7152 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7153 runtime process image. We need to remember the offset into the
7154 .rela.bss section we are going to use. */
1d7e9d18 7155 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7156 {
eea6121a 7157 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 7158 h->needs_copy = 1;
5bd4f169
AM
7159 }
7160
4ce794b7 7161 s = htab->dynbss;
5bd4f169 7162
6cabe1ea 7163 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7164}
7165
e86ce104
AM
7166/* If given a function descriptor symbol, hide both the function code
7167 sym and the descriptor. */
7168static void
4ce794b7
AM
7169ppc64_elf_hide_symbol (struct bfd_link_info *info,
7170 struct elf_link_hash_entry *h,
7171 bfd_boolean force_local)
e86ce104 7172{
34814b9f 7173 struct ppc_link_hash_entry *eh;
e86ce104
AM
7174 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7175
34814b9f
AM
7176 eh = (struct ppc_link_hash_entry *) h;
7177 if (eh->is_func_descriptor)
e86ce104 7178 {
34814b9f 7179 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7180
721956f4 7181 if (fh == NULL)
d1329ca3
AM
7182 {
7183 const char *p, *q;
7184 struct ppc_link_hash_table *htab;
7185 char save;
7186
7187 /* We aren't supposed to use alloca in BFD because on
7188 systems which do not have alloca the version in libiberty
7189 calls xmalloc, which might cause the program to crash
7190 when it runs out of memory. This function doesn't have a
7191 return status, so there's no way to gracefully return an
7192 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7193 accessed; It's either a string in an ELF string table,
7194 or allocated in an objalloc structure. */
d1329ca3 7195
34814b9f 7196 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7197 save = *p;
7198 *(char *) p = '.';
7199 htab = ppc_hash_table (info);
4dfe6ac6
NC
7200 if (htab == NULL)
7201 return;
7202
34814b9f
AM
7203 fh = (struct ppc_link_hash_entry *)
7204 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7205 *(char *) p = save;
7206
7207 /* Unfortunately, if it so happens that the string we were
7208 looking for was allocated immediately before this string,
7209 then we overwrote the string terminator. That's the only
7210 reason the lookup should fail. */
7211 if (fh == NULL)
7212 {
34814b9f
AM
7213 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7214 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7215 --q, --p;
34814b9f
AM
7216 if (q < eh->elf.root.root.string && *p == '.')
7217 fh = (struct ppc_link_hash_entry *)
7218 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7219 }
7220 if (fh != NULL)
7221 {
34814b9f
AM
7222 eh->oh = fh;
7223 fh->oh = eh;
d1329ca3
AM
7224 }
7225 }
e86ce104 7226 if (fh != NULL)
34814b9f 7227 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7228 }
7229}
7230
411e1bfb 7231static bfd_boolean
8843416a
AM
7232get_sym_h (struct elf_link_hash_entry **hp,
7233 Elf_Internal_Sym **symp,
7234 asection **symsecp,
f961d9dd 7235 unsigned char **tls_maskp,
8843416a
AM
7236 Elf_Internal_Sym **locsymsp,
7237 unsigned long r_symndx,
7238 bfd *ibfd)
411e1bfb 7239{
0ffa91dd 7240 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7241
7242 if (r_symndx >= symtab_hdr->sh_info)
7243 {
7244 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7245 struct elf_link_hash_entry *h;
7246
7247 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7248 h = elf_follow_link (h);
411e1bfb
AM
7249
7250 if (hp != NULL)
7251 *hp = h;
7252
7253 if (symp != NULL)
7254 *symp = NULL;
7255
7256 if (symsecp != NULL)
7257 {
7258 asection *symsec = NULL;
7259 if (h->root.type == bfd_link_hash_defined
7260 || h->root.type == bfd_link_hash_defweak)
7261 symsec = h->root.u.def.section;
7262 *symsecp = symsec;
7263 }
7264
e7b938ca 7265 if (tls_maskp != NULL)
411e1bfb
AM
7266 {
7267 struct ppc_link_hash_entry *eh;
7268
7269 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7270 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7271 }
7272 }
7273 else
7274 {
7275 Elf_Internal_Sym *sym;
7276 Elf_Internal_Sym *locsyms = *locsymsp;
7277
7278 if (locsyms == NULL)
7279 {
7280 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7281 if (locsyms == NULL)
7282 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7283 symtab_hdr->sh_info,
7284 0, NULL, NULL, NULL);
7285 if (locsyms == NULL)
7286 return FALSE;
7287 *locsymsp = locsyms;
7288 }
7289 sym = locsyms + r_symndx;
7290
7291 if (hp != NULL)
7292 *hp = NULL;
7293
7294 if (symp != NULL)
7295 *symp = sym;
7296
7297 if (symsecp != NULL)
cb33740c 7298 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7299
e7b938ca 7300 if (tls_maskp != NULL)
411e1bfb
AM
7301 {
7302 struct got_entry **lgot_ents;
f961d9dd 7303 unsigned char *tls_mask;
411e1bfb 7304
e7b938ca 7305 tls_mask = NULL;
411e1bfb
AM
7306 lgot_ents = elf_local_got_ents (ibfd);
7307 if (lgot_ents != NULL)
7308 {
e054468f
AM
7309 struct plt_entry **local_plt = (struct plt_entry **)
7310 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7311 unsigned char *lgot_masks = (unsigned char *)
e054468f 7312 (local_plt + symtab_hdr->sh_info);
e7b938ca 7313 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7314 }
e7b938ca 7315 *tls_maskp = tls_mask;
411e1bfb
AM
7316 }
7317 }
7318 return TRUE;
7319}
7320
e7b938ca 7321/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7322 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7323 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7324
7325static int
f961d9dd 7326get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7327 unsigned long *toc_symndx,
7328 bfd_vma *toc_addend,
0d4792f7 7329 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7330 const Elf_Internal_Rela *rel,
7331 bfd *ibfd)
411e1bfb
AM
7332{
7333 unsigned long r_symndx;
0d4792f7 7334 int next_r;
411e1bfb
AM
7335 struct elf_link_hash_entry *h;
7336 Elf_Internal_Sym *sym;
7337 asection *sec;
7338 bfd_vma off;
7339
7340 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7341 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7342 return 0;
411e1bfb 7343
e7b938ca 7344 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7345 || sec == NULL
6bee8834 7346 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7347 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7348 return 1;
411e1bfb
AM
7349
7350 /* Look inside a TOC section too. */
7351 if (h != NULL)
7352 {
7353 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7354 off = h->root.u.def.value;
7355 }
7356 else
7357 off = sym->st_value;
7358 off += rel->r_addend;
7359 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7360 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7361 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7362 if (toc_symndx != NULL)
7363 *toc_symndx = r_symndx;
3a71aa26
AM
7364 if (toc_addend != NULL)
7365 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7366 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7367 return 0;
854b41e7 7368 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7369 && (next_r == -1 || next_r == -2))
7370 return 1 - next_r;
951fd09b 7371 return 1;
411e1bfb
AM
7372}
7373
3b421ab3
AM
7374/* Find (or create) an entry in the tocsave hash table. */
7375
7376static struct tocsave_entry *
7377tocsave_find (struct ppc_link_hash_table *htab,
7378 enum insert_option insert,
7379 Elf_Internal_Sym **local_syms,
7380 const Elf_Internal_Rela *irela,
7381 bfd *ibfd)
7382{
7383 unsigned long r_indx;
7384 struct elf_link_hash_entry *h;
7385 Elf_Internal_Sym *sym;
7386 struct tocsave_entry ent, *p;
7387 hashval_t hash;
7388 struct tocsave_entry **slot;
7389
7390 r_indx = ELF64_R_SYM (irela->r_info);
7391 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7392 return NULL;
7393 if (ent.sec == NULL || ent.sec->output_section == NULL)
7394 {
7395 (*_bfd_error_handler)
7396 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7397 return NULL;
7398 }
7399
7400 if (h != NULL)
7401 ent.offset = h->root.u.def.value;
7402 else
7403 ent.offset = sym->st_value;
7404 ent.offset += irela->r_addend;
7405
7406 hash = tocsave_htab_hash (&ent);
7407 slot = ((struct tocsave_entry **)
7408 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7409 if (slot == NULL)
7410 return NULL;
7411
7412 if (*slot == NULL)
7413 {
7414 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7415 if (p == NULL)
7416 return NULL;
7417 *p = ent;
7418 *slot = p;
7419 }
7420 return *slot;
7421}
7422
754021d0 7423/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7424 code for the old ABI, these will already have been done. */
754021d0
AM
7425
7426static bfd_boolean
7427adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7428{
7429 struct ppc_link_hash_entry *eh;
7430 asection *sym_sec;
74f0fb50 7431 struct _opd_sec_data *opd;
754021d0
AM
7432
7433 if (h->root.type == bfd_link_hash_indirect)
7434 return TRUE;
7435
754021d0
AM
7436 if (h->root.type != bfd_link_hash_defined
7437 && h->root.type != bfd_link_hash_defweak)
7438 return TRUE;
7439
7440 eh = (struct ppc_link_hash_entry *) h;
7441 if (eh->adjust_done)
7442 return TRUE;
7443
7444 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7445 opd = get_opd_info (sym_sec);
7446 if (opd != NULL && opd->adjust != NULL)
754021d0 7447 {
51aecdc5 7448 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7449 if (adjust == -1)
7450 {
7451 /* This entry has been deleted. */
b3fac117 7452 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7453 if (dsec == NULL)
7454 {
7455 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7456 if (discarded_section (dsec))
81688140 7457 {
b3fac117 7458 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7459 break;
7460 }
7461 }
4025353c 7462 eh->elf.root.u.def.value = 0;
81688140 7463 eh->elf.root.u.def.section = dsec;
4025353c
AM
7464 }
7465 else
7466 eh->elf.root.u.def.value += adjust;
754021d0
AM
7467 eh->adjust_done = 1;
7468 }
7469 return TRUE;
7470}
7471
8c1d1bb8 7472/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7473 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7474 have already been determined. */
7475
7476static bfd_boolean
7477dec_dynrel_count (bfd_vma r_info,
7478 asection *sec,
7479 struct bfd_link_info *info,
7480 Elf_Internal_Sym **local_syms,
7481 struct elf_link_hash_entry *h,
19e08130 7482 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7483{
7484 enum elf_ppc64_reloc_type r_type;
19e08130 7485 asection *sym_sec = NULL;
8c1d1bb8
AM
7486
7487 /* Can this reloc be dynamic? This switch, and later tests here
7488 should be kept in sync with the code in check_relocs. */
7489 r_type = ELF64_R_TYPE (r_info);
7490 switch (r_type)
7491 {
7492 default:
7493 return TRUE;
7494
7495 case R_PPC64_TPREL16:
7496 case R_PPC64_TPREL16_LO:
7497 case R_PPC64_TPREL16_HI:
7498 case R_PPC64_TPREL16_HA:
7499 case R_PPC64_TPREL16_DS:
7500 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7501 case R_PPC64_TPREL16_HIGH:
7502 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7503 case R_PPC64_TPREL16_HIGHER:
7504 case R_PPC64_TPREL16_HIGHERA:
7505 case R_PPC64_TPREL16_HIGHEST:
7506 case R_PPC64_TPREL16_HIGHESTA:
7507 if (!info->shared)
7508 return TRUE;
7509
7510 case R_PPC64_TPREL64:
7511 case R_PPC64_DTPMOD64:
7512 case R_PPC64_DTPREL64:
7513 case R_PPC64_ADDR64:
7514 case R_PPC64_REL30:
7515 case R_PPC64_REL32:
7516 case R_PPC64_REL64:
7517 case R_PPC64_ADDR14:
7518 case R_PPC64_ADDR14_BRNTAKEN:
7519 case R_PPC64_ADDR14_BRTAKEN:
7520 case R_PPC64_ADDR16:
7521 case R_PPC64_ADDR16_DS:
7522 case R_PPC64_ADDR16_HA:
7523 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7524 case R_PPC64_ADDR16_HIGH:
7525 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7526 case R_PPC64_ADDR16_HIGHER:
7527 case R_PPC64_ADDR16_HIGHERA:
7528 case R_PPC64_ADDR16_HIGHEST:
7529 case R_PPC64_ADDR16_HIGHESTA:
7530 case R_PPC64_ADDR16_LO:
7531 case R_PPC64_ADDR16_LO_DS:
7532 case R_PPC64_ADDR24:
7533 case R_PPC64_ADDR32:
7534 case R_PPC64_UADDR16:
7535 case R_PPC64_UADDR32:
7536 case R_PPC64_UADDR64:
7537 case R_PPC64_TOC:
7538 break;
7539 }
7540
7541 if (local_syms != NULL)
7542 {
7543 unsigned long r_symndx;
8c1d1bb8
AM
7544 bfd *ibfd = sec->owner;
7545
7546 r_symndx = ELF64_R_SYM (r_info);
7547 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7548 return FALSE;
7549 }
7550
7551 if ((info->shared
1d483afe 7552 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7553 || (h != NULL
198f1157 7554 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7555 || h->root.type == bfd_link_hash_defweak
7556 || !h->def_regular))))
7557 || (ELIMINATE_COPY_RELOCS
7558 && !info->shared
7559 && h != NULL
7560 && (h->root.type == bfd_link_hash_defweak
7561 || !h->def_regular)))
7562 ;
7563 else
7564 return TRUE;
7565
7566 if (h != NULL)
6edfbbad 7567 {
19e08130
AM
7568 struct elf_dyn_relocs *p;
7569 struct elf_dyn_relocs **pp;
7570 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7571
7572 /* elf_gc_sweep may have already removed all dyn relocs associated
7573 with local syms for a given section. Also, symbol flags are
7574 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7575 report a dynreloc miscount. */
7576 if (*pp == NULL && info->gc_sections)
7577 return TRUE;
7578
7579 while ((p = *pp) != NULL)
60124e18 7580 {
19e08130
AM
7581 if (p->sec == sec)
7582 {
7583 if (!must_be_dyn_reloc (info, r_type))
7584 p->pc_count -= 1;
7585 p->count -= 1;
7586 if (p->count == 0)
7587 *pp = p->next;
7588 return TRUE;
7589 }
7590 pp = &p->next;
60124e18 7591 }
6edfbbad 7592 }
19e08130
AM
7593 else
7594 {
7595 struct ppc_dyn_relocs *p;
7596 struct ppc_dyn_relocs **pp;
7597 void *vpp;
7598 bfd_boolean is_ifunc;
8c1d1bb8 7599
19e08130
AM
7600 if (local_syms == NULL)
7601 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7602 if (sym_sec == NULL)
7603 sym_sec = sec;
c57da1a7 7604
19e08130
AM
7605 vpp = &elf_section_data (sym_sec)->local_dynrel;
7606 pp = (struct ppc_dyn_relocs **) vpp;
7607
7608 if (*pp == NULL && info->gc_sections)
7609 return TRUE;
7610
7611 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7612 while ((p = *pp) != NULL)
8c1d1bb8 7613 {
19e08130
AM
7614 if (p->sec == sec && p->ifunc == is_ifunc)
7615 {
7616 p->count -= 1;
7617 if (p->count == 0)
7618 *pp = p->next;
7619 return TRUE;
7620 }
7621 pp = &p->next;
8c1d1bb8 7622 }
8c1d1bb8
AM
7623 }
7624
8de848d8 7625 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7626 sec->owner, sec);
8c1d1bb8
AM
7627 bfd_set_error (bfd_error_bad_value);
7628 return FALSE;
7629}
7630
754021d0
AM
7631/* Remove unused Official Procedure Descriptor entries. Currently we
7632 only remove those associated with functions in discarded link-once
7633 sections, or weakly defined functions that have been overridden. It
7634 would be possible to remove many more entries for statically linked
7635 applications. */
7636
b34976b6 7637bfd_boolean
e7d1c40c 7638ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7639{
7640 bfd *ibfd;
754021d0 7641 bfd_boolean some_edited = FALSE;
3f764659 7642 asection *need_pad = NULL;
e7d1c40c
AM
7643 struct ppc_link_hash_table *htab;
7644
7645 htab = ppc_hash_table (info);
7646 if (htab == NULL)
7647 return FALSE;
1e2f5b6e 7648
c72f2fb2 7649 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7650 {
7651 asection *sec;
7652 Elf_Internal_Rela *relstart, *rel, *relend;
7653 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7654 Elf_Internal_Sym *local_syms;
74f0fb50 7655 struct _opd_sec_data *opd;
51aecdc5 7656 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7657 bfd_size_type cnt_16b = 0;
1e2f5b6e 7658
854b41e7
AM
7659 if (!is_ppc64_elf (ibfd))
7660 continue;
7661
1e2f5b6e 7662 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7663 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7664 continue;
7665
dbaa2011 7666 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7667 continue;
7668
1e2f5b6e
AM
7669 if (sec->output_section == bfd_abs_section_ptr)
7670 continue;
7671
7672 /* Look through the section relocs. */
7673 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7674 continue;
7675
6cdc0ccc 7676 local_syms = NULL;
0ffa91dd 7677 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7678
7679 /* Read the relocations. */
4ce794b7 7680 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7681 info->keep_memory);
1e2f5b6e 7682 if (relstart == NULL)
b34976b6 7683 return FALSE;
1e2f5b6e
AM
7684
7685 /* First run through the relocs to check they are sane, and to
7686 determine whether we need to edit this opd section. */
b34976b6 7687 need_edit = FALSE;
51aecdc5 7688 broken = FALSE;
3f764659 7689 need_pad = sec;
1e2f5b6e 7690 relend = relstart + sec->reloc_count;
50bc7936 7691 for (rel = relstart; rel < relend; )
1e2f5b6e 7692 {
04c9666a 7693 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7694 unsigned long r_symndx;
7695 asection *sym_sec;
7696 struct elf_link_hash_entry *h;
7697 Elf_Internal_Sym *sym;
51aecdc5 7698 bfd_vma offset;
1e2f5b6e 7699
51aecdc5 7700 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7701 only interested in the reloc pointing to a function entry
7702 point. */
51aecdc5
AM
7703 offset = rel->r_offset;
7704 if (rel + 1 == relend
7705 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7706 {
7707 /* If someone messes with .opd alignment then after a
7708 "ld -r" we might have padding in the middle of .opd.
7709 Also, there's nothing to prevent someone putting
7710 something silly in .opd with the assembler. No .opd
b34976b6 7711 optimization for them! */
3f764659 7712 broken_opd:
1e2f5b6e 7713 (*_bfd_error_handler)
d003868e 7714 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7715 broken = TRUE;
1e2f5b6e
AM
7716 break;
7717 }
7718
50bc7936
AM
7719 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7720 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7721 {
7722 (*_bfd_error_handler)
d003868e
AM
7723 (_("%B: unexpected reloc type %u in .opd section"),
7724 ibfd, r_type);
51aecdc5 7725 broken = TRUE;
50bc7936
AM
7726 break;
7727 }
7728
1e2f5b6e 7729 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7730 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7731 r_symndx, ibfd))
50bc7936 7732 goto error_ret;
1e2f5b6e
AM
7733
7734 if (sym_sec == NULL || sym_sec->owner == NULL)
7735 {
411e1bfb
AM
7736 const char *sym_name;
7737 if (h != NULL)
7738 sym_name = h->root.root.string;
7739 else
26c61ae5
L
7740 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7741 sym_sec);
411e1bfb 7742
1e2f5b6e 7743 (*_bfd_error_handler)
d003868e
AM
7744 (_("%B: undefined sym `%s' in .opd section"),
7745 ibfd, sym_name);
51aecdc5 7746 broken = TRUE;
1e2f5b6e
AM
7747 break;
7748 }
7749
51020317
AM
7750 /* opd entries are always for functions defined in the
7751 current input bfd. If the symbol isn't defined in the
7752 input bfd, then we won't be using the function in this
7753 bfd; It must be defined in a linkonce section in another
7754 bfd, or is weak. It's also possible that we are
7755 discarding the function due to a linker script /DISCARD/,
7756 which we test for via the output_section. */
7757 if (sym_sec->owner != ibfd
7758 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7759 need_edit = TRUE;
1e2f5b6e 7760
50bc7936 7761 rel += 2;
51aecdc5
AM
7762 if (rel + 1 == relend
7763 || (rel + 2 < relend
7764 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7765 ++rel;
7766
7767 if (rel == relend)
3f764659
JJ
7768 {
7769 if (sec->size == offset + 24)
7770 {
7771 need_pad = NULL;
7772 break;
7773 }
51aecdc5 7774 if (sec->size == offset + 16)
3f764659
JJ
7775 {
7776 cnt_16b++;
7777 break;
7778 }
7779 goto broken_opd;
7780 }
3f764659
JJ
7781 else if (rel + 1 < relend
7782 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7783 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7784 {
51aecdc5
AM
7785 if (rel[0].r_offset == offset + 16)
7786 cnt_16b++;
7787 else if (rel[0].r_offset != offset + 24)
7788 goto broken_opd;
3f764659
JJ
7789 }
7790 else
7791 goto broken_opd;
1e2f5b6e
AM
7792 }
7793
e7d1c40c 7794 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7795
51aecdc5 7796 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7797 {
7798 Elf_Internal_Rela *write_rel;
d4730f92 7799 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7800 bfd_byte *rptr, *wptr;
983bddc8 7801 bfd_byte *new_contents;
74f0fb50
AM
7802 bfd_size_type amt;
7803
983bddc8 7804 new_contents = NULL;
51aecdc5 7805 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7806 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7807 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7808 if (opd->adjust == NULL)
7809 return FALSE;
7810 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7811
7812 /* This seems a waste of time as input .opd sections are all
7813 zeros as generated by gcc, but I suppose there's no reason
7814 this will always be so. We might start putting something in
7815 the third word of .opd entries. */
7816 if ((sec->flags & SEC_IN_MEMORY) == 0)
7817 {
eea6121a
AM
7818 bfd_byte *loc;
7819 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7820 {
eea6121a
AM
7821 if (loc != NULL)
7822 free (loc);
50bc7936 7823 error_ret:
6cdc0ccc
AM
7824 if (local_syms != NULL
7825 && symtab_hdr->contents != (unsigned char *) local_syms)
7826 free (local_syms);
6cdc0ccc
AM
7827 if (elf_section_data (sec)->relocs != relstart)
7828 free (relstart);
b34976b6 7829 return FALSE;
6cdc0ccc 7830 }
1e2f5b6e
AM
7831 sec->contents = loc;
7832 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7833 }
7834
7835 elf_section_data (sec)->relocs = relstart;
7836
3f764659 7837 new_contents = sec->contents;
3f764659
JJ
7838 if (add_aux_fields)
7839 {
7840 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7841 if (new_contents == NULL)
7842 return FALSE;
51aecdc5 7843 need_pad = NULL;
3f764659 7844 }
b4f4e59f
AM
7845 wptr = new_contents;
7846 rptr = sec->contents;
1e2f5b6e 7847 write_rel = relstart;
51aecdc5 7848 for (rel = relstart; rel < relend; )
1e2f5b6e 7849 {
50bc7936
AM
7850 unsigned long r_symndx;
7851 asection *sym_sec;
7852 struct elf_link_hash_entry *h;
51aecdc5 7853 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7854 Elf_Internal_Sym *sym;
51aecdc5
AM
7855 long opd_ent_size;
7856 Elf_Internal_Rela *next_rel;
7857 bfd_boolean skip;
50bc7936
AM
7858
7859 r_symndx = ELF64_R_SYM (rel->r_info);
7860 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7861 r_symndx, ibfd))
50bc7936
AM
7862 goto error_ret;
7863
51aecdc5
AM
7864 next_rel = rel + 2;
7865 if (next_rel + 1 == relend
7866 || (next_rel + 2 < relend
7867 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7868 ++next_rel;
7869
7870 /* See if the .opd entry is full 24 byte or
7871 16 byte (with fd_aux entry overlapped with next
7872 fd_func). */
7873 opd_ent_size = 24;
7874 if (next_rel == relend)
1e2f5b6e 7875 {
51aecdc5 7876 if (sec->size == rel->r_offset + 16)
3f764659 7877 opd_ent_size = 16;
51aecdc5
AM
7878 }
7879 else if (next_rel->r_offset == rel->r_offset + 16)
7880 opd_ent_size = 16;
3f764659 7881
51aecdc5
AM
7882 if (h != NULL
7883 && h->root.root.string[0] == '.')
7884 {
7885 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
7886 if (fdh != NULL
7887 && fdh->elf.root.type != bfd_link_hash_defined
7888 && fdh->elf.root.type != bfd_link_hash_defweak)
7889 fdh = NULL;
7890 }
1e2f5b6e 7891
51aecdc5
AM
7892 skip = (sym_sec->owner != ibfd
7893 || sym_sec->output_section == bfd_abs_section_ptr);
7894 if (skip)
7895 {
7896 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7897 {
51aecdc5
AM
7898 /* Arrange for the function descriptor sym
7899 to be dropped. */
7900 fdh->elf.root.u.def.value = 0;
7901 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7902 }
51aecdc5 7903 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7904
51aecdc5
AM
7905 if (NO_OPD_RELOCS || info->relocatable)
7906 rel = next_rel;
7907 else
7908 while (1)
7909 {
7910 if (!dec_dynrel_count (rel->r_info, sec, info,
7911 NULL, h, sym))
7912 goto error_ret;
754021d0 7913
51aecdc5
AM
7914 if (++rel == next_rel)
7915 break;
1e2f5b6e 7916
51aecdc5
AM
7917 r_symndx = ELF64_R_SYM (rel->r_info);
7918 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7919 r_symndx, ibfd))
7920 goto error_ret;
7921 }
50bc7936
AM
7922 }
7923 else
1e2f5b6e 7924 {
51aecdc5
AM
7925 /* We'll be keeping this opd entry. */
7926 long adjust;
7927
7928 if (fdh != NULL)
7929 {
7930 /* Redefine the function descriptor symbol to
7931 this location in the opd section. It is
7932 necessary to update the value here rather
7933 than using an array of adjustments as we do
7934 for local symbols, because various places
7935 in the generic ELF code use the value
7936 stored in u.def.value. */
7937 fdh->elf.root.u.def.value = wptr - new_contents;
7938 fdh->adjust_done = 1;
7939 }
7940
7941 /* Local syms are a bit tricky. We could
7942 tweak them as they can be cached, but
7943 we'd need to look through the local syms
7944 for the function descriptor sym which we
7945 don't have at the moment. So keep an
7946 array of adjustments. */
7947 adjust = (wptr - new_contents) - (rptr - sec->contents);
7948 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7949
7950 if (wptr != rptr)
7951 memcpy (wptr, rptr, opd_ent_size);
7952 wptr += opd_ent_size;
7953 if (add_aux_fields && opd_ent_size == 16)
7954 {
7955 memset (wptr, '\0', 8);
7956 wptr += 8;
7957 }
7958
50bc7936 7959 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7960 new opd entries. */
7961 for ( ; rel != next_rel; ++rel)
7962 {
7963 rel->r_offset += adjust;
7964 if (write_rel != rel)
7965 memcpy (write_rel, rel, sizeof (*rel));
7966 ++write_rel;
7967 }
1e2f5b6e 7968 }
51aecdc5
AM
7969
7970 rptr += opd_ent_size;
1e2f5b6e
AM
7971 }
7972
3f764659 7973 sec->size = wptr - new_contents;
1e2f5b6e 7974 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7975 if (add_aux_fields)
7976 {
7977 free (sec->contents);
7978 sec->contents = new_contents;
7979 }
7980
05bf9422 7981 /* Fudge the header size too, as this is used later in
cdcf6e38 7982 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7983 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7984 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7985 some_edited = TRUE;
1e2f5b6e 7986 }
6cdc0ccc 7987 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7988 free (relstart);
6cdc0ccc 7989
411e1bfb
AM
7990 if (local_syms != NULL
7991 && symtab_hdr->contents != (unsigned char *) local_syms)
7992 {
7993 if (!info->keep_memory)
7994 free (local_syms);
7995 else
7996 symtab_hdr->contents = (unsigned char *) local_syms;
7997 }
7998 }
7999
754021d0
AM
8000 if (some_edited)
8001 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8002
3f764659
JJ
8003 /* If we are doing a final link and the last .opd entry is just 16 byte
8004 long, add a 8 byte padding after it. */
8005 if (need_pad != NULL && !info->relocatable)
8006 {
8007 bfd_byte *p;
8008
8009 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8010 {
8011 BFD_ASSERT (need_pad->size > 0);
8012
8013 p = bfd_malloc (need_pad->size + 8);
8014 if (p == NULL)
8015 return FALSE;
699733f6 8016
3f764659
JJ
8017 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8018 p, 0, need_pad->size))
8019 return FALSE;
8020
8021 need_pad->contents = p;
8022 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8023 }
8024 else
8025 {
8026 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8027 if (p == NULL)
8028 return FALSE;
8029
8030 need_pad->contents = p;
8031 }
8032
8033 memset (need_pad->contents + need_pad->size, 0, 8);
8034 need_pad->size += 8;
8035 }
8036
411e1bfb
AM
8037 return TRUE;
8038}
8039
e1918d23 8040/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8041
e1918d23 8042asection *
e7d1c40c 8043ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8044{
411e1bfb
AM
8045 struct ppc_link_hash_table *htab;
8046
411e1bfb 8047 htab = ppc_hash_table (info);
4dfe6ac6
NC
8048 if (htab == NULL)
8049 return NULL;
8050
ee67d69a
AM
8051 if (abiversion (info->output_bfd) == 1)
8052 htab->opd_abi = 1;
8053
e7d1c40c 8054 if (htab->params->no_multi_toc)
33c0ec9d
AM
8055 htab->do_multi_toc = 0;
8056 else if (!htab->do_multi_toc)
e7d1c40c 8057 htab->params->no_multi_toc = 1;
33c0ec9d 8058
3a71aa26
AM
8059 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8060 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8061 FALSE, FALSE, TRUE));
a7f2871e
AM
8062 /* Move dynamic linking info to the function descriptor sym. */
8063 if (htab->tls_get_addr != NULL)
8064 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8065 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8066 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8067 FALSE, FALSE, TRUE));
e7d1c40c 8068 if (!htab->params->no_tls_get_addr_opt)
a7f2871e
AM
8069 {
8070 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8071
8072 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8073 FALSE, FALSE, TRUE);
8074 if (opt != NULL)
8075 func_desc_adjust (opt, info);
8076 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8077 FALSE, FALSE, TRUE);
8078 if (opt_fd != NULL
8079 && (opt_fd->root.type == bfd_link_hash_defined
8080 || opt_fd->root.type == bfd_link_hash_defweak))
8081 {
8082 /* If glibc supports an optimized __tls_get_addr call stub,
8083 signalled by the presence of __tls_get_addr_opt, and we'll
8084 be calling __tls_get_addr via a plt call stub, then
8085 make __tls_get_addr point to __tls_get_addr_opt. */
8086 tga_fd = &htab->tls_get_addr_fd->elf;
8087 if (htab->elf.dynamic_sections_created
8088 && tga_fd != NULL
8089 && (tga_fd->type == STT_FUNC
8090 || tga_fd->needs_plt)
8091 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8092 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8093 && tga_fd->root.type == bfd_link_hash_undefweak)))
8094 {
8095 struct plt_entry *ent;
8096
8097 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8098 if (ent->plt.refcount > 0)
8099 break;
8100 if (ent != NULL)
8101 {
8102 tga_fd->root.type = bfd_link_hash_indirect;
8103 tga_fd->root.u.i.link = &opt_fd->root;
8104 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8105 if (opt_fd->dynindx != -1)
8106 {
8107 /* Use __tls_get_addr_opt in dynamic relocations. */
8108 opt_fd->dynindx = -1;
8109 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8110 opt_fd->dynstr_index);
8111 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8112 return NULL;
a7f2871e
AM
8113 }
8114 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8115 tga = &htab->tls_get_addr->elf;
8116 if (opt != NULL && tga != NULL)
8117 {
8118 tga->root.type = bfd_link_hash_indirect;
8119 tga->root.u.i.link = &opt->root;
8120 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8121 _bfd_elf_link_hash_hide_symbol (info, opt,
8122 tga->forced_local);
8123 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8124 }
8125 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8126 htab->tls_get_addr_fd->is_func_descriptor = 1;
8127 if (htab->tls_get_addr != NULL)
8128 {
8129 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8130 htab->tls_get_addr->is_func = 1;
8131 }
8132 }
8133 }
8134 }
8135 else
e7d1c40c 8136 htab->params->no_tls_get_addr_opt = TRUE;
a7f2871e 8137 }
33c0ec9d 8138 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8139}
8387904d 8140
3a71aa26
AM
8141/* Return TRUE iff REL is a branch reloc with a global symbol matching
8142 HASH1 or HASH2. */
8387904d 8143
3a71aa26
AM
8144static bfd_boolean
8145branch_reloc_hash_match (const bfd *ibfd,
8146 const Elf_Internal_Rela *rel,
8147 const struct ppc_link_hash_entry *hash1,
8148 const struct ppc_link_hash_entry *hash2)
8149{
8150 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8151 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8152 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8153
e054468f 8154 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8155 {
3a71aa26
AM
8156 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8157 struct elf_link_hash_entry *h;
8387904d 8158
3a71aa26 8159 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8160 h = elf_follow_link (h);
3a71aa26
AM
8161 if (h == &hash1->elf || h == &hash2->elf)
8162 return TRUE;
a48ebf4d 8163 }
3a71aa26 8164 return FALSE;
951fd09b 8165}
411e1bfb 8166
951fd09b
AM
8167/* Run through all the TLS relocs looking for optimization
8168 opportunities. The linker has been hacked (see ppc64elf.em) to do
8169 a preliminary section layout so that we know the TLS segment
8170 offsets. We can't optimize earlier because some optimizations need
8171 to know the tp offset, and we need to optimize before allocating
8172 dynamic relocations. */
8173
8174bfd_boolean
33c0ec9d 8175ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8176{
8177 bfd *ibfd;
8178 asection *sec;
8179 struct ppc_link_hash_table *htab;
663a1470 8180 unsigned char *toc_ref;
102890f0 8181 int pass;
951fd09b 8182
1d483afe 8183 if (info->relocatable || !info->executable)
411e1bfb
AM
8184 return TRUE;
8185
951fd09b 8186 htab = ppc_hash_table (info);
4dfe6ac6
NC
8187 if (htab == NULL)
8188 return FALSE;
8189
663a1470
AM
8190 /* Make two passes over the relocs. On the first pass, mark toc
8191 entries involved with tls relocs, and check that tls relocs
8192 involved in setting up a tls_get_addr call are indeed followed by
8193 such a call. If they are not, we can't do any tls optimization.
8194 On the second pass twiddle tls_mask flags to notify
8195 relocate_section that optimization can be done, and adjust got
8196 and plt refcounts. */
8197 toc_ref = NULL;
8198 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8199 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8200 {
8201 Elf_Internal_Sym *locsyms = NULL;
8202 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8203
102890f0
AM
8204 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8205 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8206 {
8207 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8208 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8209
102890f0
AM
8210 /* Read the relocations. */
8211 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8212 info->keep_memory);
8213 if (relstart == NULL)
2915c55b
JK
8214 {
8215 free (toc_ref);
8216 return FALSE;
8217 }
411e1bfb 8218
102890f0
AM
8219 relend = relstart + sec->reloc_count;
8220 for (rel = relstart; rel < relend; rel++)
8221 {
8222 enum elf_ppc64_reloc_type r_type;
8223 unsigned long r_symndx;
8224 struct elf_link_hash_entry *h;
8225 Elf_Internal_Sym *sym;
8226 asection *sym_sec;
f961d9dd
AM
8227 unsigned char *tls_mask;
8228 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8229 bfd_vma value;
8230 bfd_boolean ok_tprel, is_local;
8231 long toc_ref_index = 0;
8232 int expecting_tls_get_addr = 0;
663a1470 8233 bfd_boolean ret = FALSE;
411e1bfb 8234
102890f0
AM
8235 r_symndx = ELF64_R_SYM (rel->r_info);
8236 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8237 r_symndx, ibfd))
8238 {
8239 err_free_rel:
8240 if (elf_section_data (sec)->relocs != relstart)
8241 free (relstart);
8242 if (toc_ref != NULL)
8243 free (toc_ref);
8244 if (locsyms != NULL
0ffa91dd 8245 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8246 != (unsigned char *) locsyms))
8247 free (locsyms);
663a1470 8248 return ret;
102890f0 8249 }
411e1bfb 8250
102890f0
AM
8251 if (h != NULL)
8252 {
766bc656
AM
8253 if (h->root.type == bfd_link_hash_defined
8254 || h->root.type == bfd_link_hash_defweak)
8255 value = h->root.u.def.value;
8256 else if (h->root.type == bfd_link_hash_undefweak)
8257 value = 0;
8258 else
663a1470
AM
8259 {
8260 found_tls_get_addr_arg = 0;
8261 continue;
8262 }
102890f0
AM
8263 }
8264 else
8265 /* Symbols referenced by TLS relocs must be of type
8266 STT_TLS. So no need for .opd local sym adjust. */
8267 value = sym->st_value;
8268
8269 ok_tprel = FALSE;
8270 is_local = FALSE;
8271 if (h == NULL
8272 || !h->def_dynamic)
8273 {
8274 is_local = TRUE;
766bc656
AM
8275 if (h != NULL
8276 && h->root.type == bfd_link_hash_undefweak)
8277 ok_tprel = TRUE;
8278 else
8279 {
8280 value += sym_sec->output_offset;
8281 value += sym_sec->output_section->vma;
8282 value -= htab->elf.tls_sec->vma;
8283 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8284 < (bfd_vma) 1 << 32);
8285 }
102890f0 8286 }
951fd09b 8287
102890f0 8288 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8289 /* If this section has old-style __tls_get_addr calls
8290 without marker relocs, then check that each
8291 __tls_get_addr call reloc is preceded by a reloc
8292 that conceivably belongs to the __tls_get_addr arg
8293 setup insn. If we don't find matching arg setup
8294 relocs, don't do any tls optimization. */
8295 if (pass == 0
8296 && sec->has_tls_get_addr_call
8297 && h != NULL
8298 && (h == &htab->tls_get_addr->elf
8299 || h == &htab->tls_get_addr_fd->elf)
8300 && !found_tls_get_addr_arg
8301 && is_branch_reloc (r_type))
8302 {
25f53a85 8303 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8304 "TLS optimization disabled\n"),
8305 ibfd, sec, rel->r_offset);
8306 ret = TRUE;
8307 goto err_free_rel;
8308 }
8309
8310 found_tls_get_addr_arg = 0;
102890f0
AM
8311 switch (r_type)
8312 {
8313 case R_PPC64_GOT_TLSLD16:
8314 case R_PPC64_GOT_TLSLD16_LO:
8315 expecting_tls_get_addr = 1;
663a1470 8316 found_tls_get_addr_arg = 1;
102890f0
AM
8317 /* Fall thru */
8318
8319 case R_PPC64_GOT_TLSLD16_HI:
8320 case R_PPC64_GOT_TLSLD16_HA:
8321 /* These relocs should never be against a symbol
8322 defined in a shared lib. Leave them alone if
8323 that turns out to be the case. */
8324 if (!is_local)
8325 continue;
411e1bfb 8326
102890f0 8327 /* LD -> LE */
411e1bfb 8328 tls_set = 0;
102890f0
AM
8329 tls_clear = TLS_LD;
8330 tls_type = TLS_TLS | TLS_LD;
8331 break;
411e1bfb 8332
102890f0
AM
8333 case R_PPC64_GOT_TLSGD16:
8334 case R_PPC64_GOT_TLSGD16_LO:
8335 expecting_tls_get_addr = 1;
663a1470 8336 found_tls_get_addr_arg = 1;
102890f0
AM
8337 /* Fall thru */
8338
8339 case R_PPC64_GOT_TLSGD16_HI:
8340 case R_PPC64_GOT_TLSGD16_HA:
8341 if (ok_tprel)
8342 /* GD -> LE */
411e1bfb 8343 tls_set = 0;
102890f0
AM
8344 else
8345 /* GD -> IE */
8346 tls_set = TLS_TLS | TLS_TPRELGD;
8347 tls_clear = TLS_GD;
8348 tls_type = TLS_TLS | TLS_GD;
8349 break;
8350
8351 case R_PPC64_GOT_TPREL16_DS:
8352 case R_PPC64_GOT_TPREL16_LO_DS:
8353 case R_PPC64_GOT_TPREL16_HI:
8354 case R_PPC64_GOT_TPREL16_HA:
8355 if (ok_tprel)
8356 {
8357 /* IE -> LE */
8358 tls_set = 0;
8359 tls_clear = TLS_TPREL;
8360 tls_type = TLS_TLS | TLS_TPREL;
8361 break;
8362 }
411e1bfb
AM
8363 continue;
8364
727fc41e
AM
8365 case R_PPC64_TLSGD:
8366 case R_PPC64_TLSLD:
663a1470
AM
8367 found_tls_get_addr_arg = 1;
8368 /* Fall thru */
8369
8370 case R_PPC64_TLS:
8371 case R_PPC64_TOC16:
8372 case R_PPC64_TOC16_LO:
102890f0
AM
8373 if (sym_sec == NULL || sym_sec != toc)
8374 continue;
8375
8376 /* Mark this toc entry as referenced by a TLS
8377 code sequence. We can do that now in the
8378 case of R_PPC64_TLS, and after checking for
8379 tls_get_addr for the TOC16 relocs. */
8380 if (toc_ref == NULL)
663a1470
AM
8381 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8382 if (toc_ref == NULL)
8383 goto err_free_rel;
8384
102890f0
AM
8385 if (h != NULL)
8386 value = h->root.u.def.value;
8387 else
8388 value = sym->st_value;
8389 value += rel->r_addend;
73242275
AM
8390 if (value % 8 != 0)
8391 continue;
8392 BFD_ASSERT (value < toc->size
8393 && toc->output_offset % 8 == 0);
663a1470 8394 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8395 if (r_type == R_PPC64_TLS
8396 || r_type == R_PPC64_TLSGD
8397 || r_type == R_PPC64_TLSLD)
102890f0
AM
8398 {
8399 toc_ref[toc_ref_index] = 1;
8400 continue;
8401 }
8402
8403 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8404 continue;
8405
8406 tls_set = 0;
8407 tls_clear = 0;
8408 expecting_tls_get_addr = 2;
8409 break;
8410
8411 case R_PPC64_TPREL64:
8412 if (pass == 0
8413 || sec != toc
8414 || toc_ref == NULL
663a1470 8415 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8416 continue;
8417 if (ok_tprel)
8418 {
8419 /* IE -> LE */
8420 tls_set = TLS_EXPLICIT;
8421 tls_clear = TLS_TPREL;
8422 break;
8423 }
8424 continue;
8425
8426 case R_PPC64_DTPMOD64:
8427 if (pass == 0
8428 || sec != toc
8429 || toc_ref == NULL
663a1470 8430 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8431 continue;
8432 if (rel + 1 < relend
8433 && (rel[1].r_info
8434 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8435 && rel[1].r_offset == rel->r_offset + 8)
8436 {
8437 if (ok_tprel)
8438 /* GD -> LE */
8439 tls_set = TLS_EXPLICIT | TLS_GD;
8440 else
8441 /* GD -> IE */
8442 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8443 tls_clear = TLS_GD;
8444 }
8445 else
8446 {
8447 if (!is_local)
8448 continue;
8449
8450 /* LD -> LE */
8451 tls_set = TLS_EXPLICIT;
8452 tls_clear = TLS_LD;
8453 }
8454 break;
8455
8456 default:
8457 continue;
8458 }
8459
8460 if (pass == 0)
8461 {
727fc41e
AM
8462 if (!expecting_tls_get_addr
8463 || !sec->has_tls_get_addr_call)
102890f0
AM
8464 continue;
8465
3a71aa26
AM
8466 if (rel + 1 < relend
8467 && branch_reloc_hash_match (ibfd, rel + 1,
8468 htab->tls_get_addr,
8469 htab->tls_get_addr_fd))
102890f0 8470 {
3a71aa26 8471 if (expecting_tls_get_addr == 2)
102890f0 8472 {
3a71aa26 8473 /* Check for toc tls entries. */
f961d9dd 8474 unsigned char *toc_tls;
3a71aa26
AM
8475 int retval;
8476
8477 retval = get_tls_mask (&toc_tls, NULL, NULL,
8478 &locsyms,
8479 rel, ibfd);
8480 if (retval == 0)
8481 goto err_free_rel;
663a1470
AM
8482 if (toc_tls != NULL)
8483 {
8484 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8485 found_tls_get_addr_arg = 1;
8486 if (retval > 1)
8487 toc_ref[toc_ref_index] = 1;
8488 }
102890f0 8489 }
3a71aa26 8490 continue;
102890f0
AM
8491 }
8492
8493 if (expecting_tls_get_addr != 1)
8494 continue;
8495
8496 /* Uh oh, we didn't find the expected call. We
8497 could just mark this symbol to exclude it
8498 from tls optimization but it's safer to skip
663a1470 8499 the entire optimization. */
25f53a85 8500 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8501 "TLS optimization disabled\n"),
8502 ibfd, sec, rel->r_offset);
8503 ret = TRUE;
8504 goto err_free_rel;
102890f0
AM
8505 }
8506
85f7a9cb 8507 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8508 {
8509 struct plt_entry *ent;
8510 for (ent = htab->tls_get_addr->elf.plt.plist;
8511 ent != NULL;
8512 ent = ent->next)
8513 if (ent->addend == 0)
411e1bfb 8514 {
102890f0 8515 if (ent->plt.refcount > 0)
30038c59 8516 {
102890f0
AM
8517 ent->plt.refcount -= 1;
8518 expecting_tls_get_addr = 0;
30038c59 8519 }
102890f0 8520 break;
411e1bfb 8521 }
102890f0 8522 }
411e1bfb 8523
85f7a9cb 8524 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8525 {
8526 struct plt_entry *ent;
8527 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8528 ent != NULL;
8529 ent = ent->next)
8530 if (ent->addend == 0)
411e1bfb 8531 {
102890f0
AM
8532 if (ent->plt.refcount > 0)
8533 ent->plt.refcount -= 1;
8534 break;
411e1bfb 8535 }
102890f0 8536 }
411e1bfb 8537
102890f0 8538 if (tls_clear == 0)
30038c59
AM
8539 continue;
8540
102890f0
AM
8541 if ((tls_set & TLS_EXPLICIT) == 0)
8542 {
8543 struct got_entry *ent;
411e1bfb 8544
102890f0
AM
8545 /* Adjust got entry for this reloc. */
8546 if (h != NULL)
8547 ent = h->got.glist;
8548 else
8549 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8550
102890f0
AM
8551 for (; ent != NULL; ent = ent->next)
8552 if (ent->addend == rel->r_addend
8553 && ent->owner == ibfd
8554 && ent->tls_type == tls_type)
8555 break;
8556 if (ent == NULL)
8557 abort ();
411e1bfb 8558
102890f0
AM
8559 if (tls_set == 0)
8560 {
8561 /* We managed to get rid of a got entry. */
8562 if (ent->got.refcount > 0)
8563 ent->got.refcount -= 1;
8564 }
8565 }
8566 else
8567 {
8568 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8569 we'll lose one or two dyn relocs. */
8570 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8571 NULL, h, sym))
102890f0 8572 return FALSE;
411e1bfb 8573
102890f0
AM
8574 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8575 {
8576 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8577 NULL, h, sym))
102890f0
AM
8578 return FALSE;
8579 }
8580 }
411e1bfb 8581
102890f0
AM
8582 *tls_mask |= tls_set;
8583 *tls_mask &= ~tls_clear;
8584 }
8c1d1bb8 8585
102890f0
AM
8586 if (elf_section_data (sec)->relocs != relstart)
8587 free (relstart);
8588 }
411e1bfb 8589
663a1470
AM
8590 if (locsyms != NULL
8591 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8592 {
8593 if (!info->keep_memory)
8594 free (locsyms);
8595 else
8596 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8597 }
8598 }
411e1bfb 8599
663a1470
AM
8600 if (toc_ref != NULL)
8601 free (toc_ref);
b34976b6 8602 return TRUE;
1e2f5b6e 8603}
b34976b6 8604
c5614fa4
AM
8605/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8606 the values of any global symbols in a toc section that has been
8607 edited. Globals in toc sections should be a rarity, so this function
8608 sets a flag if any are found in toc sections other than the one just
8609 edited, so that futher hash table traversals can be avoided. */
8610
8611struct adjust_toc_info
8612{
8613 asection *toc;
8614 unsigned long *skip;
8615 bfd_boolean global_toc_syms;
8616};
8617
ba761f19
AM
8618enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8619
c5614fa4
AM
8620static bfd_boolean
8621adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8622{
8623 struct ppc_link_hash_entry *eh;
8624 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8625 unsigned long i;
c5614fa4 8626
c5614fa4
AM
8627 if (h->root.type != bfd_link_hash_defined
8628 && h->root.type != bfd_link_hash_defweak)
8629 return TRUE;
8630
8631 eh = (struct ppc_link_hash_entry *) h;
8632 if (eh->adjust_done)
8633 return TRUE;
8634
8635 if (eh->elf.root.u.def.section == toc_inf->toc)
8636 {
854b41e7
AM
8637 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8638 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8639 else
854b41e7
AM
8640 i = eh->elf.root.u.def.value >> 3;
8641
ba761f19 8642 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8643 {
8644 (*_bfd_error_handler)
854b41e7
AM
8645 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8646 do
8647 ++i;
ba761f19 8648 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8649 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8650 }
854b41e7
AM
8651
8652 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8653 eh->adjust_done = 1;
8654 }
8655 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8656 toc_inf->global_toc_syms = TRUE;
8657
8658 return TRUE;
8659}
8660
560c8763
AM
8661/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8662
8663static bfd_boolean
8664ok_lo_toc_insn (unsigned int insn)
8665{
8666 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8667 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8668 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8669 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8670 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8671 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8672 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8673 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8674 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8675 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8676 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8677 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8678 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8679 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8680 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8681 && (insn & 3) != 1)
8682 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8683 && ((insn & 3) == 0 || (insn & 3) == 3))
8684 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8685}
8686
c5614fa4
AM
8687/* Examine all relocs referencing .toc sections in order to remove
8688 unused .toc entries. */
8689
8690bfd_boolean
33c0ec9d 8691ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8692{
8693 bfd *ibfd;
8694 struct adjust_toc_info toc_inf;
67f0cbdb 8695 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8696
67f0cbdb 8697 htab->do_toc_opt = 1;
c5614fa4 8698 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8699 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8700 {
8701 asection *toc, *sec;
8702 Elf_Internal_Shdr *symtab_hdr;
8703 Elf_Internal_Sym *local_syms;
425b145b 8704 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8705 unsigned long *skip, *drop;
8706 unsigned char *used;
8707 unsigned char *keep, last, some_unused;
8708
854b41e7
AM
8709 if (!is_ppc64_elf (ibfd))
8710 continue;
8711
c5614fa4
AM
8712 toc = bfd_get_section_by_name (ibfd, ".toc");
8713 if (toc == NULL
92b7a70f 8714 || toc->size == 0
dbaa2011
AM
8715 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8716 || discarded_section (toc))
c5614fa4
AM
8717 continue;
8718
425b145b 8719 toc_relocs = NULL;
c5614fa4 8720 local_syms = NULL;
0ffa91dd 8721 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8722
8723 /* Look at sections dropped from the final link. */
8724 skip = NULL;
8725 relstart = NULL;
8726 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8727 {
8728 if (sec->reloc_count == 0
dbaa2011 8729 || !discarded_section (sec)
c5614fa4
AM
8730 || get_opd_info (sec)
8731 || (sec->flags & SEC_ALLOC) == 0
8732 || (sec->flags & SEC_DEBUGGING) != 0)
8733 continue;
8734
8735 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8736 if (relstart == NULL)
8737 goto error_ret;
8738
8739 /* Run through the relocs to see which toc entries might be
8740 unused. */
8741 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8742 {
8743 enum elf_ppc64_reloc_type r_type;
8744 unsigned long r_symndx;
8745 asection *sym_sec;
8746 struct elf_link_hash_entry *h;
8747 Elf_Internal_Sym *sym;
8748 bfd_vma val;
8749
8750 r_type = ELF64_R_TYPE (rel->r_info);
8751 switch (r_type)
8752 {
8753 default:
8754 continue;
8755
8756 case R_PPC64_TOC16:
8757 case R_PPC64_TOC16_LO:
8758 case R_PPC64_TOC16_HI:
8759 case R_PPC64_TOC16_HA:
8760 case R_PPC64_TOC16_DS:
8761 case R_PPC64_TOC16_LO_DS:
8762 break;
8763 }
8764
8765 r_symndx = ELF64_R_SYM (rel->r_info);
8766 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8767 r_symndx, ibfd))
8768 goto error_ret;
8769
8770 if (sym_sec != toc)
8771 continue;
8772
8773 if (h != NULL)
8774 val = h->root.u.def.value;
8775 else
8776 val = sym->st_value;
8777 val += rel->r_addend;
8778
8779 if (val >= toc->size)
8780 continue;
8781
8782 /* Anything in the toc ought to be aligned to 8 bytes.
8783 If not, don't mark as unused. */
8784 if (val & 7)
8785 continue;
8786
8787 if (skip == NULL)
8788 {
854b41e7 8789 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8790 if (skip == NULL)
8791 goto error_ret;
8792 }
8793
ba761f19 8794 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8795 }
8796
8797 if (elf_section_data (sec)->relocs != relstart)
8798 free (relstart);
8799 }
8800
ba761f19
AM
8801 /* For largetoc loads of address constants, we can convert
8802 . addis rx,2,addr@got@ha
8803 . ld ry,addr@got@l(rx)
8804 to
8805 . addis rx,2,addr@toc@ha
8806 . addi ry,rx,addr@toc@l
8807 when addr is within 2G of the toc pointer. This then means
8808 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8809
ba761f19
AM
8810 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8811 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8812 && toc->reloc_count != 0)
8813 {
8814 /* Read toc relocs. */
425b145b
AM
8815 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8816 info->keep_memory);
8817 if (toc_relocs == NULL)
ba761f19
AM
8818 goto error_ret;
8819
425b145b 8820 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8821 {
8822 enum elf_ppc64_reloc_type r_type;
8823 unsigned long r_symndx;
8824 asection *sym_sec;
8825 struct elf_link_hash_entry *h;
8826 Elf_Internal_Sym *sym;
8827 bfd_vma val, addr;
8828
8829 r_type = ELF64_R_TYPE (rel->r_info);
8830 if (r_type != R_PPC64_ADDR64)
8831 continue;
8832
8833 r_symndx = ELF64_R_SYM (rel->r_info);
8834 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8835 r_symndx, ibfd))
8836 goto error_ret;
8837
425b145b 8838 if (sym_sec == NULL
dbaa2011 8839 || discarded_section (sym_sec))
425b145b
AM
8840 continue;
8841
afe397ea 8842 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8843 continue;
8844
8845 if (h != NULL)
bddc25c9
AM
8846 {
8847 if (h->type == STT_GNU_IFUNC)
8848 continue;
8849 val = h->root.u.def.value;
8850 }
ba761f19 8851 else
bddc25c9
AM
8852 {
8853 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8854 continue;
8855 val = sym->st_value;
8856 }
ba761f19
AM
8857 val += rel->r_addend;
8858 val += sym_sec->output_section->vma + sym_sec->output_offset;
8859
8860 /* We don't yet know the exact toc pointer value, but we
8861 know it will be somewhere in the toc section. Don't
8862 optimize if the difference from any possible toc
8863 pointer is outside [ff..f80008000, 7fff7fff]. */
8864 addr = toc->output_section->vma + TOC_BASE_OFF;
8865 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8866 continue;
8867
8868 addr = toc->output_section->vma + toc->output_section->rawsize;
8869 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8870 continue;
8871
8872 if (skip == NULL)
8873 {
8874 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8875 if (skip == NULL)
8876 goto error_ret;
8877 }
8878
8879 skip[rel->r_offset >> 3]
425b145b 8880 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8881 }
ba761f19
AM
8882 }
8883
c5614fa4
AM
8884 if (skip == NULL)
8885 continue;
8886
8887 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8888 if (used == NULL)
8889 {
8890 error_ret:
8891 if (local_syms != NULL
8892 && symtab_hdr->contents != (unsigned char *) local_syms)
8893 free (local_syms);
8894 if (sec != NULL
8895 && relstart != NULL
8896 && elf_section_data (sec)->relocs != relstart)
8897 free (relstart);
425b145b
AM
8898 if (toc_relocs != NULL
8899 && elf_section_data (toc)->relocs != toc_relocs)
8900 free (toc_relocs);
c5614fa4
AM
8901 if (skip != NULL)
8902 free (skip);
8903 return FALSE;
8904 }
8905
30038c59
AM
8906 /* Now check all kept sections that might reference the toc.
8907 Check the toc itself last. */
8908 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8909 : ibfd->sections);
c5614fa4 8910 sec != NULL;
c5614fa4 8911 sec = (sec == toc ? NULL
c5614fa4 8912 : sec->next == NULL ? toc
30038c59 8913 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8914 : sec->next))
8915 {
8916 int repeat;
8917
8918 if (sec->reloc_count == 0
dbaa2011 8919 || discarded_section (sec)
c5614fa4
AM
8920 || get_opd_info (sec)
8921 || (sec->flags & SEC_ALLOC) == 0
8922 || (sec->flags & SEC_DEBUGGING) != 0)
8923 continue;
8924
854b41e7
AM
8925 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8926 info->keep_memory);
c5614fa4 8927 if (relstart == NULL)
2915c55b
JK
8928 {
8929 free (used);
8930 goto error_ret;
8931 }
c5614fa4
AM
8932
8933 /* Mark toc entries referenced as used. */
c5614fa4 8934 do
d4f1ee75
AM
8935 {
8936 repeat = 0;
8937 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8938 {
8939 enum elf_ppc64_reloc_type r_type;
8940 unsigned long r_symndx;
8941 asection *sym_sec;
8942 struct elf_link_hash_entry *h;
8943 Elf_Internal_Sym *sym;
8944 bfd_vma val;
8945 enum {no_check, check_lo, check_ha} insn_check;
98528052 8946
d4f1ee75
AM
8947 r_type = ELF64_R_TYPE (rel->r_info);
8948 switch (r_type)
8949 {
8950 default:
8951 insn_check = no_check;
8952 break;
98528052 8953
d4f1ee75
AM
8954 case R_PPC64_GOT_TLSLD16_HA:
8955 case R_PPC64_GOT_TLSGD16_HA:
8956 case R_PPC64_GOT_TPREL16_HA:
8957 case R_PPC64_GOT_DTPREL16_HA:
8958 case R_PPC64_GOT16_HA:
8959 case R_PPC64_TOC16_HA:
8960 insn_check = check_ha;
8961 break;
98528052 8962
d4f1ee75
AM
8963 case R_PPC64_GOT_TLSLD16_LO:
8964 case R_PPC64_GOT_TLSGD16_LO:
8965 case R_PPC64_GOT_TPREL16_LO_DS:
8966 case R_PPC64_GOT_DTPREL16_LO_DS:
8967 case R_PPC64_GOT16_LO:
8968 case R_PPC64_GOT16_LO_DS:
8969 case R_PPC64_TOC16_LO:
8970 case R_PPC64_TOC16_LO_DS:
8971 insn_check = check_lo;
8972 break;
8973 }
560c8763 8974
d4f1ee75
AM
8975 if (insn_check != no_check)
8976 {
8977 bfd_vma off = rel->r_offset & ~3;
8978 unsigned char buf[4];
8979 unsigned int insn;
c5614fa4 8980
d4f1ee75
AM
8981 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8982 {
8983 free (used);
8984 goto error_ret;
8985 }
8986 insn = bfd_get_32 (ibfd, buf);
8987 if (insn_check == check_lo
8988 ? !ok_lo_toc_insn (insn)
8989 : ((insn & ((0x3f << 26) | 0x1f << 16))
8990 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8991 {
8992 char str[12];
8993
8994 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8995 sprintf (str, "%#08x", insn);
8996 info->callbacks->einfo
8997 (_("%P: %H: toc optimization is not supported for"
8998 " %s instruction.\n"),
8999 ibfd, sec, rel->r_offset & ~3, str);
9000 }
9001 }
c5614fa4 9002
d4f1ee75
AM
9003 switch (r_type)
9004 {
9005 case R_PPC64_TOC16:
9006 case R_PPC64_TOC16_LO:
9007 case R_PPC64_TOC16_HI:
9008 case R_PPC64_TOC16_HA:
9009 case R_PPC64_TOC16_DS:
9010 case R_PPC64_TOC16_LO_DS:
9011 /* In case we're taking addresses of toc entries. */
9012 case R_PPC64_ADDR64:
9013 break;
c5614fa4 9014
d4f1ee75
AM
9015 default:
9016 continue;
9017 }
c5614fa4 9018
d4f1ee75
AM
9019 r_symndx = ELF64_R_SYM (rel->r_info);
9020 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9021 r_symndx, ibfd))
9022 {
9023 free (used);
9024 goto error_ret;
9025 }
c5614fa4 9026
d4f1ee75
AM
9027 if (sym_sec != toc)
9028 continue;
c5614fa4 9029
d4f1ee75
AM
9030 if (h != NULL)
9031 val = h->root.u.def.value;
9032 else
9033 val = sym->st_value;
9034 val += rel->r_addend;
ba761f19 9035
d4f1ee75
AM
9036 if (val >= toc->size)
9037 continue;
ba761f19 9038
d4f1ee75
AM
9039 if ((skip[val >> 3] & can_optimize) != 0)
9040 {
9041 bfd_vma off;
9042 unsigned char opc;
9043
9044 switch (r_type)
9045 {
9046 case R_PPC64_TOC16_HA:
ba761f19 9047 break;
ba761f19 9048
d4f1ee75
AM
9049 case R_PPC64_TOC16_LO_DS:
9050 off = rel->r_offset;
9051 off += (bfd_big_endian (ibfd) ? -2 : 3);
9052 if (!bfd_get_section_contents (ibfd, sec, &opc,
9053 off, 1))
9054 {
9055 free (used);
9056 goto error_ret;
9057 }
9058 if ((opc & (0x3f << 2)) == (58u << 2))
9059 break;
9060 /* Fall thru */
ba761f19 9061
d4f1ee75
AM
9062 default:
9063 /* Wrong sort of reloc, or not a ld. We may
9064 as well clear ref_from_discarded too. */
9065 skip[val >> 3] = 0;
9066 }
9067 }
9068
9069 if (sec != toc)
9070 used[val >> 3] = 1;
9071 /* For the toc section, we only mark as used if this
9072 entry itself isn't unused. */
9073 else if ((used[rel->r_offset >> 3]
9074 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9075 && !used[val >> 3])
9076 {
9077 /* Do all the relocs again, to catch reference
9078 chains. */
9079 repeat = 1;
9080 used[val >> 3] = 1;
9081 }
9082 }
9083 }
c5614fa4 9084 while (repeat);
854b41e7
AM
9085
9086 if (elf_section_data (sec)->relocs != relstart)
9087 free (relstart);
c5614fa4
AM
9088 }
9089
9090 /* Merge the used and skip arrays. Assume that TOC
9091 doublewords not appearing as either used or unused belong
9092 to to an entry more than one doubleword in size. */
9093 for (drop = skip, keep = used, last = 0, some_unused = 0;
9094 drop < skip + (toc->size + 7) / 8;
9095 ++drop, ++keep)
9096 {
9097 if (*keep)
9098 {
ba761f19
AM
9099 *drop &= ~ref_from_discarded;
9100 if ((*drop & can_optimize) != 0)
9101 some_unused = 1;
c5614fa4
AM
9102 last = 0;
9103 }
b140b010 9104 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9105 {
9106 some_unused = 1;
ba761f19 9107 last = ref_from_discarded;
c5614fa4
AM
9108 }
9109 else
9110 *drop = last;
9111 }
9112
9113 free (used);
9114
9115 if (some_unused)
9116 {
9117 bfd_byte *contents, *src;
9118 unsigned long off;
d62b3684 9119 Elf_Internal_Sym *sym;
ba761f19 9120 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9121
9122 /* Shuffle the toc contents, and at the same time convert the
9123 skip array from booleans into offsets. */
9124 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9125 goto error_ret;
9126
9127 elf_section_data (toc)->this_hdr.contents = contents;
9128
9129 for (src = contents, off = 0, drop = skip;
9130 src < contents + toc->size;
9131 src += 8, ++drop)
9132 {
ba761f19
AM
9133 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9134 off += 8;
c5614fa4
AM
9135 else if (off != 0)
9136 {
9137 *drop = off;
9138 memcpy (src - off, src, 8);
9139 }
9140 }
854b41e7 9141 *drop = off;
c5614fa4
AM
9142 toc->rawsize = toc->size;
9143 toc->size = src - contents - off;
9144
ba761f19
AM
9145 /* Adjust addends for relocs against the toc section sym,
9146 and optimize any accesses we can. */
c5614fa4
AM
9147 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9148 {
9149 if (sec->reloc_count == 0
dbaa2011 9150 || discarded_section (sec))
c5614fa4
AM
9151 continue;
9152
9153 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9154 info->keep_memory);
c5614fa4
AM
9155 if (relstart == NULL)
9156 goto error_ret;
9157
9158 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9159 {
9160 enum elf_ppc64_reloc_type r_type;
9161 unsigned long r_symndx;
9162 asection *sym_sec;
9163 struct elf_link_hash_entry *h;
854b41e7 9164 bfd_vma val;
c5614fa4
AM
9165
9166 r_type = ELF64_R_TYPE (rel->r_info);
9167 switch (r_type)
9168 {
9169 default:
9170 continue;
9171
9172 case R_PPC64_TOC16:
9173 case R_PPC64_TOC16_LO:
9174 case R_PPC64_TOC16_HI:
9175 case R_PPC64_TOC16_HA:
9176 case R_PPC64_TOC16_DS:
9177 case R_PPC64_TOC16_LO_DS:
9178 case R_PPC64_ADDR64:
9179 break;
9180 }
9181
9182 r_symndx = ELF64_R_SYM (rel->r_info);
9183 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9184 r_symndx, ibfd))
9185 goto error_ret;
9186
ba761f19 9187 if (sym_sec != toc)
c5614fa4
AM
9188 continue;
9189
ba761f19
AM
9190 if (h != NULL)
9191 val = h->root.u.def.value;
9192 else
9193 {
9194 val = sym->st_value;
9195 if (val != 0)
9196 local_toc_syms = TRUE;
9197 }
9198
9199 val += rel->r_addend;
854b41e7
AM
9200
9201 if (val > toc->rawsize)
9202 val = toc->rawsize;
ba761f19
AM
9203 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9204 continue;
9205 else if ((skip[val >> 3] & can_optimize) != 0)
9206 {
9207 Elf_Internal_Rela *tocrel
425b145b 9208 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9209 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9210
9211 switch (r_type)
9212 {
9213 case R_PPC64_TOC16_HA:
9214 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9215 break;
9216
9217 case R_PPC64_TOC16_LO_DS:
9218 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9219 break;
9220
9221 default:
28942f62
AM
9222 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9223 ppc_howto_init ();
b140b010 9224 info->callbacks->einfo
bc30df16 9225 (_("%P: %H: %s references "
b140b010
AM
9226 "optimized away TOC entry\n"),
9227 ibfd, sec, rel->r_offset,
9228 ppc64_elf_howto_table[r_type]->name);
9229 bfd_set_error (bfd_error_bad_value);
9230 goto error_ret;
ba761f19
AM
9231 }
9232 rel->r_addend = tocrel->r_addend;
9233 elf_section_data (sec)->relocs = relstart;
9234 continue;
9235 }
9236
9237 if (h != NULL || sym->st_value != 0)
9238 continue;
854b41e7
AM
9239
9240 rel->r_addend -= skip[val >> 3];
9241 elf_section_data (sec)->relocs = relstart;
c5614fa4 9242 }
854b41e7
AM
9243
9244 if (elf_section_data (sec)->relocs != relstart)
9245 free (relstart);
c5614fa4
AM
9246 }
9247
9248 /* We shouldn't have local or global symbols defined in the TOC,
9249 but handle them anyway. */
df22d223
AM
9250 if (local_syms != NULL)
9251 for (sym = local_syms;
9252 sym < local_syms + symtab_hdr->sh_info;
9253 ++sym)
9254 if (sym->st_value != 0
9255 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9256 {
9257 unsigned long i;
854b41e7 9258
df22d223
AM
9259 if (sym->st_value > toc->rawsize)
9260 i = toc->rawsize >> 3;
9261 else
9262 i = sym->st_value >> 3;
854b41e7 9263
df22d223
AM
9264 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9265 {
9266 if (local_toc_syms)
9267 (*_bfd_error_handler)
9268 (_("%s defined on removed toc entry"),
9269 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9270 do
9271 ++i;
9272 while ((skip[i] & (ref_from_discarded | can_optimize)));
9273 sym->st_value = (bfd_vma) i << 3;
9274 }
d62b3684 9275
df22d223
AM
9276 sym->st_value -= skip[i];
9277 symtab_hdr->contents = (unsigned char *) local_syms;
9278 }
c5614fa4 9279
854b41e7 9280 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9281 if (toc_inf.global_toc_syms)
9282 {
9283 toc_inf.toc = toc;
9284 toc_inf.skip = skip;
9285 toc_inf.global_toc_syms = FALSE;
9286 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9287 &toc_inf);
9288 }
854b41e7
AM
9289
9290 if (toc->reloc_count != 0)
9291 {
d4730f92 9292 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9293 Elf_Internal_Rela *wrel;
9294 bfd_size_type sz;
9295
854b41e7 9296 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9297 if (toc_relocs == NULL)
9298 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9299 info->keep_memory);
9300 if (toc_relocs == NULL)
9301 goto error_ret;
9302
425b145b
AM
9303 wrel = toc_relocs;
9304 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9305 if ((skip[rel->r_offset >> 3]
9306 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9307 {
9308 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9309 wrel->r_info = rel->r_info;
9310 wrel->r_addend = rel->r_addend;
9311 ++wrel;
9312 }
9313 else if (!dec_dynrel_count (rel->r_info, toc, info,
9314 &local_syms, NULL, NULL))
9315 goto error_ret;
9316
425b145b
AM
9317 elf_section_data (toc)->relocs = toc_relocs;
9318 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9319 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9320 sz = rel_hdr->sh_entsize;
9321 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9322 }
c5614fa4 9323 }
28be611c
AM
9324 else if (toc_relocs != NULL
9325 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9326 free (toc_relocs);
c5614fa4
AM
9327
9328 if (local_syms != NULL
9329 && symtab_hdr->contents != (unsigned char *) local_syms)
9330 {
9331 if (!info->keep_memory)
9332 free (local_syms);
9333 else
9334 symtab_hdr->contents = (unsigned char *) local_syms;
9335 }
9336 free (skip);
9337 }
9338
9339 return TRUE;
9340}
9341
1bbe0902
AM
9342/* Return true iff input section I references the TOC using
9343 instructions limited to +/-32k offsets. */
9344
9345bfd_boolean
9346ppc64_elf_has_small_toc_reloc (asection *i)
9347{
9348 return (is_ppc64_elf (i->owner)
9349 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9350}
9351
927be08e
AM
9352/* Allocate space for one GOT entry. */
9353
9354static void
9355allocate_got (struct elf_link_hash_entry *h,
9356 struct bfd_link_info *info,
9357 struct got_entry *gent)
9358{
9359 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9360 bfd_boolean dyn;
9361 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9362 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9363 ? 16 : 8);
9364 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9365 ? 2 : 1) * sizeof (Elf64_External_Rela);
9366 asection *got = ppc64_elf_tdata (gent->owner)->got;
9367
9368 gent->got.offset = got->size;
9369 got->size += entsize;
9370
9371 dyn = htab->elf.dynamic_sections_created;
19e08130 9372 if (h->type == STT_GNU_IFUNC)
927be08e 9373 {
33e44f2e 9374 htab->elf.irelplt->size += rentsize;
19e08130 9375 htab->got_reli_size += rentsize;
927be08e 9376 }
19e08130
AM
9377 else if ((info->shared
9378 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9379 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9380 || h->root.type != bfd_link_hash_undefweak))
927be08e 9381 {
19e08130 9382 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9383 relgot->size += rentsize;
927be08e
AM
9384 }
9385}
9386
7865406b
AM
9387/* This function merges got entries in the same toc group. */
9388
9389static void
9390merge_got_entries (struct got_entry **pent)
9391{
9392 struct got_entry *ent, *ent2;
9393
9394 for (ent = *pent; ent != NULL; ent = ent->next)
9395 if (!ent->is_indirect)
9396 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9397 if (!ent2->is_indirect
9398 && ent2->addend == ent->addend
9399 && ent2->tls_type == ent->tls_type
9400 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9401 {
9402 ent2->is_indirect = TRUE;
9403 ent2->got.ent = ent;
9404 }
9405}
9406
65f38f15
AM
9407/* Allocate space in .plt, .got and associated reloc sections for
9408 dynamic relocs. */
5bd4f169 9409
b34976b6 9410static bfd_boolean
4ce794b7 9411allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9412{
65f38f15
AM
9413 struct bfd_link_info *info;
9414 struct ppc_link_hash_table *htab;
5bd4f169 9415 asection *s;
65f38f15 9416 struct ppc_link_hash_entry *eh;
6061a67d 9417 struct elf_dyn_relocs *p;
0b8bcf0d 9418 struct got_entry **pgent, *gent;
5bd4f169 9419
e92d460e 9420 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9421 return TRUE;
5bd4f169 9422
65f38f15
AM
9423 info = (struct bfd_link_info *) inf;
9424 htab = ppc_hash_table (info);
4dfe6ac6
NC
9425 if (htab == NULL)
9426 return FALSE;
5bd4f169 9427
e054468f
AM
9428 if ((htab->elf.dynamic_sections_created
9429 && h->dynindx != -1
9430 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9431 || h->type == STT_GNU_IFUNC)
5bd4f169 9432 {
411e1bfb
AM
9433 struct plt_entry *pent;
9434 bfd_boolean doneone = FALSE;
9435 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9436 if (pent->plt.refcount > 0)
9437 {
25f23106
AM
9438 if (!htab->elf.dynamic_sections_created
9439 || h->dynindx == -1)
e054468f 9440 {
33e44f2e 9441 s = htab->elf.iplt;
e054468f 9442 pent->plt.offset = s->size;
b9e5796b 9443 s->size += PLT_ENTRY_SIZE (htab);
33e44f2e 9444 s = htab->elf.irelplt;
e054468f
AM
9445 }
9446 else
9447 {
9448 /* If this is the first .plt entry, make room for the special
9449 first entry. */
33e44f2e 9450 s = htab->elf.splt;
e054468f 9451 if (s->size == 0)
b9e5796b 9452 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
e054468f
AM
9453
9454 pent->plt.offset = s->size;
9455
9456 /* Make room for this entry. */
b9e5796b 9457 s->size += PLT_ENTRY_SIZE (htab);
e054468f
AM
9458
9459 /* Make room for the .glink code. */
9460 s = htab->glink;
9461 if (s->size == 0)
9462 s->size += GLINK_CALL_STUB_SIZE;
b9e5796b
AM
9463 if (htab->opd_abi)
9464 {
9465 /* We need bigger stubs past index 32767. */
9466 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9467 s->size += 4;
9468 s->size += 2*4;
9469 }
9470 else
e054468f 9471 s->size += 4;
e054468f
AM
9472
9473 /* We also need to make an entry in the .rela.plt section. */
33e44f2e 9474 s = htab->elf.srelplt;
e054468f 9475 }
eea6121a 9476 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
9477 doneone = TRUE;
9478 }
9479 else
9480 pent->plt.offset = (bfd_vma) -1;
9481 if (!doneone)
65f38f15 9482 {
411e1bfb 9483 h->plt.plist = NULL;
f5385ebf 9484 h->needs_plt = 0;
65f38f15
AM
9485 }
9486 }
9487 else
9488 {
411e1bfb 9489 h->plt.plist = NULL;
f5385ebf 9490 h->needs_plt = 0;
65f38f15
AM
9491 }
9492
951fd09b
AM
9493 eh = (struct ppc_link_hash_entry *) h;
9494 /* Run through the TLS GD got entries first if we're changing them
9495 to TPREL. */
e7b938ca 9496 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9497 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9498 if (gent->got.refcount > 0
9499 && (gent->tls_type & TLS_GD) != 0)
9500 {
9501 /* This was a GD entry that has been converted to TPREL. If
9502 there happens to be a TPREL entry we can use that one. */
9503 struct got_entry *ent;
9504 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9505 if (ent->got.refcount > 0
9506 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9507 && ent->addend == gent->addend
9508 && ent->owner == gent->owner)
951fd09b
AM
9509 {
9510 gent->got.refcount = 0;
9511 break;
9512 }
9513
9514 /* If not, then we'll be using our own TPREL entry. */
9515 if (gent->got.refcount != 0)
9516 gent->tls_type = TLS_TLS | TLS_TPREL;
9517 }
9518
7865406b
AM
9519 /* Remove any list entry that won't generate a word in the GOT before
9520 we call merge_got_entries. Otherwise we risk merging to empty
9521 entries. */
0b8bcf0d
AM
9522 pgent = &h->got.glist;
9523 while ((gent = *pgent) != NULL)
411e1bfb 9524 if (gent->got.refcount > 0)
7865406b
AM
9525 {
9526 if ((gent->tls_type & TLS_LD) != 0
9527 && !h->def_dynamic)
9528 {
9529 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9530 *pgent = gent->next;
9531 }
9532 else
9533 pgent = &gent->next;
9534 }
9535 else
9536 *pgent = gent->next;
9537
9538 if (!htab->do_multi_toc)
9539 merge_got_entries (&h->got.glist);
9540
9541 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9542 if (!gent->is_indirect)
411e1bfb
AM
9543 {
9544 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9545 Undefined weak syms won't yet be marked as dynamic,
9546 nor will all TLS symbols. */
411e1bfb 9547 if (h->dynindx == -1
b099ab9f 9548 && !h->forced_local
25f23106 9549 && h->type != STT_GNU_IFUNC
b099ab9f 9550 && htab->elf.dynamic_sections_created)
411e1bfb 9551 {
c152c796 9552 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9553 return FALSE;
9554 }
65f38f15 9555
0c8d6e5c 9556 if (!is_ppc64_elf (gent->owner))
927be08e 9557 abort ();
0ffa91dd 9558
927be08e 9559 allocate_got (h, info, gent);
411e1bfb 9560 }
65f38f15 9561
b099ab9f 9562 if (eh->dyn_relocs == NULL
25f23106 9563 || (!htab->elf.dynamic_sections_created
14b5f73f 9564 && h->type != STT_GNU_IFUNC))
b34976b6 9565 return TRUE;
65f38f15
AM
9566
9567 /* In the shared -Bsymbolic case, discard space allocated for
9568 dynamic pc-relative relocs against symbols which turn out to be
9569 defined in regular objects. For the normal shared case, discard
9570 space for relocs that have become local due to symbol visibility
9571 changes. */
9572
9573 if (info->shared)
9574 {
9c7a29a3 9575 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 9576 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
9577 generated via assembly. We want calls to protected symbols to
9578 resolve directly to the function rather than going via the plt.
9579 If people want function pointer comparisons to work as expected
9580 then they should avoid writing weird assembly. */
09695f56 9581 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 9582 {
6061a67d 9583 struct elf_dyn_relocs **pp;
65f38f15
AM
9584
9585 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 9586 {
65f38f15
AM
9587 p->count -= p->pc_count;
9588 p->pc_count = 0;
9589 if (p->count == 0)
9590 *pp = p->next;
9591 else
9592 pp = &p->next;
5bd4f169 9593 }
65f38f15 9594 }
4e795f50
AM
9595
9596 /* Also discard relocs on undefined weak syms with non-default
9597 visibility. */
cab87ef9
AM
9598 if (eh->dyn_relocs != NULL
9599 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
9600 {
9601 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9602 eh->dyn_relocs = NULL;
9603
9604 /* Make sure this symbol is output as a dynamic symbol.
9605 Undefined weak syms won't yet be marked as dynamic. */
9606 else if (h->dynindx == -1
9607 && !h->forced_local)
9608 {
9609 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9610 return FALSE;
9611 }
9612 }
65f38f15 9613 }
25f23106
AM
9614 else if (h->type == STT_GNU_IFUNC)
9615 {
9616 if (!h->non_got_ref)
9617 eh->dyn_relocs = NULL;
9618 }
f4656909 9619 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
9620 {
9621 /* For the non-shared case, discard space for relocs against
9622 symbols which turn out to need copy relocs or are not
9623 dynamic. */
9624
f5385ebf 9625 if (!h->non_got_ref
f5385ebf 9626 && !h->def_regular)
65f38f15
AM
9627 {
9628 /* Make sure this symbol is output as a dynamic symbol.
9629 Undefined weak syms won't yet be marked as dynamic. */
9630 if (h->dynindx == -1
f5385ebf 9631 && !h->forced_local)
65f38f15 9632 {
c152c796 9633 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 9634 return FALSE;
65f38f15
AM
9635 }
9636
9637 /* If that succeeded, we know we'll be keeping all the
9638 relocs. */
9639 if (h->dynindx != -1)
9640 goto keep;
9641 }
9642
9643 eh->dyn_relocs = NULL;
9644
ec338859 9645 keep: ;
65f38f15
AM
9646 }
9647
9648 /* Finally, allocate space. */
9649 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9650 {
9651 asection *sreloc = elf_section_data (p->sec)->sreloc;
19e08130 9652 if (eh->elf.type == STT_GNU_IFUNC)
33e44f2e 9653 sreloc = htab->elf.irelplt;
eea6121a 9654 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9655 }
9656
b34976b6 9657 return TRUE;
65f38f15
AM
9658}
9659
a345bc8d
AM
9660/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9661 to set up space for global entry stubs. These are put in glink,
9662 after the branch table. */
65f38f15 9663
b34976b6 9664static bfd_boolean
a345bc8d 9665size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9666{
a345bc8d
AM
9667 struct bfd_link_info *info;
9668 struct ppc_link_hash_table *htab;
9669 struct plt_entry *pent;
9670 asection *s;
65f38f15 9671
a345bc8d
AM
9672 if (h->root.type == bfd_link_hash_indirect)
9673 return TRUE;
65f38f15 9674
a345bc8d
AM
9675 if (!h->pointer_equality_needed)
9676 return TRUE;
65f38f15 9677
a345bc8d
AM
9678 if (h->def_regular)
9679 return TRUE;
65f38f15 9680
a345bc8d
AM
9681 info = inf;
9682 htab = ppc_hash_table (info);
9683 if (htab == NULL)
9684 return FALSE;
9685
9686 s = htab->glink;
9687 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9688 if (pent->plt.offset != (bfd_vma) -1
9689 && pent->addend == 0)
9690 {
afe397ea
AM
9691 /* For ELFv2, if this symbol is not defined in a regular file
9692 and we are not generating a shared library or pie, then we
9693 need to define the symbol in the executable on a call stub.
9694 This is to avoid text relocations. */
a345bc8d 9695 s->size = (s->size + 15) & -16;
afe397ea
AM
9696 h->root.u.def.section = s;
9697 h->root.u.def.value = s->size;
a345bc8d
AM
9698 s->size += 16;
9699 break;
9700 }
9701 return TRUE;
9702}
9703
9704/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9705 read-only sections. */
9706
9707static bfd_boolean
9708maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9709{
9710 if (h->root.type == bfd_link_hash_indirect)
9711 return TRUE;
9712
9713 if (readonly_dynrelocs (h))
9714 {
9715 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9716
9717 /* Not an error, just cut short the traversal. */
9718 return FALSE;
65f38f15 9719 }
b34976b6 9720 return TRUE;
65f38f15
AM
9721}
9722
9723/* Set the sizes of the dynamic sections. */
9724
b34976b6 9725static bfd_boolean
ee67d69a 9726ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9727 struct bfd_link_info *info)
65f38f15
AM
9728{
9729 struct ppc_link_hash_table *htab;
9730 bfd *dynobj;
9731 asection *s;
b34976b6 9732 bfd_boolean relocs;
65f38f15 9733 bfd *ibfd;
7865406b 9734 struct got_entry *first_tlsld;
65f38f15
AM
9735
9736 htab = ppc_hash_table (info);
4dfe6ac6
NC
9737 if (htab == NULL)
9738 return FALSE;
9739
65f38f15
AM
9740 dynobj = htab->elf.dynobj;
9741 if (dynobj == NULL)
9742 abort ();
9743
9744 if (htab->elf.dynamic_sections_created)
9745 {
9746 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 9747 if (info->executable)
65f38f15 9748 {
3d4d4302 9749 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9750 if (s == NULL)
9751 abort ();
eea6121a 9752 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9753 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9754 }
9755 }
9756
9757 /* Set up .got offsets for local syms, and space for local dynamic
9758 relocs. */
c72f2fb2 9759 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9760 {
411e1bfb
AM
9761 struct got_entry **lgot_ents;
9762 struct got_entry **end_lgot_ents;
e054468f
AM
9763 struct plt_entry **local_plt;
9764 struct plt_entry **end_local_plt;
f961d9dd 9765 unsigned char *lgot_masks;
65f38f15
AM
9766 bfd_size_type locsymcount;
9767 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9768
0c8d6e5c 9769 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9770 continue;
9771
9772 for (s = ibfd->sections; s != NULL; s = s->next)
9773 {
19e08130 9774 struct ppc_dyn_relocs *p;
65f38f15 9775
6edfbbad 9776 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9777 {
ec338859
AM
9778 if (!bfd_is_abs_section (p->sec)
9779 && bfd_is_abs_section (p->sec->output_section))
9780 {
9781 /* Input section has been discarded, either because
9782 it is a copy of a linkonce section or due to
9783 linker script /DISCARD/, so we'll be discarding
9784 the relocs too. */
9785 }
248866a8 9786 else if (p->count != 0)
ec338859 9787 {
19e08130
AM
9788 asection *srel = elf_section_data (p->sec)->sreloc;
9789 if (p->ifunc)
33e44f2e 9790 srel = htab->elf.irelplt;
eea6121a 9791 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9792 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9793 info->flags |= DF_TEXTREL;
ec338859 9794 }
65f38f15
AM
9795 }
9796 }
9797
411e1bfb
AM
9798 lgot_ents = elf_local_got_ents (ibfd);
9799 if (!lgot_ents)
65f38f15
AM
9800 continue;
9801
0ffa91dd 9802 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9803 locsymcount = symtab_hdr->sh_info;
411e1bfb 9804 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9805 local_plt = (struct plt_entry **) end_lgot_ents;
9806 end_local_plt = local_plt + locsymcount;
f961d9dd 9807 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9808 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9809 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9810 {
0b8bcf0d 9811 struct got_entry **pent, *ent;
411e1bfb 9812
0b8bcf0d
AM
9813 pent = lgot_ents;
9814 while ((ent = *pent) != NULL)
411e1bfb
AM
9815 if (ent->got.refcount > 0)
9816 {
e7b938ca 9817 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9818 {
927be08e 9819 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9820 *pent = ent->next;
411e1bfb
AM
9821 }
9822 else
9823 {
19e08130
AM
9824 unsigned int ent_size = 8;
9825 unsigned int rel_size = sizeof (Elf64_External_Rela);
9826
eea6121a 9827 ent->got.offset = s->size;
e7b938ca 9828 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9829 {
19e08130
AM
9830 ent_size *= 2;
9831 rel_size *= 2;
9832 }
9833 s->size += ent_size;
9834 if ((*lgot_masks & PLT_IFUNC) != 0)
9835 {
33e44f2e 9836 htab->elf.irelplt->size += rel_size;
19e08130
AM
9837 htab->got_reli_size += rel_size;
9838 }
9839 else if (info->shared)
9840 {
9841 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9842 srel->size += rel_size;
927be08e 9843 }
0b8bcf0d 9844 pent = &ent->next;
411e1bfb
AM
9845 }
9846 }
9847 else
0b8bcf0d 9848 *pent = ent->next;
65f38f15 9849 }
e054468f
AM
9850
9851 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9852 for (; local_plt < end_local_plt; ++local_plt)
9853 {
9854 struct plt_entry *ent;
9855
9856 for (ent = *local_plt; ent != NULL; ent = ent->next)
9857 if (ent->plt.refcount > 0)
9858 {
33e44f2e 9859 s = htab->elf.iplt;
e054468f 9860 ent->plt.offset = s->size;
b9e5796b 9861 s->size += PLT_ENTRY_SIZE (htab);
e054468f 9862
33e44f2e 9863 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9864 }
9865 else
9866 ent->plt.offset = (bfd_vma) -1;
9867 }
65f38f15
AM
9868 }
9869
9870 /* Allocate global sym .plt and .got entries, and space for global
9871 sym dynamic relocs. */
4ce794b7 9872 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
9873 /* Stash the end of glink branch table. */
9874 if (htab->glink != NULL)
9875 htab->glink->rawsize = htab->glink->size;
9876
9877 if (!htab->opd_abi && !info->shared)
9878 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9879
7865406b 9880 first_tlsld = NULL;
c72f2fb2 9881 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9882 {
7865406b
AM
9883 struct got_entry *ent;
9884
0c8d6e5c 9885 if (!is_ppc64_elf (ibfd))
102890f0
AM
9886 continue;
9887
7865406b
AM
9888 ent = ppc64_tlsld_got (ibfd);
9889 if (ent->got.refcount > 0)
102890f0 9890 {
7865406b 9891 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9892 {
7865406b
AM
9893 ent->is_indirect = TRUE;
9894 ent->got.ent = first_tlsld;
9895 }
9896 else
9897 {
9898 if (first_tlsld == NULL)
9899 first_tlsld = ent;
9900 s = ppc64_elf_tdata (ibfd)->got;
9901 ent->got.offset = s->size;
9902 ent->owner = ibfd;
9903 s->size += 16;
9904 if (info->shared)
9905 {
9906 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9907 srel->size += sizeof (Elf64_External_Rela);
9908 }
102890f0
AM
9909 }
9910 }
9911 else
7865406b 9912 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9913 }
9914
65f38f15
AM
9915 /* We now have determined the sizes of the various dynamic sections.
9916 Allocate memory for them. */
b34976b6 9917 relocs = FALSE;
65f38f15
AM
9918 for (s = dynobj->sections; s != NULL; s = s->next)
9919 {
9920 if ((s->flags & SEC_LINKER_CREATED) == 0)
9921 continue;
9922
4ce794b7 9923 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9924 /* These haven't been allocated yet; don't strip. */
9925 continue;
33e44f2e
AM
9926 else if (s == htab->elf.sgot
9927 || s == htab->elf.splt
9928 || s == htab->elf.iplt
c456f082
AM
9929 || s == htab->glink
9930 || s == htab->dynbss)
65f38f15
AM
9931 {
9932 /* Strip this section if we don't need it; see the
9933 comment below. */
5bd4f169 9934 }
58d180e8
AM
9935 else if (s == htab->glink_eh_frame)
9936 {
9937 if (!bfd_is_abs_section (s->output_section))
9938 /* Not sized yet. */
9939 continue;
9940 }
70cc837d 9941 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9942 {
c456f082 9943 if (s->size != 0)
5bd4f169 9944 {
33e44f2e 9945 if (s != htab->elf.srelplt)
b34976b6 9946 relocs = TRUE;
5bd4f169
AM
9947
9948 /* We use the reloc_count field as a counter if we need
9949 to copy relocs into the output file. */
9950 s->reloc_count = 0;
9951 }
9952 }
65f38f15 9953 else
5bd4f169
AM
9954 {
9955 /* It's not one of our sections, so don't allocate space. */
9956 continue;
9957 }
9958
eea6121a 9959 if (s->size == 0)
5bd4f169 9960 {
c456f082
AM
9961 /* If we don't need this section, strip it from the
9962 output file. This is mostly to handle .rela.bss and
9963 .rela.plt. We must create both sections in
9964 create_dynamic_sections, because they must be created
9965 before the linker maps input sections to output
9966 sections. The linker does that before
9967 adjust_dynamic_symbol is called, and it is that
9968 function which decides whether anything needs to go
9969 into these sections. */
8423293d 9970 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9971 continue;
9972 }
9973
c456f082 9974 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9975 continue;
9976
65f38f15
AM
9977 /* Allocate memory for the section contents. We use bfd_zalloc
9978 here in case unused entries are not reclaimed before the
9979 section's contents are written out. This should not happen,
411e1bfb
AM
9980 but this way if it does we get a R_PPC64_NONE reloc in .rela
9981 sections instead of garbage.
9982 We also rely on the section contents being zero when writing
9983 the GOT. */
eea6121a 9984 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9985 if (s->contents == NULL)
b34976b6 9986 return FALSE;
5bd4f169
AM
9987 }
9988
c72f2fb2 9989 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9990 {
0c8d6e5c 9991 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9992 continue;
9993
e717da7e 9994 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9995 if (s != NULL && s != htab->elf.sgot)
e717da7e 9996 {
eea6121a 9997 if (s->size == 0)
8423293d 9998 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9999 else
10000 {
eea6121a 10001 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10002 if (s->contents == NULL)
10003 return FALSE;
10004 }
10005 }
10006 s = ppc64_elf_tdata (ibfd)->relgot;
10007 if (s != NULL)
10008 {
eea6121a 10009 if (s->size == 0)
8423293d 10010 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10011 else
10012 {
eea6121a 10013 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10014 if (s->contents == NULL)
10015 return FALSE;
10016 relocs = TRUE;
10017 s->reloc_count = 0;
10018 }
10019 }
10020 }
10021
e86ce104 10022 if (htab->elf.dynamic_sections_created)
5bd4f169 10023 {
e8910a83
AM
10024 bfd_boolean tls_opt;
10025
5bd4f169
AM
10026 /* Add some entries to the .dynamic section. We fill in the
10027 values later, in ppc64_elf_finish_dynamic_sections, but we
10028 must add the entries now so that we get the correct size for
10029 the .dynamic section. The DT_DEBUG entry is filled in by the
10030 dynamic linker and used by the debugger. */
dc810e39 10031#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10032 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10033
36af4a4e 10034 if (info->executable)
5bd4f169 10035 {
dc810e39 10036 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10037 return FALSE;
5bd4f169
AM
10038 }
10039
33e44f2e 10040 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10041 {
dc810e39
AM
10042 if (!add_dynamic_entry (DT_PLTGOT, 0)
10043 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10044 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10045 || !add_dynamic_entry (DT_JMPREL, 0)
10046 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10047 return FALSE;
5bd4f169
AM
10048 }
10049
ee67d69a 10050 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10051 {
10052 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10053 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10054 return FALSE;
19397422
AM
10055 }
10056
e7d1c40c 10057 tls_opt = (!htab->params->no_tls_get_addr_opt
e8910a83
AM
10058 && htab->tls_get_addr_fd != NULL
10059 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10060 if (tls_opt || !htab->opd_abi)
10061 {
10062 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10063 return FALSE;
10064 }
a7f2871e 10065
5bd4f169
AM
10066 if (relocs)
10067 {
dc810e39
AM
10068 if (!add_dynamic_entry (DT_RELA, 0)
10069 || !add_dynamic_entry (DT_RELASZ, 0)
10070 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10071 return FALSE;
5bd4f169 10072
65f38f15
AM
10073 /* If any dynamic relocs apply to a read-only section,
10074 then we need a DT_TEXTREL entry. */
248866a8 10075 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10076 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10077
65f38f15 10078 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10079 {
65f38f15 10080 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10081 return FALSE;
5bd4f169 10082 }
5bd4f169 10083 }
5bd4f169 10084 }
65f38f15 10085#undef add_dynamic_entry
5bd4f169 10086
b34976b6 10087 return TRUE;
5bd4f169
AM
10088}
10089
a345bc8d
AM
10090/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10091
10092static bfd_boolean
10093ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10094{
10095 if (h->plt.plist != NULL
10096 && !h->def_regular
10097 && !h->pointer_equality_needed)
10098 return FALSE;
10099
10100 return _bfd_elf_hash_symbol (h);
10101}
10102
721956f4 10103/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10104
4ce794b7
AM
10105static inline enum ppc_stub_type
10106ppc_type_of_stub (asection *input_sec,
10107 const Elf_Internal_Rela *rel,
10108 struct ppc_link_hash_entry **hash,
e054468f 10109 struct plt_entry **plt_ent,
6911b7dc
AM
10110 bfd_vma destination,
10111 unsigned long local_off)
5bd4f169 10112{
721956f4
AM
10113 struct ppc_link_hash_entry *h = *hash;
10114 bfd_vma location;
10115 bfd_vma branch_offset;
10116 bfd_vma max_branch_offset;
4ce794b7 10117 enum elf_ppc64_reloc_type r_type;
5bd4f169 10118
721956f4
AM
10119 if (h != NULL)
10120 {
e054468f 10121 struct plt_entry *ent;
7fe2b9a6 10122 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10123 if (h->oh != NULL
10124 && h->oh->is_func_descriptor)
7b8f6675
AM
10125 {
10126 fdh = ppc_follow_link (h->oh);
10127 *hash = fdh;
10128 }
8387904d 10129
e054468f
AM
10130 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10131 if (ent->addend == rel->r_addend
10132 && ent->plt.offset != (bfd_vma) -1)
10133 {
e054468f
AM
10134 *plt_ent = ent;
10135 return ppc_stub_plt_call;
10136 }
5bd4f169 10137
7fe2b9a6
AM
10138 /* Here, we know we don't have a plt entry. If we don't have a
10139 either a defined function descriptor or a defined entry symbol
10140 in a regular object file, then it is pointless trying to make
10141 any other type of stub. */
854b41e7
AM
10142 if (!is_static_defined (&fdh->elf)
10143 && !is_static_defined (&h->elf))
721956f4 10144 return ppc_stub_none;
5d1634d7 10145 }
e054468f
AM
10146 else if (elf_local_got_ents (input_sec->owner) != NULL)
10147 {
10148 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10149 struct plt_entry **local_plt = (struct plt_entry **)
10150 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10151 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10152
10153 if (local_plt[r_symndx] != NULL)
10154 {
10155 struct plt_entry *ent;
10156
10157 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10158 if (ent->addend == rel->r_addend
10159 && ent->plt.offset != (bfd_vma) -1)
10160 {
10161 *plt_ent = ent;
10162 return ppc_stub_plt_call;
10163 }
10164 }
10165 }
5d1634d7 10166
721956f4
AM
10167 /* Determine where the call point is. */
10168 location = (input_sec->output_offset
10169 + input_sec->output_section->vma
10170 + rel->r_offset);
5d1634d7 10171
721956f4
AM
10172 branch_offset = destination - location;
10173 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10174
721956f4
AM
10175 /* Determine if a long branch stub is needed. */
10176 max_branch_offset = 1 << 25;
4ce794b7 10177 if (r_type != R_PPC64_REL24)
721956f4 10178 max_branch_offset = 1 << 15;
5d1634d7 10179
6911b7dc 10180 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10181 /* We need a stub. Figure out whether a long_branch or plt_branch
10182 is needed later. */
10183 return ppc_stub_long_branch;
5d1634d7 10184
721956f4 10185 return ppc_stub_none;
5d1634d7
AM
10186}
10187
794e51c0
AM
10188/* With power7 weakly ordered memory model, it is possible for ld.so
10189 to update a plt entry in one thread and have another thread see a
10190 stale zero toc entry. To avoid this we need some sort of acquire
10191 barrier in the call stub. One solution is to make the load of the
10192 toc word seem to appear to depend on the load of the function entry
10193 word. Another solution is to test for r2 being zero, and branch to
10194 the appropriate glink entry if so.
10195
10196 . fake dep barrier compare
71a39c98
AM
10197 . ld 12,xxx(2) ld 12,xxx(2)
10198 . mtctr 12 mtctr 12
10199 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10200 . add 2,2,11 cmpldi 2,0
10201 . ld 2,xxx+8(2) bnectr+
10202 . bctr b <glink_entry>
10203
10204 The solution involving the compare turns out to be faster, so
10205 that's what we use unless the branch won't reach. */
10206
10207#define ALWAYS_USE_FAKE_DEP 0
10208#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10209
5d1634d7
AM
10210#define PPC_LO(v) ((v) & 0xffff)
10211#define PPC_HI(v) (((v) >> 16) & 0xffff)
10212#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10213
794e51c0
AM
10214static inline unsigned int
10215plt_stub_size (struct ppc_link_hash_table *htab,
10216 struct ppc_stub_hash_entry *stub_entry,
10217 bfd_vma off)
10218{
b9e5796b
AM
10219 unsigned size = 12;
10220
10221 if (ALWAYS_EMIT_R2SAVE
10222 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10223 size += 4;
10224 if (PPC_HA (off) != 0)
794e51c0 10225 size += 4;
b9e5796b
AM
10226 if (htab->opd_abi)
10227 {
10228 size += 4;
e7d1c40c 10229 if (htab->params->plt_static_chain)
b9e5796b 10230 size += 4;
bd4d2eaa
AM
10231 if (htab->params->plt_thread_safe
10232 && htab->elf.dynamic_sections_created
10233 && stub_entry->h != NULL
10234 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10235 size += 8;
e7d1c40c 10236 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10237 size += 4;
10238 }
794e51c0
AM
10239 if (stub_entry->h != NULL
10240 && (stub_entry->h == htab->tls_get_addr_fd
10241 || stub_entry->h == htab->tls_get_addr)
e7d1c40c 10242 && !htab->params->no_tls_get_addr_opt)
794e51c0
AM
10243 size += 13 * 4;
10244 return size;
10245}
10246
10247/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10248 then return the padding needed to do so. */
10249static inline unsigned int
10250plt_stub_pad (struct ppc_link_hash_table *htab,
10251 struct ppc_stub_hash_entry *stub_entry,
10252 bfd_vma plt_off)
10253{
e7d1c40c 10254 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0
AM
10255 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10256 bfd_vma stub_off = stub_entry->stub_sec->size;
10257
10258 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10259 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10260 return stub_align - (stub_off & (stub_align - 1));
10261 return 0;
10262}
10263
10264/* Build a .plt call stub. */
10265
10266static inline bfd_byte *
10267build_plt_stub (struct ppc_link_hash_table *htab,
10268 struct ppc_stub_hash_entry *stub_entry,
10269 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10270{
e7d1c40c 10271 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10272 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10273 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10274 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10275 && htab->elf.dynamic_sections_created
10276 && stub_entry->h != NULL
10277 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10278 bfd_boolean use_fake_dep = plt_thread_safe;
10279 bfd_vma cmp_branch_off = 0;
10280
10281 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10282 && plt_load_toc
794e51c0 10283 && plt_thread_safe
bd4d2eaa
AM
10284 && !((stub_entry->h == htab->tls_get_addr_fd
10285 || stub_entry->h == htab->tls_get_addr)
e7d1c40c 10286 && !htab->params->no_tls_get_addr_opt))
794e51c0
AM
10287 {
10288 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10289 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10290 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10291 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10292 bfd_vma to, from;
10293
68d62958
AM
10294 if (pltindex > 32768)
10295 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10296 to = (glinkoff
10297 + htab->glink->output_offset
10298 + htab->glink->output_section->vma);
10299 from = (p - stub_entry->stub_sec->contents
10300 + 4 * (ALWAYS_EMIT_R2SAVE
10301 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10302 + 4 * (PPC_HA (offset) != 0)
10303 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10304 != PPC_HA (offset))
10305 + 4 * (plt_static_chain != 0)
10306 + 20
10307 + stub_entry->stub_sec->output_offset
10308 + stub_entry->stub_sec->output_section->vma);
10309 cmp_branch_off = to - from;
10310 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10311 }
10312
ac2df442
AM
10313 if (PPC_HA (offset) != 0)
10314 {
176a0d42
AM
10315 if (r != NULL)
10316 {
794e51c0
AM
10317 if (ALWAYS_EMIT_R2SAVE
10318 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10319 r[0].r_offset += 4;
176a0d42 10320 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10321 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10322 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10323 r[1].r_addend = r[0].r_addend;
b9e5796b 10324 if (plt_load_toc)
176a0d42 10325 {
b9e5796b 10326 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10327 {
b9e5796b
AM
10328 r[2].r_offset = r[1].r_offset + 4;
10329 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10330 r[2].r_addend = r[0].r_addend;
10331 }
10332 else
10333 {
10334 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10335 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10336 r[2].r_addend = r[0].r_addend + 8;
10337 if (plt_static_chain)
10338 {
10339 r[3].r_offset = r[2].r_offset + 4;
10340 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10341 r[3].r_addend = r[0].r_addend + 16;
10342 }
c7131b65 10343 }
176a0d42
AM
10344 }
10345 }
794e51c0
AM
10346 if (ALWAYS_EMIT_R2SAVE
10347 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10348 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10349 if (plt_load_toc)
10350 {
10351 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10352 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10353 }
10354 else
10355 {
10356 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10357 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10358 }
b9e5796b
AM
10359 if (plt_load_toc
10360 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10361 {
71a39c98 10362 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10363 offset = 0;
10364 }
71a39c98 10365 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10366 if (plt_load_toc)
794e51c0 10367 {
b9e5796b
AM
10368 if (use_fake_dep)
10369 {
10370 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10371 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10372 }
10373 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10374 if (plt_static_chain)
10375 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10376 }
ac2df442
AM
10377 }
10378 else
10379 {
176a0d42
AM
10380 if (r != NULL)
10381 {
794e51c0
AM
10382 if (ALWAYS_EMIT_R2SAVE
10383 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10384 r[0].r_offset += 4;
176a0d42 10385 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10386 if (plt_load_toc)
176a0d42 10387 {
b9e5796b 10388 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10389 {
b9e5796b
AM
10390 r[1].r_offset = r[0].r_offset + 4;
10391 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10392 r[1].r_addend = r[0].r_addend;
10393 }
10394 else
10395 {
10396 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10397 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10398 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10399 if (plt_static_chain)
10400 {
10401 r[2].r_offset = r[1].r_offset + 4;
10402 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10403 r[2].r_addend = r[0].r_addend + 8;
10404 }
c7131b65 10405 }
176a0d42
AM
10406 }
10407 }
794e51c0
AM
10408 if (ALWAYS_EMIT_R2SAVE
10409 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10410 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10411 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10412 if (plt_load_toc
10413 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10414 {
10415 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10416 offset = 0;
10417 }
71a39c98 10418 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10419 if (plt_load_toc)
794e51c0 10420 {
b9e5796b
AM
10421 if (use_fake_dep)
10422 {
10423 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10424 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10425 }
10426 if (plt_static_chain)
10427 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10428 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10429 }
ac2df442 10430 }
b9e5796b 10431 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10432 {
10433 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10434 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10435 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10436 }
10437 else
10438 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10439 return p;
10440}
10441
a7f2871e
AM
10442/* Build a special .plt call stub for __tls_get_addr. */
10443
10444#define LD_R11_0R3 0xe9630000
10445#define LD_R12_0R3 0xe9830000
10446#define MR_R0_R3 0x7c601b78
10447#define CMPDI_R11_0 0x2c2b0000
10448#define ADD_R3_R12_R13 0x7c6c6a14
10449#define BEQLR 0x4d820020
10450#define MR_R3_R0 0x7c030378
a7f2871e
AM
10451#define STD_R11_0R1 0xf9610000
10452#define BCTRL 0x4e800421
10453#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10454#define MTLR_R11 0x7d6803a6
10455
10456static inline bfd_byte *
794e51c0
AM
10457build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10458 struct ppc_stub_hash_entry *stub_entry,
10459 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10460{
e7d1c40c 10461 bfd *obfd = htab->params->stub_bfd;
794e51c0 10462
a7f2871e
AM
10463 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10464 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10465 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10466 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10467 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10468 bfd_put_32 (obfd, BEQLR, p), p += 4;
10469 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10470 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10471 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10472
10473 if (r != NULL)
10474 r[0].r_offset += 9 * 4;
794e51c0 10475 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10476 bfd_put_32 (obfd, BCTRL, p - 4);
10477
a078d95a 10478 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10479 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10480 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10481 bfd_put_32 (obfd, BLR, p), p += 4;
10482
10483 return p;
10484}
10485
176a0d42
AM
10486static Elf_Internal_Rela *
10487get_relocs (asection *sec, int count)
10488{
10489 Elf_Internal_Rela *relocs;
10490 struct bfd_elf_section_data *elfsec_data;
10491
10492 elfsec_data = elf_section_data (sec);
10493 relocs = elfsec_data->relocs;
10494 if (relocs == NULL)
10495 {
10496 bfd_size_type relsize;
10497 relsize = sec->reloc_count * sizeof (*relocs);
10498 relocs = bfd_alloc (sec->owner, relsize);
10499 if (relocs == NULL)
10500 return NULL;
10501 elfsec_data->relocs = relocs;
d4730f92
BS
10502 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10503 sizeof (Elf_Internal_Shdr));
10504 if (elfsec_data->rela.hdr == NULL)
10505 return NULL;
10506 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10507 * sizeof (Elf64_External_Rela));
10508 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10509 sec->reloc_count = 0;
10510 }
10511 relocs += sec->reloc_count;
10512 sec->reloc_count += count;
10513 return relocs;
10514}
10515
aa374f67 10516static bfd_vma
25f53a85 10517get_r2off (struct bfd_link_info *info,
aa374f67
AM
10518 struct ppc_stub_hash_entry *stub_entry)
10519{
25f53a85 10520 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
10521 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10522
10523 if (r2off == 0)
10524 {
10525 /* Support linking -R objects. Get the toc pointer from the
10526 opd entry. */
10527 char buf[8];
b9e5796b
AM
10528 if (!htab->opd_abi)
10529 return r2off;
aa374f67
AM
10530 asection *opd = stub_entry->h->elf.root.u.def.section;
10531 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10532
10533 if (strcmp (opd->name, ".opd") != 0
10534 || opd->reloc_count != 0)
10535 {
bc30df16 10536 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10537 stub_entry->h->elf.root.root.string);
aa374f67
AM
10538 bfd_set_error (bfd_error_bad_value);
10539 return 0;
10540 }
10541 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10542 return 0;
10543 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10544 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
10545 }
10546 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10547 return r2off;
10548}
10549
b34976b6 10550static bfd_boolean
4ce794b7 10551ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10552{
721956f4
AM
10553 struct ppc_stub_hash_entry *stub_entry;
10554 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10555 struct bfd_link_info *info;
10556 struct ppc_link_hash_table *htab;
721956f4
AM
10557 bfd_byte *loc;
10558 bfd_byte *p;
ee75fd95 10559 bfd_vma dest, off;
721956f4 10560 int size;
176a0d42 10561 Elf_Internal_Rela *r;
e054468f 10562 asection *plt;
5d1634d7 10563
721956f4
AM
10564 /* Massage our args to the form they really have. */
10565 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10566 info = in_arg;
5d1634d7 10567
5d1634d7 10568 htab = ppc_hash_table (info);
4dfe6ac6
NC
10569 if (htab == NULL)
10570 return FALSE;
5d1634d7 10571
721956f4 10572 /* Make a note of the offset within the stubs for this entry. */
eea6121a 10573 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 10574 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 10575
4ce794b7 10576 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10577 switch (stub_entry->stub_type)
5d1634d7 10578 {
721956f4 10579 case ppc_stub_long_branch:
ad8e1ba5 10580 case ppc_stub_long_branch_r2off:
721956f4 10581 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10582 dest = (stub_entry->target_value
10583 + stub_entry->target_section->output_offset
10584 + stub_entry->target_section->output_section->vma);
10585 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10586 off = dest;
5d1634d7 10587
721956f4
AM
10588 /* And this is where we are coming from. */
10589 off -= (stub_entry->stub_offset
97b639ba
AM
10590 + stub_entry->stub_sec->output_offset
10591 + stub_entry->stub_sec->output_section->vma);
e86ce104 10592
ac2df442
AM
10593 size = 4;
10594 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10595 {
25f53a85 10596 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10597
aa374f67
AM
10598 if (r2off == 0)
10599 {
10600 htab->stub_error = TRUE;
10601 return FALSE;
10602 }
e7d1c40c 10603 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10604 loc += 4;
ac2df442
AM
10605 size = 12;
10606 if (PPC_HA (r2off) != 0)
10607 {
10608 size = 16;
e7d1c40c
AM
10609 bfd_put_32 (htab->params->stub_bfd,
10610 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442
AM
10611 loc += 4;
10612 }
e7d1c40c 10613 bfd_put_32 (htab->params->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 10614 loc += 4;
ac2df442 10615 off -= size - 4;
ad8e1ba5 10616 }
e7d1c40c 10617 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10618
5c3dead3
AM
10619 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10620 {
bc30df16
AM
10621 info->callbacks->einfo
10622 (_("%P: long branch stub `%s' offset overflow\n"),
10623 stub_entry->root.string);
5c3dead3
AM
10624 htab->stub_error = TRUE;
10625 return FALSE;
10626 }
ee75fd95
AM
10627
10628 if (info->emitrelocations)
10629 {
176a0d42
AM
10630 r = get_relocs (stub_entry->stub_sec, 1);
10631 if (r == NULL)
10632 return FALSE;
ee75fd95
AM
10633 r->r_offset = loc - stub_entry->stub_sec->contents;
10634 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10635 r->r_addend = dest;
10636 if (stub_entry->h != NULL)
10637 {
10638 struct elf_link_hash_entry **hashes;
10639 unsigned long symndx;
10640 struct ppc_link_hash_entry *h;
10641
e7d1c40c 10642 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10643 if (hashes == NULL)
10644 {
10645 bfd_size_type hsize;
10646
10647 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10648 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10649 if (hashes == NULL)
10650 return FALSE;
e7d1c40c 10651 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10652 htab->stub_globals = 1;
10653 }
10654 symndx = htab->stub_globals++;
10655 h = stub_entry->h;
10656 hashes[symndx] = &h->elf;
10657 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10658 if (h->oh != NULL && h->oh->is_func)
b31867b6 10659 h = ppc_follow_link (h->oh);
ee75fd95
AM
10660 if (h->elf.root.u.def.section != stub_entry->target_section)
10661 /* H is an opd symbol. The addend must be zero. */
10662 r->r_addend = 0;
10663 else
10664 {
10665 off = (h->elf.root.u.def.value
10666 + h->elf.root.u.def.section->output_offset
10667 + h->elf.root.u.def.section->output_section->vma);
10668 r->r_addend -= off;
10669 }
10670 }
10671 }
721956f4 10672 break;
e86ce104 10673
721956f4 10674 case ppc_stub_plt_branch:
ad8e1ba5 10675 case ppc_stub_plt_branch_r2off:
721956f4
AM
10676 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10677 stub_entry->root.string + 9,
b34976b6 10678 FALSE, FALSE);
721956f4
AM
10679 if (br_entry == NULL)
10680 {
8de848d8 10681 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10682 stub_entry->root.string);
b34976b6
AM
10683 htab->stub_error = TRUE;
10684 return FALSE;
721956f4
AM
10685 }
10686
176a0d42
AM
10687 dest = (stub_entry->target_value
10688 + stub_entry->target_section->output_offset
10689 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10690 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10691 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10692
176a0d42 10693 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10694 htab->brlt->contents + br_entry->offset);
721956f4 10695
f94498ff 10696 if (br_entry->iter == htab->stub_iteration)
721956f4 10697 {
f94498ff 10698 br_entry->iter = 0;
84f5d08e 10699
f94498ff 10700 if (htab->relbrlt != NULL)
84f5d08e 10701 {
f94498ff
AM
10702 /* Create a reloc for the branch lookup table entry. */
10703 Elf_Internal_Rela rela;
10704 bfd_byte *rl;
10705
10706 rela.r_offset = (br_entry->offset
10707 + htab->brlt->output_offset
10708 + htab->brlt->output_section->vma);
10709 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10710 rela.r_addend = dest;
f94498ff
AM
10711
10712 rl = htab->relbrlt->contents;
10713 rl += (htab->relbrlt->reloc_count++
10714 * sizeof (Elf64_External_Rela));
10715 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10716 }
10717 else if (info->emitrelocations)
10718 {
176a0d42
AM
10719 r = get_relocs (htab->brlt, 1);
10720 if (r == NULL)
10721 return FALSE;
10722 /* brlt, being SEC_LINKER_CREATED does not go through the
10723 normal reloc processing. Symbols and offsets are not
10724 translated from input file to output file form, so
10725 set up the offset per the output file. */
f94498ff
AM
10726 r->r_offset = (br_entry->offset
10727 + htab->brlt->output_offset
10728 + htab->brlt->output_section->vma);
10729 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10730 r->r_addend = dest;
84f5d08e 10731 }
84f5d08e 10732 }
721956f4 10733
176a0d42
AM
10734 dest = (br_entry->offset
10735 + htab->brlt->output_offset
10736 + htab->brlt->output_section->vma);
10737
10738 off = (dest
4ce794b7 10739 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 10740 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10741
ad8e1ba5 10742 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10743 {
25f53a85 10744 info->callbacks->einfo
bc30df16 10745 (_("%P: linkage table error against `%T'\n"),
721956f4 10746 stub_entry->root.string);
5d1634d7 10747 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10748 htab->stub_error = TRUE;
10749 return FALSE;
5d1634d7 10750 }
41bd81ab 10751
176a0d42
AM
10752 if (info->emitrelocations)
10753 {
10754 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10755 if (r == NULL)
10756 return FALSE;
10757 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10758 if (bfd_big_endian (info->output_bfd))
10759 r[0].r_offset += 2;
00f412ee 10760 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
10761 r[0].r_offset += 4;
10762 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10763 r[0].r_addend = dest;
10764 if (PPC_HA (off) != 0)
10765 {
10766 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10767 r[1].r_offset = r[0].r_offset + 4;
10768 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10769 r[1].r_addend = r[0].r_addend;
10770 }
10771 }
10772
00f412ee 10773 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 10774 {
176a0d42 10775 if (PPC_HA (off) != 0)
ac2df442
AM
10776 {
10777 size = 16;
e7d1c40c 10778 bfd_put_32 (htab->params->stub_bfd,
397998fc 10779 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10780 loc += 4;
e7d1c40c 10781 bfd_put_32 (htab->params->stub_bfd,
397998fc 10782 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
10783 }
10784 else
10785 {
10786 size = 12;
e7d1c40c
AM
10787 bfd_put_32 (htab->params->stub_bfd,
10788 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 10789 }
ad8e1ba5
AM
10790 }
10791 else
10792 {
25f53a85 10793 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 10794
00f412ee 10795 if (r2off == 0 && htab->opd_abi)
aa374f67
AM
10796 {
10797 htab->stub_error = TRUE;
10798 return FALSE;
10799 }
ad8e1ba5 10800
e7d1c40c 10801 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10802 loc += 4;
00f412ee 10803 size = 16;
176a0d42 10804 if (PPC_HA (off) != 0)
ac2df442
AM
10805 {
10806 size += 4;
e7d1c40c 10807 bfd_put_32 (htab->params->stub_bfd,
397998fc 10808 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10809 loc += 4;
e7d1c40c 10810 bfd_put_32 (htab->params->stub_bfd,
397998fc 10811 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
10812 }
10813 else
e7d1c40c 10814 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
10815
10816 if (PPC_HA (r2off) != 0)
10817 {
10818 size += 4;
00f412ee 10819 loc += 4;
e7d1c40c
AM
10820 bfd_put_32 (htab->params->stub_bfd,
10821 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
10822 }
10823 if (PPC_LO (r2off) != 0)
10824 {
10825 size += 4;
ac2df442 10826 loc += 4;
e7d1c40c
AM
10827 bfd_put_32 (htab->params->stub_bfd,
10828 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 10829 }
ad8e1ba5
AM
10830 }
10831 loc += 4;
e7d1c40c 10832 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 10833 loc += 4;
e7d1c40c 10834 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
721956f4 10835 break;
5d1634d7 10836
721956f4 10837 case ppc_stub_plt_call:
794e51c0 10838 case ppc_stub_plt_call_r2save:
e054468f 10839 if (stub_entry->h != NULL
b31867b6
AM
10840 && stub_entry->h->is_func_descriptor
10841 && stub_entry->h->oh != NULL)
c862ae31 10842 {
b31867b6
AM
10843 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10844
10845 /* If the old-ABI "dot-symbol" is undefined make it weak so
10846 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10847 FIXME: We used to define the symbol on one of the call
10848 stubs instead, which is why we test symbol section id
10849 against htab->top_id in various places. Likely all
10850 these checks could now disappear. */
10851 if (fh->elf.root.type == bfd_link_hash_undefined)
10852 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
10853 /* Stop undo_symbol_twiddle changing it back to undefined. */
10854 fh->was_undefined = 0;
c862ae31
AM
10855 }
10856
721956f4 10857 /* Now build the stub. */
e054468f 10858 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 10859 if (dest >= (bfd_vma) -2)
721956f4
AM
10860 abort ();
10861
33e44f2e 10862 plt = htab->elf.splt;
25f23106
AM
10863 if (!htab->elf.dynamic_sections_created
10864 || stub_entry->h == NULL
10865 || stub_entry->h->elf.dynindx == -1)
33e44f2e 10866 plt = htab->elf.iplt;
e054468f
AM
10867
10868 dest += plt->output_offset + plt->output_section->vma;
10869
10870 if (stub_entry->h == NULL
10871 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10872 {
10873 Elf_Internal_Rela rela;
10874 bfd_byte *rl;
10875
10876 rela.r_offset = dest;
ee67d69a
AM
10877 if (htab->opd_abi)
10878 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10879 else
10880 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
10881 rela.r_addend = (stub_entry->target_value
10882 + stub_entry->target_section->output_offset
10883 + stub_entry->target_section->output_section->vma);
10884
33e44f2e
AM
10885 rl = (htab->elf.irelplt->contents
10886 + (htab->elf.irelplt->reloc_count++
25f23106
AM
10887 * sizeof (Elf64_External_Rela)));
10888 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
10889 stub_entry->plt_ent->plt.offset |= 1;
10890 }
176a0d42
AM
10891
10892 off = (dest
e054468f 10893 - elf_gp (plt->output_section->owner)
176a0d42 10894 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10895
ad8e1ba5 10896 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10897 {
25f53a85 10898 info->callbacks->einfo
bc30df16 10899 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
10900 stub_entry->h != NULL
10901 ? stub_entry->h->elf.root.root.string
10902 : "<local sym>");
721956f4 10903 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10904 htab->stub_error = TRUE;
10905 return FALSE;
721956f4
AM
10906 }
10907
e7d1c40c 10908 if (htab->params->plt_stub_align != 0)
794e51c0
AM
10909 {
10910 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10911
10912 stub_entry->stub_sec->size += pad;
10913 stub_entry->stub_offset = stub_entry->stub_sec->size;
10914 loc += pad;
10915 }
10916
176a0d42
AM
10917 r = NULL;
10918 if (info->emitrelocations)
10919 {
10920 r = get_relocs (stub_entry->stub_sec,
3ba720c7
AM
10921 ((PPC_HA (off) != 0)
10922 + (htab->opd_abi
e7d1c40c 10923 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
10924 && PPC_HA (off + 16) == PPC_HA (off))
10925 : 1)));
176a0d42
AM
10926 if (r == NULL)
10927 return FALSE;
10928 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10929 if (bfd_big_endian (info->output_bfd))
10930 r[0].r_offset += 2;
176a0d42
AM
10931 r[0].r_addend = dest;
10932 }
a7f2871e
AM
10933 if (stub_entry->h != NULL
10934 && (stub_entry->h == htab->tls_get_addr_fd
10935 || stub_entry->h == htab->tls_get_addr)
e7d1c40c 10936 && !htab->params->no_tls_get_addr_opt)
794e51c0 10937 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10938 else
794e51c0 10939 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10940 size = p - loc;
10941 break;
10942
10943 default:
10944 BFD_FAIL ();
b34976b6 10945 return FALSE;
721956f4
AM
10946 }
10947
eea6121a 10948 stub_entry->stub_sec->size += size;
97b639ba 10949
e7d1c40c 10950 if (htab->params->emit_stub_syms)
97b639ba
AM
10951 {
10952 struct elf_link_hash_entry *h;
ee75fd95
AM
10953 size_t len1, len2;
10954 char *name;
10955 const char *const stub_str[] = { "long_branch",
10956 "long_branch_r2off",
10957 "plt_branch",
10958 "plt_branch_r2off",
794e51c0 10959 "plt_call",
ee75fd95
AM
10960 "plt_call" };
10961
10962 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10963 len2 = strlen (stub_entry->root.string);
10964 name = bfd_malloc (len1 + len2 + 2);
10965 if (name == NULL)
10966 return FALSE;
10967 memcpy (name, stub_entry->root.string, 9);
10968 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10969 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10970 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10971 if (h == NULL)
10972 return FALSE;
10973 if (h->root.type == bfd_link_hash_new)
10974 {
10975 h->root.type = bfd_link_hash_defined;
10976 h->root.u.def.section = stub_entry->stub_sec;
10977 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10978 h->ref_regular = 1;
10979 h->def_regular = 1;
10980 h->ref_regular_nonweak = 1;
10981 h->forced_local = 1;
10982 h->non_elf = 0;
2ec55de3 10983 h->root.linker_def = 1;
97b639ba
AM
10984 }
10985 }
10986
b34976b6 10987 return TRUE;
721956f4
AM
10988}
10989
10990/* As above, but don't actually build the stub. Just bump offset so
10991 we know stub section sizes, and select plt_branch stubs where
10992 long_branch stubs won't do. */
10993
b34976b6 10994static bfd_boolean
4ce794b7 10995ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10996{
10997 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10998 struct bfd_link_info *info;
721956f4
AM
10999 struct ppc_link_hash_table *htab;
11000 bfd_vma off;
11001 int size;
11002
11003 /* Massage our args to the form they really have. */
11004 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11005 info = in_arg;
11006
11007 htab = ppc_hash_table (info);
4dfe6ac6
NC
11008 if (htab == NULL)
11009 return FALSE;
721956f4 11010
794e51c0
AM
11011 if (stub_entry->stub_type == ppc_stub_plt_call
11012 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11013 {
e054468f
AM
11014 asection *plt;
11015 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11016 if (off >= (bfd_vma) -2)
411e1bfb 11017 abort ();
33e44f2e 11018 plt = htab->elf.splt;
25f23106
AM
11019 if (!htab->elf.dynamic_sections_created
11020 || stub_entry->h == NULL
11021 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11022 plt = htab->elf.iplt;
e054468f
AM
11023 off += (plt->output_offset
11024 + plt->output_section->vma
11025 - elf_gp (plt->output_section->owner)
ad8e1ba5 11026 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 11027
794e51c0 11028 size = plt_stub_size (htab, stub_entry, off);
e7d1c40c 11029 if (htab->params->plt_stub_align)
794e51c0 11030 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11031 if (info->emitrelocations)
11032 {
11033 stub_entry->stub_sec->reloc_count
b9e5796b
AM
11034 += ((PPC_HA (off) != 0)
11035 + (htab->opd_abi
e7d1c40c 11036 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11037 && PPC_HA (off + 16) == PPC_HA (off))
11038 : 1));
176a0d42
AM
11039 stub_entry->stub_sec->flags |= SEC_RELOC;
11040 }
721956f4
AM
11041 }
11042 else
11043 {
ad8e1ba5
AM
11044 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11045 variants. */
ac2df442 11046 bfd_vma r2off = 0;
6911b7dc 11047 bfd_vma local_off = 0;
ac2df442 11048
721956f4
AM
11049 off = (stub_entry->target_value
11050 + stub_entry->target_section->output_offset
11051 + stub_entry->target_section->output_section->vma);
eea6121a 11052 off -= (stub_entry->stub_sec->size
721956f4
AM
11053 + stub_entry->stub_sec->output_offset
11054 + stub_entry->stub_sec->output_section->vma);
11055
ad8e1ba5
AM
11056 /* Reset the stub type from the plt variant in case we now
11057 can reach with a shorter stub. */
11058 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11059 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11060
11061 size = 4;
11062 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11063 {
25f53a85 11064 r2off = get_r2off (info, stub_entry);
b9e5796b 11065 if (r2off == 0 && htab->opd_abi)
aa374f67
AM
11066 {
11067 htab->stub_error = TRUE;
11068 return FALSE;
11069 }
ac2df442
AM
11070 size = 12;
11071 if (PPC_HA (r2off) != 0)
11072 size = 16;
11073 off -= size - 4;
ad8e1ba5
AM
11074 }
11075
6911b7dc
AM
11076 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11077
b9e5796b
AM
11078 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11079 Do the same for -R objects without function descriptors. */
11080 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11081 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11082 && r2off == 0))
721956f4
AM
11083 {
11084 struct ppc_branch_hash_entry *br_entry;
11085
11086 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11087 stub_entry->root.string + 9,
b34976b6 11088 TRUE, FALSE);
721956f4
AM
11089 if (br_entry == NULL)
11090 {
8de848d8 11091 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11092 stub_entry->root.string);
b34976b6
AM
11093 htab->stub_error = TRUE;
11094 return FALSE;
721956f4
AM
11095 }
11096
11097 if (br_entry->iter != htab->stub_iteration)
11098 {
11099 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11100 br_entry->offset = htab->brlt->size;
11101 htab->brlt->size += 8;
63bc6f6c 11102
ee75fd95 11103 if (htab->relbrlt != NULL)
eea6121a 11104 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11105 else if (info->emitrelocations)
11106 {
11107 htab->brlt->reloc_count += 1;
11108 htab->brlt->flags |= SEC_RELOC;
11109 }
721956f4 11110 }
ad8e1ba5
AM
11111
11112 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11113 off = (br_entry->offset
11114 + htab->brlt->output_offset
11115 + htab->brlt->output_section->vma
11116 - elf_gp (htab->brlt->output_section->owner)
11117 - htab->stub_group[stub_entry->id_sec->id].toc_off);
11118
176a0d42
AM
11119 if (info->emitrelocations)
11120 {
11121 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
11122 stub_entry->stub_sec->flags |= SEC_RELOC;
11123 }
11124
00f412ee 11125 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11126 {
11127 size = 12;
176a0d42 11128 if (PPC_HA (off) != 0)
ac2df442
AM
11129 size = 16;
11130 }
11131 else
11132 {
00f412ee 11133 size = 16;
176a0d42 11134 if (PPC_HA (off) != 0)
ac2df442
AM
11135 size += 4;
11136
11137 if (PPC_HA (r2off) != 0)
11138 size += 4;
00f412ee
AM
11139 if (PPC_LO (r2off) != 0)
11140 size += 4;
ac2df442 11141 }
721956f4 11142 }
84f5d08e
AM
11143 else if (info->emitrelocations)
11144 {
11145 stub_entry->stub_sec->reloc_count += 1;
11146 stub_entry->stub_sec->flags |= SEC_RELOC;
11147 }
721956f4
AM
11148 }
11149
eea6121a 11150 stub_entry->stub_sec->size += size;
b34976b6 11151 return TRUE;
721956f4
AM
11152}
11153
11154/* Set up various things so that we can make a list of input sections
11155 for each output section included in the link. Returns -1 on error,
cedb70c5 11156 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11157
11158int
e7d1c40c 11159ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4
AM
11160{
11161 bfd *input_bfd;
734b6cf9 11162 int top_id, top_index, id;
721956f4 11163 asection *section;
734b6cf9 11164 asection **input_list;
721956f4
AM
11165 bfd_size_type amt;
11166 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11167
4dfe6ac6
NC
11168 if (htab == NULL)
11169 return -1;
4c52953f 11170
1e2f5b6e 11171 /* Find the top input section id. */
3d6f9012 11172 for (input_bfd = info->input_bfds, top_id = 3;
721956f4 11173 input_bfd != NULL;
c72f2fb2 11174 input_bfd = input_bfd->link.next)
721956f4 11175 {
721956f4
AM
11176 for (section = input_bfd->sections;
11177 section != NULL;
11178 section = section->next)
11179 {
11180 if (top_id < section->id)
11181 top_id = section->id;
11182 }
11183 }
721956f4 11184
8f3bab57 11185 htab->top_id = top_id;
721956f4 11186 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 11187 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
11188 if (htab->stub_group == NULL)
11189 return -1;
11190
3d6f9012
AM
11191 /* Set toc_off for com, und, abs and ind sections. */
11192 for (id = 0; id < 3; id++)
11193 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 11194
734b6cf9
AM
11195 /* We can't use output_bfd->section_count here to find the top output
11196 section index as some sections may have been removed, and
8423293d 11197 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 11198 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
11199 section != NULL;
11200 section = section->next)
11201 {
11202 if (top_index < section->index)
11203 top_index = section->index;
11204 }
11205
11206 htab->top_index = top_index;
11207 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 11208 input_list = bfd_zmalloc (amt);
734b6cf9
AM
11209 htab->input_list = input_list;
11210 if (input_list == NULL)
11211 return -1;
11212
721956f4
AM
11213 return 1;
11214}
11215
927be08e
AM
11216/* Set up for first pass at multitoc partitioning. */
11217
11218void
11219ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11220{
11221 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11222
1c865ab2 11223 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11224 htab->toc_bfd = NULL;
11225 htab->toc_first_sec = NULL;
11226}
11227
e717da7e
AM
11228/* The linker repeatedly calls this function for each TOC input section
11229 and linker generated GOT section. Group input bfds such that the toc
927be08e 11230 within a group is less than 64k in size. */
ad8e1ba5 11231
927be08e 11232bfd_boolean
4ce794b7 11233ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11234{
11235 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11236 bfd_vma addr, off, limit;
ad8e1ba5 11237
4dfe6ac6
NC
11238 if (htab == NULL)
11239 return FALSE;
11240
927be08e 11241 if (!htab->second_toc_pass)
4c52953f 11242 {
927be08e 11243 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11244 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11245
11246 if (new_bfd)
bf102f86
AM
11247 {
11248 htab->toc_bfd = isec->owner;
11249 htab->toc_first_sec = isec;
11250 }
927be08e 11251
bf102f86
AM
11252 addr = isec->output_offset + isec->output_section->vma;
11253 off = addr - htab->toc_curr;
d77c8a4b
AM
11254 limit = 0x80008000;
11255 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11256 limit = 0x10000;
11257 if (off + isec->size > limit)
bf102f86
AM
11258 {
11259 addr = (htab->toc_first_sec->output_offset
11260 + htab->toc_first_sec->output_section->vma);
11261 htab->toc_curr = addr;
a27e685f 11262 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11263 }
99877b66 11264
927be08e
AM
11265 /* toc_curr is the base address of this toc group. Set elf_gp
11266 for the input section to be the offset relative to the
11267 output toc base plus 0x8000. Making the input elf_gp an
11268 offset allows us to move the toc as a whole without
11269 recalculating input elf_gp. */
11270 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11271 off += TOC_BASE_OFF;
11272
11273 /* Die if someone uses a linker script that doesn't keep input
11274 file .toc and .got together. */
a4fd3de5
AM
11275 if (new_bfd
11276 && elf_gp (isec->owner) != 0
927be08e
AM
11277 && elf_gp (isec->owner) != off)
11278 return FALSE;
11279
11280 elf_gp (isec->owner) = off;
11281 return TRUE;
4c52953f 11282 }
927be08e
AM
11283
11284 /* During the second pass toc_first_sec points to the start of
11285 a toc group, and toc_curr is used to track the old elf_gp.
11286 We use toc_bfd to ensure we only look at each bfd once. */
11287 if (htab->toc_bfd == isec->owner)
11288 return TRUE;
11289 htab->toc_bfd = isec->owner;
11290
11291 if (htab->toc_first_sec == NULL
11292 || htab->toc_curr != elf_gp (isec->owner))
11293 {
11294 htab->toc_curr = elf_gp (isec->owner);
11295 htab->toc_first_sec = isec;
11296 }
11297 addr = (htab->toc_first_sec->output_offset
11298 + htab->toc_first_sec->output_section->vma);
11299 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11300 elf_gp (isec->owner) = off;
11301
11302 return TRUE;
ad8e1ba5
AM
11303}
11304
927be08e
AM
11305/* Called via elf_link_hash_traverse to merge GOT entries for global
11306 symbol H. */
11307
11308static bfd_boolean
11309merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11310{
11311 if (h->root.type == bfd_link_hash_indirect)
11312 return TRUE;
11313
927be08e
AM
11314 merge_got_entries (&h->got.glist);
11315
11316 return TRUE;
11317}
11318
11319/* Called via elf_link_hash_traverse to allocate GOT entries for global
11320 symbol H. */
11321
11322static bfd_boolean
11323reallocate_got (struct elf_link_hash_entry *h, void *inf)
11324{
11325 struct got_entry *gent;
11326
11327 if (h->root.type == bfd_link_hash_indirect)
11328 return TRUE;
11329
927be08e
AM
11330 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11331 if (!gent->is_indirect)
11332 allocate_got (h, (struct bfd_link_info *) inf, gent);
11333 return TRUE;
11334}
11335
11336/* Called on the first multitoc pass after the last call to
11337 ppc64_elf_next_toc_section. This function removes duplicate GOT
11338 entries. */
11339
11340bfd_boolean
11341ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11342{
11343 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11344 struct bfd *ibfd, *ibfd2;
11345 bfd_boolean done_something;
11346
11347 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11348
7865406b
AM
11349 if (!htab->do_multi_toc)
11350 return FALSE;
11351
d0fae19d 11352 /* Merge global sym got entries within a toc group. */
927be08e
AM
11353 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11354
11355 /* And tlsld_got. */
c72f2fb2 11356 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11357 {
11358 struct got_entry *ent, *ent2;
11359
11360 if (!is_ppc64_elf (ibfd))
11361 continue;
11362
11363 ent = ppc64_tlsld_got (ibfd);
11364 if (!ent->is_indirect
11365 && ent->got.offset != (bfd_vma) -1)
11366 {
c72f2fb2 11367 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11368 {
11369 if (!is_ppc64_elf (ibfd2))
11370 continue;
11371
11372 ent2 = ppc64_tlsld_got (ibfd2);
11373 if (!ent2->is_indirect
11374 && ent2->got.offset != (bfd_vma) -1
11375 && elf_gp (ibfd2) == elf_gp (ibfd))
11376 {
11377 ent2->is_indirect = TRUE;
11378 ent2->got.ent = ent;
11379 }
11380 }
11381 }
11382 }
11383
11384 /* Zap sizes of got sections. */
33e44f2e
AM
11385 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11386 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11387 htab->got_reli_size = 0;
11388
c72f2fb2 11389 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11390 {
11391 asection *got, *relgot;
11392
11393 if (!is_ppc64_elf (ibfd))
11394 continue;
11395
11396 got = ppc64_elf_tdata (ibfd)->got;
11397 if (got != NULL)
11398 {
11399 got->rawsize = got->size;
11400 got->size = 0;
11401 relgot = ppc64_elf_tdata (ibfd)->relgot;
11402 relgot->rawsize = relgot->size;
11403 relgot->size = 0;
11404 }
11405 }
11406
11407 /* Now reallocate the got, local syms first. We don't need to
11408 allocate section contents again since we never increase size. */
c72f2fb2 11409 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11410 {
11411 struct got_entry **lgot_ents;
11412 struct got_entry **end_lgot_ents;
11413 struct plt_entry **local_plt;
11414 struct plt_entry **end_local_plt;
f961d9dd 11415 unsigned char *lgot_masks;
927be08e
AM
11416 bfd_size_type locsymcount;
11417 Elf_Internal_Shdr *symtab_hdr;
19e08130 11418 asection *s;
927be08e
AM
11419
11420 if (!is_ppc64_elf (ibfd))
11421 continue;
11422
11423 lgot_ents = elf_local_got_ents (ibfd);
11424 if (!lgot_ents)
11425 continue;
11426
11427 symtab_hdr = &elf_symtab_hdr (ibfd);
11428 locsymcount = symtab_hdr->sh_info;
11429 end_lgot_ents = lgot_ents + locsymcount;
11430 local_plt = (struct plt_entry **) end_lgot_ents;
11431 end_local_plt = local_plt + locsymcount;
f961d9dd 11432 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11433 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11434 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11435 {
11436 struct got_entry *ent;
11437
11438 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11439 {
19e08130
AM
11440 unsigned int ent_size = 8;
11441 unsigned int rel_size = sizeof (Elf64_External_Rela);
11442
d0fae19d
AM
11443 ent->got.offset = s->size;
11444 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11445 {
19e08130
AM
11446 ent_size *= 2;
11447 rel_size *= 2;
11448 }
11449 s->size += ent_size;
11450 if ((*lgot_masks & PLT_IFUNC) != 0)
11451 {
33e44f2e 11452 htab->elf.irelplt->size += rel_size;
19e08130
AM
11453 htab->got_reli_size += rel_size;
11454 }
11455 else if (info->shared)
11456 {
11457 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11458 srel->size += rel_size;
d0fae19d
AM
11459 }
11460 }
927be08e
AM
11461 }
11462 }
11463
11464 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11465
c72f2fb2 11466 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11467 {
11468 struct got_entry *ent;
11469
11470 if (!is_ppc64_elf (ibfd))
11471 continue;
11472
11473 ent = ppc64_tlsld_got (ibfd);
11474 if (!ent->is_indirect
11475 && ent->got.offset != (bfd_vma) -1)
11476 {
11477 asection *s = ppc64_elf_tdata (ibfd)->got;
11478 ent->got.offset = s->size;
11479 s->size += 16;
11480 if (info->shared)
11481 {
11482 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11483 srel->size += sizeof (Elf64_External_Rela);
11484 }
11485 }
11486 }
11487
33e44f2e 11488 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11489 if (!done_something)
c72f2fb2 11490 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11491 {
11492 asection *got;
11493
11494 if (!is_ppc64_elf (ibfd))
11495 continue;
11496
11497 got = ppc64_elf_tdata (ibfd)->got;
11498 if (got != NULL)
11499 {
11500 done_something = got->rawsize != got->size;
11501 if (done_something)
11502 break;
11503 }
11504 }
11505
11506 if (done_something)
e7d1c40c 11507 (*htab->params->layout_sections_again) ();
927be08e
AM
11508
11509 /* Set up for second pass over toc sections to recalculate elf_gp
11510 on input sections. */
11511 htab->toc_bfd = NULL;
11512 htab->toc_first_sec = NULL;
11513 htab->second_toc_pass = TRUE;
11514 return done_something;
11515}
11516
11517/* Called after second pass of multitoc partitioning. */
11518
11519void
11520ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11521{
11522 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11523
11524 /* After the second pass, toc_curr tracks the TOC offset used
11525 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11526 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11527}
11528
9b5ecbd0
AM
11529/* No toc references were found in ISEC. If the code in ISEC makes no
11530 calls, then there's no need to use toc adjusting stubs when branching
11531 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11532 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11533 needed, and 2 if a cyclical call-graph was found but no other reason
11534 for a stub was detected. If called from the top level, a return of
11535 2 means the same as a return of 0. */
9b5ecbd0
AM
11536
11537static int
4ce794b7 11538toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11539{
9b5ecbd0 11540 int ret;
70cc837d
AM
11541
11542 /* Mark this section as checked. */
11543 isec->call_check_done = 1;
9b5ecbd0 11544
772119ce
AM
11545 /* We know none of our code bearing sections will need toc stubs. */
11546 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11547 return 0;
11548
eea6121a 11549 if (isec->size == 0)
082c50f8
AM
11550 return 0;
11551
4c52953f
AM
11552 if (isec->output_section == NULL)
11553 return 0;
11554
4c52953f 11555 ret = 0;
70cc837d 11556 if (isec->reloc_count != 0)
9b5ecbd0 11557 {
70cc837d
AM
11558 Elf_Internal_Rela *relstart, *rel;
11559 Elf_Internal_Sym *local_syms;
11560 struct ppc_link_hash_table *htab;
2917689a 11561
70cc837d
AM
11562 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11563 info->keep_memory);
11564 if (relstart == NULL)
11565 return -1;
90aecf7a 11566
70cc837d
AM
11567 /* Look for branches to outside of this section. */
11568 local_syms = NULL;
11569 htab = ppc_hash_table (info);
11570 if (htab == NULL)
11571 return -1;
4c52953f 11572
70cc837d 11573 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11574 {
70cc837d
AM
11575 enum elf_ppc64_reloc_type r_type;
11576 unsigned long r_symndx;
11577 struct elf_link_hash_entry *h;
11578 struct ppc_link_hash_entry *eh;
11579 Elf_Internal_Sym *sym;
11580 asection *sym_sec;
11581 struct _opd_sec_data *opd;
11582 bfd_vma sym_value;
11583 bfd_vma dest;
11584
11585 r_type = ELF64_R_TYPE (rel->r_info);
11586 if (r_type != R_PPC64_REL24
11587 && r_type != R_PPC64_REL14
11588 && r_type != R_PPC64_REL14_BRTAKEN
11589 && r_type != R_PPC64_REL14_BRNTAKEN)
11590 continue;
4c52953f 11591
70cc837d
AM
11592 r_symndx = ELF64_R_SYM (rel->r_info);
11593 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11594 isec->owner))
4c52953f 11595 {
70cc837d
AM
11596 ret = -1;
11597 break;
11598 }
4c52953f 11599
70cc837d
AM
11600 /* Calls to dynamic lib functions go through a plt call stub
11601 that uses r2. */
11602 eh = (struct ppc_link_hash_entry *) h;
11603 if (eh != NULL
11604 && (eh->elf.plt.plist != NULL
11605 || (eh->oh != NULL
11606 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11607 {
11608 ret = 1;
11609 break;
4c52953f
AM
11610 }
11611
70cc837d
AM
11612 if (sym_sec == NULL)
11613 /* Ignore other undefined symbols. */
4c52953f 11614 continue;
4c52953f 11615
70cc837d
AM
11616 /* Assume branches to other sections not included in the
11617 link need stubs too, to cover -R and absolute syms. */
11618 if (sym_sec->output_section == NULL)
11619 {
11620 ret = 1;
11621 break;
11622 }
4c52953f 11623
70cc837d
AM
11624 if (h == NULL)
11625 sym_value = sym->st_value;
11626 else
11627 {
11628 if (h->root.type != bfd_link_hash_defined
11629 && h->root.type != bfd_link_hash_defweak)
11630 abort ();
11631 sym_value = h->root.u.def.value;
11632 }
11633 sym_value += rel->r_addend;
4c52953f 11634
70cc837d
AM
11635 /* If this branch reloc uses an opd sym, find the code section. */
11636 opd = get_opd_info (sym_sec);
11637 if (opd != NULL)
11638 {
11639 if (h == NULL && opd->adjust != NULL)
11640 {
11641 long adjust;
4c52953f 11642
51aecdc5 11643 adjust = opd->adjust[OPD_NDX (sym->st_value)];
70cc837d
AM
11644 if (adjust == -1)
11645 /* Assume deleted functions won't ever be called. */
11646 continue;
11647 sym_value += adjust;
11648 }
4c52953f 11649
aef36ac1
AM
11650 dest = opd_entry_value (sym_sec, sym_value,
11651 &sym_sec, NULL, FALSE);
70cc837d
AM
11652 if (dest == (bfd_vma) -1)
11653 continue;
11654 }
11655 else
11656 dest = (sym_value
11657 + sym_sec->output_offset
11658 + sym_sec->output_section->vma);
4c52953f 11659
70cc837d
AM
11660 /* Ignore branch to self. */
11661 if (sym_sec == isec)
11662 continue;
4c52953f 11663
70cc837d
AM
11664 /* If the called function uses the toc, we need a stub. */
11665 if (sym_sec->has_toc_reloc
11666 || sym_sec->makes_toc_func_call)
4c52953f 11667 {
70cc837d 11668 ret = 1;
4c52953f
AM
11669 break;
11670 }
70cc837d
AM
11671
11672 /* Assume any branch that needs a long branch stub might in fact
11673 need a plt_branch stub. A plt_branch stub uses r2. */
11674 else if (dest - (isec->output_offset
11675 + isec->output_section->vma
6911b7dc
AM
11676 + rel->r_offset) + (1 << 25)
11677 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11678 ? h->other
11679 : sym->st_other))
4c52953f 11680 {
70cc837d
AM
11681 ret = 1;
11682 break;
11683 }
11684
11685 /* If calling back to a section in the process of being
11686 tested, we can't say for sure that no toc adjusting stubs
11687 are needed, so don't return zero. */
11688 else if (sym_sec->call_check_in_progress)
11689 ret = 2;
11690
11691 /* Branches to another section that itself doesn't have any TOC
11692 references are OK. Recursively call ourselves to check. */
11693 else if (!sym_sec->call_check_done)
11694 {
11695 int recur;
11696
11697 /* Mark current section as indeterminate, so that other
11698 sections that call back to current won't be marked as
11699 known. */
11700 isec->call_check_in_progress = 1;
11701 recur = toc_adjusting_stub_needed (info, sym_sec);
11702 isec->call_check_in_progress = 0;
11703
4c52953f
AM
11704 if (recur != 0)
11705 {
70cc837d
AM
11706 ret = recur;
11707 if (recur != 2)
11708 break;
4c52953f
AM
11709 }
11710 }
4c52953f 11711 }
70cc837d
AM
11712
11713 if (local_syms != NULL
11714 && (elf_symtab_hdr (isec->owner).contents
11715 != (unsigned char *) local_syms))
11716 free (local_syms);
11717 if (elf_section_data (isec)->relocs != relstart)
11718 free (relstart);
9b5ecbd0
AM
11719 }
11720
70cc837d
AM
11721 if ((ret & 1) == 0
11722 && isec->map_head.s != NULL
11723 && (strcmp (isec->output_section->name, ".init") == 0
11724 || strcmp (isec->output_section->name, ".fini") == 0))
11725 {
11726 if (isec->map_head.s->has_toc_reloc
11727 || isec->map_head.s->makes_toc_func_call)
11728 ret = 1;
11729 else if (!isec->map_head.s->call_check_done)
11730 {
11731 int recur;
11732 isec->call_check_in_progress = 1;
11733 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11734 isec->call_check_in_progress = 0;
11735 if (recur != 0)
11736 ret = recur;
11737 }
11738 }
11739
11740 if (ret == 1)
11741 isec->makes_toc_func_call = 1;
4c52953f 11742
9b5ecbd0
AM
11743 return ret;
11744}
11745
721956f4
AM
11746/* The linker repeatedly calls this function for each input section,
11747 in the order that input sections are linked into output sections.
11748 Build lists of input sections to determine groupings between which
11749 we may insert linker stubs. */
11750
9b5ecbd0 11751bfd_boolean
4ce794b7 11752ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11753{
11754 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11755
4dfe6ac6
NC
11756 if (htab == NULL)
11757 return FALSE;
11758
734b6cf9
AM
11759 if ((isec->output_section->flags & SEC_CODE) != 0
11760 && isec->output_section->index <= htab->top_index)
721956f4 11761 {
734b6cf9 11762 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 11763 /* Steal the link_sec pointer for our list. */
721956f4 11764#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
11765 /* This happens to make the list in reverse order,
11766 which is what we want. */
734b6cf9
AM
11767 PREV_SEC (isec) = *list;
11768 *list = isec;
721956f4 11769 }
ad8e1ba5 11770
4c52953f 11771 if (htab->multi_toc_needed)
9b5ecbd0 11772 {
8b974ba3
AM
11773 /* Analyse sections that aren't already flagged as needing a
11774 valid toc pointer. Exclude .fixup for the linux kernel.
11775 .fixup contains branches, but only back to the function that
11776 hit an exception. */
11777 if (!(isec->has_toc_reloc
11778 || (isec->flags & SEC_CODE) == 0
11779 || strcmp (isec->name, ".fixup") == 0
11780 || isec->call_check_done))
11781 {
11782 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 11783 return FALSE;
8b974ba3
AM
11784 }
11785 /* Make all sections use the TOC assigned for this object file.
11786 This will be wrong for pasted sections; We fix that in
11787 check_pasted_section(). */
11788 if (elf_gp (isec->owner) != 0)
11789 htab->toc_curr = elf_gp (isec->owner);
11790 }
11791
ad8e1ba5 11792 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11793 return TRUE;
721956f4
AM
11794}
11795
70cc837d
AM
11796/* Check that all .init and .fini sections use the same toc, if they
11797 have toc relocs. */
11798
11799static bfd_boolean
11800check_pasted_section (struct bfd_link_info *info, const char *name)
11801{
11802 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11803
11804 if (o != NULL)
11805 {
11806 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11807 bfd_vma toc_off = 0;
11808 asection *i;
11809
11810 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11811 if (i->has_toc_reloc)
11812 {
11813 if (toc_off == 0)
11814 toc_off = htab->stub_group[i->id].toc_off;
11815 else if (toc_off != htab->stub_group[i->id].toc_off)
11816 return FALSE;
11817 }
6683a28d
AM
11818
11819 if (toc_off == 0)
11820 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11821 if (i->makes_toc_func_call)
11822 {
11823 toc_off = htab->stub_group[i->id].toc_off;
11824 break;
11825 }
11826
70cc837d
AM
11827 /* Make sure the whole pasted function uses the same toc offset. */
11828 if (toc_off != 0)
11829 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11830 htab->stub_group[i->id].toc_off = toc_off;
11831 }
11832 return TRUE;
11833}
11834
11835bfd_boolean
11836ppc64_elf_check_init_fini (struct bfd_link_info *info)
11837{
11838 return (check_pasted_section (info, ".init")
11839 & check_pasted_section (info, ".fini"));
11840}
11841
721956f4
AM
11842/* See whether we can group stub sections together. Grouping stub
11843 sections may result in fewer stubs. More importantly, we need to
11844 put all .init* and .fini* stubs at the beginning of the .init or
11845 .fini output sections respectively, because glibc splits the
11846 _init and _fini functions into multiple parts. Putting a stub in
11847 the middle of a function is not a good idea. */
11848
11849static void
4ce794b7
AM
11850group_sections (struct ppc_link_hash_table *htab,
11851 bfd_size_type stub_group_size,
11852 bfd_boolean stubs_always_before_branch)
721956f4 11853{
7c8fe5c4
AM
11854 asection **list;
11855 bfd_size_type stub14_group_size;
11856 bfd_boolean suppress_size_errors;
11857
11858 suppress_size_errors = FALSE;
0cfb0717 11859 stub14_group_size = stub_group_size >> 10;
7c8fe5c4
AM
11860 if (stub_group_size == 1)
11861 {
11862 /* Default values. */
11863 if (stubs_always_before_branch)
11864 {
11865 stub_group_size = 0x1e00000;
11866 stub14_group_size = 0x7800;
11867 }
11868 else
11869 {
11870 stub_group_size = 0x1c00000;
11871 stub14_group_size = 0x7000;
11872 }
11873 suppress_size_errors = TRUE;
11874 }
11875
11876 list = htab->input_list + htab->top_index;
734b6cf9 11877 do
721956f4 11878 {
734b6cf9
AM
11879 asection *tail = *list;
11880 while (tail != NULL)
721956f4 11881 {
734b6cf9
AM
11882 asection *curr;
11883 asection *prev;
11884 bfd_size_type total;
11885 bfd_boolean big_sec;
11886 bfd_vma curr_toc;
11887
11888 curr = tail;
eea6121a 11889 total = tail->size;
6bee8834
AM
11890 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11891 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
11892 ? stub14_group_size : stub_group_size);
11893 if (big_sec && !suppress_size_errors)
5c3dead3
AM
11894 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11895 tail->owner, tail);
734b6cf9
AM
11896 curr_toc = htab->stub_group[tail->id].toc_off;
11897
11898 while ((prev = PREV_SEC (curr)) != NULL
11899 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11900 < (ppc64_elf_section_data (prev) != NULL
11901 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11902 ? stub14_group_size : stub_group_size))
ad8e1ba5 11903 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
11904 curr = prev;
11905
11906 /* OK, the size from the start of CURR to the end is less
11907 than stub_group_size and thus can be handled by one stub
11908 section. (or the tail section is itself larger than
11909 stub_group_size, in which case we may be toast.) We
11910 should really be keeping track of the total size of stubs
11911 added here, as stubs contribute to the final output
11912 section size. That's a little tricky, and this way will
11913 only break if stubs added make the total size more than
11914 2^25, ie. for the default stub_group_size, if stubs total
11915 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11916 do
721956f4
AM
11917 {
11918 prev = PREV_SEC (tail);
734b6cf9 11919 /* Set up this stub group. */
721956f4
AM
11920 htab->stub_group[tail->id].link_sec = curr;
11921 }
734b6cf9
AM
11922 while (tail != curr && (tail = prev) != NULL);
11923
11924 /* But wait, there's more! Input sections up to stub_group_size
11925 bytes before the stub section can be handled by it too.
11926 Don't do this if we have a really large section after the
11927 stubs, as adding more stubs increases the chance that
11928 branches may not reach into the stub section. */
11929 if (!stubs_always_before_branch && !big_sec)
11930 {
11931 total = 0;
11932 while (prev != NULL
11933 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11934 < (ppc64_elf_section_data (prev) != NULL
11935 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11936 ? stub14_group_size : stub_group_size))
734b6cf9
AM
11937 && htab->stub_group[prev->id].toc_off == curr_toc)
11938 {
11939 tail = prev;
11940 prev = PREV_SEC (tail);
11941 htab->stub_group[tail->id].link_sec = curr;
11942 }
11943 }
11944 tail = prev;
721956f4
AM
11945 }
11946 }
734b6cf9
AM
11947 while (list-- != htab->input_list);
11948 free (htab->input_list);
721956f4
AM
11949#undef PREV_SEC
11950}
11951
58d180e8
AM
11952static const unsigned char glink_eh_frame_cie[] =
11953{
11954 0, 0, 0, 16, /* length. */
11955 0, 0, 0, 0, /* id. */
11956 1, /* CIE version. */
11957 'z', 'R', 0, /* Augmentation string. */
11958 4, /* Code alignment. */
11959 0x78, /* Data alignment. */
11960 65, /* RA reg. */
11961 1, /* Augmentation size. */
11962 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
da44f4e5
AM
11963 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
11964 0, 0, 0, 0
58d180e8
AM
11965};
11966
d969d15f
AM
11967/* Stripping output sections is normally done before dynamic section
11968 symbols have been allocated. This function is called later, and
11969 handles cases like htab->brlt which is mapped to its own output
11970 section. */
11971
11972static void
11973maybe_strip_output (struct bfd_link_info *info, asection *isec)
11974{
11975 if (isec->size == 0
11976 && isec->output_section->size == 0
53d8967a 11977 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
11978 && !bfd_section_removed_from_list (info->output_bfd,
11979 isec->output_section)
11980 && elf_section_data (isec->output_section)->dynindx == 0)
11981 {
11982 isec->output_section->flags |= SEC_EXCLUDE;
11983 bfd_section_list_remove (info->output_bfd, isec->output_section);
11984 info->output_bfd->section_count--;
11985 }
11986}
11987
721956f4
AM
11988/* Determine and set the size of the stub section for a final link.
11989
11990 The basic idea here is to examine all the relocations looking for
11991 PC-relative calls to a target that is unreachable with a "bl"
11992 instruction. */
11993
b34976b6 11994bfd_boolean
e7d1c40c 11995ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
11996{
11997 bfd_size_type stub_group_size;
b34976b6 11998 bfd_boolean stubs_always_before_branch;
721956f4
AM
11999 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12000
4dfe6ac6
NC
12001 if (htab == NULL)
12002 return FALSE;
12003
e7d1c40c
AM
12004 if (htab->params->plt_thread_safe == -1 && !info->executable)
12005 htab->params->plt_thread_safe = 1;
b9e5796b 12006 if (!htab->opd_abi)
e7d1c40c
AM
12007 htab->params->plt_thread_safe = 0;
12008 else if (htab->params->plt_thread_safe == -1)
794e51c0 12009 {
e2458743 12010 static const char *const thread_starter[] =
794e51c0
AM
12011 {
12012 "pthread_create",
12013 /* libstdc++ */
12014 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12015 /* librt */
12016 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12017 "mq_notify", "create_timer",
12018 /* libanl */
12019 "getaddrinfo_a",
12020 /* libgomp */
2300b5a1 12021 "GOMP_parallel",
794e51c0 12022 "GOMP_parallel_start",
2300b5a1 12023 "GOMP_parallel_loop_static",
794e51c0 12024 "GOMP_parallel_loop_static_start",
2300b5a1 12025 "GOMP_parallel_loop_dynamic",
794e51c0 12026 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12027 "GOMP_parallel_loop_guided",
794e51c0 12028 "GOMP_parallel_loop_guided_start",
2300b5a1 12029 "GOMP_parallel_loop_runtime",
794e51c0 12030 "GOMP_parallel_loop_runtime_start",
2300b5a1 12031 "GOMP_parallel_sections",
68ffbac6 12032 "GOMP_parallel_sections_start",
f9dffbf0
AM
12033 /* libgo */
12034 "__go_go",
794e51c0
AM
12035 };
12036 unsigned i;
12037
12038 for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
12039 {
12040 struct elf_link_hash_entry *h;
12041 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12042 FALSE, FALSE, TRUE);
e7d1c40c
AM
12043 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12044 if (htab->params->plt_thread_safe)
794e51c0
AM
12045 break;
12046 }
12047 }
e7d1c40c
AM
12048 stubs_always_before_branch = htab->params->group_size < 0;
12049 if (htab->params->group_size < 0)
12050 stub_group_size = -htab->params->group_size;
721956f4 12051 else
e7d1c40c 12052 stub_group_size = htab->params->group_size;
721956f4
AM
12053
12054 group_sections (htab, stub_group_size, stubs_always_before_branch);
12055
721956f4
AM
12056 while (1)
12057 {
12058 bfd *input_bfd;
12059 unsigned int bfd_indx;
12060 asection *stub_sec;
721956f4
AM
12061
12062 htab->stub_iteration += 1;
721956f4
AM
12063
12064 for (input_bfd = info->input_bfds, bfd_indx = 0;
12065 input_bfd != NULL;
c72f2fb2 12066 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12067 {
12068 Elf_Internal_Shdr *symtab_hdr;
12069 asection *section;
6cdc0ccc 12070 Elf_Internal_Sym *local_syms = NULL;
721956f4 12071
0c8d6e5c 12072 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12073 continue;
12074
721956f4 12075 /* We'll need the symbol table in a second. */
0ffa91dd 12076 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12077 if (symtab_hdr->sh_info == 0)
12078 continue;
12079
721956f4
AM
12080 /* Walk over each section attached to the input bfd. */
12081 for (section = input_bfd->sections;
12082 section != NULL;
12083 section = section->next)
12084 {
721956f4 12085 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12086
12087 /* If there aren't any relocs, then there's nothing more
12088 to do. */
12089 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12090 || (section->flags & SEC_ALLOC) == 0
12091 || (section->flags & SEC_LOAD) == 0
12092 || (section->flags & SEC_CODE) == 0
721956f4
AM
12093 || section->reloc_count == 0)
12094 continue;
12095
12096 /* If this section is a link-once section that will be
12097 discarded, then don't create any stubs. */
12098 if (section->output_section == NULL
927be08e 12099 || section->output_section->owner != info->output_bfd)
721956f4
AM
12100 continue;
12101
1e2f5b6e
AM
12102 /* Get the relocs. */
12103 internal_relocs
4ce794b7 12104 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12105 info->keep_memory);
721956f4 12106 if (internal_relocs == NULL)
1e2f5b6e 12107 goto error_ret_free_local;
721956f4
AM
12108
12109 /* Now examine each relocation. */
12110 irela = internal_relocs;
12111 irelaend = irela + section->reloc_count;
12112 for (; irela < irelaend; irela++)
12113 {
4ce794b7
AM
12114 enum elf_ppc64_reloc_type r_type;
12115 unsigned int r_indx;
721956f4
AM
12116 enum ppc_stub_type stub_type;
12117 struct ppc_stub_hash_entry *stub_entry;
8387904d 12118 asection *sym_sec, *code_sec;
e054468f 12119 bfd_vma sym_value, code_value;
721956f4 12120 bfd_vma destination;
6911b7dc 12121 unsigned long local_off;
8843416a 12122 bfd_boolean ok_dest;
721956f4 12123 struct ppc_link_hash_entry *hash;
8387904d 12124 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12125 struct elf_link_hash_entry *h;
12126 Elf_Internal_Sym *sym;
721956f4
AM
12127 char *stub_name;
12128 const asection *id_sec;
74f0fb50 12129 struct _opd_sec_data *opd;
e054468f 12130 struct plt_entry *plt_ent;
721956f4
AM
12131
12132 r_type = ELF64_R_TYPE (irela->r_info);
12133 r_indx = ELF64_R_SYM (irela->r_info);
12134
4ce794b7 12135 if (r_type >= R_PPC64_max)
721956f4
AM
12136 {
12137 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12138 goto error_ret_free_internal;
721956f4
AM
12139 }
12140
12141 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12142 if (r_type != R_PPC64_REL24
12143 && r_type != R_PPC64_REL14
12144 && r_type != R_PPC64_REL14_BRTAKEN
12145 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12146 continue;
12147
12148 /* Now determine the call target, its name, value,
12149 section. */
411e1bfb
AM
12150 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12151 r_indx, input_bfd))
12152 goto error_ret_free_internal;
12153 hash = (struct ppc_link_hash_entry *) h;
12154
8843416a 12155 ok_dest = FALSE;
8387904d 12156 fdh = NULL;
7fe2b9a6 12157 sym_value = 0;
411e1bfb 12158 if (hash == NULL)
721956f4 12159 {
411e1bfb 12160 sym_value = sym->st_value;
8843416a 12161 ok_dest = TRUE;
721956f4 12162 }
7fe2b9a6
AM
12163 else if (hash->elf.root.type == bfd_link_hash_defined
12164 || hash->elf.root.type == bfd_link_hash_defweak)
12165 {
12166 sym_value = hash->elf.root.u.def.value;
12167 if (sym_sec->output_section != NULL)
12168 ok_dest = TRUE;
12169 }
12170 else if (hash->elf.root.type == bfd_link_hash_undefweak
12171 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12172 {
99877b66 12173 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12174 use the func descriptor sym instead if it is
12175 defined. */
ceb1f1ef 12176 if (hash->elf.root.root.string[0] == '.'
b31867b6 12177 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 12178 {
8387904d
AM
12179 if (fdh->elf.root.type == bfd_link_hash_defined
12180 || fdh->elf.root.type == bfd_link_hash_defweak)
12181 {
12182 sym_sec = fdh->elf.root.u.def.section;
12183 sym_value = fdh->elf.root.u.def.value;
12184 if (sym_sec->output_section != NULL)
12185 ok_dest = TRUE;
12186 }
99877b66
AM
12187 else
12188 fdh = NULL;
8387904d 12189 }
7fe2b9a6
AM
12190 }
12191 else
12192 {
12193 bfd_set_error (bfd_error_bad_value);
12194 goto error_ret_free_internal;
721956f4
AM
12195 }
12196
8843416a 12197 destination = 0;
6911b7dc 12198 local_off = 0;
8843416a
AM
12199 if (ok_dest)
12200 {
12201 sym_value += irela->r_addend;
12202 destination = (sym_value
12203 + sym_sec->output_offset
12204 + sym_sec->output_section->vma);
6911b7dc
AM
12205 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12206 ? hash->elf.other
12207 : sym->st_other);
8843416a
AM
12208 }
12209
8387904d 12210 code_sec = sym_sec;
e054468f 12211 code_value = sym_value;
74f0fb50
AM
12212 opd = get_opd_info (sym_sec);
12213 if (opd != NULL)
8387904d
AM
12214 {
12215 bfd_vma dest;
12216
74f0fb50 12217 if (hash == NULL && opd->adjust != NULL)
8387904d 12218 {
51aecdc5 12219 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12220 if (adjust == -1)
12221 continue;
e054468f 12222 code_value += adjust;
8387904d
AM
12223 sym_value += adjust;
12224 }
12225 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12226 &code_sec, &code_value, FALSE);
8387904d
AM
12227 if (dest != (bfd_vma) -1)
12228 {
12229 destination = dest;
12230 if (fdh != NULL)
12231 {
12232 /* Fixup old ABI sym to point at code
12233 entry. */
99877b66 12234 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12235 hash->elf.root.u.def.section = code_sec;
e054468f 12236 hash->elf.root.u.def.value = code_value;
8387904d
AM
12237 }
12238 }
12239 }
12240
721956f4 12241 /* Determine what (if any) linker stub is needed. */
e054468f 12242 plt_ent = NULL;
721956f4 12243 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12244 &plt_ent, destination,
12245 local_off);
ad8e1ba5
AM
12246
12247 if (stub_type != ppc_stub_plt_call)
12248 {
12249 /* Check whether we need a TOC adjusting stub.
12250 Since the linker pastes together pieces from
12251 different object files when creating the
12252 _init and _fini functions, it may be that a
12253 call to what looks like a local sym is in
12254 fact a call needing a TOC adjustment. */
8387904d
AM
12255 if (code_sec != NULL
12256 && code_sec->output_section != NULL
12257 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 12258 != htab->stub_group[section->id].toc_off)
4c52953f
AM
12259 && (code_sec->has_toc_reloc
12260 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12261 stub_type = ppc_stub_long_branch_r2off;
12262 }
12263
721956f4
AM
12264 if (stub_type == ppc_stub_none)
12265 continue;
12266
411e1bfb
AM
12267 /* __tls_get_addr calls might be eliminated. */
12268 if (stub_type != ppc_stub_plt_call
12269 && hash != NULL
8387904d
AM
12270 && (hash == htab->tls_get_addr
12271 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12272 && section->has_tls_reloc
12273 && irela != internal_relocs)
12274 {
12275 /* Get tls info. */
f961d9dd 12276 unsigned char *tls_mask;
411e1bfb 12277
3a71aa26 12278 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12279 irela - 1, input_bfd))
12280 goto error_ret_free_internal;
e7b938ca 12281 if (*tls_mask != 0)
411e1bfb
AM
12282 continue;
12283 }
12284
3b421ab3
AM
12285 if (stub_type == ppc_stub_plt_call
12286 && irela + 1 < irelaend
12287 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12288 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12289 {
12290 if (!tocsave_find (htab, INSERT,
12291 &local_syms, irela + 1, input_bfd))
12292 goto error_ret_free_internal;
12293 }
12294 else if (stub_type == ppc_stub_plt_call)
12295 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12296
721956f4
AM
12297 /* Support for grouping stub sections. */
12298 id_sec = htab->stub_group[section->id].link_sec;
12299
12300 /* Get the name of this stub. */
12301 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12302 if (!stub_name)
12303 goto error_ret_free_internal;
12304
12305 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12306 stub_name, FALSE, FALSE);
721956f4
AM
12307 if (stub_entry != NULL)
12308 {
12309 /* The proper stub has already been created. */
12310 free (stub_name);
794e51c0
AM
12311 if (stub_type == ppc_stub_plt_call_r2save)
12312 stub_entry->stub_type = stub_type;
721956f4
AM
12313 continue;
12314 }
12315
25f53a85 12316 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12317 if (stub_entry == NULL)
12318 {
12319 free (stub_name);
6cdc0ccc
AM
12320 error_ret_free_internal:
12321 if (elf_section_data (section)->relocs == NULL)
12322 free (internal_relocs);
12323 error_ret_free_local:
12324 if (local_syms != NULL
12325 && (symtab_hdr->contents
12326 != (unsigned char *) local_syms))
12327 free (local_syms);
b34976b6 12328 return FALSE;
721956f4
AM
12329 }
12330
ad8e1ba5 12331 stub_entry->stub_type = stub_type;
794e51c0
AM
12332 if (stub_type != ppc_stub_plt_call
12333 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12334 {
12335 stub_entry->target_value = code_value;
12336 stub_entry->target_section = code_sec;
12337 }
12338 else
12339 {
12340 stub_entry->target_value = sym_value;
12341 stub_entry->target_section = sym_sec;
12342 }
721956f4 12343 stub_entry->h = hash;
e054468f 12344 stub_entry->plt_ent = plt_ent;
6911b7dc 12345 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12346
12347 if (stub_entry->h != NULL)
12348 htab->stub_globals += 1;
721956f4
AM
12349 }
12350
12351 /* We're done with the internal relocs, free them. */
6cdc0ccc 12352 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12353 free (internal_relocs);
721956f4 12354 }
6cdc0ccc
AM
12355
12356 if (local_syms != NULL
12357 && symtab_hdr->contents != (unsigned char *) local_syms)
12358 {
12359 if (!info->keep_memory)
12360 free (local_syms);
12361 else
12362 symtab_hdr->contents = (unsigned char *) local_syms;
12363 }
721956f4
AM
12364 }
12365
5c3dead3 12366 /* We may have added some stubs. Find out the new size of the
721956f4 12367 stub sections. */
e7d1c40c 12368 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12369 stub_sec != NULL;
12370 stub_sec = stub_sec->next)
e717da7e 12371 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12372 {
5c3dead3 12373 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12374 stub_sec->size = 0;
12375 stub_sec->reloc_count = 0;
84f5d08e 12376 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12377 }
eea6121a
AM
12378
12379 htab->brlt->size = 0;
84f5d08e
AM
12380 htab->brlt->reloc_count = 0;
12381 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12382 if (htab->relbrlt != NULL)
eea6121a 12383 htab->relbrlt->size = 0;
721956f4 12384
63bc6f6c 12385 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12386
176a0d42
AM
12387 if (info->emitrelocations
12388 && htab->glink != NULL && htab->glink->size != 0)
12389 {
12390 htab->glink->reloc_count = 1;
12391 htab->glink->flags |= SEC_RELOC;
12392 }
12393
58d180e8
AM
12394 if (htab->glink_eh_frame != NULL
12395 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12396 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12397 {
4bbe044a 12398 size_t size = 0, align;
58d180e8 12399
e7d1c40c 12400 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12401 stub_sec != NULL;
12402 stub_sec = stub_sec->next)
12403 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
da44f4e5 12404 size += 24;
58d180e8
AM
12405 if (htab->glink != NULL && htab->glink->size != 0)
12406 size += 24;
12407 if (size != 0)
12408 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12409 align = 1;
12410 align <<= htab->glink_eh_frame->output_section->alignment_power;
12411 align -= 1;
12412 size = (size + align) & ~align;
58d180e8
AM
12413 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12414 htab->glink_eh_frame->size = size;
12415 }
12416
e7d1c40c
AM
12417 if (htab->params->plt_stub_align != 0)
12418 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12419 stub_sec != NULL;
12420 stub_sec = stub_sec->next)
12421 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12422 stub_sec->size = ((stub_sec->size
12423 + (1 << htab->params->plt_stub_align) - 1)
12424 & (-1 << htab->params->plt_stub_align));
794e51c0 12425
e7d1c40c 12426 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12427 stub_sec != NULL;
12428 stub_sec = stub_sec->next)
12429 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12430 && stub_sec->rawsize != stub_sec->size)
12431 break;
12432
12433 /* Exit from this loop when no stubs have been added, and no stubs
12434 have changed size. */
58d180e8
AM
12435 if (stub_sec == NULL
12436 && (htab->glink_eh_frame == NULL
12437 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12438 break;
12439
721956f4 12440 /* Ask the linker to do its stuff. */
e7d1c40c 12441 (*htab->params->layout_sections_again) ();
721956f4
AM
12442 }
12443
da44f4e5
AM
12444 if (htab->glink_eh_frame != NULL
12445 && htab->glink_eh_frame->size != 0)
12446 {
12447 bfd_vma val;
12448 bfd_byte *p, *last_fde;
12449 size_t last_fde_len, size, align, pad;
12450 asection *stub_sec;
12451
12452 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12453 if (p == NULL)
12454 return FALSE;
12455 htab->glink_eh_frame->contents = p;
12456 last_fde = p;
12457
12458 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12459 /* CIE length (rewrite in case little-endian). */
12460 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12461 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12462 p += sizeof (glink_eh_frame_cie);
12463
12464 for (stub_sec = htab->params->stub_bfd->sections;
12465 stub_sec != NULL;
12466 stub_sec = stub_sec->next)
12467 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12468 {
12469 last_fde = p;
12470 last_fde_len = 20;
12471 /* FDE length. */
12472 bfd_put_32 (htab->elf.dynobj, 20, p);
12473 p += 4;
12474 /* CIE pointer. */
12475 val = p - htab->glink_eh_frame->contents;
12476 bfd_put_32 (htab->elf.dynobj, val, p);
12477 p += 4;
12478 /* Offset to stub section, written later. */
12479 p += 4;
12480 /* stub section size. */
12481 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12482 p += 4;
12483 /* Augmentation. */
12484 p += 1;
12485 /* Pad. */
12486 p += 7;
12487 }
12488 if (htab->glink != NULL && htab->glink->size != 0)
12489 {
12490 last_fde = p;
12491 last_fde_len = 20;
12492 /* FDE length. */
12493 bfd_put_32 (htab->elf.dynobj, 20, p);
12494 p += 4;
12495 /* CIE pointer. */
12496 val = p - htab->glink_eh_frame->contents;
12497 bfd_put_32 (htab->elf.dynobj, val, p);
12498 p += 4;
12499 /* Offset to .glink, written later. */
12500 p += 4;
12501 /* .glink size. */
12502 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12503 p += 4;
12504 /* Augmentation. */
12505 p += 1;
12506
12507 *p++ = DW_CFA_advance_loc + 1;
12508 *p++ = DW_CFA_register;
12509 *p++ = 65;
12510 *p++ = 12;
12511 *p++ = DW_CFA_advance_loc + 4;
12512 *p++ = DW_CFA_restore_extended;
12513 *p++ = 65;
12514 }
12515 /* Subsume any padding into the last FDE if user .eh_frame
12516 sections are aligned more than glink_eh_frame. Otherwise any
12517 zero padding will be seen as a terminator. */
12518 size = p - htab->glink_eh_frame->contents;
12519 align = 1;
12520 align <<= htab->glink_eh_frame->output_section->alignment_power;
12521 align -= 1;
12522 pad = ((size + align) & ~align) - size;
12523 htab->glink_eh_frame->size = size + pad;
12524 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12525 }
12526
d969d15f
AM
12527 maybe_strip_output (info, htab->brlt);
12528 if (htab->glink_eh_frame != NULL)
12529 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12530
b34976b6 12531 return TRUE;
721956f4
AM
12532}
12533
12534/* Called after we have determined section placement. If sections
805fc799 12535 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12536
805fc799 12537bfd_vma
1c865ab2 12538ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12539{
805fc799 12540 asection *s;
a27e685f 12541 bfd_vma TOCstart, adjust;
721956f4 12542
805fc799
AM
12543 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12544 order. The TOC starts where the first of these sections starts. */
12545 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12546 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12547 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12548 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12549 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12550 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12551 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12552 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12553 {
12554 /* This may happen for
12555 o references to TOC base (SYM@toc / TOC[tc0]) without a
12556 .toc directive
12557 o bad linker script
12558 o --gc-sections and empty TOC sections
12559
12560 FIXME: Warn user? */
12561
12562 /* Look for a likely section. We probably won't even be
12563 using TOCstart. */
12564 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12565 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12566 | SEC_EXCLUDE))
805fc799
AM
12567 == (SEC_ALLOC | SEC_SMALL_DATA))
12568 break;
721956f4 12569 if (s == NULL)
805fc799 12570 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12571 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12572 == (SEC_ALLOC | SEC_SMALL_DATA))
12573 break;
721956f4 12574 if (s == NULL)
805fc799 12575 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12576 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12577 == SEC_ALLOC)
805fc799 12578 break;
721956f4 12579 if (s == NULL)
805fc799 12580 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12581 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12582 break;
12583 }
721956f4 12584
805fc799
AM
12585 TOCstart = 0;
12586 if (s != NULL)
12587 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12588
a27e685f
AM
12589 /* Force alignment. */
12590 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12591 TOCstart -= adjust;
1c865ab2
AM
12592 _bfd_set_gp_value (obfd, TOCstart);
12593
810d4e75 12594 if (info != NULL && s != NULL)
1c865ab2
AM
12595 {
12596 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12597
810d4e75
AM
12598 if (htab != NULL)
12599 {
12600 if (htab->elf.hgot != NULL)
12601 {
a27e685f 12602 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12603 htab->elf.hgot->root.u.def.section = s;
12604 }
12605 }
12606 else
1c865ab2 12607 {
810d4e75
AM
12608 struct bfd_link_hash_entry *bh = NULL;
12609 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12610 s, TOC_BASE_OFF - adjust,
12611 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12612 }
12613 }
805fc799 12614 return TOCstart;
721956f4
AM
12615}
12616
a345bc8d
AM
12617/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12618 write out any global entry stubs. */
12619
12620static bfd_boolean
12621build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12622{
12623 struct bfd_link_info *info;
12624 struct ppc_link_hash_table *htab;
12625 struct plt_entry *pent;
12626 asection *s;
12627
12628 if (h->root.type == bfd_link_hash_indirect)
12629 return TRUE;
12630
12631 if (!h->pointer_equality_needed)
12632 return TRUE;
12633
12634 if (h->def_regular)
12635 return TRUE;
12636
12637 info = inf;
12638 htab = ppc_hash_table (info);
12639 if (htab == NULL)
12640 return FALSE;
12641
12642 s = htab->glink;
12643 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12644 if (pent->plt.offset != (bfd_vma) -1
12645 && pent->addend == 0)
12646 {
12647 bfd_byte *p;
12648 asection *plt;
12649 bfd_vma off;
12650
a345bc8d 12651 p = s->contents + h->root.u.def.value;
33e44f2e 12652 plt = htab->elf.splt;
a345bc8d
AM
12653 if (!htab->elf.dynamic_sections_created
12654 || h->dynindx == -1)
33e44f2e 12655 plt = htab->elf.iplt;
a345bc8d
AM
12656 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12657 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12658
12659 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12660 {
12661 info->callbacks->einfo
12662 (_("%P: linkage table error against `%T'\n"),
12663 h->root.root.string);
12664 bfd_set_error (bfd_error_bad_value);
12665 htab->stub_error = TRUE;
12666 }
12667
7341d5e2
AM
12668 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12669 if (htab->params->emit_stub_syms)
12670 {
12671 size_t len = strlen (h->root.root.string);
12672 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12673
12674 if (name == NULL)
12675 return FALSE;
12676
12677 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12678 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12679 if (h == NULL)
12680 return FALSE;
12681 if (h->root.type == bfd_link_hash_new)
12682 {
12683 h->root.type = bfd_link_hash_defined;
12684 h->root.u.def.section = s;
12685 h->root.u.def.value = p - s->contents;
12686 h->ref_regular = 1;
12687 h->def_regular = 1;
12688 h->ref_regular_nonweak = 1;
12689 h->forced_local = 1;
12690 h->non_elf = 0;
2ec55de3 12691 h->root.linker_def = 1;
7341d5e2
AM
12692 }
12693 }
12694
a345bc8d
AM
12695 if (PPC_HA (off) != 0)
12696 {
12697 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12698 p += 4;
12699 }
12700 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12701 p += 4;
12702 bfd_put_32 (s->owner, MTCTR_R12, p);
12703 p += 4;
12704 bfd_put_32 (s->owner, BCTR, p);
12705 break;
12706 }
12707 return TRUE;
12708}
12709
721956f4
AM
12710/* Build all the stubs associated with the current output file.
12711 The stubs are kept in a hash table attached to the main linker
12712 hash table. This function is called via gldelf64ppc_finish. */
12713
b34976b6 12714bfd_boolean
e7d1c40c 12715ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 12716 char **stats)
5d1634d7
AM
12717{
12718 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 12719 asection *stub_sec;
5d1634d7 12720 bfd_byte *p;
e717da7e 12721 int stub_sec_count = 0;
5d1634d7 12722
4dfe6ac6
NC
12723 if (htab == NULL)
12724 return FALSE;
12725
eea6121a 12726 /* Allocate memory to hold the linker stubs. */
e7d1c40c 12727 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12728 stub_sec != NULL;
12729 stub_sec = stub_sec->next)
eea6121a
AM
12730 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12731 && stub_sec->size != 0)
e717da7e 12732 {
e7d1c40c 12733 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
12734 if (stub_sec->contents == NULL)
12735 return FALSE;
12736 /* We want to check that built size is the same as calculated
12737 size. rawsize is a convenient location to use. */
12738 stub_sec->rawsize = stub_sec->size;
12739 stub_sec->size = 0;
e717da7e 12740 }
5d1634d7 12741
23eb7e01 12742 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12743 {
9f951329 12744 unsigned int indx;
ad8e1ba5 12745 bfd_vma plt0;
9f951329 12746
721956f4 12747 /* Build the .glink plt call stub. */
e7d1c40c 12748 if (htab->params->emit_stub_syms)
97b639ba
AM
12749 {
12750 struct elf_link_hash_entry *h;
468392fb
AM
12751 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12752 TRUE, FALSE, FALSE);
97b639ba
AM
12753 if (h == NULL)
12754 return FALSE;
12755 if (h->root.type == bfd_link_hash_new)
12756 {
12757 h->root.type = bfd_link_hash_defined;
12758 h->root.u.def.section = htab->glink;
ee4bf8d2 12759 h->root.u.def.value = 8;
f5385ebf
AM
12760 h->ref_regular = 1;
12761 h->def_regular = 1;
12762 h->ref_regular_nonweak = 1;
12763 h->forced_local = 1;
12764 h->non_elf = 0;
2ec55de3 12765 h->root.linker_def = 1;
97b639ba
AM
12766 }
12767 }
33e44f2e
AM
12768 plt0 = (htab->elf.splt->output_section->vma
12769 + htab->elf.splt->output_offset
12770 - 16);
176a0d42
AM
12771 if (info->emitrelocations)
12772 {
12773 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12774 if (r == NULL)
12775 return FALSE;
12776 r->r_offset = (htab->glink->output_offset
12777 + htab->glink->output_section->vma);
12778 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12779 r->r_addend = plt0;
12780 }
4ce794b7 12781 p = htab->glink->contents;
176a0d42 12782 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
12783 bfd_put_64 (htab->glink->owner, plt0, p);
12784 p += 8;
b9e5796b
AM
12785 if (htab->opd_abi)
12786 {
12787 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12788 p += 4;
12789 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12790 p += 4;
12791 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12792 p += 4;
12793 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12794 p += 4;
12795 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12796 p += 4;
12797 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12798 p += 4;
12799 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12800 p += 4;
12801 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12802 p += 4;
12803 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12804 p += 4;
12805 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12806 p += 4;
12807 }
12808 else
12809 {
12810 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12811 p += 4;
12812 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12813 p += 4;
12814 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12815 p += 4;
12816 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12817 p += 4;
12818 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12819 p += 4;
12820 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12821 p += 4;
12822 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12823 p += 4;
12824 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12825 p += 4;
12826 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12827 p += 4;
12828 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12829 p += 4;
12830 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12831 p += 4;
12832 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12833 p += 4;
12834 }
4ce794b7 12835 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 12836 p += 4;
ee4bf8d2
AM
12837 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12838 {
12839 bfd_put_32 (htab->glink->owner, NOP, p);
12840 p += 4;
12841 }
ad8e1ba5 12842
9f951329
AM
12843 /* Build the .glink lazy link call stubs. */
12844 indx = 0;
a345bc8d 12845 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 12846 {
b9e5796b 12847 if (htab->opd_abi)
9f951329 12848 {
b9e5796b
AM
12849 if (indx < 0x8000)
12850 {
12851 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12852 p += 4;
12853 }
12854 else
12855 {
12856 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12857 p += 4;
12858 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12859 p);
12860 p += 4;
12861 }
9f951329 12862 }
4ce794b7 12863 bfd_put_32 (htab->glink->owner,
ee4bf8d2 12864 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 12865 indx++;
9f951329
AM
12866 p += 4;
12867 }
a345bc8d
AM
12868
12869 /* Build .glink global entry stubs. */
12870 if (htab->glink->size > htab->glink->rawsize)
afe397ea 12871 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 12872 }
5d1634d7 12873
7341d5e2 12874 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 12875 {
4ce794b7 12876 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 12877 htab->brlt->size);
4ce794b7 12878 if (htab->brlt->contents == NULL)
b34976b6 12879 return FALSE;
721956f4 12880 }
ee75fd95 12881 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
12882 {
12883 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 12884 htab->relbrlt->size);
63bc6f6c
AM
12885 if (htab->relbrlt->contents == NULL)
12886 return FALSE;
12887 }
5d1634d7 12888
721956f4
AM
12889 /* Build the stubs as directed by the stub hash table. */
12890 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 12891
aa8a7074
AM
12892 if (htab->relbrlt != NULL)
12893 htab->relbrlt->reloc_count = 0;
12894
e7d1c40c
AM
12895 if (htab->params->plt_stub_align != 0)
12896 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12897 stub_sec != NULL;
12898 stub_sec = stub_sec->next)
12899 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12900 stub_sec->size = ((stub_sec->size
12901 + (1 << htab->params->plt_stub_align) - 1)
12902 & (-1 << htab->params->plt_stub_align));
794e51c0 12903
e7d1c40c 12904 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12905 stub_sec != NULL;
12906 stub_sec = stub_sec->next)
e717da7e
AM
12907 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12908 {
12909 stub_sec_count += 1;
eea6121a 12910 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
12911 break;
12912 }
5d1634d7 12913
da44f4e5
AM
12914 /* Note that the glink_eh_frame check here is not only testing that
12915 the generated size matched the calculated size but also that
12916 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 12917 if (stub_sec != NULL
58d180e8
AM
12918 || (htab->glink_eh_frame != NULL
12919 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 12920 {
b34976b6 12921 htab->stub_error = TRUE;
8de848d8 12922 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 12923 }
721956f4 12924
d2a300cf
AM
12925 if (htab->stub_error)
12926 return FALSE;
12927
12928 if (stats != NULL)
12929 {
12930 *stats = bfd_malloc (500);
12931 if (*stats == NULL)
12932 return FALSE;
12933
ee75fd95 12934 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
12935 " branch %lu\n"
12936 " toc adjust %lu\n"
12937 " long branch %lu\n"
12938 " long toc adj %lu\n"
794e51c0 12939 " plt call %lu\n"
7341d5e2
AM
12940 " plt call toc %lu\n"
12941 " global entry %lu"),
e717da7e 12942 stub_sec_count,
ee75fd95 12943 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
12944 htab->stub_count[ppc_stub_long_branch - 1],
12945 htab->stub_count[ppc_stub_long_branch_r2off - 1],
12946 htab->stub_count[ppc_stub_plt_branch - 1],
12947 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 12948 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
12949 htab->stub_count[ppc_stub_plt_call_r2save - 1],
12950 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
12951 }
12952 return TRUE;
5bd4f169
AM
12953}
12954
99877b66
AM
12955/* This function undoes the changes made by add_symbol_adjust. */
12956
12957static bfd_boolean
12958undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12959{
12960 struct ppc_link_hash_entry *eh;
12961
12962 if (h->root.type == bfd_link_hash_indirect)
12963 return TRUE;
12964
99877b66
AM
12965 eh = (struct ppc_link_hash_entry *) h;
12966 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12967 return TRUE;
12968
12969 eh->elf.root.type = bfd_link_hash_undefined;
12970 return TRUE;
12971}
12972
12973void
12974ppc64_elf_restore_symbols (struct bfd_link_info *info)
12975{
12976 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
12977
12978 if (htab != NULL)
12979 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
12980}
12981
60124e18
AM
12982/* What to do when ld finds relocations against symbols defined in
12983 discarded sections. */
12984
12985static unsigned int
12986ppc64_elf_action_discarded (asection *sec)
12987{
12988 if (strcmp (".opd", sec->name) == 0)
12989 return 0;
12990
12991 if (strcmp (".toc", sec->name) == 0)
12992 return 0;
12993
bce50a28
JJ
12994 if (strcmp (".toc1", sec->name) == 0)
12995 return 0;
12996
60124e18
AM
12997 return _bfd_elf_default_action_discarded (sec);
12998}
12999
5bd4f169
AM
13000/* The RELOCATE_SECTION function is called by the ELF backend linker
13001 to handle the relocations for a section.
13002
13003 The relocs are always passed as Rela structures; if the section
13004 actually uses Rel structures, the r_addend field will always be
13005 zero.
13006
13007 This function is responsible for adjust the section contents as
13008 necessary, and (if using Rela relocs and generating a
1049f94e 13009 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13010 necessary.
13011
13012 This function does not have to worry about setting the reloc
13013 address or the reloc symbol index.
13014
13015 LOCAL_SYMS is a pointer to the swapped in local symbols.
13016
13017 LOCAL_SECTIONS is an array giving the section in the input file
13018 corresponding to the st_shndx field of each local symbol.
13019
13020 The global hash table entry for the global symbols can be found
13021 via elf_sym_hashes (input_bfd).
13022
1049f94e 13023 When generating relocatable output, this function must handle
5bd4f169
AM
13024 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13025 going to be the section symbol corresponding to the output
13026 section, which means that the addend must be adjusted
13027 accordingly. */
13028
b34976b6 13029static bfd_boolean
4ce794b7
AM
13030ppc64_elf_relocate_section (bfd *output_bfd,
13031 struct bfd_link_info *info,
13032 bfd *input_bfd,
13033 asection *input_section,
13034 bfd_byte *contents,
13035 Elf_Internal_Rela *relocs,
13036 Elf_Internal_Sym *local_syms,
13037 asection **local_sections)
5bd4f169 13038{
65f38f15 13039 struct ppc_link_hash_table *htab;
5bd4f169
AM
13040 Elf_Internal_Shdr *symtab_hdr;
13041 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
13042 Elf_Internal_Rela *rel;
13043 Elf_Internal_Rela *relend;
411e1bfb
AM
13044 Elf_Internal_Rela outrel;
13045 bfd_byte *loc;
411e1bfb 13046 struct got_entry **local_got_ents;
5bd4f169 13047 bfd_vma TOCstart;
b34976b6
AM
13048 bfd_boolean ret = TRUE;
13049 bfd_boolean is_opd;
794e51c0
AM
13050 /* Assume 'at' branch hints. */
13051 bfd_boolean is_isa_v2 = TRUE;
4fe5ca5b 13052 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 13053
65f38f15 13054 /* Initialize howto table if needed. */
5bd4f169 13055 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13056 ppc_howto_init ();
13057
65f38f15 13058 htab = ppc_hash_table (info);
4dfe6ac6
NC
13059 if (htab == NULL)
13060 return FALSE;
ee75fd95
AM
13061
13062 /* Don't relocate stub sections. */
e7d1c40c 13063 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13064 return TRUE;
13065
0c8d6e5c 13066 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13067
411e1bfb 13068 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13069 TOCstart = elf_gp (output_bfd);
0ffa91dd 13070 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13071 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13072 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13073
5bd4f169
AM
13074 rel = relocs;
13075 relend = relocs + input_section->reloc_count;
13076 for (; rel < relend; rel++)
13077 {
04c9666a 13078 enum elf_ppc64_reloc_type r_type;
31c76678 13079 bfd_vma addend;
5bd4f169
AM
13080 bfd_reloc_status_type r;
13081 Elf_Internal_Sym *sym;
13082 asection *sec;
039b3fef
AM
13083 struct elf_link_hash_entry *h_elf;
13084 struct ppc_link_hash_entry *h;
13085 struct ppc_link_hash_entry *fdh;
5bd4f169 13086 const char *sym_name;
0d4792f7 13087 unsigned long r_symndx, toc_symndx;
3a71aa26 13088 bfd_vma toc_addend;
f961d9dd
AM
13089 unsigned char tls_mask, tls_gd, tls_type;
13090 unsigned char sym_type;
5bd4f169 13091 bfd_vma relocation;
b34976b6
AM
13092 bfd_boolean unresolved_reloc;
13093 bfd_boolean warned;
bc30df16 13094 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13095 unsigned int insn;
e11840f9 13096 unsigned int mask;
721956f4
AM
13097 struct ppc_stub_hash_entry *stub_entry;
13098 bfd_vma max_br_offset;
13099 bfd_vma from;
31c76678 13100 const Elf_Internal_Rela orig_rel = *rel;
b80eed39
AM
13101 reloc_howto_type *howto;
13102 struct reloc_howto_struct alt_howto;
5bd4f169 13103
4ce794b7 13104 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13105 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13106
13107 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13108 symbol of the previous ADDR64 reloc. The symbol gives us the
13109 proper TOC base to use. */
13110 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13111 && rel != relocs
13112 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
13113 && is_opd)
13114 r_symndx = ELF64_R_SYM (rel[-1].r_info);
13115
4ce794b7
AM
13116 sym = NULL;
13117 sec = NULL;
039b3fef 13118 h_elf = NULL;
4ce794b7 13119 sym_name = NULL;
b34976b6
AM
13120 unresolved_reloc = FALSE;
13121 warned = FALSE;
65f38f15 13122
0b13192e 13123 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13124 {
13125 /* It's a local symbol. */
74f0fb50 13126 struct _opd_sec_data *opd;
4025353c 13127
5bd4f169
AM
13128 sym = local_syms + r_symndx;
13129 sec = local_sections[r_symndx];
26c61ae5 13130 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13131 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13132 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13133 opd = get_opd_info (sec);
13134 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13135 {
51aecdc5
AM
13136 long adjust = opd->adjust[OPD_NDX (sym->st_value
13137 + rel->r_addend)];
4025353c
AM
13138 if (adjust == -1)
13139 relocation = 0;
13140 else
4cc603a5
AM
13141 {
13142 /* If this is a relocation against the opd section sym
13143 and we have edited .opd, adjust the reloc addend so
13144 that ld -r and ld --emit-relocs output is correct.
13145 If it is a reloc against some other .opd symbol,
13146 then the symbol value will be adjusted later. */
13147 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13148 rel->r_addend += adjust;
13149 else
13150 relocation += adjust;
13151 }
1e2f5b6e 13152 }
5bd4f169
AM
13153 }
13154 else
13155 {
62d887d4
L
13156 bfd_boolean ignored;
13157
b2a8e766
AM
13158 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13159 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13160 h_elf, sec, relocation,
62d887d4 13161 unresolved_reloc, warned, ignored);
039b3fef
AM
13162 sym_name = h_elf->root.root.string;
13163 sym_type = h_elf->type;
b69fdb4e
AM
13164 if (sec != NULL
13165 && sec->owner == output_bfd
13166 && strcmp (sec->name, ".opd") == 0)
13167 {
13168 /* This is a symbol defined in a linker script. All
13169 such are defined in output sections, even those
13170 defined by simple assignment from a symbol defined in
13171 an input section. Transfer the symbol to an
13172 appropriate input .opd section, so that a branch to
13173 this symbol will be mapped to the location specified
13174 by the opd entry. */
13175 struct bfd_link_order *lo;
13176 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13177 if (lo->type == bfd_indirect_link_order)
13178 {
13179 asection *isec = lo->u.indirect.section;
13180 if (h_elf->root.u.def.value >= isec->output_offset
13181 && h_elf->root.u.def.value < (isec->output_offset
13182 + isec->size))
13183 {
13184 h_elf->root.u.def.value -= isec->output_offset;
13185 h_elf->root.u.def.section = isec;
13186 sec = isec;
13187 break;
13188 }
13189 }
13190 }
5bd4f169 13191 }
039b3fef 13192 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13193
dbaa2011 13194 if (sec != NULL && discarded_section (sec))
e4067dbb 13195 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b
MR
13196 rel, 1, relend,
13197 ppc64_elf_howto_table[r_type], 0,
e4067dbb 13198 contents);
ab96bf03
AM
13199
13200 if (info->relocatable)
13201 continue;
13202
f40da81b
AM
13203 if (h != NULL && &h->elf == htab->elf.hgot)
13204 {
13205 relocation = (TOCstart
13206 + htab->stub_group[input_section->id].toc_off);
13207 sec = bfd_abs_section_ptr;
13208 unresolved_reloc = FALSE;
13209 }
13210
951fd09b
AM
13211 /* TLS optimizations. Replace instruction sequences and relocs
13212 based on information we collected in tls_optimize. We edit
13213 RELOCS so that --emit-relocs will output something sensible
13214 for the final instruction stream. */
13215 tls_mask = 0;
13216 tls_gd = 0;
0d4792f7 13217 toc_symndx = 0;
727fc41e
AM
13218 if (h != NULL)
13219 tls_mask = h->tls_mask;
13220 else if (local_got_ents != NULL)
411e1bfb 13221 {
e054468f
AM
13222 struct plt_entry **local_plt = (struct plt_entry **)
13223 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13224 unsigned char *lgot_masks = (unsigned char *)
e054468f 13225 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13226 tls_mask = lgot_masks[r_symndx];
13227 }
13228 if (tls_mask == 0
13229 && (r_type == R_PPC64_TLS
13230 || r_type == R_PPC64_TLSGD
13231 || r_type == R_PPC64_TLSLD))
13232 {
13233 /* Check for toc tls entries. */
f961d9dd 13234 unsigned char *toc_tls;
0d4792f7 13235
727fc41e
AM
13236 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13237 &local_syms, rel, input_bfd))
13238 return FALSE;
0d4792f7 13239
727fc41e
AM
13240 if (toc_tls)
13241 tls_mask = *toc_tls;
0d4792f7
AM
13242 }
13243
13244 /* Check that tls relocs are used with tls syms, and non-tls
13245 relocs are used with non-tls syms. */
cf35638d 13246 if (r_symndx != STN_UNDEF
0d4792f7
AM
13247 && r_type != R_PPC64_NONE
13248 && (h == NULL
039b3fef
AM
13249 || h->elf.root.type == bfd_link_hash_defined
13250 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13251 && (IS_PPC64_TLS_RELOC (r_type)
13252 != (sym_type == STT_TLS
13253 || (sym_type == STT_SECTION
13254 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13255 {
727fc41e
AM
13256 if (tls_mask != 0
13257 && (r_type == R_PPC64_TLS
13258 || r_type == R_PPC64_TLSGD
13259 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13260 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13261 ;
13262 else
25f53a85 13263 info->callbacks->einfo
1d483afe 13264 (!IS_PPC64_TLS_RELOC (r_type)
bc30df16
AM
13265 ? _("%P: %H: %s used with TLS symbol `%T'\n")
13266 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13267 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13268 ppc64_elf_howto_table[r_type]->name,
13269 sym_name);
411e1bfb
AM
13270 }
13271
13272 /* Ensure reloc mapping code below stays sane. */
13273 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13274 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13275 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13276 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13277 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13278 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13279 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13280 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13281 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13282 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13283 abort ();
0d4792f7 13284
411e1bfb
AM
13285 switch (r_type)
13286 {
13287 default:
411e1bfb
AM
13288 break;
13289
ba761f19
AM
13290 case R_PPC64_LO_DS_OPT:
13291 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13292 if ((insn & (0x3f << 26)) != 58u << 26)
13293 abort ();
13294 insn += (14u << 26) - (58u << 26);
13295 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13296 r_type = R_PPC64_TOC16_LO;
13297 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13298 break;
13299
411e1bfb
AM
13300 case R_PPC64_TOC16:
13301 case R_PPC64_TOC16_LO:
13302 case R_PPC64_TOC16_DS:
13303 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13304 {
13305 /* Check for toc tls entries. */
f961d9dd 13306 unsigned char *toc_tls;
951fd09b 13307 int retval;
411e1bfb 13308
3a71aa26
AM
13309 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13310 &local_syms, rel, input_bfd);
951fd09b 13311 if (retval == 0)
411e1bfb
AM
13312 return FALSE;
13313
13314 if (toc_tls)
13315 {
951fd09b 13316 tls_mask = *toc_tls;
411e1bfb
AM
13317 if (r_type == R_PPC64_TOC16_DS
13318 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13319 {
13320 if (tls_mask != 0
13321 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13322 goto toctprel;
13323 }
411e1bfb 13324 else
951fd09b
AM
13325 {
13326 /* If we found a GD reloc pair, then we might be
13327 doing a GD->IE transition. */
13328 if (retval == 2)
13329 {
13330 tls_gd = TLS_TPRELGD;
13331 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13332 goto tls_ldgd_opt;
951fd09b
AM
13333 }
13334 else if (retval == 3)
13335 {
13336 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13337 goto tls_ldgd_opt;
951fd09b
AM
13338 }
13339 }
411e1bfb
AM
13340 }
13341 }
13342 break;
13343
9d6ded02
AM
13344 case R_PPC64_GOT_TPREL16_HI:
13345 case R_PPC64_GOT_TPREL16_HA:
13346 if (tls_mask != 0
13347 && (tls_mask & TLS_TPREL) == 0)
13348 {
13349 rel->r_offset -= d_offset;
13350 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13351 r_type = R_PPC64_NONE;
13352 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13353 }
13354 break;
13355
411e1bfb
AM
13356 case R_PPC64_GOT_TPREL16_DS:
13357 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13358 if (tls_mask != 0
13359 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13360 {
81407a69 13361 toctprel:
4fe5ca5b 13362 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
13363 insn &= 31 << 21;
13364 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 13365 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 13366 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13367 if (toc_symndx != 0)
13368 {
13369 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13370 rel->r_addend = toc_addend;
0d4792f7
AM
13371 /* We changed the symbol. Start over in order to
13372 get h, sym, sec etc. right. */
13373 rel--;
13374 continue;
13375 }
13376 else
13377 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13378 }
13379 break;
13380
13381 case R_PPC64_TLS:
951fd09b
AM
13382 if (tls_mask != 0
13383 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13384 {
411e1bfb 13385 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
13386 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13387 if (insn == 0)
411e1bfb 13388 abort ();
411e1bfb 13389 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 13390 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13391 PPC64_TPREL16_LO which is at low-order half-word. */
13392 rel->r_offset += d_offset;
0d4792f7
AM
13393 r_type = R_PPC64_TPREL16_LO;
13394 if (toc_symndx != 0)
13395 {
13396 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13397 rel->r_addend = toc_addend;
0d4792f7
AM
13398 /* We changed the symbol. Start over in order to
13399 get h, sym, sec etc. right. */
13400 rel--;
13401 continue;
13402 }
13403 else
13404 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13405 }
13406 break;
13407
411e1bfb
AM
13408 case R_PPC64_GOT_TLSGD16_HI:
13409 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13410 tls_gd = TLS_TPRELGD;
13411 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13412 goto tls_gdld_hi;
13413 break;
13414
411e1bfb
AM
13415 case R_PPC64_GOT_TLSLD16_HI:
13416 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13417 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13418 {
951fd09b
AM
13419 tls_gdld_hi:
13420 if ((tls_mask & tls_gd) != 0)
13421 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13422 + R_PPC64_GOT_TPREL16_DS);
13423 else
411e1bfb 13424 {
4fe5ca5b 13425 rel->r_offset -= d_offset;
727ac201 13426 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 13427 r_type = R_PPC64_NONE;
411e1bfb 13428 }
951fd09b 13429 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13430 }
13431 break;
13432
951fd09b
AM
13433 case R_PPC64_GOT_TLSGD16:
13434 case R_PPC64_GOT_TLSGD16_LO:
13435 tls_gd = TLS_TPRELGD;
13436 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13437 goto tls_ldgd_opt;
951fd09b 13438 break;
411e1bfb 13439
951fd09b
AM
13440 case R_PPC64_GOT_TLSLD16:
13441 case R_PPC64_GOT_TLSLD16_LO:
13442 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13443 {
3a71aa26 13444 unsigned int insn1, insn2, insn3;
102890f0
AM
13445 bfd_vma offset;
13446
13447 tls_ldgd_opt:
727fc41e
AM
13448 offset = (bfd_vma) -1;
13449 /* If not using the newer R_PPC64_TLSGD/LD to mark
13450 __tls_get_addr calls, we must trust that the call
13451 stays with its arg setup insns, ie. that the next
13452 reloc is the __tls_get_addr call associated with
13453 the current reloc. Edit both insns. */
13454 if (input_section->has_tls_get_addr_call
13455 && rel + 1 < relend
13456 && branch_reloc_hash_match (input_bfd, rel + 1,
13457 htab->tls_get_addr,
13458 htab->tls_get_addr_fd))
13459 offset = rel[1].r_offset;
b86ac8e3
AM
13460 /* We read the low GOT_TLS (or TOC16) insn because we
13461 need to keep the destination reg. It may be
13462 something other than the usual r3, and moved to r3
13463 before the call by intervening code. */
13464 insn1 = bfd_get_32 (output_bfd,
13465 contents + rel->r_offset - d_offset);
102890f0 13466 if ((tls_mask & tls_gd) != 0)
411e1bfb 13467 {
102890f0 13468 /* IE */
b86ac8e3 13469 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13470 insn1 |= 58 << 26; /* ld */
13471 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13472 if (offset != (bfd_vma) -1)
f58d5a2d 13473 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13474 if ((tls_mask & TLS_EXPLICIT) == 0)
13475 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13476 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13477 else
102890f0
AM
13478 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13479 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13480 }
13481 else
13482 {
13483 /* LE */
b86ac8e3
AM
13484 insn1 &= 0x1f << 21;
13485 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13486 insn2 = 0x38630000; /* addi 3,3,0 */
13487 if (tls_gd == 0)
951fd09b 13488 {
102890f0 13489 /* Was an LD reloc. */
1d483afe
AM
13490 if (toc_symndx)
13491 sec = local_sections[toc_symndx];
13492 for (r_symndx = 0;
13493 r_symndx < symtab_hdr->sh_info;
13494 r_symndx++)
13495 if (local_sections[r_symndx] == sec)
13496 break;
13497 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13498 r_symndx = STN_UNDEF;
102890f0 13499 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13500 if (r_symndx != STN_UNDEF)
1d483afe
AM
13501 rel->r_addend -= (local_syms[r_symndx].st_value
13502 + sec->output_offset
13503 + sec->output_section->vma);
951fd09b 13504 }
102890f0 13505 else if (toc_symndx != 0)
3a71aa26
AM
13506 {
13507 r_symndx = toc_symndx;
13508 rel->r_addend = toc_addend;
13509 }
102890f0
AM
13510 r_type = R_PPC64_TPREL16_HA;
13511 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13512 if (offset != (bfd_vma) -1)
13513 {
13514 rel[1].r_info = ELF64_R_INFO (r_symndx,
13515 R_PPC64_TPREL16_LO);
13516 rel[1].r_offset = offset + d_offset;
13517 rel[1].r_addend = rel->r_addend;
13518 }
102890f0 13519 }
3a71aa26
AM
13520 bfd_put_32 (output_bfd, insn1,
13521 contents + rel->r_offset - d_offset);
727fc41e
AM
13522 if (offset != (bfd_vma) -1)
13523 {
13524 insn3 = bfd_get_32 (output_bfd,
13525 contents + offset + 4);
13526 if (insn3 == NOP
13527 || insn3 == CROR_151515 || insn3 == CROR_313131)
13528 {
13529 rel[1].r_offset += 4;
13530 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13531 insn2 = NOP;
13532 }
13533 bfd_put_32 (output_bfd, insn2, contents + offset);
13534 }
13535 if ((tls_mask & tls_gd) == 0
13536 && (tls_gd == 0 || toc_symndx != 0))
13537 {
13538 /* We changed the symbol. Start over in order
13539 to get h, sym, sec etc. right. */
13540 rel--;
13541 continue;
13542 }
13543 }
13544 break;
13545
13546 case R_PPC64_TLSGD:
13547 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13548 {
13549 unsigned int insn2, insn3;
13550 bfd_vma offset = rel->r_offset;
13551
13552 if ((tls_mask & TLS_TPRELGD) != 0)
13553 {
13554 /* IE */
13555 r_type = R_PPC64_NONE;
13556 insn2 = 0x7c636a14; /* add 3,3,13 */
13557 }
13558 else
13559 {
13560 /* LE */
13561 if (toc_symndx != 0)
13562 {
13563 r_symndx = toc_symndx;
13564 rel->r_addend = toc_addend;
13565 }
13566 r_type = R_PPC64_TPREL16_LO;
13567 rel->r_offset = offset + d_offset;
13568 insn2 = 0x38630000; /* addi 3,3,0 */
13569 }
13570 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13571 /* Zap the reloc on the _tls_get_addr call too. */
13572 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13573 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
13574 insn3 = bfd_get_32 (output_bfd,
13575 contents + offset + 4);
102890f0
AM
13576 if (insn3 == NOP
13577 || insn3 == CROR_151515 || insn3 == CROR_313131)
13578 {
727fc41e 13579 rel->r_offset += 4;
3a71aa26
AM
13580 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13581 insn2 = NOP;
102890f0 13582 }
102890f0 13583 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 13584 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 13585 {
102890f0
AM
13586 rel--;
13587 continue;
411e1bfb 13588 }
411e1bfb 13589 }
411e1bfb
AM
13590 break;
13591
727fc41e
AM
13592 case R_PPC64_TLSLD:
13593 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13594 {
13595 unsigned int insn2, insn3;
13596 bfd_vma offset = rel->r_offset;
13597
13598 if (toc_symndx)
13599 sec = local_sections[toc_symndx];
13600 for (r_symndx = 0;
13601 r_symndx < symtab_hdr->sh_info;
13602 r_symndx++)
13603 if (local_sections[r_symndx] == sec)
13604 break;
13605 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13606 r_symndx = STN_UNDEF;
727fc41e 13607 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13608 if (r_symndx != STN_UNDEF)
727fc41e
AM
13609 rel->r_addend -= (local_syms[r_symndx].st_value
13610 + sec->output_offset
13611 + sec->output_section->vma);
13612
13613 r_type = R_PPC64_TPREL16_LO;
13614 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13615 rel->r_offset = offset + d_offset;
13616 /* Zap the reloc on the _tls_get_addr call too. */
13617 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13618 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
13619 insn2 = 0x38630000; /* addi 3,3,0 */
13620 insn3 = bfd_get_32 (output_bfd,
13621 contents + offset + 4);
13622 if (insn3 == NOP
13623 || insn3 == CROR_151515 || insn3 == CROR_313131)
13624 {
13625 rel->r_offset += 4;
13626 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13627 insn2 = NOP;
13628 }
13629 bfd_put_32 (output_bfd, insn2, contents + offset);
13630 rel--;
13631 continue;
13632 }
13633 break;
13634
411e1bfb 13635 case R_PPC64_DTPMOD64:
951fd09b
AM
13636 if (rel + 1 < relend
13637 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13638 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13639 {
951fd09b
AM
13640 if ((tls_mask & TLS_GD) == 0)
13641 {
13642 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13643 if ((tls_mask & TLS_TPRELGD) != 0)
13644 r_type = R_PPC64_TPREL64;
13645 else
13646 {
4ce794b7 13647 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13648 r_type = R_PPC64_NONE;
13649 }
13650 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13651 }
13652 }
13653 else
13654 {
13655 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13656 {
4ce794b7 13657 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13658 r_type = R_PPC64_NONE;
951fd09b 13659 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13660 }
411e1bfb
AM
13661 }
13662 break;
13663
13664 case R_PPC64_TPREL64:
951fd09b 13665 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13666 {
13667 r_type = R_PPC64_NONE;
13668 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13669 }
13670 break;
52a82034
AM
13671
13672 case R_PPC64_REL16_HA:
13673 /* If we are generating a non-PIC executable, edit
13674 . 0: addis 2,12,.TOC.-0b@ha
13675 . addi 2,2,.TOC.-0b@l
13676 used by ELFv2 global entry points to set up r2, to
13677 . lis 2,.TOC.@ha
13678 . addi 2,2,.TOC.@l
13679 if .TOC. is in range. */
13680 if (!info->shared
810d4e75 13681 && !info->traditional_format
52a82034
AM
13682 && h != NULL && &h->elf == htab->elf.hgot
13683 && rel + 1 < relend
13684 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13685 && rel[1].r_offset == rel->r_offset + 4
13686 && rel[1].r_addend == rel->r_addend + 4
13687 && relocation + 0x80008000 <= 0xffffffff)
13688 {
13689 unsigned int insn1, insn2;
13690 bfd_vma offset = rel->r_offset - d_offset;
13691 insn1 = bfd_get_32 (output_bfd, contents + offset);
13692 insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
13693 if ((insn1 & 0xffff0000) == 0x3c4c0000 /* addis 2,12 */
13694 && (insn2 & 0xffff0000) == 0x38420000 /* addi 2,2 */)
13695 {
13696 r_type = R_PPC64_ADDR16_HA;
13697 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13698 rel->r_addend -= d_offset;
13699 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13700 rel[1].r_addend -= d_offset + 4;
13701 bfd_put_32 (output_bfd, 0x3c400000, contents + offset);
13702 }
13703 }
13704 break;
411e1bfb
AM
13705 }
13706
13707 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 13708 insn = 0;
b25116a9
AM
13709 max_br_offset = 1 << 25;
13710 addend = rel->r_addend;
bc30df16 13711 reloc_dest = DEST_NORMAL;
65f38f15 13712 switch (r_type)
5bd4f169
AM
13713 {
13714 default:
65f38f15 13715 break;
5bd4f169 13716
3b421ab3
AM
13717 case R_PPC64_TOCSAVE:
13718 if (relocation + addend == (rel->r_offset
13719 + input_section->output_offset
13720 + input_section->output_section->vma)
13721 && tocsave_find (htab, NO_INSERT,
13722 &local_syms, rel, input_bfd))
13723 {
13724 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13725 if (insn == NOP
13726 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
13727 bfd_put_32 (input_bfd,
13728 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
13729 contents + rel->r_offset);
13730 }
13731 break;
13732
65f38f15
AM
13733 /* Branch taken prediction relocations. */
13734 case R_PPC64_ADDR14_BRTAKEN:
13735 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
13736 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13737 /* Fall thru. */
65f38f15 13738
86c76c7b 13739 /* Branch not taken prediction relocations. */
65f38f15
AM
13740 case R_PPC64_ADDR14_BRNTAKEN:
13741 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
13742 insn |= bfd_get_32 (output_bfd,
13743 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 13744 /* Fall thru. */
86c76c7b 13745
b25116a9
AM
13746 case R_PPC64_REL14:
13747 max_br_offset = 1 << 15;
13748 /* Fall thru. */
5bd4f169 13749
65f38f15 13750 case R_PPC64_REL24:
ad8e1ba5
AM
13751 /* Calls to functions with a different TOC, such as calls to
13752 shared objects, need to alter the TOC pointer. This is
13753 done using a linkage stub. A REL24 branching to these
13754 linkage stubs needs to be followed by a nop, as the nop
13755 will be replaced with an instruction to restore the TOC
13756 base pointer. */
8387904d 13757 fdh = h;
b31867b6
AM
13758 if (h != NULL
13759 && h->oh != NULL
13760 && h->oh->is_func_descriptor)
13761 fdh = ppc_follow_link (h->oh);
31c76678
DK
13762 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13763 htab);
6abec6bc 13764 if (stub_entry != NULL
ad8e1ba5 13765 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 13766 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
13767 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13768 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 13769 {
b25116a9 13770 bfd_boolean can_plt_call = FALSE;
721956f4 13771
ba8ca3e7
AM
13772 /* All of these stubs will modify r2, so there must be a
13773 branch and link followed by a nop. The nop is
13774 replaced by an insn to restore r2. */
eea6121a 13775 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 13776 {
ba8ca3e7
AM
13777 unsigned long br;
13778
13779 br = bfd_get_32 (input_bfd,
13780 contents + rel->r_offset);
13781 if ((br & 1) != 0)
41bd81ab 13782 {
ba8ca3e7
AM
13783 unsigned long nop;
13784
13785 nop = bfd_get_32 (input_bfd,
13786 contents + rel->r_offset + 4);
13787 if (nop == NOP
13788 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 13789 {
ba8ca3e7
AM
13790 if (h != NULL
13791 && (h == htab->tls_get_addr_fd
13792 || h == htab->tls_get_addr)
e7d1c40c 13793 && !htab->params->no_tls_get_addr_opt)
ba8ca3e7
AM
13794 {
13795 /* Special stub used, leave nop alone. */
13796 }
13797 else
a078d95a
AM
13798 bfd_put_32 (input_bfd,
13799 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
13800 contents + rel->r_offset + 4);
13801 can_plt_call = TRUE;
a7f2871e 13802 }
41bd81ab 13803 }
5bd4f169 13804 }
721956f4 13805
ba8ca3e7 13806 if (!can_plt_call && h != NULL)
721956f4 13807 {
ba8ca3e7
AM
13808 const char *name = h->elf.root.root.string;
13809
13810 if (*name == '.')
13811 ++name;
13812
13813 if (strncmp (name, "__libc_start_main", 17) == 0
13814 && (name[17] == 0 || name[17] == '@'))
6ab189d5 13815 {
ba8ca3e7
AM
13816 /* Allow crt1 branch to go via a toc adjusting
13817 stub. Other calls that never return could do
13818 the same, if we could detect such. */
b25116a9 13819 can_plt_call = TRUE;
6ab189d5 13820 }
ba8ca3e7
AM
13821 }
13822
13823 if (!can_plt_call)
13824 {
13825 /* g++ as of 20130507 emits self-calls without a
13826 following nop. This is arguably wrong since we
13827 have conflicting information. On the one hand a
13828 global symbol and on the other a local call
13829 sequence, but don't error for this special case.
13830 It isn't possible to cheaply verify we have
13831 exactly such a call. Allow all calls to the same
13832 section. */
13833 asection *code_sec = sec;
13834
13835 if (get_opd_info (sec) != NULL)
ad8e1ba5 13836 {
ba8ca3e7
AM
13837 bfd_vma off = (relocation + addend
13838 - sec->output_section->vma
13839 - sec->output_offset);
bc30df16 13840
ba8ca3e7 13841 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 13842 }
ba8ca3e7
AM
13843 if (code_sec == input_section)
13844 can_plt_call = TRUE;
13845 }
13846
13847 if (!can_plt_call)
13848 {
4805fc55
AM
13849 if (stub_entry->stub_type == ppc_stub_plt_call
13850 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13851 info->callbacks->einfo
13852 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13853 "recompile with -fPIC\n"),
13854 input_bfd, input_section, rel->r_offset, sym_name);
13855 else
13856 info->callbacks->einfo
13857 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13858 "(-mcmodel=small toc adjust stub)\n"),
13859 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
13860
13861 bfd_set_error (bfd_error_bad_value);
13862 ret = FALSE;
721956f4
AM
13863 }
13864
b25116a9 13865 if (can_plt_call
794e51c0
AM
13866 && (stub_entry->stub_type == ppc_stub_plt_call
13867 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
13868 unresolved_reloc = FALSE;
13869 }
13870
6abec6bc
AM
13871 if ((stub_entry == NULL
13872 || stub_entry->stub_type == ppc_stub_long_branch
13873 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
13874 && get_opd_info (sec) != NULL)
13875 {
13876 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
13877 bfd_vma off = (relocation + addend
13878 - sec->output_section->vma
13879 - sec->output_offset);
aef36ac1 13880 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
13881 if (dest != (bfd_vma) -1)
13882 {
13883 relocation = dest;
13884 addend = 0;
bc30df16 13885 reloc_dest = DEST_OPD;
8387904d
AM
13886 }
13887 }
13888
b25116a9
AM
13889 /* If the branch is out of reach we ought to have a long
13890 branch stub. */
13891 from = (rel->r_offset
13892 + input_section->output_offset
13893 + input_section->output_section->vma);
13894
6911b7dc
AM
13895 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13896 ? fdh->elf.other
13897 : sym->st_other);
13898
6abec6bc
AM
13899 if (stub_entry != NULL
13900 && (stub_entry->stub_type == ppc_stub_long_branch
13901 || stub_entry->stub_type == ppc_stub_plt_branch)
13902 && (r_type == R_PPC64_ADDR14_BRTAKEN
13903 || r_type == R_PPC64_ADDR14_BRNTAKEN
13904 || (relocation + addend - from + max_br_offset
13905 < 2 * max_br_offset)))
13906 /* Don't use the stub if this branch is in range. */
13907 stub_entry = NULL;
b25116a9
AM
13908
13909 if (stub_entry != NULL)
13910 {
13911 /* Munge up the value and addend so that we call the stub
13912 rather than the procedure directly. */
13913 relocation = (stub_entry->stub_offset
13914 + stub_entry->stub_sec->output_offset
13915 + stub_entry->stub_sec->output_section->vma);
13916 addend = 0;
bc30df16 13917 reloc_dest = DEST_STUB;
3b421ab3 13918
794e51c0
AM
13919 if ((stub_entry->stub_type == ppc_stub_plt_call
13920 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13921 && (ALWAYS_EMIT_R2SAVE
13922 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
13923 && rel + 1 < relend
13924 && rel[1].r_offset == rel->r_offset + 4
13925 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13926 relocation += 4;
b25116a9
AM
13927 }
13928
13929 if (insn != 0)
13930 {
794e51c0 13931 if (is_isa_v2)
721956f4 13932 {
b25116a9
AM
13933 /* Set 'a' bit. This is 0b00010 in BO field for branch
13934 on CR(BI) insns (BO == 001at or 011at), and 0b01000
13935 for branch on CTR insns (BO == 1a00t or 1a01t). */
13936 if ((insn & (0x14 << 21)) == (0x04 << 21))
13937 insn |= 0x02 << 21;
13938 else if ((insn & (0x14 << 21)) == (0x10 << 21))
13939 insn |= 0x08 << 21;
13940 else
13941 break;
13942 }
13943 else
13944 {
13945 /* Invert 'y' bit if not the default. */
4cc603a5 13946 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 13947 insn ^= 0x01 << 21;
721956f4 13948 }
b25116a9
AM
13949
13950 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 13951 }
e86ce104 13952
06da1e8e
AM
13953 /* NOP out calls to undefined weak functions.
13954 We can thus call a weak function without first
13955 checking whether the function is defined. */
b25116a9 13956 else if (h != NULL
039b3fef 13957 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 13958 && h->elf.dynindx == -1
b25116a9
AM
13959 && r_type == R_PPC64_REL24
13960 && relocation == 0
4cc603a5 13961 && addend == 0)
e86ce104 13962 {
06da1e8e
AM
13963 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13964 continue;
e86ce104 13965 }
65f38f15
AM
13966 break;
13967 }
5bd4f169 13968
65f38f15 13969 /* Set `addend'. */
411e1bfb 13970 tls_type = 0;
65f38f15
AM
13971 switch (r_type)
13972 {
13973 default:
25f53a85 13974 info->callbacks->einfo
bc30df16 13975 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 13976 input_bfd, (int) r_type, sym_name);
5bd4f169 13977
65f38f15 13978 bfd_set_error (bfd_error_bad_value);
b34976b6 13979 ret = FALSE;
65f38f15 13980 continue;
5bd4f169 13981
65f38f15 13982 case R_PPC64_NONE:
411e1bfb 13983 case R_PPC64_TLS:
727fc41e
AM
13984 case R_PPC64_TLSGD:
13985 case R_PPC64_TLSLD:
3b421ab3 13986 case R_PPC64_TOCSAVE:
04c9666a
AM
13987 case R_PPC64_GNU_VTINHERIT:
13988 case R_PPC64_GNU_VTENTRY:
65f38f15 13989 continue;
5bd4f169
AM
13990
13991 /* GOT16 relocations. Like an ADDR16 using the symbol's
13992 address in the GOT as relocation value instead of the
411e1bfb 13993 symbol's value itself. Also, create a GOT entry for the
5bd4f169 13994 symbol and put the symbol value there. */
411e1bfb
AM
13995 case R_PPC64_GOT_TLSGD16:
13996 case R_PPC64_GOT_TLSGD16_LO:
13997 case R_PPC64_GOT_TLSGD16_HI:
13998 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 13999 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14000 goto dogot;
14001
14002 case R_PPC64_GOT_TLSLD16:
14003 case R_PPC64_GOT_TLSLD16_LO:
14004 case R_PPC64_GOT_TLSLD16_HI:
14005 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14006 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14007 goto dogot;
14008
14009 case R_PPC64_GOT_TPREL16_DS:
14010 case R_PPC64_GOT_TPREL16_LO_DS:
14011 case R_PPC64_GOT_TPREL16_HI:
14012 case R_PPC64_GOT_TPREL16_HA:
14013 tls_type = TLS_TLS | TLS_TPREL;
14014 goto dogot;
14015
14016 case R_PPC64_GOT_DTPREL16_DS:
14017 case R_PPC64_GOT_DTPREL16_LO_DS:
14018 case R_PPC64_GOT_DTPREL16_HI:
14019 case R_PPC64_GOT_DTPREL16_HA:
14020 tls_type = TLS_TLS | TLS_DTPREL;
14021 goto dogot;
14022
65f38f15
AM
14023 case R_PPC64_GOT16:
14024 case R_PPC64_GOT16_LO:
14025 case R_PPC64_GOT16_HI:
14026 case R_PPC64_GOT16_HA:
14027 case R_PPC64_GOT16_DS:
14028 case R_PPC64_GOT16_LO_DS:
411e1bfb 14029 dogot:
5bd4f169
AM
14030 {
14031 /* Relocation is to the entry for this symbol in the global
14032 offset table. */
e717da7e 14033 asection *got;
d881513a 14034 bfd_vma *offp;
5bd4f169 14035 bfd_vma off;
d881513a 14036 unsigned long indx = 0;
927be08e 14037 struct got_entry *ent;
65f38f15 14038
d881513a
AM
14039 if (tls_type == (TLS_TLS | TLS_LD)
14040 && (h == NULL
f5385ebf 14041 || !h->elf.def_dynamic))
927be08e 14042 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14043 else
5bd4f169 14044 {
5bd4f169 14045
d881513a
AM
14046 if (h != NULL)
14047 {
14048 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
14049 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
14050 &h->elf)
d881513a 14051 || (info->shared
afe397ea 14052 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
14053 /* This is actually a static link, or it is a
14054 -Bsymbolic link and the symbol is defined
14055 locally, or the symbol was forced to be local
14056 because of a version file. */
14057 ;
14058 else
14059 {
14acf4dc 14060 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 14061 indx = h->elf.dynindx;
d881513a
AM
14062 unresolved_reloc = FALSE;
14063 }
039b3fef 14064 ent = h->elf.got.glist;
d881513a 14065 }
411e1bfb 14066 else
5bd4f169 14067 {
d881513a
AM
14068 if (local_got_ents == NULL)
14069 abort ();
14070 ent = local_got_ents[r_symndx];
5bd4f169 14071 }
d881513a
AM
14072
14073 for (; ent != NULL; ent = ent->next)
31c76678 14074 if (ent->addend == orig_rel.r_addend
e717da7e 14075 && ent->owner == input_bfd
d881513a
AM
14076 && ent->tls_type == tls_type)
14077 break;
5bd4f169 14078 }
411e1bfb 14079
927be08e
AM
14080 if (ent == NULL)
14081 abort ();
14082 if (ent->is_indirect)
14083 ent = ent->got.ent;
14084 offp = &ent->got.offset;
14085 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14086 if (got == NULL)
14087 abort ();
14088
411e1bfb
AM
14089 /* The offset must always be a multiple of 8. We use the
14090 least significant bit to record whether we have already
14091 processed this entry. */
d881513a 14092 off = *offp;
411e1bfb
AM
14093 if ((off & 1) != 0)
14094 off &= ~1;
5bd4f169
AM
14095 else
14096 {
411e1bfb
AM
14097 /* Generate relocs for the dynamic linker, except in
14098 the case of TLSLD where we'll use one entry per
14099 module. */
25f23106
AM
14100 asection *relgot;
14101 bfd_boolean ifunc;
e717da7e 14102
d881513a 14103 *offp = off | 1;
25f23106
AM
14104 relgot = NULL;
14105 ifunc = (h != NULL
14106 ? h->elf.type == STT_GNU_IFUNC
14107 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14108 if (ifunc)
33e44f2e 14109 relgot = htab->elf.irelplt;
19e08130
AM
14110 else if ((info->shared || indx != 0)
14111 && (h == NULL
14112 || (tls_type == (TLS_TLS | TLS_LD)
14113 && !h->elf.def_dynamic)
14114 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14115 || h->elf.root.type != bfd_link_hash_undefweak))
14116 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14117 if (relgot != NULL)
5bd4f169 14118 {
e717da7e
AM
14119 outrel.r_offset = (got->output_section->vma
14120 + got->output_offset
411e1bfb 14121 + off);
4cc603a5 14122 outrel.r_addend = addend;
d881513a 14123 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14124 {
411e1bfb 14125 outrel.r_addend = 0;
e515b051 14126 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14127 if (tls_type == (TLS_TLS | TLS_GD))
14128 {
e717da7e
AM
14129 loc = relgot->contents;
14130 loc += (relgot->reloc_count++
d881513a
AM
14131 * sizeof (Elf64_External_Rela));
14132 bfd_elf64_swap_reloca_out (output_bfd,
14133 &outrel, loc);
e515b051 14134 outrel.r_offset += 8;
4cc603a5 14135 outrel.r_addend = addend;
d881513a
AM
14136 outrel.r_info
14137 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14138 }
411e1bfb 14139 }
951fd09b 14140 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14141 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14142 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14143 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14144 else if (indx != 0)
14145 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14146 else
81407a69 14147 {
25f23106
AM
14148 if (ifunc)
14149 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14150 else
14151 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14152
14153 /* Write the .got section contents for the sake
14154 of prelink. */
e717da7e 14155 loc = got->contents + off;
23fbd6fa
JJ
14156 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14157 loc);
81407a69 14158 }
81407a69
AM
14159
14160 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14161 {
14162 outrel.r_addend += relocation;
14163 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14164 {
14165 if (htab->elf.tls_sec == NULL)
14166 outrel.r_addend = 0;
14167 else
14168 outrel.r_addend -= htab->elf.tls_sec->vma;
14169 }
e515b051 14170 }
e717da7e
AM
14171 loc = relgot->contents;
14172 loc += (relgot->reloc_count++
411e1bfb
AM
14173 * sizeof (Elf64_External_Rela));
14174 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14175 }
14176
ad8e1ba5 14177 /* Init the .got section contents here if we're not
81407a69 14178 emitting a reloc. */
d881513a 14179 else
411e1bfb 14180 {
4cc603a5 14181 relocation += addend;
7b609f53
AM
14182 if (tls_type == (TLS_TLS | TLS_LD))
14183 relocation = 1;
14184 else if (tls_type != 0)
411e1bfb 14185 {
989f9879
AM
14186 if (htab->elf.tls_sec == NULL)
14187 relocation = 0;
14188 else
14189 {
14190 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14191 if (tls_type == (TLS_TLS | TLS_TPREL))
14192 relocation += DTP_OFFSET - TP_OFFSET;
14193 }
5bd4f169 14194
7b609f53
AM
14195 if (tls_type == (TLS_TLS | TLS_GD))
14196 {
14197 bfd_put_64 (output_bfd, relocation,
e717da7e 14198 got->contents + off + 8);
7b609f53
AM
14199 relocation = 1;
14200 }
411e1bfb 14201 }
7b609f53 14202
411e1bfb 14203 bfd_put_64 (output_bfd, relocation,
e717da7e 14204 got->contents + off);
5bd4f169
AM
14205 }
14206 }
14207
65f38f15
AM
14208 if (off >= (bfd_vma) -2)
14209 abort ();
14210
bf102f86
AM
14211 relocation = got->output_section->vma + got->output_offset + off;
14212 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 14213 }
65f38f15
AM
14214 break;
14215
14216 case R_PPC64_PLT16_HA:
14217 case R_PPC64_PLT16_HI:
14218 case R_PPC64_PLT16_LO:
14219 case R_PPC64_PLT32:
14220 case R_PPC64_PLT64:
14221 /* Relocation is to the entry for this symbol in the
14222 procedure linkage table. */
14223
14224 /* Resolve a PLT reloc against a local symbol directly,
14225 without using the procedure linkage table. */
14226 if (h == NULL)
14227 break;
14228
411e1bfb
AM
14229 /* It's possible that we didn't make a PLT entry for this
14230 symbol. This happens when statically linking PIC code,
14231 or when using -Bsymbolic. Go find a match if there is a
14232 PLT entry. */
33e44f2e 14233 if (htab->elf.splt != NULL)
65f38f15 14234 {
411e1bfb 14235 struct plt_entry *ent;
039b3fef 14236 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
a345bc8d
AM
14237 if (ent->plt.offset != (bfd_vma) -1
14238 && ent->addend == orig_rel.r_addend)
411e1bfb 14239 {
33e44f2e
AM
14240 relocation = (htab->elf.splt->output_section->vma
14241 + htab->elf.splt->output_offset
411e1bfb
AM
14242 + ent->plt.offset);
14243 unresolved_reloc = FALSE;
a345bc8d 14244 break;
411e1bfb 14245 }
65f38f15 14246 }
65f38f15 14247 break;
5bd4f169 14248
0b13192e
AM
14249 case R_PPC64_TOC:
14250 /* Relocation value is TOC base. */
14251 relocation = TOCstart;
cf35638d 14252 if (r_symndx == STN_UNDEF)
0b13192e 14253 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
14254 else if (unresolved_reloc)
14255 ;
14256 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
14257 relocation += htab->stub_group[sec->id].toc_off;
14258 else
14259 unresolved_reloc = TRUE;
ab96bf03 14260 goto dodyn;
0b13192e 14261
5bd4f169
AM
14262 /* TOC16 relocs. We want the offset relative to the TOC base,
14263 which is the address of the start of the TOC plus 0x8000.
14264 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14265 in this order. */
65f38f15
AM
14266 case R_PPC64_TOC16:
14267 case R_PPC64_TOC16_LO:
14268 case R_PPC64_TOC16_HI:
14269 case R_PPC64_TOC16_DS:
14270 case R_PPC64_TOC16_LO_DS:
14271 case R_PPC64_TOC16_HA:
ad8e1ba5 14272 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
14273 break;
14274
14275 /* Relocate against the beginning of the section. */
65f38f15
AM
14276 case R_PPC64_SECTOFF:
14277 case R_PPC64_SECTOFF_LO:
14278 case R_PPC64_SECTOFF_HI:
14279 case R_PPC64_SECTOFF_DS:
14280 case R_PPC64_SECTOFF_LO_DS:
14281 case R_PPC64_SECTOFF_HA:
4ce794b7 14282 if (sec != NULL)
65f38f15 14283 addend -= sec->output_section->vma;
5bd4f169
AM
14284 break;
14285
25f23106
AM
14286 case R_PPC64_REL16:
14287 case R_PPC64_REL16_LO:
14288 case R_PPC64_REL16_HI:
14289 case R_PPC64_REL16_HA:
14290 break;
14291
721956f4
AM
14292 case R_PPC64_REL14:
14293 case R_PPC64_REL14_BRNTAKEN:
14294 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14295 case R_PPC64_REL24:
14296 break;
14297
411e1bfb
AM
14298 case R_PPC64_TPREL16:
14299 case R_PPC64_TPREL16_LO:
14300 case R_PPC64_TPREL16_HI:
14301 case R_PPC64_TPREL16_HA:
14302 case R_PPC64_TPREL16_DS:
14303 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14304 case R_PPC64_TPREL16_HIGH:
14305 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14306 case R_PPC64_TPREL16_HIGHER:
14307 case R_PPC64_TPREL16_HIGHERA:
14308 case R_PPC64_TPREL16_HIGHEST:
14309 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14310 if (h != NULL
14311 && h->elf.root.type == bfd_link_hash_undefweak
14312 && h->elf.dynindx == -1)
14313 {
14314 /* Make this relocation against an undefined weak symbol
14315 resolve to zero. This is really just a tweak, since
14316 code using weak externs ought to check that they are
14317 defined before using them. */
14318 bfd_byte *p = contents + rel->r_offset - d_offset;
14319
14320 insn = bfd_get_32 (output_bfd, p);
14321 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14322 if (insn != 0)
14323 bfd_put_32 (output_bfd, insn, p);
14324 break;
14325 }
989f9879
AM
14326 if (htab->elf.tls_sec != NULL)
14327 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14328 if (info->shared)
14329 /* The TPREL16 relocs shouldn't really be used in shared
14330 libs as they will result in DT_TEXTREL being set, but
14331 support them anyway. */
14332 goto dodyn;
14333 break;
14334
14335 case R_PPC64_DTPREL16:
14336 case R_PPC64_DTPREL16_LO:
14337 case R_PPC64_DTPREL16_HI:
14338 case R_PPC64_DTPREL16_HA:
14339 case R_PPC64_DTPREL16_DS:
14340 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14341 case R_PPC64_DTPREL16_HIGH:
14342 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14343 case R_PPC64_DTPREL16_HIGHER:
14344 case R_PPC64_DTPREL16_HIGHERA:
14345 case R_PPC64_DTPREL16_HIGHEST:
14346 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14347 if (htab->elf.tls_sec != NULL)
14348 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14349 break;
14350
45965137
AM
14351 case R_PPC64_ADDR64_LOCAL:
14352 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14353 ? h->elf.other
14354 : sym->st_other);
14355 break;
14356
e515b051
AM
14357 case R_PPC64_DTPMOD64:
14358 relocation = 1;
14359 addend = 0;
14360 goto dodyn;
14361
411e1bfb 14362 case R_PPC64_TPREL64:
989f9879
AM
14363 if (htab->elf.tls_sec != NULL)
14364 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14365 goto dodyn;
14366
14367 case R_PPC64_DTPREL64:
989f9879
AM
14368 if (htab->elf.tls_sec != NULL)
14369 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14370 /* Fall thru */
14371
65f38f15
AM
14372 /* Relocations that may need to be propagated if this is a
14373 dynamic object. */
04c9666a 14374 case R_PPC64_REL30:
65f38f15
AM
14375 case R_PPC64_REL32:
14376 case R_PPC64_REL64:
14377 case R_PPC64_ADDR14:
14378 case R_PPC64_ADDR14_BRNTAKEN:
14379 case R_PPC64_ADDR14_BRTAKEN:
14380 case R_PPC64_ADDR16:
14381 case R_PPC64_ADDR16_DS:
14382 case R_PPC64_ADDR16_HA:
14383 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14384 case R_PPC64_ADDR16_HIGH:
14385 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14386 case R_PPC64_ADDR16_HIGHER:
14387 case R_PPC64_ADDR16_HIGHERA:
14388 case R_PPC64_ADDR16_HIGHEST:
14389 case R_PPC64_ADDR16_HIGHESTA:
14390 case R_PPC64_ADDR16_LO:
14391 case R_PPC64_ADDR16_LO_DS:
14392 case R_PPC64_ADDR24:
65f38f15
AM
14393 case R_PPC64_ADDR32:
14394 case R_PPC64_ADDR64:
14395 case R_PPC64_UADDR16:
14396 case R_PPC64_UADDR32:
14397 case R_PPC64_UADDR64:
411e1bfb 14398 dodyn:
5d1634d7 14399 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14400 break;
14401
41bd81ab
AM
14402 if (NO_OPD_RELOCS && is_opd)
14403 break;
14404
65f38f15 14405 if ((info->shared
4e795f50 14406 && (h == NULL
039b3fef
AM
14407 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14408 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 14409 && (must_be_dyn_reloc (info, r_type)
039b3fef 14410 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
14411 || (ELIMINATE_COPY_RELOCS
14412 && !info->shared
65f38f15 14413 && h != NULL
039b3fef 14414 && h->elf.dynindx != -1
f5385ebf 14415 && !h->elf.non_got_ref
25f23106
AM
14416 && !h->elf.def_regular)
14417 || (!info->shared
14418 && (h != NULL
14419 ? h->elf.type == STT_GNU_IFUNC
14420 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 14421 {
b34976b6 14422 bfd_boolean skip, relocate;
65f38f15 14423 asection *sreloc;
1cf1f670 14424 bfd_vma out_off;
65f38f15
AM
14425
14426 /* When generating a dynamic object, these relocations
14427 are copied into the output file to be resolved at run
14428 time. */
14429
b34976b6
AM
14430 skip = FALSE;
14431 relocate = FALSE;
65f38f15 14432
1cf1f670
AM
14433 out_off = _bfd_elf_section_offset (output_bfd, info,
14434 input_section, rel->r_offset);
14435 if (out_off == (bfd_vma) -1)
b34976b6 14436 skip = TRUE;
1cf1f670 14437 else if (out_off == (bfd_vma) -2)
b34976b6 14438 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14439 out_off += (input_section->output_section->vma
14440 + input_section->output_offset);
14441 outrel.r_offset = out_off;
411e1bfb 14442 outrel.r_addend = rel->r_addend;
65f38f15 14443
1cf1f670
AM
14444 /* Optimize unaligned reloc use. */
14445 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14446 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14447 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14448 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14449 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14450 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14451 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14452 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14453 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14454
65f38f15 14455 if (skip)
0bb2d96a 14456 memset (&outrel, 0, sizeof outrel);
afe397ea 14457 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14458 && !is_opd
14459 && r_type != R_PPC64_TOC)
14acf4dc
MR
14460 {
14461 BFD_ASSERT (h->elf.dynindx != -1);
14462 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14463 }
65f38f15
AM
14464 else
14465 {
41bd81ab
AM
14466 /* This symbol is local, or marked to become local,
14467 or this is an opd section reloc which must point
14468 at a local function. */
65f38f15 14469 outrel.r_addend += relocation;
e86ce104 14470 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14471 {
3fad3c7c 14472 if (is_opd && h != NULL)
afbe61cf
AM
14473 {
14474 /* Lie about opd entries. This case occurs
14475 when building shared libraries and we
14476 reference a function in another shared
3fad3c7c
AM
14477 lib. The same thing happens for a weak
14478 definition in an application that's
14479 overridden by a strong definition in a
14480 shared lib. (I believe this is a generic
14481 bug in binutils handling of weak syms.)
14482 In these cases we won't use the opd
1e2f5b6e 14483 entry in this lib. */
b34976b6 14484 unresolved_reloc = FALSE;
afbe61cf 14485 }
25f23106
AM
14486 if (!is_opd
14487 && r_type == R_PPC64_ADDR64
14488 && (h != NULL
14489 ? h->elf.type == STT_GNU_IFUNC
14490 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14491 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14492 else
14493 {
14494 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14495
25f23106
AM
14496 /* We need to relocate .opd contents for ld.so.
14497 Prelink also wants simple and consistent rules
14498 for relocs. This make all RELATIVE relocs have
14499 *r_offset equal to r_addend. */
14500 relocate = TRUE;
14501 }
65f38f15
AM
14502 }
14503 else
14504 {
14505 long indx = 0;
14506
25f23106
AM
14507 if (h != NULL
14508 ? h->elf.type == STT_GNU_IFUNC
14509 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14510 {
25f53a85 14511 info->callbacks->einfo
bc30df16
AM
14512 (_("%P: %H: %s for indirect "
14513 "function `%T' unsupported\n"),
25f53a85 14514 input_bfd, input_section, rel->r_offset,
25f23106
AM
14515 ppc64_elf_howto_table[r_type]->name,
14516 sym_name);
14517 ret = FALSE;
14518 }
cf35638d 14519 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14520 ;
14521 else if (sec == NULL || sec->owner == NULL)
14522 {
14523 bfd_set_error (bfd_error_bad_value);
b34976b6 14524 return FALSE;
65f38f15
AM
14525 }
14526 else
14527 {
14528 asection *osec;
14529
14530 osec = sec->output_section;
14531 indx = elf_section_data (osec)->dynindx;
14532
74541ad4
AM
14533 if (indx == 0)
14534 {
14535 if ((osec->flags & SEC_READONLY) == 0
14536 && htab->elf.data_index_section != NULL)
14537 osec = htab->elf.data_index_section;
14538 else
14539 osec = htab->elf.text_index_section;
14540 indx = elf_section_data (osec)->dynindx;
14541 }
14542 BFD_ASSERT (indx != 0);
14543
65f38f15
AM
14544 /* We are turning this relocation into one
14545 against a section symbol, so subtract out
14546 the output section's address but not the
14547 offset of the input section in the output
14548 section. */
14549 outrel.r_addend -= osec->vma;
14550 }
14551
14552 outrel.r_info = ELF64_R_INFO (indx, r_type);
14553 }
14554 }
14555
14556 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14557 if (h != NULL
14558 ? h->elf.type == STT_GNU_IFUNC
14559 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
33e44f2e 14560 sreloc = htab->elf.irelplt;
65f38f15
AM
14561 if (sreloc == NULL)
14562 abort ();
14563
dfbb6ac9
AM
14564 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14565 >= sreloc->size)
14566 abort ();
947216bf
AM
14567 loc = sreloc->contents;
14568 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14569 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14570
14571 /* If this reloc is against an external symbol, it will
14572 be computed at runtime, so there's no need to do
81407a69
AM
14573 anything now. However, for the sake of prelink ensure
14574 that the section contents are a known value. */
65f38f15 14575 if (! relocate)
81407a69
AM
14576 {
14577 unresolved_reloc = FALSE;
14578 /* The value chosen here is quite arbitrary as ld.so
14579 ignores section contents except for the special
14580 case of .opd where the contents might be accessed
14581 before relocation. Choose zero, as that won't
14582 cause reloc overflow. */
14583 relocation = 0;
14584 addend = 0;
14585 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14586 to improve backward compatibility with older
14587 versions of ld. */
14588 if (r_type == R_PPC64_ADDR64)
14589 addend = outrel.r_addend;
14590 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14591 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14592 addend = (input_section->output_section->vma
14593 + input_section->output_offset
14594 + rel->r_offset);
81407a69 14595 }
65f38f15 14596 }
5bd4f169
AM
14597 break;
14598
65f38f15
AM
14599 case R_PPC64_COPY:
14600 case R_PPC64_GLOB_DAT:
14601 case R_PPC64_JMP_SLOT:
25f23106 14602 case R_PPC64_JMP_IREL:
65f38f15
AM
14603 case R_PPC64_RELATIVE:
14604 /* We shouldn't ever see these dynamic relocs in relocatable
14605 files. */
ae9a127f 14606 /* Fall through. */
65f38f15
AM
14607
14608 case R_PPC64_PLTGOT16:
14609 case R_PPC64_PLTGOT16_DS:
14610 case R_PPC64_PLTGOT16_HA:
14611 case R_PPC64_PLTGOT16_HI:
14612 case R_PPC64_PLTGOT16_LO:
14613 case R_PPC64_PLTGOT16_LO_DS:
14614 case R_PPC64_PLTREL32:
14615 case R_PPC64_PLTREL64:
14616 /* These ones haven't been implemented yet. */
14617
25f53a85 14618 info->callbacks->einfo
bc30df16 14619 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14620 input_bfd,
4ce794b7 14621 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14622
14623 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14624 ret = FALSE;
5bd4f169 14625 continue;
65f38f15 14626 }
5bd4f169 14627
67f0cbdb
AM
14628 /* Multi-instruction sequences that access the TOC can be
14629 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14630 to nop; addi rb,r2,x; */
14631 switch (r_type)
14632 {
14633 default:
14634 break;
14635
14636 case R_PPC64_GOT_TLSLD16_HI:
14637 case R_PPC64_GOT_TLSGD16_HI:
14638 case R_PPC64_GOT_TPREL16_HI:
14639 case R_PPC64_GOT_DTPREL16_HI:
14640 case R_PPC64_GOT16_HI:
14641 case R_PPC64_TOC16_HI:
14642 /* These relocs would only be useful if building up an
14643 offset to later add to r2, perhaps in an indexed
14644 addressing mode instruction. Don't try to optimize.
14645 Unfortunately, the possibility of someone building up an
14646 offset like this or even with the HA relocs, means that
14647 we need to check the high insn when optimizing the low
14648 insn. */
14649 break;
14650
14651 case R_PPC64_GOT_TLSLD16_HA:
14652 case R_PPC64_GOT_TLSGD16_HA:
14653 case R_PPC64_GOT_TPREL16_HA:
14654 case R_PPC64_GOT_DTPREL16_HA:
14655 case R_PPC64_GOT16_HA:
14656 case R_PPC64_TOC16_HA:
98528052 14657 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14658 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
14659 {
14660 bfd_byte *p = contents + (rel->r_offset & ~3);
14661 bfd_put_32 (input_bfd, NOP, p);
14662 }
67f0cbdb
AM
14663 break;
14664
14665 case R_PPC64_GOT_TLSLD16_LO:
14666 case R_PPC64_GOT_TLSGD16_LO:
14667 case R_PPC64_GOT_TPREL16_LO_DS:
14668 case R_PPC64_GOT_DTPREL16_LO_DS:
14669 case R_PPC64_GOT16_LO:
14670 case R_PPC64_GOT16_LO_DS:
14671 case R_PPC64_TOC16_LO:
14672 case R_PPC64_TOC16_LO_DS:
98528052 14673 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14674 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
14675 {
14676 bfd_byte *p = contents + (rel->r_offset & ~3);
14677 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
14678 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14679 {
14680 /* Transform addic to addi when we change reg. */
14681 insn &= ~((0x3f << 26) | (0x1f << 16));
14682 insn |= (14u << 26) | (2 << 16);
14683 }
14684 else
67f0cbdb 14685 {
98528052
AM
14686 insn &= ~(0x1f << 16);
14687 insn |= 2 << 16;
67f0cbdb 14688 }
560c8763 14689 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
14690 }
14691 break;
14692 }
14693
65f38f15 14694 /* Do any further special processing. */
b80eed39 14695 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
14696 switch (r_type)
14697 {
14698 default:
14699 break;
14700
25f23106 14701 case R_PPC64_REL16_HA:
f9c6b907
AM
14702 case R_PPC64_ADDR16_HA:
14703 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14704 case R_PPC64_ADDR16_HIGHERA:
14705 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
14706 case R_PPC64_TOC16_HA:
14707 case R_PPC64_SECTOFF_HA:
411e1bfb 14708 case R_PPC64_TPREL16_HA:
f9c6b907 14709 case R_PPC64_TPREL16_HIGHA:
411e1bfb 14710 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 14711 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
14712 case R_PPC64_DTPREL16_HA:
14713 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 14714 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 14715 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
14716 /* It's just possible that this symbol is a weak symbol
14717 that's not actually defined anywhere. In that case,
14718 'sec' would be NULL, and we should leave the symbol
14719 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
14720 if (sec == NULL)
14721 break;
14722 /* Fall thru */
14723
14724 case R_PPC64_GOT16_HA:
14725 case R_PPC64_PLTGOT16_HA:
14726 case R_PPC64_PLT16_HA:
14727 case R_PPC64_GOT_TLSGD16_HA:
14728 case R_PPC64_GOT_TLSLD16_HA:
14729 case R_PPC64_GOT_TPREL16_HA:
14730 case R_PPC64_GOT_DTPREL16_HA:
14731 /* Add 0x10000 if sign bit in 0:15 is set.
14732 Bits 0:15 are not used. */
14733 addend += 0x8000;
65f38f15
AM
14734 break;
14735
14736 case R_PPC64_ADDR16_DS:
14737 case R_PPC64_ADDR16_LO_DS:
14738 case R_PPC64_GOT16_DS:
14739 case R_PPC64_GOT16_LO_DS:
14740 case R_PPC64_PLT16_LO_DS:
14741 case R_PPC64_SECTOFF_DS:
14742 case R_PPC64_SECTOFF_LO_DS:
14743 case R_PPC64_TOC16_DS:
14744 case R_PPC64_TOC16_LO_DS:
14745 case R_PPC64_PLTGOT16_DS:
14746 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
14747 case R_PPC64_GOT_TPREL16_DS:
14748 case R_PPC64_GOT_TPREL16_LO_DS:
14749 case R_PPC64_GOT_DTPREL16_DS:
14750 case R_PPC64_GOT_DTPREL16_LO_DS:
14751 case R_PPC64_TPREL16_DS:
14752 case R_PPC64_TPREL16_LO_DS:
14753 case R_PPC64_DTPREL16_DS:
14754 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
14755 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14756 mask = 3;
14757 /* If this reloc is against an lq insn, then the value must be
14758 a multiple of 16. This is somewhat of a hack, but the
14759 "correct" way to do this by defining _DQ forms of all the
14760 _DS relocs bloats all reloc switches in this file. It
14761 doesn't seem to make much sense to use any of these relocs
14762 in data, so testing the insn should be safe. */
494dac0c 14763 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
14764 mask = 15;
14765 if (((relocation + addend) & mask) != 0)
65f38f15 14766 {
25f53a85 14767 info->callbacks->einfo
8de848d8 14768 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 14769 input_bfd, input_section, rel->r_offset,
b80eed39 14770 howto->name,
adadcc0c 14771 mask + 1);
65f38f15 14772 bfd_set_error (bfd_error_bad_value);
b34976b6 14773 ret = FALSE;
65f38f15
AM
14774 continue;
14775 }
14776 break;
5bd4f169
AM
14777 }
14778
239e1f3a
AM
14779 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14780 because such sections are not SEC_ALLOC and thus ld.so will
14781 not process them. */
65f38f15 14782 if (unresolved_reloc
239e1f3a 14783 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
14784 && h->elf.def_dynamic)
14785 && _bfd_elf_section_offset (output_bfd, info, input_section,
14786 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 14787 {
25f53a85 14788 info->callbacks->einfo
bc30df16 14789 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 14790 input_bfd, input_section, rel->r_offset,
b80eed39 14791 howto->name,
039b3fef 14792 h->elf.root.root.string);
b34976b6 14793 ret = FALSE;
9c07fe7c 14794 }
5bd4f169 14795
b80eed39
AM
14796 /* 16-bit fields in insns mostly have signed values, but a
14797 few insns have 16-bit unsigned values. Really, we should
14798 have different reloc types. */
14799 if (howto->complain_on_overflow != complain_overflow_dont
14800 && howto->dst_mask == 0xffff
14801 && (input_section->flags & SEC_CODE) != 0)
14802 {
14803 enum complain_overflow complain = complain_overflow_signed;
14804
14805 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
14806 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
14807 complain = complain_overflow_bitfield;
14808 else if (howto->rightshift == 0
14809 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
14810 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
14811 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
14812 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
14813 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
14814 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
14815 complain = complain_overflow_unsigned;
14816 if (howto->complain_on_overflow != complain)
14817 {
14818 alt_howto = *howto;
14819 alt_howto.complain_on_overflow = complain;
14820 howto = &alt_howto;
14821 }
14822 }
14823
14824 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
14825 rel->r_offset, relocation, addend);
5bd4f169 14826
ef60b7ff 14827 if (r != bfd_reloc_ok)
5bd4f169 14828 {
bc30df16 14829 char *more_info = NULL;
b80eed39 14830 const char *reloc_name = howto->name;
bc30df16
AM
14831
14832 if (reloc_dest != DEST_NORMAL)
14833 {
14834 more_info = bfd_malloc (strlen (reloc_name) + 8);
14835 if (more_info != NULL)
14836 {
14837 strcpy (more_info, reloc_name);
14838 strcat (more_info, (reloc_dest == DEST_OPD
14839 ? " (OPD)" : " (stub)"));
14840 reloc_name = more_info;
14841 }
14842 }
14843
cd27b276 14844 if (r == bfd_reloc_overflow)
5bd4f169 14845 {
8131c122
AM
14846 /* On code like "if (foo) foo();" don't report overflow
14847 on a branch to zero when foo is undefined. */
14848 if (!warned
14849 && (reloc_dest == DEST_STUB
14850 || !(h != NULL
14851 && (h->elf.root.type == bfd_link_hash_undefweak
14852 || h->elf.root.type == bfd_link_hash_undefined)
14853 && is_branch_reloc (r_type))))
5bd4f169 14854 {
8131c122
AM
14855 if (!((*info->callbacks->reloc_overflow)
14856 (info, &h->elf.root, sym_name,
14857 reloc_name, orig_rel.r_addend,
14858 input_bfd, input_section, rel->r_offset)))
14859 return FALSE;
5bd4f169 14860 }
ef60b7ff
AM
14861 }
14862 else
14863 {
25f53a85 14864 info->callbacks->einfo
bc30df16 14865 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 14866 input_bfd, input_section, rel->r_offset,
bc30df16 14867 reloc_name, sym_name, (int) r);
b34976b6 14868 ret = FALSE;
ef60b7ff 14869 }
bc30df16
AM
14870 if (more_info != NULL)
14871 free (more_info);
5bd4f169 14872 }
5bd4f169
AM
14873 }
14874
645ea6a9
AM
14875 /* If we're emitting relocations, then shortly after this function
14876 returns, reloc offsets and addends for this section will be
14877 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
14878 file rather than the input. Save a copy of the relocs for
14879 opd_entry_value. */
4cc603a5 14880 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
14881 {
14882 bfd_size_type amt;
14883 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14884 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
14885 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14886 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
14887 if (rel == NULL)
14888 return FALSE;
14889 memcpy (rel, relocs, amt);
14890 }
5bd4f169
AM
14891 return ret;
14892}
14893
754021d0
AM
14894/* Adjust the value of any local symbols in opd sections. */
14895
6e0b88f1 14896static int
754021d0
AM
14897ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14898 const char *name ATTRIBUTE_UNUSED,
14899 Elf_Internal_Sym *elfsym,
14900 asection *input_sec,
14901 struct elf_link_hash_entry *h)
14902{
74f0fb50
AM
14903 struct _opd_sec_data *opd;
14904 long adjust;
754021d0
AM
14905 bfd_vma value;
14906
4025353c 14907 if (h != NULL)
6e0b88f1 14908 return 1;
4025353c 14909
74f0fb50
AM
14910 opd = get_opd_info (input_sec);
14911 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 14912 return 1;
754021d0
AM
14913
14914 value = elfsym->st_value - input_sec->output_offset;
14915 if (!info->relocatable)
14916 value -= input_sec->output_section->vma;
14917
51aecdc5 14918 adjust = opd->adjust[OPD_NDX (value)];
4025353c 14919 if (adjust == -1)
6e0b88f1
AM
14920 return 2;
14921
14922 elfsym->st_value += adjust;
14923 return 1;
754021d0
AM
14924}
14925
5bd4f169
AM
14926/* Finish up dynamic symbol handling. We set the contents of various
14927 dynamic sections here. */
14928
b34976b6 14929static bfd_boolean
4ce794b7
AM
14930ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14931 struct bfd_link_info *info,
14932 struct elf_link_hash_entry *h,
ab6dce23 14933 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 14934{
65f38f15 14935 struct ppc_link_hash_table *htab;
8387904d
AM
14936 struct plt_entry *ent;
14937 Elf_Internal_Rela rela;
14938 bfd_byte *loc;
5bd4f169 14939
65f38f15 14940 htab = ppc_hash_table (info);
4dfe6ac6
NC
14941 if (htab == NULL)
14942 return FALSE;
5bd4f169 14943
8387904d
AM
14944 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14945 if (ent->plt.offset != (bfd_vma) -1)
14946 {
14947 /* This symbol has an entry in the procedure linkage
14948 table. Set it up. */
e054468f
AM
14949 if (!htab->elf.dynamic_sections_created
14950 || h->dynindx == -1)
14951 {
14952 BFD_ASSERT (h->type == STT_GNU_IFUNC
14953 && h->def_regular
14954 && (h->root.type == bfd_link_hash_defined
14955 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
14956 rela.r_offset = (htab->elf.iplt->output_section->vma
14957 + htab->elf.iplt->output_offset
25f23106 14958 + ent->plt.offset);
ee67d69a
AM
14959 if (htab->opd_abi)
14960 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14961 else
14962 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
14963 rela.r_addend = (h->root.u.def.value
14964 + h->root.u.def.section->output_offset
14965 + h->root.u.def.section->output_section->vma
14966 + ent->addend);
33e44f2e
AM
14967 loc = (htab->elf.irelplt->contents
14968 + (htab->elf.irelplt->reloc_count++
25f23106 14969 * sizeof (Elf64_External_Rela)));
e054468f
AM
14970 }
14971 else
14972 {
33e44f2e
AM
14973 rela.r_offset = (htab->elf.splt->output_section->vma
14974 + htab->elf.splt->output_offset
25f23106 14975 + ent->plt.offset);
e054468f
AM
14976 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14977 rela.r_addend = ent->addend;
33e44f2e 14978 loc = (htab->elf.srelplt->contents
b9e5796b
AM
14979 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14980 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 14981 }
8387904d 14982 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
14983
14984 if (!htab->opd_abi)
14985 {
14986 if (!h->def_regular)
14987 {
14988 /* Mark the symbol as undefined, rather than as
14989 defined in glink. Leave the value if there were
14990 any relocations where pointer equality matters
14991 (this is a clue for the dynamic linker, to make
14992 function pointer comparisons work between an
14993 application and shared library), otherwise set it
14994 to zero. */
14995 sym->st_shndx = SHN_UNDEF;
14996 if (!h->pointer_equality_needed)
14997 sym->st_value = 0;
14998 else if (!h->ref_regular_nonweak)
14999 {
15000 /* This breaks function pointer comparisons, but
15001 that is better than breaking tests for a NULL
15002 function pointer. */
15003 sym->st_value = 0;
15004 }
15005 }
15006 }
8387904d 15007 }
5bd4f169 15008
f5385ebf 15009 if (h->needs_copy)
5bd4f169 15010 {
65f38f15 15011 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 15012
65f38f15
AM
15013 if (h->dynindx == -1
15014 || (h->root.type != bfd_link_hash_defined
15015 && h->root.type != bfd_link_hash_defweak)
4ce794b7 15016 || htab->relbss == NULL)
65f38f15 15017 abort ();
5bd4f169
AM
15018
15019 rela.r_offset = (h->root.u.def.value
15020 + h->root.u.def.section->output_section->vma
15021 + h->root.u.def.section->output_offset);
15022 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15023 rela.r_addend = 0;
4ce794b7
AM
15024 loc = htab->relbss->contents;
15025 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15026 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15027 }
15028
b34976b6 15029 return TRUE;
5bd4f169
AM
15030}
15031
65f38f15
AM
15032/* Used to decide how to sort relocs in an optimal manner for the
15033 dynamic linker, before writing them out. */
15034
15035static enum elf_reloc_type_class
7e612e98
AM
15036ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15037 const asection *rel_sec,
15038 const Elf_Internal_Rela *rela)
65f38f15 15039{
04c9666a 15040 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15041 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15042
33e44f2e 15043 if (rel_sec == htab->elf.irelplt)
7e612e98 15044 return reloc_class_ifunc;
a33d1f77 15045
4ce794b7 15046 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15047 switch (r_type)
65f38f15
AM
15048 {
15049 case R_PPC64_RELATIVE:
15050 return reloc_class_relative;
15051 case R_PPC64_JMP_SLOT:
15052 return reloc_class_plt;
15053 case R_PPC64_COPY:
15054 return reloc_class_copy;
15055 default:
15056 return reloc_class_normal;
15057 }
15058}
15059
5bd4f169
AM
15060/* Finish up the dynamic sections. */
15061
b34976b6 15062static bfd_boolean
4ce794b7
AM
15063ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15064 struct bfd_link_info *info)
5bd4f169 15065{
65f38f15
AM
15066 struct ppc_link_hash_table *htab;
15067 bfd *dynobj;
5bd4f169 15068 asection *sdyn;
5bd4f169 15069
65f38f15 15070 htab = ppc_hash_table (info);
4dfe6ac6
NC
15071 if (htab == NULL)
15072 return FALSE;
15073
65f38f15 15074 dynobj = htab->elf.dynobj;
3d4d4302 15075 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15076
65f38f15 15077 if (htab->elf.dynamic_sections_created)
5bd4f169 15078 {
5bd4f169
AM
15079 Elf64_External_Dyn *dyncon, *dynconend;
15080
33e44f2e 15081 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15082 abort ();
5bd4f169
AM
15083
15084 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15085 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15086 for (; dyncon < dynconend; dyncon++)
15087 {
15088 Elf_Internal_Dyn dyn;
19397422 15089 asection *s;
5bd4f169
AM
15090
15091 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15092
15093 switch (dyn.d_tag)
15094 {
65f38f15
AM
15095 default:
15096 continue;
5bd4f169 15097
5d1634d7 15098 case DT_PPC64_GLINK:
4ce794b7 15099 s = htab->glink;
6348e046 15100 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15101 /* We stupidly defined DT_PPC64_GLINK to be the start
15102 of glink rather than the first entry point, which is
15103 what ld.so needs, and now have a bigger stub to
15104 support automatic multiple TOCs. */
b9e5796b 15105 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15106 break;
15107
19397422
AM
15108 case DT_PPC64_OPD:
15109 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15110 if (s == NULL)
15111 continue;
15112 dyn.d_un.d_ptr = s->vma;
19397422
AM
15113 break;
15114
e8910a83
AM
15115 case DT_PPC64_OPT:
15116 if (htab->do_multi_toc && htab->multi_toc_needed)
15117 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15118 break;
15119
19397422
AM
15120 case DT_PPC64_OPDSZ:
15121 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15122 if (s == NULL)
15123 continue;
eea6121a 15124 dyn.d_un.d_val = s->size;
19397422
AM
15125 break;
15126
65f38f15 15127 case DT_PLTGOT:
33e44f2e 15128 s = htab->elf.splt;
6348e046 15129 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15130 break;
15131
15132 case DT_JMPREL:
33e44f2e 15133 s = htab->elf.srelplt;
6348e046 15134 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15135 break;
5bd4f169 15136
65f38f15 15137 case DT_PLTRELSZ:
33e44f2e 15138 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7
AM
15139 break;
15140
15141 case DT_RELASZ:
15142 /* Don't count procedure linkage table relocs in the
15143 overall reloc count. */
33e44f2e 15144 s = htab->elf.srelplt;
6348e046
AM
15145 if (s == NULL)
15146 continue;
eea6121a 15147 dyn.d_un.d_val -= s->size;
6348e046
AM
15148 break;
15149
15150 case DT_RELA:
15151 /* We may not be using the standard ELF linker script.
15152 If .rela.plt is the first .rela section, we adjust
15153 DT_RELA to not include it. */
33e44f2e 15154 s = htab->elf.srelplt;
6348e046
AM
15155 if (s == NULL)
15156 continue;
15157 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
15158 continue;
eea6121a 15159 dyn.d_un.d_ptr += s->size;
65f38f15 15160 break;
5bd4f169 15161 }
5bd4f169 15162
65f38f15 15163 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15164 }
5bd4f169
AM
15165 }
15166
33e44f2e 15167 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
5d1634d7
AM
15168 {
15169 /* Fill in the first entry in the global offset table.
15170 We use it to hold the link-time TOCbase. */
15171 bfd_put_64 (output_bfd,
60ee0d4a 15172 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15173 htab->elf.sgot->contents);
5d1634d7
AM
15174
15175 /* Set .got entry size. */
33e44f2e 15176 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15177 }
15178
33e44f2e 15179 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5d1634d7
AM
15180 {
15181 /* Set .plt entry size. */
33e44f2e 15182 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15183 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15184 }
15185
84f5d08e
AM
15186 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15187 brlt ourselves if emitrelocations. */
15188 if (htab->brlt != NULL
15189 && htab->brlt->reloc_count != 0
15190 && !_bfd_elf_link_output_relocs (output_bfd,
15191 htab->brlt,
d4730f92 15192 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15193 elf_section_data (htab->brlt)->relocs,
15194 NULL))
15195 return FALSE;
15196
176a0d42
AM
15197 if (htab->glink != NULL
15198 && htab->glink->reloc_count != 0
15199 && !_bfd_elf_link_output_relocs (output_bfd,
15200 htab->glink,
d4730f92 15201 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15202 elf_section_data (htab->glink)->relocs,
15203 NULL))
15204 return FALSE;
15205
58d180e8 15206 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15207 && htab->glink_eh_frame->size != 0)
15208 {
15209 bfd_vma val;
15210 bfd_byte *p;
15211 asection *stub_sec;
15212
15213 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15214 for (stub_sec = htab->params->stub_bfd->sections;
15215 stub_sec != NULL;
15216 stub_sec = stub_sec->next)
15217 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15218 {
15219 /* FDE length. */
15220 p += 4;
15221 /* CIE pointer. */
15222 p += 4;
15223 /* Offset to stub section. */
15224 val = (stub_sec->output_section->vma
15225 + stub_sec->output_offset);
15226 val -= (htab->glink_eh_frame->output_section->vma
15227 + htab->glink_eh_frame->output_offset
15228 + (p - htab->glink_eh_frame->contents));
15229 if (val + 0x80000000 > 0xffffffff)
15230 {
15231 info->callbacks->einfo
15232 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15233 stub_sec->name);
15234 return FALSE;
15235 }
15236 bfd_put_32 (dynobj, val, p);
15237 p += 4;
15238 /* stub section size. */
15239 p += 4;
15240 /* Augmentation. */
15241 p += 1;
15242 /* Pad. */
15243 p += 7;
15244 }
15245 if (htab->glink != NULL && htab->glink->size != 0)
15246 {
15247 /* FDE length. */
15248 p += 4;
15249 /* CIE pointer. */
15250 p += 4;
15251 /* Offset to .glink. */
15252 val = (htab->glink->output_section->vma
15253 + htab->glink->output_offset
15254 + 8);
15255 val -= (htab->glink_eh_frame->output_section->vma
15256 + htab->glink_eh_frame->output_offset
15257 + (p - htab->glink_eh_frame->contents));
15258 if (val + 0x80000000 > 0xffffffff)
15259 {
15260 info->callbacks->einfo
15261 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15262 htab->glink->name);
15263 return FALSE;
15264 }
15265 bfd_put_32 (dynobj, val, p);
15266 p += 4;
15267 /* .glink size. */
15268 p += 4;
15269 /* Augmentation. */
15270 p += 1;
15271 /* Ops. */
15272 p += 7;
15273 }
15274
15275 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15276 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15277 htab->glink_eh_frame,
15278 htab->glink_eh_frame->contents))
15279 return FALSE;
15280 }
58d180e8 15281
e717da7e 15282 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15283 since we didn't add them to DYNOBJ. We know dynobj is the first
15284 bfd. */
c72f2fb2 15285 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15286 {
15287 asection *s;
7b53ace3 15288
0c8d6e5c 15289 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15290 continue;
15291
e717da7e
AM
15292 s = ppc64_elf_tdata (dynobj)->got;
15293 if (s != NULL
eea6121a 15294 && s->size != 0
e717da7e
AM
15295 && s->output_section != bfd_abs_section_ptr
15296 && !bfd_set_section_contents (output_bfd, s->output_section,
15297 s->contents, s->output_offset,
eea6121a 15298 s->size))
e717da7e
AM
15299 return FALSE;
15300 s = ppc64_elf_tdata (dynobj)->relgot;
15301 if (s != NULL
eea6121a 15302 && s->size != 0
e717da7e
AM
15303 && s->output_section != bfd_abs_section_ptr
15304 && !bfd_set_section_contents (output_bfd, s->output_section,
15305 s->contents, s->output_offset,
eea6121a 15306 s->size))
e717da7e
AM
15307 return FALSE;
15308 }
f6c52c13 15309
b34976b6 15310 return TRUE;
5bd4f169
AM
15311}
15312
5bd4f169 15313#include "elf64-target.h"
7b8e7dad
AM
15314
15315/* FreeBSD support */
15316
15317#undef TARGET_LITTLE_SYM
15318#undef TARGET_LITTLE_NAME
15319
15320#undef TARGET_BIG_SYM
6d00b590 15321#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15322#undef TARGET_BIG_NAME
15323#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15324
15325#undef ELF_OSABI
15326#define ELF_OSABI ELFOSABI_FREEBSD
15327
15328#undef elf64_bed
15329#define elf64_bed elf64_powerpc_fbsd_bed
15330
15331#include "elf64-target.h"
15332