gdb.cp/derivation.exp: s/perrro/perror/
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
3a71aa26 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
794e51c0 3 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5bd4f169
AM
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 6 Largely rewritten by Alan Modra.
5bd4f169 7
ae9a127f 8 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 9
ae9a127f
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
ae9a127f 13 (at your option) any later version.
5bd4f169 14
ae9a127f
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
5bd4f169 19
4ce794b7
AM
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 23
cd123cb7 24
4ce794b7
AM
25/* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 28
3db64b00 29#include "sysdep.h"
183e98be 30#include <stdarg.h>
5bd4f169 31#include "bfd.h"
5bd4f169
AM
32#include "bfdlink.h"
33#include "libbfd.h"
34#include "elf-bfd.h"
04c9666a 35#include "elf/ppc64.h"
5d1634d7 36#include "elf64-ppc.h"
58d180e8 37#include "dwarf2.h"
5bd4f169 38
805fc799 39static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
41static bfd_reloc_status_type ppc64_elf_branch_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 43static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 45static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 47static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 49static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 51static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 53static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 55static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 57static bfd_vma opd_entry_value
aef36ac1 58 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 59
ad8e1ba5
AM
60#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
61#define TARGET_LITTLE_NAME "elf64-powerpcle"
62#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
63#define TARGET_BIG_NAME "elf64-powerpc"
64#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 65#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
66#define ELF_MACHINE_CODE EM_PPC64
67#define ELF_MAXPAGESIZE 0x10000
24718e3b 68#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
69#define elf_info_to_howto ppc64_elf_info_to_howto
70
71#define elf_backend_want_got_sym 0
72#define elf_backend_want_plt_sym 0
73#define elf_backend_plt_alignment 3
74#define elf_backend_plt_not_loaded 1
ad8e1ba5 75#define elf_backend_got_header_size 8
ad8e1ba5
AM
76#define elf_backend_can_gc_sections 1
77#define elf_backend_can_refcount 1
78#define elf_backend_rela_normal 1
6bfdb61b 79#define elf_backend_default_execstack 0
ad8e1ba5 80
e717da7e 81#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 82#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 83#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
84#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
85#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
86#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
87#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
88#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 89#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 90#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
91
92#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
93#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
94#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 95#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
96#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
97#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 98#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 99#define elf_backend_check_directives ppc64_elf_process_dot_syms
e5034e59 100#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 101#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 102#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 103#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 104#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
105#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
106#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
107#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
108#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 109#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
110#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
111#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 112#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 113#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
114#define elf_backend_relocate_section ppc64_elf_relocate_section
115#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
116#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
117#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 118#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 119#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 120#define elf_backend_post_process_headers _bfd_elf_set_osabi
6911b7dc 121#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
ad8e1ba5 122
5bd4f169
AM
123/* The name of the dynamic interpreter. This is put in the .interp
124 section. */
125#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
126
127/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 128#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
129
130/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 131#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 132
a078d95a
AM
133/* Offsets to some stack save slots. */
134#define STK_LR 16
135#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
136/* This one is dodgy. ABIv2 does not have a linker word, so use the
137 CR save slot. Used only by optimised __tls_get_addr call stub,
138 relying on __tls_get_addr_opt not saving CR.. */
139#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
140
5bd4f169 141/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
142#define TOC_BASE_OFF 0x8000
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
ee4bf8d2 177/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 178#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
179 /* 0: */
180 /* .quad plt0-1f */
181 /* __glink: */
182#define MFLR_R12 0x7d8802a6 /* mflr %12 */
183#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
184 /* 1: */
185#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 186 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 187#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
188#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
189 /* ld %12,0(%11) */
190 /* ld %2,8(%11) */
191 /* mtctr %12 */
192 /* ld %11,16(%11) */
ee4bf8d2 193 /* bctr */
b9e5796b
AM
194#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
195#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
196#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
197#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
198#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
199
200/* Pad with this. */
201#define NOP 0x60000000
202
721956f4
AM
203/* Some other nops. */
204#define CROR_151515 0x4def7b82
205#define CROR_313131 0x4ffffb82
206
cedb70c5 207/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
208#define LI_R0_0 0x38000000 /* li %r0,0 */
209#define B_DOT 0x48000000 /* b . */
210
211/* After that, we need two instructions to load the index, followed by
212 a branch. */
213#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 214#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 215
deb0e272
AM
216/* Instructions used by the save and restore reg functions. */
217#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
218#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
219#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
220#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
221#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
222#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
223#define LI_R12_0 0x39800000 /* li %r12,0 */
224#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
225#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
226#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
227#define BLR 0x4e800020 /* blr */
228
41bd81ab
AM
229/* Since .opd is an array of descriptors and each entry will end up
230 with identical R_PPC64_RELATIVE relocs, there is really no need to
231 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 232 relocate .opd without reloc entries. */
41bd81ab
AM
233#ifndef NO_OPD_RELOCS
234#define NO_OPD_RELOCS 0
235#endif
5bd4f169 236\f
f5e87a1d 237#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 238
5bd4f169 239/* Relocation HOWTO's. */
04c9666a 240static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
241
242static reloc_howto_type ppc64_elf_howto_raw[] = {
243 /* This reloc does nothing. */
244 HOWTO (R_PPC64_NONE, /* type */
245 0, /* rightshift */
411e1bfb
AM
246 2, /* size (0 = byte, 1 = short, 2 = long) */
247 32, /* bitsize */
b34976b6 248 FALSE, /* pc_relative */
5bd4f169 249 0, /* bitpos */
f5e87a1d 250 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
251 bfd_elf_generic_reloc, /* special_function */
252 "R_PPC64_NONE", /* name */
b34976b6 253 FALSE, /* partial_inplace */
d006db6c 254 0, /* src_mask */
5bd4f169 255 0, /* dst_mask */
b34976b6 256 FALSE), /* pcrel_offset */
5bd4f169
AM
257
258 /* A standard 32 bit relocation. */
259 HOWTO (R_PPC64_ADDR32, /* type */
260 0, /* rightshift */
261 2, /* size (0 = byte, 1 = short, 2 = long) */
262 32, /* bitsize */
b34976b6 263 FALSE, /* pc_relative */
5bd4f169
AM
264 0, /* bitpos */
265 complain_overflow_bitfield, /* complain_on_overflow */
266 bfd_elf_generic_reloc, /* special_function */
267 "R_PPC64_ADDR32", /* name */
b34976b6 268 FALSE, /* partial_inplace */
5bd4f169
AM
269 0, /* src_mask */
270 0xffffffff, /* dst_mask */
b34976b6 271 FALSE), /* pcrel_offset */
5bd4f169
AM
272
273 /* An absolute 26 bit branch; the lower two bits must be zero.
274 FIXME: we don't check that, we just clear them. */
275 HOWTO (R_PPC64_ADDR24, /* type */
276 0, /* rightshift */
277 2, /* size (0 = byte, 1 = short, 2 = long) */
278 26, /* bitsize */
b34976b6 279 FALSE, /* pc_relative */
5bd4f169
AM
280 0, /* bitpos */
281 complain_overflow_bitfield, /* complain_on_overflow */
282 bfd_elf_generic_reloc, /* special_function */
283 "R_PPC64_ADDR24", /* name */
b34976b6 284 FALSE, /* partial_inplace */
d006db6c 285 0, /* src_mask */
f5e87a1d 286 0x03fffffc, /* dst_mask */
b34976b6 287 FALSE), /* pcrel_offset */
5bd4f169
AM
288
289 /* A standard 16 bit relocation. */
290 HOWTO (R_PPC64_ADDR16, /* type */
291 0, /* rightshift */
292 1, /* size (0 = byte, 1 = short, 2 = long) */
293 16, /* bitsize */
b34976b6 294 FALSE, /* pc_relative */
5bd4f169
AM
295 0, /* bitpos */
296 complain_overflow_bitfield, /* complain_on_overflow */
297 bfd_elf_generic_reloc, /* special_function */
298 "R_PPC64_ADDR16", /* name */
b34976b6 299 FALSE, /* partial_inplace */
5bd4f169
AM
300 0, /* src_mask */
301 0xffff, /* dst_mask */
b34976b6 302 FALSE), /* pcrel_offset */
5bd4f169
AM
303
304 /* A 16 bit relocation without overflow. */
305 HOWTO (R_PPC64_ADDR16_LO, /* type */
306 0, /* rightshift */
307 1, /* size (0 = byte, 1 = short, 2 = long) */
308 16, /* bitsize */
b34976b6 309 FALSE, /* pc_relative */
5bd4f169
AM
310 0, /* bitpos */
311 complain_overflow_dont,/* complain_on_overflow */
312 bfd_elf_generic_reloc, /* special_function */
313 "R_PPC64_ADDR16_LO", /* name */
b34976b6 314 FALSE, /* partial_inplace */
5bd4f169
AM
315 0, /* src_mask */
316 0xffff, /* dst_mask */
b34976b6 317 FALSE), /* pcrel_offset */
5bd4f169
AM
318
319 /* Bits 16-31 of an address. */
320 HOWTO (R_PPC64_ADDR16_HI, /* type */
321 16, /* rightshift */
322 1, /* size (0 = byte, 1 = short, 2 = long) */
323 16, /* bitsize */
b34976b6 324 FALSE, /* pc_relative */
5bd4f169 325 0, /* bitpos */
f9c6b907 326 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
327 bfd_elf_generic_reloc, /* special_function */
328 "R_PPC64_ADDR16_HI", /* name */
b34976b6 329 FALSE, /* partial_inplace */
5bd4f169
AM
330 0, /* src_mask */
331 0xffff, /* dst_mask */
b34976b6 332 FALSE), /* pcrel_offset */
5bd4f169
AM
333
334 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
335 bits, treated as a signed number, is negative. */
336 HOWTO (R_PPC64_ADDR16_HA, /* type */
337 16, /* rightshift */
338 1, /* size (0 = byte, 1 = short, 2 = long) */
339 16, /* bitsize */
b34976b6 340 FALSE, /* pc_relative */
5bd4f169 341 0, /* bitpos */
f9c6b907 342 complain_overflow_signed, /* complain_on_overflow */
805fc799 343 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 344 "R_PPC64_ADDR16_HA", /* name */
b34976b6 345 FALSE, /* partial_inplace */
5bd4f169
AM
346 0, /* src_mask */
347 0xffff, /* dst_mask */
b34976b6 348 FALSE), /* pcrel_offset */
5bd4f169
AM
349
350 /* An absolute 16 bit branch; the lower two bits must be zero.
351 FIXME: we don't check that, we just clear them. */
352 HOWTO (R_PPC64_ADDR14, /* type */
353 0, /* rightshift */
354 2, /* size (0 = byte, 1 = short, 2 = long) */
355 16, /* bitsize */
b34976b6 356 FALSE, /* pc_relative */
5bd4f169
AM
357 0, /* bitpos */
358 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 359 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 360 "R_PPC64_ADDR14", /* name */
b34976b6 361 FALSE, /* partial_inplace */
d006db6c 362 0, /* src_mask */
f5e87a1d 363 0x0000fffc, /* dst_mask */
b34976b6 364 FALSE), /* pcrel_offset */
5bd4f169
AM
365
366 /* An absolute 16 bit branch, for which bit 10 should be set to
367 indicate that the branch is expected to be taken. The lower two
368 bits must be zero. */
369 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
370 0, /* rightshift */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
372 16, /* bitsize */
b34976b6 373 FALSE, /* pc_relative */
5bd4f169
AM
374 0, /* bitpos */
375 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 376 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 377 "R_PPC64_ADDR14_BRTAKEN",/* 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 not expected to be taken. The lower
385 two bits must be zero. */
386 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 16, /* bitsize */
b34976b6 390 FALSE, /* pc_relative */
5bd4f169
AM
391 0, /* bitpos */
392 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 393 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 394 "R_PPC64_ADDR14_BRNTAKEN",/* 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 /* A relative 26 bit branch; the lower two bits must be zero. */
401 HOWTO (R_PPC64_REL24, /* type */
402 0, /* rightshift */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
404 26, /* bitsize */
b34976b6 405 TRUE, /* pc_relative */
5bd4f169
AM
406 0, /* bitpos */
407 complain_overflow_signed, /* complain_on_overflow */
2441e016 408 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 409 "R_PPC64_REL24", /* name */
b34976b6 410 FALSE, /* partial_inplace */
d006db6c 411 0, /* src_mask */
f5e87a1d 412 0x03fffffc, /* dst_mask */
b34976b6 413 TRUE), /* pcrel_offset */
5bd4f169
AM
414
415 /* A relative 16 bit branch; the lower two bits must be zero. */
416 HOWTO (R_PPC64_REL14, /* type */
417 0, /* rightshift */
418 2, /* size (0 = byte, 1 = short, 2 = long) */
419 16, /* bitsize */
b34976b6 420 TRUE, /* pc_relative */
5bd4f169
AM
421 0, /* bitpos */
422 complain_overflow_signed, /* complain_on_overflow */
2441e016 423 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 424 "R_PPC64_REL14", /* name */
b34976b6 425 FALSE, /* partial_inplace */
d006db6c 426 0, /* src_mask */
f5e87a1d 427 0x0000fffc, /* dst_mask */
b34976b6 428 TRUE), /* pcrel_offset */
5bd4f169
AM
429
430 /* A relative 16 bit branch. Bit 10 should be set to indicate that
431 the branch is expected to be taken. The lower two bits must be
432 zero. */
433 HOWTO (R_PPC64_REL14_BRTAKEN, /* 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 */
805fc799 440 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 441 "R_PPC64_REL14_BRTAKEN", /* 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 not expected to be taken. The lower two bits must
449 be zero. */
450 HOWTO (R_PPC64_REL14_BRNTAKEN, /* 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_BRNTAKEN",/* 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 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
465 symbol. */
466 HOWTO (R_PPC64_GOT16, /* type */
467 0, /* rightshift */
468 1, /* size (0 = byte, 1 = short, 2 = long) */
469 16, /* bitsize */
b34976b6 470 FALSE, /* pc_relative */
5bd4f169
AM
471 0, /* bitpos */
472 complain_overflow_signed, /* complain_on_overflow */
805fc799 473 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 474 "R_PPC64_GOT16", /* name */
b34976b6 475 FALSE, /* partial_inplace */
5bd4f169
AM
476 0, /* src_mask */
477 0xffff, /* dst_mask */
b34976b6 478 FALSE), /* pcrel_offset */
5bd4f169
AM
479
480 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
481 the symbol. */
482 HOWTO (R_PPC64_GOT16_LO, /* type */
483 0, /* rightshift */
484 1, /* size (0 = byte, 1 = short, 2 = long) */
485 16, /* bitsize */
b34976b6 486 FALSE, /* pc_relative */
5bd4f169
AM
487 0, /* bitpos */
488 complain_overflow_dont, /* complain_on_overflow */
805fc799 489 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 490 "R_PPC64_GOT16_LO", /* name */
b34976b6 491 FALSE, /* partial_inplace */
5bd4f169
AM
492 0, /* src_mask */
493 0xffff, /* dst_mask */
b34976b6 494 FALSE), /* pcrel_offset */
5bd4f169
AM
495
496 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
497 the symbol. */
498 HOWTO (R_PPC64_GOT16_HI, /* type */
499 16, /* rightshift */
500 1, /* size (0 = byte, 1 = short, 2 = long) */
501 16, /* bitsize */
b34976b6 502 FALSE, /* pc_relative */
5bd4f169 503 0, /* bitpos */
f9c6b907 504 complain_overflow_signed,/* complain_on_overflow */
805fc799 505 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 506 "R_PPC64_GOT16_HI", /* name */
b34976b6 507 FALSE, /* partial_inplace */
5bd4f169
AM
508 0, /* src_mask */
509 0xffff, /* dst_mask */
b34976b6 510 FALSE), /* pcrel_offset */
5bd4f169
AM
511
512 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
513 the symbol. */
514 HOWTO (R_PPC64_GOT16_HA, /* type */
515 16, /* rightshift */
516 1, /* size (0 = byte, 1 = short, 2 = long) */
517 16, /* bitsize */
b34976b6 518 FALSE, /* pc_relative */
5bd4f169 519 0, /* bitpos */
f9c6b907 520 complain_overflow_signed,/* complain_on_overflow */
805fc799 521 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 522 "R_PPC64_GOT16_HA", /* name */
b34976b6 523 FALSE, /* partial_inplace */
5bd4f169
AM
524 0, /* src_mask */
525 0xffff, /* dst_mask */
b34976b6 526 FALSE), /* pcrel_offset */
5bd4f169
AM
527
528 /* This is used only by the dynamic linker. The symbol should exist
529 both in the object being run and in some shared library. The
530 dynamic linker copies the data addressed by the symbol from the
531 shared library into the object, because the object being
532 run has to have the data at some particular address. */
533 HOWTO (R_PPC64_COPY, /* type */
534 0, /* rightshift */
f5e87a1d
AM
535 0, /* this one is variable size */
536 0, /* bitsize */
b34976b6 537 FALSE, /* pc_relative */
5bd4f169 538 0, /* bitpos */
f5e87a1d
AM
539 complain_overflow_dont, /* complain_on_overflow */
540 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 541 "R_PPC64_COPY", /* name */
b34976b6 542 FALSE, /* partial_inplace */
5bd4f169
AM
543 0, /* src_mask */
544 0, /* dst_mask */
b34976b6 545 FALSE), /* pcrel_offset */
5bd4f169
AM
546
547 /* Like R_PPC64_ADDR64, but used when setting global offset table
548 entries. */
549 HOWTO (R_PPC64_GLOB_DAT, /* type */
550 0, /* rightshift */
551 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
552 64, /* bitsize */
b34976b6 553 FALSE, /* pc_relative */
5bd4f169
AM
554 0, /* bitpos */
555 complain_overflow_dont, /* complain_on_overflow */
805fc799 556 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 557 "R_PPC64_GLOB_DAT", /* name */
b34976b6 558 FALSE, /* partial_inplace */
5bd4f169 559 0, /* src_mask */
f5e87a1d 560 ONES (64), /* dst_mask */
b34976b6 561 FALSE), /* pcrel_offset */
5bd4f169
AM
562
563 /* Created by the link editor. Marks a procedure linkage table
564 entry for a symbol. */
565 HOWTO (R_PPC64_JMP_SLOT, /* type */
566 0, /* rightshift */
567 0, /* size (0 = byte, 1 = short, 2 = long) */
568 0, /* bitsize */
b34976b6 569 FALSE, /* pc_relative */
5bd4f169
AM
570 0, /* bitpos */
571 complain_overflow_dont, /* complain_on_overflow */
805fc799 572 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 573 "R_PPC64_JMP_SLOT", /* name */
b34976b6 574 FALSE, /* partial_inplace */
5bd4f169
AM
575 0, /* src_mask */
576 0, /* dst_mask */
b34976b6 577 FALSE), /* pcrel_offset */
5bd4f169
AM
578
579 /* Used only by the dynamic linker. When the object is run, this
580 doubleword64 is set to the load address of the object, plus the
581 addend. */
582 HOWTO (R_PPC64_RELATIVE, /* type */
583 0, /* rightshift */
584 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
585 64, /* bitsize */
b34976b6 586 FALSE, /* pc_relative */
5bd4f169
AM
587 0, /* bitpos */
588 complain_overflow_dont, /* complain_on_overflow */
589 bfd_elf_generic_reloc, /* special_function */
590 "R_PPC64_RELATIVE", /* name */
b34976b6 591 FALSE, /* partial_inplace */
5bd4f169 592 0, /* src_mask */
f5e87a1d 593 ONES (64), /* dst_mask */
b34976b6 594 FALSE), /* pcrel_offset */
5bd4f169
AM
595
596 /* Like R_PPC64_ADDR32, but may be unaligned. */
597 HOWTO (R_PPC64_UADDR32, /* type */
598 0, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 32, /* bitsize */
b34976b6 601 FALSE, /* pc_relative */
5bd4f169
AM
602 0, /* bitpos */
603 complain_overflow_bitfield, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_PPC64_UADDR32", /* name */
b34976b6 606 FALSE, /* partial_inplace */
5bd4f169
AM
607 0, /* src_mask */
608 0xffffffff, /* dst_mask */
b34976b6 609 FALSE), /* pcrel_offset */
5bd4f169
AM
610
611 /* Like R_PPC64_ADDR16, but may be unaligned. */
612 HOWTO (R_PPC64_UADDR16, /* type */
613 0, /* rightshift */
614 1, /* size (0 = byte, 1 = short, 2 = long) */
615 16, /* bitsize */
b34976b6 616 FALSE, /* pc_relative */
5bd4f169
AM
617 0, /* bitpos */
618 complain_overflow_bitfield, /* complain_on_overflow */
619 bfd_elf_generic_reloc, /* special_function */
620 "R_PPC64_UADDR16", /* name */
b34976b6 621 FALSE, /* partial_inplace */
5bd4f169
AM
622 0, /* src_mask */
623 0xffff, /* dst_mask */
b34976b6 624 FALSE), /* pcrel_offset */
5bd4f169
AM
625
626 /* 32-bit PC relative. */
627 HOWTO (R_PPC64_REL32, /* type */
628 0, /* rightshift */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
630 32, /* bitsize */
b34976b6 631 TRUE, /* pc_relative */
5bd4f169 632 0, /* bitpos */
cedb70c5 633 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
634 complain_overflow_signed, /* complain_on_overflow */
635 bfd_elf_generic_reloc, /* special_function */
636 "R_PPC64_REL32", /* name */
b34976b6 637 FALSE, /* partial_inplace */
5bd4f169
AM
638 0, /* src_mask */
639 0xffffffff, /* dst_mask */
b34976b6 640 TRUE), /* pcrel_offset */
5bd4f169 641
10ed1bba 642 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
643 HOWTO (R_PPC64_PLT32, /* type */
644 0, /* rightshift */
645 2, /* size (0 = byte, 1 = short, 2 = long) */
646 32, /* bitsize */
b34976b6 647 FALSE, /* pc_relative */
5bd4f169
AM
648 0, /* bitpos */
649 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 650 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 651 "R_PPC64_PLT32", /* name */
b34976b6 652 FALSE, /* partial_inplace */
5bd4f169 653 0, /* src_mask */
f5e87a1d 654 0xffffffff, /* dst_mask */
b34976b6 655 FALSE), /* pcrel_offset */
5bd4f169
AM
656
657 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
658 FIXME: R_PPC64_PLTREL32 not supported. */
659 HOWTO (R_PPC64_PLTREL32, /* type */
660 0, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 32, /* bitsize */
b34976b6 663 TRUE, /* pc_relative */
5bd4f169
AM
664 0, /* bitpos */
665 complain_overflow_signed, /* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_PPC64_PLTREL32", /* name */
b34976b6 668 FALSE, /* partial_inplace */
5bd4f169 669 0, /* src_mask */
f5e87a1d 670 0xffffffff, /* dst_mask */
b34976b6 671 TRUE), /* pcrel_offset */
5bd4f169
AM
672
673 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
674 the symbol. */
675 HOWTO (R_PPC64_PLT16_LO, /* type */
676 0, /* rightshift */
677 1, /* size (0 = byte, 1 = short, 2 = long) */
678 16, /* bitsize */
b34976b6 679 FALSE, /* pc_relative */
5bd4f169
AM
680 0, /* bitpos */
681 complain_overflow_dont, /* complain_on_overflow */
805fc799 682 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 683 "R_PPC64_PLT16_LO", /* name */
b34976b6 684 FALSE, /* partial_inplace */
5bd4f169
AM
685 0, /* src_mask */
686 0xffff, /* dst_mask */
b34976b6 687 FALSE), /* pcrel_offset */
5bd4f169
AM
688
689 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
690 the symbol. */
691 HOWTO (R_PPC64_PLT16_HI, /* type */
692 16, /* rightshift */
693 1, /* size (0 = byte, 1 = short, 2 = long) */
694 16, /* bitsize */
b34976b6 695 FALSE, /* pc_relative */
5bd4f169 696 0, /* bitpos */
f9c6b907 697 complain_overflow_signed, /* complain_on_overflow */
805fc799 698 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 699 "R_PPC64_PLT16_HI", /* 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_HA, but referring to the PLT table entry for
706 the symbol. */
707 HOWTO (R_PPC64_PLT16_HA, /* 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_HA", /* name */
b34976b6 716 FALSE, /* partial_inplace */
5bd4f169
AM
717 0, /* src_mask */
718 0xffff, /* dst_mask */
b34976b6 719 FALSE), /* pcrel_offset */
5bd4f169 720
c061c2d8 721 /* 16-bit section relative relocation. */
5bd4f169
AM
722 HOWTO (R_PPC64_SECTOFF, /* type */
723 0, /* rightshift */
c061c2d8
AM
724 1, /* size (0 = byte, 1 = short, 2 = long) */
725 16, /* bitsize */
b34976b6 726 FALSE, /* pc_relative */
5bd4f169
AM
727 0, /* bitpos */
728 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 729 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 730 "R_PPC64_SECTOFF", /* name */
b34976b6 731 FALSE, /* partial_inplace */
5bd4f169 732 0, /* src_mask */
c061c2d8 733 0xffff, /* dst_mask */
b34976b6 734 FALSE), /* pcrel_offset */
5bd4f169 735
c061c2d8 736 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
737 HOWTO (R_PPC64_SECTOFF_LO, /* type */
738 0, /* rightshift */
739 1, /* size (0 = byte, 1 = short, 2 = long) */
740 16, /* bitsize */
b34976b6 741 FALSE, /* pc_relative */
5bd4f169
AM
742 0, /* bitpos */
743 complain_overflow_dont, /* complain_on_overflow */
805fc799 744 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 745 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 746 FALSE, /* partial_inplace */
5bd4f169
AM
747 0, /* src_mask */
748 0xffff, /* dst_mask */
b34976b6 749 FALSE), /* pcrel_offset */
5bd4f169
AM
750
751 /* 16-bit upper half section relative relocation. */
752 HOWTO (R_PPC64_SECTOFF_HI, /* type */
753 16, /* rightshift */
754 1, /* size (0 = byte, 1 = short, 2 = long) */
755 16, /* bitsize */
b34976b6 756 FALSE, /* pc_relative */
5bd4f169 757 0, /* bitpos */
f9c6b907 758 complain_overflow_signed, /* complain_on_overflow */
805fc799 759 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 760 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 761 FALSE, /* partial_inplace */
5bd4f169
AM
762 0, /* src_mask */
763 0xffff, /* dst_mask */
b34976b6 764 FALSE), /* pcrel_offset */
5bd4f169
AM
765
766 /* 16-bit upper half adjusted section relative relocation. */
767 HOWTO (R_PPC64_SECTOFF_HA, /* type */
768 16, /* rightshift */
769 1, /* size (0 = byte, 1 = short, 2 = long) */
770 16, /* bitsize */
b34976b6 771 FALSE, /* pc_relative */
5bd4f169 772 0, /* bitpos */
f9c6b907 773 complain_overflow_signed, /* complain_on_overflow */
805fc799 774 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 775 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 776 FALSE, /* partial_inplace */
5bd4f169
AM
777 0, /* src_mask */
778 0xffff, /* dst_mask */
b34976b6 779 FALSE), /* pcrel_offset */
5bd4f169 780
04c9666a
AM
781 /* Like R_PPC64_REL24 without touching the two least significant bits. */
782 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
783 2, /* rightshift */
784 2, /* size (0 = byte, 1 = short, 2 = long) */
785 30, /* bitsize */
b34976b6 786 TRUE, /* pc_relative */
5bd4f169
AM
787 0, /* bitpos */
788 complain_overflow_dont, /* complain_on_overflow */
789 bfd_elf_generic_reloc, /* special_function */
04c9666a 790 "R_PPC64_REL30", /* name */
b34976b6 791 FALSE, /* partial_inplace */
d006db6c 792 0, /* src_mask */
5bd4f169 793 0xfffffffc, /* dst_mask */
b34976b6 794 TRUE), /* pcrel_offset */
5bd4f169
AM
795
796 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
797
798 /* A standard 64-bit relocation. */
799 HOWTO (R_PPC64_ADDR64, /* type */
800 0, /* rightshift */
801 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
802 64, /* bitsize */
b34976b6 803 FALSE, /* pc_relative */
5bd4f169
AM
804 0, /* bitpos */
805 complain_overflow_dont, /* complain_on_overflow */
806 bfd_elf_generic_reloc, /* special_function */
807 "R_PPC64_ADDR64", /* name */
b34976b6 808 FALSE, /* partial_inplace */
5bd4f169 809 0, /* src_mask */
f5e87a1d 810 ONES (64), /* dst_mask */
b34976b6 811 FALSE), /* pcrel_offset */
5bd4f169
AM
812
813 /* The bits 32-47 of an address. */
814 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
815 32, /* rightshift */
816 1, /* size (0 = byte, 1 = short, 2 = long) */
817 16, /* bitsize */
b34976b6 818 FALSE, /* pc_relative */
5bd4f169
AM
819 0, /* bitpos */
820 complain_overflow_dont, /* complain_on_overflow */
821 bfd_elf_generic_reloc, /* special_function */
822 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 823 FALSE, /* partial_inplace */
5bd4f169
AM
824 0, /* src_mask */
825 0xffff, /* dst_mask */
b34976b6 826 FALSE), /* pcrel_offset */
5bd4f169
AM
827
828 /* The bits 32-47 of an address, plus 1 if the contents of the low
829 16 bits, treated as a signed number, is negative. */
830 HOWTO (R_PPC64_ADDR16_HIGHERA, /* 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 */
805fc799 837 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 838 "R_PPC64_ADDR16_HIGHERA", /* 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 48-63 of an address. */
845 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
846 48, /* rightshift */
847 1, /* size (0 = byte, 1 = short, 2 = long) */
848 16, /* bitsize */
b34976b6 849 FALSE, /* pc_relative */
5bd4f169
AM
850 0, /* bitpos */
851 complain_overflow_dont, /* complain_on_overflow */
852 bfd_elf_generic_reloc, /* special_function */
853 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 854 FALSE, /* partial_inplace */
5bd4f169
AM
855 0, /* src_mask */
856 0xffff, /* dst_mask */
b34976b6 857 FALSE), /* pcrel_offset */
5bd4f169
AM
858
859 /* The bits 48-63 of an address, plus 1 if the contents of the low
860 16 bits, treated as a signed number, is negative. */
861 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* 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 */
805fc799 868 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 869 "R_PPC64_ADDR16_HIGHESTA", /* 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 /* Like ADDR64, but may be unaligned. */
876 HOWTO (R_PPC64_UADDR64, /* type */
877 0, /* rightshift */
878 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
879 64, /* bitsize */
b34976b6 880 FALSE, /* pc_relative */
5bd4f169
AM
881 0, /* bitpos */
882 complain_overflow_dont, /* complain_on_overflow */
883 bfd_elf_generic_reloc, /* special_function */
884 "R_PPC64_UADDR64", /* name */
b34976b6 885 FALSE, /* partial_inplace */
5bd4f169 886 0, /* src_mask */
f5e87a1d 887 ONES (64), /* dst_mask */
b34976b6 888 FALSE), /* pcrel_offset */
5bd4f169
AM
889
890 /* 64-bit relative relocation. */
891 HOWTO (R_PPC64_REL64, /* type */
892 0, /* rightshift */
893 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
894 64, /* bitsize */
b34976b6 895 TRUE, /* pc_relative */
5bd4f169
AM
896 0, /* bitpos */
897 complain_overflow_dont, /* complain_on_overflow */
898 bfd_elf_generic_reloc, /* special_function */
899 "R_PPC64_REL64", /* name */
b34976b6 900 FALSE, /* partial_inplace */
5bd4f169 901 0, /* src_mask */
f5e87a1d 902 ONES (64), /* dst_mask */
b34976b6 903 TRUE), /* pcrel_offset */
5bd4f169 904
cedb70c5 905 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
906 HOWTO (R_PPC64_PLT64, /* type */
907 0, /* rightshift */
908 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
909 64, /* bitsize */
b34976b6 910 FALSE, /* pc_relative */
5bd4f169
AM
911 0, /* bitpos */
912 complain_overflow_dont, /* complain_on_overflow */
805fc799 913 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 914 "R_PPC64_PLT64", /* name */
b34976b6 915 FALSE, /* partial_inplace */
5bd4f169 916 0, /* src_mask */
f5e87a1d 917 ONES (64), /* dst_mask */
b34976b6 918 FALSE), /* pcrel_offset */
5bd4f169
AM
919
920 /* 64-bit PC relative relocation to the symbol's procedure linkage
921 table. */
922 /* FIXME: R_PPC64_PLTREL64 not supported. */
923 HOWTO (R_PPC64_PLTREL64, /* type */
924 0, /* rightshift */
925 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
926 64, /* bitsize */
b34976b6 927 TRUE, /* pc_relative */
5bd4f169
AM
928 0, /* bitpos */
929 complain_overflow_dont, /* complain_on_overflow */
805fc799 930 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 931 "R_PPC64_PLTREL64", /* name */
b34976b6 932 FALSE, /* partial_inplace */
5bd4f169 933 0, /* src_mask */
f5e87a1d 934 ONES (64), /* dst_mask */
b34976b6 935 TRUE), /* pcrel_offset */
5bd4f169
AM
936
937 /* 16 bit TOC-relative relocation. */
938
939 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
940 HOWTO (R_PPC64_TOC16, /* type */
941 0, /* rightshift */
942 1, /* size (0 = byte, 1 = short, 2 = long) */
943 16, /* bitsize */
b34976b6 944 FALSE, /* pc_relative */
5bd4f169
AM
945 0, /* bitpos */
946 complain_overflow_signed, /* complain_on_overflow */
805fc799 947 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 948 "R_PPC64_TOC16", /* name */
b34976b6 949 FALSE, /* partial_inplace */
5bd4f169
AM
950 0, /* src_mask */
951 0xffff, /* dst_mask */
b34976b6 952 FALSE), /* pcrel_offset */
5bd4f169
AM
953
954 /* 16 bit TOC-relative relocation without overflow. */
955
956 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
957 HOWTO (R_PPC64_TOC16_LO, /* type */
958 0, /* rightshift */
959 1, /* size (0 = byte, 1 = short, 2 = long) */
960 16, /* bitsize */
b34976b6 961 FALSE, /* pc_relative */
5bd4f169
AM
962 0, /* bitpos */
963 complain_overflow_dont, /* complain_on_overflow */
805fc799 964 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 965 "R_PPC64_TOC16_LO", /* name */
b34976b6 966 FALSE, /* partial_inplace */
5bd4f169
AM
967 0, /* src_mask */
968 0xffff, /* dst_mask */
b34976b6 969 FALSE), /* pcrel_offset */
5bd4f169
AM
970
971 /* 16 bit TOC-relative relocation, high 16 bits. */
972
973 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
974 HOWTO (R_PPC64_TOC16_HI, /* type */
975 16, /* rightshift */
976 1, /* size (0 = byte, 1 = short, 2 = long) */
977 16, /* bitsize */
b34976b6 978 FALSE, /* pc_relative */
5bd4f169 979 0, /* bitpos */
f9c6b907 980 complain_overflow_signed, /* complain_on_overflow */
805fc799 981 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 982 "R_PPC64_TOC16_HI", /* name */
b34976b6 983 FALSE, /* partial_inplace */
5bd4f169
AM
984 0, /* src_mask */
985 0xffff, /* dst_mask */
b34976b6 986 FALSE), /* pcrel_offset */
5bd4f169
AM
987
988 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
989 contents of the low 16 bits, treated as a signed number, is
990 negative. */
991
992 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
993 HOWTO (R_PPC64_TOC16_HA, /* type */
994 16, /* rightshift */
995 1, /* size (0 = byte, 1 = short, 2 = long) */
996 16, /* bitsize */
b34976b6 997 FALSE, /* pc_relative */
5bd4f169 998 0, /* bitpos */
f9c6b907 999 complain_overflow_signed, /* complain_on_overflow */
805fc799 1000 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1001 "R_PPC64_TOC16_HA", /* name */
b34976b6 1002 FALSE, /* partial_inplace */
5bd4f169
AM
1003 0, /* src_mask */
1004 0xffff, /* dst_mask */
b34976b6 1005 FALSE), /* pcrel_offset */
5bd4f169
AM
1006
1007 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1008
1009 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1010 HOWTO (R_PPC64_TOC, /* type */
1011 0, /* rightshift */
1012 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1013 64, /* bitsize */
b34976b6 1014 FALSE, /* pc_relative */
5bd4f169
AM
1015 0, /* bitpos */
1016 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 1017 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1018 "R_PPC64_TOC", /* name */
b34976b6 1019 FALSE, /* partial_inplace */
5bd4f169 1020 0, /* src_mask */
f5e87a1d 1021 ONES (64), /* dst_mask */
b34976b6 1022 FALSE), /* pcrel_offset */
5bd4f169
AM
1023
1024 /* Like R_PPC64_GOT16, but also informs the link editor that the
1025 value to relocate may (!) refer to a PLT entry which the link
1026 editor (a) may replace with the symbol value. If the link editor
1027 is unable to fully resolve the symbol, it may (b) create a PLT
1028 entry and store the address to the new PLT entry in the GOT.
1029 This permits lazy resolution of function symbols at run time.
1030 The link editor may also skip all of this and just (c) emit a
1031 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1032 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1033 HOWTO (R_PPC64_PLTGOT16, /* type */
1034 0, /* rightshift */
1035 1, /* size (0 = byte, 1 = short, 2 = long) */
1036 16, /* bitsize */
b34976b6 1037 FALSE, /* pc_relative */
5bd4f169
AM
1038 0, /* bitpos */
1039 complain_overflow_signed, /* complain_on_overflow */
805fc799 1040 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1041 "R_PPC64_PLTGOT16", /* name */
1042 FALSE, /* partial_inplace */
1043 0, /* src_mask */
1044 0xffff, /* dst_mask */
1045 FALSE), /* pcrel_offset */
1046
1047 /* Like R_PPC64_PLTGOT16, but without overflow. */
1048 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1049 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1050 0, /* rightshift */
1051 1, /* size (0 = byte, 1 = short, 2 = long) */
1052 16, /* bitsize */
1053 FALSE, /* pc_relative */
1054 0, /* bitpos */
1055 complain_overflow_dont, /* complain_on_overflow */
1056 ppc64_elf_unhandled_reloc, /* special_function */
1057 "R_PPC64_PLTGOT16_LO", /* name */
1058 FALSE, /* partial_inplace */
1059 0, /* src_mask */
1060 0xffff, /* dst_mask */
1061 FALSE), /* pcrel_offset */
1062
1063 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1064 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1065 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1066 16, /* rightshift */
1067 1, /* size (0 = byte, 1 = short, 2 = long) */
1068 16, /* bitsize */
1069 FALSE, /* pc_relative */
1070 0, /* bitpos */
f9c6b907 1071 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1072 ppc64_elf_unhandled_reloc, /* special_function */
1073 "R_PPC64_PLTGOT16_HI", /* 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, plus
1080 1 if the contents of the low 16 bits, treated as a signed number,
1081 is negative. */
1082 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1083 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1084 16, /* rightshift */
1085 1, /* size (0 = byte, 1 = short, 2 = long) */
1086 16, /* bitsize */
1087 FALSE, /* pc_relative */
1088 0, /* bitpos */
f9c6b907 1089 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1090 ppc64_elf_unhandled_reloc, /* special_function */
1091 "R_PPC64_PLTGOT16_HA", /* name */
1092 FALSE, /* partial_inplace */
1093 0, /* src_mask */
1094 0xffff, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1096
1097 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1098 HOWTO (R_PPC64_ADDR16_DS, /* type */
1099 0, /* rightshift */
1100 1, /* size (0 = byte, 1 = short, 2 = long) */
1101 16, /* bitsize */
1102 FALSE, /* pc_relative */
1103 0, /* bitpos */
1104 complain_overflow_bitfield, /* complain_on_overflow */
1105 bfd_elf_generic_reloc, /* special_function */
1106 "R_PPC64_ADDR16_DS", /* name */
1107 FALSE, /* partial_inplace */
1108 0, /* src_mask */
1109 0xfffc, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1111
1112 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1113 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1114 0, /* rightshift */
1115 1, /* size (0 = byte, 1 = short, 2 = long) */
1116 16, /* bitsize */
1117 FALSE, /* pc_relative */
1118 0, /* bitpos */
1119 complain_overflow_dont,/* complain_on_overflow */
1120 bfd_elf_generic_reloc, /* special_function */
1121 "R_PPC64_ADDR16_LO_DS",/* name */
1122 FALSE, /* partial_inplace */
1123 0, /* src_mask */
1124 0xfffc, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1126
1127 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1128 HOWTO (R_PPC64_GOT16_DS, /* type */
1129 0, /* rightshift */
1130 1, /* size (0 = byte, 1 = short, 2 = long) */
1131 16, /* bitsize */
1132 FALSE, /* pc_relative */
1133 0, /* bitpos */
1134 complain_overflow_signed, /* complain_on_overflow */
1135 ppc64_elf_unhandled_reloc, /* special_function */
1136 "R_PPC64_GOT16_DS", /* name */
1137 FALSE, /* partial_inplace */
1138 0, /* src_mask */
1139 0xfffc, /* dst_mask */
1140 FALSE), /* pcrel_offset */
1141
1142 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1143 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1144 0, /* rightshift */
1145 1, /* size (0 = byte, 1 = short, 2 = long) */
1146 16, /* bitsize */
1147 FALSE, /* pc_relative */
1148 0, /* bitpos */
1149 complain_overflow_dont, /* complain_on_overflow */
1150 ppc64_elf_unhandled_reloc, /* special_function */
1151 "R_PPC64_GOT16_LO_DS", /* name */
1152 FALSE, /* partial_inplace */
1153 0, /* src_mask */
1154 0xfffc, /* dst_mask */
1155 FALSE), /* pcrel_offset */
1156
1157 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1158 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1159 0, /* rightshift */
1160 1, /* size (0 = byte, 1 = short, 2 = long) */
1161 16, /* bitsize */
1162 FALSE, /* pc_relative */
1163 0, /* bitpos */
1164 complain_overflow_dont, /* complain_on_overflow */
1165 ppc64_elf_unhandled_reloc, /* special_function */
1166 "R_PPC64_PLT16_LO_DS", /* name */
1167 FALSE, /* partial_inplace */
1168 0, /* src_mask */
1169 0xfffc, /* dst_mask */
1170 FALSE), /* pcrel_offset */
1171
1172 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1173 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1174 0, /* rightshift */
1175 1, /* size (0 = byte, 1 = short, 2 = long) */
1176 16, /* bitsize */
1177 FALSE, /* pc_relative */
1178 0, /* bitpos */
1179 complain_overflow_bitfield, /* complain_on_overflow */
1180 ppc64_elf_sectoff_reloc, /* special_function */
1181 "R_PPC64_SECTOFF_DS", /* name */
1182 FALSE, /* partial_inplace */
1183 0, /* src_mask */
1184 0xfffc, /* dst_mask */
1185 FALSE), /* pcrel_offset */
1186
1187 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1188 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1189 0, /* rightshift */
1190 1, /* size (0 = byte, 1 = short, 2 = long) */
1191 16, /* bitsize */
1192 FALSE, /* pc_relative */
1193 0, /* bitpos */
1194 complain_overflow_dont, /* complain_on_overflow */
1195 ppc64_elf_sectoff_reloc, /* special_function */
1196 "R_PPC64_SECTOFF_LO_DS",/* name */
1197 FALSE, /* partial_inplace */
1198 0, /* src_mask */
1199 0xfffc, /* dst_mask */
1200 FALSE), /* pcrel_offset */
1201
1202 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1203 HOWTO (R_PPC64_TOC16_DS, /* type */
1204 0, /* rightshift */
1205 1, /* size (0 = byte, 1 = short, 2 = long) */
1206 16, /* bitsize */
1207 FALSE, /* pc_relative */
1208 0, /* bitpos */
1209 complain_overflow_signed, /* complain_on_overflow */
1210 ppc64_elf_toc_reloc, /* special_function */
1211 "R_PPC64_TOC16_DS", /* name */
1212 FALSE, /* partial_inplace */
1213 0, /* src_mask */
1214 0xfffc, /* dst_mask */
1215 FALSE), /* pcrel_offset */
1216
1217 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1218 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1219 0, /* rightshift */
1220 1, /* size (0 = byte, 1 = short, 2 = long) */
1221 16, /* bitsize */
1222 FALSE, /* pc_relative */
1223 0, /* bitpos */
1224 complain_overflow_dont, /* complain_on_overflow */
1225 ppc64_elf_toc_reloc, /* special_function */
1226 "R_PPC64_TOC16_LO_DS", /* name */
1227 FALSE, /* partial_inplace */
1228 0, /* src_mask */
1229 0xfffc, /* dst_mask */
1230 FALSE), /* pcrel_offset */
1231
1232 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1233 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1234 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
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_signed, /* complain_on_overflow */
1241 ppc64_elf_unhandled_reloc, /* special_function */
1242 "R_PPC64_PLTGOT16_DS", /* name */
1243 FALSE, /* partial_inplace */
1244 0, /* src_mask */
1245 0xfffc, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1247
1248 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1249 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1250 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
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_dont, /* complain_on_overflow */
1257 ppc64_elf_unhandled_reloc, /* special_function */
1258 "R_PPC64_PLTGOT16_LO_DS",/* name */
1259 FALSE, /* partial_inplace */
1260 0, /* src_mask */
1261 0xfffc, /* dst_mask */
1262 FALSE), /* pcrel_offset */
1263
727fc41e 1264 /* Marker relocs for TLS. */
411e1bfb
AM
1265 HOWTO (R_PPC64_TLS,
1266 0, /* rightshift */
1267 2, /* size (0 = byte, 1 = short, 2 = long) */
1268 32, /* bitsize */
1269 FALSE, /* pc_relative */
1270 0, /* bitpos */
1271 complain_overflow_dont, /* complain_on_overflow */
1272 bfd_elf_generic_reloc, /* special_function */
1273 "R_PPC64_TLS", /* name */
1274 FALSE, /* partial_inplace */
1275 0, /* src_mask */
1276 0, /* dst_mask */
1277 FALSE), /* pcrel_offset */
1278
727fc41e
AM
1279 HOWTO (R_PPC64_TLSGD,
1280 0, /* rightshift */
1281 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 32, /* bitsize */
1283 FALSE, /* pc_relative */
1284 0, /* bitpos */
1285 complain_overflow_dont, /* complain_on_overflow */
1286 bfd_elf_generic_reloc, /* special_function */
1287 "R_PPC64_TLSGD", /* name */
1288 FALSE, /* partial_inplace */
1289 0, /* src_mask */
1290 0, /* dst_mask */
1291 FALSE), /* pcrel_offset */
1292
1293 HOWTO (R_PPC64_TLSLD,
1294 0, /* rightshift */
1295 2, /* size (0 = byte, 1 = short, 2 = long) */
1296 32, /* bitsize */
1297 FALSE, /* pc_relative */
1298 0, /* bitpos */
1299 complain_overflow_dont, /* complain_on_overflow */
1300 bfd_elf_generic_reloc, /* special_function */
1301 "R_PPC64_TLSLD", /* name */
1302 FALSE, /* partial_inplace */
1303 0, /* src_mask */
1304 0, /* dst_mask */
1305 FALSE), /* pcrel_offset */
1306
3b421ab3
AM
1307 HOWTO (R_PPC64_TOCSAVE,
1308 0, /* rightshift */
1309 2, /* size (0 = byte, 1 = short, 2 = long) */
1310 32, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 bfd_elf_generic_reloc, /* special_function */
1315 "R_PPC64_TOCSAVE", /* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1320
411e1bfb
AM
1321 /* Computes the load module index of the load module that contains the
1322 definition of its TLS sym. */
1323 HOWTO (R_PPC64_DTPMOD64,
1324 0, /* rightshift */
1325 4, /* size (0 = byte, 1 = short, 2 = long) */
1326 64, /* bitsize */
1327 FALSE, /* pc_relative */
1328 0, /* bitpos */
1329 complain_overflow_dont, /* complain_on_overflow */
1330 ppc64_elf_unhandled_reloc, /* special_function */
1331 "R_PPC64_DTPMOD64", /* name */
1332 FALSE, /* partial_inplace */
1333 0, /* src_mask */
1334 ONES (64), /* dst_mask */
1335 FALSE), /* pcrel_offset */
1336
1337 /* Computes a dtv-relative displacement, the difference between the value
1338 of sym+add and the base address of the thread-local storage block that
1339 contains the definition of sym, minus 0x8000. */
1340 HOWTO (R_PPC64_DTPREL64,
1341 0, /* rightshift */
1342 4, /* size (0 = byte, 1 = short, 2 = long) */
1343 64, /* bitsize */
1344 FALSE, /* pc_relative */
1345 0, /* bitpos */
1346 complain_overflow_dont, /* complain_on_overflow */
1347 ppc64_elf_unhandled_reloc, /* special_function */
1348 "R_PPC64_DTPREL64", /* name */
1349 FALSE, /* partial_inplace */
1350 0, /* src_mask */
1351 ONES (64), /* dst_mask */
1352 FALSE), /* pcrel_offset */
1353
1354 /* A 16 bit dtprel reloc. */
1355 HOWTO (R_PPC64_DTPREL16,
1356 0, /* rightshift */
1357 1, /* size (0 = byte, 1 = short, 2 = long) */
1358 16, /* bitsize */
1359 FALSE, /* pc_relative */
1360 0, /* bitpos */
1361 complain_overflow_signed, /* complain_on_overflow */
1362 ppc64_elf_unhandled_reloc, /* special_function */
1363 "R_PPC64_DTPREL16", /* name */
1364 FALSE, /* partial_inplace */
1365 0, /* src_mask */
1366 0xffff, /* dst_mask */
1367 FALSE), /* pcrel_offset */
1368
1369 /* Like DTPREL16, but no overflow. */
1370 HOWTO (R_PPC64_DTPREL16_LO,
1371 0, /* rightshift */
1372 1, /* size (0 = byte, 1 = short, 2 = long) */
1373 16, /* bitsize */
1374 FALSE, /* pc_relative */
1375 0, /* bitpos */
1376 complain_overflow_dont, /* complain_on_overflow */
1377 ppc64_elf_unhandled_reloc, /* special_function */
1378 "R_PPC64_DTPREL16_LO", /* name */
1379 FALSE, /* partial_inplace */
1380 0, /* src_mask */
1381 0xffff, /* dst_mask */
1382 FALSE), /* pcrel_offset */
1383
1384 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1385 HOWTO (R_PPC64_DTPREL16_HI,
1386 16, /* rightshift */
1387 1, /* size (0 = byte, 1 = short, 2 = long) */
1388 16, /* bitsize */
1389 FALSE, /* pc_relative */
1390 0, /* bitpos */
f9c6b907 1391 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1392 ppc64_elf_unhandled_reloc, /* special_function */
1393 "R_PPC64_DTPREL16_HI", /* name */
1394 FALSE, /* partial_inplace */
1395 0, /* src_mask */
1396 0xffff, /* dst_mask */
1397 FALSE), /* pcrel_offset */
1398
1399 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1400 HOWTO (R_PPC64_DTPREL16_HA,
1401 16, /* rightshift */
1402 1, /* size (0 = byte, 1 = short, 2 = long) */
1403 16, /* bitsize */
1404 FALSE, /* pc_relative */
1405 0, /* bitpos */
f9c6b907 1406 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1407 ppc64_elf_unhandled_reloc, /* special_function */
1408 "R_PPC64_DTPREL16_HA", /* name */
1409 FALSE, /* partial_inplace */
1410 0, /* src_mask */
1411 0xffff, /* dst_mask */
1412 FALSE), /* pcrel_offset */
1413
1414 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1415 HOWTO (R_PPC64_DTPREL16_HIGHER,
1416 32, /* rightshift */
1417 1, /* size (0 = byte, 1 = short, 2 = long) */
1418 16, /* bitsize */
1419 FALSE, /* pc_relative */
1420 0, /* bitpos */
1421 complain_overflow_dont, /* complain_on_overflow */
1422 ppc64_elf_unhandled_reloc, /* special_function */
1423 "R_PPC64_DTPREL16_HIGHER", /* name */
1424 FALSE, /* partial_inplace */
1425 0, /* src_mask */
1426 0xffff, /* dst_mask */
1427 FALSE), /* pcrel_offset */
1428
1429 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1430 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1431 32, /* rightshift */
1432 1, /* size (0 = byte, 1 = short, 2 = long) */
1433 16, /* bitsize */
1434 FALSE, /* pc_relative */
1435 0, /* bitpos */
1436 complain_overflow_dont, /* complain_on_overflow */
1437 ppc64_elf_unhandled_reloc, /* special_function */
1438 "R_PPC64_DTPREL16_HIGHERA", /* name */
1439 FALSE, /* partial_inplace */
1440 0, /* src_mask */
1441 0xffff, /* dst_mask */
1442 FALSE), /* pcrel_offset */
1443
1444 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1445 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1446 48, /* rightshift */
1447 1, /* size (0 = byte, 1 = short, 2 = long) */
1448 16, /* bitsize */
1449 FALSE, /* pc_relative */
1450 0, /* bitpos */
1451 complain_overflow_dont, /* complain_on_overflow */
1452 ppc64_elf_unhandled_reloc, /* special_function */
1453 "R_PPC64_DTPREL16_HIGHEST", /* name */
1454 FALSE, /* partial_inplace */
1455 0, /* src_mask */
1456 0xffff, /* dst_mask */
1457 FALSE), /* pcrel_offset */
1458
1459 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1460 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1461 48, /* rightshift */
1462 1, /* size (0 = byte, 1 = short, 2 = long) */
1463 16, /* bitsize */
1464 FALSE, /* pc_relative */
1465 0, /* bitpos */
1466 complain_overflow_dont, /* complain_on_overflow */
1467 ppc64_elf_unhandled_reloc, /* special_function */
1468 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1469 FALSE, /* partial_inplace */
1470 0, /* src_mask */
1471 0xffff, /* dst_mask */
1472 FALSE), /* pcrel_offset */
1473
1474 /* Like DTPREL16, but for insns with a DS field. */
1475 HOWTO (R_PPC64_DTPREL16_DS,
1476 0, /* rightshift */
1477 1, /* size (0 = byte, 1 = short, 2 = long) */
1478 16, /* bitsize */
1479 FALSE, /* pc_relative */
1480 0, /* bitpos */
1481 complain_overflow_signed, /* complain_on_overflow */
1482 ppc64_elf_unhandled_reloc, /* special_function */
1483 "R_PPC64_DTPREL16_DS", /* name */
1484 FALSE, /* partial_inplace */
1485 0, /* src_mask */
1486 0xfffc, /* dst_mask */
1487 FALSE), /* pcrel_offset */
1488
1489 /* Like DTPREL16_DS, but no overflow. */
1490 HOWTO (R_PPC64_DTPREL16_LO_DS,
1491 0, /* rightshift */
1492 1, /* size (0 = byte, 1 = short, 2 = long) */
1493 16, /* bitsize */
1494 FALSE, /* pc_relative */
1495 0, /* bitpos */
1496 complain_overflow_dont, /* complain_on_overflow */
1497 ppc64_elf_unhandled_reloc, /* special_function */
1498 "R_PPC64_DTPREL16_LO_DS", /* name */
1499 FALSE, /* partial_inplace */
1500 0, /* src_mask */
1501 0xfffc, /* dst_mask */
1502 FALSE), /* pcrel_offset */
1503
1504 /* Computes a tp-relative displacement, the difference between the value of
1505 sym+add and the value of the thread pointer (r13). */
1506 HOWTO (R_PPC64_TPREL64,
1507 0, /* rightshift */
1508 4, /* size (0 = byte, 1 = short, 2 = long) */
1509 64, /* 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_TPREL64", /* name */
1515 FALSE, /* partial_inplace */
1516 0, /* src_mask */
1517 ONES (64), /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519
1520 /* A 16 bit tprel reloc. */
1521 HOWTO (R_PPC64_TPREL16,
1522 0, /* rightshift */
1523 1, /* size (0 = byte, 1 = short, 2 = long) */
1524 16, /* bitsize */
1525 FALSE, /* pc_relative */
1526 0, /* bitpos */
1527 complain_overflow_signed, /* complain_on_overflow */
1528 ppc64_elf_unhandled_reloc, /* special_function */
1529 "R_PPC64_TPREL16", /* name */
1530 FALSE, /* partial_inplace */
1531 0, /* src_mask */
1532 0xffff, /* dst_mask */
1533 FALSE), /* pcrel_offset */
1534
1535 /* Like TPREL16, but no overflow. */
1536 HOWTO (R_PPC64_TPREL16_LO,
1537 0, /* rightshift */
1538 1, /* size (0 = byte, 1 = short, 2 = long) */
1539 16, /* bitsize */
1540 FALSE, /* pc_relative */
1541 0, /* bitpos */
1542 complain_overflow_dont, /* complain_on_overflow */
1543 ppc64_elf_unhandled_reloc, /* special_function */
1544 "R_PPC64_TPREL16_LO", /* name */
1545 FALSE, /* partial_inplace */
1546 0, /* src_mask */
1547 0xffff, /* dst_mask */
1548 FALSE), /* pcrel_offset */
1549
1550 /* Like TPREL16_LO, but next higher group of 16 bits. */
1551 HOWTO (R_PPC64_TPREL16_HI,
1552 16, /* rightshift */
1553 1, /* size (0 = byte, 1 = short, 2 = long) */
1554 16, /* bitsize */
1555 FALSE, /* pc_relative */
1556 0, /* bitpos */
f9c6b907 1557 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1558 ppc64_elf_unhandled_reloc, /* special_function */
1559 "R_PPC64_TPREL16_HI", /* name */
1560 FALSE, /* partial_inplace */
1561 0, /* src_mask */
1562 0xffff, /* dst_mask */
1563 FALSE), /* pcrel_offset */
1564
1565 /* Like TPREL16_HI, but adjust for low 16 bits. */
1566 HOWTO (R_PPC64_TPREL16_HA,
1567 16, /* rightshift */
1568 1, /* size (0 = byte, 1 = short, 2 = long) */
1569 16, /* bitsize */
1570 FALSE, /* pc_relative */
1571 0, /* bitpos */
f9c6b907 1572 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1573 ppc64_elf_unhandled_reloc, /* special_function */
1574 "R_PPC64_TPREL16_HA", /* name */
1575 FALSE, /* partial_inplace */
1576 0, /* src_mask */
1577 0xffff, /* dst_mask */
1578 FALSE), /* pcrel_offset */
1579
1580 /* Like TPREL16_HI, but next higher group of 16 bits. */
1581 HOWTO (R_PPC64_TPREL16_HIGHER,
1582 32, /* rightshift */
1583 1, /* size (0 = byte, 1 = short, 2 = long) */
1584 16, /* bitsize */
1585 FALSE, /* pc_relative */
1586 0, /* bitpos */
1587 complain_overflow_dont, /* complain_on_overflow */
1588 ppc64_elf_unhandled_reloc, /* special_function */
1589 "R_PPC64_TPREL16_HIGHER", /* name */
1590 FALSE, /* partial_inplace */
1591 0, /* src_mask */
1592 0xffff, /* dst_mask */
1593 FALSE), /* pcrel_offset */
1594
1595 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1596 HOWTO (R_PPC64_TPREL16_HIGHERA,
1597 32, /* rightshift */
1598 1, /* size (0 = byte, 1 = short, 2 = long) */
1599 16, /* bitsize */
1600 FALSE, /* pc_relative */
1601 0, /* bitpos */
1602 complain_overflow_dont, /* complain_on_overflow */
1603 ppc64_elf_unhandled_reloc, /* special_function */
1604 "R_PPC64_TPREL16_HIGHERA", /* name */
1605 FALSE, /* partial_inplace */
1606 0, /* src_mask */
1607 0xffff, /* dst_mask */
1608 FALSE), /* pcrel_offset */
1609
1610 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1611 HOWTO (R_PPC64_TPREL16_HIGHEST,
1612 48, /* rightshift */
1613 1, /* size (0 = byte, 1 = short, 2 = long) */
1614 16, /* bitsize */
1615 FALSE, /* pc_relative */
1616 0, /* bitpos */
1617 complain_overflow_dont, /* complain_on_overflow */
1618 ppc64_elf_unhandled_reloc, /* special_function */
1619 "R_PPC64_TPREL16_HIGHEST", /* name */
1620 FALSE, /* partial_inplace */
1621 0, /* src_mask */
1622 0xffff, /* dst_mask */
1623 FALSE), /* pcrel_offset */
1624
1625 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1626 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1627 48, /* rightshift */
1628 1, /* size (0 = byte, 1 = short, 2 = long) */
1629 16, /* bitsize */
1630 FALSE, /* pc_relative */
1631 0, /* bitpos */
1632 complain_overflow_dont, /* complain_on_overflow */
1633 ppc64_elf_unhandled_reloc, /* special_function */
1634 "R_PPC64_TPREL16_HIGHESTA", /* name */
1635 FALSE, /* partial_inplace */
1636 0, /* src_mask */
1637 0xffff, /* dst_mask */
1638 FALSE), /* pcrel_offset */
1639
1640 /* Like TPREL16, but for insns with a DS field. */
1641 HOWTO (R_PPC64_TPREL16_DS,
1642 0, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 16, /* bitsize */
1645 FALSE, /* pc_relative */
1646 0, /* bitpos */
1647 complain_overflow_signed, /* complain_on_overflow */
1648 ppc64_elf_unhandled_reloc, /* special_function */
1649 "R_PPC64_TPREL16_DS", /* name */
1650 FALSE, /* partial_inplace */
1651 0, /* src_mask */
1652 0xfffc, /* dst_mask */
1653 FALSE), /* pcrel_offset */
1654
1655 /* Like TPREL16_DS, but no overflow. */
1656 HOWTO (R_PPC64_TPREL16_LO_DS,
1657 0, /* rightshift */
1658 1, /* size (0 = byte, 1 = short, 2 = long) */
1659 16, /* bitsize */
1660 FALSE, /* pc_relative */
1661 0, /* bitpos */
1662 complain_overflow_dont, /* complain_on_overflow */
1663 ppc64_elf_unhandled_reloc, /* special_function */
1664 "R_PPC64_TPREL16_LO_DS", /* name */
1665 FALSE, /* partial_inplace */
1666 0, /* src_mask */
1667 0xfffc, /* dst_mask */
1668 FALSE), /* pcrel_offset */
1669
1670 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1671 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1672 to the first entry relative to the TOC base (r2). */
1673 HOWTO (R_PPC64_GOT_TLSGD16,
1674 0, /* rightshift */
1675 1, /* size (0 = byte, 1 = short, 2 = long) */
1676 16, /* bitsize */
1677 FALSE, /* pc_relative */
1678 0, /* bitpos */
1679 complain_overflow_signed, /* complain_on_overflow */
1680 ppc64_elf_unhandled_reloc, /* special_function */
1681 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1682 FALSE, /* partial_inplace */
5bd4f169
AM
1683 0, /* src_mask */
1684 0xffff, /* dst_mask */
b34976b6 1685 FALSE), /* pcrel_offset */
5bd4f169 1686
411e1bfb
AM
1687 /* Like GOT_TLSGD16, but no overflow. */
1688 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1689 0, /* rightshift */
1690 1, /* size (0 = byte, 1 = short, 2 = long) */
1691 16, /* bitsize */
b34976b6 1692 FALSE, /* pc_relative */
5bd4f169
AM
1693 0, /* bitpos */
1694 complain_overflow_dont, /* complain_on_overflow */
805fc799 1695 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1696 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1697 FALSE, /* partial_inplace */
5bd4f169
AM
1698 0, /* src_mask */
1699 0xffff, /* dst_mask */
b34976b6 1700 FALSE), /* pcrel_offset */
5bd4f169 1701
411e1bfb
AM
1702 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1703 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1704 16, /* rightshift */
1705 1, /* size (0 = byte, 1 = short, 2 = long) */
1706 16, /* bitsize */
b34976b6 1707 FALSE, /* pc_relative */
5bd4f169 1708 0, /* bitpos */
f9c6b907 1709 complain_overflow_signed, /* complain_on_overflow */
805fc799 1710 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1711 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1712 FALSE, /* partial_inplace */
5bd4f169
AM
1713 0, /* src_mask */
1714 0xffff, /* dst_mask */
b34976b6 1715 FALSE), /* pcrel_offset */
5bd4f169 1716
411e1bfb
AM
1717 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1718 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1719 16, /* rightshift */
1720 1, /* size (0 = byte, 1 = short, 2 = long) */
1721 16, /* bitsize */
b34976b6 1722 FALSE, /* pc_relative */
5bd4f169 1723 0, /* bitpos */
f9c6b907 1724 complain_overflow_signed, /* complain_on_overflow */
805fc799 1725 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1726 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1727 FALSE, /* partial_inplace */
5bd4f169
AM
1728 0, /* src_mask */
1729 0xffff, /* dst_mask */
b34976b6 1730 FALSE), /* pcrel_offset */
5bd4f169 1731
411e1bfb
AM
1732 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1733 with values (sym+add)@dtpmod and zero, and computes the offset to the
1734 first entry relative to the TOC base (r2). */
1735 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1736 0, /* rightshift */
1737 1, /* size (0 = byte, 1 = short, 2 = long) */
1738 16, /* bitsize */
b34976b6 1739 FALSE, /* pc_relative */
5bd4f169 1740 0, /* bitpos */
411e1bfb
AM
1741 complain_overflow_signed, /* complain_on_overflow */
1742 ppc64_elf_unhandled_reloc, /* special_function */
1743 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1744 FALSE, /* partial_inplace */
d006db6c 1745 0, /* src_mask */
411e1bfb 1746 0xffff, /* dst_mask */
b34976b6 1747 FALSE), /* pcrel_offset */
5bd4f169 1748
411e1bfb
AM
1749 /* Like GOT_TLSLD16, but no overflow. */
1750 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1751 0, /* rightshift */
1752 1, /* size (0 = byte, 1 = short, 2 = long) */
1753 16, /* bitsize */
b34976b6 1754 FALSE, /* pc_relative */
5bd4f169 1755 0, /* bitpos */
411e1bfb
AM
1756 complain_overflow_dont, /* complain_on_overflow */
1757 ppc64_elf_unhandled_reloc, /* special_function */
1758 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1759 FALSE, /* partial_inplace */
d006db6c 1760 0, /* src_mask */
411e1bfb 1761 0xffff, /* dst_mask */
b34976b6 1762 FALSE), /* pcrel_offset */
5bd4f169 1763
411e1bfb
AM
1764 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1765 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1766 16, /* rightshift */
5bd4f169
AM
1767 1, /* size (0 = byte, 1 = short, 2 = long) */
1768 16, /* bitsize */
b34976b6 1769 FALSE, /* pc_relative */
5bd4f169 1770 0, /* bitpos */
f9c6b907 1771 complain_overflow_signed, /* complain_on_overflow */
805fc799 1772 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1773 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1774 FALSE, /* partial_inplace */
d006db6c 1775 0, /* src_mask */
411e1bfb 1776 0xffff, /* dst_mask */
b34976b6 1777 FALSE), /* pcrel_offset */
5bd4f169 1778
411e1bfb
AM
1779 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1780 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1781 16, /* rightshift */
5bd4f169
AM
1782 1, /* size (0 = byte, 1 = short, 2 = long) */
1783 16, /* bitsize */
b34976b6 1784 FALSE, /* pc_relative */
5bd4f169 1785 0, /* bitpos */
f9c6b907 1786 complain_overflow_signed, /* complain_on_overflow */
805fc799 1787 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1788 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1789 FALSE, /* partial_inplace */
d006db6c 1790 0, /* src_mask */
411e1bfb 1791 0xffff, /* dst_mask */
b34976b6 1792 FALSE), /* pcrel_offset */
5bd4f169 1793
411e1bfb
AM
1794 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1795 the offset to the entry relative to the TOC base (r2). */
1796 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1797 0, /* rightshift */
1798 1, /* size (0 = byte, 1 = short, 2 = long) */
1799 16, /* bitsize */
b34976b6 1800 FALSE, /* pc_relative */
5bd4f169 1801 0, /* bitpos */
411e1bfb 1802 complain_overflow_signed, /* complain_on_overflow */
805fc799 1803 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1804 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1805 FALSE, /* partial_inplace */
d006db6c 1806 0, /* src_mask */
5bd4f169 1807 0xfffc, /* dst_mask */
b34976b6 1808 FALSE), /* pcrel_offset */
5bd4f169 1809
411e1bfb
AM
1810 /* Like GOT_DTPREL16_DS, but no overflow. */
1811 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1812 0, /* rightshift */
c061c2d8
AM
1813 1, /* size (0 = byte, 1 = short, 2 = long) */
1814 16, /* bitsize */
b34976b6 1815 FALSE, /* pc_relative */
5bd4f169 1816 0, /* bitpos */
411e1bfb
AM
1817 complain_overflow_dont, /* complain_on_overflow */
1818 ppc64_elf_unhandled_reloc, /* special_function */
1819 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1820 FALSE, /* partial_inplace */
d006db6c 1821 0, /* src_mask */
c061c2d8 1822 0xfffc, /* dst_mask */
b34976b6 1823 FALSE), /* pcrel_offset */
5bd4f169 1824
411e1bfb
AM
1825 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1826 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1827 16, /* rightshift */
5bd4f169
AM
1828 1, /* size (0 = byte, 1 = short, 2 = long) */
1829 16, /* bitsize */
b34976b6 1830 FALSE, /* pc_relative */
5bd4f169 1831 0, /* bitpos */
f9c6b907 1832 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1833 ppc64_elf_unhandled_reloc, /* special_function */
1834 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1835 FALSE, /* partial_inplace */
d006db6c 1836 0, /* src_mask */
411e1bfb 1837 0xffff, /* dst_mask */
b34976b6 1838 FALSE), /* pcrel_offset */
5bd4f169 1839
411e1bfb
AM
1840 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1841 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1842 16, /* rightshift */
1843 1, /* size (0 = byte, 1 = short, 2 = long) */
1844 16, /* bitsize */
1845 FALSE, /* pc_relative */
1846 0, /* bitpos */
f9c6b907 1847 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1848 ppc64_elf_unhandled_reloc, /* special_function */
1849 "R_PPC64_GOT_DTPREL16_HA", /* name */
1850 FALSE, /* partial_inplace */
1851 0, /* src_mask */
1852 0xffff, /* dst_mask */
1853 FALSE), /* pcrel_offset */
1854
1855 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1856 offset to the entry relative to the TOC base (r2). */
1857 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1858 0, /* rightshift */
1859 1, /* size (0 = byte, 1 = short, 2 = long) */
1860 16, /* bitsize */
b34976b6 1861 FALSE, /* pc_relative */
5bd4f169
AM
1862 0, /* bitpos */
1863 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1864 ppc64_elf_unhandled_reloc, /* special_function */
1865 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1866 FALSE, /* partial_inplace */
d006db6c 1867 0, /* src_mask */
ad8e1ba5 1868 0xfffc, /* dst_mask */
b34976b6 1869 FALSE), /* pcrel_offset */
5bd4f169 1870
411e1bfb
AM
1871 /* Like GOT_TPREL16_DS, but no overflow. */
1872 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1873 0, /* rightshift */
1874 1, /* size (0 = byte, 1 = short, 2 = long) */
1875 16, /* bitsize */
b34976b6 1876 FALSE, /* pc_relative */
5bd4f169
AM
1877 0, /* bitpos */
1878 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1879 ppc64_elf_unhandled_reloc, /* special_function */
1880 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1881 FALSE, /* partial_inplace */
d006db6c 1882 0, /* src_mask */
ad8e1ba5 1883 0xfffc, /* dst_mask */
b34976b6 1884 FALSE), /* pcrel_offset */
5bd4f169 1885
411e1bfb
AM
1886 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1887 HOWTO (R_PPC64_GOT_TPREL16_HI,
1888 16, /* rightshift */
5bd4f169
AM
1889 1, /* size (0 = byte, 1 = short, 2 = long) */
1890 16, /* bitsize */
b34976b6 1891 FALSE, /* pc_relative */
5bd4f169 1892 0, /* bitpos */
f9c6b907 1893 complain_overflow_signed, /* complain_on_overflow */
805fc799 1894 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1895 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1896 FALSE, /* partial_inplace */
d006db6c 1897 0, /* src_mask */
411e1bfb 1898 0xffff, /* dst_mask */
b34976b6 1899 FALSE), /* pcrel_offset */
5bd4f169 1900
411e1bfb
AM
1901 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1902 HOWTO (R_PPC64_GOT_TPREL16_HA,
1903 16, /* rightshift */
5bd4f169
AM
1904 1, /* size (0 = byte, 1 = short, 2 = long) */
1905 16, /* bitsize */
b34976b6 1906 FALSE, /* pc_relative */
5bd4f169 1907 0, /* bitpos */
f9c6b907 1908 complain_overflow_signed, /* complain_on_overflow */
805fc799 1909 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1910 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1911 FALSE, /* partial_inplace */
d006db6c 1912 0, /* src_mask */
411e1bfb 1913 0xffff, /* dst_mask */
b34976b6 1914 FALSE), /* pcrel_offset */
5bd4f169 1915
25f23106
AM
1916 HOWTO (R_PPC64_JMP_IREL, /* type */
1917 0, /* rightshift */
1918 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1919 0, /* bitsize */
1920 FALSE, /* pc_relative */
1921 0, /* bitpos */
1922 complain_overflow_dont, /* complain_on_overflow */
1923 ppc64_elf_unhandled_reloc, /* special_function */
1924 "R_PPC64_JMP_IREL", /* name */
1925 FALSE, /* partial_inplace */
1926 0, /* src_mask */
1927 0, /* dst_mask */
1928 FALSE), /* pcrel_offset */
1929
e054468f
AM
1930 HOWTO (R_PPC64_IRELATIVE, /* type */
1931 0, /* rightshift */
1932 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1933 64, /* bitsize */
1934 FALSE, /* pc_relative */
1935 0, /* bitpos */
1936 complain_overflow_dont, /* complain_on_overflow */
1937 bfd_elf_generic_reloc, /* special_function */
1938 "R_PPC64_IRELATIVE", /* name */
1939 FALSE, /* partial_inplace */
1940 0, /* src_mask */
1941 ONES (64), /* dst_mask */
1942 FALSE), /* pcrel_offset */
1943
25f23106
AM
1944 /* A 16 bit relative relocation. */
1945 HOWTO (R_PPC64_REL16, /* type */
1946 0, /* rightshift */
1947 1, /* size (0 = byte, 1 = short, 2 = long) */
1948 16, /* bitsize */
1949 TRUE, /* pc_relative */
1950 0, /* bitpos */
1951 complain_overflow_bitfield, /* complain_on_overflow */
1952 bfd_elf_generic_reloc, /* special_function */
1953 "R_PPC64_REL16", /* name */
1954 FALSE, /* partial_inplace */
1955 0, /* src_mask */
1956 0xffff, /* dst_mask */
1957 TRUE), /* pcrel_offset */
1958
1959 /* A 16 bit relative relocation without overflow. */
1960 HOWTO (R_PPC64_REL16_LO, /* type */
1961 0, /* rightshift */
1962 1, /* size (0 = byte, 1 = short, 2 = long) */
1963 16, /* bitsize */
1964 TRUE, /* pc_relative */
1965 0, /* bitpos */
1966 complain_overflow_dont,/* complain_on_overflow */
1967 bfd_elf_generic_reloc, /* special_function */
1968 "R_PPC64_REL16_LO", /* name */
1969 FALSE, /* partial_inplace */
1970 0, /* src_mask */
1971 0xffff, /* dst_mask */
1972 TRUE), /* pcrel_offset */
1973
1974 /* The high order 16 bits of a relative address. */
1975 HOWTO (R_PPC64_REL16_HI, /* type */
1976 16, /* rightshift */
1977 1, /* size (0 = byte, 1 = short, 2 = long) */
1978 16, /* bitsize */
1979 TRUE, /* pc_relative */
1980 0, /* bitpos */
f9c6b907 1981 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1982 bfd_elf_generic_reloc, /* special_function */
1983 "R_PPC64_REL16_HI", /* name */
1984 FALSE, /* partial_inplace */
1985 0, /* src_mask */
1986 0xffff, /* dst_mask */
1987 TRUE), /* pcrel_offset */
1988
1989 /* The high order 16 bits of a relative address, plus 1 if the contents of
1990 the low 16 bits, treated as a signed number, is negative. */
1991 HOWTO (R_PPC64_REL16_HA, /* 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 ppc64_elf_ha_reloc, /* special_function */
1999 "R_PPC64_REL16_HA", /* name */
2000 FALSE, /* partial_inplace */
2001 0, /* src_mask */
2002 0xffff, /* dst_mask */
2003 TRUE), /* pcrel_offset */
2004
f9c6b907
AM
2005 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2006 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2007 16, /* rightshift */
2008 1, /* size (0 = byte, 1 = short, 2 = long) */
2009 16, /* bitsize */
2010 FALSE, /* pc_relative */
2011 0, /* bitpos */
2012 complain_overflow_dont, /* complain_on_overflow */
2013 bfd_elf_generic_reloc, /* special_function */
2014 "R_PPC64_ADDR16_HIGH", /* name */
2015 FALSE, /* partial_inplace */
2016 0, /* src_mask */
2017 0xffff, /* dst_mask */
2018 FALSE), /* pcrel_offset */
2019
2020 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2021 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2022 16, /* rightshift */
2023 1, /* size (0 = byte, 1 = short, 2 = long) */
2024 16, /* bitsize */
2025 FALSE, /* pc_relative */
2026 0, /* bitpos */
2027 complain_overflow_dont, /* complain_on_overflow */
2028 ppc64_elf_ha_reloc, /* special_function */
2029 "R_PPC64_ADDR16_HIGHA", /* name */
2030 FALSE, /* partial_inplace */
2031 0, /* src_mask */
2032 0xffff, /* dst_mask */
2033 FALSE), /* pcrel_offset */
2034
2035 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2036 HOWTO (R_PPC64_DTPREL16_HIGH,
2037 16, /* rightshift */
2038 1, /* size (0 = byte, 1 = short, 2 = long) */
2039 16, /* bitsize */
2040 FALSE, /* pc_relative */
2041 0, /* bitpos */
2042 complain_overflow_dont, /* complain_on_overflow */
2043 ppc64_elf_unhandled_reloc, /* special_function */
2044 "R_PPC64_DTPREL16_HIGH", /* name */
2045 FALSE, /* partial_inplace */
2046 0, /* src_mask */
2047 0xffff, /* dst_mask */
2048 FALSE), /* pcrel_offset */
2049
2050 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2051 HOWTO (R_PPC64_DTPREL16_HIGHA,
2052 16, /* rightshift */
2053 1, /* size (0 = byte, 1 = short, 2 = long) */
2054 16, /* bitsize */
2055 FALSE, /* pc_relative */
2056 0, /* bitpos */
2057 complain_overflow_dont, /* complain_on_overflow */
2058 ppc64_elf_unhandled_reloc, /* special_function */
2059 "R_PPC64_DTPREL16_HIGHA", /* name */
2060 FALSE, /* partial_inplace */
2061 0, /* src_mask */
2062 0xffff, /* dst_mask */
2063 FALSE), /* pcrel_offset */
2064
2065 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2066 HOWTO (R_PPC64_TPREL16_HIGH,
2067 16, /* rightshift */
2068 1, /* size (0 = byte, 1 = short, 2 = long) */
2069 16, /* bitsize */
2070 FALSE, /* pc_relative */
2071 0, /* bitpos */
2072 complain_overflow_dont, /* complain_on_overflow */
2073 ppc64_elf_unhandled_reloc, /* special_function */
2074 "R_PPC64_TPREL16_HIGH", /* name */
2075 FALSE, /* partial_inplace */
2076 0, /* src_mask */
2077 0xffff, /* dst_mask */
2078 FALSE), /* pcrel_offset */
2079
2080 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2081 HOWTO (R_PPC64_TPREL16_HIGHA,
2082 16, /* rightshift */
2083 1, /* size (0 = byte, 1 = short, 2 = long) */
2084 16, /* bitsize */
2085 FALSE, /* pc_relative */
2086 0, /* bitpos */
2087 complain_overflow_dont, /* complain_on_overflow */
2088 ppc64_elf_unhandled_reloc, /* special_function */
2089 "R_PPC64_TPREL16_HIGHA", /* name */
2090 FALSE, /* partial_inplace */
2091 0, /* src_mask */
2092 0xffff, /* dst_mask */
2093 FALSE), /* pcrel_offset */
2094
5bd4f169
AM
2095 /* GNU extension to record C++ vtable hierarchy. */
2096 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2097 0, /* rightshift */
2098 0, /* size (0 = byte, 1 = short, 2 = long) */
2099 0, /* bitsize */
b34976b6 2100 FALSE, /* pc_relative */
5bd4f169
AM
2101 0, /* bitpos */
2102 complain_overflow_dont, /* complain_on_overflow */
2103 NULL, /* special_function */
2104 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2105 FALSE, /* partial_inplace */
5bd4f169
AM
2106 0, /* src_mask */
2107 0, /* dst_mask */
b34976b6 2108 FALSE), /* pcrel_offset */
5bd4f169
AM
2109
2110 /* GNU extension to record C++ vtable member usage. */
2111 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2112 0, /* rightshift */
2113 0, /* size (0 = byte, 1 = short, 2 = long) */
2114 0, /* bitsize */
b34976b6 2115 FALSE, /* pc_relative */
5bd4f169
AM
2116 0, /* bitpos */
2117 complain_overflow_dont, /* complain_on_overflow */
2118 NULL, /* special_function */
2119 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2120 FALSE, /* partial_inplace */
5bd4f169
AM
2121 0, /* src_mask */
2122 0, /* dst_mask */
b34976b6 2123 FALSE), /* pcrel_offset */
5bd4f169
AM
2124};
2125
2126\f
2127/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2128 be done. */
2129
2130static void
4ce794b7 2131ppc_howto_init (void)
5bd4f169
AM
2132{
2133 unsigned int i, type;
2134
2135 for (i = 0;
2136 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2137 i++)
2138 {
2139 type = ppc64_elf_howto_raw[i].type;
2140 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2141 / sizeof (ppc64_elf_howto_table[0])));
2142 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2143 }
2144}
2145
2146static reloc_howto_type *
4ce794b7
AM
2147ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2148 bfd_reloc_code_real_type code)
5bd4f169 2149{
411e1bfb 2150 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2151
2152 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2153 /* Initialize howto table if needed. */
2154 ppc_howto_init ();
2155
4ce794b7 2156 switch (code)
5bd4f169
AM
2157 {
2158 default:
4ce794b7 2159 return NULL;
5bd4f169 2160
411e1bfb
AM
2161 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2162 break;
2163 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2164 break;
2165 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2166 break;
2167 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2168 break;
2169 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2170 break;
2171 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2172 break;
f9c6b907
AM
2173 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2174 break;
411e1bfb 2175 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2176 break;
f9c6b907
AM
2177 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2178 break;
411e1bfb 2179 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2180 break;
411e1bfb 2181 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2182 break;
411e1bfb 2183 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2184 break;
411e1bfb 2185 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2186 break;
411e1bfb 2187 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2188 break;
411e1bfb 2189 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2190 break;
411e1bfb 2191 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2192 break;
411e1bfb 2193 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2194 break;
411e1bfb 2195 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2196 break;
411e1bfb 2197 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2198 break;
411e1bfb 2199 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2200 break;
411e1bfb 2201 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2202 break;
411e1bfb 2203 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2204 break;
411e1bfb 2205 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2206 break;
411e1bfb 2207 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2208 break;
411e1bfb 2209 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2210 break;
411e1bfb 2211 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2212 break;
411e1bfb 2213 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2214 break;
411e1bfb 2215 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2216 break;
411e1bfb 2217 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2218 break;
411e1bfb 2219 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2220 break;
411e1bfb 2221 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2222 break;
411e1bfb 2223 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2224 break;
411e1bfb 2225 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2226 break;
411e1bfb 2227 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2228 break;
411e1bfb 2229 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2230 break;
411e1bfb 2231 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2232 break;
411e1bfb 2233 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2234 break;
411e1bfb 2235 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2236 break;
411e1bfb 2237 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2238 break;
411e1bfb 2239 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2240 break;
411e1bfb 2241 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2242 break;
411e1bfb 2243 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2244 break;
411e1bfb 2245 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2246 break;
411e1bfb 2247 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2248 break;
411e1bfb 2249 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2250 break;
411e1bfb 2251 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2252 break;
411e1bfb 2253 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2254 break;
411e1bfb 2255 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2256 break;
411e1bfb 2257 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2258 break;
411e1bfb 2259 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2260 break;
411e1bfb 2261 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2262 break;
411e1bfb 2263 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2264 break;
411e1bfb 2265 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2266 break;
411e1bfb 2267 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2268 break;
411e1bfb 2269 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2270 break;
411e1bfb 2271 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2272 break;
411e1bfb 2273 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2274 break;
411e1bfb 2275 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2276 break;
411e1bfb 2277 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2278 break;
411e1bfb 2279 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2280 break;
411e1bfb 2281 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2282 break;
411e1bfb 2283 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2284 break;
727fc41e
AM
2285 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2286 break;
2287 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2288 break;
411e1bfb 2289 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2290 break;
411e1bfb 2291 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2292 break;
411e1bfb 2293 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2294 break;
411e1bfb 2295 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2296 break;
f9c6b907
AM
2297 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2298 break;
411e1bfb 2299 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2300 break;
f9c6b907
AM
2301 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2302 break;
411e1bfb 2303 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2304 break;
411e1bfb
AM
2305 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2306 break;
2307 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2308 break;
2309 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2310 break;
f9c6b907
AM
2311 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2312 break;
411e1bfb
AM
2313 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2314 break;
f9c6b907
AM
2315 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2316 break;
411e1bfb
AM
2317 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2318 break;
2319 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2320 break;
2321 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2322 break;
2323 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2324 break;
2325 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2326 break;
2327 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2328 break;
2329 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2330 break;
2331 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2332 break;
2333 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2334 break;
2335 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2336 break;
2337 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2338 break;
2339 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2340 break;
2341 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2342 break;
2343 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2344 break;
2345 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2346 break;
2347 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2348 break;
2349 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2350 break;
2351 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2352 break;
2353 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2354 break;
2355 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2356 break;
2357 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2358 break;
2359 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2360 break;
2361 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2362 break;
2363 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2364 break;
2365 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2366 break;
2367 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2368 break;
2369 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2370 break;
2371 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2372 break;
2373 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2374 break;
25f23106
AM
2375 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2376 break;
2377 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2378 break;
2379 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2380 break;
2381 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2382 break;
411e1bfb
AM
2383 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2384 break;
2385 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2386 break;
2387 }
2388
4ce794b7 2389 return ppc64_elf_howto_table[r];
5bd4f169
AM
2390};
2391
157090f7
AM
2392static reloc_howto_type *
2393ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2394 const char *r_name)
2395{
2396 unsigned int i;
2397
2398 for (i = 0;
2399 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2400 i++)
2401 if (ppc64_elf_howto_raw[i].name != NULL
2402 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2403 return &ppc64_elf_howto_raw[i];
2404
2405 return NULL;
2406}
2407
5bd4f169
AM
2408/* Set the howto pointer for a PowerPC ELF reloc. */
2409
2410static void
4ce794b7
AM
2411ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2412 Elf_Internal_Rela *dst)
5bd4f169 2413{
65f38f15
AM
2414 unsigned int type;
2415
ef60b7ff 2416 /* Initialize howto table if needed. */
5bd4f169 2417 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2418 ppc_howto_init ();
2419
65f38f15 2420 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2421 if (type >= (sizeof (ppc64_elf_howto_table)
2422 / sizeof (ppc64_elf_howto_table[0])))
2423 {
2424 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2425 abfd, (int) type);
2426 type = R_PPC64_NONE;
d0fb9a8d 2427 }
65f38f15 2428 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2429}
2430
04c9666a 2431/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2432
2433static bfd_reloc_status_type
4ce794b7
AM
2434ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2435 void *data, asection *input_section,
2436 bfd *output_bfd, char **error_message)
5bd4f169 2437{
805fc799
AM
2438 /* If this is a relocatable link (output_bfd test tells us), just
2439 call the generic function. Any adjustment will be done at final
2440 link time. */
2441 if (output_bfd != NULL)
cedb70c5 2442 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2443 input_section, output_bfd, error_message);
2444
2445 /* Adjust the addend for sign extension of the low 16 bits.
2446 We won't actually be using the low 16 bits, so trashing them
2447 doesn't matter. */
2448 reloc_entry->addend += 0x8000;
2449 return bfd_reloc_continue;
2450}
5bd4f169 2451
2441e016
AM
2452static bfd_reloc_status_type
2453ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2454 void *data, asection *input_section,
2455 bfd *output_bfd, char **error_message)
2456{
2457 if (output_bfd != NULL)
2458 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2459 input_section, output_bfd, error_message);
2460
699733f6
AM
2461 if (strcmp (symbol->section->name, ".opd") == 0
2462 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2463 {
2464 bfd_vma dest = opd_entry_value (symbol->section,
2465 symbol->value + reloc_entry->addend,
aef36ac1 2466 NULL, NULL, FALSE);
2441e016
AM
2467 if (dest != (bfd_vma) -1)
2468 reloc_entry->addend = dest - (symbol->value
2469 + symbol->section->output_section->vma
2470 + symbol->section->output_offset);
2471 }
2472 return bfd_reloc_continue;
2473}
2474
805fc799 2475static bfd_reloc_status_type
4ce794b7
AM
2476ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2477 void *data, asection *input_section,
2478 bfd *output_bfd, char **error_message)
805fc799
AM
2479{
2480 long insn;
04c9666a 2481 enum elf_ppc64_reloc_type r_type;
805fc799 2482 bfd_size_type octets;
794e51c0
AM
2483 /* Assume 'at' branch hints. */
2484 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2485
2486 /* If this is a relocatable link (output_bfd test tells us), just
2487 call the generic function. Any adjustment will be done at final
2488 link time. */
5bd4f169 2489 if (output_bfd != NULL)
cedb70c5 2490 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2491 input_section, output_bfd, error_message);
2492
2493 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2494 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2495 insn &= ~(0x01 << 21);
4ce794b7 2496 r_type = reloc_entry->howto->type;
805fc799
AM
2497 if (r_type == R_PPC64_ADDR14_BRTAKEN
2498 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2499 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2500
794e51c0 2501 if (is_isa_v2)
5bd4f169 2502 {
805fc799
AM
2503 /* Set 'a' bit. This is 0b00010 in BO field for branch
2504 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2505 for branch on CTR insns (BO == 1a00t or 1a01t). */
2506 if ((insn & (0x14 << 21)) == (0x04 << 21))
2507 insn |= 0x02 << 21;
2508 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2509 insn |= 0x08 << 21;
2510 else
2441e016 2511 goto out;
5bd4f169 2512 }
805fc799
AM
2513 else
2514 {
2515 bfd_vma target = 0;
2516 bfd_vma from;
5bd4f169 2517
805fc799
AM
2518 if (!bfd_is_com_section (symbol->section))
2519 target = symbol->value;
2520 target += symbol->section->output_section->vma;
2521 target += symbol->section->output_offset;
2522 target += reloc_entry->addend;
5bd4f169 2523
805fc799
AM
2524 from = (reloc_entry->address
2525 + input_section->output_offset
2526 + input_section->output_section->vma);
5bd4f169 2527
805fc799
AM
2528 /* Invert 'y' bit if not the default. */
2529 if ((bfd_signed_vma) (target - from) < 0)
2530 insn ^= 0x01 << 21;
2531 }
4ce794b7 2532 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2533 out:
2534 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2535 input_section, output_bfd, error_message);
805fc799 2536}
5bd4f169 2537
805fc799 2538static bfd_reloc_status_type
4ce794b7
AM
2539ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2540 void *data, asection *input_section,
2541 bfd *output_bfd, char **error_message)
805fc799
AM
2542{
2543 /* If this is a relocatable link (output_bfd test tells us), just
2544 call the generic function. Any adjustment will be done at final
2545 link time. */
2546 if (output_bfd != NULL)
cedb70c5 2547 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2548 input_section, output_bfd, error_message);
5bd4f169 2549
805fc799
AM
2550 /* Subtract the symbol section base address. */
2551 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2552 return bfd_reloc_continue;
2553}
2554
805fc799 2555static bfd_reloc_status_type
4ce794b7
AM
2556ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2557 void *data, asection *input_section,
2558 bfd *output_bfd, char **error_message)
805fc799
AM
2559{
2560 /* If this is a relocatable link (output_bfd test tells us), just
2561 call the generic function. Any adjustment will be done at final
2562 link time. */
2563 if (output_bfd != NULL)
cedb70c5 2564 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2565 input_section, output_bfd, error_message);
2566
2567 /* Subtract the symbol section base address. */
2568 reloc_entry->addend -= symbol->section->output_section->vma;
2569
2570 /* Adjust the addend for sign extension of the low 16 bits. */
2571 reloc_entry->addend += 0x8000;
2572 return bfd_reloc_continue;
2573}
2574
2575static bfd_reloc_status_type
4ce794b7
AM
2576ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2577 void *data, asection *input_section,
2578 bfd *output_bfd, char **error_message)
805fc799
AM
2579{
2580 bfd_vma TOCstart;
2581
2582 /* If this is a relocatable link (output_bfd test tells us), just
2583 call the generic function. Any adjustment will be done at final
2584 link time. */
2585 if (output_bfd != NULL)
cedb70c5 2586 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2587 input_section, output_bfd, error_message);
2588
2589 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2590 if (TOCstart == 0)
1c865ab2 2591 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2592
2593 /* Subtract the TOC base address. */
2594 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2595 return bfd_reloc_continue;
2596}
2597
2598static bfd_reloc_status_type
4ce794b7
AM
2599ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2600 void *data, asection *input_section,
2601 bfd *output_bfd, char **error_message)
805fc799
AM
2602{
2603 bfd_vma TOCstart;
2604
2605 /* If this is a relocatable link (output_bfd test tells us), just
2606 call the generic function. Any adjustment will be done at final
2607 link time. */
2608 if (output_bfd != NULL)
cedb70c5 2609 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2610 input_section, output_bfd, error_message);
2611
2612 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2613 if (TOCstart == 0)
1c865ab2 2614 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2615
2616 /* Subtract the TOC base address. */
2617 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2618
2619 /* Adjust the addend for sign extension of the low 16 bits. */
2620 reloc_entry->addend += 0x8000;
2621 return bfd_reloc_continue;
2622}
2623
2624static bfd_reloc_status_type
4ce794b7
AM
2625ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2626 void *data, asection *input_section,
2627 bfd *output_bfd, char **error_message)
805fc799
AM
2628{
2629 bfd_vma TOCstart;
2630 bfd_size_type octets;
2631
2632 /* If this is a relocatable link (output_bfd test tells us), just
2633 call the generic function. Any adjustment will be done at final
2634 link time. */
2635 if (output_bfd != NULL)
cedb70c5 2636 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2637 input_section, output_bfd, error_message);
2638
2639 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2640 if (TOCstart == 0)
1c865ab2 2641 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2642
2643 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2644 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2645 return bfd_reloc_ok;
2646}
2647
2648static bfd_reloc_status_type
4ce794b7
AM
2649ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2650 void *data, asection *input_section,
2651 bfd *output_bfd, char **error_message)
805fc799
AM
2652{
2653 /* If this is a relocatable link (output_bfd test tells us), just
2654 call the generic function. Any adjustment will be done at final
2655 link time. */
2656 if (output_bfd != NULL)
cedb70c5 2657 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2658 input_section, output_bfd, error_message);
2659
2660 if (error_message != NULL)
2661 {
2662 static char buf[60];
2663 sprintf (buf, "generic linker can't handle %s",
2664 reloc_entry->howto->name);
2665 *error_message = buf;
2666 }
2667 return bfd_reloc_dangerous;
2668}
2669
927be08e
AM
2670/* Track GOT entries needed for a given symbol. We might need more
2671 than one got entry per symbol. */
2672struct got_entry
2673{
2674 struct got_entry *next;
2675
2676 /* The symbol addend that we'll be placing in the GOT. */
2677 bfd_vma addend;
2678
2679 /* Unlike other ELF targets, we use separate GOT entries for the same
2680 symbol referenced from different input files. This is to support
2681 automatic multiple TOC/GOT sections, where the TOC base can vary
2682 from one input file to another. After partitioning into TOC groups
2683 we merge entries within the group.
2684
2685 Point to the BFD owning this GOT entry. */
2686 bfd *owner;
2687
2688 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2689 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2690 unsigned char tls_type;
927be08e
AM
2691
2692 /* Non-zero if got.ent points to real entry. */
f961d9dd 2693 unsigned char is_indirect;
927be08e
AM
2694
2695 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2696 union
2697 {
2698 bfd_signed_vma refcount;
2699 bfd_vma offset;
2700 struct got_entry *ent;
2701 } got;
2702};
2703
2704/* The same for PLT. */
2705struct plt_entry
2706{
2707 struct plt_entry *next;
2708
2709 bfd_vma addend;
2710
2711 union
2712 {
2713 bfd_signed_vma refcount;
2714 bfd_vma offset;
2715 } plt;
2716};
2717
e717da7e
AM
2718struct ppc64_elf_obj_tdata
2719{
2720 struct elf_obj_tdata elf;
2721
2722 /* Shortcuts to dynamic linker sections. */
2723 asection *got;
2724 asection *relgot;
2725
b3fac117
AM
2726 /* Used during garbage collection. We attach global symbols defined
2727 on removed .opd entries to this section so that the sym is removed. */
2728 asection *deleted_section;
81688140 2729
927be08e 2730 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2731 sections means we potentially need one of these for each input bfd. */
927be08e 2732 struct got_entry tlsld_got;
8860955f 2733
729eabd5
AM
2734 union {
2735 /* A copy of relocs before they are modified for --emit-relocs. */
2736 Elf_Internal_Rela *relocs;
2737
2738 /* Section contents. */
2739 bfd_byte *contents;
2740 } opd;
d77c8a4b
AM
2741
2742 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2743 the reloc to be in the range -32768 to 32767. */
98528052
AM
2744 unsigned int has_small_toc_reloc : 1;
2745
560c8763
AM
2746 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2747 instruction not one we handle. */
2748 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2749};
2750
2751#define ppc64_elf_tdata(bfd) \
2752 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2753
2754#define ppc64_tlsld_got(bfd) \
2755 (&ppc64_elf_tdata (bfd)->tlsld_got)
2756
0c8d6e5c
AM
2757#define is_ppc64_elf(bfd) \
2758 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2759 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2760
e717da7e
AM
2761/* Override the generic function because we store some extras. */
2762
2763static bfd_boolean
2764ppc64_elf_mkobject (bfd *abfd)
2765{
0ffa91dd 2766 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2767 PPC64_ELF_DATA);
e717da7e
AM
2768}
2769
feee612b
AM
2770/* Fix bad default arch selected for a 64 bit input bfd when the
2771 default is 32 bit. */
2772
b34976b6 2773static bfd_boolean
4ce794b7 2774ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2775{
2776 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2777 {
2778 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2779
2780 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2781 {
2782 /* Relies on arch after 32 bit default being 64 bit default. */
2783 abfd->arch_info = abfd->arch_info->next;
2784 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2785 }
2786 }
b34976b6 2787 return TRUE;
feee612b
AM
2788}
2789
d37c89e5
AM
2790/* Support for core dump NOTE sections. */
2791
2792static bfd_boolean
2793ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2794{
eea6121a 2795 size_t offset, size;
d37c89e5
AM
2796
2797 if (note->descsz != 504)
2798 return FALSE;
2799
2800 /* pr_cursig */
228e534f 2801 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2802
2803 /* pr_pid */
228e534f 2804 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2805
2806 /* pr_reg */
2807 offset = 112;
eea6121a 2808 size = 384;
d37c89e5
AM
2809
2810 /* Make a ".reg/999" section. */
2811 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2812 size, note->descpos + offset);
d37c89e5
AM
2813}
2814
2815static bfd_boolean
2816ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2817{
2818 if (note->descsz != 136)
2819 return FALSE;
2820
228e534f 2821 elf_tdata (abfd)->core->pid
bc989cdc 2822 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2823 elf_tdata (abfd)->core->program
d37c89e5 2824 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2825 elf_tdata (abfd)->core->command
d37c89e5
AM
2826 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2827
2828 return TRUE;
2829}
2830
183e98be
AM
2831static char *
2832ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2833 ...)
2834{
2835 switch (note_type)
2836 {
2837 default:
2838 return NULL;
2839
2840 case NT_PRPSINFO:
2841 {
2842 char data[136];
2843 va_list ap;
2844
2845 va_start (ap, note_type);
75cd47ed 2846 memset (data, 0, sizeof (data));
183e98be
AM
2847 strncpy (data + 40, va_arg (ap, const char *), 16);
2848 strncpy (data + 56, va_arg (ap, const char *), 80);
2849 va_end (ap);
2850 return elfcore_write_note (abfd, buf, bufsiz,
2851 "CORE", note_type, data, sizeof (data));
2852 }
2853
2854 case NT_PRSTATUS:
2855 {
2856 char data[504];
2857 va_list ap;
2858 long pid;
2859 int cursig;
2860 const void *greg;
2861
2862 va_start (ap, note_type);
2863 memset (data, 0, 112);
2864 pid = va_arg (ap, long);
2865 bfd_put_32 (abfd, pid, data + 32);
2866 cursig = va_arg (ap, int);
2867 bfd_put_16 (abfd, cursig, data + 12);
2868 greg = va_arg (ap, const void *);
2869 memcpy (data + 112, greg, 384);
2870 memset (data + 496, 0, 8);
2871 va_end (ap);
2872 return elfcore_write_note (abfd, buf, bufsiz,
2873 "CORE", note_type, data, sizeof (data));
2874 }
2875 }
2876}
2877
5d35169e
AM
2878/* Add extra PPC sections. */
2879
b35d266b 2880static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2881{
0112cd26
NC
2882 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2883 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2884 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2885 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2886 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2887 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2888 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2889};
2890
7c8fe5c4
AM
2891enum _ppc64_sec_type {
2892 sec_normal = 0,
2893 sec_opd = 1,
2894 sec_toc = 2
2895};
2896
f0abc2a1
AM
2897struct _ppc64_elf_section_data
2898{
2899 struct bfd_elf_section_data elf;
411e1bfb 2900
f0abc2a1
AM
2901 union
2902 {
74f0fb50
AM
2903 /* An array with one entry for each opd function descriptor. */
2904 struct _opd_sec_data
2905 {
2906 /* Points to the function code section for local opd entries. */
2907 asection **func_sec;
2908
2909 /* After editing .opd, adjust references to opd local syms. */
2910 long *adjust;
2911 } opd;
7c8fe5c4 2912
3a71aa26
AM
2913 /* An array for toc sections, indexed by offset/8. */
2914 struct _toc_sec_data
2915 {
2916 /* Specifies the relocation symbol index used at a given toc offset. */
2917 unsigned *symndx;
2918
2919 /* And the relocation addend. */
2920 bfd_vma *add;
2921 } toc;
7c8fe5c4
AM
2922 } u;
2923
2924 enum _ppc64_sec_type sec_type:2;
411e1bfb 2925
7c8fe5c4
AM
2926 /* Flag set when small branches are detected. Used to
2927 select suitable defaults for the stub group size. */
2928 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2929};
2930
2931#define ppc64_elf_section_data(sec) \
411e1bfb 2932 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2933
2934static bfd_boolean
4ce794b7 2935ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2936{
f592407e
AM
2937 if (!sec->used_by_bfd)
2938 {
2939 struct _ppc64_elf_section_data *sdata;
2940 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2941
f592407e
AM
2942 sdata = bfd_zalloc (abfd, amt);
2943 if (sdata == NULL)
2944 return FALSE;
2945 sec->used_by_bfd = sdata;
2946 }
f0abc2a1
AM
2947
2948 return _bfd_elf_new_section_hook (abfd, sec);
2949}
4025353c 2950
74f0fb50 2951static struct _opd_sec_data *
4025353c
AM
2952get_opd_info (asection * sec)
2953{
2954 if (sec != NULL
2955 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2956 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2957 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2958 return NULL;
2959}
ee67d69a
AM
2960
2961static inline int
2962abiversion (bfd *abfd)
2963{
2964 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2965}
2966
2967static inline void
2968set_abiversion (bfd *abfd, int ver)
2969{
2970 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2971 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2972}
90e3cdf2
JJ
2973\f
2974/* Parameters for the qsort hook. */
90e3cdf2
JJ
2975static bfd_boolean synthetic_relocatable;
2976
699733f6 2977/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2978
2979static int
2980compare_symbols (const void *ap, const void *bp)
2981{
2982 const asymbol *a = * (const asymbol **) ap;
2983 const asymbol *b = * (const asymbol **) bp;
2984
699733f6
AM
2985 /* Section symbols first. */
2986 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2987 return -1;
699733f6 2988 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2989 return 1;
2990
699733f6 2991 /* then .opd symbols. */
ffcfec52
AM
2992 if (strcmp (a->section->name, ".opd") == 0
2993 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2994 return -1;
ffcfec52
AM
2995 if (strcmp (a->section->name, ".opd") != 0
2996 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2997 return 1;
2998
699733f6 2999 /* then other code symbols. */
90e3cdf2
JJ
3000 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3001 == (SEC_CODE | SEC_ALLOC)
3002 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3003 != (SEC_CODE | SEC_ALLOC))
3004 return -1;
3005
3006 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3007 != (SEC_CODE | SEC_ALLOC)
3008 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3009 == (SEC_CODE | SEC_ALLOC))
3010 return 1;
3011
3012 if (synthetic_relocatable)
3013 {
3014 if (a->section->id < b->section->id)
3015 return -1;
3016
3017 if (a->section->id > b->section->id)
3018 return 1;
3019 }
3020
3021 if (a->value + a->section->vma < b->value + b->section->vma)
3022 return -1;
3023
3024 if (a->value + a->section->vma > b->value + b->section->vma)
3025 return 1;
3026
4d35a0aa
AM
3027 /* For syms with the same value, prefer strong dynamic global function
3028 syms over other syms. */
3029 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3030 return -1;
3031
3032 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3033 return 1;
3034
3035 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3036 return -1;
3037
3038 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3039 return 1;
3040
3041 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3042 return -1;
3043
3044 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3045 return 1;
3046
3047 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3048 return -1;
3049
3050 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3051 return 1;
3052
90e3cdf2
JJ
3053 return 0;
3054}
3055
699733f6 3056/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3057
699733f6
AM
3058static asymbol *
3059sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 3060{
699733f6 3061 long mid;
90e3cdf2 3062
699733f6
AM
3063 if (id == -1)
3064 {
3065 while (lo < hi)
3066 {
3067 mid = (lo + hi) >> 1;
3068 if (syms[mid]->value + syms[mid]->section->vma < value)
3069 lo = mid + 1;
3070 else if (syms[mid]->value + syms[mid]->section->vma > value)
3071 hi = mid;
3072 else
3073 return syms[mid];
3074 }
3075 }
3076 else
3077 {
3078 while (lo < hi)
3079 {
3080 mid = (lo + hi) >> 1;
3081 if (syms[mid]->section->id < id)
3082 lo = mid + 1;
3083 else if (syms[mid]->section->id > id)
3084 hi = mid;
3085 else if (syms[mid]->value < value)
3086 lo = mid + 1;
3087 else if (syms[mid]->value > value)
3088 hi = mid;
3089 else
3090 return syms[mid];
3091 }
3092 }
3093 return NULL;
90e3cdf2
JJ
3094}
3095
468392fb
AM
3096static bfd_boolean
3097section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3098{
3099 bfd_vma vma = *(bfd_vma *) ptr;
3100 return ((section->flags & SEC_ALLOC) != 0
3101 && section->vma <= vma
3102 && vma < section->vma + section->size);
3103}
3104
699733f6 3105/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 3106 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
3107
3108static long
a7535cf3
AM
3109ppc64_elf_get_synthetic_symtab (bfd *abfd,
3110 long static_count, asymbol **static_syms,
3111 long dyn_count, asymbol **dyn_syms,
c9727e01 3112 asymbol **ret)
90e3cdf2
JJ
3113{
3114 asymbol *s;
699733f6
AM
3115 long i;
3116 long count;
90e3cdf2 3117 char *names;
a7535cf3 3118 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3119 asection *opd = NULL;
90e3cdf2 3120 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3121 asymbol **syms;
ee67d69a 3122 int abi = abiversion (abfd);
90e3cdf2
JJ
3123
3124 *ret = NULL;
3125
ee67d69a
AM
3126 if (abi < 2)
3127 {
3128 opd = bfd_get_section_by_name (abfd, ".opd");
3129 if (opd == NULL && abi == 1)
3130 return 0;
3131 }
90e3cdf2 3132
a7535cf3 3133 symcount = static_count;
c9727e01 3134 if (!relocatable)
a7535cf3 3135 symcount += dyn_count;
90e3cdf2 3136 if (symcount == 0)
c9727e01 3137 return 0;
90e3cdf2 3138
a7535cf3
AM
3139 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3140 if (syms == NULL)
7356fed5 3141 return -1;
a7535cf3
AM
3142
3143 if (!relocatable && static_count != 0 && dyn_count != 0)
3144 {
3145 /* Use both symbol tables. */
3146 memcpy (syms, static_syms, static_count * sizeof (*syms));
3147 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3148 }
3149 else if (!relocatable && static_count == 0)
3150 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3151 else
3152 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3153
90e3cdf2 3154 synthetic_relocatable = relocatable;
595da8c5 3155 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3156
c9727e01
AM
3157 if (!relocatable && symcount > 1)
3158 {
3159 long j;
3160 /* Trim duplicate syms, since we may have merged the normal and
3161 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3162 different values, so trim any with the same value. */
c9727e01
AM
3163 for (i = 1, j = 1; i < symcount; ++i)
3164 if (syms[i - 1]->value + syms[i - 1]->section->vma
3165 != syms[i]->value + syms[i]->section->vma)
3166 syms[j++] = syms[i];
3167 symcount = j;
3168 }
3169
699733f6 3170 i = 0;
ffcfec52 3171 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3172 ++i;
3173 codesecsym = i;
90e3cdf2 3174
699733f6
AM
3175 for (; i < symcount; ++i)
3176 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3177 != (SEC_CODE | SEC_ALLOC))
3178 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3179 break;
3180 codesecsymend = i;
90e3cdf2 3181
699733f6
AM
3182 for (; i < symcount; ++i)
3183 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3184 break;
3185 secsymend = i;
90e3cdf2 3186
699733f6 3187 for (; i < symcount; ++i)
ffcfec52 3188 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3189 break;
3190 opdsymend = i;
90e3cdf2 3191
699733f6
AM
3192 for (; i < symcount; ++i)
3193 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3194 != (SEC_CODE | SEC_ALLOC))
3195 break;
3196 symcount = i;
3197
c9727e01 3198 count = 0;
90e3cdf2 3199
699733f6 3200 if (relocatable)
90e3cdf2 3201 {
699733f6
AM
3202 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3203 arelent *r;
3204 size_t size;
3205 long relcount;
90e3cdf2 3206
468392fb
AM
3207 if (opdsymend == secsymend)
3208 goto done;
3209
699733f6 3210 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3211 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3212 if (relcount == 0)
c9727e01 3213 goto done;
90e3cdf2 3214
7356fed5
AM
3215 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3216 {
3217 count = -1;
3218 goto done;
3219 }
3220
699733f6 3221 size = 0;
595da8c5 3222 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3223 {
3224 asymbol *sym;
90e3cdf2 3225
595da8c5 3226 while (r < opd->relocation + relcount
699733f6
AM
3227 && r->address < syms[i]->value + opd->vma)
3228 ++r;
90e3cdf2 3229
595da8c5 3230 if (r == opd->relocation + relcount)
699733f6 3231 break;
90e3cdf2 3232
699733f6
AM
3233 if (r->address != syms[i]->value + opd->vma)
3234 continue;
90e3cdf2 3235
699733f6
AM
3236 if (r->howto->type != R_PPC64_ADDR64)
3237 continue;
90e3cdf2 3238
699733f6
AM
3239 sym = *r->sym_ptr_ptr;
3240 if (!sym_exists_at (syms, opdsymend, symcount,
3241 sym->section->id, sym->value + r->addend))
3242 {
3243 ++count;
3244 size += sizeof (asymbol);
3245 size += strlen (syms[i]->name) + 2;
3246 }
3247 }
90e3cdf2 3248
699733f6
AM
3249 s = *ret = bfd_malloc (size);
3250 if (s == NULL)
3251 {
7356fed5 3252 count = -1;
c9727e01 3253 goto done;
699733f6 3254 }
90e3cdf2 3255
699733f6 3256 names = (char *) (s + count);
90e3cdf2 3257
595da8c5 3258 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3259 {
699733f6 3260 asymbol *sym;
90e3cdf2 3261
595da8c5 3262 while (r < opd->relocation + relcount
699733f6
AM
3263 && r->address < syms[i]->value + opd->vma)
3264 ++r;
90e3cdf2 3265
595da8c5 3266 if (r == opd->relocation + relcount)
699733f6
AM
3267 break;
3268
3269 if (r->address != syms[i]->value + opd->vma)
3270 continue;
3271
3272 if (r->howto->type != R_PPC64_ADDR64)
3273 continue;
90e3cdf2 3274
699733f6
AM
3275 sym = *r->sym_ptr_ptr;
3276 if (!sym_exists_at (syms, opdsymend, symcount,
3277 sym->section->id, sym->value + r->addend))
3278 {
3279 size_t len;
3280
3281 *s = *syms[i];
6ba2a415 3282 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3283 s->section = sym->section;
3284 s->value = sym->value + r->addend;
3285 s->name = names;
3286 *names++ = '.';
3287 len = strlen (syms[i]->name);
3288 memcpy (names, syms[i]->name, len + 1);
3289 names += len + 1;
6f610d07
UW
3290 /* Have udata.p point back to the original symbol this
3291 synthetic symbol was derived from. */
3292 s->udata.p = syms[i];
699733f6
AM
3293 s++;
3294 }
3295 }
3296 }
3297 else
90e3cdf2 3298 {
468392fb 3299 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3300 bfd_byte *contents = NULL;
699733f6 3301 size_t size;
468392fb
AM
3302 long plt_count = 0;
3303 bfd_vma glink_vma = 0, resolv_vma = 0;
3304 asection *dynamic, *glink = NULL, *relplt = NULL;
3305 arelent *p;
90e3cdf2 3306
ee67d69a 3307 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3308 {
ee67d69a 3309 free_contents_and_exit:
699733f6 3310 if (contents)
ee67d69a 3311 free (contents);
7356fed5 3312 count = -1;
c9727e01 3313 goto done;
699733f6 3314 }
90e3cdf2 3315
699733f6
AM
3316 size = 0;
3317 for (i = secsymend; i < opdsymend; ++i)
3318 {
3319 bfd_vma ent;
90e3cdf2 3320
5ef11c02
AM
3321 /* Ignore bogus symbols. */
3322 if (syms[i]->value > opd->size - 8)
3323 continue;
3324
699733f6
AM
3325 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3326 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3327 {
3328 ++count;
3329 size += sizeof (asymbol);
3330 size += strlen (syms[i]->name) + 2;
3331 }
3332 }
90e3cdf2 3333
468392fb 3334 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3335 if (dyn_count != 0
3336 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3337 {
3338 bfd_byte *dynbuf, *extdyn, *extdynend;
3339 size_t extdynsize;
3340 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3341
3342 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3343 goto free_contents_and_exit;
3344
3345 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3346 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3347
3348 extdyn = dynbuf;
3349 extdynend = extdyn + dynamic->size;
3350 for (; extdyn < extdynend; extdyn += extdynsize)
3351 {
3352 Elf_Internal_Dyn dyn;
3353 (*swap_dyn_in) (abfd, extdyn, &dyn);
3354
3355 if (dyn.d_tag == DT_NULL)
3356 break;
3357
3358 if (dyn.d_tag == DT_PPC64_GLINK)
3359 {
b9e5796b
AM
3360 /* The first glink stub starts at offset 32; see
3361 comment in ppc64_elf_finish_dynamic_sections. */
3362 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3363 /* The .glink section usually does not survive the final
3364 link; search for the section (usually .text) where the
3365 glink stubs now reside. */
3366 glink = bfd_sections_find_if (abfd, section_covers_vma,
3367 &glink_vma);
3368 break;
3369 }
3370 }
3371
3372 free (dynbuf);
3373 }
3374
3375 if (glink != NULL)
3376 {
3377 /* Determine __glink trampoline by reading the relative branch
3378 from the first glink stub. */
3379 bfd_byte buf[4];
b9e5796b
AM
3380 unsigned int off = 0;
3381
3382 while (bfd_get_section_contents (abfd, glink, buf,
3383 glink_vma + off - glink->vma, 4))
468392fb
AM
3384 {
3385 unsigned int insn = bfd_get_32 (abfd, buf);
3386 insn ^= B_DOT;
3387 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3388 {
3389 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3390 break;
3391 }
3392 off += 4;
3393 if (off > 4)
3394 break;
468392fb
AM
3395 }
3396
3397 if (resolv_vma)
3398 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3399
066ee829
AM
3400 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3401 if (relplt != NULL)
3402 {
3403 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3404 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3405 goto free_contents_and_exit;
68ffbac6 3406
066ee829
AM
3407 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3408 size += plt_count * sizeof (asymbol);
468392fb 3409
066ee829
AM
3410 p = relplt->relocation;
3411 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3412 {
3413 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3414 if (p->addend != 0)
3415 size += sizeof ("+0x") - 1 + 16;
3416 }
066ee829 3417 }
468392fb
AM
3418 }
3419
699733f6
AM
3420 s = *ret = bfd_malloc (size);
3421 if (s == NULL)
7356fed5 3422 goto free_contents_and_exit;
90e3cdf2 3423
468392fb 3424 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3425
699733f6 3426 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3427 {
699733f6 3428 bfd_vma ent;
90e3cdf2 3429
5ef11c02
AM
3430 if (syms[i]->value > opd->size - 8)
3431 continue;
3432
699733f6
AM
3433 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3434 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3435 {
c9727e01 3436 long lo, hi;
699733f6 3437 size_t len;
c9727e01 3438 asection *sec = abfd->sections;
90e3cdf2 3439
699733f6
AM
3440 *s = *syms[i];
3441 lo = codesecsym;
3442 hi = codesecsymend;
3443 while (lo < hi)
3444 {
c9727e01 3445 long mid = (lo + hi) >> 1;
699733f6
AM
3446 if (syms[mid]->section->vma < ent)
3447 lo = mid + 1;
3448 else if (syms[mid]->section->vma > ent)
3449 hi = mid;
3450 else
c9727e01
AM
3451 {
3452 sec = syms[mid]->section;
3453 break;
3454 }
699733f6
AM
3455 }
3456
c9727e01 3457 if (lo >= hi && lo > codesecsym)
699733f6 3458 sec = syms[lo - 1]->section;
699733f6
AM
3459
3460 for (; sec != NULL; sec = sec->next)
3461 {
3462 if (sec->vma > ent)
3463 break;
63524580
JK
3464 /* SEC_LOAD may not be set if SEC is from a separate debug
3465 info file. */
3466 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3467 break;
3468 if ((sec->flags & SEC_CODE) != 0)
3469 s->section = sec;
3470 }
6ba2a415 3471 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3472 s->value = ent - s->section->vma;
3473 s->name = names;
3474 *names++ = '.';
3475 len = strlen (syms[i]->name);
3476 memcpy (names, syms[i]->name, len + 1);
3477 names += len + 1;
6f610d07
UW
3478 /* Have udata.p point back to the original symbol this
3479 synthetic symbol was derived from. */
3480 s->udata.p = syms[i];
699733f6 3481 s++;
90e3cdf2 3482 }
90e3cdf2 3483 }
699733f6 3484 free (contents);
468392fb
AM
3485
3486 if (glink != NULL && relplt != NULL)
3487 {
3488 if (resolv_vma)
3489 {
3490 /* Add a symbol for the main glink trampoline. */
86a4952b 3491 memset (s, 0, sizeof *s);
468392fb 3492 s->the_bfd = abfd;
6ba2a415 3493 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3494 s->section = glink;
3495 s->value = resolv_vma - glink->vma;
3496 s->name = names;
3497 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3498 names += sizeof ("__glink_PLTresolve");
3499 s++;
3500 count++;
3501 }
3502
3503 /* FIXME: It would be very much nicer to put sym@plt on the
3504 stub rather than on the glink branch table entry. The
3505 objdump disassembler would then use a sensible symbol
3506 name on plt calls. The difficulty in doing so is
3507 a) finding the stubs, and,
3508 b) matching stubs against plt entries, and,
3509 c) there can be multiple stubs for a given plt entry.
3510
3511 Solving (a) could be done by code scanning, but older
3512 ppc64 binaries used different stubs to current code.
3513 (b) is the tricky one since you need to known the toc
3514 pointer for at least one function that uses a pic stub to
3515 be able to calculate the plt address referenced.
3516 (c) means gdb would need to set multiple breakpoints (or
3517 find the glink branch itself) when setting breakpoints
3518 for pending shared library loads. */
3519 p = relplt->relocation;
3520 for (i = 0; i < plt_count; i++, p++)
3521 {
3522 size_t len;
3523
3524 *s = **p->sym_ptr_ptr;
3525 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3526 we are defining a symbol, ensure one of them is set. */
3527 if ((s->flags & BSF_LOCAL) == 0)
3528 s->flags |= BSF_GLOBAL;
6ba2a415 3529 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3530 s->section = glink;
3531 s->value = glink_vma - glink->vma;
3532 s->name = names;
3533 s->udata.p = NULL;
3534 len = strlen ((*p->sym_ptr_ptr)->name);
3535 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3536 names += len;
e054468f
AM
3537 if (p->addend != 0)
3538 {
3539 memcpy (names, "+0x", sizeof ("+0x") - 1);
3540 names += sizeof ("+0x") - 1;
3541 bfd_sprintf_vma (abfd, names, p->addend);
3542 names += strlen (names);
3543 }
468392fb
AM
3544 memcpy (names, "@plt", sizeof ("@plt"));
3545 names += sizeof ("@plt");
3546 s++;
b9e5796b
AM
3547 if (abi < 2)
3548 {
3549 glink_vma += 8;
3550 if (i >= 0x8000)
3551 glink_vma += 4;
3552 }
3553 else
468392fb
AM
3554 glink_vma += 4;
3555 }
3556 count += plt_count;
3557 }
90e3cdf2
JJ
3558 }
3559
c9727e01 3560 done:
a7535cf3 3561 free (syms);
90e3cdf2
JJ
3562 return count;
3563}
5bd4f169 3564\f
65f38f15
AM
3565/* The following functions are specific to the ELF linker, while
3566 functions above are used generally. Those named ppc64_elf_* are
3567 called by the main ELF linker code. They appear in this file more
3568 or less in the order in which they are called. eg.
3569 ppc64_elf_check_relocs is called early in the link process,
3570 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3571 called.
3572
3573 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3574 functions have both a function code symbol and a function descriptor
3575 symbol. A call to foo in a relocatable object file looks like:
3576
3577 . .text
3578 . x:
3579 . bl .foo
3580 . nop
3581
3582 The function definition in another object file might be:
3583
3584 . .section .opd
3585 . foo: .quad .foo
3586 . .quad .TOC.@tocbase
3587 . .quad 0
3588 .
3589 . .text
3590 . .foo: blr
3591
3592 When the linker resolves the call during a static link, the branch
3593 unsurprisingly just goes to .foo and the .opd information is unused.
3594 If the function definition is in a shared library, things are a little
3595 different: The call goes via a plt call stub, the opd information gets
3596 copied to the plt, and the linker patches the nop.
3597
3598 . x:
3599 . bl .foo_stub
3600 . ld 2,40(1)
3601 .
3602 .
3603 . .foo_stub:
71a39c98
AM
3604 . std 2,40(1) # in practice, the call stub
3605 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3606 . addi 11,11,Lfoo@toc@l # this is the general idea
3607 . ld 12,0(11)
3608 . ld 2,8(11)
3609 . mtctr 12
3610 . ld 11,16(11)
e86ce104
AM
3611 . bctr
3612 .
3613 . .section .plt
3614 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3615
3616 The "reloc ()" notation is supposed to indicate that the linker emits
3617 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3618 copying.
3619
3620 What are the difficulties here? Well, firstly, the relocations
3621 examined by the linker in check_relocs are against the function code
3622 sym .foo, while the dynamic relocation in the plt is emitted against
3623 the function descriptor symbol, foo. Somewhere along the line, we need
3624 to carefully copy dynamic link information from one symbol to the other.
3625 Secondly, the generic part of the elf linker will make .foo a dynamic
3626 symbol as is normal for most other backends. We need foo dynamic
3627 instead, at least for an application final link. However, when
3628 creating a shared library containing foo, we need to have both symbols
3629 dynamic so that references to .foo are satisfied during the early
3630 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3631 definition from some other object, eg. a static library.
3632
3633 Update: As of August 2004, we support a new convention. Function
3634 calls may use the function descriptor symbol, ie. "bl foo". This
3635 behaves exactly as "bl .foo". */
65f38f15 3636
1d483afe 3637/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3638 selects those that must be copied when linking a shared library,
3639 even when the symbol is local. */
65f38f15 3640
1d483afe
AM
3641static int
3642must_be_dyn_reloc (struct bfd_link_info *info,
3643 enum elf_ppc64_reloc_type r_type)
3644{
3645 switch (r_type)
3646 {
3647 default:
3648 return 1;
3649
3650 case R_PPC64_REL32:
3651 case R_PPC64_REL64:
3652 case R_PPC64_REL30:
3653 return 0;
3654
3655 case R_PPC64_TPREL16:
3656 case R_PPC64_TPREL16_LO:
3657 case R_PPC64_TPREL16_HI:
3658 case R_PPC64_TPREL16_HA:
3659 case R_PPC64_TPREL16_DS:
3660 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3661 case R_PPC64_TPREL16_HIGH:
3662 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3663 case R_PPC64_TPREL16_HIGHER:
3664 case R_PPC64_TPREL16_HIGHERA:
3665 case R_PPC64_TPREL16_HIGHEST:
3666 case R_PPC64_TPREL16_HIGHESTA:
3667 case R_PPC64_TPREL64:
3668 return !info->executable;
3669 }
3670}
65f38f15 3671
f4656909
AM
3672/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3673 copying dynamic variables from a shared lib into an app's dynbss
3674 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3675 shared lib. With code that gcc generates, it's vital that this be
3676 enabled; In the PowerPC64 ABI, the address of a function is actually
3677 the address of a function descriptor, which resides in the .opd
3678 section. gcc uses the descriptor directly rather than going via the
3679 GOT as some other ABI's do, which means that initialized function
3680 pointers must reference the descriptor. Thus, a function pointer
3681 initialized to the address of a function in a shared library will
3682 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3683 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3684 presents a problem as a plt entry for that function is also
3685 initialized from the function descriptor symbol and the copy reloc
3686 may not be initialized first. */
a23b6845 3687#define ELIMINATE_COPY_RELOCS 1
f4656909 3688
721956f4 3689/* Section name for stubs is the associated section name plus this
29942be8
NC
3690 string. */
3691#define STUB_SUFFIX ".stub"
721956f4
AM
3692
3693/* Linker stubs.
3694 ppc_stub_long_branch:
3695 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3696 destination, but a 24 bit branch in a stub section will reach.
3697 . b dest
3698
3699 ppc_stub_plt_branch:
3700 Similar to the above, but a 24 bit branch in the stub section won't
3701 reach its destination.
71a39c98
AM
3702 . addis %r11,%r2,xxx@toc@ha
3703 . ld %r12,xxx@toc@l(%r11)
3704 . mtctr %r12
721956f4
AM
3705 . bctr
3706
3707 ppc_stub_plt_call:
2c66dc6c
AM
3708 Used to call a function in a shared library. If it so happens that
3709 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3710 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3711 . std %r2,40(%r1)
71a39c98
AM
3712 . addis %r11,%r2,xxx@toc@ha
3713 . ld %r12,xxx+0@toc@l(%r11)
3714 . mtctr %r12
3715 . ld %r2,xxx+8@toc@l(%r11)
3716 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3717 . bctr
ad8e1ba5
AM
3718
3719 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3720 code to adjust the value and save r2 to support multiple toc sections.
3721 A ppc_stub_long_branch with an r2 offset looks like:
3722 . std %r2,40(%r1)
3723 . addis %r2,%r2,off@ha
3724 . addi %r2,%r2,off@l
3725 . b dest
3726
3727 A ppc_stub_plt_branch with an r2 offset looks like:
3728 . std %r2,40(%r1)
71a39c98
AM
3729 . addis %r11,%r2,xxx@toc@ha
3730 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3731 . addis %r2,%r2,off@ha
3732 . addi %r2,%r2,off@l
71a39c98 3733 . mtctr %r12
ad8e1ba5 3734 . bctr
ac2df442
AM
3735
3736 In cases where the "addis" instruction would add zero, the "addis" is
3737 omitted and following instructions modified slightly in some cases.
721956f4
AM
3738*/
3739
3740enum ppc_stub_type {
3741 ppc_stub_none,
3742 ppc_stub_long_branch,
ad8e1ba5 3743 ppc_stub_long_branch_r2off,
721956f4 3744 ppc_stub_plt_branch,
ad8e1ba5 3745 ppc_stub_plt_branch_r2off,
794e51c0
AM
3746 ppc_stub_plt_call,
3747 ppc_stub_plt_call_r2save
721956f4
AM
3748};
3749
3750struct ppc_stub_hash_entry {
3751
3752 /* Base hash table entry structure. */
3753 struct bfd_hash_entry root;
3754
ad8e1ba5
AM
3755 enum ppc_stub_type stub_type;
3756
721956f4
AM
3757 /* The stub section. */
3758 asection *stub_sec;
3759
3760 /* Offset within stub_sec of the beginning of this stub. */
3761 bfd_vma stub_offset;
3762
3763 /* Given the symbol's value and its section we can determine its final
3764 value when building the stubs (so the stub knows where to jump. */
3765 bfd_vma target_value;
3766 asection *target_section;
3767
721956f4
AM
3768 /* The symbol table entry, if any, that this was derived from. */
3769 struct ppc_link_hash_entry *h;
e054468f 3770 struct plt_entry *plt_ent;
721956f4
AM
3771
3772 /* Where this stub is being called from, or, in the case of combined
3773 stub sections, the first input section in the group. */
3774 asection *id_sec;
6911b7dc
AM
3775
3776 /* Symbol st_other. */
3777 unsigned char other;
721956f4
AM
3778};
3779
3780struct ppc_branch_hash_entry {
3781
3782 /* Base hash table entry structure. */
3783 struct bfd_hash_entry root;
3784
c456f082 3785 /* Offset within branch lookup table. */
721956f4
AM
3786 unsigned int offset;
3787
3788 /* Generation marker. */
3789 unsigned int iter;
3790};
65f38f15 3791
19e08130
AM
3792/* Used to track dynamic relocations for local symbols. */
3793struct ppc_dyn_relocs
3794{
3795 struct ppc_dyn_relocs *next;
3796
3797 /* The input section of the reloc. */
3798 asection *sec;
3799
3800 /* Total number of relocs copied for the input section. */
3801 unsigned int count : 31;
3802
3803 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3804 unsigned int ifunc : 1;
3805};
3806
65f38f15
AM
3807struct ppc_link_hash_entry
3808{
3809 struct elf_link_hash_entry elf;
3810
b3fac117
AM
3811 union {
3812 /* A pointer to the most recently used stub hash entry against this
3813 symbol. */
3814 struct ppc_stub_hash_entry *stub_cache;
3815
3816 /* A pointer to the next symbol starting with a '.' */
3817 struct ppc_link_hash_entry *next_dot_sym;
3818 } u;
721956f4 3819
65f38f15 3820 /* Track dynamic relocs copied for this symbol. */
6061a67d 3821 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3822
721956f4 3823 /* Link between function code and descriptor symbols. */
34814b9f 3824 struct ppc_link_hash_entry *oh;
721956f4 3825
e86ce104
AM
3826 /* Flag function code and descriptor symbols. */
3827 unsigned int is_func:1;
3828 unsigned int is_func_descriptor:1;
908b32fc 3829 unsigned int fake:1;
411e1bfb 3830
c5614fa4
AM
3831 /* Whether global opd/toc sym has been adjusted or not.
3832 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3833 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3834 unsigned int adjust_done:1;
3835
99877b66
AM
3836 /* Set if we twiddled this symbol to weak at some stage. */
3837 unsigned int was_undefined:1;
3838
411e1bfb 3839 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3840 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3841 corresponding relocs are encountered during check_relocs.
3842 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3843 indicate the corresponding GOT entry type is not needed.
3844 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3845 a TPREL one. We use a separate flag rather than setting TPREL
3846 just for convenience in distinguishing the two cases. */
3847#define TLS_GD 1 /* GD reloc. */
3848#define TLS_LD 2 /* LD reloc. */
3849#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3850#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3851#define TLS_TLS 16 /* Any TLS reloc. */
3852#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3853#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3854#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3855 unsigned char tls_mask;
65f38f15
AM
3856};
3857
3858/* ppc64 ELF linker hash table. */
3859
3860struct ppc_link_hash_table
3861{
3862 struct elf_link_hash_table elf;
3863
721956f4
AM
3864 /* The stub hash table. */
3865 struct bfd_hash_table stub_hash_table;
3866
3867 /* Another hash table for plt_branch stubs. */
3868 struct bfd_hash_table branch_hash_table;
3869
3b421ab3
AM
3870 /* Hash table for function prologue tocsave. */
3871 htab_t tocsave_htab;
3872
721956f4
AM
3873 /* Linker stub bfd. */
3874 bfd *stub_bfd;
3875
3876 /* Linker call-backs. */
4ce794b7
AM
3877 asection * (*add_stub_section) (const char *, asection *);
3878 void (*layout_sections_again) (void);
721956f4
AM
3879
3880 /* Array to keep track of which stub sections have been created, and
3881 information on stub grouping. */
3882 struct map_stub {
3883 /* This is the section to which stubs in the group will be attached. */
3884 asection *link_sec;
3885 /* The stub section. */
3886 asection *stub_sec;
ad8e1ba5
AM
3887 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3888 bfd_vma toc_off;
721956f4
AM
3889 } *stub_group;
3890
ad8e1ba5
AM
3891 /* Temp used when calculating TOC pointers. */
3892 bfd_vma toc_curr;
bf102f86
AM
3893 bfd *toc_bfd;
3894 asection *toc_first_sec;
ad8e1ba5 3895
8f3bab57
AM
3896 /* Highest input section id. */
3897 int top_id;
3898
734b6cf9
AM
3899 /* Highest output section index. */
3900 int top_index;
3901
b3fac117
AM
3902 /* Used when adding symbols. */
3903 struct ppc_link_hash_entry *dot_syms;
3904
734b6cf9
AM
3905 /* List of input sections for each output section. */
3906 asection **input_list;
721956f4 3907
65f38f15 3908 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3909 asection *got;
4ce794b7
AM
3910 asection *plt;
3911 asection *relplt;
e054468f
AM
3912 asection *iplt;
3913 asection *reliplt;
4ce794b7
AM
3914 asection *dynbss;
3915 asection *relbss;
3916 asection *glink;
82bd7b59 3917 asection *sfpr;
4ce794b7
AM
3918 asection *brlt;
3919 asection *relbrlt;
58d180e8 3920 asection *glink_eh_frame;
ec338859 3921
8387904d
AM
3922 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3923 struct ppc_link_hash_entry *tls_get_addr;
3924 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3925
927be08e
AM
3926 /* The size of reliplt used by got entry relocs. */
3927 bfd_size_type got_reli_size;
3928
9b5ecbd0 3929 /* Statistics. */
794e51c0 3930 unsigned long stub_count[ppc_stub_plt_call_r2save];
9b5ecbd0 3931
ee75fd95
AM
3932 /* Number of stubs against global syms. */
3933 unsigned long stub_globals;
3934
794e51c0
AM
3935 /* Alignment of PLT call stubs. */
3936 unsigned int plt_stub_align:4;
3937
ee67d69a
AM
3938 /* Set if we're linking code with function descriptors. */
3939 unsigned int opd_abi:1;
3940
9df0ef5f
AM
3941 /* Set if PLT call stubs should load r11. */
3942 unsigned int plt_static_chain:1;
3943
794e51c0
AM
3944 /* Set if PLT call stubs need a read-read barrier. */
3945 unsigned int plt_thread_safe:1;
3946
ad8e1ba5 3947 /* Set if we should emit symbols for stubs. */
99877b66 3948 unsigned int emit_stub_syms:1;
ad8e1ba5 3949
a7f2871e
AM
3950 /* Set if __tls_get_addr optimization should not be done. */
3951 unsigned int no_tls_get_addr_opt:1;
3952
4c52953f 3953 /* Support for multiple toc sections. */
33c0ec9d 3954 unsigned int do_multi_toc:1;
4c52953f 3955 unsigned int multi_toc_needed:1;
927be08e 3956 unsigned int second_toc_pass:1;
67f0cbdb 3957 unsigned int do_toc_opt:1;
4c52953f 3958
5d1634d7 3959 /* Set on error. */
99877b66 3960 unsigned int stub_error:1;
721956f4 3961
7d9616d7 3962 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3963 unsigned int twiddled_syms:1;
721956f4
AM
3964
3965 /* Incremented every time we size stubs. */
3966 unsigned int stub_iteration;
5d1634d7 3967
87d72d41
AM
3968 /* Small local sym cache. */
3969 struct sym_cache sym_cache;
65f38f15
AM
3970};
3971
4c52953f
AM
3972/* Rename some of the generic section flags to better document how they
3973 are used here. */
b0dddeec
AM
3974
3975/* Nonzero if this section has TLS related relocations. */
3976#define has_tls_reloc sec_flg0
3977
3978/* Nonzero if this section has a call to __tls_get_addr. */
3979#define has_tls_get_addr_call sec_flg1
3980
3981/* Nonzero if this section has any toc or got relocs. */
3982#define has_toc_reloc sec_flg2
3983
3984/* Nonzero if this section has a call to another section that uses
3985 the toc or got. */
d77c8a4b 3986#define makes_toc_func_call sec_flg3
b0dddeec
AM
3987
3988/* Recursion protection when determining above flag. */
d77c8a4b 3989#define call_check_in_progress sec_flg4
70cc837d 3990#define call_check_done sec_flg5
4c52953f 3991
65f38f15
AM
3992/* Get the ppc64 ELF linker hash table from a link_info structure. */
3993
3994#define ppc_hash_table(p) \
4dfe6ac6
NC
3995 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3996 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3997
721956f4
AM
3998#define ppc_stub_hash_lookup(table, string, create, copy) \
3999 ((struct ppc_stub_hash_entry *) \
4000 bfd_hash_lookup ((table), (string), (create), (copy)))
4001
4002#define ppc_branch_hash_lookup(table, string, create, copy) \
4003 ((struct ppc_branch_hash_entry *) \
4004 bfd_hash_lookup ((table), (string), (create), (copy)))
4005
4006/* Create an entry in the stub hash table. */
4007
4008static struct bfd_hash_entry *
4ce794b7
AM
4009stub_hash_newfunc (struct bfd_hash_entry *entry,
4010 struct bfd_hash_table *table,
4011 const char *string)
721956f4
AM
4012{
4013 /* Allocate the structure if it has not already been allocated by a
4014 subclass. */
4015 if (entry == NULL)
4016 {
4017 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4018 if (entry == NULL)
4019 return entry;
4020 }
4021
4022 /* Call the allocation method of the superclass. */
4023 entry = bfd_hash_newfunc (entry, table, string);
4024 if (entry != NULL)
4025 {
4026 struct ppc_stub_hash_entry *eh;
4027
4028 /* Initialize the local fields. */
4029 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4030 eh->stub_type = ppc_stub_none;
721956f4
AM
4031 eh->stub_sec = NULL;
4032 eh->stub_offset = 0;
4033 eh->target_value = 0;
4034 eh->target_section = NULL;
721956f4 4035 eh->h = NULL;
6911b7dc 4036 eh->plt_ent = NULL;
721956f4 4037 eh->id_sec = NULL;
6911b7dc 4038 eh->other = 0;
721956f4
AM
4039 }
4040
4041 return entry;
4042}
4043
4044/* Create an entry in the branch hash table. */
4045
4046static struct bfd_hash_entry *
4ce794b7
AM
4047branch_hash_newfunc (struct bfd_hash_entry *entry,
4048 struct bfd_hash_table *table,
4049 const char *string)
721956f4
AM
4050{
4051 /* Allocate the structure if it has not already been allocated by a
4052 subclass. */
4053 if (entry == NULL)
4054 {
4055 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4056 if (entry == NULL)
4057 return entry;
4058 }
4059
4060 /* Call the allocation method of the superclass. */
4061 entry = bfd_hash_newfunc (entry, table, string);
4062 if (entry != NULL)
4063 {
4064 struct ppc_branch_hash_entry *eh;
4065
4066 /* Initialize the local fields. */
4067 eh = (struct ppc_branch_hash_entry *) entry;
4068 eh->offset = 0;
4069 eh->iter = 0;
4070 }
4071
4072 return entry;
4073}
4074
65f38f15
AM
4075/* Create an entry in a ppc64 ELF linker hash table. */
4076
4077static struct bfd_hash_entry *
4ce794b7
AM
4078link_hash_newfunc (struct bfd_hash_entry *entry,
4079 struct bfd_hash_table *table,
4080 const char *string)
65f38f15
AM
4081{
4082 /* Allocate the structure if it has not already been allocated by a
4083 subclass. */
4084 if (entry == NULL)
4085 {
4086 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4087 if (entry == NULL)
4088 return entry;
4089 }
4090
4091 /* Call the allocation method of the superclass. */
4092 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4093 if (entry != NULL)
4094 {
4095 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4096
b3fac117 4097 memset (&eh->u.stub_cache, 0,
908b32fc 4098 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4099 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4100
4101 /* When making function calls, old ABI code references function entry
4102 points (dot symbols), while new ABI code references the function
4103 descriptor symbol. We need to make any combination of reference and
4104 definition work together, without breaking archive linking.
4105
4106 For a defined function "foo" and an undefined call to "bar":
4107 An old object defines "foo" and ".foo", references ".bar" (possibly
4108 "bar" too).
4109 A new object defines "foo" and references "bar".
4110
4111 A new object thus has no problem with its undefined symbols being
4112 satisfied by definitions in an old object. On the other hand, the
4113 old object won't have ".bar" satisfied by a new object.
4114
4115 Keep a list of newly added dot-symbols. */
4116
4117 if (string[0] == '.')
4118 {
4119 struct ppc_link_hash_table *htab;
4120
4121 htab = (struct ppc_link_hash_table *) table;
4122 eh->u.next_dot_sym = htab->dot_syms;
4123 htab->dot_syms = eh;
4124 }
65f38f15
AM
4125 }
4126
4127 return entry;
4128}
4129
3b421ab3
AM
4130struct tocsave_entry {
4131 asection *sec;
4132 bfd_vma offset;
4133};
4134
4135static hashval_t
4136tocsave_htab_hash (const void *p)
4137{
4138 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4139 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4140}
4141
4142static int
4143tocsave_htab_eq (const void *p1, const void *p2)
4144{
4145 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4146 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4147 return e1->sec == e2->sec && e1->offset == e2->offset;
4148}
4149
65f38f15
AM
4150/* Create a ppc64 ELF linker hash table. */
4151
4152static struct bfd_link_hash_table *
4ce794b7 4153ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4154{
4155 struct ppc_link_hash_table *htab;
4156 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4157
4ce794b7 4158 htab = bfd_zmalloc (amt);
65f38f15
AM
4159 if (htab == NULL)
4160 return NULL;
4161
66eb6687 4162 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4163 sizeof (struct ppc_link_hash_entry),
4164 PPC64_ELF_DATA))
65f38f15 4165 {
e2d34d7d 4166 free (htab);
65f38f15
AM
4167 return NULL;
4168 }
4169
721956f4 4170 /* Init the stub hash table too. */
66eb6687
AM
4171 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4172 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
4173 return NULL;
4174
4175 /* And the branch hash table. */
66eb6687
AM
4176 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4177 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
4178 return NULL;
4179
3b421ab3
AM
4180 htab->tocsave_htab = htab_try_create (1024,
4181 tocsave_htab_hash,
4182 tocsave_htab_eq,
4183 NULL);
4184 if (htab->tocsave_htab == NULL)
4185 return NULL;
4186
3254fd24
AM
4187 /* Initializing two fields of the union is just cosmetic. We really
4188 only care about glist, but when compiled on a 32-bit host the
4189 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4190 debugger inspection of these fields look nicer. */
a6aa5195
AM
4191 htab->elf.init_got_refcount.refcount = 0;
4192 htab->elf.init_got_refcount.glist = NULL;
4193 htab->elf.init_plt_refcount.refcount = 0;
4194 htab->elf.init_plt_refcount.glist = NULL;
4195 htab->elf.init_got_offset.offset = 0;
4196 htab->elf.init_got_offset.glist = NULL;
4197 htab->elf.init_plt_offset.offset = 0;
4198 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4199
65f38f15
AM
4200 return &htab->elf.root;
4201}
4202
721956f4
AM
4203/* Free the derived linker hash table. */
4204
4205static void
4ce794b7 4206ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4 4207{
3b421ab3 4208 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
721956f4 4209
3b421ab3
AM
4210 bfd_hash_table_free (&htab->stub_hash_table);
4211 bfd_hash_table_free (&htab->branch_hash_table);
4212 if (htab->tocsave_htab)
4213 htab_delete (htab->tocsave_htab);
9f7c3e5e 4214 _bfd_elf_link_hash_table_free (hash);
721956f4
AM
4215}
4216
bfeb4a28
AM
4217/* Create sections for linker generated code. */
4218
4219static bfd_boolean
4220create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4221{
4222 struct ppc_link_hash_table *htab;
4223 flagword flags;
4224
4225 htab = ppc_hash_table (info);
4226
4227 /* Create .sfpr for code to save and restore fp regs. */
4228 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4229 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4230 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4231 flags);
4232 if (htab->sfpr == NULL
4233 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4234 return FALSE;
4235
4236 /* Create .glink for lazy dynamic linking support. */
4237 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4238 flags);
4239 if (htab->glink == NULL
4240 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4241 return FALSE;
4242
4243 if (!info->no_ld_generated_unwind_info)
4244 {
4245 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4246 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4247 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4248 ".eh_frame",
4249 flags);
4250 if (htab->glink_eh_frame == NULL
4251 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4252 return FALSE;
4253 }
4254
4255 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4256 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4257 if (htab->iplt == NULL
4258 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4259 return FALSE;
4260
4261 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4262 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4263 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4264 ".rela.iplt",
4265 flags);
4266 if (htab->reliplt == NULL
4267 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4268 return FALSE;
4269
4270 /* Create branch lookup table for plt_branch stubs. */
4271 flags = (SEC_ALLOC | SEC_LOAD
4272 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4273 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4274 flags);
4275 if (htab->brlt == NULL
4276 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4277 return FALSE;
4278
4279 if (!info->shared)
4280 return TRUE;
4281
4282 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4283 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4284 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4285 ".rela.branch_lt",
4286 flags);
4287 if (htab->relbrlt == NULL
4288 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4289 return FALSE;
4290
4291 return TRUE;
4292}
4293
e717da7e
AM
4294/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4295
bfeb4a28 4296bfd_boolean
e717da7e
AM
4297ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4298{
4299 struct ppc_link_hash_table *htab;
4300
4301 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4302
4303/* Always hook our dynamic sections into the first bfd, which is the
4304 linker created stub bfd. This ensures that the GOT header is at
4305 the start of the output TOC section. */
4306 htab = ppc_hash_table (info);
4dfe6ac6 4307 if (htab == NULL)
bfeb4a28 4308 return FALSE;
e717da7e
AM
4309 htab->stub_bfd = abfd;
4310 htab->elf.dynobj = abfd;
bfeb4a28
AM
4311
4312 if (info->relocatable)
4313 return TRUE;
4314
4315 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4316}
4317
721956f4
AM
4318/* Build a name for an entry in the stub hash table. */
4319
4320static char *
4ce794b7
AM
4321ppc_stub_name (const asection *input_section,
4322 const asection *sym_sec,
4323 const struct ppc_link_hash_entry *h,
4324 const Elf_Internal_Rela *rel)
721956f4
AM
4325{
4326 char *stub_name;
bcaa2f82 4327 ssize_t len;
721956f4
AM
4328
4329 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4330 offsets from a sym as a branch target? In fact, we could
4331 probably assume the addend is always zero. */
4332 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4333
4334 if (h)
4335 {
4336 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4337 stub_name = bfd_malloc (len);
46de2a7c
AM
4338 if (stub_name == NULL)
4339 return stub_name;
4340
bcaa2f82
AM
4341 len = sprintf (stub_name, "%08x.%s+%x",
4342 input_section->id & 0xffffffff,
4343 h->elf.root.root.string,
4344 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4345 }
4346 else
4347 {
ad8e1ba5 4348 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4349 stub_name = bfd_malloc (len);
46de2a7c
AM
4350 if (stub_name == NULL)
4351 return stub_name;
4352
bcaa2f82
AM
4353 len = sprintf (stub_name, "%08x.%x:%x+%x",
4354 input_section->id & 0xffffffff,
4355 sym_sec->id & 0xffffffff,
4356 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4357 (int) rel->r_addend & 0xffffffff);
721956f4 4358 }
bcaa2f82 4359 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4360 stub_name[len - 2] = 0;
721956f4
AM
4361 return stub_name;
4362}
4363
4364/* Look up an entry in the stub hash. Stub entries are cached because
4365 creating the stub name takes a bit of time. */
4366
4367static struct ppc_stub_hash_entry *
4ce794b7
AM
4368ppc_get_stub_entry (const asection *input_section,
4369 const asection *sym_sec,
039b3fef 4370 struct ppc_link_hash_entry *h,
4ce794b7
AM
4371 const Elf_Internal_Rela *rel,
4372 struct ppc_link_hash_table *htab)
721956f4
AM
4373{
4374 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4375 const asection *id_sec;
4376
4377 /* If this input section is part of a group of sections sharing one
4378 stub section, then use the id of the first section in the group.
4379 Stub names need to include a section id, as there may well be
4380 more than one stub used to reach say, printf, and we need to
4381 distinguish between them. */
4382 id_sec = htab->stub_group[input_section->id].link_sec;
4383
b3fac117
AM
4384 if (h != NULL && h->u.stub_cache != NULL
4385 && h->u.stub_cache->h == h
4386 && h->u.stub_cache->id_sec == id_sec)
721956f4 4387 {
b3fac117 4388 stub_entry = h->u.stub_cache;
721956f4
AM
4389 }
4390 else
4391 {
4392 char *stub_name;
4393
4394 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4395 if (stub_name == NULL)
4396 return NULL;
4397
4398 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4399 stub_name, FALSE, FALSE);
721956f4 4400 if (h != NULL)
b3fac117 4401 h->u.stub_cache = stub_entry;
721956f4
AM
4402
4403 free (stub_name);
4404 }
4405
4406 return stub_entry;
4407}
4408
4409/* Add a new stub entry to the stub hash. Not all fields of the new
4410 stub entry are initialised. */
4411
4412static struct ppc_stub_hash_entry *
4ce794b7
AM
4413ppc_add_stub (const char *stub_name,
4414 asection *section,
25f53a85 4415 struct bfd_link_info *info)
721956f4 4416{
25f53a85 4417 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4418 asection *link_sec;
4419 asection *stub_sec;
4420 struct ppc_stub_hash_entry *stub_entry;
4421
4422 link_sec = htab->stub_group[section->id].link_sec;
4423 stub_sec = htab->stub_group[section->id].stub_sec;
4424 if (stub_sec == NULL)
4425 {
4426 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4427 if (stub_sec == NULL)
4428 {
d4c88bbb 4429 size_t namelen;
721956f4
AM
4430 bfd_size_type len;
4431 char *s_name;
4432
d4c88bbb
AM
4433 namelen = strlen (link_sec->name);
4434 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4435 s_name = bfd_alloc (htab->stub_bfd, len);
4436 if (s_name == NULL)
4437 return NULL;
4438
d4c88bbb
AM
4439 memcpy (s_name, link_sec->name, namelen);
4440 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4441 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4442 if (stub_sec == NULL)
4443 return NULL;
4444 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4445 }
4446 htab->stub_group[section->id].stub_sec = stub_sec;
4447 }
4448
4449 /* Enter this entry into the linker stub hash table. */
4450 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4451 TRUE, FALSE);
721956f4
AM
4452 if (stub_entry == NULL)
4453 {
8de848d8 4454 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4455 section->owner, stub_name);
721956f4
AM
4456 return NULL;
4457 }
4458
4459 stub_entry->stub_sec = stub_sec;
4460 stub_entry->stub_offset = 0;
4461 stub_entry->id_sec = link_sec;
4462 return stub_entry;
4463}
4464
e717da7e
AM
4465/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4466 not already done. */
65f38f15 4467
b34976b6 4468static bfd_boolean
e717da7e 4469create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4470{
e717da7e
AM
4471 asection *got, *relgot;
4472 flagword flags;
4473 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4474
0c8d6e5c 4475 if (!is_ppc64_elf (abfd))
0ffa91dd 4476 return FALSE;
4dfe6ac6
NC
4477 if (htab == NULL)
4478 return FALSE;
0ffa91dd 4479
4ce794b7 4480 if (!htab->got)
e717da7e
AM
4481 {
4482 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4483 return FALSE;
4484
3d4d4302 4485 htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
e717da7e
AM
4486 if (!htab->got)
4487 abort ();
4488 }
4489
4490 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4491 | SEC_LINKER_CREATED);
4492
c456f082 4493 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4494 if (!got
e717da7e
AM
4495 || !bfd_set_section_alignment (abfd, got, 3))
4496 return FALSE;
65f38f15 4497
c456f082
AM
4498 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4499 flags | SEC_READONLY);
e717da7e 4500 if (!relgot
e717da7e 4501 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4502 return FALSE;
e717da7e
AM
4503
4504 ppc64_elf_tdata (abfd)->got = got;
4505 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4506 return TRUE;
65f38f15 4507}
5bd4f169 4508
82bd7b59 4509/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4510
b34976b6 4511static bfd_boolean
4ce794b7 4512ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4513{
65f38f15 4514 struct ppc_link_hash_table *htab;
5bd4f169 4515
65f38f15 4516 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4517 return FALSE;
65f38f15 4518
e717da7e 4519 htab = ppc_hash_table (info);
4dfe6ac6
NC
4520 if (htab == NULL)
4521 return FALSE;
4522
e717da7e 4523 if (!htab->got)
3d4d4302
AM
4524 htab->got = bfd_get_linker_section (dynobj, ".got");
4525 htab->plt = bfd_get_linker_section (dynobj, ".plt");
4526 htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4527 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
65f38f15 4528 if (!info->shared)
3d4d4302 4529 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4530
e717da7e 4531 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4532 || (!info->shared && !htab->relbss))
65f38f15
AM
4533 abort ();
4534
b34976b6 4535 return TRUE;
5bd4f169
AM
4536}
4537
b31867b6
AM
4538/* Follow indirect and warning symbol links. */
4539
4540static inline struct bfd_link_hash_entry *
4541follow_link (struct bfd_link_hash_entry *h)
4542{
4543 while (h->type == bfd_link_hash_indirect
4544 || h->type == bfd_link_hash_warning)
4545 h = h->u.i.link;
4546 return h;
4547}
4548
4549static inline struct elf_link_hash_entry *
4550elf_follow_link (struct elf_link_hash_entry *h)
4551{
4552 return (struct elf_link_hash_entry *) follow_link (&h->root);
4553}
4554
4555static inline struct ppc_link_hash_entry *
4556ppc_follow_link (struct ppc_link_hash_entry *h)
4557{
4558 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4559}
4560
40d16e0b
AM
4561/* Merge PLT info on FROM with that on TO. */
4562
4563static void
4564move_plt_plist (struct ppc_link_hash_entry *from,
4565 struct ppc_link_hash_entry *to)
4566{
4567 if (from->elf.plt.plist != NULL)
4568 {
4569 if (to->elf.plt.plist != NULL)
4570 {
4571 struct plt_entry **entp;
4572 struct plt_entry *ent;
4573
4574 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4575 {
4576 struct plt_entry *dent;
4577
4578 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4579 if (dent->addend == ent->addend)
4580 {
4581 dent->plt.refcount += ent->plt.refcount;
4582 *entp = ent->next;
4583 break;
4584 }
4585 if (dent == NULL)
4586 entp = &ent->next;
4587 }
4588 *entp = to->elf.plt.plist;
4589 }
4590
4591 to->elf.plt.plist = from->elf.plt.plist;
4592 from->elf.plt.plist = NULL;
4593 }
4594}
4595
65f38f15
AM
4596/* Copy the extra info we tack onto an elf_link_hash_entry. */
4597
4598static void
fcfa13d2
AM
4599ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4600 struct elf_link_hash_entry *dir,
4601 struct elf_link_hash_entry *ind)
65f38f15
AM
4602{
4603 struct ppc_link_hash_entry *edir, *eind;
4604
4605 edir = (struct ppc_link_hash_entry *) dir;
4606 eind = (struct ppc_link_hash_entry *) ind;
4607
c79d6685
AM
4608 edir->is_func |= eind->is_func;
4609 edir->is_func_descriptor |= eind->is_func_descriptor;
4610 edir->tls_mask |= eind->tls_mask;
4611 if (eind->oh != NULL)
4612 edir->oh = ppc_follow_link (eind->oh);
4613
4614 /* If called to transfer flags for a weakdef during processing
4615 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4616 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4617 if (!(ELIMINATE_COPY_RELOCS
4618 && eind->elf.root.type != bfd_link_hash_indirect
4619 && edir->elf.dynamic_adjusted))
4620 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4621
4622 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4623 edir->elf.ref_regular |= eind->elf.ref_regular;
4624 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4625 edir->elf.needs_plt |= eind->elf.needs_plt;
4626
411e1bfb 4627 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4628 if (eind->dyn_relocs != NULL)
65f38f15 4629 {
bbd7ec4a
AM
4630 if (edir->dyn_relocs != NULL)
4631 {
6061a67d
AM
4632 struct elf_dyn_relocs **pp;
4633 struct elf_dyn_relocs *p;
bbd7ec4a 4634
fcfa13d2 4635 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4636 list. Merge any entries against the same section. */
4637 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4638 {
6061a67d 4639 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4640
4641 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4642 if (q->sec == p->sec)
4643 {
4644 q->pc_count += p->pc_count;
4645 q->count += p->count;
4646 *pp = p->next;
4647 break;
4648 }
4649 if (q == NULL)
4650 pp = &p->next;
4651 }
4652 *pp = edir->dyn_relocs;
4653 }
4654
65f38f15
AM
4655 edir->dyn_relocs = eind->dyn_relocs;
4656 eind->dyn_relocs = NULL;
4657 }
65f38f15 4658
68ba6d40
AM
4659 /* If we were called to copy over info for a weak sym, that's all.
4660 You might think dyn_relocs need not be copied over; After all,
4661 both syms will be dynamic or both non-dynamic so we're just
68ffbac6 4662 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
68ba6d40
AM
4663 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4664 dyn_relocs in read-only sections, and it does so on what is the
4665 DIR sym here. */
4666 if (eind->elf.root.type != bfd_link_hash_indirect)
4667 return;
4668
81848ca0
AM
4669 /* Copy over got entries that we may have already seen to the
4670 symbol which just became indirect. */
411e1bfb
AM
4671 if (eind->elf.got.glist != NULL)
4672 {
4673 if (edir->elf.got.glist != NULL)
4674 {
4675 struct got_entry **entp;
4676 struct got_entry *ent;
4677
4678 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4679 {
4680 struct got_entry *dent;
4681
4682 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4683 if (dent->addend == ent->addend
e717da7e 4684 && dent->owner == ent->owner
411e1bfb
AM
4685 && dent->tls_type == ent->tls_type)
4686 {
4687 dent->got.refcount += ent->got.refcount;
4688 *entp = ent->next;
4689 break;
4690 }
4691 if (dent == NULL)
4692 entp = &ent->next;
4693 }
4694 *entp = edir->elf.got.glist;
4695 }
4696
4697 edir->elf.got.glist = eind->elf.got.glist;
4698 eind->elf.got.glist = NULL;
4699 }
4700
4701 /* And plt entries. */
40d16e0b 4702 move_plt_plist (eind, edir);
411e1bfb 4703
fcfa13d2 4704 if (eind->elf.dynindx != -1)
411e1bfb 4705 {
fcfa13d2
AM
4706 if (edir->elf.dynindx != -1)
4707 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4708 edir->elf.dynstr_index);
411e1bfb
AM
4709 edir->elf.dynindx = eind->elf.dynindx;
4710 edir->elf.dynstr_index = eind->elf.dynstr_index;
4711 eind->elf.dynindx = -1;
4712 eind->elf.dynstr_index = 0;
4713 }
411e1bfb
AM
4714}
4715
8387904d
AM
4716/* Find the function descriptor hash entry from the given function code
4717 hash entry FH. Link the entries via their OH fields. */
4718
4719static struct ppc_link_hash_entry *
b31867b6 4720lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4721{
4722 struct ppc_link_hash_entry *fdh = fh->oh;
4723
4724 if (fdh == NULL)
4725 {
4726 const char *fd_name = fh->elf.root.root.string + 1;
4727
4728 fdh = (struct ppc_link_hash_entry *)
4729 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4730 if (fdh == NULL)
4731 return fdh;
4732
4733 fdh->is_func_descriptor = 1;
4734 fdh->oh = fh;
4735 fh->is_func = 1;
4736 fh->oh = fdh;
8387904d
AM
4737 }
4738
b31867b6 4739 return ppc_follow_link (fdh);
8387904d
AM
4740}
4741
bb700d78
AM
4742/* Make a fake function descriptor sym for the code sym FH. */
4743
4744static struct ppc_link_hash_entry *
4745make_fdh (struct bfd_link_info *info,
908b32fc 4746 struct ppc_link_hash_entry *fh)
bb700d78
AM
4747{
4748 bfd *abfd;
4749 asymbol *newsym;
4750 struct bfd_link_hash_entry *bh;
4751 struct ppc_link_hash_entry *fdh;
4752
4753 abfd = fh->elf.root.u.undef.abfd;
4754 newsym = bfd_make_empty_symbol (abfd);
4755 newsym->name = fh->elf.root.root.string + 1;
4756 newsym->section = bfd_und_section_ptr;
4757 newsym->value = 0;
908b32fc 4758 newsym->flags = BSF_WEAK;
bb700d78
AM
4759
4760 bh = NULL;
4761 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4762 newsym->flags, newsym->section,
4763 newsym->value, NULL, FALSE, FALSE,
4764 &bh))
4765 return NULL;
4766
4767 fdh = (struct ppc_link_hash_entry *) bh;
4768 fdh->elf.non_elf = 0;
908b32fc
AM
4769 fdh->fake = 1;
4770 fdh->is_func_descriptor = 1;
4771 fdh->oh = fh;
4772 fh->is_func = 1;
4773 fh->oh = fdh;
bb700d78
AM
4774 return fdh;
4775}
4776
8387904d
AM
4777/* Fix function descriptor symbols defined in .opd sections to be
4778 function type. */
555cd476
AM
4779
4780static bfd_boolean
c16153ae 4781ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4782 struct bfd_link_info *info,
555cd476 4783 Elf_Internal_Sym *isym,
6911b7dc 4784 const char **name,
555cd476
AM
4785 flagword *flags ATTRIBUTE_UNUSED,
4786 asection **sec,
4787 bfd_vma *value ATTRIBUTE_UNUSED)
4788{
f64b2e8d
NC
4789 if ((ibfd->flags & DYNAMIC) == 0
4790 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4791 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4792
e054468f 4793 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4794 {
4795 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4796 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4797 }
e054468f
AM
4798 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4799 ;
4800 else if (*sec != NULL
70cc837d 4801 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4802 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4803
6911b7dc
AM
4804 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4805 {
4806 if (abiversion (ibfd) == 0)
4807 set_abiversion (ibfd, 2);
4808 else if (abiversion (ibfd) == 1)
4809 {
4810 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4811 " for ABI version 1\n"), name);
4812 bfd_set_error (bfd_error_bad_value);
4813 return FALSE;
4814 }
4815 }
4816
555cd476
AM
4817 return TRUE;
4818}
4819
6911b7dc
AM
4820/* Merge non-visibility st_other attributes: local entry point. */
4821
4822static void
4823ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4824 const Elf_Internal_Sym *isym,
4825 bfd_boolean definition,
4826 bfd_boolean dynamic)
4827{
4828 if (definition && !dynamic)
4829 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4830 | ELF_ST_VISIBILITY (h->other));
4831}
4832
8387904d 4833/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4834 inclusion of a new ABI object archive that defines "bar".
4835 NAME is a symbol defined in an archive. Return a symbol in the hash
4836 table that might be satisfied by the archive symbols. */
8387904d
AM
4837
4838static struct elf_link_hash_entry *
4839ppc64_elf_archive_symbol_lookup (bfd *abfd,
4840 struct bfd_link_info *info,
4841 const char *name)
4842{
4843 struct elf_link_hash_entry *h;
4844 char *dot_name;
4845 size_t len;
4846
4847 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4848 if (h != NULL
4849 /* Don't return this sym if it is a fake function descriptor
4850 created by add_symbol_adjust. */
4851 && !(h->root.type == bfd_link_hash_undefweak
4852 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4853 return h;
4854
4855 if (name[0] == '.')
4856 return h;
4857
4858 len = strlen (name);
4859 dot_name = bfd_alloc (abfd, len + 2);
4860 if (dot_name == NULL)
4861 return (struct elf_link_hash_entry *) 0 - 1;
4862 dot_name[0] = '.';
4863 memcpy (dot_name + 1, name, len + 1);
4864 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4865 bfd_release (abfd, dot_name);
4866 return h;
4867}
4868
4869/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4870 new ABI object defines "bar". Well, at least, undefined dot symbols
4871 are made weak. This stops later archive searches from including an
4872 object if we already have a function descriptor definition. It also
35b0ce59
AM
4873 prevents the linker complaining about undefined symbols.
4874 We also check and correct mismatched symbol visibility here. The
4875 most restrictive visibility of the function descriptor and the
4876 function entry symbol is used. */
8387904d
AM
4877
4878static bfd_boolean
b3fac117 4879add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4880{
8387904d
AM
4881 struct ppc_link_hash_table *htab;
4882 struct ppc_link_hash_entry *fdh;
4883
b3fac117 4884 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4885 return TRUE;
4886
b3fac117
AM
4887 if (eh->elf.root.type == bfd_link_hash_warning)
4888 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4889
b3fac117
AM
4890 if (eh->elf.root.root.string[0] != '.')
4891 abort ();
8387904d 4892
b3fac117 4893 htab = ppc_hash_table (info);
4dfe6ac6
NC
4894 if (htab == NULL)
4895 return FALSE;
4896
b31867b6
AM
4897 fdh = lookup_fdh (eh, htab);
4898 if (fdh == NULL)
4899 {
4900 if (!info->relocatable
4901 && (eh->elf.root.type == bfd_link_hash_undefined
4902 || eh->elf.root.type == bfd_link_hash_undefweak)
4903 && eh->elf.ref_regular)
4904 {
4905 /* Make an undefweak function descriptor sym, which is enough to
4906 pull in an --as-needed shared lib, but won't cause link
4907 errors. Archives are handled elsewhere. */
4908 fdh = make_fdh (info, eh);
4909 if (fdh == NULL)
4910 return FALSE;
4911 fdh->elf.ref_regular = 1;
4912 }
bb700d78 4913 }
b31867b6 4914 else
8387904d 4915 {
35b0ce59
AM
4916 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4917 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4918 if (entry_vis < descr_vis)
4919 fdh->elf.other += entry_vis - descr_vis;
4920 else if (entry_vis > descr_vis)
4921 eh->elf.other += descr_vis - entry_vis;
4922
e87d886e
AM
4923 if ((fdh->elf.root.type == bfd_link_hash_defined
4924 || fdh->elf.root.type == bfd_link_hash_defweak)
4925 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4926 {
4927 eh->elf.root.type = bfd_link_hash_undefweak;
4928 eh->was_undefined = 1;
4929 htab->twiddled_syms = 1;
4930 }
8387904d 4931 }
99877b66 4932
8387904d
AM
4933 return TRUE;
4934}
4935
b3fac117
AM
4936/* Process list of dot-symbols we made in link_hash_newfunc. */
4937
8387904d 4938static bfd_boolean
7d9616d7 4939ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4940{
99877b66 4941 struct ppc_link_hash_table *htab;
b3fac117 4942 struct ppc_link_hash_entry **p, *eh;
433817dd 4943
0c8d6e5c 4944 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4945 return TRUE;
4dfe6ac6
NC
4946 htab = ppc_hash_table (info);
4947 if (htab == NULL)
4948 return FALSE;
35b0ce59 4949
0c8d6e5c 4950 if (is_ppc64_elf (ibfd))
b3fac117
AM
4951 {
4952 p = &htab->dot_syms;
4953 while ((eh = *p) != NULL)
4954 {
4955 *p = NULL;
1c865ab2
AM
4956 if (&eh->elf == htab->elf.hgot)
4957 ;
4958 else if (htab->elf.hgot == NULL
4959 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4960 htab->elf.hgot = &eh->elf;
4961 else if (!add_symbol_adjust (eh, info))
b3fac117
AM
4962 return FALSE;
4963 p = &eh->u.next_dot_sym;
4964 }
4965 }
4966
4967 /* Clear the list for non-ppc64 input files. */
4968 p = &htab->dot_syms;
4969 while ((eh = *p) != NULL)
4970 {
4971 *p = NULL;
4972 p = &eh->u.next_dot_sym;
4973 }
99877b66
AM
4974
4975 /* We need to fix the undefs list for any syms we have twiddled to
4976 undef_weak. */
4977 if (htab->twiddled_syms)
4978 {
77cfaee6 4979 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4980 htab->twiddled_syms = 0;
4981 }
b3fac117 4982 return TRUE;
8387904d
AM
4983}
4984
97fed1c9
JJ
4985/* Undo hash table changes when an --as-needed input file is determined
4986 not to be needed. */
4987
4988static bfd_boolean
e5034e59
AM
4989ppc64_elf_notice_as_needed (bfd *ibfd,
4990 struct bfd_link_info *info,
4991 enum notice_asneeded_action act)
97fed1c9 4992{
e5034e59
AM
4993 if (act == notice_not_needed)
4994 {
4995 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4996
e5034e59
AM
4997 if (htab == NULL)
4998 return FALSE;
4dfe6ac6 4999
e5034e59
AM
5000 htab->dot_syms = NULL;
5001 }
5002 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5003}
5004
aa374f67
AM
5005/* If --just-symbols against a final linked binary, then assume we need
5006 toc adjusting stubs when calling functions defined there. */
5007
5008static void
5009ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5010{
5011 if ((sec->flags & SEC_CODE) != 0
5012 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5013 && is_ppc64_elf (sec->owner))
5014 {
5015 asection *got = bfd_get_section_by_name (sec->owner, ".got");
5016 if (got != NULL
5017 && got->size >= elf_backend_got_header_size
5018 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5019 sec->has_toc_reloc = 1;
5020 }
5021 _bfd_elf_link_just_syms (sec, info);
5022}
5023
e054468f 5024static struct plt_entry **
4ce794b7
AM
5025update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5026 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5027{
5028 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5029 struct plt_entry **local_plt;
f961d9dd 5030 unsigned char *local_got_tls_masks;
411e1bfb
AM
5031
5032 if (local_got_ents == NULL)
5033 {
5034 bfd_size_type size = symtab_hdr->sh_info;
5035
e054468f
AM
5036 size *= (sizeof (*local_got_ents)
5037 + sizeof (*local_plt)
5038 + sizeof (*local_got_tls_masks));
4ce794b7 5039 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5040 if (local_got_ents == NULL)
e054468f 5041 return NULL;
411e1bfb
AM
5042 elf_local_got_ents (abfd) = local_got_ents;
5043 }
5044
e054468f 5045 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5046 {
5047 struct got_entry *ent;
5048
5049 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5050 if (ent->addend == r_addend
5051 && ent->owner == abfd
5052 && ent->tls_type == tls_type)
411e1bfb
AM
5053 break;
5054 if (ent == NULL)
5055 {
5056 bfd_size_type amt = sizeof (*ent);
4ce794b7 5057 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5058 if (ent == NULL)
5059 return FALSE;
5060 ent->next = local_got_ents[r_symndx];
5061 ent->addend = r_addend;
e717da7e 5062 ent->owner = abfd;
411e1bfb 5063 ent->tls_type = tls_type;
927be08e 5064 ent->is_indirect = FALSE;
411e1bfb
AM
5065 ent->got.refcount = 0;
5066 local_got_ents[r_symndx] = ent;
5067 }
5068 ent->got.refcount += 1;
5069 }
5070
e054468f 5071 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5072 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5073 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5074
5075 return local_plt + r_symndx;
65f38f15
AM
5076}
5077
411e1bfb 5078static bfd_boolean
e054468f 5079update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5080{
411e1bfb 5081 struct plt_entry *ent;
1e2f5b6e 5082
e054468f 5083 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5084 if (ent->addend == addend)
5085 break;
5086 if (ent == NULL)
1e2f5b6e 5087 {
411e1bfb 5088 bfd_size_type amt = sizeof (*ent);
4ce794b7 5089 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5090 if (ent == NULL)
5091 return FALSE;
e054468f 5092 ent->next = *plist;
411e1bfb
AM
5093 ent->addend = addend;
5094 ent->plt.refcount = 0;
e054468f 5095 *plist = ent;
1e2f5b6e 5096 }
411e1bfb 5097 ent->plt.refcount += 1;
b34976b6 5098 return TRUE;
1e2f5b6e
AM
5099}
5100
e054468f
AM
5101static bfd_boolean
5102is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5103{
5104 return (r_type == R_PPC64_REL24
5105 || r_type == R_PPC64_REL14
5106 || r_type == R_PPC64_REL14_BRTAKEN
5107 || r_type == R_PPC64_REL14_BRNTAKEN
5108 || r_type == R_PPC64_ADDR24
5109 || r_type == R_PPC64_ADDR14
5110 || r_type == R_PPC64_ADDR14_BRTAKEN
5111 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5112}
5113
5bd4f169 5114/* Look through the relocs for a section during the first phase, and
65f38f15 5115 calculate needed space in the global offset table, procedure
5d1634d7 5116 linkage table, and dynamic reloc sections. */
5bd4f169 5117
b34976b6 5118static bfd_boolean
4ce794b7
AM
5119ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5120 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5121{
65f38f15 5122 struct ppc_link_hash_table *htab;
5bd4f169 5123 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5124 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5125 const Elf_Internal_Rela *rel;
5126 const Elf_Internal_Rela *rel_end;
5bd4f169 5127 asection *sreloc;
1e2f5b6e 5128 asection **opd_sym_map;
3a71aa26 5129 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5130
1049f94e 5131 if (info->relocatable)
b34976b6 5132 return TRUE;
5bd4f169 5133
680a3378
AM
5134 /* Don't do anything special with non-loaded, non-alloced sections.
5135 In particular, any relocs in such sections should not affect GOT
5136 and PLT reference counting (ie. we don't allow them to create GOT
5137 or PLT entries), there's no possibility or desire to optimize TLS
5138 relocs, and there's not much point in propagating relocs to shared
5139 libs that the dynamic linker won't relocate. */
5140 if ((sec->flags & SEC_ALLOC) == 0)
5141 return TRUE;
5142
0c8d6e5c 5143 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5144
65f38f15 5145 htab = ppc_hash_table (info);
4dfe6ac6
NC
5146 if (htab == NULL)
5147 return FALSE;
5148
3a71aa26
AM
5149 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5150 FALSE, FALSE, TRUE);
5151 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5152 FALSE, FALSE, TRUE);
0ffa91dd 5153 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5154 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5155 sreloc = NULL;
1e2f5b6e 5156 opd_sym_map = NULL;
70cc837d 5157 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
5158 {
5159 /* Garbage collection needs some extra help with .opd sections.
5160 We don't want to necessarily keep everything referenced by
5161 relocs in .opd, as that would keep all functions. Instead,
5162 if we reference an .opd symbol (a function descriptor), we
5163 want to keep the function code symbol's section. This is
5164 easy for global symbols, but for local syms we need to keep
74f0fb50 5165 information about the associated function section. */
1e2f5b6e
AM
5166 bfd_size_type amt;
5167
ee67d69a
AM
5168 if (abiversion (abfd) == 0)
5169 set_abiversion (abfd, 1);
5170 else if (abiversion (abfd) == 2)
5171 {
5172 info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5173 abiversion (abfd));
5174 bfd_set_error (bfd_error_bad_value);
5175 return FALSE;
5176 }
74f0fb50 5177 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 5178 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 5179 if (opd_sym_map == NULL)
b34976b6 5180 return FALSE;
74f0fb50 5181 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
5182 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5183 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 5184 }
5bd4f169
AM
5185
5186 rel_end = relocs + sec->reloc_count;
5187 for (rel = relocs; rel < rel_end; rel++)
5188 {
5189 unsigned long r_symndx;
5190 struct elf_link_hash_entry *h;
04c9666a 5191 enum elf_ppc64_reloc_type r_type;
727fc41e 5192 int tls_type;
7c8fe5c4 5193 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 5194 struct plt_entry **ifunc;
5bd4f169
AM
5195
5196 r_symndx = ELF64_R_SYM (rel->r_info);
5197 if (r_symndx < symtab_hdr->sh_info)
5198 h = NULL;
5199 else
973a3492
L
5200 {
5201 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5202 h = elf_follow_link (h);
81fbe831
AM
5203
5204 /* PR15323, ref flags aren't set for references in the same
5205 object. */
5206 h->root.non_ir_ref = 1;
1c865ab2
AM
5207
5208 if (h == htab->elf.hgot)
5209 sec->has_toc_reloc = 1;
973a3492 5210 }
5bd4f169 5211
727fc41e 5212 tls_type = 0;
e054468f 5213 ifunc = NULL;
25f23106
AM
5214 if (h != NULL)
5215 {
5216 if (h->type == STT_GNU_IFUNC)
5217 {
5218 h->needs_plt = 1;
5219 ifunc = &h->plt.plist;
5220 }
5221 }
5222 else
5223 {
5224 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5225 abfd, r_symndx);
5226 if (isym == NULL)
5227 return FALSE;
5228
5229 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5230 {
5231 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5232 rel->r_addend, PLT_IFUNC);
5233 if (ifunc == NULL)
5234 return FALSE;
5235 }
5236 }
4ce794b7 5237 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
5238 if (is_branch_reloc (r_type))
5239 {
5240 if (h != NULL && (h == tga || h == dottga))
5241 {
5242 if (rel != relocs
5243 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5244 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5245 /* We have a new-style __tls_get_addr call with a marker
5246 reloc. */
5247 ;
5248 else
5249 /* Mark this section as having an old-style call. */
5250 sec->has_tls_get_addr_call = 1;
5251 }
727fc41e 5252
e054468f 5253 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5254 if (ifunc != NULL
5255 && !update_plt_info (abfd, ifunc, rel->r_addend))
5256 return FALSE;
5257 }
727fc41e 5258
a33d1f77 5259 switch (r_type)
5bd4f169 5260 {
727fc41e
AM
5261 case R_PPC64_TLSGD:
5262 case R_PPC64_TLSLD:
5263 /* These special tls relocs tie a call to __tls_get_addr with
5264 its parameter symbol. */
5265 break;
5266
411e1bfb
AM
5267 case R_PPC64_GOT_TLSLD16:
5268 case R_PPC64_GOT_TLSLD16_LO:
5269 case R_PPC64_GOT_TLSLD16_HI:
5270 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5271 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5272 goto dogottls;
5273
5274 case R_PPC64_GOT_TLSGD16:
5275 case R_PPC64_GOT_TLSGD16_LO:
5276 case R_PPC64_GOT_TLSGD16_HI:
5277 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5278 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5279 goto dogottls;
5280
5281 case R_PPC64_GOT_TPREL16_DS:
5282 case R_PPC64_GOT_TPREL16_LO_DS:
5283 case R_PPC64_GOT_TPREL16_HI:
5284 case R_PPC64_GOT_TPREL16_HA:
1d483afe 5285 if (!info->executable)
411e1bfb
AM
5286 info->flags |= DF_STATIC_TLS;
5287 tls_type = TLS_TLS | TLS_TPREL;
5288 goto dogottls;
5289
5290 case R_PPC64_GOT_DTPREL16_DS:
5291 case R_PPC64_GOT_DTPREL16_LO_DS:
5292 case R_PPC64_GOT_DTPREL16_HI:
5293 case R_PPC64_GOT_DTPREL16_HA:
5294 tls_type = TLS_TLS | TLS_DTPREL;
5295 dogottls:
5296 sec->has_tls_reloc = 1;
5297 /* Fall thru */
5298
5bd4f169 5299 case R_PPC64_GOT16:
5bd4f169 5300 case R_PPC64_GOT16_DS:
65f38f15
AM
5301 case R_PPC64_GOT16_HA:
5302 case R_PPC64_GOT16_HI:
5303 case R_PPC64_GOT16_LO:
5bd4f169 5304 case R_PPC64_GOT16_LO_DS:
65f38f15 5305 /* This symbol requires a global offset table entry. */
4c52953f 5306 sec->has_toc_reloc = 1;
33c0ec9d
AM
5307 if (r_type == R_PPC64_GOT_TLSLD16
5308 || r_type == R_PPC64_GOT_TLSGD16
5309 || r_type == R_PPC64_GOT_TPREL16_DS
5310 || r_type == R_PPC64_GOT_DTPREL16_DS
5311 || r_type == R_PPC64_GOT16
5312 || r_type == R_PPC64_GOT16_DS)
5313 {
5314 htab->do_multi_toc = 1;
d77c8a4b 5315 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5316 }
5317
e717da7e
AM
5318 if (ppc64_elf_tdata (abfd)->got == NULL
5319 && !create_got_section (abfd, info))
b34976b6 5320 return FALSE;
5bd4f169
AM
5321
5322 if (h != NULL)
5323 {
411e1bfb
AM
5324 struct ppc_link_hash_entry *eh;
5325 struct got_entry *ent;
65f38f15 5326
411e1bfb
AM
5327 eh = (struct ppc_link_hash_entry *) h;
5328 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5329 if (ent->addend == rel->r_addend
e717da7e 5330 && ent->owner == abfd
411e1bfb
AM
5331 && ent->tls_type == tls_type)
5332 break;
5333 if (ent == NULL)
5bd4f169 5334 {
411e1bfb 5335 bfd_size_type amt = sizeof (*ent);
4ce794b7 5336 ent = bfd_alloc (abfd, amt);
411e1bfb 5337 if (ent == NULL)
b34976b6 5338 return FALSE;
411e1bfb
AM
5339 ent->next = eh->elf.got.glist;
5340 ent->addend = rel->r_addend;
e717da7e 5341 ent->owner = abfd;
411e1bfb 5342 ent->tls_type = tls_type;
927be08e 5343 ent->is_indirect = FALSE;
411e1bfb
AM
5344 ent->got.refcount = 0;
5345 eh->elf.got.glist = ent;
5bd4f169 5346 }
411e1bfb 5347 ent->got.refcount += 1;
e7b938ca 5348 eh->tls_mask |= tls_type;
5bd4f169 5349 }
411e1bfb
AM
5350 else
5351 /* This is a global offset table entry for a local symbol. */
5352 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5353 rel->r_addend, tls_type))
5354 return FALSE;
5bd4f169
AM
5355 break;
5356
5bd4f169 5357 case R_PPC64_PLT16_HA:
65f38f15
AM
5358 case R_PPC64_PLT16_HI:
5359 case R_PPC64_PLT16_LO:
5360 case R_PPC64_PLT32:
5361 case R_PPC64_PLT64:
5bd4f169 5362 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5363 actually build the entry in adjust_dynamic_symbol,
5364 because this might be a case of linking PIC code without
5365 linking in any dynamic objects, in which case we don't
5366 need to generate a procedure linkage table after all. */
5bd4f169
AM
5367 if (h == NULL)
5368 {
5369 /* It does not make sense to have a procedure linkage
3fad3c7c 5370 table entry for a local symbol. */
5bd4f169 5371 bfd_set_error (bfd_error_bad_value);
b34976b6 5372 return FALSE;
5bd4f169 5373 }
411e1bfb 5374 else
e054468f
AM
5375 {
5376 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5377 return FALSE;
5378 h->needs_plt = 1;
5379 if (h->root.root.string[0] == '.'
5380 && h->root.root.string[1] != '\0')
5381 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5382 }
5bd4f169
AM
5383 break;
5384
5385 /* The following relocations don't need to propagate the
5386 relocation if linking a shared object since they are
5387 section relative. */
5388 case R_PPC64_SECTOFF:
5389 case R_PPC64_SECTOFF_LO:
5390 case R_PPC64_SECTOFF_HI:
5391 case R_PPC64_SECTOFF_HA:
5392 case R_PPC64_SECTOFF_DS:
5393 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5394 case R_PPC64_DTPREL16:
5395 case R_PPC64_DTPREL16_LO:
5396 case R_PPC64_DTPREL16_HI:
5397 case R_PPC64_DTPREL16_HA:
5398 case R_PPC64_DTPREL16_DS:
5399 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5400 case R_PPC64_DTPREL16_HIGH:
5401 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5402 case R_PPC64_DTPREL16_HIGHER:
5403 case R_PPC64_DTPREL16_HIGHERA:
5404 case R_PPC64_DTPREL16_HIGHEST:
5405 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5406 break;
5407
ad8e1ba5 5408 /* Nor do these. */
25f23106
AM
5409 case R_PPC64_REL16:
5410 case R_PPC64_REL16_LO:
5411 case R_PPC64_REL16_HI:
5412 case R_PPC64_REL16_HA:
5413 break;
5414
ad8e1ba5 5415 case R_PPC64_TOC16:
33c0ec9d
AM
5416 case R_PPC64_TOC16_DS:
5417 htab->do_multi_toc = 1;
d77c8a4b 5418 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5419 case R_PPC64_TOC16_LO:
5420 case R_PPC64_TOC16_HI:
5421 case R_PPC64_TOC16_HA:
ad8e1ba5 5422 case R_PPC64_TOC16_LO_DS:
4c52953f 5423 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5424 break;
5425
5bd4f169
AM
5426 /* This relocation describes the C++ object vtable hierarchy.
5427 Reconstruct it for later use during GC. */
5428 case R_PPC64_GNU_VTINHERIT:
c152c796 5429 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5430 return FALSE;
5bd4f169
AM
5431 break;
5432
5433 /* This relocation describes which C++ vtable entries are actually
5434 used. Record for later use during GC. */
5435 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5436 BFD_ASSERT (h != NULL);
5437 if (h != NULL
5438 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5439 return FALSE;
5bd4f169
AM
5440 break;
5441
721956f4
AM
5442 case R_PPC64_REL14:
5443 case R_PPC64_REL14_BRTAKEN:
5444 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5445 {
5446 asection *dest = NULL;
5447
5448 /* Heuristic: If jumping outside our section, chances are
5449 we are going to need a stub. */
5450 if (h != NULL)
5451 {
5452 /* If the sym is weak it may be overridden later, so
5453 don't assume we know where a weak sym lives. */
5454 if (h->root.type == bfd_link_hash_defined)
5455 dest = h->root.u.def.section;
5456 }
5457 else
87d72d41
AM
5458 {
5459 Elf_Internal_Sym *isym;
5460
5461 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5462 abfd, r_symndx);
5463 if (isym == NULL)
5464 return FALSE;
5465
5466 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5467 }
5468
220c76dd 5469 if (dest != sec)
7c8fe5c4 5470 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5471 }
721956f4
AM
5472 /* Fall through. */
5473
5d1634d7 5474 case R_PPC64_REL24:
e054468f 5475 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5476 {
5477 /* We may need a .plt entry if the function this reloc
5478 refers to is in a shared lib. */
e054468f 5479 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5480 return FALSE;
e054468f
AM
5481 h->needs_plt = 1;
5482 if (h->root.root.string[0] == '.'
5483 && h->root.root.string[1] != '\0')
5484 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5485 if (h == tga || h == dottga)
411e1bfb 5486 sec->has_tls_reloc = 1;
411e1bfb
AM
5487 }
5488 break;
5489
5490 case R_PPC64_TPREL64:
5491 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5492 if (!info->executable)
411e1bfb
AM
5493 info->flags |= DF_STATIC_TLS;
5494 goto dotlstoc;
5495
5496 case R_PPC64_DTPMOD64:
5497 if (rel + 1 < rel_end
5498 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5499 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5500 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5501 else
951fd09b 5502 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5503 goto dotlstoc;
5504
5505 case R_PPC64_DTPREL64:
5506 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5507 if (rel != relocs
5508 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5509 && rel[-1].r_offset == rel->r_offset - 8)
5510 /* This is the second reloc of a dtpmod, dtprel pair.
5511 Don't mark with TLS_DTPREL. */
5512 goto dodyn;
5513
5514 dotlstoc:
5515 sec->has_tls_reloc = 1;
5516 if (h != NULL)
5517 {
5518 struct ppc_link_hash_entry *eh;
5519 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5520 eh->tls_mask |= tls_type;
411e1bfb
AM
5521 }
5522 else
5523 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5524 rel->r_addend, tls_type))
5525 return FALSE;
5526
7c8fe5c4
AM
5527 ppc64_sec = ppc64_elf_section_data (sec);
5528 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5529 {
3a71aa26
AM
5530 bfd_size_type amt;
5531
e7b938ca 5532 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5533 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5534 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5535 if (ppc64_sec->u.toc.symndx == NULL)
5536 return FALSE;
5537 amt = sec->size * sizeof (bfd_vma) / 8;
5538 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5539 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5540 return FALSE;
7c8fe5c4
AM
5541 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5542 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5543 }
5544 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5545 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5546 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5547
5548 /* Mark the second slot of a GD or LD entry.
5549 -1 to indicate GD and -2 to indicate LD. */
5550 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5551 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5552 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5553 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5554 goto dodyn;
5555
5556 case R_PPC64_TPREL16:
5557 case R_PPC64_TPREL16_LO:
5558 case R_PPC64_TPREL16_HI:
5559 case R_PPC64_TPREL16_HA:
5560 case R_PPC64_TPREL16_DS:
5561 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5562 case R_PPC64_TPREL16_HIGH:
5563 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5564 case R_PPC64_TPREL16_HIGHER:
5565 case R_PPC64_TPREL16_HIGHERA:
5566 case R_PPC64_TPREL16_HIGHEST:
5567 case R_PPC64_TPREL16_HIGHESTA:
5568 if (info->shared)
5569 {
1d483afe
AM
5570 if (!info->executable)
5571 info->flags |= DF_STATIC_TLS;
411e1bfb 5572 goto dodyn;
5d1634d7
AM
5573 }
5574 break;
5575
e86ce104 5576 case R_PPC64_ADDR64:
1e2f5b6e 5577 if (opd_sym_map != NULL
1e2f5b6e 5578 && rel + 1 < rel_end
4ce794b7 5579 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5580 {
8387904d
AM
5581 if (h != NULL)
5582 {
5583 if (h->root.root.string[0] == '.'
5584 && h->root.root.string[1] != 0
b31867b6 5585 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5586 ;
5587 else
5588 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5589 }
5590 else
5591 {
5592 asection *s;
87d72d41 5593 Elf_Internal_Sym *isym;
1e2f5b6e 5594
87d72d41
AM
5595 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5596 abfd, r_symndx);
5597 if (isym == NULL)
8387904d 5598 return FALSE;
87d72d41
AM
5599
5600 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5601 if (s != NULL && s != sec)
3f764659 5602 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5603 }
1e2f5b6e 5604 }
e86ce104
AM
5605 /* Fall through. */
5606
04c9666a 5607 case R_PPC64_REL30:
5bd4f169 5608 case R_PPC64_REL32:
04c9666a 5609 case R_PPC64_REL64:
65f38f15
AM
5610 case R_PPC64_ADDR14:
5611 case R_PPC64_ADDR14_BRNTAKEN:
5612 case R_PPC64_ADDR14_BRTAKEN:
5613 case R_PPC64_ADDR16:
5614 case R_PPC64_ADDR16_DS:
5615 case R_PPC64_ADDR16_HA:
5616 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5617 case R_PPC64_ADDR16_HIGH:
5618 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5619 case R_PPC64_ADDR16_HIGHER:
5620 case R_PPC64_ADDR16_HIGHERA:
5621 case R_PPC64_ADDR16_HIGHEST:
5622 case R_PPC64_ADDR16_HIGHESTA:
5623 case R_PPC64_ADDR16_LO:
5624 case R_PPC64_ADDR16_LO_DS:
5625 case R_PPC64_ADDR24:
65f38f15 5626 case R_PPC64_ADDR32:
65f38f15
AM
5627 case R_PPC64_UADDR16:
5628 case R_PPC64_UADDR32:
5629 case R_PPC64_UADDR64:
5bd4f169 5630 case R_PPC64_TOC:
81848ca0
AM
5631 if (h != NULL && !info->shared)
5632 /* We may need a copy reloc. */
f5385ebf 5633 h->non_got_ref = 1;
81848ca0 5634
41bd81ab 5635 /* Don't propagate .opd relocs. */
1e2f5b6e 5636 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5637 break;
e86ce104 5638
65f38f15
AM
5639 /* If we are creating a shared library, and this is a reloc
5640 against a global symbol, or a non PC relative reloc
5641 against a local symbol, then we need to copy the reloc
5642 into the shared library. However, if we are linking with
5643 -Bsymbolic, we do not need to copy a reloc against a
5644 global symbol which is defined in an object we are
5645 including in the link (i.e., DEF_REGULAR is set). At
5646 this point we have not seen all the input files, so it is
5647 possible that DEF_REGULAR is not set now but will be set
5648 later (it is never cleared). In case of a weak definition,
5649 DEF_REGULAR may be cleared later by a strong definition in
5650 a shared library. We account for that possibility below by
f4656909 5651 storing information in the dyn_relocs field of the hash
65f38f15
AM
5652 table entry. A similar situation occurs when creating
5653 shared libraries and symbol visibility changes render the
5654 symbol local.
5655
5656 If on the other hand, we are creating an executable, we
5657 may need to keep relocations for symbols satisfied by a
5658 dynamic library if we manage to avoid copy relocs for the
5659 symbol. */
411e1bfb 5660 dodyn:
65f38f15 5661 if ((info->shared
1d483afe 5662 && (must_be_dyn_reloc (info, r_type)
65f38f15 5663 || (h != NULL
198f1157 5664 && (!SYMBOLIC_BIND (info, h)
65f38f15 5665 || h->root.type == bfd_link_hash_defweak
f5385ebf 5666 || !h->def_regular))))
f4656909
AM
5667 || (ELIMINATE_COPY_RELOCS
5668 && !info->shared
65f38f15
AM
5669 && h != NULL
5670 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5671 || !h->def_regular))
5672 || (!info->shared
5673 && ifunc != NULL))
5bd4f169 5674 {
65f38f15
AM
5675 /* We must copy these reloc types into the output file.
5676 Create a reloc section in dynobj and make room for
5677 this reloc. */
5bd4f169
AM
5678 if (sreloc == NULL)
5679 {
83bac4b0
NC
5680 sreloc = _bfd_elf_make_dynamic_reloc_section
5681 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5682
5bd4f169 5683 if (sreloc == NULL)
83bac4b0 5684 return FALSE;
5bd4f169
AM
5685 }
5686
65f38f15
AM
5687 /* If this is a global symbol, we count the number of
5688 relocations we need for this symbol. */
5689 if (h != NULL)
5690 {
19e08130
AM
5691 struct elf_dyn_relocs *p;
5692 struct elf_dyn_relocs **head;
5693
ec338859 5694 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5695 p = *head;
5696 if (p == NULL || p->sec != sec)
5697 {
5698 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5699 if (p == NULL)
5700 return FALSE;
5701 p->next = *head;
5702 *head = p;
5703 p->sec = sec;
5704 p->count = 0;
5705 p->pc_count = 0;
5706 }
5707 p->count += 1;
5708 if (!must_be_dyn_reloc (info, r_type))
5709 p->pc_count += 1;
65f38f15
AM
5710 }
5711 else
5712 {
ec338859
AM
5713 /* Track dynamic relocs needed for local syms too.
5714 We really need local syms available to do this
5715 easily. Oh well. */
19e08130
AM
5716 struct ppc_dyn_relocs *p;
5717 struct ppc_dyn_relocs **head;
5718 bfd_boolean is_ifunc;
ec338859 5719 asection *s;
6edfbbad 5720 void *vpp;
87d72d41 5721 Elf_Internal_Sym *isym;
6edfbbad 5722
87d72d41
AM
5723 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5724 abfd, r_symndx);
5725 if (isym == NULL)
b34976b6 5726 return FALSE;
ec338859 5727
87d72d41
AM
5728 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5729 if (s == NULL)
5730 s = sec;
5731
6edfbbad 5732 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5733 head = (struct ppc_dyn_relocs **) vpp;
5734 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5735 p = *head;
5736 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5737 p = p->next;
5738 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5739 {
5740 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5741 if (p == NULL)
5742 return FALSE;
5743 p->next = *head;
5744 *head = p;
5745 p->sec = sec;
5746 p->ifunc = is_ifunc;
5747 p->count = 0;
5748 }
5749 p->count += 1;
ec338859 5750 }
65f38f15 5751 }
5bd4f169 5752 break;
65f38f15
AM
5753
5754 default:
96e0dda4 5755 break;
5bd4f169
AM
5756 }
5757 }
5758
b34976b6 5759 return TRUE;
5bd4f169
AM
5760}
5761
ee67d69a
AM
5762/* Merge backend specific data from an object file to the output
5763 object file when linking. */
5764
5765static bfd_boolean
5766ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5767{
5768 unsigned long iflags, oflags;
5769
5770 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5771 return TRUE;
5772
5773 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5774 return TRUE;
5775
5776 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5777 return FALSE;
5778
5779 iflags = elf_elfheader (ibfd)->e_flags;
5780 oflags = elf_elfheader (obfd)->e_flags;
5781
5782 if (!elf_flags_init (obfd) || oflags == 0)
5783 {
5784 elf_flags_init (obfd) = TRUE;
5785 elf_elfheader (obfd)->e_flags = iflags;
5786 }
5787 else if (iflags == oflags || iflags == 0)
5788 ;
5789 else if (iflags & ~EF_PPC64_ABI)
5790 {
5791 (*_bfd_error_handler)
5792 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5793 bfd_set_error (bfd_error_bad_value);
5794 return FALSE;
5795 }
5796 else
5797 {
5798 (*_bfd_error_handler)
5799 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5800 ibfd, iflags, oflags);
5801 bfd_set_error (bfd_error_bad_value);
5802 return FALSE;
5803 }
5804
5805 /* Merge Tag_compatibility attributes and any common GNU ones. */
5806 _bfd_elf_merge_object_attributes (ibfd, obfd);
5807
5808 return TRUE;
5809}
5810
5811static bfd_boolean
5812ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5813{
5814 /* Print normal ELF private data. */
5815 _bfd_elf_print_private_bfd_data (abfd, ptr);
5816
5817 if (elf_elfheader (abfd)->e_flags != 0)
5818 {
5819 FILE *file = ptr;
5820
5821 /* xgettext:c-format */
5822 fprintf (file, _("private flags = 0x%lx:"),
5823 elf_elfheader (abfd)->e_flags);
5824
5825 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5826 fprintf (file, _(" [abiv%ld]"),
5827 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5828 fputc ('\n', file);
5829 }
5830
5831 return TRUE;
5832}
5833
8387904d
AM
5834/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5835 of the code entry point, and its section. */
5836
5837static bfd_vma
5838opd_entry_value (asection *opd_sec,
5839 bfd_vma offset,
5840 asection **code_sec,
aef36ac1
AM
5841 bfd_vma *code_off,
5842 bfd_boolean in_code_sec)
8387904d
AM
5843{
5844 bfd *opd_bfd = opd_sec->owner;
8860955f 5845 Elf_Internal_Rela *relocs;
8387904d 5846 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5847 bfd_vma val;
8387904d 5848
9f296da3
AM
5849 /* No relocs implies we are linking a --just-symbols object, or looking
5850 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5851 if (opd_sec->reloc_count == 0)
5852 {
729eabd5 5853 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5854
729eabd5
AM
5855 if (contents == NULL)
5856 {
5857 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5858 return (bfd_vma) -1;
5859 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5860 }
ee1e4ede 5861
729eabd5 5862 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5863 if (code_sec != NULL)
5864 {
5865 asection *sec, *likely = NULL;
ee1e4ede 5866
aef36ac1 5867 if (in_code_sec)
4b85d634 5868 {
aef36ac1
AM
5869 sec = *code_sec;
5870 if (sec->vma <= val
5871 && val < sec->vma + sec->size)
5872 likely = sec;
5873 else
5874 val = -1;
5875 }
5876 else
5877 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5878 if (sec->vma <= val
5879 && (sec->flags & SEC_LOAD) != 0
5880 && (sec->flags & SEC_ALLOC) != 0)
5881 likely = sec;
5882 if (likely != NULL)
5883 {
5884 *code_sec = likely;
5885 if (code_off != NULL)
5886 *code_off = val - likely->vma;
4b85d634
AM
5887 }
5888 }
aef36ac1 5889 return val;
4b85d634
AM
5890 }
5891
0c8d6e5c 5892 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5893
729eabd5 5894 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5895 if (relocs == NULL)
5896 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5897
8387904d 5898 /* Go find the opd reloc at the sym address. */
8860955f 5899 lo = relocs;
8387904d
AM
5900 BFD_ASSERT (lo != NULL);
5901 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5902 val = (bfd_vma) -1;
8387904d
AM
5903 while (lo < hi)
5904 {
5905 look = lo + (hi - lo) / 2;
5906 if (look->r_offset < offset)
5907 lo = look + 1;
5908 else if (look->r_offset > offset)
5909 hi = look;
5910 else
5911 {
0ffa91dd
NC
5912 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5913
8387904d
AM
5914 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5915 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5916 {
5917 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5918 asection *sec;
5919
62599110
AM
5920 if (symndx < symtab_hdr->sh_info
5921 || elf_sym_hashes (opd_bfd) == NULL)
8387904d
AM
5922 {
5923 Elf_Internal_Sym *sym;
5924
5925 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5926 if (sym == NULL)
5927 {
62599110
AM
5928 size_t symcnt = symtab_hdr->sh_info;
5929 if (elf_sym_hashes (opd_bfd) == NULL)
5930 symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5931 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
8387904d
AM
5932 0, NULL, NULL, NULL);
5933 if (sym == NULL)
645ea6a9 5934 break;
8387904d
AM
5935 symtab_hdr->contents = (bfd_byte *) sym;
5936 }
5937
5938 sym += symndx;
5939 val = sym->st_value;
cb33740c 5940 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5941 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5942 }
5943 else
5944 {
5945 struct elf_link_hash_entry **sym_hashes;
5946 struct elf_link_hash_entry *rh;
5947
5948 sym_hashes = elf_sym_hashes (opd_bfd);
5949 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5950 if (rh != NULL)
5951 {
5952 rh = elf_follow_link (rh);
5953 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5954 || rh->root.type == bfd_link_hash_defweak);
5955 val = rh->root.u.def.value;
5956 sec = rh->root.u.def.section;
5957 }
5958 else
5959 {
5960 /* Handle the odd case where we can be called
5961 during bfd_elf_link_add_symbols before the
5962 symbol hashes have been fully populated. */
5963 Elf_Internal_Sym *sym;
5964
5965 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5966 symndx, NULL, NULL, NULL);
5967 if (sym == NULL)
5968 break;
5969
5970 val = sym->st_value;
5971 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5972 free (sym);
5973 }
8387904d
AM
5974 }
5975 val += look->r_addend;
5976 if (code_off != NULL)
5977 *code_off = val;
5978 if (code_sec != NULL)
aef36ac1
AM
5979 {
5980 if (in_code_sec && *code_sec != sec)
5981 return -1;
5982 else
5983 *code_sec = sec;
5984 }
8387904d
AM
5985 if (sec != NULL && sec->output_section != NULL)
5986 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5987 }
5988 break;
5989 }
5990 }
645ea6a9 5991
645ea6a9 5992 return val;
8387904d
AM
5993}
5994
aef36ac1
AM
5995/* If the ELF symbol SYM might be a function in SEC, return the
5996 function size and set *CODE_OFF to the function's entry point,
5997 otherwise return zero. */
9f296da3 5998
aef36ac1
AM
5999static bfd_size_type
6000ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6001 bfd_vma *code_off)
9f296da3 6002{
aef36ac1
AM
6003 bfd_size_type size;
6004
6005 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6006 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6007 return 0;
6008
6009 size = 0;
6010 if (!(sym->flags & BSF_SYNTHETIC))
6011 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6012
6013 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6014 {
aef36ac1
AM
6015 if (opd_entry_value (sym->section, sym->value,
6016 &sec, code_off, TRUE) == (bfd_vma) -1)
6017 return 0;
6018 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6019 symbol. This size has nothing to do with the code size of the
6020 function, which is what we're supposed to return, but the
6021 code size isn't available without looking up the dot-sym.
6022 However, doing that would be a waste of time particularly
6023 since elf_find_function will look at the dot-sym anyway.
6024 Now, elf_find_function will keep the largest size of any
6025 function sym found at the code address of interest, so return
6026 1 here to avoid it incorrectly caching a larger function size
6027 for a small function. This does mean we return the wrong
6028 size for a new-ABI function of size 24, but all that does is
6029 disable caching for such functions. */
6030 if (size == 24)
6031 size = 1;
9f296da3 6032 }
aef36ac1
AM
6033 else
6034 {
6035 if (sym->section != sec)
6036 return 0;
6037 *code_off = sym->value;
6038 }
6039 if (size == 0)
6040 size = 1;
6041 return size;
9f296da3
AM
6042}
6043
854b41e7
AM
6044/* Return true if symbol is defined in a regular object file. */
6045
6046static bfd_boolean
6047is_static_defined (struct elf_link_hash_entry *h)
6048{
6049 return ((h->root.type == bfd_link_hash_defined
6050 || h->root.type == bfd_link_hash_defweak)
6051 && h->root.u.def.section != NULL
6052 && h->root.u.def.section->output_section != NULL);
6053}
6054
b31867b6
AM
6055/* If FDH is a function descriptor symbol, return the associated code
6056 entry symbol if it is defined. Return NULL otherwise. */
6057
6058static struct ppc_link_hash_entry *
6059defined_code_entry (struct ppc_link_hash_entry *fdh)
6060{
6061 if (fdh->is_func_descriptor)
6062 {
6063 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6064 if (fh->elf.root.type == bfd_link_hash_defined
6065 || fh->elf.root.type == bfd_link_hash_defweak)
6066 return fh;
6067 }
6068 return NULL;
6069}
6070
6071/* If FH is a function code entry symbol, return the associated
6072 function descriptor symbol if it is defined. Return NULL otherwise. */
6073
6074static struct ppc_link_hash_entry *
6075defined_func_desc (struct ppc_link_hash_entry *fh)
6076{
6077 if (fh->oh != NULL
6078 && fh->oh->is_func_descriptor)
6079 {
6080 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6081 if (fdh->elf.root.type == bfd_link_hash_defined
6082 || fdh->elf.root.type == bfd_link_hash_defweak)
6083 return fdh;
6084 }
6085 return NULL;
6086}
6087
74f0fb50
AM
6088/* Mark all our entry sym sections, both opd and code section. */
6089
6090static void
6091ppc64_elf_gc_keep (struct bfd_link_info *info)
6092{
6093 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6094 struct bfd_sym_chain *sym;
6095
4dfe6ac6
NC
6096 if (htab == NULL)
6097 return;
6098
74f0fb50
AM
6099 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6100 {
b31867b6 6101 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6102 asection *sec;
6103
6104 eh = (struct ppc_link_hash_entry *)
b31867b6 6105 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6106 if (eh == NULL)
6107 continue;
6108 if (eh->elf.root.type != bfd_link_hash_defined
6109 && eh->elf.root.type != bfd_link_hash_defweak)
6110 continue;
6111
b31867b6
AM
6112 fh = defined_code_entry (eh);
6113 if (fh != NULL)
74f0fb50 6114 {
b31867b6 6115 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6116 sec->flags |= SEC_KEEP;
6117 }
6118 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6119 && opd_entry_value (eh->elf.root.u.def.section,
6120 eh->elf.root.u.def.value,
aef36ac1 6121 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6122 sec->flags |= SEC_KEEP;
6123
6124 sec = eh->elf.root.u.def.section;
6125 sec->flags |= SEC_KEEP;
6126 }
6127}
6128
64d03ab5
AM
6129/* Mark sections containing dynamically referenced symbols. When
6130 building shared libraries, we must assume that any visible symbol is
6131 referenced. */
6132
6133static bfd_boolean
6134ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6135{
6136 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6137 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6138 struct ppc_link_hash_entry *fdh;
64d03ab5 6139
64d03ab5 6140 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6141 fdh = defined_func_desc (eh);
6142 if (fdh != NULL)
6143 eh = fdh;
64d03ab5
AM
6144
6145 if ((eh->elf.root.type == bfd_link_hash_defined
6146 || eh->elf.root.type == bfd_link_hash_defweak)
6147 && (eh->elf.ref_dynamic
6148 || (!info->executable
6149 && eh->elf.def_regular
6150 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8
AM
6151 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6152 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6153 || !bfd_hide_sym_by_version (info->version_info,
6154 eh->elf.root.root.string)))))
64d03ab5
AM
6155 {
6156 asection *code_sec;
b31867b6 6157 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6158
6159 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6160
6161 /* Function descriptor syms cause the associated
6162 function code sym section to be marked. */
b31867b6
AM
6163 fh = defined_code_entry (eh);
6164 if (fh != NULL)
6165 {
6166 code_sec = fh->elf.root.u.def.section;
6167 code_sec->flags |= SEC_KEEP;
6168 }
64d03ab5
AM
6169 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6170 && opd_entry_value (eh->elf.root.u.def.section,
6171 eh->elf.root.u.def.value,
aef36ac1 6172 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6173 code_sec->flags |= SEC_KEEP;
6174 }
6175
6176 return TRUE;
6177}
6178
5bd4f169
AM
6179/* Return the section that should be marked against GC for a given
6180 relocation. */
6181
6182static asection *
4ce794b7 6183ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6184 struct bfd_link_info *info,
4ce794b7
AM
6185 Elf_Internal_Rela *rel,
6186 struct elf_link_hash_entry *h,
6187 Elf_Internal_Sym *sym)
5bd4f169 6188{
ccfa59ea
AM
6189 asection *rsec;
6190
ccfa59ea
AM
6191 /* Syms return NULL if we're marking .opd, so we avoid marking all
6192 function sections, as all functions are referenced in .opd. */
6193 rsec = NULL;
6194 if (get_opd_info (sec) != NULL)
6195 return rsec;
1e2f5b6e 6196
5bd4f169
AM
6197 if (h != NULL)
6198 {
04c9666a 6199 enum elf_ppc64_reloc_type r_type;
b31867b6 6200 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6201
4ce794b7 6202 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6203 switch (r_type)
5bd4f169
AM
6204 {
6205 case R_PPC64_GNU_VTINHERIT:
6206 case R_PPC64_GNU_VTENTRY:
6207 break;
6208
6209 default:
6210 switch (h->root.type)
6211 {
6212 case bfd_link_hash_defined:
6213 case bfd_link_hash_defweak:
ccfa59ea 6214 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6215 fdh = defined_func_desc (eh);
6216 if (fdh != NULL)
6217 eh = fdh;
1e2f5b6e
AM
6218
6219 /* Function descriptor syms cause the associated
6220 function code sym section to be marked. */
b31867b6
AM
6221 fh = defined_code_entry (eh);
6222 if (fh != NULL)
ccfa59ea
AM
6223 {
6224 /* They also mark their opd section. */
74f0fb50 6225 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6226
b31867b6 6227 rsec = fh->elf.root.u.def.section;
ccfa59ea 6228 }
8387904d
AM
6229 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6230 && opd_entry_value (eh->elf.root.u.def.section,
6231 eh->elf.root.u.def.value,
aef36ac1 6232 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6233 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6234 else
1e2f5b6e
AM
6235 rsec = h->root.u.def.section;
6236 break;
5bd4f169
AM
6237
6238 case bfd_link_hash_common:
1e2f5b6e
AM
6239 rsec = h->root.u.c.p->section;
6240 break;
5bd4f169
AM
6241
6242 default:
fb34365b 6243 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6244 }
6245 }
6246 }
6247 else
6248 {
74f0fb50 6249 struct _opd_sec_data *opd;
1e2f5b6e
AM
6250
6251 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6252 opd = get_opd_info (rsec);
6253 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6254 {
74f0fb50 6255 rsec->gc_mark = 1;
ccfa59ea 6256
74f0fb50 6257 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 6258 }
5bd4f169
AM
6259 }
6260
1e2f5b6e 6261 return rsec;
5bd4f169
AM
6262}
6263
65f38f15
AM
6264/* Update the .got, .plt. and dynamic reloc reference counts for the
6265 section being removed. */
5bd4f169 6266
b34976b6 6267static bfd_boolean
4ce794b7
AM
6268ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6269 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6270{
411e1bfb 6271 struct ppc_link_hash_table *htab;
5bd4f169
AM
6272 Elf_Internal_Shdr *symtab_hdr;
6273 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6274 struct got_entry **local_got_ents;
5bd4f169 6275 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6276
7dda2462
TG
6277 if (info->relocatable)
6278 return TRUE;
6279
680a3378
AM
6280 if ((sec->flags & SEC_ALLOC) == 0)
6281 return TRUE;
6282
ec338859
AM
6283 elf_section_data (sec)->local_dynrel = NULL;
6284
411e1bfb 6285 htab = ppc_hash_table (info);
4dfe6ac6
NC
6286 if (htab == NULL)
6287 return FALSE;
6288
0ffa91dd 6289 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6290 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6291 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6292
6293 relend = relocs + sec->reloc_count;
6294 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6295 {
6296 unsigned long r_symndx;
04c9666a 6297 enum elf_ppc64_reloc_type r_type;
58ac9f71 6298 struct elf_link_hash_entry *h = NULL;
f961d9dd 6299 unsigned char tls_type = 0;
5bd4f169 6300
a33d1f77 6301 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6302 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6303 if (r_symndx >= symtab_hdr->sh_info)
6304 {
6305 struct ppc_link_hash_entry *eh;
6061a67d
AM
6306 struct elf_dyn_relocs **pp;
6307 struct elf_dyn_relocs *p;
58ac9f71
AM
6308
6309 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6310 h = elf_follow_link (h);
58ac9f71
AM
6311 eh = (struct ppc_link_hash_entry *) h;
6312
6313 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6314 if (p->sec == sec)
6315 {
6316 /* Everything must go for SEC. */
6317 *pp = p->next;
6318 break;
6319 }
6320 }
6321
e054468f
AM
6322 if (is_branch_reloc (r_type))
6323 {
6324 struct plt_entry **ifunc = NULL;
6325 if (h != NULL)
6326 {
6327 if (h->type == STT_GNU_IFUNC)
6328 ifunc = &h->plt.plist;
6329 }
6330 else if (local_got_ents != NULL)
6331 {
6332 struct plt_entry **local_plt = (struct plt_entry **)
6333 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 6334 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
6335 (local_plt + symtab_hdr->sh_info);
6336 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6337 ifunc = local_plt + r_symndx;
6338 }
6339 if (ifunc != NULL)
6340 {
6341 struct plt_entry *ent;
6342
6343 for (ent = *ifunc; ent != NULL; ent = ent->next)
6344 if (ent->addend == rel->r_addend)
6345 break;
6346 if (ent == NULL)
6347 abort ();
6348 if (ent->plt.refcount > 0)
6349 ent->plt.refcount -= 1;
6350 continue;
6351 }
6352 }
6353
a33d1f77
AM
6354 switch (r_type)
6355 {
411e1bfb
AM
6356 case R_PPC64_GOT_TLSLD16:
6357 case R_PPC64_GOT_TLSLD16_LO:
6358 case R_PPC64_GOT_TLSLD16_HI:
6359 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6360 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6361 goto dogot;
6362
6363 case R_PPC64_GOT_TLSGD16:
6364 case R_PPC64_GOT_TLSGD16_LO:
6365 case R_PPC64_GOT_TLSGD16_HI:
6366 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6367 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6368 goto dogot;
6369
6370 case R_PPC64_GOT_TPREL16_DS:
6371 case R_PPC64_GOT_TPREL16_LO_DS:
6372 case R_PPC64_GOT_TPREL16_HI:
6373 case R_PPC64_GOT_TPREL16_HA:
6374 tls_type = TLS_TLS | TLS_TPREL;
6375 goto dogot;
6376
6377 case R_PPC64_GOT_DTPREL16_DS:
6378 case R_PPC64_GOT_DTPREL16_LO_DS:
6379 case R_PPC64_GOT_DTPREL16_HI:
6380 case R_PPC64_GOT_DTPREL16_HA:
6381 tls_type = TLS_TLS | TLS_DTPREL;
6382 goto dogot;
6383
a33d1f77
AM
6384 case R_PPC64_GOT16:
6385 case R_PPC64_GOT16_DS:
6386 case R_PPC64_GOT16_HA:
6387 case R_PPC64_GOT16_HI:
6388 case R_PPC64_GOT16_LO:
6389 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6390 dogot:
6391 {
6392 struct got_entry *ent;
6393
58ac9f71
AM
6394 if (h != NULL)
6395 ent = h->got.glist;
411e1bfb
AM
6396 else
6397 ent = local_got_ents[r_symndx];
6398
6399 for (; ent != NULL; ent = ent->next)
6400 if (ent->addend == rel->r_addend
e717da7e 6401 && ent->owner == abfd
411e1bfb
AM
6402 && ent->tls_type == tls_type)
6403 break;
6404 if (ent == NULL)
6405 abort ();
6406 if (ent->got.refcount > 0)
6407 ent->got.refcount -= 1;
6408 }
a33d1f77 6409 break;
65f38f15 6410
a33d1f77
AM
6411 case R_PPC64_PLT16_HA:
6412 case R_PPC64_PLT16_HI:
6413 case R_PPC64_PLT16_LO:
6414 case R_PPC64_PLT32:
6415 case R_PPC64_PLT64:
721956f4
AM
6416 case R_PPC64_REL14:
6417 case R_PPC64_REL14_BRNTAKEN:
6418 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6419 case R_PPC64_REL24:
58ac9f71 6420 if (h != NULL)
5d1634d7 6421 {
411e1bfb
AM
6422 struct plt_entry *ent;
6423
411e1bfb
AM
6424 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6425 if (ent->addend == rel->r_addend)
6426 break;
7c6c1722 6427 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6428 ent->plt.refcount -= 1;
5d1634d7 6429 }
e86ce104 6430 break;
5d1634d7 6431
a33d1f77
AM
6432 default:
6433 break;
6434 }
6435 }
b34976b6 6436 return TRUE;
5bd4f169
AM
6437}
6438
deb0e272
AM
6439/* The maximum size of .sfpr. */
6440#define SFPR_MAX (218*4)
6441
6442struct sfpr_def_parms
6443{
699733f6
AM
6444 const char name[12];
6445 unsigned char lo, hi;
deb0e272
AM
6446 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6447 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6448};
6449
6450/* Auto-generate _save*, _rest* functions in .sfpr. */
6451
4dfe6ac6 6452static bfd_boolean
deb0e272
AM
6453sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6454{
6455 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6456 unsigned int i;
6457 size_t len = strlen (parm->name);
6458 bfd_boolean writing = FALSE;
699733f6 6459 char sym[16];
deb0e272 6460
4dfe6ac6
NC
6461 if (htab == NULL)
6462 return FALSE;
6463
deb0e272
AM
6464 memcpy (sym, parm->name, len);
6465 sym[len + 2] = 0;
6466
6467 for (i = parm->lo; i <= parm->hi; i++)
6468 {
6469 struct elf_link_hash_entry *h;
6470
6471 sym[len + 0] = i / 10 + '0';
6472 sym[len + 1] = i % 10 + '0';
6473 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6474 if (h != NULL
f5385ebf 6475 && !h->def_regular)
deb0e272
AM
6476 {
6477 h->root.type = bfd_link_hash_defined;
6478 h->root.u.def.section = htab->sfpr;
6479 h->root.u.def.value = htab->sfpr->size;
6480 h->type = STT_FUNC;
f5385ebf 6481 h->def_regular = 1;
deb0e272
AM
6482 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6483 writing = TRUE;
6484 if (htab->sfpr->contents == NULL)
6485 {
6486 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6487 if (htab->sfpr->contents == NULL)
6488 return FALSE;
6489 }
6490 }
6491 if (writing)
6492 {
6493 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6494 if (i != parm->hi)
6495 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6496 else
6497 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6498 htab->sfpr->size = p - htab->sfpr->contents;
6499 }
6500 }
6501
6502 return TRUE;
6503}
6504
6505static bfd_byte *
6506savegpr0 (bfd *abfd, bfd_byte *p, int r)
6507{
6508 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6509 return p + 4;
6510}
6511
6512static bfd_byte *
6513savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6514{
6515 p = savegpr0 (abfd, p, r);
a078d95a 6516 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6517 p = p + 4;
6518 bfd_put_32 (abfd, BLR, p);
6519 return p + 4;
6520}
6521
6522static bfd_byte *
6523restgpr0 (bfd *abfd, bfd_byte *p, int r)
6524{
6525 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6526 return p + 4;
6527}
6528
6529static bfd_byte *
6530restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6531{
a078d95a 6532 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6533 p = p + 4;
6534 p = restgpr0 (abfd, p, r);
6535 bfd_put_32 (abfd, MTLR_R0, p);
6536 p = p + 4;
6537 if (r == 29)
6538 {
6539 p = restgpr0 (abfd, p, 30);
6540 p = restgpr0 (abfd, p, 31);
6541 }
6542 bfd_put_32 (abfd, BLR, p);
6543 return p + 4;
6544}
6545
6546static bfd_byte *
6547savegpr1 (bfd *abfd, bfd_byte *p, int r)
6548{
6549 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6550 return p + 4;
6551}
6552
6553static bfd_byte *
6554savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6555{
6556 p = savegpr1 (abfd, p, r);
6557 bfd_put_32 (abfd, BLR, p);
6558 return p + 4;
6559}
6560
6561static bfd_byte *
6562restgpr1 (bfd *abfd, bfd_byte *p, int r)
6563{
6564 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6565 return p + 4;
6566}
6567
6568static bfd_byte *
6569restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6570{
6571 p = restgpr1 (abfd, p, r);
6572 bfd_put_32 (abfd, BLR, p);
6573 return p + 4;
6574}
6575
6576static bfd_byte *
6577savefpr (bfd *abfd, bfd_byte *p, int r)
6578{
6579 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6580 return p + 4;
6581}
6582
6583static bfd_byte *
6584savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6585{
6586 p = savefpr (abfd, p, r);
a078d95a 6587 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6588 p = p + 4;
6589 bfd_put_32 (abfd, BLR, p);
6590 return p + 4;
6591}
6592
6593static bfd_byte *
6594restfpr (bfd *abfd, bfd_byte *p, int r)
6595{
6596 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6597 return p + 4;
6598}
6599
6600static bfd_byte *
6601restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6602{
a078d95a 6603 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6604 p = p + 4;
6605 p = restfpr (abfd, p, r);
6606 bfd_put_32 (abfd, MTLR_R0, p);
6607 p = p + 4;
6608 if (r == 29)
6609 {
6610 p = restfpr (abfd, p, 30);
6611 p = restfpr (abfd, p, 31);
6612 }
6613 bfd_put_32 (abfd, BLR, p);
6614 return p + 4;
6615}
6616
6617static bfd_byte *
6618savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6619{
6620 p = savefpr (abfd, p, r);
6621 bfd_put_32 (abfd, BLR, p);
6622 return p + 4;
6623}
6624
6625static bfd_byte *
6626restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6627{
6628 p = restfpr (abfd, p, r);
6629 bfd_put_32 (abfd, BLR, p);
6630 return p + 4;
6631}
6632
6633static bfd_byte *
6634savevr (bfd *abfd, bfd_byte *p, int r)
6635{
6636 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6637 p = p + 4;
6638 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6639 return p + 4;
6640}
6641
6642static bfd_byte *
6643savevr_tail (bfd *abfd, bfd_byte *p, int r)
6644{
6645 p = savevr (abfd, p, r);
6646 bfd_put_32 (abfd, BLR, p);
6647 return p + 4;
6648}
6649
6650static bfd_byte *
6651restvr (bfd *abfd, bfd_byte *p, int r)
6652{
6653 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6654 p = p + 4;
6655 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6656 return p + 4;
6657}
6658
6659static bfd_byte *
6660restvr_tail (bfd *abfd, bfd_byte *p, int r)
6661{
6662 p = restvr (abfd, p, r);
6663 bfd_put_32 (abfd, BLR, p);
6664 return p + 4;
6665}
6666
e86ce104
AM
6667/* Called via elf_link_hash_traverse to transfer dynamic linking
6668 information on function code symbol entries to their corresponding
6669 function descriptor symbol entries. */
deb0e272 6670
b34976b6 6671static bfd_boolean
4ce794b7 6672func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6673{
e86ce104 6674 struct bfd_link_info *info;
65f38f15 6675 struct ppc_link_hash_table *htab;
411e1bfb 6676 struct plt_entry *ent;
50bc7936
AM
6677 struct ppc_link_hash_entry *fh;
6678 struct ppc_link_hash_entry *fdh;
6679 bfd_boolean force_local;
5bd4f169 6680
50bc7936
AM
6681 fh = (struct ppc_link_hash_entry *) h;
6682 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6683 return TRUE;
e86ce104 6684
4ce794b7 6685 info = inf;
65f38f15 6686 htab = ppc_hash_table (info);
4dfe6ac6
NC
6687 if (htab == NULL)
6688 return FALSE;
5bd4f169 6689
c09bdfe5
AM
6690 /* Resolve undefined references to dot-symbols as the value
6691 in the function descriptor, if we have one in a regular object.
6692 This is to satisfy cases like ".quad .foo". Calls to functions
6693 in dynamic objects are handled elsewhere. */
6694 if (fh->elf.root.type == bfd_link_hash_undefweak
6695 && fh->was_undefined
b31867b6
AM
6696 && (fdh = defined_func_desc (fh)) != NULL
6697 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6698 && opd_entry_value (fdh->elf.root.u.def.section,
6699 fdh->elf.root.u.def.value,
c09bdfe5 6700 &fh->elf.root.u.def.section,
aef36ac1 6701 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6702 {
b31867b6 6703 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6704 fh->elf.forced_local = 1;
b31867b6
AM
6705 fh->elf.def_regular = fdh->elf.def_regular;
6706 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6707 }
6708
e86ce104
AM
6709 /* If this is a function code symbol, transfer dynamic linking
6710 information to the function descriptor symbol. */
50bc7936 6711 if (!fh->is_func)
b34976b6 6712 return TRUE;
e86ce104 6713
50bc7936 6714 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6715 if (ent->plt.refcount > 0)
6716 break;
50bc7936
AM
6717 if (ent == NULL
6718 || fh->elf.root.root.string[0] != '.'
6719 || fh->elf.root.root.string[1] == '\0')
6720 return TRUE;
5bd4f169 6721
50bc7936
AM
6722 /* Find the corresponding function descriptor symbol. Create it
6723 as undefined if necessary. */
5bd4f169 6724
b31867b6 6725 fdh = lookup_fdh (fh, htab);
50bc7936 6726 if (fdh == NULL
df131623 6727 && !info->executable
50bc7936
AM
6728 && (fh->elf.root.type == bfd_link_hash_undefined
6729 || fh->elf.root.type == bfd_link_hash_undefweak))
6730 {
908b32fc 6731 fdh = make_fdh (info, fh);
bb700d78
AM
6732 if (fdh == NULL)
6733 return FALSE;
50bc7936 6734 }
648cca2c 6735
908b32fc 6736 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6737 code symbol is strong undefined, make the fake sym the same.
6738 If the function code symbol is defined, then force the fake
6739 descriptor local; We can't support overriding of symbols in a
6740 shared library on a fake descriptor. */
908b32fc
AM
6741
6742 if (fdh != NULL
6743 && fdh->fake
433817dd 6744 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6745 {
433817dd
AM
6746 if (fh->elf.root.type == bfd_link_hash_undefined)
6747 {
6748 fdh->elf.root.type = bfd_link_hash_undefined;
6749 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6750 }
6751 else if (fh->elf.root.type == bfd_link_hash_defined
6752 || fh->elf.root.type == bfd_link_hash_defweak)
6753 {
6754 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6755 }
908b32fc
AM
6756 }
6757
50bc7936 6758 if (fdh != NULL
f5385ebf 6759 && !fdh->elf.forced_local
df131623 6760 && (!info->executable
f5385ebf
AM
6761 || fdh->elf.def_dynamic
6762 || fdh->elf.ref_dynamic
50bc7936
AM
6763 || (fdh->elf.root.type == bfd_link_hash_undefweak
6764 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6765 {
6766 if (fdh->elf.dynindx == -1)
c152c796 6767 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6768 return FALSE;
f5385ebf
AM
6769 fdh->elf.ref_regular |= fh->elf.ref_regular;
6770 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6771 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6772 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6773 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6774 {
40d16e0b 6775 move_plt_plist (fh, fdh);
f5385ebf 6776 fdh->elf.needs_plt = 1;
e86ce104 6777 }
50bc7936 6778 fdh->is_func_descriptor = 1;
34814b9f
AM
6779 fdh->oh = fh;
6780 fh->oh = fdh;
e86ce104
AM
6781 }
6782
50bc7936
AM
6783 /* Now that the info is on the function descriptor, clear the
6784 function code sym info. Any function code syms for which we
6785 don't have a definition in a regular file, we force local.
6786 This prevents a shared library from exporting syms that have
6787 been imported from another library. Function code syms that
6788 are really in the library we must leave global to prevent the
6789 linker dragging in a definition from a static library. */
93f3fa99
AM
6790 force_local = (!fh->elf.def_regular
6791 || fdh == NULL
6792 || !fdh->elf.def_regular
6793 || fdh->elf.forced_local);
50bc7936
AM
6794 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6795
b34976b6 6796 return TRUE;
e86ce104 6797}
40b8271b 6798
e86ce104 6799/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6800 this hook to a) provide some gcc support functions, and b) transfer
6801 dynamic linking information gathered so far on function code symbol
6802 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6803
b34976b6 6804static bfd_boolean
4ce794b7
AM
6805ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6806 struct bfd_link_info *info)
e86ce104
AM
6807{
6808 struct ppc_link_hash_table *htab;
82bd7b59 6809 unsigned int i;
27fc25a1 6810 static const struct sfpr_def_parms funcs[] =
deb0e272
AM
6811 {
6812 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6813 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6814 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6815 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6816 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6817 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6818 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6819 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6820 { "._savef", 14, 31, savefpr, savefpr1_tail },
6821 { "._restf", 14, 31, restfpr, restfpr1_tail },
6822 { "_savevr_", 20, 31, savevr, savevr_tail },
6823 { "_restvr_", 20, 31, restvr, restvr_tail }
6824 };
e86ce104
AM
6825
6826 htab = ppc_hash_table (info);
4dfe6ac6
NC
6827 if (htab == NULL)
6828 return FALSE;
6829
5295321c
AM
6830 if (!info->relocatable
6831 && htab->elf.hgot != NULL)
6832 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
c66bb0ee 6833
82bd7b59
AM
6834 if (htab->sfpr == NULL)
6835 /* We don't have any relocs. */
b34976b6 6836 return TRUE;
82bd7b59 6837
deb0e272
AM
6838 /* Provide any missing _save* and _rest* functions. */
6839 htab->sfpr->size = 0;
27fc25a1
AM
6840 if (!info->relocatable)
6841 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6842 if (!sfpr_define (info, &funcs[i]))
6843 return FALSE;
82bd7b59 6844
4ce794b7 6845 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6846
eea6121a 6847 if (htab->sfpr->size == 0)
8423293d 6848 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6849
b34976b6 6850 return TRUE;
e86ce104
AM
6851}
6852
6853/* Adjust a symbol defined by a dynamic object and referenced by a
6854 regular object. The current definition is in some section of the
6855 dynamic object, but we're not including those sections. We have to
6856 change the definition to something the rest of the link can
6857 understand. */
6858
b34976b6 6859static bfd_boolean
4ce794b7
AM
6860ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6861 struct elf_link_hash_entry *h)
e86ce104
AM
6862{
6863 struct ppc_link_hash_table *htab;
e86ce104 6864 asection *s;
e86ce104
AM
6865
6866 htab = ppc_hash_table (info);
4dfe6ac6
NC
6867 if (htab == NULL)
6868 return FALSE;
e86ce104
AM
6869
6870 /* Deal with function syms. */
6871 if (h->type == STT_FUNC
e054468f 6872 || h->type == STT_GNU_IFUNC
f5385ebf 6873 || h->needs_plt)
e86ce104
AM
6874 {
6875 /* Clear procedure linkage table information for any symbol that
6876 won't need a .plt entry. */
411e1bfb
AM
6877 struct plt_entry *ent;
6878 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6879 if (ent->plt.refcount > 0)
6880 break;
8387904d 6881 if (ent == NULL
e054468f
AM
6882 || (h->type != STT_GNU_IFUNC
6883 && (SYMBOL_CALLS_LOCAL (info, h)
6884 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6885 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6886 {
411e1bfb 6887 h->plt.plist = NULL;
f5385ebf 6888 h->needs_plt = 0;
40b8271b 6889 }
5bd4f169 6890 }
bbd7ec4a 6891 else
411e1bfb 6892 h->plt.plist = NULL;
5bd4f169
AM
6893
6894 /* If this is a weak symbol, and there is a real definition, the
6895 processor independent code will have arranged for us to see the
6896 real definition first, and we can just use the same value. */
f6e332e6 6897 if (h->u.weakdef != NULL)
5bd4f169 6898 {
f6e332e6
AM
6899 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6900 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6901 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6902 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6903 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6904 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6905 return TRUE;
5bd4f169
AM
6906 }
6907
5bd4f169
AM
6908 /* If we are creating a shared library, we must presume that the
6909 only references to the symbol are via the global offset table.
6910 For such cases we need not do anything here; the relocations will
6911 be handled correctly by relocate_section. */
6912 if (info->shared)
b34976b6 6913 return TRUE;
5bd4f169 6914
65f38f15
AM
6915 /* If there are no references to this symbol that do not use the
6916 GOT, we don't need to generate a copy reloc. */
f5385ebf 6917 if (!h->non_got_ref)
b34976b6 6918 return TRUE;
65f38f15 6919
b186458a
JJ
6920 /* Don't generate a copy reloc for symbols defined in the executable. */
6921 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6922 return TRUE;
6923
f4656909 6924 if (ELIMINATE_COPY_RELOCS)
65f38f15 6925 {
f4656909 6926 struct ppc_link_hash_entry * eh;
6061a67d 6927 struct elf_dyn_relocs *p;
65f38f15 6928
f4656909
AM
6929 eh = (struct ppc_link_hash_entry *) h;
6930 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6931 {
6932 s = p->sec->output_section;
6933 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6934 break;
6935 }
6936
6937 /* If we didn't find any dynamic relocs in read-only sections, then
6938 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6939 if (p == NULL)
6940 {
f5385ebf 6941 h->non_got_ref = 0;
f4656909
AM
6942 return TRUE;
6943 }
65f38f15
AM
6944 }
6945
5d35169e 6946 if (h->plt.plist != NULL)
97b639ba
AM
6947 {
6948 /* We should never get here, but unfortunately there are versions
6949 of gcc out there that improperly (for this ABI) put initialized
6950 function pointers, vtable refs and suchlike in read-only
6951 sections. Allow them to proceed, but warn that this might
6952 break at runtime. */
25f53a85 6953 info->callbacks->einfo
bc30df16 6954 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 6955 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6956 h->root.root.string);
6957 }
5d35169e
AM
6958
6959 /* This is a reference to a symbol defined by a dynamic object which
6960 is not a function. */
6961
5bd4f169
AM
6962 /* We must allocate the symbol in our .dynbss section, which will
6963 become part of the .bss section of the executable. There will be
6964 an entry for this symbol in the .dynsym section. The dynamic
6965 object will contain position independent code, so all references
6966 from the dynamic object to this symbol will go through the global
6967 offset table. The dynamic linker will use the .dynsym entry to
6968 determine the address it must put in the global offset table, so
6969 both the dynamic object and the regular object will refer to the
6970 same memory location for the variable. */
5bd4f169 6971
04c9666a
AM
6972 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6973 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6974 runtime process image. We need to remember the offset into the
6975 .rela.bss section we are going to use. */
1d7e9d18 6976 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6977 {
eea6121a 6978 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6979 h->needs_copy = 1;
5bd4f169
AM
6980 }
6981
4ce794b7 6982 s = htab->dynbss;
5bd4f169 6983
027297b7 6984 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6985}
6986
e86ce104
AM
6987/* If given a function descriptor symbol, hide both the function code
6988 sym and the descriptor. */
6989static void
4ce794b7
AM
6990ppc64_elf_hide_symbol (struct bfd_link_info *info,
6991 struct elf_link_hash_entry *h,
6992 bfd_boolean force_local)
e86ce104 6993{
34814b9f 6994 struct ppc_link_hash_entry *eh;
e86ce104
AM
6995 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6996
34814b9f
AM
6997 eh = (struct ppc_link_hash_entry *) h;
6998 if (eh->is_func_descriptor)
e86ce104 6999 {
34814b9f 7000 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7001
721956f4 7002 if (fh == NULL)
d1329ca3
AM
7003 {
7004 const char *p, *q;
7005 struct ppc_link_hash_table *htab;
7006 char save;
7007
7008 /* We aren't supposed to use alloca in BFD because on
7009 systems which do not have alloca the version in libiberty
7010 calls xmalloc, which might cause the program to crash
7011 when it runs out of memory. This function doesn't have a
7012 return status, so there's no way to gracefully return an
7013 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7014 accessed; It's either a string in an ELF string table,
7015 or allocated in an objalloc structure. */
d1329ca3 7016
34814b9f 7017 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7018 save = *p;
7019 *(char *) p = '.';
7020 htab = ppc_hash_table (info);
4dfe6ac6
NC
7021 if (htab == NULL)
7022 return;
7023
34814b9f
AM
7024 fh = (struct ppc_link_hash_entry *)
7025 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7026 *(char *) p = save;
7027
7028 /* Unfortunately, if it so happens that the string we were
7029 looking for was allocated immediately before this string,
7030 then we overwrote the string terminator. That's the only
7031 reason the lookup should fail. */
7032 if (fh == NULL)
7033 {
34814b9f
AM
7034 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7035 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7036 --q, --p;
34814b9f
AM
7037 if (q < eh->elf.root.root.string && *p == '.')
7038 fh = (struct ppc_link_hash_entry *)
7039 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7040 }
7041 if (fh != NULL)
7042 {
34814b9f
AM
7043 eh->oh = fh;
7044 fh->oh = eh;
d1329ca3
AM
7045 }
7046 }
e86ce104 7047 if (fh != NULL)
34814b9f 7048 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7049 }
7050}
7051
411e1bfb 7052static bfd_boolean
8843416a
AM
7053get_sym_h (struct elf_link_hash_entry **hp,
7054 Elf_Internal_Sym **symp,
7055 asection **symsecp,
f961d9dd 7056 unsigned char **tls_maskp,
8843416a
AM
7057 Elf_Internal_Sym **locsymsp,
7058 unsigned long r_symndx,
7059 bfd *ibfd)
411e1bfb 7060{
0ffa91dd 7061 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7062
7063 if (r_symndx >= symtab_hdr->sh_info)
7064 {
7065 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7066 struct elf_link_hash_entry *h;
7067
7068 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7069 h = elf_follow_link (h);
411e1bfb
AM
7070
7071 if (hp != NULL)
7072 *hp = h;
7073
7074 if (symp != NULL)
7075 *symp = NULL;
7076
7077 if (symsecp != NULL)
7078 {
7079 asection *symsec = NULL;
7080 if (h->root.type == bfd_link_hash_defined
7081 || h->root.type == bfd_link_hash_defweak)
7082 symsec = h->root.u.def.section;
7083 *symsecp = symsec;
7084 }
7085
e7b938ca 7086 if (tls_maskp != NULL)
411e1bfb
AM
7087 {
7088 struct ppc_link_hash_entry *eh;
7089
7090 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7091 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7092 }
7093 }
7094 else
7095 {
7096 Elf_Internal_Sym *sym;
7097 Elf_Internal_Sym *locsyms = *locsymsp;
7098
7099 if (locsyms == NULL)
7100 {
7101 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7102 if (locsyms == NULL)
7103 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7104 symtab_hdr->sh_info,
7105 0, NULL, NULL, NULL);
7106 if (locsyms == NULL)
7107 return FALSE;
7108 *locsymsp = locsyms;
7109 }
7110 sym = locsyms + r_symndx;
7111
7112 if (hp != NULL)
7113 *hp = NULL;
7114
7115 if (symp != NULL)
7116 *symp = sym;
7117
7118 if (symsecp != NULL)
cb33740c 7119 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7120
e7b938ca 7121 if (tls_maskp != NULL)
411e1bfb
AM
7122 {
7123 struct got_entry **lgot_ents;
f961d9dd 7124 unsigned char *tls_mask;
411e1bfb 7125
e7b938ca 7126 tls_mask = NULL;
411e1bfb
AM
7127 lgot_ents = elf_local_got_ents (ibfd);
7128 if (lgot_ents != NULL)
7129 {
e054468f
AM
7130 struct plt_entry **local_plt = (struct plt_entry **)
7131 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7132 unsigned char *lgot_masks = (unsigned char *)
e054468f 7133 (local_plt + symtab_hdr->sh_info);
e7b938ca 7134 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7135 }
e7b938ca 7136 *tls_maskp = tls_mask;
411e1bfb
AM
7137 }
7138 }
7139 return TRUE;
7140}
7141
e7b938ca 7142/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7143 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7144 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7145
7146static int
f961d9dd 7147get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7148 unsigned long *toc_symndx,
7149 bfd_vma *toc_addend,
0d4792f7 7150 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7151 const Elf_Internal_Rela *rel,
7152 bfd *ibfd)
411e1bfb
AM
7153{
7154 unsigned long r_symndx;
0d4792f7 7155 int next_r;
411e1bfb
AM
7156 struct elf_link_hash_entry *h;
7157 Elf_Internal_Sym *sym;
7158 asection *sec;
7159 bfd_vma off;
7160
7161 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7162 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7163 return 0;
411e1bfb 7164
e7b938ca 7165 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7166 || sec == NULL
6bee8834 7167 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7168 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7169 return 1;
411e1bfb
AM
7170
7171 /* Look inside a TOC section too. */
7172 if (h != NULL)
7173 {
7174 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7175 off = h->root.u.def.value;
7176 }
7177 else
7178 off = sym->st_value;
7179 off += rel->r_addend;
7180 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7181 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7182 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7183 if (toc_symndx != NULL)
7184 *toc_symndx = r_symndx;
3a71aa26
AM
7185 if (toc_addend != NULL)
7186 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7187 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7188 return 0;
854b41e7 7189 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7190 && (next_r == -1 || next_r == -2))
7191 return 1 - next_r;
951fd09b 7192 return 1;
411e1bfb
AM
7193}
7194
3b421ab3
AM
7195/* Find (or create) an entry in the tocsave hash table. */
7196
7197static struct tocsave_entry *
7198tocsave_find (struct ppc_link_hash_table *htab,
7199 enum insert_option insert,
7200 Elf_Internal_Sym **local_syms,
7201 const Elf_Internal_Rela *irela,
7202 bfd *ibfd)
7203{
7204 unsigned long r_indx;
7205 struct elf_link_hash_entry *h;
7206 Elf_Internal_Sym *sym;
7207 struct tocsave_entry ent, *p;
7208 hashval_t hash;
7209 struct tocsave_entry **slot;
7210
7211 r_indx = ELF64_R_SYM (irela->r_info);
7212 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7213 return NULL;
7214 if (ent.sec == NULL || ent.sec->output_section == NULL)
7215 {
7216 (*_bfd_error_handler)
7217 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7218 return NULL;
7219 }
7220
7221 if (h != NULL)
7222 ent.offset = h->root.u.def.value;
7223 else
7224 ent.offset = sym->st_value;
7225 ent.offset += irela->r_addend;
7226
7227 hash = tocsave_htab_hash (&ent);
7228 slot = ((struct tocsave_entry **)
7229 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7230 if (slot == NULL)
7231 return NULL;
7232
7233 if (*slot == NULL)
7234 {
7235 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7236 if (p == NULL)
7237 return NULL;
7238 *p = ent;
7239 *slot = p;
7240 }
7241 return *slot;
7242}
7243
754021d0 7244/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7245 code for the old ABI, these will already have been done. */
754021d0
AM
7246
7247static bfd_boolean
7248adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7249{
7250 struct ppc_link_hash_entry *eh;
7251 asection *sym_sec;
74f0fb50 7252 struct _opd_sec_data *opd;
754021d0
AM
7253
7254 if (h->root.type == bfd_link_hash_indirect)
7255 return TRUE;
7256
754021d0
AM
7257 if (h->root.type != bfd_link_hash_defined
7258 && h->root.type != bfd_link_hash_defweak)
7259 return TRUE;
7260
7261 eh = (struct ppc_link_hash_entry *) h;
7262 if (eh->adjust_done)
7263 return TRUE;
7264
7265 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7266 opd = get_opd_info (sym_sec);
7267 if (opd != NULL && opd->adjust != NULL)
754021d0 7268 {
74f0fb50 7269 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
7270 if (adjust == -1)
7271 {
7272 /* This entry has been deleted. */
b3fac117 7273 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7274 if (dsec == NULL)
7275 {
7276 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7277 if (discarded_section (dsec))
81688140 7278 {
b3fac117 7279 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7280 break;
7281 }
7282 }
4025353c 7283 eh->elf.root.u.def.value = 0;
81688140 7284 eh->elf.root.u.def.section = dsec;
4025353c
AM
7285 }
7286 else
7287 eh->elf.root.u.def.value += adjust;
754021d0
AM
7288 eh->adjust_done = 1;
7289 }
7290 return TRUE;
7291}
7292
8c1d1bb8 7293/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7294 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7295 have already been determined. */
7296
7297static bfd_boolean
7298dec_dynrel_count (bfd_vma r_info,
7299 asection *sec,
7300 struct bfd_link_info *info,
7301 Elf_Internal_Sym **local_syms,
7302 struct elf_link_hash_entry *h,
19e08130 7303 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7304{
7305 enum elf_ppc64_reloc_type r_type;
19e08130 7306 asection *sym_sec = NULL;
8c1d1bb8
AM
7307
7308 /* Can this reloc be dynamic? This switch, and later tests here
7309 should be kept in sync with the code in check_relocs. */
7310 r_type = ELF64_R_TYPE (r_info);
7311 switch (r_type)
7312 {
7313 default:
7314 return TRUE;
7315
7316 case R_PPC64_TPREL16:
7317 case R_PPC64_TPREL16_LO:
7318 case R_PPC64_TPREL16_HI:
7319 case R_PPC64_TPREL16_HA:
7320 case R_PPC64_TPREL16_DS:
7321 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7322 case R_PPC64_TPREL16_HIGH:
7323 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7324 case R_PPC64_TPREL16_HIGHER:
7325 case R_PPC64_TPREL16_HIGHERA:
7326 case R_PPC64_TPREL16_HIGHEST:
7327 case R_PPC64_TPREL16_HIGHESTA:
7328 if (!info->shared)
7329 return TRUE;
7330
7331 case R_PPC64_TPREL64:
7332 case R_PPC64_DTPMOD64:
7333 case R_PPC64_DTPREL64:
7334 case R_PPC64_ADDR64:
7335 case R_PPC64_REL30:
7336 case R_PPC64_REL32:
7337 case R_PPC64_REL64:
7338 case R_PPC64_ADDR14:
7339 case R_PPC64_ADDR14_BRNTAKEN:
7340 case R_PPC64_ADDR14_BRTAKEN:
7341 case R_PPC64_ADDR16:
7342 case R_PPC64_ADDR16_DS:
7343 case R_PPC64_ADDR16_HA:
7344 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7345 case R_PPC64_ADDR16_HIGH:
7346 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7347 case R_PPC64_ADDR16_HIGHER:
7348 case R_PPC64_ADDR16_HIGHERA:
7349 case R_PPC64_ADDR16_HIGHEST:
7350 case R_PPC64_ADDR16_HIGHESTA:
7351 case R_PPC64_ADDR16_LO:
7352 case R_PPC64_ADDR16_LO_DS:
7353 case R_PPC64_ADDR24:
7354 case R_PPC64_ADDR32:
7355 case R_PPC64_UADDR16:
7356 case R_PPC64_UADDR32:
7357 case R_PPC64_UADDR64:
7358 case R_PPC64_TOC:
7359 break;
7360 }
7361
7362 if (local_syms != NULL)
7363 {
7364 unsigned long r_symndx;
8c1d1bb8
AM
7365 bfd *ibfd = sec->owner;
7366
7367 r_symndx = ELF64_R_SYM (r_info);
7368 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7369 return FALSE;
7370 }
7371
7372 if ((info->shared
1d483afe 7373 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7374 || (h != NULL
198f1157 7375 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7376 || h->root.type == bfd_link_hash_defweak
7377 || !h->def_regular))))
7378 || (ELIMINATE_COPY_RELOCS
7379 && !info->shared
7380 && h != NULL
7381 && (h->root.type == bfd_link_hash_defweak
7382 || !h->def_regular)))
7383 ;
7384 else
7385 return TRUE;
7386
7387 if (h != NULL)
6edfbbad 7388 {
19e08130
AM
7389 struct elf_dyn_relocs *p;
7390 struct elf_dyn_relocs **pp;
7391 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7392
7393 /* elf_gc_sweep may have already removed all dyn relocs associated
7394 with local syms for a given section. Also, symbol flags are
7395 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7396 report a dynreloc miscount. */
7397 if (*pp == NULL && info->gc_sections)
7398 return TRUE;
7399
7400 while ((p = *pp) != NULL)
60124e18 7401 {
19e08130
AM
7402 if (p->sec == sec)
7403 {
7404 if (!must_be_dyn_reloc (info, r_type))
7405 p->pc_count -= 1;
7406 p->count -= 1;
7407 if (p->count == 0)
7408 *pp = p->next;
7409 return TRUE;
7410 }
7411 pp = &p->next;
60124e18 7412 }
6edfbbad 7413 }
19e08130
AM
7414 else
7415 {
7416 struct ppc_dyn_relocs *p;
7417 struct ppc_dyn_relocs **pp;
7418 void *vpp;
7419 bfd_boolean is_ifunc;
8c1d1bb8 7420
19e08130
AM
7421 if (local_syms == NULL)
7422 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7423 if (sym_sec == NULL)
7424 sym_sec = sec;
c57da1a7 7425
19e08130
AM
7426 vpp = &elf_section_data (sym_sec)->local_dynrel;
7427 pp = (struct ppc_dyn_relocs **) vpp;
7428
7429 if (*pp == NULL && info->gc_sections)
7430 return TRUE;
7431
7432 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7433 while ((p = *pp) != NULL)
8c1d1bb8 7434 {
19e08130
AM
7435 if (p->sec == sec && p->ifunc == is_ifunc)
7436 {
7437 p->count -= 1;
7438 if (p->count == 0)
7439 *pp = p->next;
7440 return TRUE;
7441 }
7442 pp = &p->next;
8c1d1bb8 7443 }
8c1d1bb8
AM
7444 }
7445
8de848d8 7446 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7447 sec->owner, sec);
8c1d1bb8
AM
7448 bfd_set_error (bfd_error_bad_value);
7449 return FALSE;
7450}
7451
754021d0
AM
7452/* Remove unused Official Procedure Descriptor entries. Currently we
7453 only remove those associated with functions in discarded link-once
7454 sections, or weakly defined functions that have been overridden. It
7455 would be possible to remove many more entries for statically linked
7456 applications. */
7457
b34976b6 7458bfd_boolean
33c0ec9d 7459ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
7460{
7461 bfd *ibfd;
754021d0 7462 bfd_boolean some_edited = FALSE;
3f764659 7463 asection *need_pad = NULL;
1e2f5b6e 7464
411e1bfb 7465 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
7466 {
7467 asection *sec;
7468 Elf_Internal_Rela *relstart, *rel, *relend;
7469 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7470 Elf_Internal_Sym *local_syms;
1e2f5b6e 7471 bfd_vma offset;
74f0fb50 7472 struct _opd_sec_data *opd;
3f764659
JJ
7473 bfd_boolean need_edit, add_aux_fields;
7474 bfd_size_type cnt_16b = 0;
1e2f5b6e 7475
854b41e7
AM
7476 if (!is_ppc64_elf (ibfd))
7477 continue;
7478
1e2f5b6e 7479 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7480 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7481 continue;
7482
dbaa2011 7483 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7484 continue;
7485
1e2f5b6e
AM
7486 if (sec->output_section == bfd_abs_section_ptr)
7487 continue;
7488
7489 /* Look through the section relocs. */
7490 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7491 continue;
7492
6cdc0ccc 7493 local_syms = NULL;
0ffa91dd 7494 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7495
7496 /* Read the relocations. */
4ce794b7 7497 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7498 info->keep_memory);
1e2f5b6e 7499 if (relstart == NULL)
b34976b6 7500 return FALSE;
1e2f5b6e
AM
7501
7502 /* First run through the relocs to check they are sane, and to
7503 determine whether we need to edit this opd section. */
b34976b6 7504 need_edit = FALSE;
3f764659 7505 need_pad = sec;
1e2f5b6e
AM
7506 offset = 0;
7507 relend = relstart + sec->reloc_count;
50bc7936 7508 for (rel = relstart; rel < relend; )
1e2f5b6e 7509 {
04c9666a 7510 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7511 unsigned long r_symndx;
7512 asection *sym_sec;
7513 struct elf_link_hash_entry *h;
7514 Elf_Internal_Sym *sym;
7515
3f764659 7516 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7517 only interested in the reloc pointing to a function entry
7518 point. */
50bc7936
AM
7519 if (rel->r_offset != offset
7520 || rel + 1 >= relend
7521 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
7522 {
7523 /* If someone messes with .opd alignment then after a
7524 "ld -r" we might have padding in the middle of .opd.
7525 Also, there's nothing to prevent someone putting
7526 something silly in .opd with the assembler. No .opd
b34976b6 7527 optimization for them! */
3f764659 7528 broken_opd:
1e2f5b6e 7529 (*_bfd_error_handler)
d003868e 7530 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 7531 need_edit = FALSE;
1e2f5b6e
AM
7532 break;
7533 }
7534
50bc7936
AM
7535 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7536 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7537 {
7538 (*_bfd_error_handler)
d003868e
AM
7539 (_("%B: unexpected reloc type %u in .opd section"),
7540 ibfd, r_type);
50bc7936
AM
7541 need_edit = FALSE;
7542 break;
7543 }
7544
1e2f5b6e 7545 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7546 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7547 r_symndx, ibfd))
50bc7936 7548 goto error_ret;
1e2f5b6e
AM
7549
7550 if (sym_sec == NULL || sym_sec->owner == NULL)
7551 {
411e1bfb
AM
7552 const char *sym_name;
7553 if (h != NULL)
7554 sym_name = h->root.root.string;
7555 else
26c61ae5
L
7556 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7557 sym_sec);
411e1bfb 7558
1e2f5b6e 7559 (*_bfd_error_handler)
d003868e
AM
7560 (_("%B: undefined sym `%s' in .opd section"),
7561 ibfd, sym_name);
b34976b6 7562 need_edit = FALSE;
1e2f5b6e
AM
7563 break;
7564 }
7565
51020317
AM
7566 /* opd entries are always for functions defined in the
7567 current input bfd. If the symbol isn't defined in the
7568 input bfd, then we won't be using the function in this
7569 bfd; It must be defined in a linkonce section in another
7570 bfd, or is weak. It's also possible that we are
7571 discarding the function due to a linker script /DISCARD/,
7572 which we test for via the output_section. */
7573 if (sym_sec->owner != ibfd
7574 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7575 need_edit = TRUE;
1e2f5b6e 7576
50bc7936 7577 rel += 2;
3f764659
JJ
7578 if (rel == relend
7579 || (rel + 1 == relend && rel->r_offset == offset + 16))
7580 {
7581 if (sec->size == offset + 24)
7582 {
7583 need_pad = NULL;
7584 break;
7585 }
7586 if (rel == relend && sec->size == offset + 16)
7587 {
7588 cnt_16b++;
7589 break;
7590 }
7591 goto broken_opd;
7592 }
7593
7594 if (rel->r_offset == offset + 24)
7595 offset += 24;
7596 else if (rel->r_offset != offset + 16)
7597 goto broken_opd;
7598 else if (rel + 1 < relend
7599 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7600 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7601 {
7602 offset += 16;
7603 cnt_16b++;
7604 }
7605 else if (rel + 2 < relend
7606 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7607 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7608 {
7609 offset += 24;
7610 rel += 1;
7611 }
7612 else
7613 goto broken_opd;
1e2f5b6e
AM
7614 }
7615
3f764659
JJ
7616 add_aux_fields = non_overlapping && cnt_16b > 0;
7617
7618 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7619 {
7620 Elf_Internal_Rela *write_rel;
d4730f92 7621 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7622 bfd_byte *rptr, *wptr;
983bddc8 7623 bfd_byte *new_contents;
b34976b6 7624 bfd_boolean skip;
3f764659 7625 long opd_ent_size;
74f0fb50
AM
7626 bfd_size_type amt;
7627
983bddc8 7628 new_contents = NULL;
74f0fb50
AM
7629 amt = sec->size * sizeof (long) / 8;
7630 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7631 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7632 if (opd->adjust == NULL)
7633 return FALSE;
7634 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7635
7636 /* This seems a waste of time as input .opd sections are all
7637 zeros as generated by gcc, but I suppose there's no reason
7638 this will always be so. We might start putting something in
7639 the third word of .opd entries. */
7640 if ((sec->flags & SEC_IN_MEMORY) == 0)
7641 {
eea6121a
AM
7642 bfd_byte *loc;
7643 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7644 {
eea6121a
AM
7645 if (loc != NULL)
7646 free (loc);
50bc7936 7647 error_ret:
6cdc0ccc
AM
7648 if (local_syms != NULL
7649 && symtab_hdr->contents != (unsigned char *) local_syms)
7650 free (local_syms);
6cdc0ccc
AM
7651 if (elf_section_data (sec)->relocs != relstart)
7652 free (relstart);
b34976b6 7653 return FALSE;
6cdc0ccc 7654 }
1e2f5b6e
AM
7655 sec->contents = loc;
7656 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7657 }
7658
7659 elf_section_data (sec)->relocs = relstart;
7660
3f764659 7661 new_contents = sec->contents;
3f764659
JJ
7662 if (add_aux_fields)
7663 {
7664 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7665 if (new_contents == NULL)
7666 return FALSE;
7667 need_pad = FALSE;
3f764659 7668 }
b4f4e59f
AM
7669 wptr = new_contents;
7670 rptr = sec->contents;
3f764659 7671
1e2f5b6e 7672 write_rel = relstart;
b34976b6 7673 skip = FALSE;
1e2f5b6e 7674 offset = 0;
3f764659 7675 opd_ent_size = 0;
1e2f5b6e
AM
7676 for (rel = relstart; rel < relend; rel++)
7677 {
50bc7936
AM
7678 unsigned long r_symndx;
7679 asection *sym_sec;
7680 struct elf_link_hash_entry *h;
7681 Elf_Internal_Sym *sym;
7682
7683 r_symndx = ELF64_R_SYM (rel->r_info);
7684 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7685 r_symndx, ibfd))
50bc7936
AM
7686 goto error_ret;
7687
1e2f5b6e
AM
7688 if (rel->r_offset == offset)
7689 {
50bc7936 7690 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7691
7692 /* See if the .opd entry is full 24 byte or
7693 16 byte (with fd_aux entry overlapped with next
7694 fd_func). */
7695 opd_ent_size = 24;
7696 if ((rel + 2 == relend && sec->size == offset + 16)
7697 || (rel + 3 < relend
7698 && rel[2].r_offset == offset + 16
7699 && rel[3].r_offset == offset + 24
7700 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7701 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7702 opd_ent_size = 16;
7703
4025353c
AM
7704 if (h != NULL
7705 && h->root.root.string[0] == '.')
c4f68ce3 7706 {
4dfe6ac6
NC
7707 struct ppc_link_hash_table *htab;
7708
7709 htab = ppc_hash_table (info);
7710 if (htab != NULL)
7711 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7712 htab);
c4f68ce3
AM
7713 if (fdh != NULL
7714 && fdh->elf.root.type != bfd_link_hash_defined
7715 && fdh->elf.root.type != bfd_link_hash_defweak)
7716 fdh = NULL;
7717 }
1e2f5b6e 7718
51020317
AM
7719 skip = (sym_sec->owner != ibfd
7720 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7721 if (skip)
7722 {
4025353c 7723 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7724 {
7725 /* Arrange for the function descriptor sym
7726 to be dropped. */
d6fe2dc1
AM
7727 fdh->elf.root.u.def.value = 0;
7728 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7729 }
74f0fb50 7730 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7731 }
7732 else
1e2f5b6e
AM
7733 {
7734 /* We'll be keeping this opd entry. */
7735
4025353c 7736 if (fdh != NULL)
1e2f5b6e 7737 {
754021d0
AM
7738 /* Redefine the function descriptor symbol to
7739 this location in the opd section. It is
7740 necessary to update the value here rather
7741 than using an array of adjustments as we do
7742 for local symbols, because various places
7743 in the generic ELF code use the value
7744 stored in u.def.value. */
3f764659 7745 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7746 fdh->adjust_done = 1;
1e2f5b6e 7747 }
754021d0
AM
7748
7749 /* Local syms are a bit tricky. We could
7750 tweak them as they can be cached, but
7751 we'd need to look through the local syms
7752 for the function descriptor sym which we
7753 don't have at the moment. So keep an
7754 array of adjustments. */
74f0fb50 7755 opd->adjust[rel->r_offset / 8]
3f764659 7756 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7757
7758 if (wptr != rptr)
3f764659
JJ
7759 memcpy (wptr, rptr, opd_ent_size);
7760 wptr += opd_ent_size;
7761 if (add_aux_fields && opd_ent_size == 16)
7762 {
7763 memset (wptr, '\0', 8);
7764 wptr += 8;
7765 }
1e2f5b6e 7766 }
3f764659
JJ
7767 rptr += opd_ent_size;
7768 offset += opd_ent_size;
1e2f5b6e
AM
7769 }
7770
50bc7936
AM
7771 if (skip)
7772 {
60124e18
AM
7773 if (!NO_OPD_RELOCS
7774 && !info->relocatable
18d944df 7775 && !dec_dynrel_count (rel->r_info, sec, info,
19e08130 7776 NULL, h, sym))
8c1d1bb8 7777 goto error_ret;
50bc7936
AM
7778 }
7779 else
1e2f5b6e 7780 {
50bc7936
AM
7781 /* We need to adjust any reloc offsets to point to the
7782 new opd entries. While we're at it, we may as well
7783 remove redundant relocs. */
74f0fb50 7784 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7785 if (write_rel != rel)
7786 memcpy (write_rel, rel, sizeof (*rel));
7787 ++write_rel;
7788 }
7789 }
7790
3f764659 7791 sec->size = wptr - new_contents;
1e2f5b6e 7792 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7793 if (add_aux_fields)
7794 {
7795 free (sec->contents);
7796 sec->contents = new_contents;
7797 }
7798
05bf9422 7799 /* Fudge the header size too, as this is used later in
cdcf6e38 7800 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7801 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7802 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7803 some_edited = TRUE;
1e2f5b6e 7804 }
6cdc0ccc 7805 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7806 free (relstart);
6cdc0ccc 7807
411e1bfb
AM
7808 if (local_syms != NULL
7809 && symtab_hdr->contents != (unsigned char *) local_syms)
7810 {
7811 if (!info->keep_memory)
7812 free (local_syms);
7813 else
7814 symtab_hdr->contents = (unsigned char *) local_syms;
7815 }
7816 }
7817
754021d0
AM
7818 if (some_edited)
7819 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7820
3f764659
JJ
7821 /* If we are doing a final link and the last .opd entry is just 16 byte
7822 long, add a 8 byte padding after it. */
7823 if (need_pad != NULL && !info->relocatable)
7824 {
7825 bfd_byte *p;
7826
7827 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7828 {
7829 BFD_ASSERT (need_pad->size > 0);
7830
7831 p = bfd_malloc (need_pad->size + 8);
7832 if (p == NULL)
7833 return FALSE;
699733f6 7834
3f764659
JJ
7835 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7836 p, 0, need_pad->size))
7837 return FALSE;
7838
7839 need_pad->contents = p;
7840 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7841 }
7842 else
7843 {
7844 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7845 if (p == NULL)
7846 return FALSE;
7847
7848 need_pad->contents = p;
7849 }
7850
7851 memset (need_pad->contents + need_pad->size, 0, 8);
7852 need_pad->size += 8;
7853 }
7854
411e1bfb
AM
7855 return TRUE;
7856}
7857
e1918d23 7858/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7859
e1918d23 7860asection *
33c0ec9d
AM
7861ppc64_elf_tls_setup (struct bfd_link_info *info,
7862 int no_tls_get_addr_opt,
7863 int *no_multi_toc)
411e1bfb 7864{
411e1bfb
AM
7865 struct ppc_link_hash_table *htab;
7866
411e1bfb 7867 htab = ppc_hash_table (info);
4dfe6ac6
NC
7868 if (htab == NULL)
7869 return NULL;
7870
ee67d69a
AM
7871 if (abiversion (info->output_bfd) == 1)
7872 htab->opd_abi = 1;
7873
33c0ec9d
AM
7874 if (*no_multi_toc)
7875 htab->do_multi_toc = 0;
7876 else if (!htab->do_multi_toc)
7877 *no_multi_toc = 1;
7878
3a71aa26
AM
7879 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7880 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7881 FALSE, FALSE, TRUE));
a7f2871e
AM
7882 /* Move dynamic linking info to the function descriptor sym. */
7883 if (htab->tls_get_addr != NULL)
7884 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7885 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7886 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7887 FALSE, FALSE, TRUE));
a7f2871e
AM
7888 if (!no_tls_get_addr_opt)
7889 {
7890 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7891
7892 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7893 FALSE, FALSE, TRUE);
7894 if (opt != NULL)
7895 func_desc_adjust (opt, info);
7896 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7897 FALSE, FALSE, TRUE);
7898 if (opt_fd != NULL
7899 && (opt_fd->root.type == bfd_link_hash_defined
7900 || opt_fd->root.type == bfd_link_hash_defweak))
7901 {
7902 /* If glibc supports an optimized __tls_get_addr call stub,
7903 signalled by the presence of __tls_get_addr_opt, and we'll
7904 be calling __tls_get_addr via a plt call stub, then
7905 make __tls_get_addr point to __tls_get_addr_opt. */
7906 tga_fd = &htab->tls_get_addr_fd->elf;
7907 if (htab->elf.dynamic_sections_created
7908 && tga_fd != NULL
7909 && (tga_fd->type == STT_FUNC
7910 || tga_fd->needs_plt)
7911 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7912 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7913 && tga_fd->root.type == bfd_link_hash_undefweak)))
7914 {
7915 struct plt_entry *ent;
7916
7917 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7918 if (ent->plt.refcount > 0)
7919 break;
7920 if (ent != NULL)
7921 {
7922 tga_fd->root.type = bfd_link_hash_indirect;
7923 tga_fd->root.u.i.link = &opt_fd->root;
7924 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7925 if (opt_fd->dynindx != -1)
7926 {
7927 /* Use __tls_get_addr_opt in dynamic relocations. */
7928 opt_fd->dynindx = -1;
7929 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7930 opt_fd->dynstr_index);
7931 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7932 return NULL;
a7f2871e
AM
7933 }
7934 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7935 tga = &htab->tls_get_addr->elf;
7936 if (opt != NULL && tga != NULL)
7937 {
7938 tga->root.type = bfd_link_hash_indirect;
7939 tga->root.u.i.link = &opt->root;
7940 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7941 _bfd_elf_link_hash_hide_symbol (info, opt,
7942 tga->forced_local);
7943 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7944 }
7945 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7946 htab->tls_get_addr_fd->is_func_descriptor = 1;
7947 if (htab->tls_get_addr != NULL)
7948 {
7949 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7950 htab->tls_get_addr->is_func = 1;
7951 }
7952 }
7953 }
7954 }
7955 else
7956 no_tls_get_addr_opt = TRUE;
7957 }
7958 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7959 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7960}
8387904d 7961
3a71aa26
AM
7962/* Return TRUE iff REL is a branch reloc with a global symbol matching
7963 HASH1 or HASH2. */
8387904d 7964
3a71aa26
AM
7965static bfd_boolean
7966branch_reloc_hash_match (const bfd *ibfd,
7967 const Elf_Internal_Rela *rel,
7968 const struct ppc_link_hash_entry *hash1,
7969 const struct ppc_link_hash_entry *hash2)
7970{
7971 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7972 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7973 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7974
e054468f 7975 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7976 {
3a71aa26
AM
7977 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7978 struct elf_link_hash_entry *h;
8387904d 7979
3a71aa26 7980 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7981 h = elf_follow_link (h);
3a71aa26
AM
7982 if (h == &hash1->elf || h == &hash2->elf)
7983 return TRUE;
a48ebf4d 7984 }
3a71aa26 7985 return FALSE;
951fd09b 7986}
411e1bfb 7987
951fd09b
AM
7988/* Run through all the TLS relocs looking for optimization
7989 opportunities. The linker has been hacked (see ppc64elf.em) to do
7990 a preliminary section layout so that we know the TLS segment
7991 offsets. We can't optimize earlier because some optimizations need
7992 to know the tp offset, and we need to optimize before allocating
7993 dynamic relocations. */
7994
7995bfd_boolean
33c0ec9d 7996ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7997{
7998 bfd *ibfd;
7999 asection *sec;
8000 struct ppc_link_hash_table *htab;
663a1470 8001 unsigned char *toc_ref;
102890f0 8002 int pass;
951fd09b 8003
1d483afe 8004 if (info->relocatable || !info->executable)
411e1bfb
AM
8005 return TRUE;
8006
951fd09b 8007 htab = ppc_hash_table (info);
4dfe6ac6
NC
8008 if (htab == NULL)
8009 return FALSE;
8010
663a1470
AM
8011 /* Make two passes over the relocs. On the first pass, mark toc
8012 entries involved with tls relocs, and check that tls relocs
8013 involved in setting up a tls_get_addr call are indeed followed by
8014 such a call. If they are not, we can't do any tls optimization.
8015 On the second pass twiddle tls_mask flags to notify
8016 relocate_section that optimization can be done, and adjust got
8017 and plt refcounts. */
8018 toc_ref = NULL;
8019 for (pass = 0; pass < 2; ++pass)
8020 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8021 {
8022 Elf_Internal_Sym *locsyms = NULL;
8023 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8024
102890f0
AM
8025 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8026 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8027 {
8028 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8029 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8030
102890f0
AM
8031 /* Read the relocations. */
8032 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8033 info->keep_memory);
8034 if (relstart == NULL)
8035 return FALSE;
411e1bfb 8036
102890f0
AM
8037 relend = relstart + sec->reloc_count;
8038 for (rel = relstart; rel < relend; rel++)
8039 {
8040 enum elf_ppc64_reloc_type r_type;
8041 unsigned long r_symndx;
8042 struct elf_link_hash_entry *h;
8043 Elf_Internal_Sym *sym;
8044 asection *sym_sec;
f961d9dd
AM
8045 unsigned char *tls_mask;
8046 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8047 bfd_vma value;
8048 bfd_boolean ok_tprel, is_local;
8049 long toc_ref_index = 0;
8050 int expecting_tls_get_addr = 0;
663a1470 8051 bfd_boolean ret = FALSE;
411e1bfb 8052
102890f0
AM
8053 r_symndx = ELF64_R_SYM (rel->r_info);
8054 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8055 r_symndx, ibfd))
8056 {
8057 err_free_rel:
8058 if (elf_section_data (sec)->relocs != relstart)
8059 free (relstart);
8060 if (toc_ref != NULL)
8061 free (toc_ref);
8062 if (locsyms != NULL
0ffa91dd 8063 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8064 != (unsigned char *) locsyms))
8065 free (locsyms);
663a1470 8066 return ret;
102890f0 8067 }
411e1bfb 8068
102890f0
AM
8069 if (h != NULL)
8070 {
766bc656
AM
8071 if (h->root.type == bfd_link_hash_defined
8072 || h->root.type == bfd_link_hash_defweak)
8073 value = h->root.u.def.value;
8074 else if (h->root.type == bfd_link_hash_undefweak)
8075 value = 0;
8076 else
663a1470
AM
8077 {
8078 found_tls_get_addr_arg = 0;
8079 continue;
8080 }
102890f0
AM
8081 }
8082 else
8083 /* Symbols referenced by TLS relocs must be of type
8084 STT_TLS. So no need for .opd local sym adjust. */
8085 value = sym->st_value;
8086
8087 ok_tprel = FALSE;
8088 is_local = FALSE;
8089 if (h == NULL
8090 || !h->def_dynamic)
8091 {
8092 is_local = TRUE;
766bc656
AM
8093 if (h != NULL
8094 && h->root.type == bfd_link_hash_undefweak)
8095 ok_tprel = TRUE;
8096 else
8097 {
8098 value += sym_sec->output_offset;
8099 value += sym_sec->output_section->vma;
8100 value -= htab->elf.tls_sec->vma;
8101 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8102 < (bfd_vma) 1 << 32);
8103 }
102890f0 8104 }
951fd09b 8105
102890f0 8106 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8107 /* If this section has old-style __tls_get_addr calls
8108 without marker relocs, then check that each
8109 __tls_get_addr call reloc is preceded by a reloc
8110 that conceivably belongs to the __tls_get_addr arg
8111 setup insn. If we don't find matching arg setup
8112 relocs, don't do any tls optimization. */
8113 if (pass == 0
8114 && sec->has_tls_get_addr_call
8115 && h != NULL
8116 && (h == &htab->tls_get_addr->elf
8117 || h == &htab->tls_get_addr_fd->elf)
8118 && !found_tls_get_addr_arg
8119 && is_branch_reloc (r_type))
8120 {
25f53a85 8121 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8122 "TLS optimization disabled\n"),
8123 ibfd, sec, rel->r_offset);
8124 ret = TRUE;
8125 goto err_free_rel;
8126 }
8127
8128 found_tls_get_addr_arg = 0;
102890f0
AM
8129 switch (r_type)
8130 {
8131 case R_PPC64_GOT_TLSLD16:
8132 case R_PPC64_GOT_TLSLD16_LO:
8133 expecting_tls_get_addr = 1;
663a1470 8134 found_tls_get_addr_arg = 1;
102890f0
AM
8135 /* Fall thru */
8136
8137 case R_PPC64_GOT_TLSLD16_HI:
8138 case R_PPC64_GOT_TLSLD16_HA:
8139 /* These relocs should never be against a symbol
8140 defined in a shared lib. Leave them alone if
8141 that turns out to be the case. */
8142 if (!is_local)
8143 continue;
411e1bfb 8144
102890f0 8145 /* LD -> LE */
411e1bfb 8146 tls_set = 0;
102890f0
AM
8147 tls_clear = TLS_LD;
8148 tls_type = TLS_TLS | TLS_LD;
8149 break;
411e1bfb 8150
102890f0
AM
8151 case R_PPC64_GOT_TLSGD16:
8152 case R_PPC64_GOT_TLSGD16_LO:
8153 expecting_tls_get_addr = 1;
663a1470 8154 found_tls_get_addr_arg = 1;
102890f0
AM
8155 /* Fall thru */
8156
8157 case R_PPC64_GOT_TLSGD16_HI:
8158 case R_PPC64_GOT_TLSGD16_HA:
8159 if (ok_tprel)
8160 /* GD -> LE */
411e1bfb 8161 tls_set = 0;
102890f0
AM
8162 else
8163 /* GD -> IE */
8164 tls_set = TLS_TLS | TLS_TPRELGD;
8165 tls_clear = TLS_GD;
8166 tls_type = TLS_TLS | TLS_GD;
8167 break;
8168
8169 case R_PPC64_GOT_TPREL16_DS:
8170 case R_PPC64_GOT_TPREL16_LO_DS:
8171 case R_PPC64_GOT_TPREL16_HI:
8172 case R_PPC64_GOT_TPREL16_HA:
8173 if (ok_tprel)
8174 {
8175 /* IE -> LE */
8176 tls_set = 0;
8177 tls_clear = TLS_TPREL;
8178 tls_type = TLS_TLS | TLS_TPREL;
8179 break;
8180 }
411e1bfb
AM
8181 continue;
8182
727fc41e
AM
8183 case R_PPC64_TLSGD:
8184 case R_PPC64_TLSLD:
663a1470
AM
8185 found_tls_get_addr_arg = 1;
8186 /* Fall thru */
8187
8188 case R_PPC64_TLS:
8189 case R_PPC64_TOC16:
8190 case R_PPC64_TOC16_LO:
102890f0
AM
8191 if (sym_sec == NULL || sym_sec != toc)
8192 continue;
8193
8194 /* Mark this toc entry as referenced by a TLS
8195 code sequence. We can do that now in the
8196 case of R_PPC64_TLS, and after checking for
8197 tls_get_addr for the TOC16 relocs. */
8198 if (toc_ref == NULL)
663a1470
AM
8199 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8200 if (toc_ref == NULL)
8201 goto err_free_rel;
8202
102890f0
AM
8203 if (h != NULL)
8204 value = h->root.u.def.value;
8205 else
8206 value = sym->st_value;
8207 value += rel->r_addend;
8208 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 8209 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8210 if (r_type == R_PPC64_TLS
8211 || r_type == R_PPC64_TLSGD
8212 || r_type == R_PPC64_TLSLD)
102890f0
AM
8213 {
8214 toc_ref[toc_ref_index] = 1;
8215 continue;
8216 }
8217
8218 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8219 continue;
8220
8221 tls_set = 0;
8222 tls_clear = 0;
8223 expecting_tls_get_addr = 2;
8224 break;
8225
8226 case R_PPC64_TPREL64:
8227 if (pass == 0
8228 || sec != toc
8229 || toc_ref == NULL
663a1470 8230 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8231 continue;
8232 if (ok_tprel)
8233 {
8234 /* IE -> LE */
8235 tls_set = TLS_EXPLICIT;
8236 tls_clear = TLS_TPREL;
8237 break;
8238 }
8239 continue;
8240
8241 case R_PPC64_DTPMOD64:
8242 if (pass == 0
8243 || sec != toc
8244 || toc_ref == NULL
663a1470 8245 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8246 continue;
8247 if (rel + 1 < relend
8248 && (rel[1].r_info
8249 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8250 && rel[1].r_offset == rel->r_offset + 8)
8251 {
8252 if (ok_tprel)
8253 /* GD -> LE */
8254 tls_set = TLS_EXPLICIT | TLS_GD;
8255 else
8256 /* GD -> IE */
8257 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8258 tls_clear = TLS_GD;
8259 }
8260 else
8261 {
8262 if (!is_local)
8263 continue;
8264
8265 /* LD -> LE */
8266 tls_set = TLS_EXPLICIT;
8267 tls_clear = TLS_LD;
8268 }
8269 break;
8270
8271 default:
8272 continue;
8273 }
8274
8275 if (pass == 0)
8276 {
727fc41e
AM
8277 if (!expecting_tls_get_addr
8278 || !sec->has_tls_get_addr_call)
102890f0
AM
8279 continue;
8280
3a71aa26
AM
8281 if (rel + 1 < relend
8282 && branch_reloc_hash_match (ibfd, rel + 1,
8283 htab->tls_get_addr,
8284 htab->tls_get_addr_fd))
102890f0 8285 {
3a71aa26 8286 if (expecting_tls_get_addr == 2)
102890f0 8287 {
3a71aa26 8288 /* Check for toc tls entries. */
f961d9dd 8289 unsigned char *toc_tls;
3a71aa26
AM
8290 int retval;
8291
8292 retval = get_tls_mask (&toc_tls, NULL, NULL,
8293 &locsyms,
8294 rel, ibfd);
8295 if (retval == 0)
8296 goto err_free_rel;
663a1470
AM
8297 if (toc_tls != NULL)
8298 {
8299 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8300 found_tls_get_addr_arg = 1;
8301 if (retval > 1)
8302 toc_ref[toc_ref_index] = 1;
8303 }
102890f0 8304 }
3a71aa26 8305 continue;
102890f0
AM
8306 }
8307
8308 if (expecting_tls_get_addr != 1)
8309 continue;
8310
8311 /* Uh oh, we didn't find the expected call. We
8312 could just mark this symbol to exclude it
8313 from tls optimization but it's safer to skip
663a1470 8314 the entire optimization. */
25f53a85 8315 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8316 "TLS optimization disabled\n"),
8317 ibfd, sec, rel->r_offset);
8318 ret = TRUE;
8319 goto err_free_rel;
102890f0
AM
8320 }
8321
85f7a9cb 8322 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8323 {
8324 struct plt_entry *ent;
8325 for (ent = htab->tls_get_addr->elf.plt.plist;
8326 ent != NULL;
8327 ent = ent->next)
8328 if (ent->addend == 0)
411e1bfb 8329 {
102890f0 8330 if (ent->plt.refcount > 0)
30038c59 8331 {
102890f0
AM
8332 ent->plt.refcount -= 1;
8333 expecting_tls_get_addr = 0;
30038c59 8334 }
102890f0 8335 break;
411e1bfb 8336 }
102890f0 8337 }
411e1bfb 8338
85f7a9cb 8339 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8340 {
8341 struct plt_entry *ent;
8342 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8343 ent != NULL;
8344 ent = ent->next)
8345 if (ent->addend == 0)
411e1bfb 8346 {
102890f0
AM
8347 if (ent->plt.refcount > 0)
8348 ent->plt.refcount -= 1;
8349 break;
411e1bfb 8350 }
102890f0 8351 }
411e1bfb 8352
102890f0 8353 if (tls_clear == 0)
30038c59
AM
8354 continue;
8355
102890f0
AM
8356 if ((tls_set & TLS_EXPLICIT) == 0)
8357 {
8358 struct got_entry *ent;
411e1bfb 8359
102890f0
AM
8360 /* Adjust got entry for this reloc. */
8361 if (h != NULL)
8362 ent = h->got.glist;
8363 else
8364 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8365
102890f0
AM
8366 for (; ent != NULL; ent = ent->next)
8367 if (ent->addend == rel->r_addend
8368 && ent->owner == ibfd
8369 && ent->tls_type == tls_type)
8370 break;
8371 if (ent == NULL)
8372 abort ();
411e1bfb 8373
102890f0
AM
8374 if (tls_set == 0)
8375 {
8376 /* We managed to get rid of a got entry. */
8377 if (ent->got.refcount > 0)
8378 ent->got.refcount -= 1;
8379 }
8380 }
8381 else
8382 {
8383 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8384 we'll lose one or two dyn relocs. */
8385 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8386 NULL, h, sym))
102890f0 8387 return FALSE;
411e1bfb 8388
102890f0
AM
8389 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8390 {
8391 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8392 NULL, h, sym))
102890f0
AM
8393 return FALSE;
8394 }
8395 }
411e1bfb 8396
102890f0
AM
8397 *tls_mask |= tls_set;
8398 *tls_mask &= ~tls_clear;
8399 }
8c1d1bb8 8400
102890f0
AM
8401 if (elf_section_data (sec)->relocs != relstart)
8402 free (relstart);
8403 }
411e1bfb 8404
663a1470
AM
8405 if (locsyms != NULL
8406 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8407 {
8408 if (!info->keep_memory)
8409 free (locsyms);
8410 else
8411 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8412 }
8413 }
411e1bfb 8414
663a1470
AM
8415 if (toc_ref != NULL)
8416 free (toc_ref);
b34976b6 8417 return TRUE;
1e2f5b6e 8418}
b34976b6 8419
c5614fa4
AM
8420/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8421 the values of any global symbols in a toc section that has been
8422 edited. Globals in toc sections should be a rarity, so this function
8423 sets a flag if any are found in toc sections other than the one just
8424 edited, so that futher hash table traversals can be avoided. */
8425
8426struct adjust_toc_info
8427{
8428 asection *toc;
8429 unsigned long *skip;
8430 bfd_boolean global_toc_syms;
8431};
8432
ba761f19
AM
8433enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8434
c5614fa4
AM
8435static bfd_boolean
8436adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8437{
8438 struct ppc_link_hash_entry *eh;
8439 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8440 unsigned long i;
c5614fa4 8441
c5614fa4
AM
8442 if (h->root.type != bfd_link_hash_defined
8443 && h->root.type != bfd_link_hash_defweak)
8444 return TRUE;
8445
8446 eh = (struct ppc_link_hash_entry *) h;
8447 if (eh->adjust_done)
8448 return TRUE;
8449
8450 if (eh->elf.root.u.def.section == toc_inf->toc)
8451 {
854b41e7
AM
8452 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8453 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8454 else
854b41e7
AM
8455 i = eh->elf.root.u.def.value >> 3;
8456
ba761f19 8457 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8458 {
8459 (*_bfd_error_handler)
854b41e7
AM
8460 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8461 do
8462 ++i;
ba761f19 8463 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8464 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8465 }
854b41e7
AM
8466
8467 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8468 eh->adjust_done = 1;
8469 }
8470 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8471 toc_inf->global_toc_syms = TRUE;
8472
8473 return TRUE;
8474}
8475
560c8763
AM
8476/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8477
8478static bfd_boolean
8479ok_lo_toc_insn (unsigned int insn)
8480{
8481 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8482 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8483 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8484 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8485 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8486 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8487 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8488 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8489 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8490 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8491 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8492 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8493 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8494 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8495 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8496 && (insn & 3) != 1)
8497 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8498 && ((insn & 3) == 0 || (insn & 3) == 3))
8499 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8500}
8501
c5614fa4
AM
8502/* Examine all relocs referencing .toc sections in order to remove
8503 unused .toc entries. */
8504
8505bfd_boolean
33c0ec9d 8506ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8507{
8508 bfd *ibfd;
8509 struct adjust_toc_info toc_inf;
67f0cbdb 8510 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8511
67f0cbdb 8512 htab->do_toc_opt = 1;
c5614fa4
AM
8513 toc_inf.global_toc_syms = TRUE;
8514 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8515 {
8516 asection *toc, *sec;
8517 Elf_Internal_Shdr *symtab_hdr;
8518 Elf_Internal_Sym *local_syms;
425b145b 8519 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8520 unsigned long *skip, *drop;
8521 unsigned char *used;
8522 unsigned char *keep, last, some_unused;
8523
854b41e7
AM
8524 if (!is_ppc64_elf (ibfd))
8525 continue;
8526
c5614fa4
AM
8527 toc = bfd_get_section_by_name (ibfd, ".toc");
8528 if (toc == NULL
92b7a70f 8529 || toc->size == 0
dbaa2011
AM
8530 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8531 || discarded_section (toc))
c5614fa4
AM
8532 continue;
8533
425b145b 8534 toc_relocs = NULL;
c5614fa4 8535 local_syms = NULL;
0ffa91dd 8536 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8537
8538 /* Look at sections dropped from the final link. */
8539 skip = NULL;
8540 relstart = NULL;
8541 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8542 {
8543 if (sec->reloc_count == 0
dbaa2011 8544 || !discarded_section (sec)
c5614fa4
AM
8545 || get_opd_info (sec)
8546 || (sec->flags & SEC_ALLOC) == 0
8547 || (sec->flags & SEC_DEBUGGING) != 0)
8548 continue;
8549
8550 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8551 if (relstart == NULL)
8552 goto error_ret;
8553
8554 /* Run through the relocs to see which toc entries might be
8555 unused. */
8556 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8557 {
8558 enum elf_ppc64_reloc_type r_type;
8559 unsigned long r_symndx;
8560 asection *sym_sec;
8561 struct elf_link_hash_entry *h;
8562 Elf_Internal_Sym *sym;
8563 bfd_vma val;
8564
8565 r_type = ELF64_R_TYPE (rel->r_info);
8566 switch (r_type)
8567 {
8568 default:
8569 continue;
8570
8571 case R_PPC64_TOC16:
8572 case R_PPC64_TOC16_LO:
8573 case R_PPC64_TOC16_HI:
8574 case R_PPC64_TOC16_HA:
8575 case R_PPC64_TOC16_DS:
8576 case R_PPC64_TOC16_LO_DS:
8577 break;
8578 }
8579
8580 r_symndx = ELF64_R_SYM (rel->r_info);
8581 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8582 r_symndx, ibfd))
8583 goto error_ret;
8584
8585 if (sym_sec != toc)
8586 continue;
8587
8588 if (h != NULL)
8589 val = h->root.u.def.value;
8590 else
8591 val = sym->st_value;
8592 val += rel->r_addend;
8593
8594 if (val >= toc->size)
8595 continue;
8596
8597 /* Anything in the toc ought to be aligned to 8 bytes.
8598 If not, don't mark as unused. */
8599 if (val & 7)
8600 continue;
8601
8602 if (skip == NULL)
8603 {
854b41e7 8604 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8605 if (skip == NULL)
8606 goto error_ret;
8607 }
8608
ba761f19 8609 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8610 }
8611
8612 if (elf_section_data (sec)->relocs != relstart)
8613 free (relstart);
8614 }
8615
ba761f19
AM
8616 /* For largetoc loads of address constants, we can convert
8617 . addis rx,2,addr@got@ha
8618 . ld ry,addr@got@l(rx)
8619 to
8620 . addis rx,2,addr@toc@ha
8621 . addi ry,rx,addr@toc@l
8622 when addr is within 2G of the toc pointer. This then means
8623 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8624
ba761f19
AM
8625 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8626 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8627 && toc->reloc_count != 0)
8628 {
8629 /* Read toc relocs. */
425b145b
AM
8630 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8631 info->keep_memory);
8632 if (toc_relocs == NULL)
ba761f19
AM
8633 goto error_ret;
8634
425b145b 8635 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8636 {
8637 enum elf_ppc64_reloc_type r_type;
8638 unsigned long r_symndx;
8639 asection *sym_sec;
8640 struct elf_link_hash_entry *h;
8641 Elf_Internal_Sym *sym;
8642 bfd_vma val, addr;
8643
8644 r_type = ELF64_R_TYPE (rel->r_info);
8645 if (r_type != R_PPC64_ADDR64)
8646 continue;
8647
8648 r_symndx = ELF64_R_SYM (rel->r_info);
8649 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8650 r_symndx, ibfd))
8651 goto error_ret;
8652
425b145b 8653 if (sym_sec == NULL
dbaa2011 8654 || discarded_section (sym_sec))
425b145b
AM
8655 continue;
8656
726d3ab0 8657 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8658 continue;
8659
8660 if (h != NULL)
bddc25c9
AM
8661 {
8662 if (h->type == STT_GNU_IFUNC)
8663 continue;
8664 val = h->root.u.def.value;
8665 }
ba761f19 8666 else
bddc25c9
AM
8667 {
8668 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8669 continue;
8670 val = sym->st_value;
8671 }
ba761f19
AM
8672 val += rel->r_addend;
8673 val += sym_sec->output_section->vma + sym_sec->output_offset;
8674
8675 /* We don't yet know the exact toc pointer value, but we
8676 know it will be somewhere in the toc section. Don't
8677 optimize if the difference from any possible toc
8678 pointer is outside [ff..f80008000, 7fff7fff]. */
8679 addr = toc->output_section->vma + TOC_BASE_OFF;
8680 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8681 continue;
8682
8683 addr = toc->output_section->vma + toc->output_section->rawsize;
8684 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8685 continue;
8686
8687 if (skip == NULL)
8688 {
8689 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8690 if (skip == NULL)
8691 goto error_ret;
8692 }
8693
8694 skip[rel->r_offset >> 3]
425b145b 8695 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8696 }
ba761f19
AM
8697 }
8698
c5614fa4
AM
8699 if (skip == NULL)
8700 continue;
8701
8702 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8703 if (used == NULL)
8704 {
8705 error_ret:
8706 if (local_syms != NULL
8707 && symtab_hdr->contents != (unsigned char *) local_syms)
8708 free (local_syms);
8709 if (sec != NULL
8710 && relstart != NULL
8711 && elf_section_data (sec)->relocs != relstart)
8712 free (relstart);
425b145b
AM
8713 if (toc_relocs != NULL
8714 && elf_section_data (toc)->relocs != toc_relocs)
8715 free (toc_relocs);
c5614fa4
AM
8716 if (skip != NULL)
8717 free (skip);
8718 return FALSE;
8719 }
8720
30038c59
AM
8721 /* Now check all kept sections that might reference the toc.
8722 Check the toc itself last. */
8723 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8724 : ibfd->sections);
c5614fa4 8725 sec != NULL;
c5614fa4 8726 sec = (sec == toc ? NULL
c5614fa4 8727 : sec->next == NULL ? toc
30038c59 8728 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8729 : sec->next))
8730 {
8731 int repeat;
8732
8733 if (sec->reloc_count == 0
dbaa2011 8734 || discarded_section (sec)
c5614fa4
AM
8735 || get_opd_info (sec)
8736 || (sec->flags & SEC_ALLOC) == 0
8737 || (sec->flags & SEC_DEBUGGING) != 0)
8738 continue;
8739
854b41e7
AM
8740 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8741 info->keep_memory);
c5614fa4
AM
8742 if (relstart == NULL)
8743 goto error_ret;
8744
8745 /* Mark toc entries referenced as used. */
c5614fa4 8746 do
d4f1ee75
AM
8747 {
8748 repeat = 0;
8749 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8750 {
8751 enum elf_ppc64_reloc_type r_type;
8752 unsigned long r_symndx;
8753 asection *sym_sec;
8754 struct elf_link_hash_entry *h;
8755 Elf_Internal_Sym *sym;
8756 bfd_vma val;
8757 enum {no_check, check_lo, check_ha} insn_check;
98528052 8758
d4f1ee75
AM
8759 r_type = ELF64_R_TYPE (rel->r_info);
8760 switch (r_type)
8761 {
8762 default:
8763 insn_check = no_check;
8764 break;
98528052 8765
d4f1ee75
AM
8766 case R_PPC64_GOT_TLSLD16_HA:
8767 case R_PPC64_GOT_TLSGD16_HA:
8768 case R_PPC64_GOT_TPREL16_HA:
8769 case R_PPC64_GOT_DTPREL16_HA:
8770 case R_PPC64_GOT16_HA:
8771 case R_PPC64_TOC16_HA:
8772 insn_check = check_ha;
8773 break;
98528052 8774
d4f1ee75
AM
8775 case R_PPC64_GOT_TLSLD16_LO:
8776 case R_PPC64_GOT_TLSGD16_LO:
8777 case R_PPC64_GOT_TPREL16_LO_DS:
8778 case R_PPC64_GOT_DTPREL16_LO_DS:
8779 case R_PPC64_GOT16_LO:
8780 case R_PPC64_GOT16_LO_DS:
8781 case R_PPC64_TOC16_LO:
8782 case R_PPC64_TOC16_LO_DS:
8783 insn_check = check_lo;
8784 break;
8785 }
560c8763 8786
d4f1ee75
AM
8787 if (insn_check != no_check)
8788 {
8789 bfd_vma off = rel->r_offset & ~3;
8790 unsigned char buf[4];
8791 unsigned int insn;
c5614fa4 8792
d4f1ee75
AM
8793 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8794 {
8795 free (used);
8796 goto error_ret;
8797 }
8798 insn = bfd_get_32 (ibfd, buf);
8799 if (insn_check == check_lo
8800 ? !ok_lo_toc_insn (insn)
8801 : ((insn & ((0x3f << 26) | 0x1f << 16))
8802 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8803 {
8804 char str[12];
8805
8806 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8807 sprintf (str, "%#08x", insn);
8808 info->callbacks->einfo
8809 (_("%P: %H: toc optimization is not supported for"
8810 " %s instruction.\n"),
8811 ibfd, sec, rel->r_offset & ~3, str);
8812 }
8813 }
c5614fa4 8814
d4f1ee75
AM
8815 switch (r_type)
8816 {
8817 case R_PPC64_TOC16:
8818 case R_PPC64_TOC16_LO:
8819 case R_PPC64_TOC16_HI:
8820 case R_PPC64_TOC16_HA:
8821 case R_PPC64_TOC16_DS:
8822 case R_PPC64_TOC16_LO_DS:
8823 /* In case we're taking addresses of toc entries. */
8824 case R_PPC64_ADDR64:
8825 break;
c5614fa4 8826
d4f1ee75
AM
8827 default:
8828 continue;
8829 }
c5614fa4 8830
d4f1ee75
AM
8831 r_symndx = ELF64_R_SYM (rel->r_info);
8832 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8833 r_symndx, ibfd))
8834 {
8835 free (used);
8836 goto error_ret;
8837 }
c5614fa4 8838
d4f1ee75
AM
8839 if (sym_sec != toc)
8840 continue;
c5614fa4 8841
d4f1ee75
AM
8842 if (h != NULL)
8843 val = h->root.u.def.value;
8844 else
8845 val = sym->st_value;
8846 val += rel->r_addend;
ba761f19 8847
d4f1ee75
AM
8848 if (val >= toc->size)
8849 continue;
ba761f19 8850
d4f1ee75
AM
8851 if ((skip[val >> 3] & can_optimize) != 0)
8852 {
8853 bfd_vma off;
8854 unsigned char opc;
8855
8856 switch (r_type)
8857 {
8858 case R_PPC64_TOC16_HA:
ba761f19 8859 break;
ba761f19 8860
d4f1ee75
AM
8861 case R_PPC64_TOC16_LO_DS:
8862 off = rel->r_offset;
8863 off += (bfd_big_endian (ibfd) ? -2 : 3);
8864 if (!bfd_get_section_contents (ibfd, sec, &opc,
8865 off, 1))
8866 {
8867 free (used);
8868 goto error_ret;
8869 }
8870 if ((opc & (0x3f << 2)) == (58u << 2))
8871 break;
8872 /* Fall thru */
ba761f19 8873
d4f1ee75
AM
8874 default:
8875 /* Wrong sort of reloc, or not a ld. We may
8876 as well clear ref_from_discarded too. */
8877 skip[val >> 3] = 0;
8878 }
8879 }
8880
8881 if (sec != toc)
8882 used[val >> 3] = 1;
8883 /* For the toc section, we only mark as used if this
8884 entry itself isn't unused. */
8885 else if ((used[rel->r_offset >> 3]
8886 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8887 && !used[val >> 3])
8888 {
8889 /* Do all the relocs again, to catch reference
8890 chains. */
8891 repeat = 1;
8892 used[val >> 3] = 1;
8893 }
8894 }
8895 }
c5614fa4 8896 while (repeat);
854b41e7
AM
8897
8898 if (elf_section_data (sec)->relocs != relstart)
8899 free (relstart);
c5614fa4
AM
8900 }
8901
8902 /* Merge the used and skip arrays. Assume that TOC
8903 doublewords not appearing as either used or unused belong
8904 to to an entry more than one doubleword in size. */
8905 for (drop = skip, keep = used, last = 0, some_unused = 0;
8906 drop < skip + (toc->size + 7) / 8;
8907 ++drop, ++keep)
8908 {
8909 if (*keep)
8910 {
ba761f19
AM
8911 *drop &= ~ref_from_discarded;
8912 if ((*drop & can_optimize) != 0)
8913 some_unused = 1;
c5614fa4
AM
8914 last = 0;
8915 }
b140b010 8916 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8917 {
8918 some_unused = 1;
ba761f19 8919 last = ref_from_discarded;
c5614fa4
AM
8920 }
8921 else
8922 *drop = last;
8923 }
8924
8925 free (used);
8926
8927 if (some_unused)
8928 {
8929 bfd_byte *contents, *src;
8930 unsigned long off;
d62b3684 8931 Elf_Internal_Sym *sym;
ba761f19 8932 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8933
8934 /* Shuffle the toc contents, and at the same time convert the
8935 skip array from booleans into offsets. */
8936 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8937 goto error_ret;
8938
8939 elf_section_data (toc)->this_hdr.contents = contents;
8940
8941 for (src = contents, off = 0, drop = skip;
8942 src < contents + toc->size;
8943 src += 8, ++drop)
8944 {
ba761f19
AM
8945 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8946 off += 8;
c5614fa4
AM
8947 else if (off != 0)
8948 {
8949 *drop = off;
8950 memcpy (src - off, src, 8);
8951 }
8952 }
854b41e7 8953 *drop = off;
c5614fa4
AM
8954 toc->rawsize = toc->size;
8955 toc->size = src - contents - off;
8956
ba761f19
AM
8957 /* Adjust addends for relocs against the toc section sym,
8958 and optimize any accesses we can. */
c5614fa4
AM
8959 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8960 {
8961 if (sec->reloc_count == 0
dbaa2011 8962 || discarded_section (sec))
c5614fa4
AM
8963 continue;
8964
8965 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8966 info->keep_memory);
c5614fa4
AM
8967 if (relstart == NULL)
8968 goto error_ret;
8969
8970 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8971 {
8972 enum elf_ppc64_reloc_type r_type;
8973 unsigned long r_symndx;
8974 asection *sym_sec;
8975 struct elf_link_hash_entry *h;
854b41e7 8976 bfd_vma val;
c5614fa4
AM
8977
8978 r_type = ELF64_R_TYPE (rel->r_info);
8979 switch (r_type)
8980 {
8981 default:
8982 continue;
8983
8984 case R_PPC64_TOC16:
8985 case R_PPC64_TOC16_LO:
8986 case R_PPC64_TOC16_HI:
8987 case R_PPC64_TOC16_HA:
8988 case R_PPC64_TOC16_DS:
8989 case R_PPC64_TOC16_LO_DS:
8990 case R_PPC64_ADDR64:
8991 break;
8992 }
8993
8994 r_symndx = ELF64_R_SYM (rel->r_info);
8995 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8996 r_symndx, ibfd))
8997 goto error_ret;
8998
ba761f19 8999 if (sym_sec != toc)
c5614fa4
AM
9000 continue;
9001
ba761f19
AM
9002 if (h != NULL)
9003 val = h->root.u.def.value;
9004 else
9005 {
9006 val = sym->st_value;
9007 if (val != 0)
9008 local_toc_syms = TRUE;
9009 }
9010
9011 val += rel->r_addend;
854b41e7
AM
9012
9013 if (val > toc->rawsize)
9014 val = toc->rawsize;
ba761f19
AM
9015 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9016 continue;
9017 else if ((skip[val >> 3] & can_optimize) != 0)
9018 {
9019 Elf_Internal_Rela *tocrel
425b145b 9020 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9021 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9022
9023 switch (r_type)
9024 {
9025 case R_PPC64_TOC16_HA:
9026 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9027 break;
9028
9029 case R_PPC64_TOC16_LO_DS:
9030 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9031 break;
9032
9033 default:
28942f62
AM
9034 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9035 ppc_howto_init ();
b140b010 9036 info->callbacks->einfo
bc30df16 9037 (_("%P: %H: %s references "
b140b010
AM
9038 "optimized away TOC entry\n"),
9039 ibfd, sec, rel->r_offset,
9040 ppc64_elf_howto_table[r_type]->name);
9041 bfd_set_error (bfd_error_bad_value);
9042 goto error_ret;
ba761f19
AM
9043 }
9044 rel->r_addend = tocrel->r_addend;
9045 elf_section_data (sec)->relocs = relstart;
9046 continue;
9047 }
9048
9049 if (h != NULL || sym->st_value != 0)
9050 continue;
854b41e7
AM
9051
9052 rel->r_addend -= skip[val >> 3];
9053 elf_section_data (sec)->relocs = relstart;
c5614fa4 9054 }
854b41e7
AM
9055
9056 if (elf_section_data (sec)->relocs != relstart)
9057 free (relstart);
c5614fa4
AM
9058 }
9059
9060 /* We shouldn't have local or global symbols defined in the TOC,
9061 but handle them anyway. */
df22d223
AM
9062 if (local_syms != NULL)
9063 for (sym = local_syms;
9064 sym < local_syms + symtab_hdr->sh_info;
9065 ++sym)
9066 if (sym->st_value != 0
9067 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9068 {
9069 unsigned long i;
854b41e7 9070
df22d223
AM
9071 if (sym->st_value > toc->rawsize)
9072 i = toc->rawsize >> 3;
9073 else
9074 i = sym->st_value >> 3;
854b41e7 9075
df22d223
AM
9076 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9077 {
9078 if (local_toc_syms)
9079 (*_bfd_error_handler)
9080 (_("%s defined on removed toc entry"),
9081 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9082 do
9083 ++i;
9084 while ((skip[i] & (ref_from_discarded | can_optimize)));
9085 sym->st_value = (bfd_vma) i << 3;
9086 }
d62b3684 9087
df22d223
AM
9088 sym->st_value -= skip[i];
9089 symtab_hdr->contents = (unsigned char *) local_syms;
9090 }
c5614fa4 9091
854b41e7 9092 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9093 if (toc_inf.global_toc_syms)
9094 {
9095 toc_inf.toc = toc;
9096 toc_inf.skip = skip;
9097 toc_inf.global_toc_syms = FALSE;
9098 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9099 &toc_inf);
9100 }
854b41e7
AM
9101
9102 if (toc->reloc_count != 0)
9103 {
d4730f92 9104 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9105 Elf_Internal_Rela *wrel;
9106 bfd_size_type sz;
9107
854b41e7 9108 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9109 if (toc_relocs == NULL)
9110 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9111 info->keep_memory);
9112 if (toc_relocs == NULL)
9113 goto error_ret;
9114
425b145b
AM
9115 wrel = toc_relocs;
9116 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9117 if ((skip[rel->r_offset >> 3]
9118 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9119 {
9120 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9121 wrel->r_info = rel->r_info;
9122 wrel->r_addend = rel->r_addend;
9123 ++wrel;
9124 }
9125 else if (!dec_dynrel_count (rel->r_info, toc, info,
9126 &local_syms, NULL, NULL))
9127 goto error_ret;
9128
425b145b
AM
9129 elf_section_data (toc)->relocs = toc_relocs;
9130 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9131 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9132 sz = rel_hdr->sh_entsize;
9133 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9134 }
c5614fa4 9135 }
28be611c
AM
9136 else if (toc_relocs != NULL
9137 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9138 free (toc_relocs);
c5614fa4
AM
9139
9140 if (local_syms != NULL
9141 && symtab_hdr->contents != (unsigned char *) local_syms)
9142 {
9143 if (!info->keep_memory)
9144 free (local_syms);
9145 else
9146 symtab_hdr->contents = (unsigned char *) local_syms;
9147 }
9148 free (skip);
9149 }
9150
9151 return TRUE;
9152}
9153
1bbe0902
AM
9154/* Return true iff input section I references the TOC using
9155 instructions limited to +/-32k offsets. */
9156
9157bfd_boolean
9158ppc64_elf_has_small_toc_reloc (asection *i)
9159{
9160 return (is_ppc64_elf (i->owner)
9161 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9162}
9163
927be08e
AM
9164/* Allocate space for one GOT entry. */
9165
9166static void
9167allocate_got (struct elf_link_hash_entry *h,
9168 struct bfd_link_info *info,
9169 struct got_entry *gent)
9170{
9171 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9172 bfd_boolean dyn;
9173 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9174 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9175 ? 16 : 8);
9176 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9177 ? 2 : 1) * sizeof (Elf64_External_Rela);
9178 asection *got = ppc64_elf_tdata (gent->owner)->got;
9179
9180 gent->got.offset = got->size;
9181 got->size += entsize;
9182
9183 dyn = htab->elf.dynamic_sections_created;
19e08130 9184 if (h->type == STT_GNU_IFUNC)
927be08e 9185 {
19e08130
AM
9186 htab->reliplt->size += rentsize;
9187 htab->got_reli_size += rentsize;
927be08e 9188 }
19e08130
AM
9189 else if ((info->shared
9190 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9191 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9192 || h->root.type != bfd_link_hash_undefweak))
927be08e 9193 {
19e08130 9194 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9195 relgot->size += rentsize;
927be08e
AM
9196 }
9197}
9198
7865406b
AM
9199/* This function merges got entries in the same toc group. */
9200
9201static void
9202merge_got_entries (struct got_entry **pent)
9203{
9204 struct got_entry *ent, *ent2;
9205
9206 for (ent = *pent; ent != NULL; ent = ent->next)
9207 if (!ent->is_indirect)
9208 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9209 if (!ent2->is_indirect
9210 && ent2->addend == ent->addend
9211 && ent2->tls_type == ent->tls_type
9212 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9213 {
9214 ent2->is_indirect = TRUE;
9215 ent2->got.ent = ent;
9216 }
9217}
9218
65f38f15
AM
9219/* Allocate space in .plt, .got and associated reloc sections for
9220 dynamic relocs. */
5bd4f169 9221
b34976b6 9222static bfd_boolean
4ce794b7 9223allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9224{
65f38f15
AM
9225 struct bfd_link_info *info;
9226 struct ppc_link_hash_table *htab;
5bd4f169 9227 asection *s;
65f38f15 9228 struct ppc_link_hash_entry *eh;
6061a67d 9229 struct elf_dyn_relocs *p;
0b8bcf0d 9230 struct got_entry **pgent, *gent;
5bd4f169 9231
e92d460e 9232 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9233 return TRUE;
5bd4f169 9234
65f38f15
AM
9235 info = (struct bfd_link_info *) inf;
9236 htab = ppc_hash_table (info);
4dfe6ac6
NC
9237 if (htab == NULL)
9238 return FALSE;
5bd4f169 9239
e054468f
AM
9240 if ((htab->elf.dynamic_sections_created
9241 && h->dynindx != -1
9242 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9243 || h->type == STT_GNU_IFUNC)
5bd4f169 9244 {
411e1bfb
AM
9245 struct plt_entry *pent;
9246 bfd_boolean doneone = FALSE;
9247 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9248 if (pent->plt.refcount > 0)
9249 {
25f23106
AM
9250 if (!htab->elf.dynamic_sections_created
9251 || h->dynindx == -1)
e054468f
AM
9252 {
9253 s = htab->iplt;
9254 pent->plt.offset = s->size;
b9e5796b 9255 s->size += PLT_ENTRY_SIZE (htab);
e054468f
AM
9256 s = htab->reliplt;
9257 }
9258 else
9259 {
9260 /* If this is the first .plt entry, make room for the special
9261 first entry. */
9262 s = htab->plt;
9263 if (s->size == 0)
b9e5796b 9264 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
e054468f
AM
9265
9266 pent->plt.offset = s->size;
9267
9268 /* Make room for this entry. */
b9e5796b 9269 s->size += PLT_ENTRY_SIZE (htab);
e054468f
AM
9270
9271 /* Make room for the .glink code. */
9272 s = htab->glink;
9273 if (s->size == 0)
9274 s->size += GLINK_CALL_STUB_SIZE;
b9e5796b
AM
9275 if (htab->opd_abi)
9276 {
9277 /* We need bigger stubs past index 32767. */
9278 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9279 s->size += 4;
9280 s->size += 2*4;
9281 }
9282 else
e054468f 9283 s->size += 4;
e054468f
AM
9284
9285 /* We also need to make an entry in the .rela.plt section. */
9286 s = htab->relplt;
9287 }
eea6121a 9288 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
9289 doneone = TRUE;
9290 }
9291 else
9292 pent->plt.offset = (bfd_vma) -1;
9293 if (!doneone)
65f38f15 9294 {
411e1bfb 9295 h->plt.plist = NULL;
f5385ebf 9296 h->needs_plt = 0;
65f38f15
AM
9297 }
9298 }
9299 else
9300 {
411e1bfb 9301 h->plt.plist = NULL;
f5385ebf 9302 h->needs_plt = 0;
65f38f15
AM
9303 }
9304
951fd09b
AM
9305 eh = (struct ppc_link_hash_entry *) h;
9306 /* Run through the TLS GD got entries first if we're changing them
9307 to TPREL. */
e7b938ca 9308 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9309 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9310 if (gent->got.refcount > 0
9311 && (gent->tls_type & TLS_GD) != 0)
9312 {
9313 /* This was a GD entry that has been converted to TPREL. If
9314 there happens to be a TPREL entry we can use that one. */
9315 struct got_entry *ent;
9316 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9317 if (ent->got.refcount > 0
9318 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9319 && ent->addend == gent->addend
9320 && ent->owner == gent->owner)
951fd09b
AM
9321 {
9322 gent->got.refcount = 0;
9323 break;
9324 }
9325
9326 /* If not, then we'll be using our own TPREL entry. */
9327 if (gent->got.refcount != 0)
9328 gent->tls_type = TLS_TLS | TLS_TPREL;
9329 }
9330
7865406b
AM
9331 /* Remove any list entry that won't generate a word in the GOT before
9332 we call merge_got_entries. Otherwise we risk merging to empty
9333 entries. */
0b8bcf0d
AM
9334 pgent = &h->got.glist;
9335 while ((gent = *pgent) != NULL)
411e1bfb 9336 if (gent->got.refcount > 0)
7865406b
AM
9337 {
9338 if ((gent->tls_type & TLS_LD) != 0
9339 && !h->def_dynamic)
9340 {
9341 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9342 *pgent = gent->next;
9343 }
9344 else
9345 pgent = &gent->next;
9346 }
9347 else
9348 *pgent = gent->next;
9349
9350 if (!htab->do_multi_toc)
9351 merge_got_entries (&h->got.glist);
9352
9353 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9354 if (!gent->is_indirect)
411e1bfb
AM
9355 {
9356 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9357 Undefined weak syms won't yet be marked as dynamic,
9358 nor will all TLS symbols. */
411e1bfb 9359 if (h->dynindx == -1
b099ab9f 9360 && !h->forced_local
25f23106 9361 && h->type != STT_GNU_IFUNC
b099ab9f 9362 && htab->elf.dynamic_sections_created)
411e1bfb 9363 {
c152c796 9364 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9365 return FALSE;
9366 }
65f38f15 9367
0c8d6e5c 9368 if (!is_ppc64_elf (gent->owner))
927be08e 9369 abort ();
0ffa91dd 9370
927be08e 9371 allocate_got (h, info, gent);
411e1bfb 9372 }
65f38f15 9373
b099ab9f 9374 if (eh->dyn_relocs == NULL
25f23106
AM
9375 || (!htab->elf.dynamic_sections_created
9376 && h->type != STT_GNU_IFUNC))
b34976b6 9377 return TRUE;
65f38f15
AM
9378
9379 /* In the shared -Bsymbolic case, discard space allocated for
9380 dynamic pc-relative relocs against symbols which turn out to be
9381 defined in regular objects. For the normal shared case, discard
9382 space for relocs that have become local due to symbol visibility
9383 changes. */
9384
9385 if (info->shared)
9386 {
9c7a29a3 9387 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 9388 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
9389 generated via assembly. We want calls to protected symbols to
9390 resolve directly to the function rather than going via the plt.
9391 If people want function pointer comparisons to work as expected
9392 then they should avoid writing weird assembly. */
09695f56 9393 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 9394 {
6061a67d 9395 struct elf_dyn_relocs **pp;
65f38f15
AM
9396
9397 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 9398 {
65f38f15
AM
9399 p->count -= p->pc_count;
9400 p->pc_count = 0;
9401 if (p->count == 0)
9402 *pp = p->next;
9403 else
9404 pp = &p->next;
5bd4f169 9405 }
65f38f15 9406 }
4e795f50
AM
9407
9408 /* Also discard relocs on undefined weak syms with non-default
9409 visibility. */
cab87ef9
AM
9410 if (eh->dyn_relocs != NULL
9411 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
9412 {
9413 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9414 eh->dyn_relocs = NULL;
9415
9416 /* Make sure this symbol is output as a dynamic symbol.
9417 Undefined weak syms won't yet be marked as dynamic. */
9418 else if (h->dynindx == -1
9419 && !h->forced_local)
9420 {
9421 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9422 return FALSE;
9423 }
9424 }
65f38f15 9425 }
25f23106
AM
9426 else if (h->type == STT_GNU_IFUNC)
9427 {
9428 if (!h->non_got_ref)
9429 eh->dyn_relocs = NULL;
9430 }
f4656909 9431 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
9432 {
9433 /* For the non-shared case, discard space for relocs against
9434 symbols which turn out to need copy relocs or are not
9435 dynamic. */
9436
f5385ebf 9437 if (!h->non_got_ref
f5385ebf 9438 && !h->def_regular)
65f38f15
AM
9439 {
9440 /* Make sure this symbol is output as a dynamic symbol.
9441 Undefined weak syms won't yet be marked as dynamic. */
9442 if (h->dynindx == -1
f5385ebf 9443 && !h->forced_local)
65f38f15 9444 {
c152c796 9445 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 9446 return FALSE;
65f38f15
AM
9447 }
9448
9449 /* If that succeeded, we know we'll be keeping all the
9450 relocs. */
9451 if (h->dynindx != -1)
9452 goto keep;
9453 }
9454
9455 eh->dyn_relocs = NULL;
9456
ec338859 9457 keep: ;
65f38f15
AM
9458 }
9459
9460 /* Finally, allocate space. */
9461 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9462 {
9463 asection *sreloc = elf_section_data (p->sec)->sreloc;
19e08130 9464 if (eh->elf.type == STT_GNU_IFUNC)
25f23106 9465 sreloc = htab->reliplt;
eea6121a 9466 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9467 }
9468
b34976b6 9469 return TRUE;
65f38f15
AM
9470}
9471
9472/* Find any dynamic relocs that apply to read-only sections. */
9473
b34976b6 9474static bfd_boolean
4ce794b7 9475readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
9476{
9477 struct ppc_link_hash_entry *eh;
6061a67d 9478 struct elf_dyn_relocs *p;
65f38f15
AM
9479
9480 eh = (struct ppc_link_hash_entry *) h;
9481 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9482 {
9483 asection *s = p->sec->output_section;
9484
9485 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9486 {
4ce794b7 9487 struct bfd_link_info *info = inf;
65f38f15
AM
9488
9489 info->flags |= DF_TEXTREL;
9490
9491 /* Not an error, just cut short the traversal. */
b34976b6 9492 return FALSE;
65f38f15
AM
9493 }
9494 }
b34976b6 9495 return TRUE;
65f38f15
AM
9496}
9497
9498/* Set the sizes of the dynamic sections. */
9499
b34976b6 9500static bfd_boolean
ee67d69a 9501ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9502 struct bfd_link_info *info)
65f38f15
AM
9503{
9504 struct ppc_link_hash_table *htab;
9505 bfd *dynobj;
9506 asection *s;
b34976b6 9507 bfd_boolean relocs;
65f38f15 9508 bfd *ibfd;
7865406b 9509 struct got_entry *first_tlsld;
65f38f15
AM
9510
9511 htab = ppc_hash_table (info);
4dfe6ac6
NC
9512 if (htab == NULL)
9513 return FALSE;
9514
65f38f15
AM
9515 dynobj = htab->elf.dynobj;
9516 if (dynobj == NULL)
9517 abort ();
9518
9519 if (htab->elf.dynamic_sections_created)
9520 {
9521 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 9522 if (info->executable)
65f38f15 9523 {
3d4d4302 9524 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9525 if (s == NULL)
9526 abort ();
eea6121a 9527 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9528 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9529 }
9530 }
9531
9532 /* Set up .got offsets for local syms, and space for local dynamic
9533 relocs. */
9534 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9535 {
411e1bfb
AM
9536 struct got_entry **lgot_ents;
9537 struct got_entry **end_lgot_ents;
e054468f
AM
9538 struct plt_entry **local_plt;
9539 struct plt_entry **end_local_plt;
f961d9dd 9540 unsigned char *lgot_masks;
65f38f15
AM
9541 bfd_size_type locsymcount;
9542 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9543
0c8d6e5c 9544 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9545 continue;
9546
9547 for (s = ibfd->sections; s != NULL; s = s->next)
9548 {
19e08130 9549 struct ppc_dyn_relocs *p;
65f38f15 9550
6edfbbad 9551 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9552 {
ec338859
AM
9553 if (!bfd_is_abs_section (p->sec)
9554 && bfd_is_abs_section (p->sec->output_section))
9555 {
9556 /* Input section has been discarded, either because
9557 it is a copy of a linkonce section or due to
9558 linker script /DISCARD/, so we'll be discarding
9559 the relocs too. */
9560 }
248866a8 9561 else if (p->count != 0)
ec338859 9562 {
19e08130
AM
9563 asection *srel = elf_section_data (p->sec)->sreloc;
9564 if (p->ifunc)
25f23106 9565 srel = htab->reliplt;
eea6121a 9566 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9567 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9568 info->flags |= DF_TEXTREL;
ec338859 9569 }
65f38f15
AM
9570 }
9571 }
9572
411e1bfb
AM
9573 lgot_ents = elf_local_got_ents (ibfd);
9574 if (!lgot_ents)
65f38f15
AM
9575 continue;
9576
0ffa91dd 9577 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9578 locsymcount = symtab_hdr->sh_info;
411e1bfb 9579 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9580 local_plt = (struct plt_entry **) end_lgot_ents;
9581 end_local_plt = local_plt + locsymcount;
f961d9dd 9582 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9583 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9584 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9585 {
0b8bcf0d 9586 struct got_entry **pent, *ent;
411e1bfb 9587
0b8bcf0d
AM
9588 pent = lgot_ents;
9589 while ((ent = *pent) != NULL)
411e1bfb
AM
9590 if (ent->got.refcount > 0)
9591 {
e7b938ca 9592 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9593 {
927be08e 9594 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9595 *pent = ent->next;
411e1bfb
AM
9596 }
9597 else
9598 {
19e08130
AM
9599 unsigned int ent_size = 8;
9600 unsigned int rel_size = sizeof (Elf64_External_Rela);
9601
eea6121a 9602 ent->got.offset = s->size;
e7b938ca 9603 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9604 {
19e08130
AM
9605 ent_size *= 2;
9606 rel_size *= 2;
9607 }
9608 s->size += ent_size;
9609 if ((*lgot_masks & PLT_IFUNC) != 0)
9610 {
9611 htab->reliplt->size += rel_size;
9612 htab->got_reli_size += rel_size;
9613 }
9614 else if (info->shared)
9615 {
9616 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9617 srel->size += rel_size;
927be08e 9618 }
0b8bcf0d 9619 pent = &ent->next;
411e1bfb
AM
9620 }
9621 }
9622 else
0b8bcf0d 9623 *pent = ent->next;
65f38f15 9624 }
e054468f
AM
9625
9626 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9627 for (; local_plt < end_local_plt; ++local_plt)
9628 {
9629 struct plt_entry *ent;
9630
9631 for (ent = *local_plt; ent != NULL; ent = ent->next)
9632 if (ent->plt.refcount > 0)
9633 {
91d6fa6a 9634 s = htab->iplt;
e054468f 9635 ent->plt.offset = s->size;
b9e5796b 9636 s->size += PLT_ENTRY_SIZE (htab);
e054468f 9637
25f23106 9638 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9639 }
9640 else
9641 ent->plt.offset = (bfd_vma) -1;
9642 }
65f38f15
AM
9643 }
9644
9645 /* Allocate global sym .plt and .got entries, and space for global
9646 sym dynamic relocs. */
4ce794b7 9647 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 9648
7865406b 9649 first_tlsld = NULL;
102890f0
AM
9650 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9651 {
7865406b
AM
9652 struct got_entry *ent;
9653
0c8d6e5c 9654 if (!is_ppc64_elf (ibfd))
102890f0
AM
9655 continue;
9656
7865406b
AM
9657 ent = ppc64_tlsld_got (ibfd);
9658 if (ent->got.refcount > 0)
102890f0 9659 {
7865406b 9660 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9661 {
7865406b
AM
9662 ent->is_indirect = TRUE;
9663 ent->got.ent = first_tlsld;
9664 }
9665 else
9666 {
9667 if (first_tlsld == NULL)
9668 first_tlsld = ent;
9669 s = ppc64_elf_tdata (ibfd)->got;
9670 ent->got.offset = s->size;
9671 ent->owner = ibfd;
9672 s->size += 16;
9673 if (info->shared)
9674 {
9675 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9676 srel->size += sizeof (Elf64_External_Rela);
9677 }
102890f0
AM
9678 }
9679 }
9680 else
7865406b 9681 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9682 }
9683
65f38f15
AM
9684 /* We now have determined the sizes of the various dynamic sections.
9685 Allocate memory for them. */
b34976b6 9686 relocs = FALSE;
65f38f15
AM
9687 for (s = dynobj->sections; s != NULL; s = s->next)
9688 {
9689 if ((s->flags & SEC_LINKER_CREATED) == 0)
9690 continue;
9691
4ce794b7 9692 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9693 /* These haven't been allocated yet; don't strip. */
9694 continue;
e717da7e
AM
9695 else if (s == htab->got
9696 || s == htab->plt
e054468f 9697 || s == htab->iplt
c456f082
AM
9698 || s == htab->glink
9699 || s == htab->dynbss)
65f38f15
AM
9700 {
9701 /* Strip this section if we don't need it; see the
9702 comment below. */
5bd4f169 9703 }
58d180e8
AM
9704 else if (s == htab->glink_eh_frame)
9705 {
9706 if (!bfd_is_abs_section (s->output_section))
9707 /* Not sized yet. */
9708 continue;
9709 }
70cc837d 9710 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9711 {
c456f082 9712 if (s->size != 0)
5bd4f169 9713 {
4ce794b7 9714 if (s != htab->relplt)
b34976b6 9715 relocs = TRUE;
5bd4f169
AM
9716
9717 /* We use the reloc_count field as a counter if we need
9718 to copy relocs into the output file. */
9719 s->reloc_count = 0;
9720 }
9721 }
65f38f15 9722 else
5bd4f169
AM
9723 {
9724 /* It's not one of our sections, so don't allocate space. */
9725 continue;
9726 }
9727
eea6121a 9728 if (s->size == 0)
5bd4f169 9729 {
c456f082
AM
9730 /* If we don't need this section, strip it from the
9731 output file. This is mostly to handle .rela.bss and
9732 .rela.plt. We must create both sections in
9733 create_dynamic_sections, because they must be created
9734 before the linker maps input sections to output
9735 sections. The linker does that before
9736 adjust_dynamic_symbol is called, and it is that
9737 function which decides whether anything needs to go
9738 into these sections. */
8423293d 9739 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9740 continue;
9741 }
9742
c456f082 9743 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9744 continue;
9745
65f38f15
AM
9746 /* Allocate memory for the section contents. We use bfd_zalloc
9747 here in case unused entries are not reclaimed before the
9748 section's contents are written out. This should not happen,
411e1bfb
AM
9749 but this way if it does we get a R_PPC64_NONE reloc in .rela
9750 sections instead of garbage.
9751 We also rely on the section contents being zero when writing
9752 the GOT. */
eea6121a 9753 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9754 if (s->contents == NULL)
b34976b6 9755 return FALSE;
5bd4f169
AM
9756 }
9757
e717da7e
AM
9758 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9759 {
0c8d6e5c 9760 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9761 continue;
9762
e717da7e
AM
9763 s = ppc64_elf_tdata (ibfd)->got;
9764 if (s != NULL && s != htab->got)
9765 {
eea6121a 9766 if (s->size == 0)
8423293d 9767 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9768 else
9769 {
eea6121a 9770 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9771 if (s->contents == NULL)
9772 return FALSE;
9773 }
9774 }
9775 s = ppc64_elf_tdata (ibfd)->relgot;
9776 if (s != NULL)
9777 {
eea6121a 9778 if (s->size == 0)
8423293d 9779 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9780 else
9781 {
eea6121a 9782 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9783 if (s->contents == NULL)
9784 return FALSE;
9785 relocs = TRUE;
9786 s->reloc_count = 0;
9787 }
9788 }
9789 }
9790
e86ce104 9791 if (htab->elf.dynamic_sections_created)
5bd4f169 9792 {
e8910a83
AM
9793 bfd_boolean tls_opt;
9794
5bd4f169
AM
9795 /* Add some entries to the .dynamic section. We fill in the
9796 values later, in ppc64_elf_finish_dynamic_sections, but we
9797 must add the entries now so that we get the correct size for
9798 the .dynamic section. The DT_DEBUG entry is filled in by the
9799 dynamic linker and used by the debugger. */
dc810e39 9800#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9801 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9802
36af4a4e 9803 if (info->executable)
5bd4f169 9804 {
dc810e39 9805 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9806 return FALSE;
5bd4f169
AM
9807 }
9808
eea6121a 9809 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9810 {
dc810e39
AM
9811 if (!add_dynamic_entry (DT_PLTGOT, 0)
9812 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9813 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9814 || !add_dynamic_entry (DT_JMPREL, 0)
9815 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9816 return FALSE;
5bd4f169
AM
9817 }
9818
ee67d69a 9819 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9820 {
9821 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9822 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9823 return FALSE;
19397422
AM
9824 }
9825
e8910a83
AM
9826 tls_opt = (!htab->no_tls_get_addr_opt
9827 && htab->tls_get_addr_fd != NULL
9828 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9829 if (tls_opt || !htab->opd_abi)
9830 {
9831 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9832 return FALSE;
9833 }
a7f2871e 9834
5bd4f169
AM
9835 if (relocs)
9836 {
dc810e39
AM
9837 if (!add_dynamic_entry (DT_RELA, 0)
9838 || !add_dynamic_entry (DT_RELASZ, 0)
9839 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9840 return FALSE;
5bd4f169 9841
65f38f15
AM
9842 /* If any dynamic relocs apply to a read-only section,
9843 then we need a DT_TEXTREL entry. */
248866a8 9844 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9845 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9846
65f38f15 9847 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9848 {
65f38f15 9849 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9850 return FALSE;
5bd4f169 9851 }
5bd4f169 9852 }
5bd4f169 9853 }
65f38f15 9854#undef add_dynamic_entry
5bd4f169 9855
b34976b6 9856 return TRUE;
5bd4f169
AM
9857}
9858
721956f4 9859/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9860
4ce794b7
AM
9861static inline enum ppc_stub_type
9862ppc_type_of_stub (asection *input_sec,
9863 const Elf_Internal_Rela *rel,
9864 struct ppc_link_hash_entry **hash,
e054468f 9865 struct plt_entry **plt_ent,
6911b7dc
AM
9866 bfd_vma destination,
9867 unsigned long local_off)
5bd4f169 9868{
721956f4
AM
9869 struct ppc_link_hash_entry *h = *hash;
9870 bfd_vma location;
9871 bfd_vma branch_offset;
9872 bfd_vma max_branch_offset;
4ce794b7 9873 enum elf_ppc64_reloc_type r_type;
5bd4f169 9874
721956f4
AM
9875 if (h != NULL)
9876 {
e054468f 9877 struct plt_entry *ent;
7fe2b9a6 9878 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9879 if (h->oh != NULL
9880 && h->oh->is_func_descriptor)
7b8f6675
AM
9881 {
9882 fdh = ppc_follow_link (h->oh);
9883 *hash = fdh;
9884 }
8387904d 9885
e054468f
AM
9886 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9887 if (ent->addend == rel->r_addend
9888 && ent->plt.offset != (bfd_vma) -1)
9889 {
e054468f
AM
9890 *plt_ent = ent;
9891 return ppc_stub_plt_call;
9892 }
5bd4f169 9893
7fe2b9a6
AM
9894 /* Here, we know we don't have a plt entry. If we don't have a
9895 either a defined function descriptor or a defined entry symbol
9896 in a regular object file, then it is pointless trying to make
9897 any other type of stub. */
854b41e7
AM
9898 if (!is_static_defined (&fdh->elf)
9899 && !is_static_defined (&h->elf))
721956f4 9900 return ppc_stub_none;
5d1634d7 9901 }
e054468f
AM
9902 else if (elf_local_got_ents (input_sec->owner) != NULL)
9903 {
9904 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9905 struct plt_entry **local_plt = (struct plt_entry **)
9906 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9907 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9908
9909 if (local_plt[r_symndx] != NULL)
9910 {
9911 struct plt_entry *ent;
9912
9913 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9914 if (ent->addend == rel->r_addend
9915 && ent->plt.offset != (bfd_vma) -1)
9916 {
9917 *plt_ent = ent;
9918 return ppc_stub_plt_call;
9919 }
9920 }
9921 }
5d1634d7 9922
721956f4
AM
9923 /* Determine where the call point is. */
9924 location = (input_sec->output_offset
9925 + input_sec->output_section->vma
9926 + rel->r_offset);
5d1634d7 9927
721956f4
AM
9928 branch_offset = destination - location;
9929 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9930
721956f4
AM
9931 /* Determine if a long branch stub is needed. */
9932 max_branch_offset = 1 << 25;
4ce794b7 9933 if (r_type != R_PPC64_REL24)
721956f4 9934 max_branch_offset = 1 << 15;
5d1634d7 9935
6911b7dc 9936 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
9937 /* We need a stub. Figure out whether a long_branch or plt_branch
9938 is needed later. */
9939 return ppc_stub_long_branch;
5d1634d7 9940
721956f4 9941 return ppc_stub_none;
5d1634d7
AM
9942}
9943
794e51c0
AM
9944/* With power7 weakly ordered memory model, it is possible for ld.so
9945 to update a plt entry in one thread and have another thread see a
9946 stale zero toc entry. To avoid this we need some sort of acquire
9947 barrier in the call stub. One solution is to make the load of the
9948 toc word seem to appear to depend on the load of the function entry
9949 word. Another solution is to test for r2 being zero, and branch to
9950 the appropriate glink entry if so.
9951
9952 . fake dep barrier compare
71a39c98
AM
9953 . ld 12,xxx(2) ld 12,xxx(2)
9954 . mtctr 12 mtctr 12
9955 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
9956 . add 2,2,11 cmpldi 2,0
9957 . ld 2,xxx+8(2) bnectr+
9958 . bctr b <glink_entry>
9959
9960 The solution involving the compare turns out to be faster, so
9961 that's what we use unless the branch won't reach. */
9962
9963#define ALWAYS_USE_FAKE_DEP 0
9964#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 9965
5d1634d7
AM
9966#define PPC_LO(v) ((v) & 0xffff)
9967#define PPC_HI(v) (((v) >> 16) & 0xffff)
9968#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9969
794e51c0
AM
9970static inline unsigned int
9971plt_stub_size (struct ppc_link_hash_table *htab,
9972 struct ppc_stub_hash_entry *stub_entry,
9973 bfd_vma off)
9974{
b9e5796b
AM
9975 unsigned size = 12;
9976
9977 if (ALWAYS_EMIT_R2SAVE
9978 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9979 size += 4;
9980 if (PPC_HA (off) != 0)
794e51c0 9981 size += 4;
b9e5796b
AM
9982 if (htab->opd_abi)
9983 {
9984 size += 4;
9985 if (htab->plt_static_chain)
9986 size += 4;
9987 if (htab->plt_thread_safe)
9988 size += 8;
9989 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9990 size += 4;
9991 }
794e51c0
AM
9992 if (stub_entry->h != NULL
9993 && (stub_entry->h == htab->tls_get_addr_fd
9994 || stub_entry->h == htab->tls_get_addr)
9995 && !htab->no_tls_get_addr_opt)
9996 size += 13 * 4;
9997 return size;
9998}
9999
10000/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10001 then return the padding needed to do so. */
10002static inline unsigned int
10003plt_stub_pad (struct ppc_link_hash_table *htab,
10004 struct ppc_stub_hash_entry *stub_entry,
10005 bfd_vma plt_off)
10006{
10007 int stub_align = 1 << htab->plt_stub_align;
10008 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10009 bfd_vma stub_off = stub_entry->stub_sec->size;
10010
10011 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10012 > (stub_size & -stub_align))
10013 return stub_align - (stub_off & (stub_align - 1));
10014 return 0;
10015}
10016
10017/* Build a .plt call stub. */
10018
10019static inline bfd_byte *
10020build_plt_stub (struct ppc_link_hash_table *htab,
10021 struct ppc_stub_hash_entry *stub_entry,
10022 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10023{
10024 bfd *obfd = htab->stub_bfd;
b9e5796b 10025 bfd_boolean plt_load_toc = htab->opd_abi;
794e51c0
AM
10026 bfd_boolean plt_static_chain = htab->plt_static_chain;
10027 bfd_boolean plt_thread_safe = htab->plt_thread_safe;
10028 bfd_boolean use_fake_dep = plt_thread_safe;
10029 bfd_vma cmp_branch_off = 0;
10030
10031 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10032 && plt_load_toc
794e51c0
AM
10033 && plt_thread_safe
10034 && !(stub_entry->h != NULL
10035 && (stub_entry->h == htab->tls_get_addr_fd
10036 || stub_entry->h == htab->tls_get_addr)
10037 && !htab->no_tls_get_addr_opt))
10038 {
10039 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10040 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10041 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10042 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10043 bfd_vma to, from;
10044
68d62958
AM
10045 if (pltindex > 32768)
10046 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10047 to = (glinkoff
10048 + htab->glink->output_offset
10049 + htab->glink->output_section->vma);
10050 from = (p - stub_entry->stub_sec->contents
10051 + 4 * (ALWAYS_EMIT_R2SAVE
10052 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10053 + 4 * (PPC_HA (offset) != 0)
10054 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10055 != PPC_HA (offset))
10056 + 4 * (plt_static_chain != 0)
10057 + 20
10058 + stub_entry->stub_sec->output_offset
10059 + stub_entry->stub_sec->output_section->vma);
10060 cmp_branch_off = to - from;
10061 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10062 }
10063
ac2df442
AM
10064 if (PPC_HA (offset) != 0)
10065 {
176a0d42
AM
10066 if (r != NULL)
10067 {
794e51c0
AM
10068 if (ALWAYS_EMIT_R2SAVE
10069 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10070 r[0].r_offset += 4;
176a0d42 10071 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10072 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10073 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10074 r[1].r_addend = r[0].r_addend;
b9e5796b 10075 if (plt_load_toc)
176a0d42 10076 {
b9e5796b 10077 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10078 {
b9e5796b
AM
10079 r[2].r_offset = r[1].r_offset + 4;
10080 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10081 r[2].r_addend = r[0].r_addend;
10082 }
10083 else
10084 {
10085 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10086 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10087 r[2].r_addend = r[0].r_addend + 8;
10088 if (plt_static_chain)
10089 {
10090 r[3].r_offset = r[2].r_offset + 4;
10091 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10092 r[3].r_addend = r[0].r_addend + 16;
10093 }
c7131b65 10094 }
176a0d42
AM
10095 }
10096 }
794e51c0
AM
10097 if (ALWAYS_EMIT_R2SAVE
10098 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10099 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98
AM
10100 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10101 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10102 if (plt_load_toc
10103 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10104 {
71a39c98 10105 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10106 offset = 0;
10107 }
71a39c98 10108 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10109 if (plt_load_toc)
794e51c0 10110 {
b9e5796b
AM
10111 if (use_fake_dep)
10112 {
10113 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10114 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10115 }
10116 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10117 if (plt_static_chain)
10118 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10119 }
ac2df442
AM
10120 }
10121 else
10122 {
176a0d42
AM
10123 if (r != NULL)
10124 {
794e51c0
AM
10125 if (ALWAYS_EMIT_R2SAVE
10126 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10127 r[0].r_offset += 4;
176a0d42 10128 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10129 if (plt_load_toc)
176a0d42 10130 {
b9e5796b 10131 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10132 {
b9e5796b
AM
10133 r[1].r_offset = r[0].r_offset + 4;
10134 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10135 r[1].r_addend = r[0].r_addend;
10136 }
10137 else
10138 {
10139 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10140 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10141 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10142 if (plt_static_chain)
10143 {
10144 r[2].r_offset = r[1].r_offset + 4;
10145 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10146 r[2].r_addend = r[0].r_addend + 8;
10147 }
c7131b65 10148 }
176a0d42
AM
10149 }
10150 }
794e51c0
AM
10151 if (ALWAYS_EMIT_R2SAVE
10152 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10153 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10154 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10155 if (plt_load_toc
10156 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10157 {
10158 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10159 offset = 0;
10160 }
71a39c98 10161 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10162 if (plt_load_toc)
794e51c0 10163 {
b9e5796b
AM
10164 if (use_fake_dep)
10165 {
10166 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10167 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10168 }
10169 if (plt_static_chain)
10170 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10171 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10172 }
ac2df442 10173 }
b9e5796b 10174 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10175 {
10176 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10177 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10178 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10179 }
10180 else
10181 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10182 return p;
10183}
10184
a7f2871e
AM
10185/* Build a special .plt call stub for __tls_get_addr. */
10186
10187#define LD_R11_0R3 0xe9630000
10188#define LD_R12_0R3 0xe9830000
10189#define MR_R0_R3 0x7c601b78
10190#define CMPDI_R11_0 0x2c2b0000
10191#define ADD_R3_R12_R13 0x7c6c6a14
10192#define BEQLR 0x4d820020
10193#define MR_R3_R0 0x7c030378
a7f2871e
AM
10194#define STD_R11_0R1 0xf9610000
10195#define BCTRL 0x4e800421
10196#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10197#define MTLR_R11 0x7d6803a6
10198
10199static inline bfd_byte *
794e51c0
AM
10200build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10201 struct ppc_stub_hash_entry *stub_entry,
10202 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10203{
794e51c0
AM
10204 bfd *obfd = htab->stub_bfd;
10205
a7f2871e
AM
10206 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10207 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10208 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10209 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10210 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10211 bfd_put_32 (obfd, BEQLR, p), p += 4;
10212 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10213 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10214 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10215
10216 if (r != NULL)
10217 r[0].r_offset += 9 * 4;
794e51c0 10218 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10219 bfd_put_32 (obfd, BCTRL, p - 4);
10220
a078d95a
AM
10221 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10222 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
a7f2871e
AM
10223 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10224 bfd_put_32 (obfd, BLR, p), p += 4;
10225
10226 return p;
10227}
10228
176a0d42
AM
10229static Elf_Internal_Rela *
10230get_relocs (asection *sec, int count)
10231{
10232 Elf_Internal_Rela *relocs;
10233 struct bfd_elf_section_data *elfsec_data;
10234
10235 elfsec_data = elf_section_data (sec);
10236 relocs = elfsec_data->relocs;
10237 if (relocs == NULL)
10238 {
10239 bfd_size_type relsize;
10240 relsize = sec->reloc_count * sizeof (*relocs);
10241 relocs = bfd_alloc (sec->owner, relsize);
10242 if (relocs == NULL)
10243 return NULL;
10244 elfsec_data->relocs = relocs;
d4730f92
BS
10245 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10246 sizeof (Elf_Internal_Shdr));
10247 if (elfsec_data->rela.hdr == NULL)
10248 return NULL;
10249 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10250 * sizeof (Elf64_External_Rela));
10251 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10252 sec->reloc_count = 0;
10253 }
10254 relocs += sec->reloc_count;
10255 sec->reloc_count += count;
10256 return relocs;
10257}
10258
aa374f67 10259static bfd_vma
25f53a85 10260get_r2off (struct bfd_link_info *info,
aa374f67
AM
10261 struct ppc_stub_hash_entry *stub_entry)
10262{
25f53a85 10263 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
10264 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10265
10266 if (r2off == 0)
10267 {
10268 /* Support linking -R objects. Get the toc pointer from the
10269 opd entry. */
10270 char buf[8];
b9e5796b
AM
10271 if (!htab->opd_abi)
10272 return r2off;
aa374f67
AM
10273 asection *opd = stub_entry->h->elf.root.u.def.section;
10274 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10275
10276 if (strcmp (opd->name, ".opd") != 0
10277 || opd->reloc_count != 0)
10278 {
bc30df16 10279 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10280 stub_entry->h->elf.root.root.string);
aa374f67
AM
10281 bfd_set_error (bfd_error_bad_value);
10282 return 0;
10283 }
10284 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10285 return 0;
10286 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10287 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
10288 }
10289 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10290 return r2off;
10291}
10292
b34976b6 10293static bfd_boolean
4ce794b7 10294ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10295{
721956f4
AM
10296 struct ppc_stub_hash_entry *stub_entry;
10297 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10298 struct bfd_link_info *info;
10299 struct ppc_link_hash_table *htab;
721956f4
AM
10300 bfd_byte *loc;
10301 bfd_byte *p;
ee75fd95 10302 bfd_vma dest, off;
721956f4 10303 int size;
176a0d42 10304 Elf_Internal_Rela *r;
e054468f 10305 asection *plt;
5d1634d7 10306
721956f4
AM
10307 /* Massage our args to the form they really have. */
10308 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10309 info = in_arg;
5d1634d7 10310
5d1634d7 10311 htab = ppc_hash_table (info);
4dfe6ac6
NC
10312 if (htab == NULL)
10313 return FALSE;
5d1634d7 10314
721956f4 10315 /* Make a note of the offset within the stubs for this entry. */
eea6121a 10316 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 10317 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 10318
4ce794b7 10319 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10320 switch (stub_entry->stub_type)
5d1634d7 10321 {
721956f4 10322 case ppc_stub_long_branch:
ad8e1ba5 10323 case ppc_stub_long_branch_r2off:
721956f4 10324 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10325 dest = (stub_entry->target_value
10326 + stub_entry->target_section->output_offset
10327 + stub_entry->target_section->output_section->vma);
10328 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10329 off = dest;
5d1634d7 10330
721956f4
AM
10331 /* And this is where we are coming from. */
10332 off -= (stub_entry->stub_offset
97b639ba
AM
10333 + stub_entry->stub_sec->output_offset
10334 + stub_entry->stub_sec->output_section->vma);
e86ce104 10335
ac2df442
AM
10336 size = 4;
10337 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10338 {
25f53a85 10339 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10340
aa374f67
AM
10341 if (r2off == 0)
10342 {
10343 htab->stub_error = TRUE;
10344 return FALSE;
10345 }
a078d95a 10346 bfd_put_32 (htab->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10347 loc += 4;
ac2df442
AM
10348 size = 12;
10349 if (PPC_HA (r2off) != 0)
10350 {
10351 size = 16;
10352 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10353 loc += 4;
10354 }
97b639ba 10355 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 10356 loc += 4;
ac2df442 10357 off -= size - 4;
ad8e1ba5 10358 }
97b639ba 10359 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10360
5c3dead3
AM
10361 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10362 {
bc30df16
AM
10363 info->callbacks->einfo
10364 (_("%P: long branch stub `%s' offset overflow\n"),
10365 stub_entry->root.string);
5c3dead3
AM
10366 htab->stub_error = TRUE;
10367 return FALSE;
10368 }
ee75fd95
AM
10369
10370 if (info->emitrelocations)
10371 {
176a0d42
AM
10372 r = get_relocs (stub_entry->stub_sec, 1);
10373 if (r == NULL)
10374 return FALSE;
ee75fd95
AM
10375 r->r_offset = loc - stub_entry->stub_sec->contents;
10376 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10377 r->r_addend = dest;
10378 if (stub_entry->h != NULL)
10379 {
10380 struct elf_link_hash_entry **hashes;
10381 unsigned long symndx;
10382 struct ppc_link_hash_entry *h;
10383
10384 hashes = elf_sym_hashes (htab->stub_bfd);
10385 if (hashes == NULL)
10386 {
10387 bfd_size_type hsize;
10388
10389 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10390 hashes = bfd_zalloc (htab->stub_bfd, hsize);
10391 if (hashes == NULL)
10392 return FALSE;
10393 elf_sym_hashes (htab->stub_bfd) = hashes;
10394 htab->stub_globals = 1;
10395 }
10396 symndx = htab->stub_globals++;
10397 h = stub_entry->h;
10398 hashes[symndx] = &h->elf;
10399 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10400 if (h->oh != NULL && h->oh->is_func)
b31867b6 10401 h = ppc_follow_link (h->oh);
ee75fd95
AM
10402 if (h->elf.root.u.def.section != stub_entry->target_section)
10403 /* H is an opd symbol. The addend must be zero. */
10404 r->r_addend = 0;
10405 else
10406 {
10407 off = (h->elf.root.u.def.value
10408 + h->elf.root.u.def.section->output_offset
10409 + h->elf.root.u.def.section->output_section->vma);
10410 r->r_addend -= off;
10411 }
10412 }
10413 }
721956f4 10414 break;
e86ce104 10415
721956f4 10416 case ppc_stub_plt_branch:
ad8e1ba5 10417 case ppc_stub_plt_branch_r2off:
721956f4
AM
10418 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10419 stub_entry->root.string + 9,
b34976b6 10420 FALSE, FALSE);
721956f4
AM
10421 if (br_entry == NULL)
10422 {
8de848d8 10423 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10424 stub_entry->root.string);
b34976b6
AM
10425 htab->stub_error = TRUE;
10426 return FALSE;
721956f4
AM
10427 }
10428
176a0d42
AM
10429 dest = (stub_entry->target_value
10430 + stub_entry->target_section->output_offset
10431 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10432 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10433 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10434
176a0d42 10435 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10436 htab->brlt->contents + br_entry->offset);
721956f4 10437
f94498ff 10438 if (br_entry->iter == htab->stub_iteration)
721956f4 10439 {
f94498ff 10440 br_entry->iter = 0;
84f5d08e 10441
f94498ff 10442 if (htab->relbrlt != NULL)
84f5d08e 10443 {
f94498ff
AM
10444 /* Create a reloc for the branch lookup table entry. */
10445 Elf_Internal_Rela rela;
10446 bfd_byte *rl;
10447
10448 rela.r_offset = (br_entry->offset
10449 + htab->brlt->output_offset
10450 + htab->brlt->output_section->vma);
10451 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10452 rela.r_addend = dest;
f94498ff
AM
10453
10454 rl = htab->relbrlt->contents;
10455 rl += (htab->relbrlt->reloc_count++
10456 * sizeof (Elf64_External_Rela));
10457 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10458 }
10459 else if (info->emitrelocations)
10460 {
176a0d42
AM
10461 r = get_relocs (htab->brlt, 1);
10462 if (r == NULL)
10463 return FALSE;
10464 /* brlt, being SEC_LINKER_CREATED does not go through the
10465 normal reloc processing. Symbols and offsets are not
10466 translated from input file to output file form, so
10467 set up the offset per the output file. */
f94498ff
AM
10468 r->r_offset = (br_entry->offset
10469 + htab->brlt->output_offset
10470 + htab->brlt->output_section->vma);
10471 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10472 r->r_addend = dest;
84f5d08e 10473 }
84f5d08e 10474 }
721956f4 10475
176a0d42
AM
10476 dest = (br_entry->offset
10477 + htab->brlt->output_offset
10478 + htab->brlt->output_section->vma);
10479
10480 off = (dest
4ce794b7 10481 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 10482 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10483
ad8e1ba5 10484 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10485 {
25f53a85 10486 info->callbacks->einfo
bc30df16 10487 (_("%P: linkage table error against `%T'\n"),
721956f4 10488 stub_entry->root.string);
5d1634d7 10489 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10490 htab->stub_error = TRUE;
10491 return FALSE;
5d1634d7 10492 }
41bd81ab 10493
176a0d42
AM
10494 if (info->emitrelocations)
10495 {
10496 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10497 if (r == NULL)
10498 return FALSE;
10499 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10500 if (bfd_big_endian (info->output_bfd))
10501 r[0].r_offset += 2;
b9e5796b
AM
10502 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off
10503 && htab->opd_abi)
176a0d42
AM
10504 r[0].r_offset += 4;
10505 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10506 r[0].r_addend = dest;
10507 if (PPC_HA (off) != 0)
10508 {
10509 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10510 r[1].r_offset = r[0].r_offset + 4;
10511 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10512 r[1].r_addend = r[0].r_addend;
10513 }
10514 }
10515
b9e5796b
AM
10516 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10517 || !htab->opd_abi)
ad8e1ba5 10518 {
176a0d42 10519 if (PPC_HA (off) != 0)
ac2df442
AM
10520 {
10521 size = 16;
71a39c98 10522 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
ac2df442 10523 loc += 4;
71a39c98 10524 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
ac2df442
AM
10525 }
10526 else
10527 {
10528 size = 12;
71a39c98 10529 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 10530 }
ad8e1ba5
AM
10531 }
10532 else
10533 {
25f53a85 10534 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
10535
10536 if (r2off == 0)
10537 {
10538 htab->stub_error = TRUE;
10539 return FALSE;
10540 }
ad8e1ba5 10541
a078d95a 10542 bfd_put_32 (htab->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10543 loc += 4;
ac2df442 10544 size = 20;
176a0d42 10545 if (PPC_HA (off) != 0)
ac2df442
AM
10546 {
10547 size += 4;
71a39c98 10548 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
ac2df442 10549 loc += 4;
71a39c98 10550 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
ac2df442
AM
10551 loc += 4;
10552 }
10553 else
10554 {
71a39c98 10555 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
10556 loc += 4;
10557 }
10558
10559 if (PPC_HA (r2off) != 0)
10560 {
10561 size += 4;
10562 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10563 loc += 4;
10564 }
97b639ba 10565 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
10566 }
10567 loc += 4;
71a39c98 10568 bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 10569 loc += 4;
97b639ba 10570 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 10571 break;
5d1634d7 10572
721956f4 10573 case ppc_stub_plt_call:
794e51c0 10574 case ppc_stub_plt_call_r2save:
e054468f 10575 if (stub_entry->h != NULL
b31867b6
AM
10576 && stub_entry->h->is_func_descriptor
10577 && stub_entry->h->oh != NULL)
c862ae31 10578 {
b31867b6
AM
10579 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10580
10581 /* If the old-ABI "dot-symbol" is undefined make it weak so
10582 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10583 FIXME: We used to define the symbol on one of the call
10584 stubs instead, which is why we test symbol section id
10585 against htab->top_id in various places. Likely all
10586 these checks could now disappear. */
10587 if (fh->elf.root.type == bfd_link_hash_undefined)
10588 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
10589 /* Stop undo_symbol_twiddle changing it back to undefined. */
10590 fh->was_undefined = 0;
c862ae31
AM
10591 }
10592
721956f4 10593 /* Now build the stub. */
e054468f 10594 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 10595 if (dest >= (bfd_vma) -2)
721956f4
AM
10596 abort ();
10597
e054468f 10598 plt = htab->plt;
25f23106
AM
10599 if (!htab->elf.dynamic_sections_created
10600 || stub_entry->h == NULL
10601 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10602 plt = htab->iplt;
10603
10604 dest += plt->output_offset + plt->output_section->vma;
10605
10606 if (stub_entry->h == NULL
10607 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10608 {
10609 Elf_Internal_Rela rela;
10610 bfd_byte *rl;
10611
10612 rela.r_offset = dest;
ee67d69a
AM
10613 if (htab->opd_abi)
10614 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10615 else
10616 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
10617 rela.r_addend = (stub_entry->target_value
10618 + stub_entry->target_section->output_offset
10619 + stub_entry->target_section->output_section->vma);
10620
25f23106
AM
10621 rl = (htab->reliplt->contents
10622 + (htab->reliplt->reloc_count++
10623 * sizeof (Elf64_External_Rela)));
10624 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
10625 stub_entry->plt_ent->plt.offset |= 1;
10626 }
176a0d42
AM
10627
10628 off = (dest
e054468f 10629 - elf_gp (plt->output_section->owner)
176a0d42 10630 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10631
ad8e1ba5 10632 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10633 {
25f53a85 10634 info->callbacks->einfo
bc30df16 10635 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
10636 stub_entry->h != NULL
10637 ? stub_entry->h->elf.root.root.string
10638 : "<local sym>");
721956f4 10639 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10640 htab->stub_error = TRUE;
10641 return FALSE;
721956f4
AM
10642 }
10643
794e51c0
AM
10644 if (htab->plt_stub_align != 0)
10645 {
10646 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10647
10648 stub_entry->stub_sec->size += pad;
10649 stub_entry->stub_offset = stub_entry->stub_sec->size;
10650 loc += pad;
10651 }
10652
176a0d42
AM
10653 r = NULL;
10654 if (info->emitrelocations)
10655 {
10656 r = get_relocs (stub_entry->stub_sec,
c7131b65
AM
10657 (2
10658 + (PPC_HA (off) != 0)
10659 + (htab->plt_static_chain
10660 && PPC_HA (off + 16) == PPC_HA (off))));
176a0d42
AM
10661 if (r == NULL)
10662 return FALSE;
10663 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10664 if (bfd_big_endian (info->output_bfd))
10665 r[0].r_offset += 2;
176a0d42
AM
10666 r[0].r_addend = dest;
10667 }
a7f2871e
AM
10668 if (stub_entry->h != NULL
10669 && (stub_entry->h == htab->tls_get_addr_fd
10670 || stub_entry->h == htab->tls_get_addr)
10671 && !htab->no_tls_get_addr_opt)
794e51c0 10672 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10673 else
794e51c0 10674 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10675 size = p - loc;
10676 break;
10677
10678 default:
10679 BFD_FAIL ();
b34976b6 10680 return FALSE;
721956f4
AM
10681 }
10682
eea6121a 10683 stub_entry->stub_sec->size += size;
97b639ba 10684
ee75fd95 10685 if (htab->emit_stub_syms)
97b639ba
AM
10686 {
10687 struct elf_link_hash_entry *h;
ee75fd95
AM
10688 size_t len1, len2;
10689 char *name;
10690 const char *const stub_str[] = { "long_branch",
10691 "long_branch_r2off",
10692 "plt_branch",
10693 "plt_branch_r2off",
794e51c0 10694 "plt_call",
ee75fd95
AM
10695 "plt_call" };
10696
10697 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10698 len2 = strlen (stub_entry->root.string);
10699 name = bfd_malloc (len1 + len2 + 2);
10700 if (name == NULL)
10701 return FALSE;
10702 memcpy (name, stub_entry->root.string, 9);
10703 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10704 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10705 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10706 if (h == NULL)
10707 return FALSE;
10708 if (h->root.type == bfd_link_hash_new)
10709 {
10710 h->root.type = bfd_link_hash_defined;
10711 h->root.u.def.section = stub_entry->stub_sec;
10712 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10713 h->ref_regular = 1;
10714 h->def_regular = 1;
10715 h->ref_regular_nonweak = 1;
10716 h->forced_local = 1;
10717 h->non_elf = 0;
97b639ba
AM
10718 }
10719 }
10720
b34976b6 10721 return TRUE;
721956f4
AM
10722}
10723
10724/* As above, but don't actually build the stub. Just bump offset so
10725 we know stub section sizes, and select plt_branch stubs where
10726 long_branch stubs won't do. */
10727
b34976b6 10728static bfd_boolean
4ce794b7 10729ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10730{
10731 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10732 struct bfd_link_info *info;
721956f4
AM
10733 struct ppc_link_hash_table *htab;
10734 bfd_vma off;
10735 int size;
10736
10737 /* Massage our args to the form they really have. */
10738 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10739 info = in_arg;
10740
10741 htab = ppc_hash_table (info);
4dfe6ac6
NC
10742 if (htab == NULL)
10743 return FALSE;
721956f4 10744
794e51c0
AM
10745 if (stub_entry->stub_type == ppc_stub_plt_call
10746 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 10747 {
e054468f
AM
10748 asection *plt;
10749 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 10750 if (off >= (bfd_vma) -2)
411e1bfb 10751 abort ();
e054468f 10752 plt = htab->plt;
25f23106
AM
10753 if (!htab->elf.dynamic_sections_created
10754 || stub_entry->h == NULL
10755 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10756 plt = htab->iplt;
10757 off += (plt->output_offset
10758 + plt->output_section->vma
10759 - elf_gp (plt->output_section->owner)
ad8e1ba5 10760 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10761
794e51c0
AM
10762 size = plt_stub_size (htab, stub_entry, off);
10763 if (htab->plt_stub_align)
10764 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
10765 if (info->emitrelocations)
10766 {
10767 stub_entry->stub_sec->reloc_count
b9e5796b
AM
10768 += ((PPC_HA (off) != 0)
10769 + (htab->opd_abi
10770 ? 2 + (htab->plt_static_chain
10771 && PPC_HA (off + 16) == PPC_HA (off))
10772 : 1));
176a0d42
AM
10773 stub_entry->stub_sec->flags |= SEC_RELOC;
10774 }
721956f4
AM
10775 }
10776 else
10777 {
ad8e1ba5
AM
10778 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10779 variants. */
ac2df442 10780 bfd_vma r2off = 0;
6911b7dc 10781 bfd_vma local_off = 0;
ac2df442 10782
721956f4
AM
10783 off = (stub_entry->target_value
10784 + stub_entry->target_section->output_offset
10785 + stub_entry->target_section->output_section->vma);
eea6121a 10786 off -= (stub_entry->stub_sec->size
721956f4
AM
10787 + stub_entry->stub_sec->output_offset
10788 + stub_entry->stub_sec->output_section->vma);
10789
ad8e1ba5
AM
10790 /* Reset the stub type from the plt variant in case we now
10791 can reach with a shorter stub. */
10792 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10793 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10794
10795 size = 4;
10796 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10797 {
25f53a85 10798 r2off = get_r2off (info, stub_entry);
b9e5796b 10799 if (r2off == 0 && htab->opd_abi)
aa374f67
AM
10800 {
10801 htab->stub_error = TRUE;
10802 return FALSE;
10803 }
ac2df442
AM
10804 size = 12;
10805 if (PPC_HA (r2off) != 0)
10806 size = 16;
10807 off -= size - 4;
ad8e1ba5
AM
10808 }
10809
6911b7dc
AM
10810 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10811
b9e5796b
AM
10812 /* If the branch offset if too big, use a ppc_stub_plt_branch.
10813 Do the same for -R objects without function descriptors. */
10814 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
10815 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
10816 && r2off == 0))
721956f4
AM
10817 {
10818 struct ppc_branch_hash_entry *br_entry;
10819
10820 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10821 stub_entry->root.string + 9,
b34976b6 10822 TRUE, FALSE);
721956f4
AM
10823 if (br_entry == NULL)
10824 {
8de848d8 10825 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 10826 stub_entry->root.string);
b34976b6
AM
10827 htab->stub_error = TRUE;
10828 return FALSE;
721956f4
AM
10829 }
10830
10831 if (br_entry->iter != htab->stub_iteration)
10832 {
10833 br_entry->iter = htab->stub_iteration;
eea6121a
AM
10834 br_entry->offset = htab->brlt->size;
10835 htab->brlt->size += 8;
63bc6f6c 10836
ee75fd95 10837 if (htab->relbrlt != NULL)
eea6121a 10838 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
10839 else if (info->emitrelocations)
10840 {
10841 htab->brlt->reloc_count += 1;
10842 htab->brlt->flags |= SEC_RELOC;
10843 }
721956f4 10844 }
ad8e1ba5
AM
10845
10846 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
10847 off = (br_entry->offset
10848 + htab->brlt->output_offset
10849 + htab->brlt->output_section->vma
10850 - elf_gp (htab->brlt->output_section->owner)
10851 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10852
176a0d42
AM
10853 if (info->emitrelocations)
10854 {
10855 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10856 stub_entry->stub_sec->flags |= SEC_RELOC;
10857 }
10858
b9e5796b
AM
10859 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10860 || !htab->opd_abi)
ac2df442
AM
10861 {
10862 size = 12;
176a0d42 10863 if (PPC_HA (off) != 0)
ac2df442
AM
10864 size = 16;
10865 }
10866 else
10867 {
10868 size = 20;
176a0d42 10869 if (PPC_HA (off) != 0)
ac2df442
AM
10870 size += 4;
10871
10872 if (PPC_HA (r2off) != 0)
10873 size += 4;
10874 }
721956f4 10875 }
84f5d08e
AM
10876 else if (info->emitrelocations)
10877 {
10878 stub_entry->stub_sec->reloc_count += 1;
10879 stub_entry->stub_sec->flags |= SEC_RELOC;
10880 }
721956f4
AM
10881 }
10882
eea6121a 10883 stub_entry->stub_sec->size += size;
b34976b6 10884 return TRUE;
721956f4
AM
10885}
10886
10887/* Set up various things so that we can make a list of input sections
10888 for each output section included in the link. Returns -1 on error,
cedb70c5 10889 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10890
10891int
927be08e
AM
10892ppc64_elf_setup_section_lists
10893 (struct bfd_link_info *info,
10894 asection *(*add_stub_section) (const char *, asection *),
10895 void (*layout_sections_again) (void))
721956f4
AM
10896{
10897 bfd *input_bfd;
734b6cf9 10898 int top_id, top_index, id;
721956f4 10899 asection *section;
734b6cf9 10900 asection **input_list;
721956f4
AM
10901 bfd_size_type amt;
10902 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10903
4dfe6ac6
NC
10904 if (htab == NULL)
10905 return -1;
927be08e
AM
10906 /* Stash our params away. */
10907 htab->add_stub_section = add_stub_section;
10908 htab->layout_sections_again = layout_sections_again;
4c52953f 10909
1e2f5b6e 10910 /* Find the top input section id. */
3d6f9012 10911 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10912 input_bfd != NULL;
10913 input_bfd = input_bfd->link_next)
10914 {
721956f4
AM
10915 for (section = input_bfd->sections;
10916 section != NULL;
10917 section = section->next)
10918 {
10919 if (top_id < section->id)
10920 top_id = section->id;
10921 }
10922 }
721956f4 10923
8f3bab57 10924 htab->top_id = top_id;
721956f4 10925 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10926 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10927 if (htab->stub_group == NULL)
10928 return -1;
10929
3d6f9012
AM
10930 /* Set toc_off for com, und, abs and ind sections. */
10931 for (id = 0; id < 3; id++)
10932 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10933
734b6cf9
AM
10934 /* We can't use output_bfd->section_count here to find the top output
10935 section index as some sections may have been removed, and
8423293d 10936 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10937 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10938 section != NULL;
10939 section = section->next)
10940 {
10941 if (top_index < section->index)
10942 top_index = section->index;
10943 }
10944
10945 htab->top_index = top_index;
10946 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10947 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10948 htab->input_list = input_list;
10949 if (input_list == NULL)
10950 return -1;
10951
721956f4
AM
10952 return 1;
10953}
10954
927be08e
AM
10955/* Set up for first pass at multitoc partitioning. */
10956
10957void
10958ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10959{
10960 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10961
1c865ab2 10962 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
10963 htab->toc_bfd = NULL;
10964 htab->toc_first_sec = NULL;
10965}
10966
e717da7e
AM
10967/* The linker repeatedly calls this function for each TOC input section
10968 and linker generated GOT section. Group input bfds such that the toc
927be08e 10969 within a group is less than 64k in size. */
ad8e1ba5 10970
927be08e 10971bfd_boolean
4ce794b7 10972ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10973{
10974 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10975 bfd_vma addr, off, limit;
ad8e1ba5 10976
4dfe6ac6
NC
10977 if (htab == NULL)
10978 return FALSE;
10979
927be08e 10980 if (!htab->second_toc_pass)
4c52953f 10981 {
927be08e 10982 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
10983 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10984
10985 if (new_bfd)
bf102f86
AM
10986 {
10987 htab->toc_bfd = isec->owner;
10988 htab->toc_first_sec = isec;
10989 }
927be08e 10990
bf102f86
AM
10991 addr = isec->output_offset + isec->output_section->vma;
10992 off = addr - htab->toc_curr;
d77c8a4b
AM
10993 limit = 0x80008000;
10994 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10995 limit = 0x10000;
10996 if (off + isec->size > limit)
bf102f86
AM
10997 {
10998 addr = (htab->toc_first_sec->output_offset
10999 + htab->toc_first_sec->output_section->vma);
11000 htab->toc_curr = addr;
11001 }
99877b66 11002
927be08e
AM
11003 /* toc_curr is the base address of this toc group. Set elf_gp
11004 for the input section to be the offset relative to the
11005 output toc base plus 0x8000. Making the input elf_gp an
11006 offset allows us to move the toc as a whole without
11007 recalculating input elf_gp. */
11008 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11009 off += TOC_BASE_OFF;
11010
11011 /* Die if someone uses a linker script that doesn't keep input
11012 file .toc and .got together. */
a4fd3de5
AM
11013 if (new_bfd
11014 && elf_gp (isec->owner) != 0
927be08e
AM
11015 && elf_gp (isec->owner) != off)
11016 return FALSE;
11017
11018 elf_gp (isec->owner) = off;
11019 return TRUE;
4c52953f 11020 }
927be08e
AM
11021
11022 /* During the second pass toc_first_sec points to the start of
11023 a toc group, and toc_curr is used to track the old elf_gp.
11024 We use toc_bfd to ensure we only look at each bfd once. */
11025 if (htab->toc_bfd == isec->owner)
11026 return TRUE;
11027 htab->toc_bfd = isec->owner;
11028
11029 if (htab->toc_first_sec == NULL
11030 || htab->toc_curr != elf_gp (isec->owner))
11031 {
11032 htab->toc_curr = elf_gp (isec->owner);
11033 htab->toc_first_sec = isec;
11034 }
11035 addr = (htab->toc_first_sec->output_offset
11036 + htab->toc_first_sec->output_section->vma);
11037 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11038 elf_gp (isec->owner) = off;
11039
11040 return TRUE;
ad8e1ba5
AM
11041}
11042
927be08e
AM
11043/* Called via elf_link_hash_traverse to merge GOT entries for global
11044 symbol H. */
11045
11046static bfd_boolean
11047merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11048{
11049 if (h->root.type == bfd_link_hash_indirect)
11050 return TRUE;
11051
927be08e
AM
11052 merge_got_entries (&h->got.glist);
11053
11054 return TRUE;
11055}
11056
11057/* Called via elf_link_hash_traverse to allocate GOT entries for global
11058 symbol H. */
11059
11060static bfd_boolean
11061reallocate_got (struct elf_link_hash_entry *h, void *inf)
11062{
11063 struct got_entry *gent;
11064
11065 if (h->root.type == bfd_link_hash_indirect)
11066 return TRUE;
11067
927be08e
AM
11068 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11069 if (!gent->is_indirect)
11070 allocate_got (h, (struct bfd_link_info *) inf, gent);
11071 return TRUE;
11072}
11073
11074/* Called on the first multitoc pass after the last call to
11075 ppc64_elf_next_toc_section. This function removes duplicate GOT
11076 entries. */
11077
11078bfd_boolean
11079ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11080{
11081 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11082 struct bfd *ibfd, *ibfd2;
11083 bfd_boolean done_something;
11084
11085 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11086
7865406b
AM
11087 if (!htab->do_multi_toc)
11088 return FALSE;
11089
d0fae19d 11090 /* Merge global sym got entries within a toc group. */
927be08e
AM
11091 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11092
11093 /* And tlsld_got. */
11094 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11095 {
11096 struct got_entry *ent, *ent2;
11097
11098 if (!is_ppc64_elf (ibfd))
11099 continue;
11100
11101 ent = ppc64_tlsld_got (ibfd);
11102 if (!ent->is_indirect
11103 && ent->got.offset != (bfd_vma) -1)
11104 {
11105 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11106 {
11107 if (!is_ppc64_elf (ibfd2))
11108 continue;
11109
11110 ent2 = ppc64_tlsld_got (ibfd2);
11111 if (!ent2->is_indirect
11112 && ent2->got.offset != (bfd_vma) -1
11113 && elf_gp (ibfd2) == elf_gp (ibfd))
11114 {
11115 ent2->is_indirect = TRUE;
11116 ent2->got.ent = ent;
11117 }
11118 }
11119 }
11120 }
11121
11122 /* Zap sizes of got sections. */
11123 htab->reliplt->rawsize = htab->reliplt->size;
11124 htab->reliplt->size -= htab->got_reli_size;
11125 htab->got_reli_size = 0;
11126
11127 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11128 {
11129 asection *got, *relgot;
11130
11131 if (!is_ppc64_elf (ibfd))
11132 continue;
11133
11134 got = ppc64_elf_tdata (ibfd)->got;
11135 if (got != NULL)
11136 {
11137 got->rawsize = got->size;
11138 got->size = 0;
11139 relgot = ppc64_elf_tdata (ibfd)->relgot;
11140 relgot->rawsize = relgot->size;
11141 relgot->size = 0;
11142 }
11143 }
11144
11145 /* Now reallocate the got, local syms first. We don't need to
11146 allocate section contents again since we never increase size. */
11147 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11148 {
11149 struct got_entry **lgot_ents;
11150 struct got_entry **end_lgot_ents;
11151 struct plt_entry **local_plt;
11152 struct plt_entry **end_local_plt;
f961d9dd 11153 unsigned char *lgot_masks;
927be08e
AM
11154 bfd_size_type locsymcount;
11155 Elf_Internal_Shdr *symtab_hdr;
19e08130 11156 asection *s;
927be08e
AM
11157
11158 if (!is_ppc64_elf (ibfd))
11159 continue;
11160
11161 lgot_ents = elf_local_got_ents (ibfd);
11162 if (!lgot_ents)
11163 continue;
11164
11165 symtab_hdr = &elf_symtab_hdr (ibfd);
11166 locsymcount = symtab_hdr->sh_info;
11167 end_lgot_ents = lgot_ents + locsymcount;
11168 local_plt = (struct plt_entry **) end_lgot_ents;
11169 end_local_plt = local_plt + locsymcount;
f961d9dd 11170 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11171 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11172 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11173 {
11174 struct got_entry *ent;
11175
11176 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11177 {
19e08130
AM
11178 unsigned int ent_size = 8;
11179 unsigned int rel_size = sizeof (Elf64_External_Rela);
11180
d0fae19d
AM
11181 ent->got.offset = s->size;
11182 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11183 {
19e08130
AM
11184 ent_size *= 2;
11185 rel_size *= 2;
11186 }
11187 s->size += ent_size;
11188 if ((*lgot_masks & PLT_IFUNC) != 0)
11189 {
11190 htab->reliplt->size += rel_size;
11191 htab->got_reli_size += rel_size;
11192 }
11193 else if (info->shared)
11194 {
11195 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11196 srel->size += rel_size;
d0fae19d
AM
11197 }
11198 }
927be08e
AM
11199 }
11200 }
11201
11202 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11203
11204 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11205 {
11206 struct got_entry *ent;
11207
11208 if (!is_ppc64_elf (ibfd))
11209 continue;
11210
11211 ent = ppc64_tlsld_got (ibfd);
11212 if (!ent->is_indirect
11213 && ent->got.offset != (bfd_vma) -1)
11214 {
11215 asection *s = ppc64_elf_tdata (ibfd)->got;
11216 ent->got.offset = s->size;
11217 s->size += 16;
11218 if (info->shared)
11219 {
11220 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11221 srel->size += sizeof (Elf64_External_Rela);
11222 }
11223 }
11224 }
11225
11226 done_something = htab->reliplt->rawsize != htab->reliplt->size;
11227 if (!done_something)
11228 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11229 {
11230 asection *got;
11231
11232 if (!is_ppc64_elf (ibfd))
11233 continue;
11234
11235 got = ppc64_elf_tdata (ibfd)->got;
11236 if (got != NULL)
11237 {
11238 done_something = got->rawsize != got->size;
11239 if (done_something)
11240 break;
11241 }
11242 }
11243
11244 if (done_something)
11245 (*htab->layout_sections_again) ();
11246
11247 /* Set up for second pass over toc sections to recalculate elf_gp
11248 on input sections. */
11249 htab->toc_bfd = NULL;
11250 htab->toc_first_sec = NULL;
11251 htab->second_toc_pass = TRUE;
11252 return done_something;
11253}
11254
11255/* Called after second pass of multitoc partitioning. */
11256
11257void
11258ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11259{
11260 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11261
11262 /* After the second pass, toc_curr tracks the TOC offset used
11263 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11264 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11265}
11266
9b5ecbd0
AM
11267/* No toc references were found in ISEC. If the code in ISEC makes no
11268 calls, then there's no need to use toc adjusting stubs when branching
11269 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11270 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11271 needed, and 2 if a cyclical call-graph was found but no other reason
11272 for a stub was detected. If called from the top level, a return of
11273 2 means the same as a return of 0. */
9b5ecbd0
AM
11274
11275static int
4ce794b7 11276toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11277{
9b5ecbd0 11278 int ret;
70cc837d
AM
11279
11280 /* Mark this section as checked. */
11281 isec->call_check_done = 1;
9b5ecbd0 11282
772119ce
AM
11283 /* We know none of our code bearing sections will need toc stubs. */
11284 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11285 return 0;
11286
eea6121a 11287 if (isec->size == 0)
082c50f8
AM
11288 return 0;
11289
4c52953f
AM
11290 if (isec->output_section == NULL)
11291 return 0;
11292
4c52953f 11293 ret = 0;
70cc837d 11294 if (isec->reloc_count != 0)
9b5ecbd0 11295 {
70cc837d
AM
11296 Elf_Internal_Rela *relstart, *rel;
11297 Elf_Internal_Sym *local_syms;
11298 struct ppc_link_hash_table *htab;
2917689a 11299
70cc837d
AM
11300 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11301 info->keep_memory);
11302 if (relstart == NULL)
11303 return -1;
90aecf7a 11304
70cc837d
AM
11305 /* Look for branches to outside of this section. */
11306 local_syms = NULL;
11307 htab = ppc_hash_table (info);
11308 if (htab == NULL)
11309 return -1;
4c52953f 11310
70cc837d 11311 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11312 {
70cc837d
AM
11313 enum elf_ppc64_reloc_type r_type;
11314 unsigned long r_symndx;
11315 struct elf_link_hash_entry *h;
11316 struct ppc_link_hash_entry *eh;
11317 Elf_Internal_Sym *sym;
11318 asection *sym_sec;
11319 struct _opd_sec_data *opd;
11320 bfd_vma sym_value;
11321 bfd_vma dest;
11322
11323 r_type = ELF64_R_TYPE (rel->r_info);
11324 if (r_type != R_PPC64_REL24
11325 && r_type != R_PPC64_REL14
11326 && r_type != R_PPC64_REL14_BRTAKEN
11327 && r_type != R_PPC64_REL14_BRNTAKEN)
11328 continue;
4c52953f 11329
70cc837d
AM
11330 r_symndx = ELF64_R_SYM (rel->r_info);
11331 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11332 isec->owner))
4c52953f 11333 {
70cc837d
AM
11334 ret = -1;
11335 break;
11336 }
4c52953f 11337
70cc837d
AM
11338 /* Calls to dynamic lib functions go through a plt call stub
11339 that uses r2. */
11340 eh = (struct ppc_link_hash_entry *) h;
11341 if (eh != NULL
11342 && (eh->elf.plt.plist != NULL
11343 || (eh->oh != NULL
11344 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11345 {
11346 ret = 1;
11347 break;
4c52953f
AM
11348 }
11349
70cc837d
AM
11350 if (sym_sec == NULL)
11351 /* Ignore other undefined symbols. */
4c52953f 11352 continue;
4c52953f 11353
70cc837d
AM
11354 /* Assume branches to other sections not included in the
11355 link need stubs too, to cover -R and absolute syms. */
11356 if (sym_sec->output_section == NULL)
11357 {
11358 ret = 1;
11359 break;
11360 }
4c52953f 11361
70cc837d
AM
11362 if (h == NULL)
11363 sym_value = sym->st_value;
11364 else
11365 {
11366 if (h->root.type != bfd_link_hash_defined
11367 && h->root.type != bfd_link_hash_defweak)
11368 abort ();
11369 sym_value = h->root.u.def.value;
11370 }
11371 sym_value += rel->r_addend;
4c52953f 11372
70cc837d
AM
11373 /* If this branch reloc uses an opd sym, find the code section. */
11374 opd = get_opd_info (sym_sec);
11375 if (opd != NULL)
11376 {
11377 if (h == NULL && opd->adjust != NULL)
11378 {
11379 long adjust;
4c52953f 11380
70cc837d
AM
11381 adjust = opd->adjust[sym->st_value / 8];
11382 if (adjust == -1)
11383 /* Assume deleted functions won't ever be called. */
11384 continue;
11385 sym_value += adjust;
11386 }
4c52953f 11387
aef36ac1
AM
11388 dest = opd_entry_value (sym_sec, sym_value,
11389 &sym_sec, NULL, FALSE);
70cc837d
AM
11390 if (dest == (bfd_vma) -1)
11391 continue;
11392 }
11393 else
11394 dest = (sym_value
11395 + sym_sec->output_offset
11396 + sym_sec->output_section->vma);
4c52953f 11397
70cc837d
AM
11398 /* Ignore branch to self. */
11399 if (sym_sec == isec)
11400 continue;
4c52953f 11401
70cc837d
AM
11402 /* If the called function uses the toc, we need a stub. */
11403 if (sym_sec->has_toc_reloc
11404 || sym_sec->makes_toc_func_call)
4c52953f 11405 {
70cc837d 11406 ret = 1;
4c52953f
AM
11407 break;
11408 }
70cc837d
AM
11409
11410 /* Assume any branch that needs a long branch stub might in fact
11411 need a plt_branch stub. A plt_branch stub uses r2. */
11412 else if (dest - (isec->output_offset
11413 + isec->output_section->vma
6911b7dc
AM
11414 + rel->r_offset) + (1 << 25)
11415 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11416 ? h->other
11417 : sym->st_other))
4c52953f 11418 {
70cc837d
AM
11419 ret = 1;
11420 break;
11421 }
11422
11423 /* If calling back to a section in the process of being
11424 tested, we can't say for sure that no toc adjusting stubs
11425 are needed, so don't return zero. */
11426 else if (sym_sec->call_check_in_progress)
11427 ret = 2;
11428
11429 /* Branches to another section that itself doesn't have any TOC
11430 references are OK. Recursively call ourselves to check. */
11431 else if (!sym_sec->call_check_done)
11432 {
11433 int recur;
11434
11435 /* Mark current section as indeterminate, so that other
11436 sections that call back to current won't be marked as
11437 known. */
11438 isec->call_check_in_progress = 1;
11439 recur = toc_adjusting_stub_needed (info, sym_sec);
11440 isec->call_check_in_progress = 0;
11441
4c52953f
AM
11442 if (recur != 0)
11443 {
70cc837d
AM
11444 ret = recur;
11445 if (recur != 2)
11446 break;
4c52953f
AM
11447 }
11448 }
4c52953f 11449 }
70cc837d
AM
11450
11451 if (local_syms != NULL
11452 && (elf_symtab_hdr (isec->owner).contents
11453 != (unsigned char *) local_syms))
11454 free (local_syms);
11455 if (elf_section_data (isec)->relocs != relstart)
11456 free (relstart);
9b5ecbd0
AM
11457 }
11458
70cc837d
AM
11459 if ((ret & 1) == 0
11460 && isec->map_head.s != NULL
11461 && (strcmp (isec->output_section->name, ".init") == 0
11462 || strcmp (isec->output_section->name, ".fini") == 0))
11463 {
11464 if (isec->map_head.s->has_toc_reloc
11465 || isec->map_head.s->makes_toc_func_call)
11466 ret = 1;
11467 else if (!isec->map_head.s->call_check_done)
11468 {
11469 int recur;
11470 isec->call_check_in_progress = 1;
11471 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11472 isec->call_check_in_progress = 0;
11473 if (recur != 0)
11474 ret = recur;
11475 }
11476 }
11477
11478 if (ret == 1)
11479 isec->makes_toc_func_call = 1;
4c52953f 11480
9b5ecbd0
AM
11481 return ret;
11482}
11483
721956f4
AM
11484/* The linker repeatedly calls this function for each input section,
11485 in the order that input sections are linked into output sections.
11486 Build lists of input sections to determine groupings between which
11487 we may insert linker stubs. */
11488
9b5ecbd0 11489bfd_boolean
4ce794b7 11490ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11491{
11492 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11493
4dfe6ac6
NC
11494 if (htab == NULL)
11495 return FALSE;
11496
734b6cf9
AM
11497 if ((isec->output_section->flags & SEC_CODE) != 0
11498 && isec->output_section->index <= htab->top_index)
721956f4 11499 {
734b6cf9 11500 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 11501 /* Steal the link_sec pointer for our list. */
721956f4 11502#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
11503 /* This happens to make the list in reverse order,
11504 which is what we want. */
734b6cf9
AM
11505 PREV_SEC (isec) = *list;
11506 *list = isec;
721956f4 11507 }
ad8e1ba5 11508
4c52953f 11509 if (htab->multi_toc_needed)
9b5ecbd0 11510 {
4c52953f
AM
11511 /* If a code section has a function that uses the TOC then we need
11512 to use the right TOC (obviously). Also, make sure that .opd gets
11513 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
11514 can't find their function symbol (shouldn't ever happen now).
11515 Also specially treat .fixup for the linux kernel. .fixup
11516 contains branches, but only back to the function that hit an
11517 exception. */
11518 if (isec->has_toc_reloc
11519 || (isec->flags & SEC_CODE) == 0
11520 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
11521 {
11522 if (elf_gp (isec->owner) != 0)
11523 htab->toc_curr = elf_gp (isec->owner);
11524 }
6683a28d
AM
11525 else
11526 {
11527 if (!isec->call_check_done
11528 && toc_adjusting_stub_needed (info, isec) < 0)
11529 return FALSE;
11530 /* If we make a local call from this section, ie. a branch
11531 without a following nop, then we have no place to put a
11532 toc restoring insn. We must use the same toc group as
11533 the callee.
11534 Testing makes_toc_func_call actually tests for *any*
11535 calls to functions that need a good toc pointer. A more
11536 precise test would be better, as this one will set
11537 incorrect values for pasted .init/.fini fragments.
11538 (Fixed later in check_pasted_section.) */
11539 if (isec->makes_toc_func_call
11540 && elf_gp (isec->owner) != 0)
11541 htab->toc_curr = elf_gp (isec->owner);
11542 }
9b5ecbd0 11543 }
ad8e1ba5
AM
11544
11545 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 11546 Use the last TOC base. */
ad8e1ba5 11547 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11548 return TRUE;
721956f4
AM
11549}
11550
70cc837d
AM
11551/* Check that all .init and .fini sections use the same toc, if they
11552 have toc relocs. */
11553
11554static bfd_boolean
11555check_pasted_section (struct bfd_link_info *info, const char *name)
11556{
11557 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11558
11559 if (o != NULL)
11560 {
11561 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11562 bfd_vma toc_off = 0;
11563 asection *i;
11564
11565 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11566 if (i->has_toc_reloc)
11567 {
11568 if (toc_off == 0)
11569 toc_off = htab->stub_group[i->id].toc_off;
11570 else if (toc_off != htab->stub_group[i->id].toc_off)
11571 return FALSE;
11572 }
6683a28d
AM
11573
11574 if (toc_off == 0)
11575 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11576 if (i->makes_toc_func_call)
11577 {
11578 toc_off = htab->stub_group[i->id].toc_off;
11579 break;
11580 }
11581
70cc837d
AM
11582 /* Make sure the whole pasted function uses the same toc offset. */
11583 if (toc_off != 0)
11584 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11585 htab->stub_group[i->id].toc_off = toc_off;
11586 }
11587 return TRUE;
11588}
11589
11590bfd_boolean
11591ppc64_elf_check_init_fini (struct bfd_link_info *info)
11592{
11593 return (check_pasted_section (info, ".init")
11594 & check_pasted_section (info, ".fini"));
11595}
11596
721956f4
AM
11597/* See whether we can group stub sections together. Grouping stub
11598 sections may result in fewer stubs. More importantly, we need to
11599 put all .init* and .fini* stubs at the beginning of the .init or
11600 .fini output sections respectively, because glibc splits the
11601 _init and _fini functions into multiple parts. Putting a stub in
11602 the middle of a function is not a good idea. */
11603
11604static void
4ce794b7
AM
11605group_sections (struct ppc_link_hash_table *htab,
11606 bfd_size_type stub_group_size,
11607 bfd_boolean stubs_always_before_branch)
721956f4 11608{
7c8fe5c4
AM
11609 asection **list;
11610 bfd_size_type stub14_group_size;
11611 bfd_boolean suppress_size_errors;
11612
11613 suppress_size_errors = FALSE;
11614 stub14_group_size = stub_group_size;
11615 if (stub_group_size == 1)
11616 {
11617 /* Default values. */
11618 if (stubs_always_before_branch)
11619 {
11620 stub_group_size = 0x1e00000;
11621 stub14_group_size = 0x7800;
11622 }
11623 else
11624 {
11625 stub_group_size = 0x1c00000;
11626 stub14_group_size = 0x7000;
11627 }
11628 suppress_size_errors = TRUE;
11629 }
11630
11631 list = htab->input_list + htab->top_index;
734b6cf9 11632 do
721956f4 11633 {
734b6cf9
AM
11634 asection *tail = *list;
11635 while (tail != NULL)
721956f4 11636 {
734b6cf9
AM
11637 asection *curr;
11638 asection *prev;
11639 bfd_size_type total;
11640 bfd_boolean big_sec;
11641 bfd_vma curr_toc;
11642
11643 curr = tail;
eea6121a 11644 total = tail->size;
6bee8834
AM
11645 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11646 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
11647 ? stub14_group_size : stub_group_size);
11648 if (big_sec && !suppress_size_errors)
5c3dead3
AM
11649 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11650 tail->owner, tail);
734b6cf9
AM
11651 curr_toc = htab->stub_group[tail->id].toc_off;
11652
11653 while ((prev = PREV_SEC (curr)) != NULL
11654 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11655 < (ppc64_elf_section_data (prev) != NULL
11656 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11657 ? stub14_group_size : stub_group_size))
ad8e1ba5 11658 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
11659 curr = prev;
11660
11661 /* OK, the size from the start of CURR to the end is less
11662 than stub_group_size and thus can be handled by one stub
11663 section. (or the tail section is itself larger than
11664 stub_group_size, in which case we may be toast.) We
11665 should really be keeping track of the total size of stubs
11666 added here, as stubs contribute to the final output
11667 section size. That's a little tricky, and this way will
11668 only break if stubs added make the total size more than
11669 2^25, ie. for the default stub_group_size, if stubs total
11670 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11671 do
721956f4
AM
11672 {
11673 prev = PREV_SEC (tail);
734b6cf9 11674 /* Set up this stub group. */
721956f4
AM
11675 htab->stub_group[tail->id].link_sec = curr;
11676 }
734b6cf9
AM
11677 while (tail != curr && (tail = prev) != NULL);
11678
11679 /* But wait, there's more! Input sections up to stub_group_size
11680 bytes before the stub section can be handled by it too.
11681 Don't do this if we have a really large section after the
11682 stubs, as adding more stubs increases the chance that
11683 branches may not reach into the stub section. */
11684 if (!stubs_always_before_branch && !big_sec)
11685 {
11686 total = 0;
11687 while (prev != NULL
11688 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11689 < (ppc64_elf_section_data (prev) != NULL
11690 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11691 ? stub14_group_size : stub_group_size))
734b6cf9
AM
11692 && htab->stub_group[prev->id].toc_off == curr_toc)
11693 {
11694 tail = prev;
11695 prev = PREV_SEC (tail);
11696 htab->stub_group[tail->id].link_sec = curr;
11697 }
11698 }
11699 tail = prev;
721956f4
AM
11700 }
11701 }
734b6cf9
AM
11702 while (list-- != htab->input_list);
11703 free (htab->input_list);
721956f4
AM
11704#undef PREV_SEC
11705}
11706
58d180e8
AM
11707static const unsigned char glink_eh_frame_cie[] =
11708{
11709 0, 0, 0, 16, /* length. */
11710 0, 0, 0, 0, /* id. */
11711 1, /* CIE version. */
11712 'z', 'R', 0, /* Augmentation string. */
11713 4, /* Code alignment. */
11714 0x78, /* Data alignment. */
11715 65, /* RA reg. */
11716 1, /* Augmentation size. */
11717 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11718 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11719};
11720
d969d15f
AM
11721/* Stripping output sections is normally done before dynamic section
11722 symbols have been allocated. This function is called later, and
11723 handles cases like htab->brlt which is mapped to its own output
11724 section. */
11725
11726static void
11727maybe_strip_output (struct bfd_link_info *info, asection *isec)
11728{
11729 if (isec->size == 0
11730 && isec->output_section->size == 0
53d8967a 11731 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
11732 && !bfd_section_removed_from_list (info->output_bfd,
11733 isec->output_section)
11734 && elf_section_data (isec->output_section)->dynindx == 0)
11735 {
11736 isec->output_section->flags |= SEC_EXCLUDE;
11737 bfd_section_list_remove (info->output_bfd, isec->output_section);
11738 info->output_bfd->section_count--;
11739 }
11740}
11741
721956f4
AM
11742/* Determine and set the size of the stub section for a final link.
11743
11744 The basic idea here is to examine all the relocations looking for
11745 PC-relative calls to a target that is unreachable with a "bl"
11746 instruction. */
11747
b34976b6 11748bfd_boolean
9df0ef5f 11749ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
794e51c0
AM
11750 bfd_boolean plt_static_chain, int plt_thread_safe,
11751 int plt_stub_align)
721956f4
AM
11752{
11753 bfd_size_type stub_group_size;
b34976b6 11754 bfd_boolean stubs_always_before_branch;
721956f4
AM
11755 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11756
4dfe6ac6
NC
11757 if (htab == NULL)
11758 return FALSE;
11759
9df0ef5f 11760 htab->plt_static_chain = plt_static_chain;
794e51c0 11761 htab->plt_stub_align = plt_stub_align;
e2458743
AM
11762 if (plt_thread_safe == -1 && !info->executable)
11763 plt_thread_safe = 1;
b9e5796b
AM
11764 if (!htab->opd_abi)
11765 plt_thread_safe = 0;
11766 else if (plt_thread_safe == -1)
794e51c0 11767 {
e2458743 11768 static const char *const thread_starter[] =
794e51c0
AM
11769 {
11770 "pthread_create",
11771 /* libstdc++ */
11772 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11773 /* librt */
11774 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11775 "mq_notify", "create_timer",
11776 /* libanl */
11777 "getaddrinfo_a",
11778 /* libgomp */
11779 "GOMP_parallel_start",
11780 "GOMP_parallel_loop_static_start",
11781 "GOMP_parallel_loop_dynamic_start",
11782 "GOMP_parallel_loop_guided_start",
11783 "GOMP_parallel_loop_runtime_start",
68ffbac6 11784 "GOMP_parallel_sections_start",
794e51c0
AM
11785 };
11786 unsigned i;
11787
11788 for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11789 {
11790 struct elf_link_hash_entry *h;
11791 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11792 FALSE, FALSE, TRUE);
11793 plt_thread_safe = h != NULL && h->ref_regular;
11794 if (plt_thread_safe)
11795 break;
11796 }
11797 }
11798 htab->plt_thread_safe = plt_thread_safe;
721956f4
AM
11799 stubs_always_before_branch = group_size < 0;
11800 if (group_size < 0)
11801 stub_group_size = -group_size;
11802 else
11803 stub_group_size = group_size;
721956f4
AM
11804
11805 group_sections (htab, stub_group_size, stubs_always_before_branch);
11806
721956f4
AM
11807 while (1)
11808 {
11809 bfd *input_bfd;
11810 unsigned int bfd_indx;
11811 asection *stub_sec;
721956f4
AM
11812
11813 htab->stub_iteration += 1;
721956f4
AM
11814
11815 for (input_bfd = info->input_bfds, bfd_indx = 0;
11816 input_bfd != NULL;
11817 input_bfd = input_bfd->link_next, bfd_indx++)
11818 {
11819 Elf_Internal_Shdr *symtab_hdr;
11820 asection *section;
6cdc0ccc 11821 Elf_Internal_Sym *local_syms = NULL;
721956f4 11822
0c8d6e5c 11823 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11824 continue;
11825
721956f4 11826 /* We'll need the symbol table in a second. */
0ffa91dd 11827 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11828 if (symtab_hdr->sh_info == 0)
11829 continue;
11830
721956f4
AM
11831 /* Walk over each section attached to the input bfd. */
11832 for (section = input_bfd->sections;
11833 section != NULL;
11834 section = section->next)
11835 {
721956f4 11836 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11837
11838 /* If there aren't any relocs, then there's nothing more
11839 to do. */
11840 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11841 || (section->flags & SEC_ALLOC) == 0
11842 || (section->flags & SEC_LOAD) == 0
11843 || (section->flags & SEC_CODE) == 0
721956f4
AM
11844 || section->reloc_count == 0)
11845 continue;
11846
11847 /* If this section is a link-once section that will be
11848 discarded, then don't create any stubs. */
11849 if (section->output_section == NULL
927be08e 11850 || section->output_section->owner != info->output_bfd)
721956f4
AM
11851 continue;
11852
1e2f5b6e
AM
11853 /* Get the relocs. */
11854 internal_relocs
4ce794b7 11855 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 11856 info->keep_memory);
721956f4 11857 if (internal_relocs == NULL)
1e2f5b6e 11858 goto error_ret_free_local;
721956f4
AM
11859
11860 /* Now examine each relocation. */
11861 irela = internal_relocs;
11862 irelaend = irela + section->reloc_count;
11863 for (; irela < irelaend; irela++)
11864 {
4ce794b7
AM
11865 enum elf_ppc64_reloc_type r_type;
11866 unsigned int r_indx;
721956f4
AM
11867 enum ppc_stub_type stub_type;
11868 struct ppc_stub_hash_entry *stub_entry;
8387904d 11869 asection *sym_sec, *code_sec;
e054468f 11870 bfd_vma sym_value, code_value;
721956f4 11871 bfd_vma destination;
6911b7dc 11872 unsigned long local_off;
8843416a 11873 bfd_boolean ok_dest;
721956f4 11874 struct ppc_link_hash_entry *hash;
8387904d 11875 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
11876 struct elf_link_hash_entry *h;
11877 Elf_Internal_Sym *sym;
721956f4
AM
11878 char *stub_name;
11879 const asection *id_sec;
74f0fb50 11880 struct _opd_sec_data *opd;
e054468f 11881 struct plt_entry *plt_ent;
721956f4
AM
11882
11883 r_type = ELF64_R_TYPE (irela->r_info);
11884 r_indx = ELF64_R_SYM (irela->r_info);
11885
4ce794b7 11886 if (r_type >= R_PPC64_max)
721956f4
AM
11887 {
11888 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 11889 goto error_ret_free_internal;
721956f4
AM
11890 }
11891
11892 /* Only look for stubs on branch instructions. */
4ce794b7
AM
11893 if (r_type != R_PPC64_REL24
11894 && r_type != R_PPC64_REL14
11895 && r_type != R_PPC64_REL14_BRTAKEN
11896 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
11897 continue;
11898
11899 /* Now determine the call target, its name, value,
11900 section. */
411e1bfb
AM
11901 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11902 r_indx, input_bfd))
11903 goto error_ret_free_internal;
11904 hash = (struct ppc_link_hash_entry *) h;
11905
8843416a 11906 ok_dest = FALSE;
8387904d 11907 fdh = NULL;
7fe2b9a6 11908 sym_value = 0;
411e1bfb 11909 if (hash == NULL)
721956f4 11910 {
411e1bfb 11911 sym_value = sym->st_value;
8843416a 11912 ok_dest = TRUE;
721956f4 11913 }
7fe2b9a6
AM
11914 else if (hash->elf.root.type == bfd_link_hash_defined
11915 || hash->elf.root.type == bfd_link_hash_defweak)
11916 {
11917 sym_value = hash->elf.root.u.def.value;
11918 if (sym_sec->output_section != NULL)
11919 ok_dest = TRUE;
11920 }
11921 else if (hash->elf.root.type == bfd_link_hash_undefweak
11922 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11923 {
99877b66 11924 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11925 use the func descriptor sym instead if it is
11926 defined. */
ceb1f1ef 11927 if (hash->elf.root.root.string[0] == '.'
b31867b6 11928 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 11929 {
8387904d
AM
11930 if (fdh->elf.root.type == bfd_link_hash_defined
11931 || fdh->elf.root.type == bfd_link_hash_defweak)
11932 {
11933 sym_sec = fdh->elf.root.u.def.section;
11934 sym_value = fdh->elf.root.u.def.value;
11935 if (sym_sec->output_section != NULL)
11936 ok_dest = TRUE;
11937 }
99877b66
AM
11938 else
11939 fdh = NULL;
8387904d 11940 }
7fe2b9a6
AM
11941 }
11942 else
11943 {
11944 bfd_set_error (bfd_error_bad_value);
11945 goto error_ret_free_internal;
721956f4
AM
11946 }
11947
8843416a 11948 destination = 0;
6911b7dc 11949 local_off = 0;
8843416a
AM
11950 if (ok_dest)
11951 {
11952 sym_value += irela->r_addend;
11953 destination = (sym_value
11954 + sym_sec->output_offset
11955 + sym_sec->output_section->vma);
6911b7dc
AM
11956 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11957 ? hash->elf.other
11958 : sym->st_other);
8843416a
AM
11959 }
11960
8387904d 11961 code_sec = sym_sec;
e054468f 11962 code_value = sym_value;
74f0fb50
AM
11963 opd = get_opd_info (sym_sec);
11964 if (opd != NULL)
8387904d
AM
11965 {
11966 bfd_vma dest;
11967
74f0fb50 11968 if (hash == NULL && opd->adjust != NULL)
8387904d 11969 {
74f0fb50 11970 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11971 if (adjust == -1)
11972 continue;
e054468f 11973 code_value += adjust;
8387904d
AM
11974 sym_value += adjust;
11975 }
11976 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 11977 &code_sec, &code_value, FALSE);
8387904d
AM
11978 if (dest != (bfd_vma) -1)
11979 {
11980 destination = dest;
11981 if (fdh != NULL)
11982 {
11983 /* Fixup old ABI sym to point at code
11984 entry. */
99877b66 11985 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11986 hash->elf.root.u.def.section = code_sec;
e054468f 11987 hash->elf.root.u.def.value = code_value;
8387904d
AM
11988 }
11989 }
11990 }
11991
721956f4 11992 /* Determine what (if any) linker stub is needed. */
e054468f 11993 plt_ent = NULL;
721956f4 11994 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
11995 &plt_ent, destination,
11996 local_off);
ad8e1ba5
AM
11997
11998 if (stub_type != ppc_stub_plt_call)
11999 {
12000 /* Check whether we need a TOC adjusting stub.
12001 Since the linker pastes together pieces from
12002 different object files when creating the
12003 _init and _fini functions, it may be that a
12004 call to what looks like a local sym is in
12005 fact a call needing a TOC adjustment. */
8387904d
AM
12006 if (code_sec != NULL
12007 && code_sec->output_section != NULL
12008 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 12009 != htab->stub_group[section->id].toc_off)
4c52953f
AM
12010 && (code_sec->has_toc_reloc
12011 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12012 stub_type = ppc_stub_long_branch_r2off;
12013 }
12014
721956f4
AM
12015 if (stub_type == ppc_stub_none)
12016 continue;
12017
411e1bfb
AM
12018 /* __tls_get_addr calls might be eliminated. */
12019 if (stub_type != ppc_stub_plt_call
12020 && hash != NULL
8387904d
AM
12021 && (hash == htab->tls_get_addr
12022 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12023 && section->has_tls_reloc
12024 && irela != internal_relocs)
12025 {
12026 /* Get tls info. */
f961d9dd 12027 unsigned char *tls_mask;
411e1bfb 12028
3a71aa26 12029 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12030 irela - 1, input_bfd))
12031 goto error_ret_free_internal;
e7b938ca 12032 if (*tls_mask != 0)
411e1bfb
AM
12033 continue;
12034 }
12035
3b421ab3
AM
12036 if (stub_type == ppc_stub_plt_call
12037 && irela + 1 < irelaend
12038 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12039 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12040 {
12041 if (!tocsave_find (htab, INSERT,
12042 &local_syms, irela + 1, input_bfd))
12043 goto error_ret_free_internal;
12044 }
12045 else if (stub_type == ppc_stub_plt_call)
12046 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12047
721956f4
AM
12048 /* Support for grouping stub sections. */
12049 id_sec = htab->stub_group[section->id].link_sec;
12050
12051 /* Get the name of this stub. */
12052 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12053 if (!stub_name)
12054 goto error_ret_free_internal;
12055
12056 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12057 stub_name, FALSE, FALSE);
721956f4
AM
12058 if (stub_entry != NULL)
12059 {
12060 /* The proper stub has already been created. */
12061 free (stub_name);
794e51c0
AM
12062 if (stub_type == ppc_stub_plt_call_r2save)
12063 stub_entry->stub_type = stub_type;
721956f4
AM
12064 continue;
12065 }
12066
25f53a85 12067 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12068 if (stub_entry == NULL)
12069 {
12070 free (stub_name);
6cdc0ccc
AM
12071 error_ret_free_internal:
12072 if (elf_section_data (section)->relocs == NULL)
12073 free (internal_relocs);
12074 error_ret_free_local:
12075 if (local_syms != NULL
12076 && (symtab_hdr->contents
12077 != (unsigned char *) local_syms))
12078 free (local_syms);
b34976b6 12079 return FALSE;
721956f4
AM
12080 }
12081
ad8e1ba5 12082 stub_entry->stub_type = stub_type;
794e51c0
AM
12083 if (stub_type != ppc_stub_plt_call
12084 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12085 {
12086 stub_entry->target_value = code_value;
12087 stub_entry->target_section = code_sec;
12088 }
12089 else
12090 {
12091 stub_entry->target_value = sym_value;
12092 stub_entry->target_section = sym_sec;
12093 }
721956f4 12094 stub_entry->h = hash;
e054468f 12095 stub_entry->plt_ent = plt_ent;
6911b7dc 12096 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12097
12098 if (stub_entry->h != NULL)
12099 htab->stub_globals += 1;
721956f4
AM
12100 }
12101
12102 /* We're done with the internal relocs, free them. */
6cdc0ccc 12103 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12104 free (internal_relocs);
721956f4 12105 }
6cdc0ccc
AM
12106
12107 if (local_syms != NULL
12108 && symtab_hdr->contents != (unsigned char *) local_syms)
12109 {
12110 if (!info->keep_memory)
12111 free (local_syms);
12112 else
12113 symtab_hdr->contents = (unsigned char *) local_syms;
12114 }
721956f4
AM
12115 }
12116
5c3dead3 12117 /* We may have added some stubs. Find out the new size of the
721956f4
AM
12118 stub sections. */
12119 for (stub_sec = htab->stub_bfd->sections;
12120 stub_sec != NULL;
12121 stub_sec = stub_sec->next)
e717da7e 12122 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12123 {
5c3dead3 12124 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12125 stub_sec->size = 0;
12126 stub_sec->reloc_count = 0;
84f5d08e 12127 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12128 }
eea6121a
AM
12129
12130 htab->brlt->size = 0;
84f5d08e
AM
12131 htab->brlt->reloc_count = 0;
12132 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12133 if (htab->relbrlt != NULL)
eea6121a 12134 htab->relbrlt->size = 0;
721956f4 12135
63bc6f6c 12136 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12137
176a0d42
AM
12138 if (info->emitrelocations
12139 && htab->glink != NULL && htab->glink->size != 0)
12140 {
12141 htab->glink->reloc_count = 1;
12142 htab->glink->flags |= SEC_RELOC;
12143 }
12144
58d180e8
AM
12145 if (htab->glink_eh_frame != NULL
12146 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12147 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12148 {
4bbe044a 12149 size_t size = 0, align;
58d180e8
AM
12150
12151 for (stub_sec = htab->stub_bfd->sections;
12152 stub_sec != NULL;
12153 stub_sec = stub_sec->next)
12154 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12155 size += 20;
12156 if (htab->glink != NULL && htab->glink->size != 0)
12157 size += 24;
12158 if (size != 0)
12159 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12160 align = 1;
12161 align <<= htab->glink_eh_frame->output_section->alignment_power;
12162 align -= 1;
12163 size = (size + align) & ~align;
58d180e8
AM
12164 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12165 htab->glink_eh_frame->size = size;
12166 }
12167
794e51c0
AM
12168 if (htab->plt_stub_align != 0)
12169 for (stub_sec = htab->stub_bfd->sections;
12170 stub_sec != NULL;
12171 stub_sec = stub_sec->next)
12172 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12173 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12174 & (-1 << htab->plt_stub_align));
12175
5c3dead3
AM
12176 for (stub_sec = htab->stub_bfd->sections;
12177 stub_sec != NULL;
12178 stub_sec = stub_sec->next)
12179 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12180 && stub_sec->rawsize != stub_sec->size)
12181 break;
12182
12183 /* Exit from this loop when no stubs have been added, and no stubs
12184 have changed size. */
58d180e8
AM
12185 if (stub_sec == NULL
12186 && (htab->glink_eh_frame == NULL
12187 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12188 break;
12189
721956f4
AM
12190 /* Ask the linker to do its stuff. */
12191 (*htab->layout_sections_again) ();
12192 }
12193
d969d15f
AM
12194 maybe_strip_output (info, htab->brlt);
12195 if (htab->glink_eh_frame != NULL)
12196 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12197
b34976b6 12198 return TRUE;
721956f4
AM
12199}
12200
12201/* Called after we have determined section placement. If sections
805fc799 12202 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12203
805fc799 12204bfd_vma
1c865ab2 12205ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12206{
805fc799
AM
12207 asection *s;
12208 bfd_vma TOCstart;
721956f4 12209
805fc799
AM
12210 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12211 order. The TOC starts where the first of these sections starts. */
12212 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12213 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12214 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12215 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12216 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12217 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12218 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12219 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12220 {
12221 /* This may happen for
12222 o references to TOC base (SYM@toc / TOC[tc0]) without a
12223 .toc directive
12224 o bad linker script
12225 o --gc-sections and empty TOC sections
12226
12227 FIXME: Warn user? */
12228
12229 /* Look for a likely section. We probably won't even be
12230 using TOCstart. */
12231 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12232 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12233 | SEC_EXCLUDE))
805fc799
AM
12234 == (SEC_ALLOC | SEC_SMALL_DATA))
12235 break;
721956f4 12236 if (s == NULL)
805fc799 12237 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12238 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12239 == (SEC_ALLOC | SEC_SMALL_DATA))
12240 break;
721956f4 12241 if (s == NULL)
805fc799 12242 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12243 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12244 == SEC_ALLOC)
805fc799 12245 break;
721956f4 12246 if (s == NULL)
805fc799 12247 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12248 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12249 break;
12250 }
721956f4 12251
805fc799
AM
12252 TOCstart = 0;
12253 if (s != NULL)
12254 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12255
1c865ab2
AM
12256 _bfd_set_gp_value (obfd, TOCstart);
12257
12258 if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12259 {
12260 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12261
12262 if (htab != NULL
12263 && htab->elf.hgot != NULL)
12264 {
5295321c 12265 htab->elf.hgot->type = STT_OBJECT;
1c865ab2
AM
12266 htab->elf.hgot->root.type = bfd_link_hash_defined;
12267 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12268 htab->elf.hgot->root.u.def.section = s;
12269 }
12270 }
805fc799 12271 return TOCstart;
721956f4
AM
12272}
12273
12274/* Build all the stubs associated with the current output file.
12275 The stubs are kept in a hash table attached to the main linker
12276 hash table. This function is called via gldelf64ppc_finish. */
12277
b34976b6 12278bfd_boolean
4ce794b7
AM
12279ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12280 struct bfd_link_info *info,
12281 char **stats)
5d1634d7
AM
12282{
12283 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 12284 asection *stub_sec;
5d1634d7 12285 bfd_byte *p;
e717da7e 12286 int stub_sec_count = 0;
5d1634d7 12287
4dfe6ac6
NC
12288 if (htab == NULL)
12289 return FALSE;
12290
ad8e1ba5 12291 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
12292
12293 /* Allocate memory to hold the linker stubs. */
721956f4
AM
12294 for (stub_sec = htab->stub_bfd->sections;
12295 stub_sec != NULL;
12296 stub_sec = stub_sec->next)
eea6121a
AM
12297 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12298 && stub_sec->size != 0)
e717da7e 12299 {
eea6121a
AM
12300 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12301 if (stub_sec->contents == NULL)
12302 return FALSE;
12303 /* We want to check that built size is the same as calculated
12304 size. rawsize is a convenient location to use. */
12305 stub_sec->rawsize = stub_sec->size;
12306 stub_sec->size = 0;
e717da7e 12307 }
5d1634d7 12308
23eb7e01 12309 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12310 {
9f951329 12311 unsigned int indx;
ad8e1ba5 12312 bfd_vma plt0;
9f951329 12313
721956f4 12314 /* Build the .glink plt call stub. */
97b639ba
AM
12315 if (htab->emit_stub_syms)
12316 {
12317 struct elf_link_hash_entry *h;
468392fb
AM
12318 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12319 TRUE, FALSE, FALSE);
97b639ba
AM
12320 if (h == NULL)
12321 return FALSE;
12322 if (h->root.type == bfd_link_hash_new)
12323 {
12324 h->root.type = bfd_link_hash_defined;
12325 h->root.u.def.section = htab->glink;
ee4bf8d2 12326 h->root.u.def.value = 8;
f5385ebf
AM
12327 h->ref_regular = 1;
12328 h->def_regular = 1;
12329 h->ref_regular_nonweak = 1;
12330 h->forced_local = 1;
12331 h->non_elf = 0;
97b639ba
AM
12332 }
12333 }
176a0d42
AM
12334 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12335 if (info->emitrelocations)
12336 {
12337 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12338 if (r == NULL)
12339 return FALSE;
12340 r->r_offset = (htab->glink->output_offset
12341 + htab->glink->output_section->vma);
12342 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12343 r->r_addend = plt0;
12344 }
4ce794b7 12345 p = htab->glink->contents;
176a0d42 12346 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
12347 bfd_put_64 (htab->glink->owner, plt0, p);
12348 p += 8;
b9e5796b
AM
12349 if (htab->opd_abi)
12350 {
12351 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12352 p += 4;
12353 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12354 p += 4;
12355 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12356 p += 4;
12357 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12358 p += 4;
12359 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12360 p += 4;
12361 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12362 p += 4;
12363 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12364 p += 4;
12365 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12366 p += 4;
12367 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12368 p += 4;
12369 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12370 p += 4;
12371 }
12372 else
12373 {
12374 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12375 p += 4;
12376 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12377 p += 4;
12378 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12379 p += 4;
12380 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12381 p += 4;
12382 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12383 p += 4;
12384 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12385 p += 4;
12386 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12387 p += 4;
12388 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12389 p += 4;
12390 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12391 p += 4;
12392 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12393 p += 4;
12394 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12395 p += 4;
12396 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12397 p += 4;
12398 }
4ce794b7 12399 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 12400 p += 4;
ee4bf8d2
AM
12401 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12402 {
12403 bfd_put_32 (htab->glink->owner, NOP, p);
12404 p += 4;
12405 }
ad8e1ba5 12406
9f951329
AM
12407 /* Build the .glink lazy link call stubs. */
12408 indx = 0;
eea6121a 12409 while (p < htab->glink->contents + htab->glink->size)
9f951329 12410 {
b9e5796b 12411 if (htab->opd_abi)
9f951329 12412 {
b9e5796b
AM
12413 if (indx < 0x8000)
12414 {
12415 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12416 p += 4;
12417 }
12418 else
12419 {
12420 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12421 p += 4;
12422 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12423 p);
12424 p += 4;
12425 }
9f951329 12426 }
4ce794b7 12427 bfd_put_32 (htab->glink->owner,
ee4bf8d2 12428 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 12429 indx++;
9f951329
AM
12430 p += 4;
12431 }
eea6121a 12432 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 12433 }
5d1634d7 12434
eea6121a 12435 if (htab->brlt->size != 0)
721956f4 12436 {
4ce794b7 12437 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 12438 htab->brlt->size);
4ce794b7 12439 if (htab->brlt->contents == NULL)
b34976b6 12440 return FALSE;
721956f4 12441 }
ee75fd95 12442 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
12443 {
12444 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 12445 htab->relbrlt->size);
63bc6f6c
AM
12446 if (htab->relbrlt->contents == NULL)
12447 return FALSE;
12448 }
5d1634d7 12449
58d180e8
AM
12450 if (htab->glink_eh_frame != NULL
12451 && htab->glink_eh_frame->size != 0)
12452 {
12453 bfd_vma val;
4bbe044a
AM
12454 bfd_byte *last_fde;
12455 size_t last_fde_len, size, align, pad;
58d180e8
AM
12456
12457 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12458 if (p == NULL)
12459 return FALSE;
12460 htab->glink_eh_frame->contents = p;
4bbe044a 12461 last_fde = p;
58d180e8
AM
12462
12463 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12464
12465 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12466 /* CIE length (rewrite in case little-endian). */
4bbe044a
AM
12467 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12468 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
58d180e8
AM
12469 p += sizeof (glink_eh_frame_cie);
12470
12471 for (stub_sec = htab->stub_bfd->sections;
12472 stub_sec != NULL;
12473 stub_sec = stub_sec->next)
12474 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12475 {
4bbe044a
AM
12476 last_fde = p;
12477 last_fde_len = 16;
58d180e8
AM
12478 /* FDE length. */
12479 bfd_put_32 (htab->elf.dynobj, 16, p);
12480 p += 4;
12481 /* CIE pointer. */
12482 val = p - htab->glink_eh_frame->contents;
12483 bfd_put_32 (htab->elf.dynobj, val, p);
12484 p += 4;
12485 /* Offset to stub section. */
12486 val = (stub_sec->output_section->vma
12487 + stub_sec->output_offset);
12488 val -= (htab->glink_eh_frame->output_section->vma
12489 + htab->glink_eh_frame->output_offset);
12490 val -= p - htab->glink_eh_frame->contents;
12491 if (val + 0x80000000 > 0xffffffff)
12492 {
12493 info->callbacks->einfo
8de848d8 12494 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
12495 stub_sec->name);
12496 return FALSE;
12497 }
12498 bfd_put_32 (htab->elf.dynobj, val, p);
12499 p += 4;
12500 /* stub section size. */
12501 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12502 p += 4;
12503 /* Augmentation. */
12504 p += 1;
12505 /* Pad. */
12506 p += 3;
12507 }
12508 if (htab->glink != NULL && htab->glink->size != 0)
12509 {
4bbe044a
AM
12510 last_fde = p;
12511 last_fde_len = 20;
58d180e8
AM
12512 /* FDE length. */
12513 bfd_put_32 (htab->elf.dynobj, 20, p);
12514 p += 4;
12515 /* CIE pointer. */
12516 val = p - htab->glink_eh_frame->contents;
12517 bfd_put_32 (htab->elf.dynobj, val, p);
12518 p += 4;
12519 /* Offset to .glink. */
12520 val = (htab->glink->output_section->vma
12521 + htab->glink->output_offset
12522 + 8);
12523 val -= (htab->glink_eh_frame->output_section->vma
12524 + htab->glink_eh_frame->output_offset);
12525 val -= p - htab->glink_eh_frame->contents;
12526 if (val + 0x80000000 > 0xffffffff)
12527 {
12528 info->callbacks->einfo
8de848d8 12529 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
12530 htab->glink->name);
12531 return FALSE;
12532 }
12533 bfd_put_32 (htab->elf.dynobj, val, p);
12534 p += 4;
12535 /* .glink size. */
12536 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12537 p += 4;
12538 /* Augmentation. */
12539 p += 1;
12540
12541 *p++ = DW_CFA_advance_loc + 1;
12542 *p++ = DW_CFA_register;
12543 *p++ = 65;
12544 *p++ = 12;
12545 *p++ = DW_CFA_advance_loc + 4;
12546 *p++ = DW_CFA_restore_extended;
12547 *p++ = 65;
12548 }
4bbe044a
AM
12549 /* Subsume any padding into the last FDE if user .eh_frame
12550 sections are aligned more than glink_eh_frame. Otherwise any
12551 zero padding will be seen as a terminator. */
12552 size = p - htab->glink_eh_frame->contents;
12553 align = 1;
12554 align <<= htab->glink_eh_frame->output_section->alignment_power;
12555 align -= 1;
12556 pad = ((size + align) & ~align) - size;
12557 htab->glink_eh_frame->size = size + pad;
12558 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
58d180e8
AM
12559 }
12560
721956f4
AM
12561 /* Build the stubs as directed by the stub hash table. */
12562 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 12563
aa8a7074
AM
12564 if (htab->relbrlt != NULL)
12565 htab->relbrlt->reloc_count = 0;
12566
794e51c0
AM
12567 if (htab->plt_stub_align != 0)
12568 for (stub_sec = htab->stub_bfd->sections;
12569 stub_sec != NULL;
12570 stub_sec = stub_sec->next)
12571 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12572 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12573 & (-1 << htab->plt_stub_align));
12574
721956f4
AM
12575 for (stub_sec = htab->stub_bfd->sections;
12576 stub_sec != NULL;
12577 stub_sec = stub_sec->next)
e717da7e
AM
12578 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12579 {
12580 stub_sec_count += 1;
eea6121a 12581 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
12582 break;
12583 }
5d1634d7 12584
721956f4 12585 if (stub_sec != NULL
58d180e8
AM
12586 || htab->glink->rawsize != htab->glink->size
12587 || (htab->glink_eh_frame != NULL
12588 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 12589 {
b34976b6 12590 htab->stub_error = TRUE;
8de848d8 12591 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 12592 }
721956f4 12593
d2a300cf
AM
12594 if (htab->stub_error)
12595 return FALSE;
12596
12597 if (stats != NULL)
12598 {
12599 *stats = bfd_malloc (500);
12600 if (*stats == NULL)
12601 return FALSE;
12602
ee75fd95 12603 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
12604 " branch %lu\n"
12605 " toc adjust %lu\n"
12606 " long branch %lu\n"
12607 " long toc adj %lu\n"
794e51c0
AM
12608 " plt call %lu\n"
12609 " plt call toc %lu"),
e717da7e 12610 stub_sec_count,
ee75fd95 12611 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
12612 htab->stub_count[ppc_stub_long_branch - 1],
12613 htab->stub_count[ppc_stub_long_branch_r2off - 1],
12614 htab->stub_count[ppc_stub_plt_branch - 1],
12615 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0
AM
12616 htab->stub_count[ppc_stub_plt_call - 1],
12617 htab->stub_count[ppc_stub_plt_call_r2save - 1]);
d2a300cf
AM
12618 }
12619 return TRUE;
5bd4f169
AM
12620}
12621
99877b66
AM
12622/* This function undoes the changes made by add_symbol_adjust. */
12623
12624static bfd_boolean
12625undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12626{
12627 struct ppc_link_hash_entry *eh;
12628
12629 if (h->root.type == bfd_link_hash_indirect)
12630 return TRUE;
12631
99877b66
AM
12632 eh = (struct ppc_link_hash_entry *) h;
12633 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12634 return TRUE;
12635
12636 eh->elf.root.type = bfd_link_hash_undefined;
12637 return TRUE;
12638}
12639
12640void
12641ppc64_elf_restore_symbols (struct bfd_link_info *info)
12642{
12643 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
12644
12645 if (htab != NULL)
12646 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
12647}
12648
60124e18
AM
12649/* What to do when ld finds relocations against symbols defined in
12650 discarded sections. */
12651
12652static unsigned int
12653ppc64_elf_action_discarded (asection *sec)
12654{
12655 if (strcmp (".opd", sec->name) == 0)
12656 return 0;
12657
12658 if (strcmp (".toc", sec->name) == 0)
12659 return 0;
12660
bce50a28
JJ
12661 if (strcmp (".toc1", sec->name) == 0)
12662 return 0;
12663
60124e18
AM
12664 return _bfd_elf_default_action_discarded (sec);
12665}
12666
5bd4f169
AM
12667/* The RELOCATE_SECTION function is called by the ELF backend linker
12668 to handle the relocations for a section.
12669
12670 The relocs are always passed as Rela structures; if the section
12671 actually uses Rel structures, the r_addend field will always be
12672 zero.
12673
12674 This function is responsible for adjust the section contents as
12675 necessary, and (if using Rela relocs and generating a
1049f94e 12676 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
12677 necessary.
12678
12679 This function does not have to worry about setting the reloc
12680 address or the reloc symbol index.
12681
12682 LOCAL_SYMS is a pointer to the swapped in local symbols.
12683
12684 LOCAL_SECTIONS is an array giving the section in the input file
12685 corresponding to the st_shndx field of each local symbol.
12686
12687 The global hash table entry for the global symbols can be found
12688 via elf_sym_hashes (input_bfd).
12689
1049f94e 12690 When generating relocatable output, this function must handle
5bd4f169
AM
12691 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
12692 going to be the section symbol corresponding to the output
12693 section, which means that the addend must be adjusted
12694 accordingly. */
12695
b34976b6 12696static bfd_boolean
4ce794b7
AM
12697ppc64_elf_relocate_section (bfd *output_bfd,
12698 struct bfd_link_info *info,
12699 bfd *input_bfd,
12700 asection *input_section,
12701 bfd_byte *contents,
12702 Elf_Internal_Rela *relocs,
12703 Elf_Internal_Sym *local_syms,
12704 asection **local_sections)
5bd4f169 12705{
65f38f15 12706 struct ppc_link_hash_table *htab;
5bd4f169
AM
12707 Elf_Internal_Shdr *symtab_hdr;
12708 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
12709 Elf_Internal_Rela *rel;
12710 Elf_Internal_Rela *relend;
411e1bfb
AM
12711 Elf_Internal_Rela outrel;
12712 bfd_byte *loc;
411e1bfb 12713 struct got_entry **local_got_ents;
5bd4f169 12714 bfd_vma TOCstart;
b34976b6
AM
12715 bfd_boolean ret = TRUE;
12716 bfd_boolean is_opd;
794e51c0
AM
12717 /* Assume 'at' branch hints. */
12718 bfd_boolean is_isa_v2 = TRUE;
4fe5ca5b 12719 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 12720
65f38f15 12721 /* Initialize howto table if needed. */
5bd4f169 12722 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
12723 ppc_howto_init ();
12724
65f38f15 12725 htab = ppc_hash_table (info);
4dfe6ac6
NC
12726 if (htab == NULL)
12727 return FALSE;
ee75fd95
AM
12728
12729 /* Don't relocate stub sections. */
12730 if (input_section->owner == htab->stub_bfd)
12731 return TRUE;
12732
0c8d6e5c 12733 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 12734
411e1bfb 12735 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 12736 TOCstart = elf_gp (output_bfd);
0ffa91dd 12737 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 12738 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 12739 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 12740
5bd4f169
AM
12741 rel = relocs;
12742 relend = relocs + input_section->reloc_count;
12743 for (; rel < relend; rel++)
12744 {
04c9666a 12745 enum elf_ppc64_reloc_type r_type;
31c76678 12746 bfd_vma addend;
5bd4f169
AM
12747 bfd_reloc_status_type r;
12748 Elf_Internal_Sym *sym;
12749 asection *sec;
039b3fef
AM
12750 struct elf_link_hash_entry *h_elf;
12751 struct ppc_link_hash_entry *h;
12752 struct ppc_link_hash_entry *fdh;
5bd4f169 12753 const char *sym_name;
0d4792f7 12754 unsigned long r_symndx, toc_symndx;
3a71aa26 12755 bfd_vma toc_addend;
f961d9dd
AM
12756 unsigned char tls_mask, tls_gd, tls_type;
12757 unsigned char sym_type;
5bd4f169 12758 bfd_vma relocation;
b34976b6
AM
12759 bfd_boolean unresolved_reloc;
12760 bfd_boolean warned;
bc30df16 12761 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 12762 unsigned int insn;
e11840f9 12763 unsigned int mask;
721956f4
AM
12764 struct ppc_stub_hash_entry *stub_entry;
12765 bfd_vma max_br_offset;
12766 bfd_vma from;
31c76678 12767 const Elf_Internal_Rela orig_rel = *rel;
5bd4f169 12768
4ce794b7 12769 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 12770 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
12771
12772 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12773 symbol of the previous ADDR64 reloc. The symbol gives us the
12774 proper TOC base to use. */
12775 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12776 && rel != relocs
12777 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12778 && is_opd)
12779 r_symndx = ELF64_R_SYM (rel[-1].r_info);
12780
4ce794b7
AM
12781 sym = NULL;
12782 sec = NULL;
039b3fef 12783 h_elf = NULL;
4ce794b7 12784 sym_name = NULL;
b34976b6
AM
12785 unresolved_reloc = FALSE;
12786 warned = FALSE;
65f38f15 12787
0b13192e 12788 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
12789 {
12790 /* It's a local symbol. */
74f0fb50 12791 struct _opd_sec_data *opd;
4025353c 12792
5bd4f169
AM
12793 sym = local_syms + r_symndx;
12794 sec = local_sections[r_symndx];
26c61ae5 12795 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 12796 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 12797 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
12798 opd = get_opd_info (sec);
12799 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 12800 {
74f0fb50 12801 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
12802 if (adjust == -1)
12803 relocation = 0;
12804 else
4cc603a5
AM
12805 {
12806 /* If this is a relocation against the opd section sym
12807 and we have edited .opd, adjust the reloc addend so
12808 that ld -r and ld --emit-relocs output is correct.
12809 If it is a reloc against some other .opd symbol,
12810 then the symbol value will be adjusted later. */
12811 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12812 rel->r_addend += adjust;
12813 else
12814 relocation += adjust;
12815 }
1e2f5b6e 12816 }
5bd4f169
AM
12817 }
12818 else
12819 {
b2a8e766
AM
12820 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12821 r_symndx, symtab_hdr, sym_hashes,
039b3fef 12822 h_elf, sec, relocation,
b2a8e766 12823 unresolved_reloc, warned);
039b3fef
AM
12824 sym_name = h_elf->root.root.string;
12825 sym_type = h_elf->type;
b69fdb4e
AM
12826 if (sec != NULL
12827 && sec->owner == output_bfd
12828 && strcmp (sec->name, ".opd") == 0)
12829 {
12830 /* This is a symbol defined in a linker script. All
12831 such are defined in output sections, even those
12832 defined by simple assignment from a symbol defined in
12833 an input section. Transfer the symbol to an
12834 appropriate input .opd section, so that a branch to
12835 this symbol will be mapped to the location specified
12836 by the opd entry. */
12837 struct bfd_link_order *lo;
12838 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12839 if (lo->type == bfd_indirect_link_order)
12840 {
12841 asection *isec = lo->u.indirect.section;
12842 if (h_elf->root.u.def.value >= isec->output_offset
12843 && h_elf->root.u.def.value < (isec->output_offset
12844 + isec->size))
12845 {
12846 h_elf->root.u.def.value -= isec->output_offset;
12847 h_elf->root.u.def.section = isec;
12848 sec = isec;
12849 break;
12850 }
12851 }
12852 }
5bd4f169 12853 }
039b3fef 12854 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 12855
dbaa2011 12856 if (sec != NULL && discarded_section (sec))
e4067dbb 12857 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b
MR
12858 rel, 1, relend,
12859 ppc64_elf_howto_table[r_type], 0,
e4067dbb 12860 contents);
ab96bf03
AM
12861
12862 if (info->relocatable)
12863 continue;
12864
f40da81b
AM
12865 if (h != NULL && &h->elf == htab->elf.hgot)
12866 {
12867 relocation = (TOCstart
12868 + htab->stub_group[input_section->id].toc_off);
12869 sec = bfd_abs_section_ptr;
12870 unresolved_reloc = FALSE;
12871 }
12872
951fd09b
AM
12873 /* TLS optimizations. Replace instruction sequences and relocs
12874 based on information we collected in tls_optimize. We edit
12875 RELOCS so that --emit-relocs will output something sensible
12876 for the final instruction stream. */
12877 tls_mask = 0;
12878 tls_gd = 0;
0d4792f7 12879 toc_symndx = 0;
727fc41e
AM
12880 if (h != NULL)
12881 tls_mask = h->tls_mask;
12882 else if (local_got_ents != NULL)
411e1bfb 12883 {
e054468f
AM
12884 struct plt_entry **local_plt = (struct plt_entry **)
12885 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 12886 unsigned char *lgot_masks = (unsigned char *)
e054468f 12887 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
12888 tls_mask = lgot_masks[r_symndx];
12889 }
12890 if (tls_mask == 0
12891 && (r_type == R_PPC64_TLS
12892 || r_type == R_PPC64_TLSGD
12893 || r_type == R_PPC64_TLSLD))
12894 {
12895 /* Check for toc tls entries. */
f961d9dd 12896 unsigned char *toc_tls;
0d4792f7 12897
727fc41e
AM
12898 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12899 &local_syms, rel, input_bfd))
12900 return FALSE;
0d4792f7 12901
727fc41e
AM
12902 if (toc_tls)
12903 tls_mask = *toc_tls;
0d4792f7
AM
12904 }
12905
12906 /* Check that tls relocs are used with tls syms, and non-tls
12907 relocs are used with non-tls syms. */
cf35638d 12908 if (r_symndx != STN_UNDEF
0d4792f7
AM
12909 && r_type != R_PPC64_NONE
12910 && (h == NULL
039b3fef
AM
12911 || h->elf.root.type == bfd_link_hash_defined
12912 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
12913 && (IS_PPC64_TLS_RELOC (r_type)
12914 != (sym_type == STT_TLS
12915 || (sym_type == STT_SECTION
12916 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 12917 {
727fc41e
AM
12918 if (tls_mask != 0
12919 && (r_type == R_PPC64_TLS
12920 || r_type == R_PPC64_TLSGD
12921 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
12922 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12923 ;
12924 else
25f53a85 12925 info->callbacks->einfo
1d483afe 12926 (!IS_PPC64_TLS_RELOC (r_type)
bc30df16
AM
12927 ? _("%P: %H: %s used with TLS symbol `%T'\n")
12928 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 12929 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
12930 ppc64_elf_howto_table[r_type]->name,
12931 sym_name);
411e1bfb
AM
12932 }
12933
12934 /* Ensure reloc mapping code below stays sane. */
12935 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12936 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12937 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12938 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12939 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12940 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12941 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12942 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12943 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12944 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12945 abort ();
0d4792f7 12946
411e1bfb
AM
12947 switch (r_type)
12948 {
12949 default:
411e1bfb
AM
12950 break;
12951
ba761f19
AM
12952 case R_PPC64_LO_DS_OPT:
12953 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12954 if ((insn & (0x3f << 26)) != 58u << 26)
12955 abort ();
12956 insn += (14u << 26) - (58u << 26);
12957 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12958 r_type = R_PPC64_TOC16_LO;
12959 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12960 break;
12961
411e1bfb
AM
12962 case R_PPC64_TOC16:
12963 case R_PPC64_TOC16_LO:
12964 case R_PPC64_TOC16_DS:
12965 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
12966 {
12967 /* Check for toc tls entries. */
f961d9dd 12968 unsigned char *toc_tls;
951fd09b 12969 int retval;
411e1bfb 12970
3a71aa26
AM
12971 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12972 &local_syms, rel, input_bfd);
951fd09b 12973 if (retval == 0)
411e1bfb
AM
12974 return FALSE;
12975
12976 if (toc_tls)
12977 {
951fd09b 12978 tls_mask = *toc_tls;
411e1bfb
AM
12979 if (r_type == R_PPC64_TOC16_DS
12980 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
12981 {
12982 if (tls_mask != 0
12983 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12984 goto toctprel;
12985 }
411e1bfb 12986 else
951fd09b
AM
12987 {
12988 /* If we found a GD reloc pair, then we might be
12989 doing a GD->IE transition. */
12990 if (retval == 2)
12991 {
12992 tls_gd = TLS_TPRELGD;
12993 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12994 goto tls_ldgd_opt;
951fd09b
AM
12995 }
12996 else if (retval == 3)
12997 {
12998 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 12999 goto tls_ldgd_opt;
951fd09b
AM
13000 }
13001 }
411e1bfb
AM
13002 }
13003 }
13004 break;
13005
9d6ded02
AM
13006 case R_PPC64_GOT_TPREL16_HI:
13007 case R_PPC64_GOT_TPREL16_HA:
13008 if (tls_mask != 0
13009 && (tls_mask & TLS_TPREL) == 0)
13010 {
13011 rel->r_offset -= d_offset;
13012 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13013 r_type = R_PPC64_NONE;
13014 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13015 }
13016 break;
13017
411e1bfb
AM
13018 case R_PPC64_GOT_TPREL16_DS:
13019 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13020 if (tls_mask != 0
13021 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13022 {
81407a69 13023 toctprel:
4fe5ca5b 13024 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
13025 insn &= 31 << 21;
13026 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 13027 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 13028 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13029 if (toc_symndx != 0)
13030 {
13031 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13032 rel->r_addend = toc_addend;
0d4792f7
AM
13033 /* We changed the symbol. Start over in order to
13034 get h, sym, sec etc. right. */
13035 rel--;
13036 continue;
13037 }
13038 else
13039 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13040 }
13041 break;
13042
13043 case R_PPC64_TLS:
951fd09b
AM
13044 if (tls_mask != 0
13045 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13046 {
411e1bfb 13047 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
13048 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13049 if (insn == 0)
411e1bfb 13050 abort ();
411e1bfb 13051 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 13052 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13053 PPC64_TPREL16_LO which is at low-order half-word. */
13054 rel->r_offset += d_offset;
0d4792f7
AM
13055 r_type = R_PPC64_TPREL16_LO;
13056 if (toc_symndx != 0)
13057 {
13058 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13059 rel->r_addend = toc_addend;
0d4792f7
AM
13060 /* We changed the symbol. Start over in order to
13061 get h, sym, sec etc. right. */
13062 rel--;
13063 continue;
13064 }
13065 else
13066 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13067 }
13068 break;
13069
411e1bfb
AM
13070 case R_PPC64_GOT_TLSGD16_HI:
13071 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13072 tls_gd = TLS_TPRELGD;
13073 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13074 goto tls_gdld_hi;
13075 break;
13076
411e1bfb
AM
13077 case R_PPC64_GOT_TLSLD16_HI:
13078 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13079 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13080 {
951fd09b
AM
13081 tls_gdld_hi:
13082 if ((tls_mask & tls_gd) != 0)
13083 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13084 + R_PPC64_GOT_TPREL16_DS);
13085 else
411e1bfb 13086 {
4fe5ca5b 13087 rel->r_offset -= d_offset;
727ac201 13088 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 13089 r_type = R_PPC64_NONE;
411e1bfb 13090 }
951fd09b 13091 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13092 }
13093 break;
13094
951fd09b
AM
13095 case R_PPC64_GOT_TLSGD16:
13096 case R_PPC64_GOT_TLSGD16_LO:
13097 tls_gd = TLS_TPRELGD;
13098 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13099 goto tls_ldgd_opt;
951fd09b 13100 break;
411e1bfb 13101
951fd09b
AM
13102 case R_PPC64_GOT_TLSLD16:
13103 case R_PPC64_GOT_TLSLD16_LO:
13104 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13105 {
3a71aa26 13106 unsigned int insn1, insn2, insn3;
102890f0
AM
13107 bfd_vma offset;
13108
13109 tls_ldgd_opt:
727fc41e
AM
13110 offset = (bfd_vma) -1;
13111 /* If not using the newer R_PPC64_TLSGD/LD to mark
13112 __tls_get_addr calls, we must trust that the call
13113 stays with its arg setup insns, ie. that the next
13114 reloc is the __tls_get_addr call associated with
13115 the current reloc. Edit both insns. */
13116 if (input_section->has_tls_get_addr_call
13117 && rel + 1 < relend
13118 && branch_reloc_hash_match (input_bfd, rel + 1,
13119 htab->tls_get_addr,
13120 htab->tls_get_addr_fd))
13121 offset = rel[1].r_offset;
102890f0 13122 if ((tls_mask & tls_gd) != 0)
411e1bfb 13123 {
102890f0 13124 /* IE */
3a71aa26
AM
13125 insn1 = bfd_get_32 (output_bfd,
13126 contents + rel->r_offset - d_offset);
102890f0
AM
13127 insn1 &= (1 << 26) - (1 << 2);
13128 insn1 |= 58 << 26; /* ld */
13129 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13130 if (offset != (bfd_vma) -1)
f58d5a2d 13131 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13132 if ((tls_mask & TLS_EXPLICIT) == 0)
13133 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13134 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13135 else
102890f0
AM
13136 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13137 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13138 }
13139 else
13140 {
13141 /* LE */
13142 insn1 = 0x3c6d0000; /* addis 3,13,0 */
13143 insn2 = 0x38630000; /* addi 3,3,0 */
13144 if (tls_gd == 0)
951fd09b 13145 {
102890f0 13146 /* Was an LD reloc. */
1d483afe
AM
13147 if (toc_symndx)
13148 sec = local_sections[toc_symndx];
13149 for (r_symndx = 0;
13150 r_symndx < symtab_hdr->sh_info;
13151 r_symndx++)
13152 if (local_sections[r_symndx] == sec)
13153 break;
13154 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13155 r_symndx = STN_UNDEF;
102890f0 13156 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13157 if (r_symndx != STN_UNDEF)
1d483afe
AM
13158 rel->r_addend -= (local_syms[r_symndx].st_value
13159 + sec->output_offset
13160 + sec->output_section->vma);
951fd09b 13161 }
102890f0 13162 else if (toc_symndx != 0)
3a71aa26
AM
13163 {
13164 r_symndx = toc_symndx;
13165 rel->r_addend = toc_addend;
13166 }
102890f0
AM
13167 r_type = R_PPC64_TPREL16_HA;
13168 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13169 if (offset != (bfd_vma) -1)
13170 {
13171 rel[1].r_info = ELF64_R_INFO (r_symndx,
13172 R_PPC64_TPREL16_LO);
13173 rel[1].r_offset = offset + d_offset;
13174 rel[1].r_addend = rel->r_addend;
13175 }
102890f0 13176 }
3a71aa26
AM
13177 bfd_put_32 (output_bfd, insn1,
13178 contents + rel->r_offset - d_offset);
727fc41e
AM
13179 if (offset != (bfd_vma) -1)
13180 {
13181 insn3 = bfd_get_32 (output_bfd,
13182 contents + offset + 4);
13183 if (insn3 == NOP
13184 || insn3 == CROR_151515 || insn3 == CROR_313131)
13185 {
13186 rel[1].r_offset += 4;
13187 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13188 insn2 = NOP;
13189 }
13190 bfd_put_32 (output_bfd, insn2, contents + offset);
13191 }
13192 if ((tls_mask & tls_gd) == 0
13193 && (tls_gd == 0 || toc_symndx != 0))
13194 {
13195 /* We changed the symbol. Start over in order
13196 to get h, sym, sec etc. right. */
13197 rel--;
13198 continue;
13199 }
13200 }
13201 break;
13202
13203 case R_PPC64_TLSGD:
13204 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13205 {
13206 unsigned int insn2, insn3;
13207 bfd_vma offset = rel->r_offset;
13208
13209 if ((tls_mask & TLS_TPRELGD) != 0)
13210 {
13211 /* IE */
13212 r_type = R_PPC64_NONE;
13213 insn2 = 0x7c636a14; /* add 3,3,13 */
13214 }
13215 else
13216 {
13217 /* LE */
13218 if (toc_symndx != 0)
13219 {
13220 r_symndx = toc_symndx;
13221 rel->r_addend = toc_addend;
13222 }
13223 r_type = R_PPC64_TPREL16_LO;
13224 rel->r_offset = offset + d_offset;
13225 insn2 = 0x38630000; /* addi 3,3,0 */
13226 }
13227 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13228 /* Zap the reloc on the _tls_get_addr call too. */
13229 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13230 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
13231 insn3 = bfd_get_32 (output_bfd,
13232 contents + offset + 4);
102890f0
AM
13233 if (insn3 == NOP
13234 || insn3 == CROR_151515 || insn3 == CROR_313131)
13235 {
727fc41e 13236 rel->r_offset += 4;
3a71aa26
AM
13237 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13238 insn2 = NOP;
102890f0 13239 }
102890f0 13240 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 13241 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 13242 {
102890f0
AM
13243 rel--;
13244 continue;
411e1bfb 13245 }
411e1bfb 13246 }
411e1bfb
AM
13247 break;
13248
727fc41e
AM
13249 case R_PPC64_TLSLD:
13250 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13251 {
13252 unsigned int insn2, insn3;
13253 bfd_vma offset = rel->r_offset;
13254
13255 if (toc_symndx)
13256 sec = local_sections[toc_symndx];
13257 for (r_symndx = 0;
13258 r_symndx < symtab_hdr->sh_info;
13259 r_symndx++)
13260 if (local_sections[r_symndx] == sec)
13261 break;
13262 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13263 r_symndx = STN_UNDEF;
727fc41e 13264 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13265 if (r_symndx != STN_UNDEF)
727fc41e
AM
13266 rel->r_addend -= (local_syms[r_symndx].st_value
13267 + sec->output_offset
13268 + sec->output_section->vma);
13269
13270 r_type = R_PPC64_TPREL16_LO;
13271 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13272 rel->r_offset = offset + d_offset;
13273 /* Zap the reloc on the _tls_get_addr call too. */
13274 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13275 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
13276 insn2 = 0x38630000; /* addi 3,3,0 */
13277 insn3 = bfd_get_32 (output_bfd,
13278 contents + offset + 4);
13279 if (insn3 == NOP
13280 || insn3 == CROR_151515 || insn3 == CROR_313131)
13281 {
13282 rel->r_offset += 4;
13283 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13284 insn2 = NOP;
13285 }
13286 bfd_put_32 (output_bfd, insn2, contents + offset);
13287 rel--;
13288 continue;
13289 }
13290 break;
13291
411e1bfb 13292 case R_PPC64_DTPMOD64:
951fd09b
AM
13293 if (rel + 1 < relend
13294 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13295 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13296 {
951fd09b
AM
13297 if ((tls_mask & TLS_GD) == 0)
13298 {
13299 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13300 if ((tls_mask & TLS_TPRELGD) != 0)
13301 r_type = R_PPC64_TPREL64;
13302 else
13303 {
4ce794b7 13304 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13305 r_type = R_PPC64_NONE;
13306 }
13307 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13308 }
13309 }
13310 else
13311 {
13312 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13313 {
4ce794b7 13314 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13315 r_type = R_PPC64_NONE;
951fd09b 13316 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13317 }
411e1bfb
AM
13318 }
13319 break;
13320
13321 case R_PPC64_TPREL64:
951fd09b 13322 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13323 {
13324 r_type = R_PPC64_NONE;
13325 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13326 }
13327 break;
13328 }
13329
13330 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 13331 insn = 0;
b25116a9
AM
13332 max_br_offset = 1 << 25;
13333 addend = rel->r_addend;
bc30df16 13334 reloc_dest = DEST_NORMAL;
65f38f15 13335 switch (r_type)
5bd4f169
AM
13336 {
13337 default:
65f38f15 13338 break;
5bd4f169 13339
3b421ab3
AM
13340 case R_PPC64_TOCSAVE:
13341 if (relocation + addend == (rel->r_offset
13342 + input_section->output_offset
13343 + input_section->output_section->vma)
13344 && tocsave_find (htab, NO_INSERT,
13345 &local_syms, rel, input_bfd))
13346 {
13347 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13348 if (insn == NOP
13349 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
13350 bfd_put_32 (input_bfd,
13351 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
13352 contents + rel->r_offset);
13353 }
13354 break;
13355
65f38f15
AM
13356 /* Branch taken prediction relocations. */
13357 case R_PPC64_ADDR14_BRTAKEN:
13358 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
13359 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13360 /* Fall thru. */
65f38f15 13361
86c76c7b 13362 /* Branch not taken prediction relocations. */
65f38f15
AM
13363 case R_PPC64_ADDR14_BRNTAKEN:
13364 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
13365 insn |= bfd_get_32 (output_bfd,
13366 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 13367 /* Fall thru. */
86c76c7b 13368
b25116a9
AM
13369 case R_PPC64_REL14:
13370 max_br_offset = 1 << 15;
13371 /* Fall thru. */
5bd4f169 13372
65f38f15 13373 case R_PPC64_REL24:
ad8e1ba5
AM
13374 /* Calls to functions with a different TOC, such as calls to
13375 shared objects, need to alter the TOC pointer. This is
13376 done using a linkage stub. A REL24 branching to these
13377 linkage stubs needs to be followed by a nop, as the nop
13378 will be replaced with an instruction to restore the TOC
13379 base pointer. */
8387904d 13380 fdh = h;
b31867b6
AM
13381 if (h != NULL
13382 && h->oh != NULL
13383 && h->oh->is_func_descriptor)
13384 fdh = ppc_follow_link (h->oh);
31c76678
DK
13385 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13386 htab);
6abec6bc 13387 if (stub_entry != NULL
ad8e1ba5 13388 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 13389 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
13390 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13391 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 13392 {
b25116a9 13393 bfd_boolean can_plt_call = FALSE;
721956f4 13394
ba8ca3e7
AM
13395 /* All of these stubs will modify r2, so there must be a
13396 branch and link followed by a nop. The nop is
13397 replaced by an insn to restore r2. */
eea6121a 13398 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 13399 {
ba8ca3e7
AM
13400 unsigned long br;
13401
13402 br = bfd_get_32 (input_bfd,
13403 contents + rel->r_offset);
13404 if ((br & 1) != 0)
41bd81ab 13405 {
ba8ca3e7
AM
13406 unsigned long nop;
13407
13408 nop = bfd_get_32 (input_bfd,
13409 contents + rel->r_offset + 4);
13410 if (nop == NOP
13411 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 13412 {
ba8ca3e7
AM
13413 if (h != NULL
13414 && (h == htab->tls_get_addr_fd
13415 || h == htab->tls_get_addr)
13416 && !htab->no_tls_get_addr_opt)
13417 {
13418 /* Special stub used, leave nop alone. */
13419 }
13420 else
a078d95a
AM
13421 bfd_put_32 (input_bfd,
13422 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
13423 contents + rel->r_offset + 4);
13424 can_plt_call = TRUE;
a7f2871e 13425 }
41bd81ab 13426 }
5bd4f169 13427 }
721956f4 13428
ba8ca3e7 13429 if (!can_plt_call && h != NULL)
721956f4 13430 {
ba8ca3e7
AM
13431 const char *name = h->elf.root.root.string;
13432
13433 if (*name == '.')
13434 ++name;
13435
13436 if (strncmp (name, "__libc_start_main", 17) == 0
13437 && (name[17] == 0 || name[17] == '@'))
6ab189d5 13438 {
ba8ca3e7
AM
13439 /* Allow crt1 branch to go via a toc adjusting
13440 stub. Other calls that never return could do
13441 the same, if we could detect such. */
b25116a9 13442 can_plt_call = TRUE;
6ab189d5 13443 }
ba8ca3e7
AM
13444 }
13445
13446 if (!can_plt_call)
13447 {
13448 /* g++ as of 20130507 emits self-calls without a
13449 following nop. This is arguably wrong since we
13450 have conflicting information. On the one hand a
13451 global symbol and on the other a local call
13452 sequence, but don't error for this special case.
13453 It isn't possible to cheaply verify we have
13454 exactly such a call. Allow all calls to the same
13455 section. */
13456 asection *code_sec = sec;
13457
13458 if (get_opd_info (sec) != NULL)
ad8e1ba5 13459 {
ba8ca3e7
AM
13460 bfd_vma off = (relocation + addend
13461 - sec->output_section->vma
13462 - sec->output_offset);
bc30df16 13463
ba8ca3e7 13464 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 13465 }
ba8ca3e7
AM
13466 if (code_sec == input_section)
13467 can_plt_call = TRUE;
13468 }
13469
13470 if (!can_plt_call)
13471 {
13472 info->callbacks->einfo
13473 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13474 "recompile with -fPIC"),
13475 input_bfd, input_section, rel->r_offset, sym_name);
13476
13477 bfd_set_error (bfd_error_bad_value);
13478 ret = FALSE;
721956f4
AM
13479 }
13480
b25116a9 13481 if (can_plt_call
794e51c0
AM
13482 && (stub_entry->stub_type == ppc_stub_plt_call
13483 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
13484 unresolved_reloc = FALSE;
13485 }
13486
6abec6bc
AM
13487 if ((stub_entry == NULL
13488 || stub_entry->stub_type == ppc_stub_long_branch
13489 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
13490 && get_opd_info (sec) != NULL)
13491 {
13492 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
13493 bfd_vma off = (relocation + addend
13494 - sec->output_section->vma
13495 - sec->output_offset);
aef36ac1 13496 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
13497 if (dest != (bfd_vma) -1)
13498 {
13499 relocation = dest;
13500 addend = 0;
bc30df16 13501 reloc_dest = DEST_OPD;
8387904d
AM
13502 }
13503 }
13504
b25116a9
AM
13505 /* If the branch is out of reach we ought to have a long
13506 branch stub. */
13507 from = (rel->r_offset
13508 + input_section->output_offset
13509 + input_section->output_section->vma);
13510
6911b7dc
AM
13511 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13512 ? fdh->elf.other
13513 : sym->st_other);
13514
6abec6bc
AM
13515 if (stub_entry != NULL
13516 && (stub_entry->stub_type == ppc_stub_long_branch
13517 || stub_entry->stub_type == ppc_stub_plt_branch)
13518 && (r_type == R_PPC64_ADDR14_BRTAKEN
13519 || r_type == R_PPC64_ADDR14_BRNTAKEN
13520 || (relocation + addend - from + max_br_offset
13521 < 2 * max_br_offset)))
13522 /* Don't use the stub if this branch is in range. */
13523 stub_entry = NULL;
b25116a9
AM
13524
13525 if (stub_entry != NULL)
13526 {
13527 /* Munge up the value and addend so that we call the stub
13528 rather than the procedure directly. */
13529 relocation = (stub_entry->stub_offset
13530 + stub_entry->stub_sec->output_offset
13531 + stub_entry->stub_sec->output_section->vma);
13532 addend = 0;
bc30df16 13533 reloc_dest = DEST_STUB;
3b421ab3 13534
794e51c0
AM
13535 if ((stub_entry->stub_type == ppc_stub_plt_call
13536 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13537 && (ALWAYS_EMIT_R2SAVE
13538 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
13539 && rel + 1 < relend
13540 && rel[1].r_offset == rel->r_offset + 4
13541 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13542 relocation += 4;
b25116a9
AM
13543 }
13544
13545 if (insn != 0)
13546 {
794e51c0 13547 if (is_isa_v2)
721956f4 13548 {
b25116a9
AM
13549 /* Set 'a' bit. This is 0b00010 in BO field for branch
13550 on CR(BI) insns (BO == 001at or 011at), and 0b01000
13551 for branch on CTR insns (BO == 1a00t or 1a01t). */
13552 if ((insn & (0x14 << 21)) == (0x04 << 21))
13553 insn |= 0x02 << 21;
13554 else if ((insn & (0x14 << 21)) == (0x10 << 21))
13555 insn |= 0x08 << 21;
13556 else
13557 break;
13558 }
13559 else
13560 {
13561 /* Invert 'y' bit if not the default. */
4cc603a5 13562 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 13563 insn ^= 0x01 << 21;
721956f4 13564 }
b25116a9
AM
13565
13566 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 13567 }
e86ce104 13568
06da1e8e
AM
13569 /* NOP out calls to undefined weak functions.
13570 We can thus call a weak function without first
13571 checking whether the function is defined. */
b25116a9 13572 else if (h != NULL
039b3fef 13573 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 13574 && h->elf.dynindx == -1
b25116a9
AM
13575 && r_type == R_PPC64_REL24
13576 && relocation == 0
4cc603a5 13577 && addend == 0)
e86ce104 13578 {
06da1e8e
AM
13579 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13580 continue;
e86ce104 13581 }
65f38f15
AM
13582 break;
13583 }
5bd4f169 13584
65f38f15 13585 /* Set `addend'. */
411e1bfb 13586 tls_type = 0;
65f38f15
AM
13587 switch (r_type)
13588 {
13589 default:
25f53a85 13590 info->callbacks->einfo
bc30df16 13591 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 13592 input_bfd, (int) r_type, sym_name);
5bd4f169 13593
65f38f15 13594 bfd_set_error (bfd_error_bad_value);
b34976b6 13595 ret = FALSE;
65f38f15 13596 continue;
5bd4f169 13597
65f38f15 13598 case R_PPC64_NONE:
411e1bfb 13599 case R_PPC64_TLS:
727fc41e
AM
13600 case R_PPC64_TLSGD:
13601 case R_PPC64_TLSLD:
3b421ab3 13602 case R_PPC64_TOCSAVE:
04c9666a
AM
13603 case R_PPC64_GNU_VTINHERIT:
13604 case R_PPC64_GNU_VTENTRY:
65f38f15 13605 continue;
5bd4f169
AM
13606
13607 /* GOT16 relocations. Like an ADDR16 using the symbol's
13608 address in the GOT as relocation value instead of the
411e1bfb 13609 symbol's value itself. Also, create a GOT entry for the
5bd4f169 13610 symbol and put the symbol value there. */
411e1bfb
AM
13611 case R_PPC64_GOT_TLSGD16:
13612 case R_PPC64_GOT_TLSGD16_LO:
13613 case R_PPC64_GOT_TLSGD16_HI:
13614 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 13615 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
13616 goto dogot;
13617
13618 case R_PPC64_GOT_TLSLD16:
13619 case R_PPC64_GOT_TLSLD16_LO:
13620 case R_PPC64_GOT_TLSLD16_HI:
13621 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13622 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
13623 goto dogot;
13624
13625 case R_PPC64_GOT_TPREL16_DS:
13626 case R_PPC64_GOT_TPREL16_LO_DS:
13627 case R_PPC64_GOT_TPREL16_HI:
13628 case R_PPC64_GOT_TPREL16_HA:
13629 tls_type = TLS_TLS | TLS_TPREL;
13630 goto dogot;
13631
13632 case R_PPC64_GOT_DTPREL16_DS:
13633 case R_PPC64_GOT_DTPREL16_LO_DS:
13634 case R_PPC64_GOT_DTPREL16_HI:
13635 case R_PPC64_GOT_DTPREL16_HA:
13636 tls_type = TLS_TLS | TLS_DTPREL;
13637 goto dogot;
13638
65f38f15
AM
13639 case R_PPC64_GOT16:
13640 case R_PPC64_GOT16_LO:
13641 case R_PPC64_GOT16_HI:
13642 case R_PPC64_GOT16_HA:
13643 case R_PPC64_GOT16_DS:
13644 case R_PPC64_GOT16_LO_DS:
411e1bfb 13645 dogot:
5bd4f169
AM
13646 {
13647 /* Relocation is to the entry for this symbol in the global
13648 offset table. */
e717da7e 13649 asection *got;
d881513a 13650 bfd_vma *offp;
5bd4f169 13651 bfd_vma off;
d881513a 13652 unsigned long indx = 0;
927be08e 13653 struct got_entry *ent;
65f38f15 13654
d881513a
AM
13655 if (tls_type == (TLS_TLS | TLS_LD)
13656 && (h == NULL
f5385ebf 13657 || !h->elf.def_dynamic))
927be08e 13658 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 13659 else
5bd4f169 13660 {
5bd4f169 13661
d881513a
AM
13662 if (h != NULL)
13663 {
13664 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
13665 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13666 &h->elf)
d881513a 13667 || (info->shared
726d3ab0 13668 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
13669 /* This is actually a static link, or it is a
13670 -Bsymbolic link and the symbol is defined
13671 locally, or the symbol was forced to be local
13672 because of a version file. */
13673 ;
13674 else
13675 {
14acf4dc 13676 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 13677 indx = h->elf.dynindx;
d881513a
AM
13678 unresolved_reloc = FALSE;
13679 }
039b3fef 13680 ent = h->elf.got.glist;
d881513a 13681 }
411e1bfb 13682 else
5bd4f169 13683 {
d881513a
AM
13684 if (local_got_ents == NULL)
13685 abort ();
13686 ent = local_got_ents[r_symndx];
5bd4f169 13687 }
d881513a
AM
13688
13689 for (; ent != NULL; ent = ent->next)
31c76678 13690 if (ent->addend == orig_rel.r_addend
e717da7e 13691 && ent->owner == input_bfd
d881513a
AM
13692 && ent->tls_type == tls_type)
13693 break;
5bd4f169 13694 }
411e1bfb 13695
927be08e
AM
13696 if (ent == NULL)
13697 abort ();
13698 if (ent->is_indirect)
13699 ent = ent->got.ent;
13700 offp = &ent->got.offset;
13701 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
13702 if (got == NULL)
13703 abort ();
13704
411e1bfb
AM
13705 /* The offset must always be a multiple of 8. We use the
13706 least significant bit to record whether we have already
13707 processed this entry. */
d881513a 13708 off = *offp;
411e1bfb
AM
13709 if ((off & 1) != 0)
13710 off &= ~1;
5bd4f169
AM
13711 else
13712 {
411e1bfb
AM
13713 /* Generate relocs for the dynamic linker, except in
13714 the case of TLSLD where we'll use one entry per
13715 module. */
25f23106
AM
13716 asection *relgot;
13717 bfd_boolean ifunc;
e717da7e 13718
d881513a 13719 *offp = off | 1;
25f23106
AM
13720 relgot = NULL;
13721 ifunc = (h != NULL
13722 ? h->elf.type == STT_GNU_IFUNC
13723 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 13724 if (ifunc)
25f23106 13725 relgot = htab->reliplt;
19e08130
AM
13726 else if ((info->shared || indx != 0)
13727 && (h == NULL
13728 || (tls_type == (TLS_TLS | TLS_LD)
13729 && !h->elf.def_dynamic)
13730 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13731 || h->elf.root.type != bfd_link_hash_undefweak))
13732 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 13733 if (relgot != NULL)
5bd4f169 13734 {
e717da7e
AM
13735 outrel.r_offset = (got->output_section->vma
13736 + got->output_offset
411e1bfb 13737 + off);
4cc603a5 13738 outrel.r_addend = addend;
d881513a 13739 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 13740 {
411e1bfb 13741 outrel.r_addend = 0;
e515b051 13742 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
13743 if (tls_type == (TLS_TLS | TLS_GD))
13744 {
e717da7e
AM
13745 loc = relgot->contents;
13746 loc += (relgot->reloc_count++
d881513a
AM
13747 * sizeof (Elf64_External_Rela));
13748 bfd_elf64_swap_reloca_out (output_bfd,
13749 &outrel, loc);
e515b051 13750 outrel.r_offset += 8;
4cc603a5 13751 outrel.r_addend = addend;
d881513a
AM
13752 outrel.r_info
13753 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 13754 }
411e1bfb 13755 }
951fd09b 13756 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 13757 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 13758 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13759 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
13760 else if (indx != 0)
13761 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13762 else
81407a69 13763 {
25f23106
AM
13764 if (ifunc)
13765 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13766 else
13767 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
13768
13769 /* Write the .got section contents for the sake
13770 of prelink. */
e717da7e 13771 loc = got->contents + off;
23fbd6fa
JJ
13772 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13773 loc);
81407a69 13774 }
81407a69
AM
13775
13776 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
13777 {
13778 outrel.r_addend += relocation;
13779 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 13780 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 13781 }
e717da7e
AM
13782 loc = relgot->contents;
13783 loc += (relgot->reloc_count++
411e1bfb
AM
13784 * sizeof (Elf64_External_Rela));
13785 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13786 }
13787
ad8e1ba5 13788 /* Init the .got section contents here if we're not
81407a69 13789 emitting a reloc. */
d881513a 13790 else
411e1bfb 13791 {
4cc603a5 13792 relocation += addend;
7b609f53
AM
13793 if (tls_type == (TLS_TLS | TLS_LD))
13794 relocation = 1;
13795 else if (tls_type != 0)
411e1bfb 13796 {
e1918d23 13797 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 13798 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13799 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 13800
7b609f53
AM
13801 if (tls_type == (TLS_TLS | TLS_GD))
13802 {
13803 bfd_put_64 (output_bfd, relocation,
e717da7e 13804 got->contents + off + 8);
7b609f53
AM
13805 relocation = 1;
13806 }
411e1bfb 13807 }
7b609f53 13808
411e1bfb 13809 bfd_put_64 (output_bfd, relocation,
e717da7e 13810 got->contents + off);
5bd4f169
AM
13811 }
13812 }
13813
65f38f15
AM
13814 if (off >= (bfd_vma) -2)
13815 abort ();
13816
bf102f86
AM
13817 relocation = got->output_section->vma + got->output_offset + off;
13818 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 13819 }
65f38f15
AM
13820 break;
13821
13822 case R_PPC64_PLT16_HA:
13823 case R_PPC64_PLT16_HI:
13824 case R_PPC64_PLT16_LO:
13825 case R_PPC64_PLT32:
13826 case R_PPC64_PLT64:
13827 /* Relocation is to the entry for this symbol in the
13828 procedure linkage table. */
13829
13830 /* Resolve a PLT reloc against a local symbol directly,
13831 without using the procedure linkage table. */
13832 if (h == NULL)
13833 break;
13834
411e1bfb
AM
13835 /* It's possible that we didn't make a PLT entry for this
13836 symbol. This happens when statically linking PIC code,
13837 or when using -Bsymbolic. Go find a match if there is a
13838 PLT entry. */
4ce794b7 13839 if (htab->plt != NULL)
65f38f15 13840 {
411e1bfb 13841 struct plt_entry *ent;
039b3fef 13842 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
31c76678 13843 if (ent->addend == orig_rel.r_addend
411e1bfb
AM
13844 && ent->plt.offset != (bfd_vma) -1)
13845 {
4ce794b7
AM
13846 relocation = (htab->plt->output_section->vma
13847 + htab->plt->output_offset
411e1bfb
AM
13848 + ent->plt.offset);
13849 unresolved_reloc = FALSE;
13850 }
65f38f15 13851 }
65f38f15 13852 break;
5bd4f169 13853
0b13192e
AM
13854 case R_PPC64_TOC:
13855 /* Relocation value is TOC base. */
13856 relocation = TOCstart;
cf35638d 13857 if (r_symndx == STN_UNDEF)
0b13192e 13858 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
13859 else if (unresolved_reloc)
13860 ;
13861 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
13862 relocation += htab->stub_group[sec->id].toc_off;
13863 else
13864 unresolved_reloc = TRUE;
ab96bf03 13865 goto dodyn;
0b13192e 13866
5bd4f169
AM
13867 /* TOC16 relocs. We want the offset relative to the TOC base,
13868 which is the address of the start of the TOC plus 0x8000.
13869 The TOC consists of sections .got, .toc, .tocbss, and .plt,
13870 in this order. */
65f38f15
AM
13871 case R_PPC64_TOC16:
13872 case R_PPC64_TOC16_LO:
13873 case R_PPC64_TOC16_HI:
13874 case R_PPC64_TOC16_DS:
13875 case R_PPC64_TOC16_LO_DS:
13876 case R_PPC64_TOC16_HA:
ad8e1ba5 13877 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
13878 break;
13879
13880 /* Relocate against the beginning of the section. */
65f38f15
AM
13881 case R_PPC64_SECTOFF:
13882 case R_PPC64_SECTOFF_LO:
13883 case R_PPC64_SECTOFF_HI:
13884 case R_PPC64_SECTOFF_DS:
13885 case R_PPC64_SECTOFF_LO_DS:
13886 case R_PPC64_SECTOFF_HA:
4ce794b7 13887 if (sec != NULL)
65f38f15 13888 addend -= sec->output_section->vma;
5bd4f169
AM
13889 break;
13890
25f23106
AM
13891 case R_PPC64_REL16:
13892 case R_PPC64_REL16_LO:
13893 case R_PPC64_REL16_HI:
13894 case R_PPC64_REL16_HA:
13895 break;
13896
721956f4
AM
13897 case R_PPC64_REL14:
13898 case R_PPC64_REL14_BRNTAKEN:
13899 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
13900 case R_PPC64_REL24:
13901 break;
13902
411e1bfb
AM
13903 case R_PPC64_TPREL16:
13904 case R_PPC64_TPREL16_LO:
13905 case R_PPC64_TPREL16_HI:
13906 case R_PPC64_TPREL16_HA:
13907 case R_PPC64_TPREL16_DS:
13908 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
13909 case R_PPC64_TPREL16_HIGH:
13910 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
13911 case R_PPC64_TPREL16_HIGHER:
13912 case R_PPC64_TPREL16_HIGHERA:
13913 case R_PPC64_TPREL16_HIGHEST:
13914 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
13915 if (h != NULL
13916 && h->elf.root.type == bfd_link_hash_undefweak
13917 && h->elf.dynindx == -1)
13918 {
13919 /* Make this relocation against an undefined weak symbol
13920 resolve to zero. This is really just a tweak, since
13921 code using weak externs ought to check that they are
13922 defined before using them. */
13923 bfd_byte *p = contents + rel->r_offset - d_offset;
13924
13925 insn = bfd_get_32 (output_bfd, p);
13926 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13927 if (insn != 0)
13928 bfd_put_32 (output_bfd, insn, p);
13929 break;
13930 }
e1918d23 13931 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13932 if (info->shared)
13933 /* The TPREL16 relocs shouldn't really be used in shared
13934 libs as they will result in DT_TEXTREL being set, but
13935 support them anyway. */
13936 goto dodyn;
13937 break;
13938
13939 case R_PPC64_DTPREL16:
13940 case R_PPC64_DTPREL16_LO:
13941 case R_PPC64_DTPREL16_HI:
13942 case R_PPC64_DTPREL16_HA:
13943 case R_PPC64_DTPREL16_DS:
13944 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
13945 case R_PPC64_DTPREL16_HIGH:
13946 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
13947 case R_PPC64_DTPREL16_HIGHER:
13948 case R_PPC64_DTPREL16_HIGHERA:
13949 case R_PPC64_DTPREL16_HIGHEST:
13950 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 13951 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13952 break;
13953
e515b051
AM
13954 case R_PPC64_DTPMOD64:
13955 relocation = 1;
13956 addend = 0;
13957 goto dodyn;
13958
411e1bfb 13959 case R_PPC64_TPREL64:
e1918d23 13960 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13961 goto dodyn;
13962
13963 case R_PPC64_DTPREL64:
e1918d23 13964 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13965 /* Fall thru */
13966
65f38f15
AM
13967 /* Relocations that may need to be propagated if this is a
13968 dynamic object. */
04c9666a 13969 case R_PPC64_REL30:
65f38f15
AM
13970 case R_PPC64_REL32:
13971 case R_PPC64_REL64:
13972 case R_PPC64_ADDR14:
13973 case R_PPC64_ADDR14_BRNTAKEN:
13974 case R_PPC64_ADDR14_BRTAKEN:
13975 case R_PPC64_ADDR16:
13976 case R_PPC64_ADDR16_DS:
13977 case R_PPC64_ADDR16_HA:
13978 case R_PPC64_ADDR16_HI:
f9c6b907
AM
13979 case R_PPC64_ADDR16_HIGH:
13980 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
13981 case R_PPC64_ADDR16_HIGHER:
13982 case R_PPC64_ADDR16_HIGHERA:
13983 case R_PPC64_ADDR16_HIGHEST:
13984 case R_PPC64_ADDR16_HIGHESTA:
13985 case R_PPC64_ADDR16_LO:
13986 case R_PPC64_ADDR16_LO_DS:
13987 case R_PPC64_ADDR24:
65f38f15
AM
13988 case R_PPC64_ADDR32:
13989 case R_PPC64_ADDR64:
13990 case R_PPC64_UADDR16:
13991 case R_PPC64_UADDR32:
13992 case R_PPC64_UADDR64:
411e1bfb 13993 dodyn:
5d1634d7 13994 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
13995 break;
13996
41bd81ab
AM
13997 if (NO_OPD_RELOCS && is_opd)
13998 break;
13999
65f38f15 14000 if ((info->shared
4e795f50 14001 && (h == NULL
039b3fef
AM
14002 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14003 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 14004 && (must_be_dyn_reloc (info, r_type)
039b3fef 14005 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
14006 || (ELIMINATE_COPY_RELOCS
14007 && !info->shared
65f38f15 14008 && h != NULL
039b3fef 14009 && h->elf.dynindx != -1
f5385ebf 14010 && !h->elf.non_got_ref
25f23106
AM
14011 && !h->elf.def_regular)
14012 || (!info->shared
14013 && (h != NULL
14014 ? h->elf.type == STT_GNU_IFUNC
14015 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 14016 {
b34976b6 14017 bfd_boolean skip, relocate;
65f38f15 14018 asection *sreloc;
1cf1f670 14019 bfd_vma out_off;
65f38f15
AM
14020
14021 /* When generating a dynamic object, these relocations
14022 are copied into the output file to be resolved at run
14023 time. */
14024
b34976b6
AM
14025 skip = FALSE;
14026 relocate = FALSE;
65f38f15 14027
1cf1f670
AM
14028 out_off = _bfd_elf_section_offset (output_bfd, info,
14029 input_section, rel->r_offset);
14030 if (out_off == (bfd_vma) -1)
b34976b6 14031 skip = TRUE;
1cf1f670 14032 else if (out_off == (bfd_vma) -2)
b34976b6 14033 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14034 out_off += (input_section->output_section->vma
14035 + input_section->output_offset);
14036 outrel.r_offset = out_off;
411e1bfb 14037 outrel.r_addend = rel->r_addend;
65f38f15 14038
1cf1f670
AM
14039 /* Optimize unaligned reloc use. */
14040 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14041 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14042 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14043 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14044 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14045 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14046 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14047 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14048 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14049
65f38f15 14050 if (skip)
0bb2d96a 14051 memset (&outrel, 0, sizeof outrel);
726d3ab0 14052 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
14053 && !is_opd
14054 && r_type != R_PPC64_TOC)
14acf4dc
MR
14055 {
14056 BFD_ASSERT (h->elf.dynindx != -1);
14057 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14058 }
65f38f15
AM
14059 else
14060 {
41bd81ab
AM
14061 /* This symbol is local, or marked to become local,
14062 or this is an opd section reloc which must point
14063 at a local function. */
65f38f15 14064 outrel.r_addend += relocation;
e86ce104 14065 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14066 {
3fad3c7c 14067 if (is_opd && h != NULL)
afbe61cf
AM
14068 {
14069 /* Lie about opd entries. This case occurs
14070 when building shared libraries and we
14071 reference a function in another shared
3fad3c7c
AM
14072 lib. The same thing happens for a weak
14073 definition in an application that's
14074 overridden by a strong definition in a
14075 shared lib. (I believe this is a generic
14076 bug in binutils handling of weak syms.)
14077 In these cases we won't use the opd
1e2f5b6e 14078 entry in this lib. */
b34976b6 14079 unresolved_reloc = FALSE;
afbe61cf 14080 }
25f23106
AM
14081 if (!is_opd
14082 && r_type == R_PPC64_ADDR64
14083 && (h != NULL
14084 ? h->elf.type == STT_GNU_IFUNC
14085 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14086 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14087 else
14088 {
14089 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14090
25f23106
AM
14091 /* We need to relocate .opd contents for ld.so.
14092 Prelink also wants simple and consistent rules
14093 for relocs. This make all RELATIVE relocs have
14094 *r_offset equal to r_addend. */
14095 relocate = TRUE;
14096 }
65f38f15
AM
14097 }
14098 else
14099 {
14100 long indx = 0;
14101
25f23106
AM
14102 if (h != NULL
14103 ? h->elf.type == STT_GNU_IFUNC
14104 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14105 {
25f53a85 14106 info->callbacks->einfo
bc30df16
AM
14107 (_("%P: %H: %s for indirect "
14108 "function `%T' unsupported\n"),
25f53a85 14109 input_bfd, input_section, rel->r_offset,
25f23106
AM
14110 ppc64_elf_howto_table[r_type]->name,
14111 sym_name);
14112 ret = FALSE;
14113 }
cf35638d 14114 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14115 ;
14116 else if (sec == NULL || sec->owner == NULL)
14117 {
14118 bfd_set_error (bfd_error_bad_value);
b34976b6 14119 return FALSE;
65f38f15
AM
14120 }
14121 else
14122 {
14123 asection *osec;
14124
14125 osec = sec->output_section;
14126 indx = elf_section_data (osec)->dynindx;
14127
74541ad4
AM
14128 if (indx == 0)
14129 {
14130 if ((osec->flags & SEC_READONLY) == 0
14131 && htab->elf.data_index_section != NULL)
14132 osec = htab->elf.data_index_section;
14133 else
14134 osec = htab->elf.text_index_section;
14135 indx = elf_section_data (osec)->dynindx;
14136 }
14137 BFD_ASSERT (indx != 0);
14138
65f38f15
AM
14139 /* We are turning this relocation into one
14140 against a section symbol, so subtract out
14141 the output section's address but not the
14142 offset of the input section in the output
14143 section. */
14144 outrel.r_addend -= osec->vma;
14145 }
14146
14147 outrel.r_info = ELF64_R_INFO (indx, r_type);
14148 }
14149 }
14150
14151 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14152 if (h != NULL
14153 ? h->elf.type == STT_GNU_IFUNC
14154 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
25f23106 14155 sreloc = htab->reliplt;
65f38f15
AM
14156 if (sreloc == NULL)
14157 abort ();
14158
dfbb6ac9
AM
14159 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14160 >= sreloc->size)
14161 abort ();
947216bf
AM
14162 loc = sreloc->contents;
14163 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14164 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14165
14166 /* If this reloc is against an external symbol, it will
14167 be computed at runtime, so there's no need to do
81407a69
AM
14168 anything now. However, for the sake of prelink ensure
14169 that the section contents are a known value. */
65f38f15 14170 if (! relocate)
81407a69
AM
14171 {
14172 unresolved_reloc = FALSE;
14173 /* The value chosen here is quite arbitrary as ld.so
14174 ignores section contents except for the special
14175 case of .opd where the contents might be accessed
14176 before relocation. Choose zero, as that won't
14177 cause reloc overflow. */
14178 relocation = 0;
14179 addend = 0;
14180 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14181 to improve backward compatibility with older
14182 versions of ld. */
14183 if (r_type == R_PPC64_ADDR64)
14184 addend = outrel.r_addend;
14185 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14186 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14187 addend = (input_section->output_section->vma
14188 + input_section->output_offset
14189 + rel->r_offset);
81407a69 14190 }
65f38f15 14191 }
5bd4f169
AM
14192 break;
14193
65f38f15
AM
14194 case R_PPC64_COPY:
14195 case R_PPC64_GLOB_DAT:
14196 case R_PPC64_JMP_SLOT:
25f23106 14197 case R_PPC64_JMP_IREL:
65f38f15
AM
14198 case R_PPC64_RELATIVE:
14199 /* We shouldn't ever see these dynamic relocs in relocatable
14200 files. */
ae9a127f 14201 /* Fall through. */
65f38f15
AM
14202
14203 case R_PPC64_PLTGOT16:
14204 case R_PPC64_PLTGOT16_DS:
14205 case R_PPC64_PLTGOT16_HA:
14206 case R_PPC64_PLTGOT16_HI:
14207 case R_PPC64_PLTGOT16_LO:
14208 case R_PPC64_PLTGOT16_LO_DS:
14209 case R_PPC64_PLTREL32:
14210 case R_PPC64_PLTREL64:
14211 /* These ones haven't been implemented yet. */
14212
25f53a85 14213 info->callbacks->einfo
bc30df16 14214 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14215 input_bfd,
4ce794b7 14216 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14217
14218 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14219 ret = FALSE;
5bd4f169 14220 continue;
65f38f15 14221 }
5bd4f169 14222
67f0cbdb
AM
14223 /* Multi-instruction sequences that access the TOC can be
14224 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14225 to nop; addi rb,r2,x; */
14226 switch (r_type)
14227 {
14228 default:
14229 break;
14230
14231 case R_PPC64_GOT_TLSLD16_HI:
14232 case R_PPC64_GOT_TLSGD16_HI:
14233 case R_PPC64_GOT_TPREL16_HI:
14234 case R_PPC64_GOT_DTPREL16_HI:
14235 case R_PPC64_GOT16_HI:
14236 case R_PPC64_TOC16_HI:
14237 /* These relocs would only be useful if building up an
14238 offset to later add to r2, perhaps in an indexed
14239 addressing mode instruction. Don't try to optimize.
14240 Unfortunately, the possibility of someone building up an
14241 offset like this or even with the HA relocs, means that
14242 we need to check the high insn when optimizing the low
14243 insn. */
14244 break;
14245
14246 case R_PPC64_GOT_TLSLD16_HA:
14247 case R_PPC64_GOT_TLSGD16_HA:
14248 case R_PPC64_GOT_TPREL16_HA:
14249 case R_PPC64_GOT_DTPREL16_HA:
14250 case R_PPC64_GOT16_HA:
14251 case R_PPC64_TOC16_HA:
98528052 14252 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14253 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
14254 {
14255 bfd_byte *p = contents + (rel->r_offset & ~3);
14256 bfd_put_32 (input_bfd, NOP, p);
14257 }
67f0cbdb
AM
14258 break;
14259
14260 case R_PPC64_GOT_TLSLD16_LO:
14261 case R_PPC64_GOT_TLSGD16_LO:
14262 case R_PPC64_GOT_TPREL16_LO_DS:
14263 case R_PPC64_GOT_DTPREL16_LO_DS:
14264 case R_PPC64_GOT16_LO:
14265 case R_PPC64_GOT16_LO_DS:
14266 case R_PPC64_TOC16_LO:
14267 case R_PPC64_TOC16_LO_DS:
98528052 14268 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14269 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
14270 {
14271 bfd_byte *p = contents + (rel->r_offset & ~3);
14272 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
14273 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14274 {
14275 /* Transform addic to addi when we change reg. */
14276 insn &= ~((0x3f << 26) | (0x1f << 16));
14277 insn |= (14u << 26) | (2 << 16);
14278 }
14279 else
67f0cbdb 14280 {
98528052
AM
14281 insn &= ~(0x1f << 16);
14282 insn |= 2 << 16;
67f0cbdb 14283 }
560c8763 14284 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
14285 }
14286 break;
14287 }
14288
65f38f15
AM
14289 /* Do any further special processing. */
14290 switch (r_type)
14291 {
14292 default:
14293 break;
14294
25f23106 14295 case R_PPC64_REL16_HA:
f9c6b907
AM
14296 case R_PPC64_ADDR16_HA:
14297 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14298 case R_PPC64_ADDR16_HIGHERA:
14299 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
14300 case R_PPC64_TOC16_HA:
14301 case R_PPC64_SECTOFF_HA:
411e1bfb 14302 case R_PPC64_TPREL16_HA:
f9c6b907 14303 case R_PPC64_TPREL16_HIGHA:
411e1bfb 14304 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 14305 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
14306 case R_PPC64_DTPREL16_HA:
14307 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 14308 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 14309 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
14310 /* It's just possible that this symbol is a weak symbol
14311 that's not actually defined anywhere. In that case,
14312 'sec' would be NULL, and we should leave the symbol
14313 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
14314 if (sec == NULL)
14315 break;
14316 /* Fall thru */
14317
14318 case R_PPC64_GOT16_HA:
14319 case R_PPC64_PLTGOT16_HA:
14320 case R_PPC64_PLT16_HA:
14321 case R_PPC64_GOT_TLSGD16_HA:
14322 case R_PPC64_GOT_TLSLD16_HA:
14323 case R_PPC64_GOT_TPREL16_HA:
14324 case R_PPC64_GOT_DTPREL16_HA:
14325 /* Add 0x10000 if sign bit in 0:15 is set.
14326 Bits 0:15 are not used. */
14327 addend += 0x8000;
65f38f15
AM
14328 break;
14329
14330 case R_PPC64_ADDR16_DS:
14331 case R_PPC64_ADDR16_LO_DS:
14332 case R_PPC64_GOT16_DS:
14333 case R_PPC64_GOT16_LO_DS:
14334 case R_PPC64_PLT16_LO_DS:
14335 case R_PPC64_SECTOFF_DS:
14336 case R_PPC64_SECTOFF_LO_DS:
14337 case R_PPC64_TOC16_DS:
14338 case R_PPC64_TOC16_LO_DS:
14339 case R_PPC64_PLTGOT16_DS:
14340 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
14341 case R_PPC64_GOT_TPREL16_DS:
14342 case R_PPC64_GOT_TPREL16_LO_DS:
14343 case R_PPC64_GOT_DTPREL16_DS:
14344 case R_PPC64_GOT_DTPREL16_LO_DS:
14345 case R_PPC64_TPREL16_DS:
14346 case R_PPC64_TPREL16_LO_DS:
14347 case R_PPC64_DTPREL16_DS:
14348 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
14349 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14350 mask = 3;
14351 /* If this reloc is against an lq insn, then the value must be
14352 a multiple of 16. This is somewhat of a hack, but the
14353 "correct" way to do this by defining _DQ forms of all the
14354 _DS relocs bloats all reloc switches in this file. It
14355 doesn't seem to make much sense to use any of these relocs
14356 in data, so testing the insn should be safe. */
494dac0c 14357 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
14358 mask = 15;
14359 if (((relocation + addend) & mask) != 0)
65f38f15 14360 {
25f53a85 14361 info->callbacks->einfo
8de848d8 14362 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 14363 input_bfd, input_section, rel->r_offset,
4ce794b7 14364 ppc64_elf_howto_table[r_type]->name,
adadcc0c 14365 mask + 1);
65f38f15 14366 bfd_set_error (bfd_error_bad_value);
b34976b6 14367 ret = FALSE;
65f38f15
AM
14368 continue;
14369 }
14370 break;
5bd4f169
AM
14371 }
14372
239e1f3a
AM
14373 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14374 because such sections are not SEC_ALLOC and thus ld.so will
14375 not process them. */
65f38f15 14376 if (unresolved_reloc
239e1f3a 14377 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
14378 && h->elf.def_dynamic)
14379 && _bfd_elf_section_offset (output_bfd, info, input_section,
14380 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 14381 {
25f53a85 14382 info->callbacks->einfo
bc30df16 14383 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 14384 input_bfd, input_section, rel->r_offset,
7b609f53 14385 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 14386 h->elf.root.root.string);
b34976b6 14387 ret = FALSE;
9c07fe7c 14388 }
5bd4f169 14389
65f38f15 14390 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
14391 input_bfd,
14392 input_section,
14393 contents,
411e1bfb 14394 rel->r_offset,
5bd4f169
AM
14395 relocation,
14396 addend);
14397
ef60b7ff 14398 if (r != bfd_reloc_ok)
5bd4f169 14399 {
bc30df16
AM
14400 char *more_info = NULL;
14401 const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14402
14403 if (reloc_dest != DEST_NORMAL)
14404 {
14405 more_info = bfd_malloc (strlen (reloc_name) + 8);
14406 if (more_info != NULL)
14407 {
14408 strcpy (more_info, reloc_name);
14409 strcat (more_info, (reloc_dest == DEST_OPD
14410 ? " (OPD)" : " (stub)"));
14411 reloc_name = more_info;
14412 }
14413 }
14414
cd27b276 14415 if (r == bfd_reloc_overflow)
5bd4f169 14416 {
cd27b276
AM
14417 if (warned)
14418 continue;
14419 if (h != NULL
039b3fef 14420 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 14421 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
14422 {
14423 /* Assume this is a call protected by other code that
14424 detects the symbol is undefined. If this is the case,
14425 we can safely ignore the overflow. If not, the
14426 program is hosed anyway, and a little warning isn't
14427 going to help. */
14428
14429 continue;
14430 }
14431
ef60b7ff 14432 if (!((*info->callbacks->reloc_overflow)
bc30df16
AM
14433 (info, &h->elf.root, sym_name,
14434 reloc_name, orig_rel.r_addend,
14435 input_bfd, input_section, rel->r_offset)))
b34976b6 14436 return FALSE;
ef60b7ff
AM
14437 }
14438 else
14439 {
25f53a85 14440 info->callbacks->einfo
bc30df16 14441 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 14442 input_bfd, input_section, rel->r_offset,
bc30df16 14443 reloc_name, sym_name, (int) r);
b34976b6 14444 ret = FALSE;
ef60b7ff 14445 }
bc30df16
AM
14446 if (more_info != NULL)
14447 free (more_info);
5bd4f169 14448 }
5bd4f169
AM
14449 }
14450
645ea6a9
AM
14451 /* If we're emitting relocations, then shortly after this function
14452 returns, reloc offsets and addends for this section will be
14453 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
14454 file rather than the input. Save a copy of the relocs for
14455 opd_entry_value. */
4cc603a5 14456 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
14457 {
14458 bfd_size_type amt;
14459 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14460 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
14461 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14462 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
14463 if (rel == NULL)
14464 return FALSE;
14465 memcpy (rel, relocs, amt);
14466 }
5bd4f169
AM
14467 return ret;
14468}
14469
754021d0
AM
14470/* Adjust the value of any local symbols in opd sections. */
14471
6e0b88f1 14472static int
754021d0
AM
14473ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14474 const char *name ATTRIBUTE_UNUSED,
14475 Elf_Internal_Sym *elfsym,
14476 asection *input_sec,
14477 struct elf_link_hash_entry *h)
14478{
74f0fb50
AM
14479 struct _opd_sec_data *opd;
14480 long adjust;
754021d0
AM
14481 bfd_vma value;
14482
4025353c 14483 if (h != NULL)
6e0b88f1 14484 return 1;
4025353c 14485
74f0fb50
AM
14486 opd = get_opd_info (input_sec);
14487 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 14488 return 1;
754021d0
AM
14489
14490 value = elfsym->st_value - input_sec->output_offset;
14491 if (!info->relocatable)
14492 value -= input_sec->output_section->vma;
14493
74f0fb50 14494 adjust = opd->adjust[value / 8];
4025353c 14495 if (adjust == -1)
6e0b88f1
AM
14496 return 2;
14497
14498 elfsym->st_value += adjust;
14499 return 1;
754021d0
AM
14500}
14501
5bd4f169
AM
14502/* Finish up dynamic symbol handling. We set the contents of various
14503 dynamic sections here. */
14504
b34976b6 14505static bfd_boolean
4ce794b7
AM
14506ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14507 struct bfd_link_info *info,
14508 struct elf_link_hash_entry *h,
ab6dce23 14509 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 14510{
65f38f15 14511 struct ppc_link_hash_table *htab;
8387904d
AM
14512 struct plt_entry *ent;
14513 Elf_Internal_Rela rela;
14514 bfd_byte *loc;
5bd4f169 14515
65f38f15 14516 htab = ppc_hash_table (info);
4dfe6ac6
NC
14517 if (htab == NULL)
14518 return FALSE;
5bd4f169 14519
8387904d
AM
14520 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14521 if (ent->plt.offset != (bfd_vma) -1)
14522 {
14523 /* This symbol has an entry in the procedure linkage
14524 table. Set it up. */
e054468f
AM
14525 if (!htab->elf.dynamic_sections_created
14526 || h->dynindx == -1)
14527 {
14528 BFD_ASSERT (h->type == STT_GNU_IFUNC
14529 && h->def_regular
14530 && (h->root.type == bfd_link_hash_defined
14531 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
14532 rela.r_offset = (htab->iplt->output_section->vma
14533 + htab->iplt->output_offset
14534 + ent->plt.offset);
ee67d69a
AM
14535 if (htab->opd_abi)
14536 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14537 else
14538 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
14539 rela.r_addend = (h->root.u.def.value
14540 + h->root.u.def.section->output_offset
14541 + h->root.u.def.section->output_section->vma
14542 + ent->addend);
25f23106
AM
14543 loc = (htab->reliplt->contents
14544 + (htab->reliplt->reloc_count++
14545 * sizeof (Elf64_External_Rela)));
e054468f
AM
14546 }
14547 else
14548 {
25f23106
AM
14549 rela.r_offset = (htab->plt->output_section->vma
14550 + htab->plt->output_offset
14551 + ent->plt.offset);
e054468f
AM
14552 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14553 rela.r_addend = ent->addend;
25f23106 14554 loc = (htab->relplt->contents
b9e5796b
AM
14555 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14556 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 14557 }
8387904d
AM
14558 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14559 }
5bd4f169 14560
f5385ebf 14561 if (h->needs_copy)
5bd4f169 14562 {
65f38f15 14563 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 14564
65f38f15
AM
14565 if (h->dynindx == -1
14566 || (h->root.type != bfd_link_hash_defined
14567 && h->root.type != bfd_link_hash_defweak)
4ce794b7 14568 || htab->relbss == NULL)
65f38f15 14569 abort ();
5bd4f169
AM
14570
14571 rela.r_offset = (h->root.u.def.value
14572 + h->root.u.def.section->output_section->vma
14573 + h->root.u.def.section->output_offset);
14574 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14575 rela.r_addend = 0;
4ce794b7
AM
14576 loc = htab->relbss->contents;
14577 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 14578 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
14579 }
14580
b34976b6 14581 return TRUE;
5bd4f169
AM
14582}
14583
65f38f15
AM
14584/* Used to decide how to sort relocs in an optimal manner for the
14585 dynamic linker, before writing them out. */
14586
14587static enum elf_reloc_type_class
7e612e98
AM
14588ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14589 const asection *rel_sec,
14590 const Elf_Internal_Rela *rela)
65f38f15 14591{
04c9666a 14592 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
14593 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14594
14595 if (rel_sec == htab->reliplt)
14596 return reloc_class_ifunc;
a33d1f77 14597
4ce794b7 14598 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 14599 switch (r_type)
65f38f15
AM
14600 {
14601 case R_PPC64_RELATIVE:
14602 return reloc_class_relative;
14603 case R_PPC64_JMP_SLOT:
14604 return reloc_class_plt;
14605 case R_PPC64_COPY:
14606 return reloc_class_copy;
14607 default:
14608 return reloc_class_normal;
14609 }
14610}
14611
5bd4f169
AM
14612/* Finish up the dynamic sections. */
14613
b34976b6 14614static bfd_boolean
4ce794b7
AM
14615ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14616 struct bfd_link_info *info)
5bd4f169 14617{
65f38f15
AM
14618 struct ppc_link_hash_table *htab;
14619 bfd *dynobj;
5bd4f169 14620 asection *sdyn;
5bd4f169 14621
65f38f15 14622 htab = ppc_hash_table (info);
4dfe6ac6
NC
14623 if (htab == NULL)
14624 return FALSE;
14625
65f38f15 14626 dynobj = htab->elf.dynobj;
3d4d4302 14627 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 14628
65f38f15 14629 if (htab->elf.dynamic_sections_created)
5bd4f169 14630 {
5bd4f169
AM
14631 Elf64_External_Dyn *dyncon, *dynconend;
14632
4ce794b7 14633 if (sdyn == NULL || htab->got == NULL)
65f38f15 14634 abort ();
5bd4f169
AM
14635
14636 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 14637 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
14638 for (; dyncon < dynconend; dyncon++)
14639 {
14640 Elf_Internal_Dyn dyn;
19397422 14641 asection *s;
5bd4f169
AM
14642
14643 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14644
14645 switch (dyn.d_tag)
14646 {
65f38f15
AM
14647 default:
14648 continue;
5bd4f169 14649
5d1634d7 14650 case DT_PPC64_GLINK:
4ce794b7 14651 s = htab->glink;
6348e046 14652 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
14653 /* We stupidly defined DT_PPC64_GLINK to be the start
14654 of glink rather than the first entry point, which is
14655 what ld.so needs, and now have a bigger stub to
14656 support automatic multiple TOCs. */
b9e5796b 14657 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
14658 break;
14659
19397422
AM
14660 case DT_PPC64_OPD:
14661 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
14662 if (s == NULL)
14663 continue;
14664 dyn.d_un.d_ptr = s->vma;
19397422
AM
14665 break;
14666
e8910a83
AM
14667 case DT_PPC64_OPT:
14668 if (htab->do_multi_toc && htab->multi_toc_needed)
14669 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
14670 break;
14671
19397422
AM
14672 case DT_PPC64_OPDSZ:
14673 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
14674 if (s == NULL)
14675 continue;
eea6121a 14676 dyn.d_un.d_val = s->size;
19397422
AM
14677 break;
14678
65f38f15 14679 case DT_PLTGOT:
4ce794b7 14680 s = htab->plt;
6348e046 14681 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
14682 break;
14683
14684 case DT_JMPREL:
4ce794b7 14685 s = htab->relplt;
6348e046 14686 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 14687 break;
5bd4f169 14688
65f38f15 14689 case DT_PLTRELSZ:
eea6121a 14690 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
14691 break;
14692
14693 case DT_RELASZ:
14694 /* Don't count procedure linkage table relocs in the
14695 overall reloc count. */
4ce794b7 14696 s = htab->relplt;
6348e046
AM
14697 if (s == NULL)
14698 continue;
eea6121a 14699 dyn.d_un.d_val -= s->size;
6348e046
AM
14700 break;
14701
14702 case DT_RELA:
14703 /* We may not be using the standard ELF linker script.
14704 If .rela.plt is the first .rela section, we adjust
14705 DT_RELA to not include it. */
4ce794b7 14706 s = htab->relplt;
6348e046
AM
14707 if (s == NULL)
14708 continue;
14709 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14710 continue;
eea6121a 14711 dyn.d_un.d_ptr += s->size;
65f38f15 14712 break;
5bd4f169 14713 }
5bd4f169 14714
65f38f15 14715 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 14716 }
5bd4f169
AM
14717 }
14718
eea6121a 14719 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
14720 {
14721 /* Fill in the first entry in the global offset table.
14722 We use it to hold the link-time TOCbase. */
14723 bfd_put_64 (output_bfd,
60ee0d4a 14724 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 14725 htab->got->contents);
5d1634d7
AM
14726
14727 /* Set .got entry size. */
4ce794b7 14728 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
14729 }
14730
eea6121a 14731 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
14732 {
14733 /* Set .plt entry size. */
4ce794b7 14734 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
b9e5796b 14735 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
14736 }
14737
84f5d08e
AM
14738 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14739 brlt ourselves if emitrelocations. */
14740 if (htab->brlt != NULL
14741 && htab->brlt->reloc_count != 0
14742 && !_bfd_elf_link_output_relocs (output_bfd,
14743 htab->brlt,
d4730f92 14744 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
14745 elf_section_data (htab->brlt)->relocs,
14746 NULL))
14747 return FALSE;
14748
176a0d42
AM
14749 if (htab->glink != NULL
14750 && htab->glink->reloc_count != 0
14751 && !_bfd_elf_link_output_relocs (output_bfd,
14752 htab->glink,
d4730f92 14753 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
14754 elf_section_data (htab->glink)->relocs,
14755 NULL))
14756 return FALSE;
14757
58d180e8
AM
14758
14759 if (htab->glink_eh_frame != NULL
dbaa2011 14760 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
58d180e8
AM
14761 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14762 htab->glink_eh_frame,
14763 htab->glink_eh_frame->contents))
14764 return FALSE;
14765
e717da7e 14766 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
14767 since we didn't add them to DYNOBJ. We know dynobj is the first
14768 bfd. */
e717da7e
AM
14769 while ((dynobj = dynobj->link_next) != NULL)
14770 {
14771 asection *s;
7b53ace3 14772
0c8d6e5c 14773 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
14774 continue;
14775
e717da7e
AM
14776 s = ppc64_elf_tdata (dynobj)->got;
14777 if (s != NULL
eea6121a 14778 && s->size != 0
e717da7e
AM
14779 && s->output_section != bfd_abs_section_ptr
14780 && !bfd_set_section_contents (output_bfd, s->output_section,
14781 s->contents, s->output_offset,
eea6121a 14782 s->size))
e717da7e
AM
14783 return FALSE;
14784 s = ppc64_elf_tdata (dynobj)->relgot;
14785 if (s != NULL
eea6121a 14786 && s->size != 0
e717da7e
AM
14787 && s->output_section != bfd_abs_section_ptr
14788 && !bfd_set_section_contents (output_bfd, s->output_section,
14789 s->contents, s->output_offset,
eea6121a 14790 s->size))
e717da7e
AM
14791 return FALSE;
14792 }
f6c52c13 14793
b34976b6 14794 return TRUE;
5bd4f169
AM
14795}
14796
5bd4f169 14797#include "elf64-target.h"
7b8e7dad
AM
14798
14799/* FreeBSD support */
14800
14801#undef TARGET_LITTLE_SYM
14802#undef TARGET_LITTLE_NAME
14803
14804#undef TARGET_BIG_SYM
14805#define TARGET_BIG_SYM bfd_elf64_powerpc_freebsd_vec
14806#undef TARGET_BIG_NAME
14807#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14808
14809#undef ELF_OSABI
14810#define ELF_OSABI ELFOSABI_FREEBSD
14811
14812#undef elf64_bed
14813#define elf64_bed elf64_powerpc_fbsd_bed
14814
14815#include "elf64-target.h"
14816
This page took 2.158122 seconds and 4 git commands to generate.