2012-01-16 Sergio Durigan Junior <sergiodj@redhat.com>
[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
AM
57static bfd_vma opd_entry_value
58 (asection *, bfd_vma, asection **, bfd_vma *);
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
c79d6685 84#define bfd_elf64_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
ad8e1ba5
AM
85#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
86#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
87#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 88#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 89#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
90
91#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
92#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
93#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 94#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
95#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
96#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 97#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 98#define elf_backend_check_directives ppc64_elf_process_dot_syms
97fed1c9 99#define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
8387904d 100#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 101#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 102#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 103#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
104#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
105#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
106#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
107#define elf_backend_hide_symbol ppc64_elf_hide_symbol
108#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
109#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 110#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 111#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
112#define elf_backend_relocate_section ppc64_elf_relocate_section
113#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
114#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
115#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 116#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 117#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 118#define elf_backend_post_process_headers _bfd_elf_set_osabi
ad8e1ba5 119
5bd4f169
AM
120/* The name of the dynamic interpreter. This is put in the .interp
121 section. */
122#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
123
124/* The size in bytes of an entry in the procedure linkage table. */
125#define PLT_ENTRY_SIZE 24
126
127/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 128#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
129
130/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
131#define TOC_BASE_OFF 0x8000
132
133/* Offset of tp and dtp pointers from start of TLS block. */
134#define TP_OFFSET 0x7000
135#define DTP_OFFSET 0x8000
5bd4f169 136
ad8e1ba5
AM
137/* .plt call stub instructions. The normal stub is like this, but
138 sometimes the .plt entry crosses a 64k boundary and we need to
ac2df442 139 insert an addi to adjust r12. */
ad8e1ba5 140#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7
AM
141#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
142#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
143#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
5d1634d7 144#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
ac2df442 145#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
5d1634d7
AM
146 /* ld %r11,xxx+16@l(%r12) */
147#define BCTR 0x4e800420 /* bctr */
148
5d1634d7 149
ee4bf8d2 150#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
ac2df442 151#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
ad8e1ba5
AM
152#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
153#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
154
794e51c0
AM
155#define XOR_R11_R11_R11 0x7d6b5a78 /* xor %r11,%r11,%r11 */
156#define ADD_R12_R12_R11 0x7d8c5a14 /* add %r12,%r12,%r11 */
157#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
158#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
159#define BNECTR 0x4ca20420 /* bnectr+ */
160#define BNECTR_P4 0x4ce20420 /* bnectr+ */
161
ac2df442
AM
162#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
163#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
164
ad8e1ba5
AM
165#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
166
ee4bf8d2 167/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 168#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
169 /* 0: */
170 /* .quad plt0-1f */
171 /* __glink: */
172#define MFLR_R12 0x7d8802a6 /* mflr %12 */
173#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
174 /* 1: */
175#define MFLR_R11 0x7d6802a6 /* mflr %11 */
176#define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
177#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
178#define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
179 /* ld %11,0(%12) */
180 /* ld %2,8(%12) */
181 /* mtctr %11 */
182 /* ld %11,16(%12) */
183 /* bctr */
5d1634d7
AM
184
185/* Pad with this. */
186#define NOP 0x60000000
187
721956f4
AM
188/* Some other nops. */
189#define CROR_151515 0x4def7b82
190#define CROR_313131 0x4ffffb82
191
cedb70c5 192/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
193#define LI_R0_0 0x38000000 /* li %r0,0 */
194#define B_DOT 0x48000000 /* b . */
195
196/* After that, we need two instructions to load the index, followed by
197 a branch. */
198#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 199#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 200
deb0e272
AM
201/* Instructions used by the save and restore reg functions. */
202#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
203#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
204#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
205#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
206#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
207#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
208#define LI_R12_0 0x39800000 /* li %r12,0 */
209#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
210#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
211#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
212#define BLR 0x4e800020 /* blr */
213
41bd81ab
AM
214/* Since .opd is an array of descriptors and each entry will end up
215 with identical R_PPC64_RELATIVE relocs, there is really no need to
216 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 217 relocate .opd without reloc entries. */
41bd81ab
AM
218#ifndef NO_OPD_RELOCS
219#define NO_OPD_RELOCS 0
220#endif
5bd4f169 221\f
f5e87a1d 222#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 223
5bd4f169 224/* Relocation HOWTO's. */
04c9666a 225static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
226
227static reloc_howto_type ppc64_elf_howto_raw[] = {
228 /* This reloc does nothing. */
229 HOWTO (R_PPC64_NONE, /* type */
230 0, /* rightshift */
411e1bfb
AM
231 2, /* size (0 = byte, 1 = short, 2 = long) */
232 32, /* bitsize */
b34976b6 233 FALSE, /* pc_relative */
5bd4f169 234 0, /* bitpos */
f5e87a1d 235 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
236 bfd_elf_generic_reloc, /* special_function */
237 "R_PPC64_NONE", /* name */
b34976b6 238 FALSE, /* partial_inplace */
d006db6c 239 0, /* src_mask */
5bd4f169 240 0, /* dst_mask */
b34976b6 241 FALSE), /* pcrel_offset */
5bd4f169
AM
242
243 /* A standard 32 bit relocation. */
244 HOWTO (R_PPC64_ADDR32, /* type */
245 0, /* rightshift */
246 2, /* size (0 = byte, 1 = short, 2 = long) */
247 32, /* bitsize */
b34976b6 248 FALSE, /* pc_relative */
5bd4f169
AM
249 0, /* bitpos */
250 complain_overflow_bitfield, /* complain_on_overflow */
251 bfd_elf_generic_reloc, /* special_function */
252 "R_PPC64_ADDR32", /* name */
b34976b6 253 FALSE, /* partial_inplace */
5bd4f169
AM
254 0, /* src_mask */
255 0xffffffff, /* dst_mask */
b34976b6 256 FALSE), /* pcrel_offset */
5bd4f169
AM
257
258 /* An absolute 26 bit branch; the lower two bits must be zero.
259 FIXME: we don't check that, we just clear them. */
260 HOWTO (R_PPC64_ADDR24, /* type */
261 0, /* rightshift */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
263 26, /* bitsize */
b34976b6 264 FALSE, /* pc_relative */
5bd4f169
AM
265 0, /* bitpos */
266 complain_overflow_bitfield, /* complain_on_overflow */
267 bfd_elf_generic_reloc, /* special_function */
268 "R_PPC64_ADDR24", /* name */
b34976b6 269 FALSE, /* partial_inplace */
d006db6c 270 0, /* src_mask */
f5e87a1d 271 0x03fffffc, /* dst_mask */
b34976b6 272 FALSE), /* pcrel_offset */
5bd4f169
AM
273
274 /* A standard 16 bit relocation. */
275 HOWTO (R_PPC64_ADDR16, /* type */
276 0, /* rightshift */
277 1, /* size (0 = byte, 1 = short, 2 = long) */
278 16, /* 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_ADDR16", /* name */
b34976b6 284 FALSE, /* partial_inplace */
5bd4f169
AM
285 0, /* src_mask */
286 0xffff, /* dst_mask */
b34976b6 287 FALSE), /* pcrel_offset */
5bd4f169
AM
288
289 /* A 16 bit relocation without overflow. */
290 HOWTO (R_PPC64_ADDR16_LO, /* 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_dont,/* complain_on_overflow */
297 bfd_elf_generic_reloc, /* special_function */
298 "R_PPC64_ADDR16_LO", /* 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 /* Bits 16-31 of an address. */
305 HOWTO (R_PPC64_ADDR16_HI, /* type */
306 16, /* 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_HI", /* 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, plus 1 if the contents of the low 16
320 bits, treated as a signed number, is negative. */
321 HOWTO (R_PPC64_ADDR16_HA, /* type */
322 16, /* rightshift */
323 1, /* size (0 = byte, 1 = short, 2 = long) */
324 16, /* bitsize */
b34976b6 325 FALSE, /* pc_relative */
5bd4f169
AM
326 0, /* bitpos */
327 complain_overflow_dont, /* complain_on_overflow */
805fc799 328 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 329 "R_PPC64_ADDR16_HA", /* name */
b34976b6 330 FALSE, /* partial_inplace */
5bd4f169
AM
331 0, /* src_mask */
332 0xffff, /* dst_mask */
b34976b6 333 FALSE), /* pcrel_offset */
5bd4f169
AM
334
335 /* An absolute 16 bit branch; the lower two bits must be zero.
336 FIXME: we don't check that, we just clear them. */
337 HOWTO (R_PPC64_ADDR14, /* type */
338 0, /* rightshift */
339 2, /* size (0 = byte, 1 = short, 2 = long) */
340 16, /* bitsize */
b34976b6 341 FALSE, /* pc_relative */
5bd4f169
AM
342 0, /* bitpos */
343 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 344 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 345 "R_PPC64_ADDR14", /* name */
b34976b6 346 FALSE, /* partial_inplace */
d006db6c 347 0, /* src_mask */
f5e87a1d 348 0x0000fffc, /* dst_mask */
b34976b6 349 FALSE), /* pcrel_offset */
5bd4f169
AM
350
351 /* An absolute 16 bit branch, for which bit 10 should be set to
352 indicate that the branch is expected to be taken. The lower two
353 bits must be zero. */
354 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
355 0, /* rightshift */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
357 16, /* bitsize */
b34976b6 358 FALSE, /* pc_relative */
5bd4f169
AM
359 0, /* bitpos */
360 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 361 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 362 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 363 FALSE, /* partial_inplace */
d006db6c 364 0, /* src_mask */
f5e87a1d 365 0x0000fffc, /* dst_mask */
b34976b6 366 FALSE), /* pcrel_offset */
5bd4f169
AM
367
368 /* An absolute 16 bit branch, for which bit 10 should be set to
369 indicate that the branch is not expected to be taken. The lower
370 two bits must be zero. */
371 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
372 0, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 16, /* bitsize */
b34976b6 375 FALSE, /* pc_relative */
5bd4f169
AM
376 0, /* bitpos */
377 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 378 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 379 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 380 FALSE, /* partial_inplace */
d006db6c 381 0, /* src_mask */
f5e87a1d 382 0x0000fffc, /* dst_mask */
b34976b6 383 FALSE), /* pcrel_offset */
5bd4f169
AM
384
385 /* A relative 26 bit branch; the lower two bits must be zero. */
386 HOWTO (R_PPC64_REL24, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 26, /* bitsize */
b34976b6 390 TRUE, /* pc_relative */
5bd4f169
AM
391 0, /* bitpos */
392 complain_overflow_signed, /* complain_on_overflow */
2441e016 393 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 394 "R_PPC64_REL24", /* name */
b34976b6 395 FALSE, /* partial_inplace */
d006db6c 396 0, /* src_mask */
f5e87a1d 397 0x03fffffc, /* dst_mask */
b34976b6 398 TRUE), /* pcrel_offset */
5bd4f169
AM
399
400 /* A relative 16 bit branch; the lower two bits must be zero. */
401 HOWTO (R_PPC64_REL14, /* type */
402 0, /* rightshift */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* 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_REL14", /* name */
b34976b6 410 FALSE, /* partial_inplace */
d006db6c 411 0, /* src_mask */
f5e87a1d 412 0x0000fffc, /* dst_mask */
b34976b6 413 TRUE), /* pcrel_offset */
5bd4f169
AM
414
415 /* A relative 16 bit branch. Bit 10 should be set to indicate that
416 the branch is expected to be taken. The lower two bits must be
417 zero. */
418 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
419 0, /* rightshift */
420 2, /* size (0 = byte, 1 = short, 2 = long) */
421 16, /* bitsize */
b34976b6 422 TRUE, /* pc_relative */
5bd4f169
AM
423 0, /* bitpos */
424 complain_overflow_signed, /* complain_on_overflow */
805fc799 425 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 426 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 427 FALSE, /* partial_inplace */
d006db6c 428 0, /* src_mask */
f5e87a1d 429 0x0000fffc, /* dst_mask */
b34976b6 430 TRUE), /* pcrel_offset */
5bd4f169
AM
431
432 /* A relative 16 bit branch. Bit 10 should be set to indicate that
433 the branch is not expected to be taken. The lower two bits must
434 be zero. */
435 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
436 0, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 16, /* bitsize */
b34976b6 439 TRUE, /* pc_relative */
5bd4f169
AM
440 0, /* bitpos */
441 complain_overflow_signed, /* complain_on_overflow */
805fc799 442 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 443 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 444 FALSE, /* partial_inplace */
d006db6c 445 0, /* src_mask */
f5e87a1d 446 0x0000fffc, /* dst_mask */
b34976b6 447 TRUE), /* pcrel_offset */
5bd4f169
AM
448
449 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
450 symbol. */
451 HOWTO (R_PPC64_GOT16, /* type */
452 0, /* rightshift */
453 1, /* size (0 = byte, 1 = short, 2 = long) */
454 16, /* bitsize */
b34976b6 455 FALSE, /* pc_relative */
5bd4f169
AM
456 0, /* bitpos */
457 complain_overflow_signed, /* complain_on_overflow */
805fc799 458 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 459 "R_PPC64_GOT16", /* name */
b34976b6 460 FALSE, /* partial_inplace */
5bd4f169
AM
461 0, /* src_mask */
462 0xffff, /* dst_mask */
b34976b6 463 FALSE), /* pcrel_offset */
5bd4f169
AM
464
465 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
466 the symbol. */
467 HOWTO (R_PPC64_GOT16_LO, /* type */
468 0, /* rightshift */
469 1, /* size (0 = byte, 1 = short, 2 = long) */
470 16, /* bitsize */
b34976b6 471 FALSE, /* pc_relative */
5bd4f169
AM
472 0, /* bitpos */
473 complain_overflow_dont, /* complain_on_overflow */
805fc799 474 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 475 "R_PPC64_GOT16_LO", /* name */
b34976b6 476 FALSE, /* partial_inplace */
5bd4f169
AM
477 0, /* src_mask */
478 0xffff, /* dst_mask */
b34976b6 479 FALSE), /* pcrel_offset */
5bd4f169
AM
480
481 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
482 the symbol. */
483 HOWTO (R_PPC64_GOT16_HI, /* type */
484 16, /* rightshift */
485 1, /* size (0 = byte, 1 = short, 2 = long) */
486 16, /* bitsize */
b34976b6 487 FALSE, /* pc_relative */
5bd4f169
AM
488 0, /* bitpos */
489 complain_overflow_dont,/* complain_on_overflow */
805fc799 490 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 491 "R_PPC64_GOT16_HI", /* name */
b34976b6 492 FALSE, /* partial_inplace */
5bd4f169
AM
493 0, /* src_mask */
494 0xffff, /* dst_mask */
b34976b6 495 FALSE), /* pcrel_offset */
5bd4f169
AM
496
497 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
498 the symbol. */
499 HOWTO (R_PPC64_GOT16_HA, /* type */
500 16, /* rightshift */
501 1, /* size (0 = byte, 1 = short, 2 = long) */
502 16, /* bitsize */
b34976b6 503 FALSE, /* pc_relative */
5bd4f169
AM
504 0, /* bitpos */
505 complain_overflow_dont,/* complain_on_overflow */
805fc799 506 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 507 "R_PPC64_GOT16_HA", /* name */
b34976b6 508 FALSE, /* partial_inplace */
5bd4f169
AM
509 0, /* src_mask */
510 0xffff, /* dst_mask */
b34976b6 511 FALSE), /* pcrel_offset */
5bd4f169
AM
512
513 /* This is used only by the dynamic linker. The symbol should exist
514 both in the object being run and in some shared library. The
515 dynamic linker copies the data addressed by the symbol from the
516 shared library into the object, because the object being
517 run has to have the data at some particular address. */
518 HOWTO (R_PPC64_COPY, /* type */
519 0, /* rightshift */
f5e87a1d
AM
520 0, /* this one is variable size */
521 0, /* bitsize */
b34976b6 522 FALSE, /* pc_relative */
5bd4f169 523 0, /* bitpos */
f5e87a1d
AM
524 complain_overflow_dont, /* complain_on_overflow */
525 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 526 "R_PPC64_COPY", /* name */
b34976b6 527 FALSE, /* partial_inplace */
5bd4f169
AM
528 0, /* src_mask */
529 0, /* dst_mask */
b34976b6 530 FALSE), /* pcrel_offset */
5bd4f169
AM
531
532 /* Like R_PPC64_ADDR64, but used when setting global offset table
533 entries. */
534 HOWTO (R_PPC64_GLOB_DAT, /* type */
535 0, /* rightshift */
536 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
537 64, /* bitsize */
b34976b6 538 FALSE, /* pc_relative */
5bd4f169
AM
539 0, /* bitpos */
540 complain_overflow_dont, /* complain_on_overflow */
805fc799 541 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 542 "R_PPC64_GLOB_DAT", /* name */
b34976b6 543 FALSE, /* partial_inplace */
5bd4f169 544 0, /* src_mask */
f5e87a1d 545 ONES (64), /* dst_mask */
b34976b6 546 FALSE), /* pcrel_offset */
5bd4f169
AM
547
548 /* Created by the link editor. Marks a procedure linkage table
549 entry for a symbol. */
550 HOWTO (R_PPC64_JMP_SLOT, /* type */
551 0, /* rightshift */
552 0, /* size (0 = byte, 1 = short, 2 = long) */
553 0, /* bitsize */
b34976b6 554 FALSE, /* pc_relative */
5bd4f169
AM
555 0, /* bitpos */
556 complain_overflow_dont, /* complain_on_overflow */
805fc799 557 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 558 "R_PPC64_JMP_SLOT", /* name */
b34976b6 559 FALSE, /* partial_inplace */
5bd4f169
AM
560 0, /* src_mask */
561 0, /* dst_mask */
b34976b6 562 FALSE), /* pcrel_offset */
5bd4f169
AM
563
564 /* Used only by the dynamic linker. When the object is run, this
565 doubleword64 is set to the load address of the object, plus the
566 addend. */
567 HOWTO (R_PPC64_RELATIVE, /* type */
568 0, /* rightshift */
569 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
570 64, /* bitsize */
b34976b6 571 FALSE, /* pc_relative */
5bd4f169
AM
572 0, /* bitpos */
573 complain_overflow_dont, /* complain_on_overflow */
574 bfd_elf_generic_reloc, /* special_function */
575 "R_PPC64_RELATIVE", /* name */
b34976b6 576 FALSE, /* partial_inplace */
5bd4f169 577 0, /* src_mask */
f5e87a1d 578 ONES (64), /* dst_mask */
b34976b6 579 FALSE), /* pcrel_offset */
5bd4f169
AM
580
581 /* Like R_PPC64_ADDR32, but may be unaligned. */
582 HOWTO (R_PPC64_UADDR32, /* type */
583 0, /* rightshift */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
585 32, /* bitsize */
b34976b6 586 FALSE, /* pc_relative */
5bd4f169
AM
587 0, /* bitpos */
588 complain_overflow_bitfield, /* complain_on_overflow */
589 bfd_elf_generic_reloc, /* special_function */
590 "R_PPC64_UADDR32", /* name */
b34976b6 591 FALSE, /* partial_inplace */
5bd4f169
AM
592 0, /* src_mask */
593 0xffffffff, /* dst_mask */
b34976b6 594 FALSE), /* pcrel_offset */
5bd4f169
AM
595
596 /* Like R_PPC64_ADDR16, but may be unaligned. */
597 HOWTO (R_PPC64_UADDR16, /* type */
598 0, /* rightshift */
599 1, /* size (0 = byte, 1 = short, 2 = long) */
600 16, /* 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_UADDR16", /* name */
b34976b6 606 FALSE, /* partial_inplace */
5bd4f169
AM
607 0, /* src_mask */
608 0xffff, /* dst_mask */
b34976b6 609 FALSE), /* pcrel_offset */
5bd4f169
AM
610
611 /* 32-bit PC relative. */
612 HOWTO (R_PPC64_REL32, /* type */
613 0, /* rightshift */
614 2, /* size (0 = byte, 1 = short, 2 = long) */
615 32, /* bitsize */
b34976b6 616 TRUE, /* pc_relative */
5bd4f169 617 0, /* bitpos */
cedb70c5 618 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
619 complain_overflow_signed, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_PPC64_REL32", /* name */
b34976b6 622 FALSE, /* partial_inplace */
5bd4f169
AM
623 0, /* src_mask */
624 0xffffffff, /* dst_mask */
b34976b6 625 TRUE), /* pcrel_offset */
5bd4f169 626
10ed1bba 627 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
628 HOWTO (R_PPC64_PLT32, /* type */
629 0, /* rightshift */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
631 32, /* bitsize */
b34976b6 632 FALSE, /* pc_relative */
5bd4f169
AM
633 0, /* bitpos */
634 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 635 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 636 "R_PPC64_PLT32", /* name */
b34976b6 637 FALSE, /* partial_inplace */
5bd4f169 638 0, /* src_mask */
f5e87a1d 639 0xffffffff, /* dst_mask */
b34976b6 640 FALSE), /* pcrel_offset */
5bd4f169
AM
641
642 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
643 FIXME: R_PPC64_PLTREL32 not supported. */
644 HOWTO (R_PPC64_PLTREL32, /* type */
645 0, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 32, /* bitsize */
b34976b6 648 TRUE, /* pc_relative */
5bd4f169
AM
649 0, /* bitpos */
650 complain_overflow_signed, /* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_PPC64_PLTREL32", /* name */
b34976b6 653 FALSE, /* partial_inplace */
5bd4f169 654 0, /* src_mask */
f5e87a1d 655 0xffffffff, /* dst_mask */
b34976b6 656 TRUE), /* pcrel_offset */
5bd4f169
AM
657
658 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
659 the symbol. */
660 HOWTO (R_PPC64_PLT16_LO, /* type */
661 0, /* rightshift */
662 1, /* size (0 = byte, 1 = short, 2 = long) */
663 16, /* bitsize */
b34976b6 664 FALSE, /* pc_relative */
5bd4f169
AM
665 0, /* bitpos */
666 complain_overflow_dont, /* complain_on_overflow */
805fc799 667 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 668 "R_PPC64_PLT16_LO", /* name */
b34976b6 669 FALSE, /* partial_inplace */
5bd4f169
AM
670 0, /* src_mask */
671 0xffff, /* dst_mask */
b34976b6 672 FALSE), /* pcrel_offset */
5bd4f169
AM
673
674 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
675 the symbol. */
676 HOWTO (R_PPC64_PLT16_HI, /* type */
677 16, /* rightshift */
678 1, /* size (0 = byte, 1 = short, 2 = long) */
679 16, /* bitsize */
b34976b6 680 FALSE, /* pc_relative */
5bd4f169
AM
681 0, /* bitpos */
682 complain_overflow_dont, /* complain_on_overflow */
805fc799 683 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 684 "R_PPC64_PLT16_HI", /* name */
b34976b6 685 FALSE, /* partial_inplace */
5bd4f169
AM
686 0, /* src_mask */
687 0xffff, /* dst_mask */
b34976b6 688 FALSE), /* pcrel_offset */
5bd4f169
AM
689
690 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
691 the symbol. */
692 HOWTO (R_PPC64_PLT16_HA, /* type */
693 16, /* rightshift */
694 1, /* size (0 = byte, 1 = short, 2 = long) */
695 16, /* bitsize */
b34976b6 696 FALSE, /* pc_relative */
5bd4f169
AM
697 0, /* bitpos */
698 complain_overflow_dont, /* complain_on_overflow */
805fc799 699 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 700 "R_PPC64_PLT16_HA", /* name */
b34976b6 701 FALSE, /* partial_inplace */
5bd4f169
AM
702 0, /* src_mask */
703 0xffff, /* dst_mask */
b34976b6 704 FALSE), /* pcrel_offset */
5bd4f169 705
c061c2d8 706 /* 16-bit section relative relocation. */
5bd4f169
AM
707 HOWTO (R_PPC64_SECTOFF, /* type */
708 0, /* rightshift */
c061c2d8
AM
709 1, /* size (0 = byte, 1 = short, 2 = long) */
710 16, /* bitsize */
b34976b6 711 FALSE, /* pc_relative */
5bd4f169
AM
712 0, /* bitpos */
713 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 714 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 715 "R_PPC64_SECTOFF", /* name */
b34976b6 716 FALSE, /* partial_inplace */
5bd4f169 717 0, /* src_mask */
c061c2d8 718 0xffff, /* dst_mask */
b34976b6 719 FALSE), /* pcrel_offset */
5bd4f169 720
c061c2d8 721 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
722 HOWTO (R_PPC64_SECTOFF_LO, /* type */
723 0, /* rightshift */
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_dont, /* complain_on_overflow */
805fc799 729 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 730 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 731 FALSE, /* partial_inplace */
5bd4f169
AM
732 0, /* src_mask */
733 0xffff, /* dst_mask */
b34976b6 734 FALSE), /* pcrel_offset */
5bd4f169
AM
735
736 /* 16-bit upper half section relative relocation. */
737 HOWTO (R_PPC64_SECTOFF_HI, /* type */
738 16, /* 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_HI", /* 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 adjusted section relative relocation. */
752 HOWTO (R_PPC64_SECTOFF_HA, /* type */
753 16, /* rightshift */
754 1, /* size (0 = byte, 1 = short, 2 = long) */
755 16, /* bitsize */
b34976b6 756 FALSE, /* pc_relative */
5bd4f169
AM
757 0, /* bitpos */
758 complain_overflow_dont, /* complain_on_overflow */
805fc799 759 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 760 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 761 FALSE, /* partial_inplace */
5bd4f169
AM
762 0, /* src_mask */
763 0xffff, /* dst_mask */
b34976b6 764 FALSE), /* pcrel_offset */
5bd4f169 765
04c9666a
AM
766 /* Like R_PPC64_REL24 without touching the two least significant bits. */
767 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
768 2, /* rightshift */
769 2, /* size (0 = byte, 1 = short, 2 = long) */
770 30, /* bitsize */
b34976b6 771 TRUE, /* pc_relative */
5bd4f169
AM
772 0, /* bitpos */
773 complain_overflow_dont, /* complain_on_overflow */
774 bfd_elf_generic_reloc, /* special_function */
04c9666a 775 "R_PPC64_REL30", /* name */
b34976b6 776 FALSE, /* partial_inplace */
d006db6c 777 0, /* src_mask */
5bd4f169 778 0xfffffffc, /* dst_mask */
b34976b6 779 TRUE), /* pcrel_offset */
5bd4f169
AM
780
781 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
782
783 /* A standard 64-bit relocation. */
784 HOWTO (R_PPC64_ADDR64, /* type */
785 0, /* rightshift */
786 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
787 64, /* bitsize */
b34976b6 788 FALSE, /* pc_relative */
5bd4f169
AM
789 0, /* bitpos */
790 complain_overflow_dont, /* complain_on_overflow */
791 bfd_elf_generic_reloc, /* special_function */
792 "R_PPC64_ADDR64", /* name */
b34976b6 793 FALSE, /* partial_inplace */
5bd4f169 794 0, /* src_mask */
f5e87a1d 795 ONES (64), /* dst_mask */
b34976b6 796 FALSE), /* pcrel_offset */
5bd4f169
AM
797
798 /* The bits 32-47 of an address. */
799 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
800 32, /* rightshift */
801 1, /* size (0 = byte, 1 = short, 2 = long) */
802 16, /* 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_ADDR16_HIGHER", /* name */
b34976b6 808 FALSE, /* partial_inplace */
5bd4f169
AM
809 0, /* src_mask */
810 0xffff, /* dst_mask */
b34976b6 811 FALSE), /* pcrel_offset */
5bd4f169
AM
812
813 /* The bits 32-47 of an address, plus 1 if the contents of the low
814 16 bits, treated as a signed number, is negative. */
815 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
816 32, /* rightshift */
817 1, /* size (0 = byte, 1 = short, 2 = long) */
818 16, /* bitsize */
b34976b6 819 FALSE, /* pc_relative */
5bd4f169
AM
820 0, /* bitpos */
821 complain_overflow_dont, /* complain_on_overflow */
805fc799 822 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 823 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 824 FALSE, /* partial_inplace */
5bd4f169
AM
825 0, /* src_mask */
826 0xffff, /* dst_mask */
b34976b6 827 FALSE), /* pcrel_offset */
5bd4f169
AM
828
829 /* The bits 48-63 of an address. */
830 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
831 48, /* rightshift */
832 1, /* size (0 = byte, 1 = short, 2 = long) */
833 16, /* bitsize */
b34976b6 834 FALSE, /* pc_relative */
5bd4f169
AM
835 0, /* bitpos */
836 complain_overflow_dont, /* complain_on_overflow */
837 bfd_elf_generic_reloc, /* special_function */
838 "R_PPC64_ADDR16_HIGHEST", /* 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, plus 1 if the contents of the low
845 16 bits, treated as a signed number, is negative. */
846 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
847 48, /* rightshift */
848 1, /* size (0 = byte, 1 = short, 2 = long) */
849 16, /* bitsize */
b34976b6 850 FALSE, /* pc_relative */
5bd4f169
AM
851 0, /* bitpos */
852 complain_overflow_dont, /* complain_on_overflow */
805fc799 853 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 854 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 855 FALSE, /* partial_inplace */
5bd4f169
AM
856 0, /* src_mask */
857 0xffff, /* dst_mask */
b34976b6 858 FALSE), /* pcrel_offset */
5bd4f169
AM
859
860 /* Like ADDR64, but may be unaligned. */
861 HOWTO (R_PPC64_UADDR64, /* type */
862 0, /* rightshift */
863 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
864 64, /* bitsize */
b34976b6 865 FALSE, /* pc_relative */
5bd4f169
AM
866 0, /* bitpos */
867 complain_overflow_dont, /* complain_on_overflow */
868 bfd_elf_generic_reloc, /* special_function */
869 "R_PPC64_UADDR64", /* name */
b34976b6 870 FALSE, /* partial_inplace */
5bd4f169 871 0, /* src_mask */
f5e87a1d 872 ONES (64), /* dst_mask */
b34976b6 873 FALSE), /* pcrel_offset */
5bd4f169
AM
874
875 /* 64-bit relative relocation. */
876 HOWTO (R_PPC64_REL64, /* type */
877 0, /* rightshift */
878 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
879 64, /* bitsize */
b34976b6 880 TRUE, /* pc_relative */
5bd4f169
AM
881 0, /* bitpos */
882 complain_overflow_dont, /* complain_on_overflow */
883 bfd_elf_generic_reloc, /* special_function */
884 "R_PPC64_REL64", /* name */
b34976b6 885 FALSE, /* partial_inplace */
5bd4f169 886 0, /* src_mask */
f5e87a1d 887 ONES (64), /* dst_mask */
b34976b6 888 TRUE), /* pcrel_offset */
5bd4f169 889
cedb70c5 890 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
891 HOWTO (R_PPC64_PLT64, /* type */
892 0, /* rightshift */
893 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
894 64, /* bitsize */
b34976b6 895 FALSE, /* pc_relative */
5bd4f169
AM
896 0, /* bitpos */
897 complain_overflow_dont, /* complain_on_overflow */
805fc799 898 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 899 "R_PPC64_PLT64", /* name */
b34976b6 900 FALSE, /* partial_inplace */
5bd4f169 901 0, /* src_mask */
f5e87a1d 902 ONES (64), /* dst_mask */
b34976b6 903 FALSE), /* pcrel_offset */
5bd4f169
AM
904
905 /* 64-bit PC relative relocation to the symbol's procedure linkage
906 table. */
907 /* FIXME: R_PPC64_PLTREL64 not supported. */
908 HOWTO (R_PPC64_PLTREL64, /* type */
909 0, /* rightshift */
910 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
911 64, /* bitsize */
b34976b6 912 TRUE, /* pc_relative */
5bd4f169
AM
913 0, /* bitpos */
914 complain_overflow_dont, /* complain_on_overflow */
805fc799 915 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 916 "R_PPC64_PLTREL64", /* name */
b34976b6 917 FALSE, /* partial_inplace */
5bd4f169 918 0, /* src_mask */
f5e87a1d 919 ONES (64), /* dst_mask */
b34976b6 920 TRUE), /* pcrel_offset */
5bd4f169
AM
921
922 /* 16 bit TOC-relative relocation. */
923
924 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
925 HOWTO (R_PPC64_TOC16, /* type */
926 0, /* rightshift */
927 1, /* size (0 = byte, 1 = short, 2 = long) */
928 16, /* bitsize */
b34976b6 929 FALSE, /* pc_relative */
5bd4f169
AM
930 0, /* bitpos */
931 complain_overflow_signed, /* complain_on_overflow */
805fc799 932 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 933 "R_PPC64_TOC16", /* name */
b34976b6 934 FALSE, /* partial_inplace */
5bd4f169
AM
935 0, /* src_mask */
936 0xffff, /* dst_mask */
b34976b6 937 FALSE), /* pcrel_offset */
5bd4f169
AM
938
939 /* 16 bit TOC-relative relocation without overflow. */
940
941 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
942 HOWTO (R_PPC64_TOC16_LO, /* type */
943 0, /* rightshift */
944 1, /* size (0 = byte, 1 = short, 2 = long) */
945 16, /* bitsize */
b34976b6 946 FALSE, /* pc_relative */
5bd4f169
AM
947 0, /* bitpos */
948 complain_overflow_dont, /* complain_on_overflow */
805fc799 949 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 950 "R_PPC64_TOC16_LO", /* name */
b34976b6 951 FALSE, /* partial_inplace */
5bd4f169
AM
952 0, /* src_mask */
953 0xffff, /* dst_mask */
b34976b6 954 FALSE), /* pcrel_offset */
5bd4f169
AM
955
956 /* 16 bit TOC-relative relocation, high 16 bits. */
957
958 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
959 HOWTO (R_PPC64_TOC16_HI, /* type */
960 16, /* rightshift */
961 1, /* size (0 = byte, 1 = short, 2 = long) */
962 16, /* bitsize */
b34976b6 963 FALSE, /* pc_relative */
5bd4f169
AM
964 0, /* bitpos */
965 complain_overflow_dont, /* complain_on_overflow */
805fc799 966 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 967 "R_PPC64_TOC16_HI", /* name */
b34976b6 968 FALSE, /* partial_inplace */
5bd4f169
AM
969 0, /* src_mask */
970 0xffff, /* dst_mask */
b34976b6 971 FALSE), /* pcrel_offset */
5bd4f169
AM
972
973 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
974 contents of the low 16 bits, treated as a signed number, is
975 negative. */
976
977 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
978 HOWTO (R_PPC64_TOC16_HA, /* type */
979 16, /* rightshift */
980 1, /* size (0 = byte, 1 = short, 2 = long) */
981 16, /* bitsize */
b34976b6 982 FALSE, /* pc_relative */
5bd4f169
AM
983 0, /* bitpos */
984 complain_overflow_dont, /* complain_on_overflow */
805fc799 985 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 986 "R_PPC64_TOC16_HA", /* name */
b34976b6 987 FALSE, /* partial_inplace */
5bd4f169
AM
988 0, /* src_mask */
989 0xffff, /* dst_mask */
b34976b6 990 FALSE), /* pcrel_offset */
5bd4f169
AM
991
992 /* 64-bit relocation; insert value of TOC base (.TOC.). */
993
994 /* R_PPC64_TOC 51 doubleword64 .TOC. */
995 HOWTO (R_PPC64_TOC, /* type */
996 0, /* rightshift */
997 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
998 64, /* bitsize */
b34976b6 999 FALSE, /* pc_relative */
5bd4f169
AM
1000 0, /* bitpos */
1001 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 1002 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1003 "R_PPC64_TOC", /* name */
b34976b6 1004 FALSE, /* partial_inplace */
5bd4f169 1005 0, /* src_mask */
f5e87a1d 1006 ONES (64), /* dst_mask */
b34976b6 1007 FALSE), /* pcrel_offset */
5bd4f169
AM
1008
1009 /* Like R_PPC64_GOT16, but also informs the link editor that the
1010 value to relocate may (!) refer to a PLT entry which the link
1011 editor (a) may replace with the symbol value. If the link editor
1012 is unable to fully resolve the symbol, it may (b) create a PLT
1013 entry and store the address to the new PLT entry in the GOT.
1014 This permits lazy resolution of function symbols at run time.
1015 The link editor may also skip all of this and just (c) emit a
1016 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1017 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1018 HOWTO (R_PPC64_PLTGOT16, /* type */
1019 0, /* rightshift */
1020 1, /* size (0 = byte, 1 = short, 2 = long) */
1021 16, /* bitsize */
b34976b6 1022 FALSE, /* pc_relative */
5bd4f169
AM
1023 0, /* bitpos */
1024 complain_overflow_signed, /* complain_on_overflow */
805fc799 1025 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1026 "R_PPC64_PLTGOT16", /* name */
1027 FALSE, /* partial_inplace */
1028 0, /* src_mask */
1029 0xffff, /* dst_mask */
1030 FALSE), /* pcrel_offset */
1031
1032 /* Like R_PPC64_PLTGOT16, but without overflow. */
1033 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1034 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1035 0, /* rightshift */
1036 1, /* size (0 = byte, 1 = short, 2 = long) */
1037 16, /* bitsize */
1038 FALSE, /* pc_relative */
1039 0, /* bitpos */
1040 complain_overflow_dont, /* complain_on_overflow */
1041 ppc64_elf_unhandled_reloc, /* special_function */
1042 "R_PPC64_PLTGOT16_LO", /* name */
1043 FALSE, /* partial_inplace */
1044 0, /* src_mask */
1045 0xffff, /* dst_mask */
1046 FALSE), /* pcrel_offset */
1047
1048 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1049 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1050 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1051 16, /* rightshift */
1052 1, /* size (0 = byte, 1 = short, 2 = long) */
1053 16, /* bitsize */
1054 FALSE, /* pc_relative */
1055 0, /* bitpos */
1056 complain_overflow_dont, /* complain_on_overflow */
1057 ppc64_elf_unhandled_reloc, /* special_function */
1058 "R_PPC64_PLTGOT16_HI", /* name */
1059 FALSE, /* partial_inplace */
1060 0, /* src_mask */
1061 0xffff, /* dst_mask */
1062 FALSE), /* pcrel_offset */
1063
1064 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1065 1 if the contents of the low 16 bits, treated as a signed number,
1066 is negative. */
1067 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1068 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1069 16, /* rightshift */
1070 1, /* size (0 = byte, 1 = short, 2 = long) */
1071 16, /* bitsize */
1072 FALSE, /* pc_relative */
1073 0, /* bitpos */
1074 complain_overflow_dont,/* complain_on_overflow */
1075 ppc64_elf_unhandled_reloc, /* special_function */
1076 "R_PPC64_PLTGOT16_HA", /* name */
1077 FALSE, /* partial_inplace */
1078 0, /* src_mask */
1079 0xffff, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1081
1082 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1083 HOWTO (R_PPC64_ADDR16_DS, /* type */
1084 0, /* rightshift */
1085 1, /* size (0 = byte, 1 = short, 2 = long) */
1086 16, /* bitsize */
1087 FALSE, /* pc_relative */
1088 0, /* bitpos */
1089 complain_overflow_bitfield, /* complain_on_overflow */
1090 bfd_elf_generic_reloc, /* special_function */
1091 "R_PPC64_ADDR16_DS", /* name */
1092 FALSE, /* partial_inplace */
1093 0, /* src_mask */
1094 0xfffc, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1096
1097 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1098 HOWTO (R_PPC64_ADDR16_LO_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_dont,/* complain_on_overflow */
1105 bfd_elf_generic_reloc, /* special_function */
1106 "R_PPC64_ADDR16_LO_DS",/* name */
1107 FALSE, /* partial_inplace */
1108 0, /* src_mask */
1109 0xfffc, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1111
1112 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1113 HOWTO (R_PPC64_GOT16_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_signed, /* complain_on_overflow */
1120 ppc64_elf_unhandled_reloc, /* special_function */
1121 "R_PPC64_GOT16_DS", /* name */
1122 FALSE, /* partial_inplace */
1123 0, /* src_mask */
1124 0xfffc, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1126
1127 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1128 HOWTO (R_PPC64_GOT16_LO_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_dont, /* complain_on_overflow */
1135 ppc64_elf_unhandled_reloc, /* special_function */
1136 "R_PPC64_GOT16_LO_DS", /* name */
1137 FALSE, /* partial_inplace */
1138 0, /* src_mask */
1139 0xfffc, /* dst_mask */
1140 FALSE), /* pcrel_offset */
1141
1142 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1143 HOWTO (R_PPC64_PLT16_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_PLT16_LO_DS", /* name */
1152 FALSE, /* partial_inplace */
1153 0, /* src_mask */
1154 0xfffc, /* dst_mask */
1155 FALSE), /* pcrel_offset */
1156
1157 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1158 HOWTO (R_PPC64_SECTOFF_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_bitfield, /* complain_on_overflow */
1165 ppc64_elf_sectoff_reloc, /* special_function */
1166 "R_PPC64_SECTOFF_DS", /* name */
1167 FALSE, /* partial_inplace */
1168 0, /* src_mask */
1169 0xfffc, /* dst_mask */
1170 FALSE), /* pcrel_offset */
1171
1172 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1173 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1174 0, /* rightshift */
1175 1, /* size (0 = byte, 1 = short, 2 = long) */
1176 16, /* bitsize */
1177 FALSE, /* pc_relative */
1178 0, /* bitpos */
1179 complain_overflow_dont, /* complain_on_overflow */
1180 ppc64_elf_sectoff_reloc, /* special_function */
1181 "R_PPC64_SECTOFF_LO_DS",/* name */
1182 FALSE, /* partial_inplace */
1183 0, /* src_mask */
1184 0xfffc, /* dst_mask */
1185 FALSE), /* pcrel_offset */
1186
1187 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1188 HOWTO (R_PPC64_TOC16_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_signed, /* complain_on_overflow */
1195 ppc64_elf_toc_reloc, /* special_function */
1196 "R_PPC64_TOC16_DS", /* name */
1197 FALSE, /* partial_inplace */
1198 0, /* src_mask */
1199 0xfffc, /* dst_mask */
1200 FALSE), /* pcrel_offset */
1201
1202 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1203 HOWTO (R_PPC64_TOC16_LO_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_dont, /* complain_on_overflow */
1210 ppc64_elf_toc_reloc, /* special_function */
1211 "R_PPC64_TOC16_LO_DS", /* name */
1212 FALSE, /* partial_inplace */
1213 0, /* src_mask */
1214 0xfffc, /* dst_mask */
1215 FALSE), /* pcrel_offset */
1216
1217 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1218 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1219 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1220 0, /* rightshift */
1221 1, /* size (0 = byte, 1 = short, 2 = long) */
1222 16, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_signed, /* complain_on_overflow */
1226 ppc64_elf_unhandled_reloc, /* special_function */
1227 "R_PPC64_PLTGOT16_DS", /* name */
1228 FALSE, /* partial_inplace */
1229 0, /* src_mask */
1230 0xfffc, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1232
1233 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1234 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1235 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1236 0, /* rightshift */
1237 1, /* size (0 = byte, 1 = short, 2 = long) */
1238 16, /* bitsize */
1239 FALSE, /* pc_relative */
1240 0, /* bitpos */
1241 complain_overflow_dont, /* complain_on_overflow */
1242 ppc64_elf_unhandled_reloc, /* special_function */
1243 "R_PPC64_PLTGOT16_LO_DS",/* name */
1244 FALSE, /* partial_inplace */
1245 0, /* src_mask */
1246 0xfffc, /* dst_mask */
1247 FALSE), /* pcrel_offset */
1248
727fc41e 1249 /* Marker relocs for TLS. */
411e1bfb
AM
1250 HOWTO (R_PPC64_TLS,
1251 0, /* rightshift */
1252 2, /* size (0 = byte, 1 = short, 2 = long) */
1253 32, /* bitsize */
1254 FALSE, /* pc_relative */
1255 0, /* bitpos */
1256 complain_overflow_dont, /* complain_on_overflow */
1257 bfd_elf_generic_reloc, /* special_function */
1258 "R_PPC64_TLS", /* name */
1259 FALSE, /* partial_inplace */
1260 0, /* src_mask */
1261 0, /* dst_mask */
1262 FALSE), /* pcrel_offset */
1263
727fc41e
AM
1264 HOWTO (R_PPC64_TLSGD,
1265 0, /* rightshift */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 32, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_dont, /* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 "R_PPC64_TLSGD", /* name */
1273 FALSE, /* partial_inplace */
1274 0, /* src_mask */
1275 0, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1277
1278 HOWTO (R_PPC64_TLSLD,
1279 0, /* rightshift */
1280 2, /* size (0 = byte, 1 = short, 2 = long) */
1281 32, /* bitsize */
1282 FALSE, /* pc_relative */
1283 0, /* bitpos */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 bfd_elf_generic_reloc, /* special_function */
1286 "R_PPC64_TLSLD", /* name */
1287 FALSE, /* partial_inplace */
1288 0, /* src_mask */
1289 0, /* dst_mask */
1290 FALSE), /* pcrel_offset */
1291
3b421ab3
AM
1292 HOWTO (R_PPC64_TOCSAVE,
1293 0, /* rightshift */
1294 2, /* size (0 = byte, 1 = short, 2 = long) */
1295 32, /* bitsize */
1296 FALSE, /* pc_relative */
1297 0, /* bitpos */
1298 complain_overflow_dont, /* complain_on_overflow */
1299 bfd_elf_generic_reloc, /* special_function */
1300 "R_PPC64_TOCSAVE", /* name */
1301 FALSE, /* partial_inplace */
1302 0, /* src_mask */
1303 0, /* dst_mask */
1304 FALSE), /* pcrel_offset */
1305
411e1bfb
AM
1306 /* Computes the load module index of the load module that contains the
1307 definition of its TLS sym. */
1308 HOWTO (R_PPC64_DTPMOD64,
1309 0, /* rightshift */
1310 4, /* size (0 = byte, 1 = short, 2 = long) */
1311 64, /* bitsize */
1312 FALSE, /* pc_relative */
1313 0, /* bitpos */
1314 complain_overflow_dont, /* complain_on_overflow */
1315 ppc64_elf_unhandled_reloc, /* special_function */
1316 "R_PPC64_DTPMOD64", /* name */
1317 FALSE, /* partial_inplace */
1318 0, /* src_mask */
1319 ONES (64), /* dst_mask */
1320 FALSE), /* pcrel_offset */
1321
1322 /* Computes a dtv-relative displacement, the difference between the value
1323 of sym+add and the base address of the thread-local storage block that
1324 contains the definition of sym, minus 0x8000. */
1325 HOWTO (R_PPC64_DTPREL64,
1326 0, /* rightshift */
1327 4, /* size (0 = byte, 1 = short, 2 = long) */
1328 64, /* bitsize */
1329 FALSE, /* pc_relative */
1330 0, /* bitpos */
1331 complain_overflow_dont, /* complain_on_overflow */
1332 ppc64_elf_unhandled_reloc, /* special_function */
1333 "R_PPC64_DTPREL64", /* name */
1334 FALSE, /* partial_inplace */
1335 0, /* src_mask */
1336 ONES (64), /* dst_mask */
1337 FALSE), /* pcrel_offset */
1338
1339 /* A 16 bit dtprel reloc. */
1340 HOWTO (R_PPC64_DTPREL16,
1341 0, /* rightshift */
1342 1, /* size (0 = byte, 1 = short, 2 = long) */
1343 16, /* bitsize */
1344 FALSE, /* pc_relative */
1345 0, /* bitpos */
1346 complain_overflow_signed, /* complain_on_overflow */
1347 ppc64_elf_unhandled_reloc, /* special_function */
1348 "R_PPC64_DTPREL16", /* name */
1349 FALSE, /* partial_inplace */
1350 0, /* src_mask */
1351 0xffff, /* dst_mask */
1352 FALSE), /* pcrel_offset */
1353
1354 /* Like DTPREL16, but no overflow. */
1355 HOWTO (R_PPC64_DTPREL16_LO,
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_dont, /* complain_on_overflow */
1362 ppc64_elf_unhandled_reloc, /* special_function */
1363 "R_PPC64_DTPREL16_LO", /* name */
1364 FALSE, /* partial_inplace */
1365 0, /* src_mask */
1366 0xffff, /* dst_mask */
1367 FALSE), /* pcrel_offset */
1368
1369 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1370 HOWTO (R_PPC64_DTPREL16_HI,
1371 16, /* 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_HI", /* name */
1379 FALSE, /* partial_inplace */
1380 0, /* src_mask */
1381 0xffff, /* dst_mask */
1382 FALSE), /* pcrel_offset */
1383
1384 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1385 HOWTO (R_PPC64_DTPREL16_HA,
1386 16, /* rightshift */
1387 1, /* size (0 = byte, 1 = short, 2 = long) */
1388 16, /* bitsize */
1389 FALSE, /* pc_relative */
1390 0, /* bitpos */
1391 complain_overflow_dont, /* complain_on_overflow */
1392 ppc64_elf_unhandled_reloc, /* special_function */
1393 "R_PPC64_DTPREL16_HA", /* name */
1394 FALSE, /* partial_inplace */
1395 0, /* src_mask */
1396 0xffff, /* dst_mask */
1397 FALSE), /* pcrel_offset */
1398
1399 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1400 HOWTO (R_PPC64_DTPREL16_HIGHER,
1401 32, /* rightshift */
1402 1, /* size (0 = byte, 1 = short, 2 = long) */
1403 16, /* bitsize */
1404 FALSE, /* pc_relative */
1405 0, /* bitpos */
1406 complain_overflow_dont, /* complain_on_overflow */
1407 ppc64_elf_unhandled_reloc, /* special_function */
1408 "R_PPC64_DTPREL16_HIGHER", /* name */
1409 FALSE, /* partial_inplace */
1410 0, /* src_mask */
1411 0xffff, /* dst_mask */
1412 FALSE), /* pcrel_offset */
1413
1414 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1415 HOWTO (R_PPC64_DTPREL16_HIGHERA,
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_HIGHERA", /* name */
1424 FALSE, /* partial_inplace */
1425 0, /* src_mask */
1426 0xffff, /* dst_mask */
1427 FALSE), /* pcrel_offset */
1428
1429 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1430 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1431 48, /* 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_HIGHEST", /* name */
1439 FALSE, /* partial_inplace */
1440 0, /* src_mask */
1441 0xffff, /* dst_mask */
1442 FALSE), /* pcrel_offset */
1443
1444 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1445 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
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_HIGHESTA", /* name */
1454 FALSE, /* partial_inplace */
1455 0, /* src_mask */
1456 0xffff, /* dst_mask */
1457 FALSE), /* pcrel_offset */
1458
1459 /* Like DTPREL16, but for insns with a DS field. */
1460 HOWTO (R_PPC64_DTPREL16_DS,
1461 0, /* rightshift */
1462 1, /* size (0 = byte, 1 = short, 2 = long) */
1463 16, /* bitsize */
1464 FALSE, /* pc_relative */
1465 0, /* bitpos */
1466 complain_overflow_signed, /* complain_on_overflow */
1467 ppc64_elf_unhandled_reloc, /* special_function */
1468 "R_PPC64_DTPREL16_DS", /* name */
1469 FALSE, /* partial_inplace */
1470 0, /* src_mask */
1471 0xfffc, /* dst_mask */
1472 FALSE), /* pcrel_offset */
1473
1474 /* Like DTPREL16_DS, but no overflow. */
1475 HOWTO (R_PPC64_DTPREL16_LO_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_dont, /* complain_on_overflow */
1482 ppc64_elf_unhandled_reloc, /* special_function */
1483 "R_PPC64_DTPREL16_LO_DS", /* name */
1484 FALSE, /* partial_inplace */
1485 0, /* src_mask */
1486 0xfffc, /* dst_mask */
1487 FALSE), /* pcrel_offset */
1488
1489 /* Computes a tp-relative displacement, the difference between the value of
1490 sym+add and the value of the thread pointer (r13). */
1491 HOWTO (R_PPC64_TPREL64,
1492 0, /* rightshift */
1493 4, /* size (0 = byte, 1 = short, 2 = long) */
1494 64, /* bitsize */
1495 FALSE, /* pc_relative */
1496 0, /* bitpos */
1497 complain_overflow_dont, /* complain_on_overflow */
1498 ppc64_elf_unhandled_reloc, /* special_function */
1499 "R_PPC64_TPREL64", /* name */
1500 FALSE, /* partial_inplace */
1501 0, /* src_mask */
1502 ONES (64), /* dst_mask */
1503 FALSE), /* pcrel_offset */
1504
1505 /* A 16 bit tprel reloc. */
1506 HOWTO (R_PPC64_TPREL16,
1507 0, /* rightshift */
1508 1, /* size (0 = byte, 1 = short, 2 = long) */
1509 16, /* bitsize */
1510 FALSE, /* pc_relative */
1511 0, /* bitpos */
1512 complain_overflow_signed, /* complain_on_overflow */
1513 ppc64_elf_unhandled_reloc, /* special_function */
1514 "R_PPC64_TPREL16", /* name */
1515 FALSE, /* partial_inplace */
1516 0, /* src_mask */
1517 0xffff, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519
1520 /* Like TPREL16, but no overflow. */
1521 HOWTO (R_PPC64_TPREL16_LO,
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_dont, /* complain_on_overflow */
1528 ppc64_elf_unhandled_reloc, /* special_function */
1529 "R_PPC64_TPREL16_LO", /* name */
1530 FALSE, /* partial_inplace */
1531 0, /* src_mask */
1532 0xffff, /* dst_mask */
1533 FALSE), /* pcrel_offset */
1534
1535 /* Like TPREL16_LO, but next higher group of 16 bits. */
1536 HOWTO (R_PPC64_TPREL16_HI,
1537 16, /* 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_HI", /* name */
1545 FALSE, /* partial_inplace */
1546 0, /* src_mask */
1547 0xffff, /* dst_mask */
1548 FALSE), /* pcrel_offset */
1549
1550 /* Like TPREL16_HI, but adjust for low 16 bits. */
1551 HOWTO (R_PPC64_TPREL16_HA,
1552 16, /* rightshift */
1553 1, /* size (0 = byte, 1 = short, 2 = long) */
1554 16, /* bitsize */
1555 FALSE, /* pc_relative */
1556 0, /* bitpos */
1557 complain_overflow_dont, /* complain_on_overflow */
1558 ppc64_elf_unhandled_reloc, /* special_function */
1559 "R_PPC64_TPREL16_HA", /* name */
1560 FALSE, /* partial_inplace */
1561 0, /* src_mask */
1562 0xffff, /* dst_mask */
1563 FALSE), /* pcrel_offset */
1564
1565 /* Like TPREL16_HI, but next higher group of 16 bits. */
1566 HOWTO (R_PPC64_TPREL16_HIGHER,
1567 32, /* rightshift */
1568 1, /* size (0 = byte, 1 = short, 2 = long) */
1569 16, /* bitsize */
1570 FALSE, /* pc_relative */
1571 0, /* bitpos */
1572 complain_overflow_dont, /* complain_on_overflow */
1573 ppc64_elf_unhandled_reloc, /* special_function */
1574 "R_PPC64_TPREL16_HIGHER", /* name */
1575 FALSE, /* partial_inplace */
1576 0, /* src_mask */
1577 0xffff, /* dst_mask */
1578 FALSE), /* pcrel_offset */
1579
1580 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1581 HOWTO (R_PPC64_TPREL16_HIGHERA,
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_HIGHERA", /* name */
1590 FALSE, /* partial_inplace */
1591 0, /* src_mask */
1592 0xffff, /* dst_mask */
1593 FALSE), /* pcrel_offset */
1594
1595 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1596 HOWTO (R_PPC64_TPREL16_HIGHEST,
1597 48, /* 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_HIGHEST", /* name */
1605 FALSE, /* partial_inplace */
1606 0, /* src_mask */
1607 0xffff, /* dst_mask */
1608 FALSE), /* pcrel_offset */
1609
1610 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1611 HOWTO (R_PPC64_TPREL16_HIGHESTA,
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_HIGHESTA", /* name */
1620 FALSE, /* partial_inplace */
1621 0, /* src_mask */
1622 0xffff, /* dst_mask */
1623 FALSE), /* pcrel_offset */
1624
1625 /* Like TPREL16, but for insns with a DS field. */
1626 HOWTO (R_PPC64_TPREL16_DS,
1627 0, /* rightshift */
1628 1, /* size (0 = byte, 1 = short, 2 = long) */
1629 16, /* bitsize */
1630 FALSE, /* pc_relative */
1631 0, /* bitpos */
1632 complain_overflow_signed, /* complain_on_overflow */
1633 ppc64_elf_unhandled_reloc, /* special_function */
1634 "R_PPC64_TPREL16_DS", /* name */
1635 FALSE, /* partial_inplace */
1636 0, /* src_mask */
1637 0xfffc, /* dst_mask */
1638 FALSE), /* pcrel_offset */
1639
1640 /* Like TPREL16_DS, but no overflow. */
1641 HOWTO (R_PPC64_TPREL16_LO_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_dont, /* complain_on_overflow */
1648 ppc64_elf_unhandled_reloc, /* special_function */
1649 "R_PPC64_TPREL16_LO_DS", /* name */
1650 FALSE, /* partial_inplace */
1651 0, /* src_mask */
1652 0xfffc, /* dst_mask */
1653 FALSE), /* pcrel_offset */
1654
1655 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1656 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1657 to the first entry relative to the TOC base (r2). */
1658 HOWTO (R_PPC64_GOT_TLSGD16,
1659 0, /* rightshift */
1660 1, /* size (0 = byte, 1 = short, 2 = long) */
1661 16, /* bitsize */
1662 FALSE, /* pc_relative */
1663 0, /* bitpos */
1664 complain_overflow_signed, /* complain_on_overflow */
1665 ppc64_elf_unhandled_reloc, /* special_function */
1666 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1667 FALSE, /* partial_inplace */
5bd4f169
AM
1668 0, /* src_mask */
1669 0xffff, /* dst_mask */
b34976b6 1670 FALSE), /* pcrel_offset */
5bd4f169 1671
411e1bfb
AM
1672 /* Like GOT_TLSGD16, but no overflow. */
1673 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1674 0, /* rightshift */
1675 1, /* size (0 = byte, 1 = short, 2 = long) */
1676 16, /* bitsize */
b34976b6 1677 FALSE, /* pc_relative */
5bd4f169
AM
1678 0, /* bitpos */
1679 complain_overflow_dont, /* complain_on_overflow */
805fc799 1680 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1681 "R_PPC64_GOT_TLSGD16_LO", /* 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_LO, but next higher group of 16 bits. */
1688 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1689 16, /* 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_HI", /* 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_HI, but adjust for low 16 bits. */
1703 HOWTO (R_PPC64_GOT_TLSGD16_HA,
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 */
411e1bfb 1709 complain_overflow_dont, /* complain_on_overflow */
805fc799 1710 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1711 "R_PPC64_GOT_TLSGD16_HA", /* 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 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1718 with values (sym+add)@dtpmod and zero, and computes the offset to the
1719 first entry relative to the TOC base (r2). */
1720 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1721 0, /* rightshift */
1722 1, /* size (0 = byte, 1 = short, 2 = long) */
1723 16, /* bitsize */
b34976b6 1724 FALSE, /* pc_relative */
5bd4f169 1725 0, /* bitpos */
411e1bfb
AM
1726 complain_overflow_signed, /* complain_on_overflow */
1727 ppc64_elf_unhandled_reloc, /* special_function */
1728 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1729 FALSE, /* partial_inplace */
d006db6c 1730 0, /* src_mask */
411e1bfb 1731 0xffff, /* dst_mask */
b34976b6 1732 FALSE), /* pcrel_offset */
5bd4f169 1733
411e1bfb
AM
1734 /* Like GOT_TLSLD16, but no overflow. */
1735 HOWTO (R_PPC64_GOT_TLSLD16_LO,
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_dont, /* complain_on_overflow */
1742 ppc64_elf_unhandled_reloc, /* special_function */
1743 "R_PPC64_GOT_TLSLD16_LO", /* 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_LO, but next higher group of 16 bits. */
1750 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1751 16, /* rightshift */
5bd4f169
AM
1752 1, /* size (0 = byte, 1 = short, 2 = long) */
1753 16, /* bitsize */
b34976b6 1754 FALSE, /* pc_relative */
5bd4f169 1755 0, /* bitpos */
411e1bfb 1756 complain_overflow_dont, /* complain_on_overflow */
805fc799 1757 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1758 "R_PPC64_GOT_TLSLD16_HI", /* 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_HI, but adjust for low 16 bits. */
1765 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1766 16, /* rightshift */
5bd4f169
AM
1767 1, /* size (0 = byte, 1 = short, 2 = long) */
1768 16, /* bitsize */
b34976b6 1769 FALSE, /* pc_relative */
5bd4f169
AM
1770 0, /* bitpos */
1771 complain_overflow_dont, /* complain_on_overflow */
805fc799 1772 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1773 "R_PPC64_GOT_TLSLD16_HA", /* 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 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1780 the offset to the entry relative to the TOC base (r2). */
1781 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1782 0, /* rightshift */
1783 1, /* size (0 = byte, 1 = short, 2 = long) */
1784 16, /* bitsize */
b34976b6 1785 FALSE, /* pc_relative */
5bd4f169 1786 0, /* bitpos */
411e1bfb 1787 complain_overflow_signed, /* complain_on_overflow */
805fc799 1788 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1789 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1790 FALSE, /* partial_inplace */
d006db6c 1791 0, /* src_mask */
5bd4f169 1792 0xfffc, /* dst_mask */
b34976b6 1793 FALSE), /* pcrel_offset */
5bd4f169 1794
411e1bfb
AM
1795 /* Like GOT_DTPREL16_DS, but no overflow. */
1796 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1797 0, /* rightshift */
c061c2d8
AM
1798 1, /* size (0 = byte, 1 = short, 2 = long) */
1799 16, /* bitsize */
b34976b6 1800 FALSE, /* pc_relative */
5bd4f169 1801 0, /* bitpos */
411e1bfb
AM
1802 complain_overflow_dont, /* complain_on_overflow */
1803 ppc64_elf_unhandled_reloc, /* special_function */
1804 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1805 FALSE, /* partial_inplace */
d006db6c 1806 0, /* src_mask */
c061c2d8 1807 0xfffc, /* dst_mask */
b34976b6 1808 FALSE), /* pcrel_offset */
5bd4f169 1809
411e1bfb
AM
1810 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1811 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1812 16, /* rightshift */
5bd4f169
AM
1813 1, /* size (0 = byte, 1 = short, 2 = long) */
1814 16, /* bitsize */
b34976b6 1815 FALSE, /* pc_relative */
5bd4f169
AM
1816 0, /* bitpos */
1817 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1818 ppc64_elf_unhandled_reloc, /* special_function */
1819 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1820 FALSE, /* partial_inplace */
d006db6c 1821 0, /* src_mask */
411e1bfb 1822 0xffff, /* dst_mask */
b34976b6 1823 FALSE), /* pcrel_offset */
5bd4f169 1824
411e1bfb
AM
1825 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1826 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1827 16, /* rightshift */
1828 1, /* size (0 = byte, 1 = short, 2 = long) */
1829 16, /* bitsize */
1830 FALSE, /* pc_relative */
1831 0, /* bitpos */
1832 complain_overflow_dont, /* complain_on_overflow */
1833 ppc64_elf_unhandled_reloc, /* special_function */
1834 "R_PPC64_GOT_DTPREL16_HA", /* name */
1835 FALSE, /* partial_inplace */
1836 0, /* src_mask */
1837 0xffff, /* dst_mask */
1838 FALSE), /* pcrel_offset */
1839
1840 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1841 offset to the entry relative to the TOC base (r2). */
1842 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1843 0, /* rightshift */
1844 1, /* size (0 = byte, 1 = short, 2 = long) */
1845 16, /* bitsize */
b34976b6 1846 FALSE, /* pc_relative */
5bd4f169
AM
1847 0, /* bitpos */
1848 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1849 ppc64_elf_unhandled_reloc, /* special_function */
1850 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1851 FALSE, /* partial_inplace */
d006db6c 1852 0, /* src_mask */
ad8e1ba5 1853 0xfffc, /* dst_mask */
b34976b6 1854 FALSE), /* pcrel_offset */
5bd4f169 1855
411e1bfb
AM
1856 /* Like GOT_TPREL16_DS, but no overflow. */
1857 HOWTO (R_PPC64_GOT_TPREL16_LO_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_dont, /* complain_on_overflow */
411e1bfb
AM
1864 ppc64_elf_unhandled_reloc, /* special_function */
1865 "R_PPC64_GOT_TPREL16_LO_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_LO_DS, but next higher group of 16 bits. */
1872 HOWTO (R_PPC64_GOT_TPREL16_HI,
1873 16, /* rightshift */
5bd4f169
AM
1874 1, /* size (0 = byte, 1 = short, 2 = long) */
1875 16, /* bitsize */
b34976b6 1876 FALSE, /* pc_relative */
5bd4f169 1877 0, /* bitpos */
411e1bfb 1878 complain_overflow_dont, /* complain_on_overflow */
805fc799 1879 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1880 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1881 FALSE, /* partial_inplace */
d006db6c 1882 0, /* src_mask */
411e1bfb 1883 0xffff, /* dst_mask */
b34976b6 1884 FALSE), /* pcrel_offset */
5bd4f169 1885
411e1bfb
AM
1886 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1887 HOWTO (R_PPC64_GOT_TPREL16_HA,
1888 16, /* rightshift */
5bd4f169
AM
1889 1, /* size (0 = byte, 1 = short, 2 = long) */
1890 16, /* bitsize */
b34976b6 1891 FALSE, /* pc_relative */
5bd4f169
AM
1892 0, /* bitpos */
1893 complain_overflow_dont, /* complain_on_overflow */
805fc799 1894 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1895 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1896 FALSE, /* partial_inplace */
d006db6c 1897 0, /* src_mask */
411e1bfb 1898 0xffff, /* dst_mask */
b34976b6 1899 FALSE), /* pcrel_offset */
5bd4f169 1900
25f23106
AM
1901 HOWTO (R_PPC64_JMP_IREL, /* type */
1902 0, /* rightshift */
1903 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1904 0, /* bitsize */
1905 FALSE, /* pc_relative */
1906 0, /* bitpos */
1907 complain_overflow_dont, /* complain_on_overflow */
1908 ppc64_elf_unhandled_reloc, /* special_function */
1909 "R_PPC64_JMP_IREL", /* name */
1910 FALSE, /* partial_inplace */
1911 0, /* src_mask */
1912 0, /* dst_mask */
1913 FALSE), /* pcrel_offset */
1914
e054468f
AM
1915 HOWTO (R_PPC64_IRELATIVE, /* type */
1916 0, /* rightshift */
1917 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1918 64, /* bitsize */
1919 FALSE, /* pc_relative */
1920 0, /* bitpos */
1921 complain_overflow_dont, /* complain_on_overflow */
1922 bfd_elf_generic_reloc, /* special_function */
1923 "R_PPC64_IRELATIVE", /* name */
1924 FALSE, /* partial_inplace */
1925 0, /* src_mask */
1926 ONES (64), /* dst_mask */
1927 FALSE), /* pcrel_offset */
1928
25f23106
AM
1929 /* A 16 bit relative relocation. */
1930 HOWTO (R_PPC64_REL16, /* type */
1931 0, /* rightshift */
1932 1, /* size (0 = byte, 1 = short, 2 = long) */
1933 16, /* bitsize */
1934 TRUE, /* pc_relative */
1935 0, /* bitpos */
1936 complain_overflow_bitfield, /* complain_on_overflow */
1937 bfd_elf_generic_reloc, /* special_function */
1938 "R_PPC64_REL16", /* name */
1939 FALSE, /* partial_inplace */
1940 0, /* src_mask */
1941 0xffff, /* dst_mask */
1942 TRUE), /* pcrel_offset */
1943
1944 /* A 16 bit relative relocation without overflow. */
1945 HOWTO (R_PPC64_REL16_LO, /* 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_dont,/* complain_on_overflow */
1952 bfd_elf_generic_reloc, /* special_function */
1953 "R_PPC64_REL16_LO", /* name */
1954 FALSE, /* partial_inplace */
1955 0, /* src_mask */
1956 0xffff, /* dst_mask */
1957 TRUE), /* pcrel_offset */
1958
1959 /* The high order 16 bits of a relative address. */
1960 HOWTO (R_PPC64_REL16_HI, /* type */
1961 16, /* 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_HI", /* 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, plus 1 if the contents of
1975 the low 16 bits, treated as a signed number, is negative. */
1976 HOWTO (R_PPC64_REL16_HA, /* type */
1977 16, /* rightshift */
1978 1, /* size (0 = byte, 1 = short, 2 = long) */
1979 16, /* bitsize */
1980 TRUE, /* pc_relative */
1981 0, /* bitpos */
1982 complain_overflow_dont, /* complain_on_overflow */
1983 ppc64_elf_ha_reloc, /* special_function */
1984 "R_PPC64_REL16_HA", /* name */
1985 FALSE, /* partial_inplace */
1986 0, /* src_mask */
1987 0xffff, /* dst_mask */
1988 TRUE), /* pcrel_offset */
1989
5bd4f169
AM
1990 /* GNU extension to record C++ vtable hierarchy. */
1991 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1992 0, /* rightshift */
1993 0, /* size (0 = byte, 1 = short, 2 = long) */
1994 0, /* bitsize */
b34976b6 1995 FALSE, /* pc_relative */
5bd4f169
AM
1996 0, /* bitpos */
1997 complain_overflow_dont, /* complain_on_overflow */
1998 NULL, /* special_function */
1999 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2000 FALSE, /* partial_inplace */
5bd4f169
AM
2001 0, /* src_mask */
2002 0, /* dst_mask */
b34976b6 2003 FALSE), /* pcrel_offset */
5bd4f169
AM
2004
2005 /* GNU extension to record C++ vtable member usage. */
2006 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2007 0, /* rightshift */
2008 0, /* size (0 = byte, 1 = short, 2 = long) */
2009 0, /* bitsize */
b34976b6 2010 FALSE, /* pc_relative */
5bd4f169
AM
2011 0, /* bitpos */
2012 complain_overflow_dont, /* complain_on_overflow */
2013 NULL, /* special_function */
2014 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2015 FALSE, /* partial_inplace */
5bd4f169
AM
2016 0, /* src_mask */
2017 0, /* dst_mask */
b34976b6 2018 FALSE), /* pcrel_offset */
5bd4f169
AM
2019};
2020
2021\f
2022/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2023 be done. */
2024
2025static void
4ce794b7 2026ppc_howto_init (void)
5bd4f169
AM
2027{
2028 unsigned int i, type;
2029
2030 for (i = 0;
2031 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2032 i++)
2033 {
2034 type = ppc64_elf_howto_raw[i].type;
2035 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2036 / sizeof (ppc64_elf_howto_table[0])));
2037 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2038 }
2039}
2040
2041static reloc_howto_type *
4ce794b7
AM
2042ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2043 bfd_reloc_code_real_type code)
5bd4f169 2044{
411e1bfb 2045 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2046
2047 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2048 /* Initialize howto table if needed. */
2049 ppc_howto_init ();
2050
4ce794b7 2051 switch (code)
5bd4f169
AM
2052 {
2053 default:
4ce794b7 2054 return NULL;
5bd4f169 2055
411e1bfb
AM
2056 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2057 break;
2058 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2059 break;
2060 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2061 break;
2062 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2063 break;
2064 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2065 break;
2066 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2067 break;
2068 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2069 break;
411e1bfb 2070 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2071 break;
411e1bfb 2072 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2073 break;
411e1bfb 2074 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2075 break;
411e1bfb 2076 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2077 break;
411e1bfb 2078 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2079 break;
411e1bfb 2080 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2081 break;
411e1bfb 2082 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2083 break;
411e1bfb 2084 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2085 break;
411e1bfb 2086 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2087 break;
411e1bfb 2088 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2089 break;
411e1bfb 2090 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2091 break;
411e1bfb 2092 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2093 break;
411e1bfb 2094 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2095 break;
411e1bfb 2096 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2097 break;
411e1bfb 2098 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2099 break;
411e1bfb 2100 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2101 break;
411e1bfb 2102 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2103 break;
411e1bfb 2104 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2105 break;
411e1bfb 2106 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2107 break;
411e1bfb 2108 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2109 break;
411e1bfb 2110 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2111 break;
411e1bfb 2112 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2113 break;
411e1bfb 2114 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2115 break;
411e1bfb 2116 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2117 break;
411e1bfb 2118 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2119 break;
411e1bfb 2120 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2121 break;
411e1bfb 2122 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2123 break;
411e1bfb 2124 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2125 break;
411e1bfb 2126 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2127 break;
411e1bfb 2128 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2129 break;
411e1bfb 2130 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2131 break;
411e1bfb 2132 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2133 break;
411e1bfb 2134 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2135 break;
411e1bfb 2136 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2137 break;
411e1bfb 2138 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2139 break;
411e1bfb 2140 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2141 break;
411e1bfb 2142 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2143 break;
411e1bfb 2144 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2145 break;
411e1bfb 2146 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2147 break;
411e1bfb 2148 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2149 break;
411e1bfb 2150 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2151 break;
411e1bfb 2152 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2153 break;
411e1bfb 2154 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2155 break;
411e1bfb 2156 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2157 break;
411e1bfb 2158 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2159 break;
411e1bfb 2160 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2161 break;
411e1bfb 2162 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2163 break;
411e1bfb 2164 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2165 break;
411e1bfb 2166 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2167 break;
411e1bfb 2168 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2169 break;
411e1bfb 2170 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2171 break;
411e1bfb 2172 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2173 break;
411e1bfb 2174 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2175 break;
727fc41e
AM
2176 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2177 break;
2178 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2179 break;
411e1bfb 2180 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2181 break;
411e1bfb 2182 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2183 break;
411e1bfb 2184 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2185 break;
411e1bfb 2186 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2187 break;
411e1bfb 2188 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2189 break;
411e1bfb 2190 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2191 break;
411e1bfb
AM
2192 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2193 break;
2194 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2195 break;
2196 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2197 break;
2198 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2199 break;
2200 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2201 break;
2202 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2203 break;
2204 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2205 break;
2206 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2207 break;
2208 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2209 break;
2210 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2211 break;
2212 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2213 break;
2214 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2215 break;
2216 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2217 break;
2218 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2219 break;
2220 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2221 break;
2222 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2223 break;
2224 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2225 break;
2226 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2227 break;
2228 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2229 break;
2230 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2231 break;
2232 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2233 break;
2234 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2235 break;
2236 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2237 break;
2238 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2239 break;
2240 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2241 break;
2242 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2243 break;
2244 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2245 break;
2246 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2247 break;
2248 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2249 break;
2250 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2251 break;
2252 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2253 break;
2254 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2255 break;
2256 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2257 break;
25f23106
AM
2258 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2259 break;
2260 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2261 break;
2262 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2263 break;
2264 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2265 break;
411e1bfb
AM
2266 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2267 break;
2268 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2269 break;
2270 }
2271
4ce794b7 2272 return ppc64_elf_howto_table[r];
5bd4f169
AM
2273};
2274
157090f7
AM
2275static reloc_howto_type *
2276ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2277 const char *r_name)
2278{
2279 unsigned int i;
2280
2281 for (i = 0;
2282 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2283 i++)
2284 if (ppc64_elf_howto_raw[i].name != NULL
2285 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2286 return &ppc64_elf_howto_raw[i];
2287
2288 return NULL;
2289}
2290
5bd4f169
AM
2291/* Set the howto pointer for a PowerPC ELF reloc. */
2292
2293static void
4ce794b7
AM
2294ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2295 Elf_Internal_Rela *dst)
5bd4f169 2296{
65f38f15
AM
2297 unsigned int type;
2298
ef60b7ff 2299 /* Initialize howto table if needed. */
5bd4f169 2300 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2301 ppc_howto_init ();
2302
65f38f15 2303 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2304 if (type >= (sizeof (ppc64_elf_howto_table)
2305 / sizeof (ppc64_elf_howto_table[0])))
2306 {
2307 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2308 abfd, (int) type);
2309 type = R_PPC64_NONE;
d0fb9a8d 2310 }
65f38f15 2311 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2312}
2313
04c9666a 2314/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2315
2316static bfd_reloc_status_type
4ce794b7
AM
2317ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2318 void *data, asection *input_section,
2319 bfd *output_bfd, char **error_message)
5bd4f169 2320{
805fc799
AM
2321 /* If this is a relocatable link (output_bfd test tells us), just
2322 call the generic function. Any adjustment will be done at final
2323 link time. */
2324 if (output_bfd != NULL)
cedb70c5 2325 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2326 input_section, output_bfd, error_message);
2327
2328 /* Adjust the addend for sign extension of the low 16 bits.
2329 We won't actually be using the low 16 bits, so trashing them
2330 doesn't matter. */
2331 reloc_entry->addend += 0x8000;
2332 return bfd_reloc_continue;
2333}
5bd4f169 2334
2441e016
AM
2335static bfd_reloc_status_type
2336ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2337 void *data, asection *input_section,
2338 bfd *output_bfd, char **error_message)
2339{
2340 if (output_bfd != NULL)
2341 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2342 input_section, output_bfd, error_message);
2343
699733f6
AM
2344 if (strcmp (symbol->section->name, ".opd") == 0
2345 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2346 {
2347 bfd_vma dest = opd_entry_value (symbol->section,
2348 symbol->value + reloc_entry->addend,
2349 NULL, NULL);
2350 if (dest != (bfd_vma) -1)
2351 reloc_entry->addend = dest - (symbol->value
2352 + symbol->section->output_section->vma
2353 + symbol->section->output_offset);
2354 }
2355 return bfd_reloc_continue;
2356}
2357
805fc799 2358static bfd_reloc_status_type
4ce794b7
AM
2359ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2360 void *data, asection *input_section,
2361 bfd *output_bfd, char **error_message)
805fc799
AM
2362{
2363 long insn;
04c9666a 2364 enum elf_ppc64_reloc_type r_type;
805fc799 2365 bfd_size_type octets;
794e51c0
AM
2366 /* Assume 'at' branch hints. */
2367 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2368
2369 /* If this is a relocatable link (output_bfd test tells us), just
2370 call the generic function. Any adjustment will be done at final
2371 link time. */
5bd4f169 2372 if (output_bfd != NULL)
cedb70c5 2373 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2374 input_section, output_bfd, error_message);
2375
2376 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2377 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2378 insn &= ~(0x01 << 21);
4ce794b7 2379 r_type = reloc_entry->howto->type;
805fc799
AM
2380 if (r_type == R_PPC64_ADDR14_BRTAKEN
2381 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2382 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2383
794e51c0 2384 if (is_isa_v2)
5bd4f169 2385 {
805fc799
AM
2386 /* Set 'a' bit. This is 0b00010 in BO field for branch
2387 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2388 for branch on CTR insns (BO == 1a00t or 1a01t). */
2389 if ((insn & (0x14 << 21)) == (0x04 << 21))
2390 insn |= 0x02 << 21;
2391 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2392 insn |= 0x08 << 21;
2393 else
2441e016 2394 goto out;
5bd4f169 2395 }
805fc799
AM
2396 else
2397 {
2398 bfd_vma target = 0;
2399 bfd_vma from;
5bd4f169 2400
805fc799
AM
2401 if (!bfd_is_com_section (symbol->section))
2402 target = symbol->value;
2403 target += symbol->section->output_section->vma;
2404 target += symbol->section->output_offset;
2405 target += reloc_entry->addend;
5bd4f169 2406
805fc799
AM
2407 from = (reloc_entry->address
2408 + input_section->output_offset
2409 + input_section->output_section->vma);
5bd4f169 2410
805fc799
AM
2411 /* Invert 'y' bit if not the default. */
2412 if ((bfd_signed_vma) (target - from) < 0)
2413 insn ^= 0x01 << 21;
2414 }
4ce794b7 2415 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2416 out:
2417 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2418 input_section, output_bfd, error_message);
805fc799 2419}
5bd4f169 2420
805fc799 2421static bfd_reloc_status_type
4ce794b7
AM
2422ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2423 void *data, asection *input_section,
2424 bfd *output_bfd, char **error_message)
805fc799
AM
2425{
2426 /* If this is a relocatable link (output_bfd test tells us), just
2427 call the generic function. Any adjustment will be done at final
2428 link time. */
2429 if (output_bfd != NULL)
cedb70c5 2430 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2431 input_section, output_bfd, error_message);
5bd4f169 2432
805fc799
AM
2433 /* Subtract the symbol section base address. */
2434 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2435 return bfd_reloc_continue;
2436}
2437
805fc799 2438static bfd_reloc_status_type
4ce794b7
AM
2439ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2440 void *data, asection *input_section,
2441 bfd *output_bfd, char **error_message)
805fc799
AM
2442{
2443 /* If this is a relocatable link (output_bfd test tells us), just
2444 call the generic function. Any adjustment will be done at final
2445 link time. */
2446 if (output_bfd != NULL)
cedb70c5 2447 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2448 input_section, output_bfd, error_message);
2449
2450 /* Subtract the symbol section base address. */
2451 reloc_entry->addend -= symbol->section->output_section->vma;
2452
2453 /* Adjust the addend for sign extension of the low 16 bits. */
2454 reloc_entry->addend += 0x8000;
2455 return bfd_reloc_continue;
2456}
2457
2458static bfd_reloc_status_type
4ce794b7
AM
2459ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2460 void *data, asection *input_section,
2461 bfd *output_bfd, char **error_message)
805fc799
AM
2462{
2463 bfd_vma TOCstart;
2464
2465 /* If this is a relocatable link (output_bfd test tells us), just
2466 call the generic function. Any adjustment will be done at final
2467 link time. */
2468 if (output_bfd != NULL)
cedb70c5 2469 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2470 input_section, output_bfd, error_message);
2471
2472 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2473 if (TOCstart == 0)
2474 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2475
2476 /* Subtract the TOC base address. */
2477 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2478 return bfd_reloc_continue;
2479}
2480
2481static bfd_reloc_status_type
4ce794b7
AM
2482ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2483 void *data, asection *input_section,
2484 bfd *output_bfd, char **error_message)
805fc799
AM
2485{
2486 bfd_vma TOCstart;
2487
2488 /* If this is a relocatable link (output_bfd test tells us), just
2489 call the generic function. Any adjustment will be done at final
2490 link time. */
2491 if (output_bfd != NULL)
cedb70c5 2492 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2493 input_section, output_bfd, error_message);
2494
2495 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2496 if (TOCstart == 0)
2497 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2498
2499 /* Subtract the TOC base address. */
2500 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2501
2502 /* Adjust the addend for sign extension of the low 16 bits. */
2503 reloc_entry->addend += 0x8000;
2504 return bfd_reloc_continue;
2505}
2506
2507static bfd_reloc_status_type
4ce794b7
AM
2508ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509 void *data, asection *input_section,
2510 bfd *output_bfd, char **error_message)
805fc799
AM
2511{
2512 bfd_vma TOCstart;
2513 bfd_size_type octets;
2514
2515 /* If this is a relocatable link (output_bfd test tells us), just
2516 call the generic function. Any adjustment will be done at final
2517 link time. */
2518 if (output_bfd != NULL)
cedb70c5 2519 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2520 input_section, output_bfd, error_message);
2521
2522 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2523 if (TOCstart == 0)
2524 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2525
2526 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2527 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2528 return bfd_reloc_ok;
2529}
2530
2531static bfd_reloc_status_type
4ce794b7
AM
2532ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2533 void *data, asection *input_section,
2534 bfd *output_bfd, char **error_message)
805fc799
AM
2535{
2536 /* If this is a relocatable link (output_bfd test tells us), just
2537 call the generic function. Any adjustment will be done at final
2538 link time. */
2539 if (output_bfd != NULL)
cedb70c5 2540 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2541 input_section, output_bfd, error_message);
2542
2543 if (error_message != NULL)
2544 {
2545 static char buf[60];
2546 sprintf (buf, "generic linker can't handle %s",
2547 reloc_entry->howto->name);
2548 *error_message = buf;
2549 }
2550 return bfd_reloc_dangerous;
2551}
2552
927be08e
AM
2553/* Track GOT entries needed for a given symbol. We might need more
2554 than one got entry per symbol. */
2555struct got_entry
2556{
2557 struct got_entry *next;
2558
2559 /* The symbol addend that we'll be placing in the GOT. */
2560 bfd_vma addend;
2561
2562 /* Unlike other ELF targets, we use separate GOT entries for the same
2563 symbol referenced from different input files. This is to support
2564 automatic multiple TOC/GOT sections, where the TOC base can vary
2565 from one input file to another. After partitioning into TOC groups
2566 we merge entries within the group.
2567
2568 Point to the BFD owning this GOT entry. */
2569 bfd *owner;
2570
2571 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2572 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2573 unsigned char tls_type;
927be08e
AM
2574
2575 /* Non-zero if got.ent points to real entry. */
f961d9dd 2576 unsigned char is_indirect;
927be08e
AM
2577
2578 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2579 union
2580 {
2581 bfd_signed_vma refcount;
2582 bfd_vma offset;
2583 struct got_entry *ent;
2584 } got;
2585};
2586
2587/* The same for PLT. */
2588struct plt_entry
2589{
2590 struct plt_entry *next;
2591
2592 bfd_vma addend;
2593
2594 union
2595 {
2596 bfd_signed_vma refcount;
2597 bfd_vma offset;
2598 } plt;
2599};
2600
e717da7e
AM
2601struct ppc64_elf_obj_tdata
2602{
2603 struct elf_obj_tdata elf;
2604
2605 /* Shortcuts to dynamic linker sections. */
2606 asection *got;
2607 asection *relgot;
2608
b3fac117
AM
2609 /* Used during garbage collection. We attach global symbols defined
2610 on removed .opd entries to this section so that the sym is removed. */
2611 asection *deleted_section;
81688140 2612
927be08e 2613 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2614 sections means we potentially need one of these for each input bfd. */
927be08e 2615 struct got_entry tlsld_got;
8860955f
AM
2616
2617 /* A copy of relocs before they are modified for --emit-relocs. */
2618 Elf_Internal_Rela *opd_relocs;
d77c8a4b
AM
2619
2620 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2621 the reloc to be in the range -32768 to 32767. */
98528052
AM
2622 unsigned int has_small_toc_reloc : 1;
2623
560c8763
AM
2624 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2625 instruction not one we handle. */
2626 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2627};
2628
2629#define ppc64_elf_tdata(bfd) \
2630 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2631
2632#define ppc64_tlsld_got(bfd) \
2633 (&ppc64_elf_tdata (bfd)->tlsld_got)
2634
0c8d6e5c
AM
2635#define is_ppc64_elf(bfd) \
2636 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2637 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2638
e717da7e
AM
2639/* Override the generic function because we store some extras. */
2640
2641static bfd_boolean
2642ppc64_elf_mkobject (bfd *abfd)
2643{
0ffa91dd 2644 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2645 PPC64_ELF_DATA);
e717da7e
AM
2646}
2647
feee612b
AM
2648/* Fix bad default arch selected for a 64 bit input bfd when the
2649 default is 32 bit. */
2650
b34976b6 2651static bfd_boolean
4ce794b7 2652ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2653{
2654 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2655 {
2656 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2657
2658 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2659 {
2660 /* Relies on arch after 32 bit default being 64 bit default. */
2661 abfd->arch_info = abfd->arch_info->next;
2662 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2663 }
2664 }
b34976b6 2665 return TRUE;
feee612b
AM
2666}
2667
d37c89e5
AM
2668/* Support for core dump NOTE sections. */
2669
2670static bfd_boolean
2671ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2672{
eea6121a 2673 size_t offset, size;
d37c89e5
AM
2674
2675 if (note->descsz != 504)
2676 return FALSE;
2677
2678 /* pr_cursig */
2679 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2680
2681 /* pr_pid */
261b8d08 2682 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2683
2684 /* pr_reg */
2685 offset = 112;
eea6121a 2686 size = 384;
d37c89e5
AM
2687
2688 /* Make a ".reg/999" section. */
2689 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2690 size, note->descpos + offset);
d37c89e5
AM
2691}
2692
2693static bfd_boolean
2694ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2695{
2696 if (note->descsz != 136)
2697 return FALSE;
2698
bc989cdc
JK
2699 elf_tdata (abfd)->core_pid
2700 = bfd_get_32 (abfd, note->descdata + 24);
d37c89e5
AM
2701 elf_tdata (abfd)->core_program
2702 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2703 elf_tdata (abfd)->core_command
2704 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2705
2706 return TRUE;
2707}
2708
183e98be
AM
2709static char *
2710ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2711 ...)
2712{
2713 switch (note_type)
2714 {
2715 default:
2716 return NULL;
2717
2718 case NT_PRPSINFO:
2719 {
2720 char data[136];
2721 va_list ap;
2722
2723 va_start (ap, note_type);
2724 memset (data, 0, 40);
2725 strncpy (data + 40, va_arg (ap, const char *), 16);
2726 strncpy (data + 56, va_arg (ap, const char *), 80);
2727 va_end (ap);
2728 return elfcore_write_note (abfd, buf, bufsiz,
2729 "CORE", note_type, data, sizeof (data));
2730 }
2731
2732 case NT_PRSTATUS:
2733 {
2734 char data[504];
2735 va_list ap;
2736 long pid;
2737 int cursig;
2738 const void *greg;
2739
2740 va_start (ap, note_type);
2741 memset (data, 0, 112);
2742 pid = va_arg (ap, long);
2743 bfd_put_32 (abfd, pid, data + 32);
2744 cursig = va_arg (ap, int);
2745 bfd_put_16 (abfd, cursig, data + 12);
2746 greg = va_arg (ap, const void *);
2747 memcpy (data + 112, greg, 384);
2748 memset (data + 496, 0, 8);
2749 va_end (ap);
2750 return elfcore_write_note (abfd, buf, bufsiz,
2751 "CORE", note_type, data, sizeof (data));
2752 }
2753 }
2754}
2755
5d35169e
AM
2756/* Add extra PPC sections. */
2757
b35d266b 2758static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2759{
0112cd26
NC
2760 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2761 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2762 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2763 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2766 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2767};
2768
7c8fe5c4
AM
2769enum _ppc64_sec_type {
2770 sec_normal = 0,
2771 sec_opd = 1,
2772 sec_toc = 2
2773};
2774
f0abc2a1
AM
2775struct _ppc64_elf_section_data
2776{
2777 struct bfd_elf_section_data elf;
411e1bfb 2778
f0abc2a1
AM
2779 union
2780 {
74f0fb50
AM
2781 /* An array with one entry for each opd function descriptor. */
2782 struct _opd_sec_data
2783 {
2784 /* Points to the function code section for local opd entries. */
2785 asection **func_sec;
2786
2787 /* After editing .opd, adjust references to opd local syms. */
2788 long *adjust;
2789 } opd;
7c8fe5c4 2790
3a71aa26
AM
2791 /* An array for toc sections, indexed by offset/8. */
2792 struct _toc_sec_data
2793 {
2794 /* Specifies the relocation symbol index used at a given toc offset. */
2795 unsigned *symndx;
2796
2797 /* And the relocation addend. */
2798 bfd_vma *add;
2799 } toc;
7c8fe5c4
AM
2800 } u;
2801
2802 enum _ppc64_sec_type sec_type:2;
411e1bfb 2803
7c8fe5c4
AM
2804 /* Flag set when small branches are detected. Used to
2805 select suitable defaults for the stub group size. */
2806 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2807};
2808
2809#define ppc64_elf_section_data(sec) \
411e1bfb 2810 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2811
2812static bfd_boolean
4ce794b7 2813ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2814{
f592407e
AM
2815 if (!sec->used_by_bfd)
2816 {
2817 struct _ppc64_elf_section_data *sdata;
2818 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2819
f592407e
AM
2820 sdata = bfd_zalloc (abfd, amt);
2821 if (sdata == NULL)
2822 return FALSE;
2823 sec->used_by_bfd = sdata;
2824 }
f0abc2a1
AM
2825
2826 return _bfd_elf_new_section_hook (abfd, sec);
2827}
4025353c 2828
74f0fb50 2829static struct _opd_sec_data *
4025353c
AM
2830get_opd_info (asection * sec)
2831{
2832 if (sec != NULL
2833 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2834 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2835 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2836 return NULL;
2837}
90e3cdf2
JJ
2838\f
2839/* Parameters for the qsort hook. */
90e3cdf2
JJ
2840static bfd_boolean synthetic_relocatable;
2841
699733f6 2842/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2843
2844static int
2845compare_symbols (const void *ap, const void *bp)
2846{
2847 const asymbol *a = * (const asymbol **) ap;
2848 const asymbol *b = * (const asymbol **) bp;
2849
699733f6
AM
2850 /* Section symbols first. */
2851 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2852 return -1;
699733f6 2853 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2854 return 1;
2855
699733f6 2856 /* then .opd symbols. */
ffcfec52
AM
2857 if (strcmp (a->section->name, ".opd") == 0
2858 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2859 return -1;
ffcfec52
AM
2860 if (strcmp (a->section->name, ".opd") != 0
2861 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2862 return 1;
2863
699733f6 2864 /* then other code symbols. */
90e3cdf2
JJ
2865 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2866 == (SEC_CODE | SEC_ALLOC)
2867 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2868 != (SEC_CODE | SEC_ALLOC))
2869 return -1;
2870
2871 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2872 != (SEC_CODE | SEC_ALLOC)
2873 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2874 == (SEC_CODE | SEC_ALLOC))
2875 return 1;
2876
2877 if (synthetic_relocatable)
2878 {
2879 if (a->section->id < b->section->id)
2880 return -1;
2881
2882 if (a->section->id > b->section->id)
2883 return 1;
2884 }
2885
2886 if (a->value + a->section->vma < b->value + b->section->vma)
2887 return -1;
2888
2889 if (a->value + a->section->vma > b->value + b->section->vma)
2890 return 1;
2891
4d35a0aa
AM
2892 /* For syms with the same value, prefer strong dynamic global function
2893 syms over other syms. */
2894 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2895 return -1;
2896
2897 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2898 return 1;
2899
2900 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2901 return -1;
2902
2903 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2904 return 1;
2905
2906 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2907 return -1;
2908
2909 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2910 return 1;
2911
2912 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2913 return -1;
2914
2915 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2916 return 1;
2917
90e3cdf2
JJ
2918 return 0;
2919}
2920
699733f6 2921/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2922
699733f6
AM
2923static asymbol *
2924sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2925{
699733f6 2926 long mid;
90e3cdf2 2927
699733f6
AM
2928 if (id == -1)
2929 {
2930 while (lo < hi)
2931 {
2932 mid = (lo + hi) >> 1;
2933 if (syms[mid]->value + syms[mid]->section->vma < value)
2934 lo = mid + 1;
2935 else if (syms[mid]->value + syms[mid]->section->vma > value)
2936 hi = mid;
2937 else
2938 return syms[mid];
2939 }
2940 }
2941 else
2942 {
2943 while (lo < hi)
2944 {
2945 mid = (lo + hi) >> 1;
2946 if (syms[mid]->section->id < id)
2947 lo = mid + 1;
2948 else if (syms[mid]->section->id > id)
2949 hi = mid;
2950 else if (syms[mid]->value < value)
2951 lo = mid + 1;
2952 else if (syms[mid]->value > value)
2953 hi = mid;
2954 else
2955 return syms[mid];
2956 }
2957 }
2958 return NULL;
90e3cdf2
JJ
2959}
2960
468392fb
AM
2961static bfd_boolean
2962section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2963{
2964 bfd_vma vma = *(bfd_vma *) ptr;
2965 return ((section->flags & SEC_ALLOC) != 0
2966 && section->vma <= vma
2967 && vma < section->vma + section->size);
2968}
2969
699733f6 2970/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 2971 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
2972
2973static long
a7535cf3
AM
2974ppc64_elf_get_synthetic_symtab (bfd *abfd,
2975 long static_count, asymbol **static_syms,
2976 long dyn_count, asymbol **dyn_syms,
c9727e01 2977 asymbol **ret)
90e3cdf2
JJ
2978{
2979 asymbol *s;
699733f6
AM
2980 long i;
2981 long count;
90e3cdf2 2982 char *names;
a7535cf3 2983 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2984 asection *opd;
90e3cdf2 2985 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2986 asymbol **syms;
90e3cdf2
JJ
2987
2988 *ret = NULL;
2989
2990 opd = bfd_get_section_by_name (abfd, ".opd");
2991 if (opd == NULL)
2992 return 0;
2993
a7535cf3 2994 symcount = static_count;
c9727e01 2995 if (!relocatable)
a7535cf3 2996 symcount += dyn_count;
90e3cdf2 2997 if (symcount == 0)
c9727e01 2998 return 0;
90e3cdf2 2999
a7535cf3
AM
3000 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3001 if (syms == NULL)
7356fed5 3002 return -1;
a7535cf3
AM
3003
3004 if (!relocatable && static_count != 0 && dyn_count != 0)
3005 {
3006 /* Use both symbol tables. */
3007 memcpy (syms, static_syms, static_count * sizeof (*syms));
3008 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3009 }
3010 else if (!relocatable && static_count == 0)
3011 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3012 else
3013 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3014
90e3cdf2 3015 synthetic_relocatable = relocatable;
595da8c5 3016 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3017
c9727e01
AM
3018 if (!relocatable && symcount > 1)
3019 {
3020 long j;
3021 /* Trim duplicate syms, since we may have merged the normal and
3022 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3023 different values, so trim any with the same value. */
c9727e01
AM
3024 for (i = 1, j = 1; i < symcount; ++i)
3025 if (syms[i - 1]->value + syms[i - 1]->section->vma
3026 != syms[i]->value + syms[i]->section->vma)
3027 syms[j++] = syms[i];
3028 symcount = j;
3029 }
3030
699733f6 3031 i = 0;
ffcfec52 3032 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3033 ++i;
3034 codesecsym = i;
90e3cdf2 3035
699733f6
AM
3036 for (; i < symcount; ++i)
3037 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3038 != (SEC_CODE | SEC_ALLOC))
3039 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3040 break;
3041 codesecsymend = i;
90e3cdf2 3042
699733f6
AM
3043 for (; i < symcount; ++i)
3044 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3045 break;
3046 secsymend = i;
90e3cdf2 3047
699733f6 3048 for (; i < symcount; ++i)
ffcfec52 3049 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3050 break;
3051 opdsymend = i;
90e3cdf2 3052
699733f6
AM
3053 for (; i < symcount; ++i)
3054 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3055 != (SEC_CODE | SEC_ALLOC))
3056 break;
3057 symcount = i;
3058
c9727e01 3059 count = 0;
90e3cdf2 3060
699733f6 3061 if (relocatable)
90e3cdf2 3062 {
699733f6
AM
3063 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3064 arelent *r;
3065 size_t size;
3066 long relcount;
90e3cdf2 3067
468392fb
AM
3068 if (opdsymend == secsymend)
3069 goto done;
3070
699733f6 3071 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3072 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3073 if (relcount == 0)
c9727e01 3074 goto done;
90e3cdf2 3075
7356fed5
AM
3076 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3077 {
3078 count = -1;
3079 goto done;
3080 }
3081
699733f6 3082 size = 0;
595da8c5 3083 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3084 {
3085 asymbol *sym;
90e3cdf2 3086
595da8c5 3087 while (r < opd->relocation + relcount
699733f6
AM
3088 && r->address < syms[i]->value + opd->vma)
3089 ++r;
90e3cdf2 3090
595da8c5 3091 if (r == opd->relocation + relcount)
699733f6 3092 break;
90e3cdf2 3093
699733f6
AM
3094 if (r->address != syms[i]->value + opd->vma)
3095 continue;
90e3cdf2 3096
699733f6
AM
3097 if (r->howto->type != R_PPC64_ADDR64)
3098 continue;
90e3cdf2 3099
699733f6
AM
3100 sym = *r->sym_ptr_ptr;
3101 if (!sym_exists_at (syms, opdsymend, symcount,
3102 sym->section->id, sym->value + r->addend))
3103 {
3104 ++count;
3105 size += sizeof (asymbol);
3106 size += strlen (syms[i]->name) + 2;
3107 }
3108 }
90e3cdf2 3109
699733f6
AM
3110 s = *ret = bfd_malloc (size);
3111 if (s == NULL)
3112 {
7356fed5 3113 count = -1;
c9727e01 3114 goto done;
699733f6 3115 }
90e3cdf2 3116
699733f6 3117 names = (char *) (s + count);
90e3cdf2 3118
595da8c5 3119 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3120 {
699733f6 3121 asymbol *sym;
90e3cdf2 3122
595da8c5 3123 while (r < opd->relocation + relcount
699733f6
AM
3124 && r->address < syms[i]->value + opd->vma)
3125 ++r;
90e3cdf2 3126
595da8c5 3127 if (r == opd->relocation + relcount)
699733f6
AM
3128 break;
3129
3130 if (r->address != syms[i]->value + opd->vma)
3131 continue;
3132
3133 if (r->howto->type != R_PPC64_ADDR64)
3134 continue;
90e3cdf2 3135
699733f6
AM
3136 sym = *r->sym_ptr_ptr;
3137 if (!sym_exists_at (syms, opdsymend, symcount,
3138 sym->section->id, sym->value + r->addend))
3139 {
3140 size_t len;
3141
3142 *s = *syms[i];
6ba2a415 3143 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3144 s->section = sym->section;
3145 s->value = sym->value + r->addend;
3146 s->name = names;
3147 *names++ = '.';
3148 len = strlen (syms[i]->name);
3149 memcpy (names, syms[i]->name, len + 1);
3150 names += len + 1;
6f610d07
UW
3151 /* Have udata.p point back to the original symbol this
3152 synthetic symbol was derived from. */
3153 s->udata.p = syms[i];
699733f6
AM
3154 s++;
3155 }
3156 }
3157 }
3158 else
90e3cdf2 3159 {
468392fb 3160 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
699733f6
AM
3161 bfd_byte *contents;
3162 size_t size;
468392fb
AM
3163 long plt_count = 0;
3164 bfd_vma glink_vma = 0, resolv_vma = 0;
3165 asection *dynamic, *glink = NULL, *relplt = NULL;
3166 arelent *p;
90e3cdf2 3167
699733f6
AM
3168 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3169 {
3170 if (contents)
c9727e01
AM
3171 {
3172 free_contents_and_exit:
3173 free (contents);
3174 }
7356fed5 3175 count = -1;
c9727e01 3176 goto done;
699733f6 3177 }
90e3cdf2 3178
699733f6
AM
3179 size = 0;
3180 for (i = secsymend; i < opdsymend; ++i)
3181 {
3182 bfd_vma ent;
90e3cdf2 3183
5ef11c02
AM
3184 /* Ignore bogus symbols. */
3185 if (syms[i]->value > opd->size - 8)
3186 continue;
3187
699733f6
AM
3188 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3189 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3190 {
3191 ++count;
3192 size += sizeof (asymbol);
3193 size += strlen (syms[i]->name) + 2;
3194 }
3195 }
90e3cdf2 3196
468392fb 3197 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3198 if (dyn_count != 0
3199 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3200 {
3201 bfd_byte *dynbuf, *extdyn, *extdynend;
3202 size_t extdynsize;
3203 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3204
3205 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3206 goto free_contents_and_exit;
3207
3208 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3209 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3210
3211 extdyn = dynbuf;
3212 extdynend = extdyn + dynamic->size;
3213 for (; extdyn < extdynend; extdyn += extdynsize)
3214 {
3215 Elf_Internal_Dyn dyn;
3216 (*swap_dyn_in) (abfd, extdyn, &dyn);
3217
3218 if (dyn.d_tag == DT_NULL)
3219 break;
3220
3221 if (dyn.d_tag == DT_PPC64_GLINK)
3222 {
3223 /* The first glink stub starts at offset 32; see comment in
3224 ppc64_elf_finish_dynamic_sections. */
3225 glink_vma = dyn.d_un.d_val + 32;
3226 /* The .glink section usually does not survive the final
3227 link; search for the section (usually .text) where the
3228 glink stubs now reside. */
3229 glink = bfd_sections_find_if (abfd, section_covers_vma,
3230 &glink_vma);
3231 break;
3232 }
3233 }
3234
3235 free (dynbuf);
3236 }
3237
3238 if (glink != NULL)
3239 {
3240 /* Determine __glink trampoline by reading the relative branch
3241 from the first glink stub. */
3242 bfd_byte buf[4];
3243 if (bfd_get_section_contents (abfd, glink, buf,
3244 glink_vma + 4 - glink->vma, 4))
3245 {
3246 unsigned int insn = bfd_get_32 (abfd, buf);
3247 insn ^= B_DOT;
3248 if ((insn & ~0x3fffffc) == 0)
3249 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3250 }
3251
3252 if (resolv_vma)
3253 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3254
066ee829
AM
3255 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3256 if (relplt != NULL)
3257 {
3258 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3259 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3260 goto free_contents_and_exit;
468392fb 3261
066ee829
AM
3262 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3263 size += plt_count * sizeof (asymbol);
468392fb 3264
066ee829
AM
3265 p = relplt->relocation;
3266 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3267 {
3268 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3269 if (p->addend != 0)
3270 size += sizeof ("+0x") - 1 + 16;
3271 }
066ee829 3272 }
468392fb
AM
3273 }
3274
699733f6
AM
3275 s = *ret = bfd_malloc (size);
3276 if (s == NULL)
7356fed5 3277 goto free_contents_and_exit;
90e3cdf2 3278
468392fb 3279 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3280
699733f6 3281 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3282 {
699733f6 3283 bfd_vma ent;
90e3cdf2 3284
5ef11c02
AM
3285 if (syms[i]->value > opd->size - 8)
3286 continue;
3287
699733f6
AM
3288 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3289 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3290 {
c9727e01 3291 long lo, hi;
699733f6 3292 size_t len;
c9727e01 3293 asection *sec = abfd->sections;
90e3cdf2 3294
699733f6
AM
3295 *s = *syms[i];
3296 lo = codesecsym;
3297 hi = codesecsymend;
3298 while (lo < hi)
3299 {
c9727e01 3300 long mid = (lo + hi) >> 1;
699733f6
AM
3301 if (syms[mid]->section->vma < ent)
3302 lo = mid + 1;
3303 else if (syms[mid]->section->vma > ent)
3304 hi = mid;
3305 else
c9727e01
AM
3306 {
3307 sec = syms[mid]->section;
3308 break;
3309 }
699733f6
AM
3310 }
3311
c9727e01 3312 if (lo >= hi && lo > codesecsym)
699733f6 3313 sec = syms[lo - 1]->section;
699733f6
AM
3314
3315 for (; sec != NULL; sec = sec->next)
3316 {
3317 if (sec->vma > ent)
3318 break;
63524580
JK
3319 /* SEC_LOAD may not be set if SEC is from a separate debug
3320 info file. */
3321 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3322 break;
3323 if ((sec->flags & SEC_CODE) != 0)
3324 s->section = sec;
3325 }
6ba2a415 3326 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3327 s->value = ent - s->section->vma;
3328 s->name = names;
3329 *names++ = '.';
3330 len = strlen (syms[i]->name);
3331 memcpy (names, syms[i]->name, len + 1);
3332 names += len + 1;
6f610d07
UW
3333 /* Have udata.p point back to the original symbol this
3334 synthetic symbol was derived from. */
3335 s->udata.p = syms[i];
699733f6 3336 s++;
90e3cdf2 3337 }
90e3cdf2 3338 }
699733f6 3339 free (contents);
468392fb
AM
3340
3341 if (glink != NULL && relplt != NULL)
3342 {
3343 if (resolv_vma)
3344 {
3345 /* Add a symbol for the main glink trampoline. */
86a4952b 3346 memset (s, 0, sizeof *s);
468392fb 3347 s->the_bfd = abfd;
6ba2a415 3348 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3349 s->section = glink;
3350 s->value = resolv_vma - glink->vma;
3351 s->name = names;
3352 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3353 names += sizeof ("__glink_PLTresolve");
3354 s++;
3355 count++;
3356 }
3357
3358 /* FIXME: It would be very much nicer to put sym@plt on the
3359 stub rather than on the glink branch table entry. The
3360 objdump disassembler would then use a sensible symbol
3361 name on plt calls. The difficulty in doing so is
3362 a) finding the stubs, and,
3363 b) matching stubs against plt entries, and,
3364 c) there can be multiple stubs for a given plt entry.
3365
3366 Solving (a) could be done by code scanning, but older
3367 ppc64 binaries used different stubs to current code.
3368 (b) is the tricky one since you need to known the toc
3369 pointer for at least one function that uses a pic stub to
3370 be able to calculate the plt address referenced.
3371 (c) means gdb would need to set multiple breakpoints (or
3372 find the glink branch itself) when setting breakpoints
3373 for pending shared library loads. */
3374 p = relplt->relocation;
3375 for (i = 0; i < plt_count; i++, p++)
3376 {
3377 size_t len;
3378
3379 *s = **p->sym_ptr_ptr;
3380 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3381 we are defining a symbol, ensure one of them is set. */
3382 if ((s->flags & BSF_LOCAL) == 0)
3383 s->flags |= BSF_GLOBAL;
6ba2a415 3384 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3385 s->section = glink;
3386 s->value = glink_vma - glink->vma;
3387 s->name = names;
3388 s->udata.p = NULL;
3389 len = strlen ((*p->sym_ptr_ptr)->name);
3390 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3391 names += len;
e054468f
AM
3392 if (p->addend != 0)
3393 {
3394 memcpy (names, "+0x", sizeof ("+0x") - 1);
3395 names += sizeof ("+0x") - 1;
3396 bfd_sprintf_vma (abfd, names, p->addend);
3397 names += strlen (names);
3398 }
468392fb
AM
3399 memcpy (names, "@plt", sizeof ("@plt"));
3400 names += sizeof ("@plt");
3401 s++;
3402 glink_vma += 8;
3403 if (i >= 0x8000)
3404 glink_vma += 4;
3405 }
3406 count += plt_count;
3407 }
90e3cdf2
JJ
3408 }
3409
c9727e01 3410 done:
a7535cf3 3411 free (syms);
90e3cdf2
JJ
3412 return count;
3413}
5bd4f169 3414\f
65f38f15
AM
3415/* The following functions are specific to the ELF linker, while
3416 functions above are used generally. Those named ppc64_elf_* are
3417 called by the main ELF linker code. They appear in this file more
3418 or less in the order in which they are called. eg.
3419 ppc64_elf_check_relocs is called early in the link process,
3420 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3421 called.
3422
3423 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3424 functions have both a function code symbol and a function descriptor
3425 symbol. A call to foo in a relocatable object file looks like:
3426
3427 . .text
3428 . x:
3429 . bl .foo
3430 . nop
3431
3432 The function definition in another object file might be:
3433
3434 . .section .opd
3435 . foo: .quad .foo
3436 . .quad .TOC.@tocbase
3437 . .quad 0
3438 .
3439 . .text
3440 . .foo: blr
3441
3442 When the linker resolves the call during a static link, the branch
3443 unsurprisingly just goes to .foo and the .opd information is unused.
3444 If the function definition is in a shared library, things are a little
3445 different: The call goes via a plt call stub, the opd information gets
3446 copied to the plt, and the linker patches the nop.
3447
3448 . x:
3449 . bl .foo_stub
3450 . ld 2,40(1)
3451 .
3452 .
3453 . .foo_stub:
3454 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 3455 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
3456 . std 2,40(1) # this is the general idea
3457 . ld 11,0(12)
3458 . ld 2,8(12)
3459 . mtctr 11
3460 . ld 11,16(12)
3461 . bctr
3462 .
3463 . .section .plt
3464 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3465
3466 The "reloc ()" notation is supposed to indicate that the linker emits
3467 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3468 copying.
3469
3470 What are the difficulties here? Well, firstly, the relocations
3471 examined by the linker in check_relocs are against the function code
3472 sym .foo, while the dynamic relocation in the plt is emitted against
3473 the function descriptor symbol, foo. Somewhere along the line, we need
3474 to carefully copy dynamic link information from one symbol to the other.
3475 Secondly, the generic part of the elf linker will make .foo a dynamic
3476 symbol as is normal for most other backends. We need foo dynamic
3477 instead, at least for an application final link. However, when
3478 creating a shared library containing foo, we need to have both symbols
3479 dynamic so that references to .foo are satisfied during the early
3480 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3481 definition from some other object, eg. a static library.
3482
3483 Update: As of August 2004, we support a new convention. Function
3484 calls may use the function descriptor symbol, ie. "bl foo". This
3485 behaves exactly as "bl .foo". */
65f38f15 3486
1d483afe 3487/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3488 selects those that must be copied when linking a shared library,
3489 even when the symbol is local. */
65f38f15 3490
1d483afe
AM
3491static int
3492must_be_dyn_reloc (struct bfd_link_info *info,
3493 enum elf_ppc64_reloc_type r_type)
3494{
3495 switch (r_type)
3496 {
3497 default:
3498 return 1;
3499
3500 case R_PPC64_REL32:
3501 case R_PPC64_REL64:
3502 case R_PPC64_REL30:
3503 return 0;
3504
3505 case R_PPC64_TPREL16:
3506 case R_PPC64_TPREL16_LO:
3507 case R_PPC64_TPREL16_HI:
3508 case R_PPC64_TPREL16_HA:
3509 case R_PPC64_TPREL16_DS:
3510 case R_PPC64_TPREL16_LO_DS:
3511 case R_PPC64_TPREL16_HIGHER:
3512 case R_PPC64_TPREL16_HIGHERA:
3513 case R_PPC64_TPREL16_HIGHEST:
3514 case R_PPC64_TPREL16_HIGHESTA:
3515 case R_PPC64_TPREL64:
3516 return !info->executable;
3517 }
3518}
65f38f15 3519
f4656909
AM
3520/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3521 copying dynamic variables from a shared lib into an app's dynbss
3522 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3523 shared lib. With code that gcc generates, it's vital that this be
3524 enabled; In the PowerPC64 ABI, the address of a function is actually
3525 the address of a function descriptor, which resides in the .opd
3526 section. gcc uses the descriptor directly rather than going via the
3527 GOT as some other ABI's do, which means that initialized function
3528 pointers must reference the descriptor. Thus, a function pointer
3529 initialized to the address of a function in a shared library will
3530 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3531 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3532 presents a problem as a plt entry for that function is also
3533 initialized from the function descriptor symbol and the copy reloc
3534 may not be initialized first. */
a23b6845 3535#define ELIMINATE_COPY_RELOCS 1
f4656909 3536
721956f4 3537/* Section name for stubs is the associated section name plus this
29942be8
NC
3538 string. */
3539#define STUB_SUFFIX ".stub"
721956f4
AM
3540
3541/* Linker stubs.
3542 ppc_stub_long_branch:
3543 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3544 destination, but a 24 bit branch in a stub section will reach.
3545 . b dest
3546
3547 ppc_stub_plt_branch:
3548 Similar to the above, but a 24 bit branch in the stub section won't
3549 reach its destination.
87e226ce
AM
3550 . addis %r12,%r2,xxx@toc@ha
3551 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3552 . mtctr %r11
3553 . bctr
3554
3555 ppc_stub_plt_call:
2c66dc6c
AM
3556 Used to call a function in a shared library. If it so happens that
3557 the plt entry referenced crosses a 64k boundary, then an extra
ac2df442 3558 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
87e226ce 3559 . addis %r12,%r2,xxx@toc@ha
721956f4 3560 . std %r2,40(%r1)
87e226ce 3561 . ld %r11,xxx+0@toc@l(%r12)
721956f4 3562 . mtctr %r11
ac2df442 3563 . ld %r2,xxx+8@toc@l(%r12)
87e226ce 3564 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3565 . bctr
ad8e1ba5
AM
3566
3567 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3568 code to adjust the value and save r2 to support multiple toc sections.
3569 A ppc_stub_long_branch with an r2 offset looks like:
3570 . std %r2,40(%r1)
3571 . addis %r2,%r2,off@ha
3572 . addi %r2,%r2,off@l
3573 . b dest
3574
3575 A ppc_stub_plt_branch with an r2 offset looks like:
3576 . std %r2,40(%r1)
3577 . addis %r12,%r2,xxx@toc@ha
3578 . ld %r11,xxx@toc@l(%r12)
3579 . addis %r2,%r2,off@ha
3580 . addi %r2,%r2,off@l
3581 . mtctr %r11
3582 . bctr
ac2df442
AM
3583
3584 In cases where the "addis" instruction would add zero, the "addis" is
3585 omitted and following instructions modified slightly in some cases.
721956f4
AM
3586*/
3587
3588enum ppc_stub_type {
3589 ppc_stub_none,
3590 ppc_stub_long_branch,
ad8e1ba5 3591 ppc_stub_long_branch_r2off,
721956f4 3592 ppc_stub_plt_branch,
ad8e1ba5 3593 ppc_stub_plt_branch_r2off,
794e51c0
AM
3594 ppc_stub_plt_call,
3595 ppc_stub_plt_call_r2save
721956f4
AM
3596};
3597
3598struct ppc_stub_hash_entry {
3599
3600 /* Base hash table entry structure. */
3601 struct bfd_hash_entry root;
3602
ad8e1ba5
AM
3603 enum ppc_stub_type stub_type;
3604
721956f4
AM
3605 /* The stub section. */
3606 asection *stub_sec;
3607
3608 /* Offset within stub_sec of the beginning of this stub. */
3609 bfd_vma stub_offset;
3610
3611 /* Given the symbol's value and its section we can determine its final
3612 value when building the stubs (so the stub knows where to jump. */
3613 bfd_vma target_value;
3614 asection *target_section;
3615
721956f4
AM
3616 /* The symbol table entry, if any, that this was derived from. */
3617 struct ppc_link_hash_entry *h;
e054468f 3618 struct plt_entry *plt_ent;
721956f4 3619
411e1bfb
AM
3620 /* And the reloc addend that this was derived from. */
3621 bfd_vma addend;
3622
721956f4
AM
3623 /* Where this stub is being called from, or, in the case of combined
3624 stub sections, the first input section in the group. */
3625 asection *id_sec;
3626};
3627
3628struct ppc_branch_hash_entry {
3629
3630 /* Base hash table entry structure. */
3631 struct bfd_hash_entry root;
3632
c456f082 3633 /* Offset within branch lookup table. */
721956f4
AM
3634 unsigned int offset;
3635
3636 /* Generation marker. */
3637 unsigned int iter;
3638};
65f38f15
AM
3639
3640struct ppc_link_hash_entry
3641{
3642 struct elf_link_hash_entry elf;
3643
b3fac117
AM
3644 union {
3645 /* A pointer to the most recently used stub hash entry against this
3646 symbol. */
3647 struct ppc_stub_hash_entry *stub_cache;
3648
3649 /* A pointer to the next symbol starting with a '.' */
3650 struct ppc_link_hash_entry *next_dot_sym;
3651 } u;
721956f4 3652
65f38f15 3653 /* Track dynamic relocs copied for this symbol. */
6061a67d 3654 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3655
721956f4 3656 /* Link between function code and descriptor symbols. */
34814b9f 3657 struct ppc_link_hash_entry *oh;
721956f4 3658
e86ce104
AM
3659 /* Flag function code and descriptor symbols. */
3660 unsigned int is_func:1;
3661 unsigned int is_func_descriptor:1;
908b32fc 3662 unsigned int fake:1;
411e1bfb 3663
c5614fa4
AM
3664 /* Whether global opd/toc sym has been adjusted or not.
3665 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3666 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3667 unsigned int adjust_done:1;
3668
99877b66
AM
3669 /* Set if we twiddled this symbol to weak at some stage. */
3670 unsigned int was_undefined:1;
3671
411e1bfb 3672 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3673 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3674 corresponding relocs are encountered during check_relocs.
3675 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3676 indicate the corresponding GOT entry type is not needed.
3677 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3678 a TPREL one. We use a separate flag rather than setting TPREL
3679 just for convenience in distinguishing the two cases. */
3680#define TLS_GD 1 /* GD reloc. */
3681#define TLS_LD 2 /* LD reloc. */
3682#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3683#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3684#define TLS_TLS 16 /* Any TLS reloc. */
3685#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3686#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3687#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3688 unsigned char tls_mask;
65f38f15
AM
3689};
3690
3691/* ppc64 ELF linker hash table. */
3692
3693struct ppc_link_hash_table
3694{
3695 struct elf_link_hash_table elf;
3696
721956f4
AM
3697 /* The stub hash table. */
3698 struct bfd_hash_table stub_hash_table;
3699
3700 /* Another hash table for plt_branch stubs. */
3701 struct bfd_hash_table branch_hash_table;
3702
3b421ab3
AM
3703 /* Hash table for function prologue tocsave. */
3704 htab_t tocsave_htab;
3705
721956f4
AM
3706 /* Linker stub bfd. */
3707 bfd *stub_bfd;
3708
3709 /* Linker call-backs. */
4ce794b7
AM
3710 asection * (*add_stub_section) (const char *, asection *);
3711 void (*layout_sections_again) (void);
721956f4
AM
3712
3713 /* Array to keep track of which stub sections have been created, and
3714 information on stub grouping. */
3715 struct map_stub {
3716 /* This is the section to which stubs in the group will be attached. */
3717 asection *link_sec;
3718 /* The stub section. */
3719 asection *stub_sec;
ad8e1ba5
AM
3720 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3721 bfd_vma toc_off;
721956f4
AM
3722 } *stub_group;
3723
ad8e1ba5
AM
3724 /* Temp used when calculating TOC pointers. */
3725 bfd_vma toc_curr;
bf102f86
AM
3726 bfd *toc_bfd;
3727 asection *toc_first_sec;
ad8e1ba5 3728
8f3bab57
AM
3729 /* Highest input section id. */
3730 int top_id;
3731
734b6cf9
AM
3732 /* Highest output section index. */
3733 int top_index;
3734
b3fac117
AM
3735 /* Used when adding symbols. */
3736 struct ppc_link_hash_entry *dot_syms;
3737
734b6cf9
AM
3738 /* List of input sections for each output section. */
3739 asection **input_list;
721956f4 3740
65f38f15 3741 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3742 asection *got;
4ce794b7
AM
3743 asection *plt;
3744 asection *relplt;
e054468f
AM
3745 asection *iplt;
3746 asection *reliplt;
4ce794b7
AM
3747 asection *dynbss;
3748 asection *relbss;
3749 asection *glink;
82bd7b59 3750 asection *sfpr;
4ce794b7
AM
3751 asection *brlt;
3752 asection *relbrlt;
58d180e8 3753 asection *glink_eh_frame;
ec338859 3754
8387904d
AM
3755 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3756 struct ppc_link_hash_entry *tls_get_addr;
3757 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3758
927be08e
AM
3759 /* The size of reliplt used by got entry relocs. */
3760 bfd_size_type got_reli_size;
3761
9b5ecbd0 3762 /* Statistics. */
794e51c0 3763 unsigned long stub_count[ppc_stub_plt_call_r2save];
9b5ecbd0 3764
ee75fd95
AM
3765 /* Number of stubs against global syms. */
3766 unsigned long stub_globals;
3767
794e51c0
AM
3768 /* Alignment of PLT call stubs. */
3769 unsigned int plt_stub_align:4;
3770
9df0ef5f
AM
3771 /* Set if PLT call stubs should load r11. */
3772 unsigned int plt_static_chain:1;
3773
794e51c0
AM
3774 /* Set if PLT call stubs need a read-read barrier. */
3775 unsigned int plt_thread_safe:1;
3776
ad8e1ba5 3777 /* Set if we should emit symbols for stubs. */
99877b66 3778 unsigned int emit_stub_syms:1;
ad8e1ba5 3779
a7f2871e
AM
3780 /* Set if __tls_get_addr optimization should not be done. */
3781 unsigned int no_tls_get_addr_opt:1;
3782
4c52953f 3783 /* Support for multiple toc sections. */
33c0ec9d 3784 unsigned int do_multi_toc:1;
4c52953f 3785 unsigned int multi_toc_needed:1;
927be08e 3786 unsigned int second_toc_pass:1;
67f0cbdb 3787 unsigned int do_toc_opt:1;
4c52953f 3788
5d1634d7 3789 /* Set on error. */
99877b66 3790 unsigned int stub_error:1;
721956f4 3791
7d9616d7 3792 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3793 unsigned int twiddled_syms:1;
721956f4
AM
3794
3795 /* Incremented every time we size stubs. */
3796 unsigned int stub_iteration;
5d1634d7 3797
87d72d41
AM
3798 /* Small local sym cache. */
3799 struct sym_cache sym_cache;
65f38f15
AM
3800};
3801
4c52953f
AM
3802/* Rename some of the generic section flags to better document how they
3803 are used here. */
b0dddeec
AM
3804
3805/* Nonzero if this section has TLS related relocations. */
3806#define has_tls_reloc sec_flg0
3807
3808/* Nonzero if this section has a call to __tls_get_addr. */
3809#define has_tls_get_addr_call sec_flg1
3810
3811/* Nonzero if this section has any toc or got relocs. */
3812#define has_toc_reloc sec_flg2
3813
3814/* Nonzero if this section has a call to another section that uses
3815 the toc or got. */
d77c8a4b 3816#define makes_toc_func_call sec_flg3
b0dddeec
AM
3817
3818/* Recursion protection when determining above flag. */
d77c8a4b 3819#define call_check_in_progress sec_flg4
70cc837d 3820#define call_check_done sec_flg5
4c52953f 3821
65f38f15
AM
3822/* Get the ppc64 ELF linker hash table from a link_info structure. */
3823
3824#define ppc_hash_table(p) \
4dfe6ac6
NC
3825 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3826 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3827
721956f4
AM
3828#define ppc_stub_hash_lookup(table, string, create, copy) \
3829 ((struct ppc_stub_hash_entry *) \
3830 bfd_hash_lookup ((table), (string), (create), (copy)))
3831
3832#define ppc_branch_hash_lookup(table, string, create, copy) \
3833 ((struct ppc_branch_hash_entry *) \
3834 bfd_hash_lookup ((table), (string), (create), (copy)))
3835
3836/* Create an entry in the stub hash table. */
3837
3838static struct bfd_hash_entry *
4ce794b7
AM
3839stub_hash_newfunc (struct bfd_hash_entry *entry,
3840 struct bfd_hash_table *table,
3841 const char *string)
721956f4
AM
3842{
3843 /* Allocate the structure if it has not already been allocated by a
3844 subclass. */
3845 if (entry == NULL)
3846 {
3847 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3848 if (entry == NULL)
3849 return entry;
3850 }
3851
3852 /* Call the allocation method of the superclass. */
3853 entry = bfd_hash_newfunc (entry, table, string);
3854 if (entry != NULL)
3855 {
3856 struct ppc_stub_hash_entry *eh;
3857
3858 /* Initialize the local fields. */
3859 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3860 eh->stub_type = ppc_stub_none;
721956f4
AM
3861 eh->stub_sec = NULL;
3862 eh->stub_offset = 0;
3863 eh->target_value = 0;
3864 eh->target_section = NULL;
721956f4
AM
3865 eh->h = NULL;
3866 eh->id_sec = NULL;
3867 }
3868
3869 return entry;
3870}
3871
3872/* Create an entry in the branch hash table. */
3873
3874static struct bfd_hash_entry *
4ce794b7
AM
3875branch_hash_newfunc (struct bfd_hash_entry *entry,
3876 struct bfd_hash_table *table,
3877 const char *string)
721956f4
AM
3878{
3879 /* Allocate the structure if it has not already been allocated by a
3880 subclass. */
3881 if (entry == NULL)
3882 {
3883 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3884 if (entry == NULL)
3885 return entry;
3886 }
3887
3888 /* Call the allocation method of the superclass. */
3889 entry = bfd_hash_newfunc (entry, table, string);
3890 if (entry != NULL)
3891 {
3892 struct ppc_branch_hash_entry *eh;
3893
3894 /* Initialize the local fields. */
3895 eh = (struct ppc_branch_hash_entry *) entry;
3896 eh->offset = 0;
3897 eh->iter = 0;
3898 }
3899
3900 return entry;
3901}
3902
65f38f15
AM
3903/* Create an entry in a ppc64 ELF linker hash table. */
3904
3905static struct bfd_hash_entry *
4ce794b7
AM
3906link_hash_newfunc (struct bfd_hash_entry *entry,
3907 struct bfd_hash_table *table,
3908 const char *string)
65f38f15
AM
3909{
3910 /* Allocate the structure if it has not already been allocated by a
3911 subclass. */
3912 if (entry == NULL)
3913 {
3914 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3915 if (entry == NULL)
3916 return entry;
3917 }
3918
3919 /* Call the allocation method of the superclass. */
3920 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3921 if (entry != NULL)
3922 {
3923 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3924
b3fac117 3925 memset (&eh->u.stub_cache, 0,
908b32fc 3926 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3927 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3928
3929 /* When making function calls, old ABI code references function entry
3930 points (dot symbols), while new ABI code references the function
3931 descriptor symbol. We need to make any combination of reference and
3932 definition work together, without breaking archive linking.
3933
3934 For a defined function "foo" and an undefined call to "bar":
3935 An old object defines "foo" and ".foo", references ".bar" (possibly
3936 "bar" too).
3937 A new object defines "foo" and references "bar".
3938
3939 A new object thus has no problem with its undefined symbols being
3940 satisfied by definitions in an old object. On the other hand, the
3941 old object won't have ".bar" satisfied by a new object.
3942
3943 Keep a list of newly added dot-symbols. */
3944
3945 if (string[0] == '.')
3946 {
3947 struct ppc_link_hash_table *htab;
3948
3949 htab = (struct ppc_link_hash_table *) table;
3950 eh->u.next_dot_sym = htab->dot_syms;
3951 htab->dot_syms = eh;
3952 }
65f38f15
AM
3953 }
3954
3955 return entry;
3956}
3957
3b421ab3
AM
3958struct tocsave_entry {
3959 asection *sec;
3960 bfd_vma offset;
3961};
3962
3963static hashval_t
3964tocsave_htab_hash (const void *p)
3965{
3966 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3967 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
3968}
3969
3970static int
3971tocsave_htab_eq (const void *p1, const void *p2)
3972{
3973 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3974 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3975 return e1->sec == e2->sec && e1->offset == e2->offset;
3976}
3977
65f38f15
AM
3978/* Create a ppc64 ELF linker hash table. */
3979
3980static struct bfd_link_hash_table *
4ce794b7 3981ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3982{
3983 struct ppc_link_hash_table *htab;
3984 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3985
4ce794b7 3986 htab = bfd_zmalloc (amt);
65f38f15
AM
3987 if (htab == NULL)
3988 return NULL;
3989
66eb6687 3990 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3991 sizeof (struct ppc_link_hash_entry),
3992 PPC64_ELF_DATA))
65f38f15 3993 {
e2d34d7d 3994 free (htab);
65f38f15
AM
3995 return NULL;
3996 }
3997
721956f4 3998 /* Init the stub hash table too. */
66eb6687
AM
3999 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4000 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
4001 return NULL;
4002
4003 /* And the branch hash table. */
66eb6687
AM
4004 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4005 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
4006 return NULL;
4007
3b421ab3
AM
4008 htab->tocsave_htab = htab_try_create (1024,
4009 tocsave_htab_hash,
4010 tocsave_htab_eq,
4011 NULL);
4012 if (htab->tocsave_htab == NULL)
4013 return NULL;
4014
3254fd24
AM
4015 /* Initializing two fields of the union is just cosmetic. We really
4016 only care about glist, but when compiled on a 32-bit host the
4017 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4018 debugger inspection of these fields look nicer. */
a6aa5195
AM
4019 htab->elf.init_got_refcount.refcount = 0;
4020 htab->elf.init_got_refcount.glist = NULL;
4021 htab->elf.init_plt_refcount.refcount = 0;
4022 htab->elf.init_plt_refcount.glist = NULL;
4023 htab->elf.init_got_offset.offset = 0;
4024 htab->elf.init_got_offset.glist = NULL;
4025 htab->elf.init_plt_offset.offset = 0;
4026 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4027
65f38f15
AM
4028 return &htab->elf.root;
4029}
4030
721956f4
AM
4031/* Free the derived linker hash table. */
4032
4033static void
4ce794b7 4034ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4 4035{
3b421ab3 4036 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
721956f4 4037
3b421ab3
AM
4038 bfd_hash_table_free (&htab->stub_hash_table);
4039 bfd_hash_table_free (&htab->branch_hash_table);
4040 if (htab->tocsave_htab)
4041 htab_delete (htab->tocsave_htab);
721956f4
AM
4042 _bfd_generic_link_hash_table_free (hash);
4043}
4044
e717da7e
AM
4045/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4046
4047void
4048ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4049{
4050 struct ppc_link_hash_table *htab;
4051
4052 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4053
4054/* Always hook our dynamic sections into the first bfd, which is the
4055 linker created stub bfd. This ensures that the GOT header is at
4056 the start of the output TOC section. */
4057 htab = ppc_hash_table (info);
4dfe6ac6
NC
4058 if (htab == NULL)
4059 return;
e717da7e
AM
4060 htab->stub_bfd = abfd;
4061 htab->elf.dynobj = abfd;
4062}
4063
721956f4
AM
4064/* Build a name for an entry in the stub hash table. */
4065
4066static char *
4ce794b7
AM
4067ppc_stub_name (const asection *input_section,
4068 const asection *sym_sec,
4069 const struct ppc_link_hash_entry *h,
4070 const Elf_Internal_Rela *rel)
721956f4
AM
4071{
4072 char *stub_name;
4073 bfd_size_type len;
4074
4075 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4076 offsets from a sym as a branch target? In fact, we could
4077 probably assume the addend is always zero. */
4078 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4079
4080 if (h)
4081 {
4082 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4083 stub_name = bfd_malloc (len);
46de2a7c
AM
4084 if (stub_name == NULL)
4085 return stub_name;
4086
4087 sprintf (stub_name, "%08x.%s+%x",
4088 input_section->id & 0xffffffff,
4089 h->elf.root.root.string,
4090 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4091 }
4092 else
4093 {
ad8e1ba5 4094 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4095 stub_name = bfd_malloc (len);
46de2a7c
AM
4096 if (stub_name == NULL)
4097 return stub_name;
4098
4099 sprintf (stub_name, "%08x.%x:%x+%x",
4100 input_section->id & 0xffffffff,
4101 sym_sec->id & 0xffffffff,
4102 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4103 (int) rel->r_addend & 0xffffffff);
721956f4 4104 }
ee75fd95
AM
4105 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4106 stub_name[len - 2] = 0;
721956f4
AM
4107 return stub_name;
4108}
4109
4110/* Look up an entry in the stub hash. Stub entries are cached because
4111 creating the stub name takes a bit of time. */
4112
4113static struct ppc_stub_hash_entry *
4ce794b7
AM
4114ppc_get_stub_entry (const asection *input_section,
4115 const asection *sym_sec,
039b3fef 4116 struct ppc_link_hash_entry *h,
4ce794b7
AM
4117 const Elf_Internal_Rela *rel,
4118 struct ppc_link_hash_table *htab)
721956f4
AM
4119{
4120 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4121 const asection *id_sec;
4122
4123 /* If this input section is part of a group of sections sharing one
4124 stub section, then use the id of the first section in the group.
4125 Stub names need to include a section id, as there may well be
4126 more than one stub used to reach say, printf, and we need to
4127 distinguish between them. */
4128 id_sec = htab->stub_group[input_section->id].link_sec;
4129
b3fac117
AM
4130 if (h != NULL && h->u.stub_cache != NULL
4131 && h->u.stub_cache->h == h
4132 && h->u.stub_cache->id_sec == id_sec)
721956f4 4133 {
b3fac117 4134 stub_entry = h->u.stub_cache;
721956f4
AM
4135 }
4136 else
4137 {
4138 char *stub_name;
4139
4140 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4141 if (stub_name == NULL)
4142 return NULL;
4143
4144 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4145 stub_name, FALSE, FALSE);
721956f4 4146 if (h != NULL)
b3fac117 4147 h->u.stub_cache = stub_entry;
721956f4
AM
4148
4149 free (stub_name);
4150 }
4151
4152 return stub_entry;
4153}
4154
4155/* Add a new stub entry to the stub hash. Not all fields of the new
4156 stub entry are initialised. */
4157
4158static struct ppc_stub_hash_entry *
4ce794b7
AM
4159ppc_add_stub (const char *stub_name,
4160 asection *section,
25f53a85 4161 struct bfd_link_info *info)
721956f4 4162{
25f53a85 4163 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4164 asection *link_sec;
4165 asection *stub_sec;
4166 struct ppc_stub_hash_entry *stub_entry;
4167
4168 link_sec = htab->stub_group[section->id].link_sec;
4169 stub_sec = htab->stub_group[section->id].stub_sec;
4170 if (stub_sec == NULL)
4171 {
4172 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4173 if (stub_sec == NULL)
4174 {
d4c88bbb 4175 size_t namelen;
721956f4
AM
4176 bfd_size_type len;
4177 char *s_name;
4178
d4c88bbb
AM
4179 namelen = strlen (link_sec->name);
4180 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4181 s_name = bfd_alloc (htab->stub_bfd, len);
4182 if (s_name == NULL)
4183 return NULL;
4184
d4c88bbb
AM
4185 memcpy (s_name, link_sec->name, namelen);
4186 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4187 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4188 if (stub_sec == NULL)
4189 return NULL;
4190 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4191 }
4192 htab->stub_group[section->id].stub_sec = stub_sec;
4193 }
4194
4195 /* Enter this entry into the linker stub hash table. */
4196 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4197 TRUE, FALSE);
721956f4
AM
4198 if (stub_entry == NULL)
4199 {
8de848d8 4200 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4201 section->owner, stub_name);
721956f4
AM
4202 return NULL;
4203 }
4204
4205 stub_entry->stub_sec = stub_sec;
4206 stub_entry->stub_offset = 0;
4207 stub_entry->id_sec = link_sec;
4208 return stub_entry;
4209}
4210
82bd7b59
AM
4211/* Create sections for linker generated code. */
4212
b34976b6 4213static bfd_boolean
4ce794b7 4214create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
4215{
4216 struct ppc_link_hash_table *htab;
4217 flagword flags;
4218
4219 htab = ppc_hash_table (info);
4dfe6ac6
NC
4220 if (htab == NULL)
4221 return FALSE;
82bd7b59
AM
4222
4223 /* Create .sfpr for code to save and restore fp regs. */
4224 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4225 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
4226 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4227 flags);
82bd7b59 4228 if (htab->sfpr == NULL
82bd7b59 4229 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 4230 return FALSE;
82bd7b59 4231
721956f4 4232 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
4233 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4234 flags);
4ce794b7 4235 if (htab->glink == NULL
ee4bf8d2 4236 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 4237 return FALSE;
82bd7b59 4238
58d180e8
AM
4239 if (!info->no_ld_generated_unwind_info)
4240 {
4241 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4242 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4243 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4244 ".eh_frame",
4245 flags);
4246 if (htab->glink_eh_frame == NULL
4247 || !bfd_set_section_alignment (abfd, htab->glink_eh_frame, 2))
4248 return FALSE;
4249 }
4250
e054468f
AM
4251 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4252 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4253 if (htab->iplt == NULL
4254 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4255 return FALSE;
4256
4257 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4258 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4259 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4260 ".rela.iplt",
4261 flags);
4262 if (htab->reliplt == NULL
4263 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4264 return FALSE;
4265
c456f082 4266 /* Create branch lookup table for plt_branch stubs. */
e4e0193e
AM
4267 flags = (SEC_ALLOC | SEC_LOAD
4268 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4269 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4270 flags);
4ce794b7 4271 if (htab->brlt == NULL
4ce794b7 4272 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 4273 return FALSE;
721956f4 4274
e4e0193e 4275 if (!info->shared)
c456f082
AM
4276 return TRUE;
4277
e4e0193e
AM
4278 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4279 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4280 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4281 ".rela.branch_lt",
4282 flags);
e054468f 4283 if (htab->relbrlt == NULL
c456f082
AM
4284 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4285 return FALSE;
4286
b34976b6 4287 return TRUE;
82bd7b59
AM
4288}
4289
e717da7e
AM
4290/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4291 not already done. */
65f38f15 4292
b34976b6 4293static bfd_boolean
e717da7e 4294create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4295{
e717da7e
AM
4296 asection *got, *relgot;
4297 flagword flags;
4298 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4299
0c8d6e5c 4300 if (!is_ppc64_elf (abfd))
0ffa91dd 4301 return FALSE;
4dfe6ac6
NC
4302 if (htab == NULL)
4303 return FALSE;
0ffa91dd 4304
4ce794b7 4305 if (!htab->got)
e717da7e
AM
4306 {
4307 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4308 return FALSE;
4309
4310 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4311 if (!htab->got)
4312 abort ();
4313 }
4314
4315 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4316 | SEC_LINKER_CREATED);
4317
c456f082 4318 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4319 if (!got
e717da7e
AM
4320 || !bfd_set_section_alignment (abfd, got, 3))
4321 return FALSE;
65f38f15 4322
c456f082
AM
4323 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4324 flags | SEC_READONLY);
e717da7e 4325 if (!relgot
e717da7e 4326 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4327 return FALSE;
e717da7e
AM
4328
4329 ppc64_elf_tdata (abfd)->got = got;
4330 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4331 return TRUE;
65f38f15 4332}
5bd4f169 4333
82bd7b59 4334/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4335
b34976b6 4336static bfd_boolean
4ce794b7 4337ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4338{
65f38f15 4339 struct ppc_link_hash_table *htab;
5bd4f169 4340
65f38f15 4341 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4342 return FALSE;
65f38f15 4343
e717da7e 4344 htab = ppc_hash_table (info);
4dfe6ac6
NC
4345 if (htab == NULL)
4346 return FALSE;
4347
e717da7e
AM
4348 if (!htab->got)
4349 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
4350 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4351 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4352 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 4353 if (!info->shared)
4ce794b7 4354 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 4355
e717da7e 4356 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4357 || (!info->shared && !htab->relbss))
65f38f15
AM
4358 abort ();
4359
b34976b6 4360 return TRUE;
5bd4f169
AM
4361}
4362
b31867b6
AM
4363/* Follow indirect and warning symbol links. */
4364
4365static inline struct bfd_link_hash_entry *
4366follow_link (struct bfd_link_hash_entry *h)
4367{
4368 while (h->type == bfd_link_hash_indirect
4369 || h->type == bfd_link_hash_warning)
4370 h = h->u.i.link;
4371 return h;
4372}
4373
4374static inline struct elf_link_hash_entry *
4375elf_follow_link (struct elf_link_hash_entry *h)
4376{
4377 return (struct elf_link_hash_entry *) follow_link (&h->root);
4378}
4379
4380static inline struct ppc_link_hash_entry *
4381ppc_follow_link (struct ppc_link_hash_entry *h)
4382{
4383 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4384}
4385
40d16e0b
AM
4386/* Merge PLT info on FROM with that on TO. */
4387
4388static void
4389move_plt_plist (struct ppc_link_hash_entry *from,
4390 struct ppc_link_hash_entry *to)
4391{
4392 if (from->elf.plt.plist != NULL)
4393 {
4394 if (to->elf.plt.plist != NULL)
4395 {
4396 struct plt_entry **entp;
4397 struct plt_entry *ent;
4398
4399 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4400 {
4401 struct plt_entry *dent;
4402
4403 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4404 if (dent->addend == ent->addend)
4405 {
4406 dent->plt.refcount += ent->plt.refcount;
4407 *entp = ent->next;
4408 break;
4409 }
4410 if (dent == NULL)
4411 entp = &ent->next;
4412 }
4413 *entp = to->elf.plt.plist;
4414 }
4415
4416 to->elf.plt.plist = from->elf.plt.plist;
4417 from->elf.plt.plist = NULL;
4418 }
4419}
4420
65f38f15
AM
4421/* Copy the extra info we tack onto an elf_link_hash_entry. */
4422
4423static void
fcfa13d2
AM
4424ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4425 struct elf_link_hash_entry *dir,
4426 struct elf_link_hash_entry *ind)
65f38f15
AM
4427{
4428 struct ppc_link_hash_entry *edir, *eind;
4429
4430 edir = (struct ppc_link_hash_entry *) dir;
4431 eind = (struct ppc_link_hash_entry *) ind;
4432
c79d6685
AM
4433 edir->is_func |= eind->is_func;
4434 edir->is_func_descriptor |= eind->is_func_descriptor;
4435 edir->tls_mask |= eind->tls_mask;
4436 if (eind->oh != NULL)
4437 edir->oh = ppc_follow_link (eind->oh);
4438
4439 /* If called to transfer flags for a weakdef during processing
4440 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4441 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4442 if (!(ELIMINATE_COPY_RELOCS
4443 && eind->elf.root.type != bfd_link_hash_indirect
4444 && edir->elf.dynamic_adjusted))
4445 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4446
4447 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4448 edir->elf.ref_regular |= eind->elf.ref_regular;
4449 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4450 edir->elf.needs_plt |= eind->elf.needs_plt;
4451
411e1bfb 4452 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4453 if (eind->dyn_relocs != NULL)
65f38f15 4454 {
bbd7ec4a
AM
4455 if (edir->dyn_relocs != NULL)
4456 {
6061a67d
AM
4457 struct elf_dyn_relocs **pp;
4458 struct elf_dyn_relocs *p;
bbd7ec4a 4459
fcfa13d2 4460 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4461 list. Merge any entries against the same section. */
4462 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4463 {
6061a67d 4464 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4465
4466 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4467 if (q->sec == p->sec)
4468 {
4469 q->pc_count += p->pc_count;
4470 q->count += p->count;
4471 *pp = p->next;
4472 break;
4473 }
4474 if (q == NULL)
4475 pp = &p->next;
4476 }
4477 *pp = edir->dyn_relocs;
4478 }
4479
65f38f15
AM
4480 edir->dyn_relocs = eind->dyn_relocs;
4481 eind->dyn_relocs = NULL;
4482 }
65f38f15 4483
68ba6d40
AM
4484 /* If we were called to copy over info for a weak sym, that's all.
4485 You might think dyn_relocs need not be copied over; After all,
4486 both syms will be dynamic or both non-dynamic so we're just
4487 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
4488 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4489 dyn_relocs in read-only sections, and it does so on what is the
4490 DIR sym here. */
4491 if (eind->elf.root.type != bfd_link_hash_indirect)
4492 return;
4493
81848ca0
AM
4494 /* Copy over got entries that we may have already seen to the
4495 symbol which just became indirect. */
411e1bfb
AM
4496 if (eind->elf.got.glist != NULL)
4497 {
4498 if (edir->elf.got.glist != NULL)
4499 {
4500 struct got_entry **entp;
4501 struct got_entry *ent;
4502
4503 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4504 {
4505 struct got_entry *dent;
4506
4507 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4508 if (dent->addend == ent->addend
e717da7e 4509 && dent->owner == ent->owner
411e1bfb
AM
4510 && dent->tls_type == ent->tls_type)
4511 {
4512 dent->got.refcount += ent->got.refcount;
4513 *entp = ent->next;
4514 break;
4515 }
4516 if (dent == NULL)
4517 entp = &ent->next;
4518 }
4519 *entp = edir->elf.got.glist;
4520 }
4521
4522 edir->elf.got.glist = eind->elf.got.glist;
4523 eind->elf.got.glist = NULL;
4524 }
4525
4526 /* And plt entries. */
40d16e0b 4527 move_plt_plist (eind, edir);
411e1bfb 4528
fcfa13d2 4529 if (eind->elf.dynindx != -1)
411e1bfb 4530 {
fcfa13d2
AM
4531 if (edir->elf.dynindx != -1)
4532 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4533 edir->elf.dynstr_index);
411e1bfb
AM
4534 edir->elf.dynindx = eind->elf.dynindx;
4535 edir->elf.dynstr_index = eind->elf.dynstr_index;
4536 eind->elf.dynindx = -1;
4537 eind->elf.dynstr_index = 0;
4538 }
411e1bfb
AM
4539}
4540
8387904d
AM
4541/* Find the function descriptor hash entry from the given function code
4542 hash entry FH. Link the entries via their OH fields. */
4543
4544static struct ppc_link_hash_entry *
b31867b6 4545lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4546{
4547 struct ppc_link_hash_entry *fdh = fh->oh;
4548
4549 if (fdh == NULL)
4550 {
4551 const char *fd_name = fh->elf.root.root.string + 1;
4552
4553 fdh = (struct ppc_link_hash_entry *)
4554 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4555 if (fdh == NULL)
4556 return fdh;
4557
4558 fdh->is_func_descriptor = 1;
4559 fdh->oh = fh;
4560 fh->is_func = 1;
4561 fh->oh = fdh;
8387904d
AM
4562 }
4563
b31867b6 4564 return ppc_follow_link (fdh);
8387904d
AM
4565}
4566
bb700d78
AM
4567/* Make a fake function descriptor sym for the code sym FH. */
4568
4569static struct ppc_link_hash_entry *
4570make_fdh (struct bfd_link_info *info,
908b32fc 4571 struct ppc_link_hash_entry *fh)
bb700d78
AM
4572{
4573 bfd *abfd;
4574 asymbol *newsym;
4575 struct bfd_link_hash_entry *bh;
4576 struct ppc_link_hash_entry *fdh;
4577
4578 abfd = fh->elf.root.u.undef.abfd;
4579 newsym = bfd_make_empty_symbol (abfd);
4580 newsym->name = fh->elf.root.root.string + 1;
4581 newsym->section = bfd_und_section_ptr;
4582 newsym->value = 0;
908b32fc 4583 newsym->flags = BSF_WEAK;
bb700d78
AM
4584
4585 bh = NULL;
4586 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4587 newsym->flags, newsym->section,
4588 newsym->value, NULL, FALSE, FALSE,
4589 &bh))
4590 return NULL;
4591
4592 fdh = (struct ppc_link_hash_entry *) bh;
4593 fdh->elf.non_elf = 0;
908b32fc
AM
4594 fdh->fake = 1;
4595 fdh->is_func_descriptor = 1;
4596 fdh->oh = fh;
4597 fh->is_func = 1;
4598 fh->oh = fdh;
bb700d78
AM
4599 return fdh;
4600}
4601
8387904d
AM
4602/* Fix function descriptor symbols defined in .opd sections to be
4603 function type. */
555cd476
AM
4604
4605static bfd_boolean
c16153ae 4606ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4607 struct bfd_link_info *info,
555cd476 4608 Elf_Internal_Sym *isym,
b3fac117 4609 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4610 flagword *flags ATTRIBUTE_UNUSED,
4611 asection **sec,
4612 bfd_vma *value ATTRIBUTE_UNUSED)
4613{
f64b2e8d
NC
4614 if ((ibfd->flags & DYNAMIC) == 0
4615 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4616 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4617
e054468f 4618 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4619 {
4620 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4621 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4622 }
e054468f
AM
4623 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4624 ;
4625 else if (*sec != NULL
70cc837d 4626 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4627 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4628
555cd476
AM
4629 return TRUE;
4630}
4631
8387904d 4632/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4633 inclusion of a new ABI object archive that defines "bar".
4634 NAME is a symbol defined in an archive. Return a symbol in the hash
4635 table that might be satisfied by the archive symbols. */
8387904d
AM
4636
4637static struct elf_link_hash_entry *
4638ppc64_elf_archive_symbol_lookup (bfd *abfd,
4639 struct bfd_link_info *info,
4640 const char *name)
4641{
4642 struct elf_link_hash_entry *h;
4643 char *dot_name;
4644 size_t len;
4645
4646 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4647 if (h != NULL
4648 /* Don't return this sym if it is a fake function descriptor
4649 created by add_symbol_adjust. */
4650 && !(h->root.type == bfd_link_hash_undefweak
4651 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4652 return h;
4653
4654 if (name[0] == '.')
4655 return h;
4656
4657 len = strlen (name);
4658 dot_name = bfd_alloc (abfd, len + 2);
4659 if (dot_name == NULL)
4660 return (struct elf_link_hash_entry *) 0 - 1;
4661 dot_name[0] = '.';
4662 memcpy (dot_name + 1, name, len + 1);
4663 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4664 bfd_release (abfd, dot_name);
4665 return h;
4666}
4667
4668/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4669 new ABI object defines "bar". Well, at least, undefined dot symbols
4670 are made weak. This stops later archive searches from including an
4671 object if we already have a function descriptor definition. It also
35b0ce59
AM
4672 prevents the linker complaining about undefined symbols.
4673 We also check and correct mismatched symbol visibility here. The
4674 most restrictive visibility of the function descriptor and the
4675 function entry symbol is used. */
8387904d
AM
4676
4677static bfd_boolean
b3fac117 4678add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4679{
8387904d
AM
4680 struct ppc_link_hash_table *htab;
4681 struct ppc_link_hash_entry *fdh;
4682
b3fac117 4683 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4684 return TRUE;
4685
b3fac117
AM
4686 if (eh->elf.root.type == bfd_link_hash_warning)
4687 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4688
b3fac117
AM
4689 if (eh->elf.root.root.string[0] != '.')
4690 abort ();
8387904d 4691
b3fac117 4692 htab = ppc_hash_table (info);
4dfe6ac6
NC
4693 if (htab == NULL)
4694 return FALSE;
4695
b31867b6
AM
4696 fdh = lookup_fdh (eh, htab);
4697 if (fdh == NULL)
4698 {
4699 if (!info->relocatable
4700 && (eh->elf.root.type == bfd_link_hash_undefined
4701 || eh->elf.root.type == bfd_link_hash_undefweak)
4702 && eh->elf.ref_regular)
4703 {
4704 /* Make an undefweak function descriptor sym, which is enough to
4705 pull in an --as-needed shared lib, but won't cause link
4706 errors. Archives are handled elsewhere. */
4707 fdh = make_fdh (info, eh);
4708 if (fdh == NULL)
4709 return FALSE;
4710 fdh->elf.ref_regular = 1;
4711 }
bb700d78 4712 }
b31867b6 4713 else
8387904d 4714 {
35b0ce59
AM
4715 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4716 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4717 if (entry_vis < descr_vis)
4718 fdh->elf.other += entry_vis - descr_vis;
4719 else if (entry_vis > descr_vis)
4720 eh->elf.other += descr_vis - entry_vis;
4721
e87d886e
AM
4722 if ((fdh->elf.root.type == bfd_link_hash_defined
4723 || fdh->elf.root.type == bfd_link_hash_defweak)
4724 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4725 {
4726 eh->elf.root.type = bfd_link_hash_undefweak;
4727 eh->was_undefined = 1;
4728 htab->twiddled_syms = 1;
4729 }
8387904d 4730 }
99877b66 4731
8387904d
AM
4732 return TRUE;
4733}
4734
b3fac117
AM
4735/* Process list of dot-symbols we made in link_hash_newfunc. */
4736
8387904d 4737static bfd_boolean
7d9616d7 4738ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4739{
99877b66 4740 struct ppc_link_hash_table *htab;
b3fac117 4741 struct ppc_link_hash_entry **p, *eh;
433817dd 4742
0c8d6e5c 4743 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4744 return TRUE;
4dfe6ac6
NC
4745 htab = ppc_hash_table (info);
4746 if (htab == NULL)
4747 return FALSE;
35b0ce59 4748
0c8d6e5c 4749 if (is_ppc64_elf (ibfd))
b3fac117
AM
4750 {
4751 p = &htab->dot_syms;
4752 while ((eh = *p) != NULL)
4753 {
4754 *p = NULL;
4755 if (!add_symbol_adjust (eh, info))
4756 return FALSE;
4757 p = &eh->u.next_dot_sym;
4758 }
4759 }
4760
4761 /* Clear the list for non-ppc64 input files. */
4762 p = &htab->dot_syms;
4763 while ((eh = *p) != NULL)
4764 {
4765 *p = NULL;
4766 p = &eh->u.next_dot_sym;
4767 }
99877b66
AM
4768
4769 /* We need to fix the undefs list for any syms we have twiddled to
4770 undef_weak. */
4771 if (htab->twiddled_syms)
4772 {
77cfaee6 4773 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4774 htab->twiddled_syms = 0;
4775 }
b3fac117 4776 return TRUE;
8387904d
AM
4777}
4778
97fed1c9
JJ
4779/* Undo hash table changes when an --as-needed input file is determined
4780 not to be needed. */
4781
4782static bfd_boolean
4783ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4784 struct bfd_link_info *info)
4785{
4dfe6ac6
NC
4786 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4787
4788 if (htab == NULL)
4789 return FALSE;
4790
4791 htab->dot_syms = NULL;
97fed1c9
JJ
4792 return TRUE;
4793}
4794
aa374f67
AM
4795/* If --just-symbols against a final linked binary, then assume we need
4796 toc adjusting stubs when calling functions defined there. */
4797
4798static void
4799ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4800{
4801 if ((sec->flags & SEC_CODE) != 0
4802 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4803 && is_ppc64_elf (sec->owner))
4804 {
4805 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4806 if (got != NULL
4807 && got->size >= elf_backend_got_header_size
4808 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4809 sec->has_toc_reloc = 1;
4810 }
4811 _bfd_elf_link_just_syms (sec, info);
4812}
4813
e054468f 4814static struct plt_entry **
4ce794b7
AM
4815update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4816 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4817{
4818 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4819 struct plt_entry **local_plt;
f961d9dd 4820 unsigned char *local_got_tls_masks;
411e1bfb
AM
4821
4822 if (local_got_ents == NULL)
4823 {
4824 bfd_size_type size = symtab_hdr->sh_info;
4825
e054468f
AM
4826 size *= (sizeof (*local_got_ents)
4827 + sizeof (*local_plt)
4828 + sizeof (*local_got_tls_masks));
4ce794b7 4829 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4830 if (local_got_ents == NULL)
e054468f 4831 return NULL;
411e1bfb
AM
4832 elf_local_got_ents (abfd) = local_got_ents;
4833 }
4834
e054468f 4835 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4836 {
4837 struct got_entry *ent;
4838
4839 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4840 if (ent->addend == r_addend
4841 && ent->owner == abfd
4842 && ent->tls_type == tls_type)
411e1bfb
AM
4843 break;
4844 if (ent == NULL)
4845 {
4846 bfd_size_type amt = sizeof (*ent);
4ce794b7 4847 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4848 if (ent == NULL)
4849 return FALSE;
4850 ent->next = local_got_ents[r_symndx];
4851 ent->addend = r_addend;
e717da7e 4852 ent->owner = abfd;
411e1bfb 4853 ent->tls_type = tls_type;
927be08e 4854 ent->is_indirect = FALSE;
411e1bfb
AM
4855 ent->got.refcount = 0;
4856 local_got_ents[r_symndx] = ent;
4857 }
4858 ent->got.refcount += 1;
4859 }
4860
e054468f 4861 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4862 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 4863 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
4864
4865 return local_plt + r_symndx;
65f38f15
AM
4866}
4867
411e1bfb 4868static bfd_boolean
e054468f 4869update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4870{
411e1bfb 4871 struct plt_entry *ent;
1e2f5b6e 4872
e054468f 4873 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4874 if (ent->addend == addend)
4875 break;
4876 if (ent == NULL)
1e2f5b6e 4877 {
411e1bfb 4878 bfd_size_type amt = sizeof (*ent);
4ce794b7 4879 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4880 if (ent == NULL)
4881 return FALSE;
e054468f 4882 ent->next = *plist;
411e1bfb
AM
4883 ent->addend = addend;
4884 ent->plt.refcount = 0;
e054468f 4885 *plist = ent;
1e2f5b6e 4886 }
411e1bfb 4887 ent->plt.refcount += 1;
b34976b6 4888 return TRUE;
1e2f5b6e
AM
4889}
4890
e054468f
AM
4891static bfd_boolean
4892is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4893{
4894 return (r_type == R_PPC64_REL24
4895 || r_type == R_PPC64_REL14
4896 || r_type == R_PPC64_REL14_BRTAKEN
4897 || r_type == R_PPC64_REL14_BRNTAKEN
4898 || r_type == R_PPC64_ADDR24
4899 || r_type == R_PPC64_ADDR14
4900 || r_type == R_PPC64_ADDR14_BRTAKEN
4901 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4902}
4903
5bd4f169 4904/* Look through the relocs for a section during the first phase, and
65f38f15 4905 calculate needed space in the global offset table, procedure
5d1634d7 4906 linkage table, and dynamic reloc sections. */
5bd4f169 4907
b34976b6 4908static bfd_boolean
4ce794b7
AM
4909ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4910 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4911{
65f38f15 4912 struct ppc_link_hash_table *htab;
5bd4f169 4913 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4914 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4915 const Elf_Internal_Rela *rel;
4916 const Elf_Internal_Rela *rel_end;
5bd4f169 4917 asection *sreloc;
1e2f5b6e 4918 asection **opd_sym_map;
3a71aa26 4919 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 4920
1049f94e 4921 if (info->relocatable)
b34976b6 4922 return TRUE;
5bd4f169 4923
680a3378
AM
4924 /* Don't do anything special with non-loaded, non-alloced sections.
4925 In particular, any relocs in such sections should not affect GOT
4926 and PLT reference counting (ie. we don't allow them to create GOT
4927 or PLT entries), there's no possibility or desire to optimize TLS
4928 relocs, and there's not much point in propagating relocs to shared
4929 libs that the dynamic linker won't relocate. */
4930 if ((sec->flags & SEC_ALLOC) == 0)
4931 return TRUE;
4932
0c8d6e5c 4933 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4934
65f38f15 4935 htab = ppc_hash_table (info);
4dfe6ac6
NC
4936 if (htab == NULL)
4937 return FALSE;
4938
3a71aa26
AM
4939 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4940 FALSE, FALSE, TRUE);
4941 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4942 FALSE, FALSE, TRUE);
0ffa91dd 4943 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4944 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4945 sreloc = NULL;
1e2f5b6e 4946 opd_sym_map = NULL;
70cc837d 4947 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
4948 {
4949 /* Garbage collection needs some extra help with .opd sections.
4950 We don't want to necessarily keep everything referenced by
4951 relocs in .opd, as that would keep all functions. Instead,
4952 if we reference an .opd symbol (a function descriptor), we
4953 want to keep the function code symbol's section. This is
4954 easy for global symbols, but for local syms we need to keep
74f0fb50 4955 information about the associated function section. */
1e2f5b6e
AM
4956 bfd_size_type amt;
4957
74f0fb50 4958 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 4959 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4960 if (opd_sym_map == NULL)
b34976b6 4961 return FALSE;
74f0fb50 4962 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
4963 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4964 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4965 }
5bd4f169 4966
82bd7b59
AM
4967 if (htab->sfpr == NULL
4968 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4969 return FALSE;
82bd7b59 4970
5bd4f169
AM
4971 rel_end = relocs + sec->reloc_count;
4972 for (rel = relocs; rel < rel_end; rel++)
4973 {
4974 unsigned long r_symndx;
4975 struct elf_link_hash_entry *h;
04c9666a 4976 enum elf_ppc64_reloc_type r_type;
727fc41e 4977 int tls_type;
7c8fe5c4 4978 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 4979 struct plt_entry **ifunc;
5bd4f169
AM
4980
4981 r_symndx = ELF64_R_SYM (rel->r_info);
4982 if (r_symndx < symtab_hdr->sh_info)
4983 h = NULL;
4984 else
973a3492
L
4985 {
4986 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4987 h = elf_follow_link (h);
973a3492 4988 }
5bd4f169 4989
727fc41e 4990 tls_type = 0;
e054468f 4991 ifunc = NULL;
25f23106
AM
4992 if (h != NULL)
4993 {
4994 if (h->type == STT_GNU_IFUNC)
4995 {
4996 h->needs_plt = 1;
4997 ifunc = &h->plt.plist;
4998 }
4999 }
5000 else
5001 {
5002 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5003 abfd, r_symndx);
5004 if (isym == NULL)
5005 return FALSE;
5006
5007 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5008 {
5009 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5010 rel->r_addend, PLT_IFUNC);
5011 if (ifunc == NULL)
5012 return FALSE;
5013 }
5014 }
4ce794b7 5015 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
5016 if (is_branch_reloc (r_type))
5017 {
5018 if (h != NULL && (h == tga || h == dottga))
5019 {
5020 if (rel != relocs
5021 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5022 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5023 /* We have a new-style __tls_get_addr call with a marker
5024 reloc. */
5025 ;
5026 else
5027 /* Mark this section as having an old-style call. */
5028 sec->has_tls_get_addr_call = 1;
5029 }
727fc41e 5030
e054468f 5031 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5032 if (ifunc != NULL
5033 && !update_plt_info (abfd, ifunc, rel->r_addend))
5034 return FALSE;
5035 }
727fc41e 5036
a33d1f77 5037 switch (r_type)
5bd4f169 5038 {
727fc41e
AM
5039 case R_PPC64_TLSGD:
5040 case R_PPC64_TLSLD:
5041 /* These special tls relocs tie a call to __tls_get_addr with
5042 its parameter symbol. */
5043 break;
5044
411e1bfb
AM
5045 case R_PPC64_GOT_TLSLD16:
5046 case R_PPC64_GOT_TLSLD16_LO:
5047 case R_PPC64_GOT_TLSLD16_HI:
5048 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5049 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5050 goto dogottls;
5051
5052 case R_PPC64_GOT_TLSGD16:
5053 case R_PPC64_GOT_TLSGD16_LO:
5054 case R_PPC64_GOT_TLSGD16_HI:
5055 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5056 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5057 goto dogottls;
5058
5059 case R_PPC64_GOT_TPREL16_DS:
5060 case R_PPC64_GOT_TPREL16_LO_DS:
5061 case R_PPC64_GOT_TPREL16_HI:
5062 case R_PPC64_GOT_TPREL16_HA:
1d483afe 5063 if (!info->executable)
411e1bfb
AM
5064 info->flags |= DF_STATIC_TLS;
5065 tls_type = TLS_TLS | TLS_TPREL;
5066 goto dogottls;
5067
5068 case R_PPC64_GOT_DTPREL16_DS:
5069 case R_PPC64_GOT_DTPREL16_LO_DS:
5070 case R_PPC64_GOT_DTPREL16_HI:
5071 case R_PPC64_GOT_DTPREL16_HA:
5072 tls_type = TLS_TLS | TLS_DTPREL;
5073 dogottls:
5074 sec->has_tls_reloc = 1;
5075 /* Fall thru */
5076
5bd4f169 5077 case R_PPC64_GOT16:
5bd4f169 5078 case R_PPC64_GOT16_DS:
65f38f15
AM
5079 case R_PPC64_GOT16_HA:
5080 case R_PPC64_GOT16_HI:
5081 case R_PPC64_GOT16_LO:
5bd4f169 5082 case R_PPC64_GOT16_LO_DS:
65f38f15 5083 /* This symbol requires a global offset table entry. */
4c52953f 5084 sec->has_toc_reloc = 1;
33c0ec9d
AM
5085 if (r_type == R_PPC64_GOT_TLSLD16
5086 || r_type == R_PPC64_GOT_TLSGD16
5087 || r_type == R_PPC64_GOT_TPREL16_DS
5088 || r_type == R_PPC64_GOT_DTPREL16_DS
5089 || r_type == R_PPC64_GOT16
5090 || r_type == R_PPC64_GOT16_DS)
5091 {
5092 htab->do_multi_toc = 1;
d77c8a4b 5093 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5094 }
5095
e717da7e
AM
5096 if (ppc64_elf_tdata (abfd)->got == NULL
5097 && !create_got_section (abfd, info))
b34976b6 5098 return FALSE;
5bd4f169
AM
5099
5100 if (h != NULL)
5101 {
411e1bfb
AM
5102 struct ppc_link_hash_entry *eh;
5103 struct got_entry *ent;
65f38f15 5104
411e1bfb
AM
5105 eh = (struct ppc_link_hash_entry *) h;
5106 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5107 if (ent->addend == rel->r_addend
e717da7e 5108 && ent->owner == abfd
411e1bfb
AM
5109 && ent->tls_type == tls_type)
5110 break;
5111 if (ent == NULL)
5bd4f169 5112 {
411e1bfb 5113 bfd_size_type amt = sizeof (*ent);
4ce794b7 5114 ent = bfd_alloc (abfd, amt);
411e1bfb 5115 if (ent == NULL)
b34976b6 5116 return FALSE;
411e1bfb
AM
5117 ent->next = eh->elf.got.glist;
5118 ent->addend = rel->r_addend;
e717da7e 5119 ent->owner = abfd;
411e1bfb 5120 ent->tls_type = tls_type;
927be08e 5121 ent->is_indirect = FALSE;
411e1bfb
AM
5122 ent->got.refcount = 0;
5123 eh->elf.got.glist = ent;
5bd4f169 5124 }
411e1bfb 5125 ent->got.refcount += 1;
e7b938ca 5126 eh->tls_mask |= tls_type;
5bd4f169 5127 }
411e1bfb
AM
5128 else
5129 /* This is a global offset table entry for a local symbol. */
5130 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5131 rel->r_addend, tls_type))
5132 return FALSE;
5bd4f169
AM
5133 break;
5134
5bd4f169 5135 case R_PPC64_PLT16_HA:
65f38f15
AM
5136 case R_PPC64_PLT16_HI:
5137 case R_PPC64_PLT16_LO:
5138 case R_PPC64_PLT32:
5139 case R_PPC64_PLT64:
5bd4f169 5140 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5141 actually build the entry in adjust_dynamic_symbol,
5142 because this might be a case of linking PIC code without
5143 linking in any dynamic objects, in which case we don't
5144 need to generate a procedure linkage table after all. */
5bd4f169
AM
5145 if (h == NULL)
5146 {
5147 /* It does not make sense to have a procedure linkage
3fad3c7c 5148 table entry for a local symbol. */
5bd4f169 5149 bfd_set_error (bfd_error_bad_value);
b34976b6 5150 return FALSE;
5bd4f169 5151 }
411e1bfb 5152 else
e054468f
AM
5153 {
5154 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5155 return FALSE;
5156 h->needs_plt = 1;
5157 if (h->root.root.string[0] == '.'
5158 && h->root.root.string[1] != '\0')
5159 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5160 }
5bd4f169
AM
5161 break;
5162
5163 /* The following relocations don't need to propagate the
5164 relocation if linking a shared object since they are
5165 section relative. */
5166 case R_PPC64_SECTOFF:
5167 case R_PPC64_SECTOFF_LO:
5168 case R_PPC64_SECTOFF_HI:
5169 case R_PPC64_SECTOFF_HA:
5170 case R_PPC64_SECTOFF_DS:
5171 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5172 case R_PPC64_DTPREL16:
5173 case R_PPC64_DTPREL16_LO:
5174 case R_PPC64_DTPREL16_HI:
5175 case R_PPC64_DTPREL16_HA:
5176 case R_PPC64_DTPREL16_DS:
5177 case R_PPC64_DTPREL16_LO_DS:
5178 case R_PPC64_DTPREL16_HIGHER:
5179 case R_PPC64_DTPREL16_HIGHERA:
5180 case R_PPC64_DTPREL16_HIGHEST:
5181 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5182 break;
5183
ad8e1ba5 5184 /* Nor do these. */
25f23106
AM
5185 case R_PPC64_REL16:
5186 case R_PPC64_REL16_LO:
5187 case R_PPC64_REL16_HI:
5188 case R_PPC64_REL16_HA:
5189 break;
5190
ad8e1ba5 5191 case R_PPC64_TOC16:
33c0ec9d
AM
5192 case R_PPC64_TOC16_DS:
5193 htab->do_multi_toc = 1;
d77c8a4b 5194 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5195 case R_PPC64_TOC16_LO:
5196 case R_PPC64_TOC16_HI:
5197 case R_PPC64_TOC16_HA:
ad8e1ba5 5198 case R_PPC64_TOC16_LO_DS:
4c52953f 5199 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5200 break;
5201
5bd4f169
AM
5202 /* This relocation describes the C++ object vtable hierarchy.
5203 Reconstruct it for later use during GC. */
5204 case R_PPC64_GNU_VTINHERIT:
c152c796 5205 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5206 return FALSE;
5bd4f169
AM
5207 break;
5208
5209 /* This relocation describes which C++ vtable entries are actually
5210 used. Record for later use during GC. */
5211 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5212 BFD_ASSERT (h != NULL);
5213 if (h != NULL
5214 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5215 return FALSE;
5bd4f169
AM
5216 break;
5217
721956f4
AM
5218 case R_PPC64_REL14:
5219 case R_PPC64_REL14_BRTAKEN:
5220 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5221 {
5222 asection *dest = NULL;
5223
5224 /* Heuristic: If jumping outside our section, chances are
5225 we are going to need a stub. */
5226 if (h != NULL)
5227 {
5228 /* If the sym is weak it may be overridden later, so
5229 don't assume we know where a weak sym lives. */
5230 if (h->root.type == bfd_link_hash_defined)
5231 dest = h->root.u.def.section;
5232 }
5233 else
87d72d41
AM
5234 {
5235 Elf_Internal_Sym *isym;
5236
5237 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5238 abfd, r_symndx);
5239 if (isym == NULL)
5240 return FALSE;
5241
5242 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5243 }
5244
220c76dd 5245 if (dest != sec)
7c8fe5c4 5246 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5247 }
721956f4
AM
5248 /* Fall through. */
5249
5d1634d7 5250 case R_PPC64_REL24:
e054468f 5251 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5252 {
5253 /* We may need a .plt entry if the function this reloc
5254 refers to is in a shared lib. */
e054468f 5255 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5256 return FALSE;
e054468f
AM
5257 h->needs_plt = 1;
5258 if (h->root.root.string[0] == '.'
5259 && h->root.root.string[1] != '\0')
5260 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5261 if (h == tga || h == dottga)
411e1bfb 5262 sec->has_tls_reloc = 1;
411e1bfb
AM
5263 }
5264 break;
5265
5266 case R_PPC64_TPREL64:
5267 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5268 if (!info->executable)
411e1bfb
AM
5269 info->flags |= DF_STATIC_TLS;
5270 goto dotlstoc;
5271
5272 case R_PPC64_DTPMOD64:
5273 if (rel + 1 < rel_end
5274 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5275 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5276 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5277 else
951fd09b 5278 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5279 goto dotlstoc;
5280
5281 case R_PPC64_DTPREL64:
5282 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5283 if (rel != relocs
5284 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5285 && rel[-1].r_offset == rel->r_offset - 8)
5286 /* This is the second reloc of a dtpmod, dtprel pair.
5287 Don't mark with TLS_DTPREL. */
5288 goto dodyn;
5289
5290 dotlstoc:
5291 sec->has_tls_reloc = 1;
5292 if (h != NULL)
5293 {
5294 struct ppc_link_hash_entry *eh;
5295 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5296 eh->tls_mask |= tls_type;
411e1bfb
AM
5297 }
5298 else
5299 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5300 rel->r_addend, tls_type))
5301 return FALSE;
5302
7c8fe5c4
AM
5303 ppc64_sec = ppc64_elf_section_data (sec);
5304 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5305 {
3a71aa26
AM
5306 bfd_size_type amt;
5307
e7b938ca 5308 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5309 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5310 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5311 if (ppc64_sec->u.toc.symndx == NULL)
5312 return FALSE;
5313 amt = sec->size * sizeof (bfd_vma) / 8;
5314 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5315 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5316 return FALSE;
7c8fe5c4
AM
5317 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5318 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5319 }
5320 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5321 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5322 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5323
5324 /* Mark the second slot of a GD or LD entry.
5325 -1 to indicate GD and -2 to indicate LD. */
5326 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5327 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5328 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5329 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5330 goto dodyn;
5331
5332 case R_PPC64_TPREL16:
5333 case R_PPC64_TPREL16_LO:
5334 case R_PPC64_TPREL16_HI:
5335 case R_PPC64_TPREL16_HA:
5336 case R_PPC64_TPREL16_DS:
5337 case R_PPC64_TPREL16_LO_DS:
5338 case R_PPC64_TPREL16_HIGHER:
5339 case R_PPC64_TPREL16_HIGHERA:
5340 case R_PPC64_TPREL16_HIGHEST:
5341 case R_PPC64_TPREL16_HIGHESTA:
5342 if (info->shared)
5343 {
1d483afe
AM
5344 if (!info->executable)
5345 info->flags |= DF_STATIC_TLS;
411e1bfb 5346 goto dodyn;
5d1634d7
AM
5347 }
5348 break;
5349
e86ce104 5350 case R_PPC64_ADDR64:
1e2f5b6e 5351 if (opd_sym_map != NULL
1e2f5b6e 5352 && rel + 1 < rel_end
4ce794b7 5353 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5354 {
8387904d
AM
5355 if (h != NULL)
5356 {
5357 if (h->root.root.string[0] == '.'
5358 && h->root.root.string[1] != 0
b31867b6 5359 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5360 ;
5361 else
5362 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5363 }
5364 else
5365 {
5366 asection *s;
87d72d41 5367 Elf_Internal_Sym *isym;
1e2f5b6e 5368
87d72d41
AM
5369 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5370 abfd, r_symndx);
5371 if (isym == NULL)
8387904d 5372 return FALSE;
87d72d41
AM
5373
5374 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5375 if (s != NULL && s != sec)
3f764659 5376 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5377 }
1e2f5b6e 5378 }
e86ce104
AM
5379 /* Fall through. */
5380
04c9666a 5381 case R_PPC64_REL30:
5bd4f169 5382 case R_PPC64_REL32:
04c9666a 5383 case R_PPC64_REL64:
65f38f15
AM
5384 case R_PPC64_ADDR14:
5385 case R_PPC64_ADDR14_BRNTAKEN:
5386 case R_PPC64_ADDR14_BRTAKEN:
5387 case R_PPC64_ADDR16:
5388 case R_PPC64_ADDR16_DS:
5389 case R_PPC64_ADDR16_HA:
5390 case R_PPC64_ADDR16_HI:
5391 case R_PPC64_ADDR16_HIGHER:
5392 case R_PPC64_ADDR16_HIGHERA:
5393 case R_PPC64_ADDR16_HIGHEST:
5394 case R_PPC64_ADDR16_HIGHESTA:
5395 case R_PPC64_ADDR16_LO:
5396 case R_PPC64_ADDR16_LO_DS:
5397 case R_PPC64_ADDR24:
65f38f15 5398 case R_PPC64_ADDR32:
65f38f15
AM
5399 case R_PPC64_UADDR16:
5400 case R_PPC64_UADDR32:
5401 case R_PPC64_UADDR64:
5bd4f169 5402 case R_PPC64_TOC:
81848ca0
AM
5403 if (h != NULL && !info->shared)
5404 /* We may need a copy reloc. */
f5385ebf 5405 h->non_got_ref = 1;
81848ca0 5406
41bd81ab 5407 /* Don't propagate .opd relocs. */
1e2f5b6e 5408 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5409 break;
e86ce104 5410
65f38f15
AM
5411 /* If we are creating a shared library, and this is a reloc
5412 against a global symbol, or a non PC relative reloc
5413 against a local symbol, then we need to copy the reloc
5414 into the shared library. However, if we are linking with
5415 -Bsymbolic, we do not need to copy a reloc against a
5416 global symbol which is defined in an object we are
5417 including in the link (i.e., DEF_REGULAR is set). At
5418 this point we have not seen all the input files, so it is
5419 possible that DEF_REGULAR is not set now but will be set
5420 later (it is never cleared). In case of a weak definition,
5421 DEF_REGULAR may be cleared later by a strong definition in
5422 a shared library. We account for that possibility below by
f4656909 5423 storing information in the dyn_relocs field of the hash
65f38f15
AM
5424 table entry. A similar situation occurs when creating
5425 shared libraries and symbol visibility changes render the
5426 symbol local.
5427
5428 If on the other hand, we are creating an executable, we
5429 may need to keep relocations for symbols satisfied by a
5430 dynamic library if we manage to avoid copy relocs for the
5431 symbol. */
411e1bfb 5432 dodyn:
65f38f15 5433 if ((info->shared
1d483afe 5434 && (must_be_dyn_reloc (info, r_type)
65f38f15
AM
5435 || (h != NULL
5436 && (! info->symbolic
5437 || h->root.type == bfd_link_hash_defweak
f5385ebf 5438 || !h->def_regular))))
f4656909
AM
5439 || (ELIMINATE_COPY_RELOCS
5440 && !info->shared
65f38f15
AM
5441 && h != NULL
5442 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5443 || !h->def_regular))
5444 || (!info->shared
5445 && ifunc != NULL))
5bd4f169 5446 {
6061a67d
AM
5447 struct elf_dyn_relocs *p;
5448 struct elf_dyn_relocs **head;
ec338859 5449
65f38f15
AM
5450 /* We must copy these reloc types into the output file.
5451 Create a reloc section in dynobj and make room for
5452 this reloc. */
5bd4f169
AM
5453 if (sreloc == NULL)
5454 {
83bac4b0
NC
5455 sreloc = _bfd_elf_make_dynamic_reloc_section
5456 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5457
5bd4f169 5458 if (sreloc == NULL)
83bac4b0 5459 return FALSE;
5bd4f169
AM
5460 }
5461
65f38f15
AM
5462 /* If this is a global symbol, we count the number of
5463 relocations we need for this symbol. */
5464 if (h != NULL)
5465 {
ec338859 5466 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
5467 }
5468 else
5469 {
ec338859
AM
5470 /* Track dynamic relocs needed for local syms too.
5471 We really need local syms available to do this
5472 easily. Oh well. */
ec338859 5473 asection *s;
6edfbbad 5474 void *vpp;
87d72d41 5475 Elf_Internal_Sym *isym;
6edfbbad 5476
87d72d41
AM
5477 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5478 abfd, r_symndx);
5479 if (isym == NULL)
b34976b6 5480 return FALSE;
ec338859 5481
87d72d41
AM
5482 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5483 if (s == NULL)
5484 s = sec;
5485
6edfbbad 5486 vpp = &elf_section_data (s)->local_dynrel;
6061a67d 5487 head = (struct elf_dyn_relocs **) vpp;
65f38f15 5488 }
ec338859
AM
5489
5490 p = *head;
5491 if (p == NULL || p->sec != sec)
5492 {
4ce794b7 5493 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 5494 if (p == NULL)
b34976b6 5495 return FALSE;
ec338859
AM
5496 p->next = *head;
5497 *head = p;
5498 p->sec = sec;
5499 p->count = 0;
5500 p->pc_count = 0;
5501 }
5502
5503 p->count += 1;
1d483afe 5504 if (!must_be_dyn_reloc (info, r_type))
ec338859 5505 p->pc_count += 1;
65f38f15 5506 }
5bd4f169 5507 break;
65f38f15
AM
5508
5509 default:
96e0dda4 5510 break;
5bd4f169
AM
5511 }
5512 }
5513
b34976b6 5514 return TRUE;
5bd4f169
AM
5515}
5516
8387904d
AM
5517/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5518 of the code entry point, and its section. */
5519
5520static bfd_vma
5521opd_entry_value (asection *opd_sec,
5522 bfd_vma offset,
5523 asection **code_sec,
5524 bfd_vma *code_off)
5525{
5526 bfd *opd_bfd = opd_sec->owner;
8860955f 5527 Elf_Internal_Rela *relocs;
8387904d 5528 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5529 bfd_vma val;
8387904d 5530
4b85d634
AM
5531 /* No relocs implies we are linking a --just-symbols object. */
5532 if (opd_sec->reloc_count == 0)
5533 {
aa374f67
AM
5534 char buf[8];
5535
5536 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
4b85d634 5537 return (bfd_vma) -1;
3b36f7e6 5538
aa374f67 5539 val = bfd_get_64 (opd_bfd, buf);
4b85d634
AM
5540 if (code_sec != NULL)
5541 {
5542 asection *sec, *likely = NULL;
5543 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5544 if (sec->vma <= val
5545 && (sec->flags & SEC_LOAD) != 0
5546 && (sec->flags & SEC_ALLOC) != 0)
5547 likely = sec;
5548 if (likely != NULL)
5549 {
5550 *code_sec = likely;
5551 if (code_off != NULL)
5552 *code_off = val - likely->vma;
5553 }
5554 }
5555 return val;
5556 }
5557
0c8d6e5c 5558 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5559
8860955f
AM
5560 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5561 if (relocs == NULL)
5562 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5563
8387904d 5564 /* Go find the opd reloc at the sym address. */
8860955f 5565 lo = relocs;
8387904d
AM
5566 BFD_ASSERT (lo != NULL);
5567 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5568 val = (bfd_vma) -1;
8387904d
AM
5569 while (lo < hi)
5570 {
5571 look = lo + (hi - lo) / 2;
5572 if (look->r_offset < offset)
5573 lo = look + 1;
5574 else if (look->r_offset > offset)
5575 hi = look;
5576 else
5577 {
0ffa91dd
NC
5578 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5579
8387904d
AM
5580 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5581 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5582 {
5583 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5584 asection *sec;
5585
5586 if (symndx < symtab_hdr->sh_info)
5587 {
5588 Elf_Internal_Sym *sym;
5589
5590 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5591 if (sym == NULL)
5592 {
5593 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5594 symtab_hdr->sh_info,
5595 0, NULL, NULL, NULL);
5596 if (sym == NULL)
645ea6a9 5597 break;
8387904d
AM
5598 symtab_hdr->contents = (bfd_byte *) sym;
5599 }
5600
5601 sym += symndx;
5602 val = sym->st_value;
cb33740c 5603 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5604 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5605 }
5606 else
5607 {
5608 struct elf_link_hash_entry **sym_hashes;
5609 struct elf_link_hash_entry *rh;
5610
5611 sym_hashes = elf_sym_hashes (opd_bfd);
5612 rh = sym_hashes[symndx - symtab_hdr->sh_info];
b31867b6 5613 rh = elf_follow_link (rh);
8387904d
AM
5614 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5615 || rh->root.type == bfd_link_hash_defweak);
5616 val = rh->root.u.def.value;
5617 sec = rh->root.u.def.section;
5618 }
5619 val += look->r_addend;
5620 if (code_off != NULL)
5621 *code_off = val;
5622 if (code_sec != NULL)
5623 *code_sec = sec;
5624 if (sec != NULL && sec->output_section != NULL)
5625 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5626 }
5627 break;
5628 }
5629 }
645ea6a9 5630
645ea6a9 5631 return val;
8387904d
AM
5632}
5633
854b41e7
AM
5634/* Return true if symbol is defined in a regular object file. */
5635
5636static bfd_boolean
5637is_static_defined (struct elf_link_hash_entry *h)
5638{
5639 return ((h->root.type == bfd_link_hash_defined
5640 || h->root.type == bfd_link_hash_defweak)
5641 && h->root.u.def.section != NULL
5642 && h->root.u.def.section->output_section != NULL);
5643}
5644
b31867b6
AM
5645/* If FDH is a function descriptor symbol, return the associated code
5646 entry symbol if it is defined. Return NULL otherwise. */
5647
5648static struct ppc_link_hash_entry *
5649defined_code_entry (struct ppc_link_hash_entry *fdh)
5650{
5651 if (fdh->is_func_descriptor)
5652 {
5653 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5654 if (fh->elf.root.type == bfd_link_hash_defined
5655 || fh->elf.root.type == bfd_link_hash_defweak)
5656 return fh;
5657 }
5658 return NULL;
5659}
5660
5661/* If FH is a function code entry symbol, return the associated
5662 function descriptor symbol if it is defined. Return NULL otherwise. */
5663
5664static struct ppc_link_hash_entry *
5665defined_func_desc (struct ppc_link_hash_entry *fh)
5666{
5667 if (fh->oh != NULL
5668 && fh->oh->is_func_descriptor)
5669 {
5670 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5671 if (fdh->elf.root.type == bfd_link_hash_defined
5672 || fdh->elf.root.type == bfd_link_hash_defweak)
5673 return fdh;
5674 }
5675 return NULL;
5676}
5677
74f0fb50
AM
5678/* Mark all our entry sym sections, both opd and code section. */
5679
5680static void
5681ppc64_elf_gc_keep (struct bfd_link_info *info)
5682{
5683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5684 struct bfd_sym_chain *sym;
5685
4dfe6ac6
NC
5686 if (htab == NULL)
5687 return;
5688
74f0fb50
AM
5689 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5690 {
b31867b6 5691 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5692 asection *sec;
5693
5694 eh = (struct ppc_link_hash_entry *)
b31867b6 5695 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5696 if (eh == NULL)
5697 continue;
5698 if (eh->elf.root.type != bfd_link_hash_defined
5699 && eh->elf.root.type != bfd_link_hash_defweak)
5700 continue;
5701
b31867b6
AM
5702 fh = defined_code_entry (eh);
5703 if (fh != NULL)
74f0fb50 5704 {
b31867b6 5705 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5706 sec->flags |= SEC_KEEP;
5707 }
5708 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5709 && opd_entry_value (eh->elf.root.u.def.section,
5710 eh->elf.root.u.def.value,
5711 &sec, NULL) != (bfd_vma) -1)
5712 sec->flags |= SEC_KEEP;
5713
5714 sec = eh->elf.root.u.def.section;
5715 sec->flags |= SEC_KEEP;
5716 }
5717}
5718
64d03ab5
AM
5719/* Mark sections containing dynamically referenced symbols. When
5720 building shared libraries, we must assume that any visible symbol is
5721 referenced. */
5722
5723static bfd_boolean
5724ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5725{
5726 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5727 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5728 struct ppc_link_hash_entry *fdh;
64d03ab5 5729
64d03ab5 5730 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5731 fdh = defined_func_desc (eh);
5732 if (fdh != NULL)
5733 eh = fdh;
64d03ab5
AM
5734
5735 if ((eh->elf.root.type == bfd_link_hash_defined
5736 || eh->elf.root.type == bfd_link_hash_defweak)
5737 && (eh->elf.ref_dynamic
5738 || (!info->executable
5739 && eh->elf.def_regular
5740 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8
AM
5741 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5742 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
5743 || !bfd_hide_sym_by_version (info->version_info,
5744 eh->elf.root.root.string)))))
64d03ab5
AM
5745 {
5746 asection *code_sec;
b31867b6 5747 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5748
5749 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5750
5751 /* Function descriptor syms cause the associated
5752 function code sym section to be marked. */
b31867b6
AM
5753 fh = defined_code_entry (eh);
5754 if (fh != NULL)
5755 {
5756 code_sec = fh->elf.root.u.def.section;
5757 code_sec->flags |= SEC_KEEP;
5758 }
64d03ab5
AM
5759 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5760 && opd_entry_value (eh->elf.root.u.def.section,
5761 eh->elf.root.u.def.value,
5762 &code_sec, NULL) != (bfd_vma) -1)
5763 code_sec->flags |= SEC_KEEP;
5764 }
5765
5766 return TRUE;
5767}
5768
5bd4f169
AM
5769/* Return the section that should be marked against GC for a given
5770 relocation. */
5771
5772static asection *
4ce794b7 5773ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5774 struct bfd_link_info *info,
4ce794b7
AM
5775 Elf_Internal_Rela *rel,
5776 struct elf_link_hash_entry *h,
5777 Elf_Internal_Sym *sym)
5bd4f169 5778{
ccfa59ea
AM
5779 asection *rsec;
5780
ccfa59ea
AM
5781 /* Syms return NULL if we're marking .opd, so we avoid marking all
5782 function sections, as all functions are referenced in .opd. */
5783 rsec = NULL;
5784 if (get_opd_info (sec) != NULL)
5785 return rsec;
1e2f5b6e 5786
5bd4f169
AM
5787 if (h != NULL)
5788 {
04c9666a 5789 enum elf_ppc64_reloc_type r_type;
b31867b6 5790 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5791
4ce794b7 5792 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5793 switch (r_type)
5bd4f169
AM
5794 {
5795 case R_PPC64_GNU_VTINHERIT:
5796 case R_PPC64_GNU_VTENTRY:
5797 break;
5798
5799 default:
5800 switch (h->root.type)
5801 {
5802 case bfd_link_hash_defined:
5803 case bfd_link_hash_defweak:
ccfa59ea 5804 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5805 fdh = defined_func_desc (eh);
5806 if (fdh != NULL)
5807 eh = fdh;
1e2f5b6e
AM
5808
5809 /* Function descriptor syms cause the associated
5810 function code sym section to be marked. */
b31867b6
AM
5811 fh = defined_code_entry (eh);
5812 if (fh != NULL)
ccfa59ea
AM
5813 {
5814 /* They also mark their opd section. */
74f0fb50 5815 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5816
b31867b6 5817 rsec = fh->elf.root.u.def.section;
ccfa59ea 5818 }
8387904d
AM
5819 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5820 && opd_entry_value (eh->elf.root.u.def.section,
5821 eh->elf.root.u.def.value,
5822 &rsec, NULL) != (bfd_vma) -1)
74f0fb50 5823 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5824 else
1e2f5b6e
AM
5825 rsec = h->root.u.def.section;
5826 break;
5bd4f169
AM
5827
5828 case bfd_link_hash_common:
1e2f5b6e
AM
5829 rsec = h->root.u.c.p->section;
5830 break;
5bd4f169
AM
5831
5832 default:
fb34365b 5833 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5834 }
5835 }
5836 }
5837 else
5838 {
74f0fb50 5839 struct _opd_sec_data *opd;
1e2f5b6e
AM
5840
5841 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5842 opd = get_opd_info (rsec);
5843 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5844 {
74f0fb50 5845 rsec->gc_mark = 1;
ccfa59ea 5846
74f0fb50 5847 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5848 }
5bd4f169
AM
5849 }
5850
1e2f5b6e 5851 return rsec;
5bd4f169
AM
5852}
5853
65f38f15
AM
5854/* Update the .got, .plt. and dynamic reloc reference counts for the
5855 section being removed. */
5bd4f169 5856
b34976b6 5857static bfd_boolean
4ce794b7
AM
5858ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5859 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5860{
411e1bfb 5861 struct ppc_link_hash_table *htab;
5bd4f169
AM
5862 Elf_Internal_Shdr *symtab_hdr;
5863 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5864 struct got_entry **local_got_ents;
5bd4f169 5865 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5866
7dda2462
TG
5867 if (info->relocatable)
5868 return TRUE;
5869
680a3378
AM
5870 if ((sec->flags & SEC_ALLOC) == 0)
5871 return TRUE;
5872
ec338859
AM
5873 elf_section_data (sec)->local_dynrel = NULL;
5874
411e1bfb 5875 htab = ppc_hash_table (info);
4dfe6ac6
NC
5876 if (htab == NULL)
5877 return FALSE;
5878
0ffa91dd 5879 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5880 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5881 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5882
5883 relend = relocs + sec->reloc_count;
5884 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5885 {
5886 unsigned long r_symndx;
04c9666a 5887 enum elf_ppc64_reloc_type r_type;
58ac9f71 5888 struct elf_link_hash_entry *h = NULL;
f961d9dd 5889 unsigned char tls_type = 0;
5bd4f169 5890
a33d1f77 5891 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5892 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5893 if (r_symndx >= symtab_hdr->sh_info)
5894 {
5895 struct ppc_link_hash_entry *eh;
6061a67d
AM
5896 struct elf_dyn_relocs **pp;
5897 struct elf_dyn_relocs *p;
58ac9f71
AM
5898
5899 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5900 h = elf_follow_link (h);
58ac9f71
AM
5901 eh = (struct ppc_link_hash_entry *) h;
5902
5903 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5904 if (p->sec == sec)
5905 {
5906 /* Everything must go for SEC. */
5907 *pp = p->next;
5908 break;
5909 }
5910 }
5911
e054468f
AM
5912 if (is_branch_reloc (r_type))
5913 {
5914 struct plt_entry **ifunc = NULL;
5915 if (h != NULL)
5916 {
5917 if (h->type == STT_GNU_IFUNC)
5918 ifunc = &h->plt.plist;
5919 }
5920 else if (local_got_ents != NULL)
5921 {
5922 struct plt_entry **local_plt = (struct plt_entry **)
5923 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5924 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
5925 (local_plt + symtab_hdr->sh_info);
5926 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5927 ifunc = local_plt + r_symndx;
5928 }
5929 if (ifunc != NULL)
5930 {
5931 struct plt_entry *ent;
5932
5933 for (ent = *ifunc; ent != NULL; ent = ent->next)
5934 if (ent->addend == rel->r_addend)
5935 break;
5936 if (ent == NULL)
5937 abort ();
5938 if (ent->plt.refcount > 0)
5939 ent->plt.refcount -= 1;
5940 continue;
5941 }
5942 }
5943
a33d1f77
AM
5944 switch (r_type)
5945 {
411e1bfb
AM
5946 case R_PPC64_GOT_TLSLD16:
5947 case R_PPC64_GOT_TLSLD16_LO:
5948 case R_PPC64_GOT_TLSLD16_HI:
5949 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5950 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5951 goto dogot;
5952
5953 case R_PPC64_GOT_TLSGD16:
5954 case R_PPC64_GOT_TLSGD16_LO:
5955 case R_PPC64_GOT_TLSGD16_HI:
5956 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5957 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5958 goto dogot;
5959
5960 case R_PPC64_GOT_TPREL16_DS:
5961 case R_PPC64_GOT_TPREL16_LO_DS:
5962 case R_PPC64_GOT_TPREL16_HI:
5963 case R_PPC64_GOT_TPREL16_HA:
5964 tls_type = TLS_TLS | TLS_TPREL;
5965 goto dogot;
5966
5967 case R_PPC64_GOT_DTPREL16_DS:
5968 case R_PPC64_GOT_DTPREL16_LO_DS:
5969 case R_PPC64_GOT_DTPREL16_HI:
5970 case R_PPC64_GOT_DTPREL16_HA:
5971 tls_type = TLS_TLS | TLS_DTPREL;
5972 goto dogot;
5973
a33d1f77
AM
5974 case R_PPC64_GOT16:
5975 case R_PPC64_GOT16_DS:
5976 case R_PPC64_GOT16_HA:
5977 case R_PPC64_GOT16_HI:
5978 case R_PPC64_GOT16_LO:
5979 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5980 dogot:
5981 {
5982 struct got_entry *ent;
5983
58ac9f71
AM
5984 if (h != NULL)
5985 ent = h->got.glist;
411e1bfb
AM
5986 else
5987 ent = local_got_ents[r_symndx];
5988
5989 for (; ent != NULL; ent = ent->next)
5990 if (ent->addend == rel->r_addend
e717da7e 5991 && ent->owner == abfd
411e1bfb
AM
5992 && ent->tls_type == tls_type)
5993 break;
5994 if (ent == NULL)
5995 abort ();
5996 if (ent->got.refcount > 0)
5997 ent->got.refcount -= 1;
5998 }
a33d1f77 5999 break;
65f38f15 6000
a33d1f77
AM
6001 case R_PPC64_PLT16_HA:
6002 case R_PPC64_PLT16_HI:
6003 case R_PPC64_PLT16_LO:
6004 case R_PPC64_PLT32:
6005 case R_PPC64_PLT64:
721956f4
AM
6006 case R_PPC64_REL14:
6007 case R_PPC64_REL14_BRNTAKEN:
6008 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6009 case R_PPC64_REL24:
58ac9f71 6010 if (h != NULL)
5d1634d7 6011 {
411e1bfb
AM
6012 struct plt_entry *ent;
6013
411e1bfb
AM
6014 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6015 if (ent->addend == rel->r_addend)
6016 break;
7c6c1722 6017 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6018 ent->plt.refcount -= 1;
5d1634d7 6019 }
e86ce104 6020 break;
5d1634d7 6021
a33d1f77
AM
6022 default:
6023 break;
6024 }
6025 }
b34976b6 6026 return TRUE;
5bd4f169
AM
6027}
6028
deb0e272
AM
6029/* The maximum size of .sfpr. */
6030#define SFPR_MAX (218*4)
6031
6032struct sfpr_def_parms
6033{
699733f6
AM
6034 const char name[12];
6035 unsigned char lo, hi;
deb0e272
AM
6036 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6037 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6038};
6039
6040/* Auto-generate _save*, _rest* functions in .sfpr. */
6041
4dfe6ac6 6042static bfd_boolean
deb0e272
AM
6043sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6044{
6045 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6046 unsigned int i;
6047 size_t len = strlen (parm->name);
6048 bfd_boolean writing = FALSE;
699733f6 6049 char sym[16];
deb0e272 6050
4dfe6ac6
NC
6051 if (htab == NULL)
6052 return FALSE;
6053
deb0e272
AM
6054 memcpy (sym, parm->name, len);
6055 sym[len + 2] = 0;
6056
6057 for (i = parm->lo; i <= parm->hi; i++)
6058 {
6059 struct elf_link_hash_entry *h;
6060
6061 sym[len + 0] = i / 10 + '0';
6062 sym[len + 1] = i % 10 + '0';
6063 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6064 if (h != NULL
f5385ebf 6065 && !h->def_regular)
deb0e272
AM
6066 {
6067 h->root.type = bfd_link_hash_defined;
6068 h->root.u.def.section = htab->sfpr;
6069 h->root.u.def.value = htab->sfpr->size;
6070 h->type = STT_FUNC;
f5385ebf 6071 h->def_regular = 1;
deb0e272
AM
6072 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6073 writing = TRUE;
6074 if (htab->sfpr->contents == NULL)
6075 {
6076 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6077 if (htab->sfpr->contents == NULL)
6078 return FALSE;
6079 }
6080 }
6081 if (writing)
6082 {
6083 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6084 if (i != parm->hi)
6085 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6086 else
6087 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6088 htab->sfpr->size = p - htab->sfpr->contents;
6089 }
6090 }
6091
6092 return TRUE;
6093}
6094
6095static bfd_byte *
6096savegpr0 (bfd *abfd, bfd_byte *p, int r)
6097{
6098 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6099 return p + 4;
6100}
6101
6102static bfd_byte *
6103savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6104{
6105 p = savegpr0 (abfd, p, r);
6106 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6107 p = p + 4;
6108 bfd_put_32 (abfd, BLR, p);
6109 return p + 4;
6110}
6111
6112static bfd_byte *
6113restgpr0 (bfd *abfd, bfd_byte *p, int r)
6114{
6115 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6116 return p + 4;
6117}
6118
6119static bfd_byte *
6120restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6121{
6122 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6123 p = p + 4;
6124 p = restgpr0 (abfd, p, r);
6125 bfd_put_32 (abfd, MTLR_R0, p);
6126 p = p + 4;
6127 if (r == 29)
6128 {
6129 p = restgpr0 (abfd, p, 30);
6130 p = restgpr0 (abfd, p, 31);
6131 }
6132 bfd_put_32 (abfd, BLR, p);
6133 return p + 4;
6134}
6135
6136static bfd_byte *
6137savegpr1 (bfd *abfd, bfd_byte *p, int r)
6138{
6139 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6140 return p + 4;
6141}
6142
6143static bfd_byte *
6144savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6145{
6146 p = savegpr1 (abfd, p, r);
6147 bfd_put_32 (abfd, BLR, p);
6148 return p + 4;
6149}
6150
6151static bfd_byte *
6152restgpr1 (bfd *abfd, bfd_byte *p, int r)
6153{
6154 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6155 return p + 4;
6156}
6157
6158static bfd_byte *
6159restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6160{
6161 p = restgpr1 (abfd, p, r);
6162 bfd_put_32 (abfd, BLR, p);
6163 return p + 4;
6164}
6165
6166static bfd_byte *
6167savefpr (bfd *abfd, bfd_byte *p, int r)
6168{
6169 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6170 return p + 4;
6171}
6172
6173static bfd_byte *
6174savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6175{
6176 p = savefpr (abfd, p, r);
6177 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6178 p = p + 4;
6179 bfd_put_32 (abfd, BLR, p);
6180 return p + 4;
6181}
6182
6183static bfd_byte *
6184restfpr (bfd *abfd, bfd_byte *p, int r)
6185{
6186 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6187 return p + 4;
6188}
6189
6190static bfd_byte *
6191restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6192{
6193 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6194 p = p + 4;
6195 p = restfpr (abfd, p, r);
6196 bfd_put_32 (abfd, MTLR_R0, p);
6197 p = p + 4;
6198 if (r == 29)
6199 {
6200 p = restfpr (abfd, p, 30);
6201 p = restfpr (abfd, p, 31);
6202 }
6203 bfd_put_32 (abfd, BLR, p);
6204 return p + 4;
6205}
6206
6207static bfd_byte *
6208savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6209{
6210 p = savefpr (abfd, p, r);
6211 bfd_put_32 (abfd, BLR, p);
6212 return p + 4;
6213}
6214
6215static bfd_byte *
6216restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6217{
6218 p = restfpr (abfd, p, r);
6219 bfd_put_32 (abfd, BLR, p);
6220 return p + 4;
6221}
6222
6223static bfd_byte *
6224savevr (bfd *abfd, bfd_byte *p, int r)
6225{
6226 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6227 p = p + 4;
6228 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6229 return p + 4;
6230}
6231
6232static bfd_byte *
6233savevr_tail (bfd *abfd, bfd_byte *p, int r)
6234{
6235 p = savevr (abfd, p, r);
6236 bfd_put_32 (abfd, BLR, p);
6237 return p + 4;
6238}
6239
6240static bfd_byte *
6241restvr (bfd *abfd, bfd_byte *p, int r)
6242{
6243 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6244 p = p + 4;
6245 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6246 return p + 4;
6247}
6248
6249static bfd_byte *
6250restvr_tail (bfd *abfd, bfd_byte *p, int r)
6251{
6252 p = restvr (abfd, p, r);
6253 bfd_put_32 (abfd, BLR, p);
6254 return p + 4;
6255}
6256
e86ce104
AM
6257/* Called via elf_link_hash_traverse to transfer dynamic linking
6258 information on function code symbol entries to their corresponding
6259 function descriptor symbol entries. */
deb0e272 6260
b34976b6 6261static bfd_boolean
4ce794b7 6262func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6263{
e86ce104 6264 struct bfd_link_info *info;
65f38f15 6265 struct ppc_link_hash_table *htab;
411e1bfb 6266 struct plt_entry *ent;
50bc7936
AM
6267 struct ppc_link_hash_entry *fh;
6268 struct ppc_link_hash_entry *fdh;
6269 bfd_boolean force_local;
5bd4f169 6270
50bc7936
AM
6271 fh = (struct ppc_link_hash_entry *) h;
6272 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6273 return TRUE;
e86ce104 6274
4ce794b7 6275 info = inf;
65f38f15 6276 htab = ppc_hash_table (info);
4dfe6ac6
NC
6277 if (htab == NULL)
6278 return FALSE;
5bd4f169 6279
c09bdfe5
AM
6280 /* Resolve undefined references to dot-symbols as the value
6281 in the function descriptor, if we have one in a regular object.
6282 This is to satisfy cases like ".quad .foo". Calls to functions
6283 in dynamic objects are handled elsewhere. */
6284 if (fh->elf.root.type == bfd_link_hash_undefweak
6285 && fh->was_undefined
b31867b6
AM
6286 && (fdh = defined_func_desc (fh)) != NULL
6287 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6288 && opd_entry_value (fdh->elf.root.u.def.section,
6289 fdh->elf.root.u.def.value,
c09bdfe5
AM
6290 &fh->elf.root.u.def.section,
6291 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6292 {
b31867b6 6293 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6294 fh->elf.forced_local = 1;
b31867b6
AM
6295 fh->elf.def_regular = fdh->elf.def_regular;
6296 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6297 }
6298
e86ce104
AM
6299 /* If this is a function code symbol, transfer dynamic linking
6300 information to the function descriptor symbol. */
50bc7936 6301 if (!fh->is_func)
b34976b6 6302 return TRUE;
e86ce104 6303
50bc7936 6304 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6305 if (ent->plt.refcount > 0)
6306 break;
50bc7936
AM
6307 if (ent == NULL
6308 || fh->elf.root.root.string[0] != '.'
6309 || fh->elf.root.root.string[1] == '\0')
6310 return TRUE;
5bd4f169 6311
50bc7936
AM
6312 /* Find the corresponding function descriptor symbol. Create it
6313 as undefined if necessary. */
5bd4f169 6314
b31867b6 6315 fdh = lookup_fdh (fh, htab);
50bc7936 6316 if (fdh == NULL
df131623 6317 && !info->executable
50bc7936
AM
6318 && (fh->elf.root.type == bfd_link_hash_undefined
6319 || fh->elf.root.type == bfd_link_hash_undefweak))
6320 {
908b32fc 6321 fdh = make_fdh (info, fh);
bb700d78
AM
6322 if (fdh == NULL)
6323 return FALSE;
50bc7936 6324 }
648cca2c 6325
908b32fc 6326 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6327 code symbol is strong undefined, make the fake sym the same.
6328 If the function code symbol is defined, then force the fake
6329 descriptor local; We can't support overriding of symbols in a
6330 shared library on a fake descriptor. */
908b32fc
AM
6331
6332 if (fdh != NULL
6333 && fdh->fake
433817dd 6334 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6335 {
433817dd
AM
6336 if (fh->elf.root.type == bfd_link_hash_undefined)
6337 {
6338 fdh->elf.root.type = bfd_link_hash_undefined;
6339 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6340 }
6341 else if (fh->elf.root.type == bfd_link_hash_defined
6342 || fh->elf.root.type == bfd_link_hash_defweak)
6343 {
6344 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6345 }
908b32fc
AM
6346 }
6347
50bc7936 6348 if (fdh != NULL
f5385ebf 6349 && !fdh->elf.forced_local
df131623 6350 && (!info->executable
f5385ebf
AM
6351 || fdh->elf.def_dynamic
6352 || fdh->elf.ref_dynamic
50bc7936
AM
6353 || (fdh->elf.root.type == bfd_link_hash_undefweak
6354 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6355 {
6356 if (fdh->elf.dynindx == -1)
c152c796 6357 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6358 return FALSE;
f5385ebf
AM
6359 fdh->elf.ref_regular |= fh->elf.ref_regular;
6360 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6361 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6362 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6363 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6364 {
40d16e0b 6365 move_plt_plist (fh, fdh);
f5385ebf 6366 fdh->elf.needs_plt = 1;
e86ce104 6367 }
50bc7936 6368 fdh->is_func_descriptor = 1;
34814b9f
AM
6369 fdh->oh = fh;
6370 fh->oh = fdh;
e86ce104
AM
6371 }
6372
50bc7936
AM
6373 /* Now that the info is on the function descriptor, clear the
6374 function code sym info. Any function code syms for which we
6375 don't have a definition in a regular file, we force local.
6376 This prevents a shared library from exporting syms that have
6377 been imported from another library. Function code syms that
6378 are really in the library we must leave global to prevent the
6379 linker dragging in a definition from a static library. */
93f3fa99
AM
6380 force_local = (!fh->elf.def_regular
6381 || fdh == NULL
6382 || !fdh->elf.def_regular
6383 || fdh->elf.forced_local);
50bc7936
AM
6384 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6385
b34976b6 6386 return TRUE;
e86ce104 6387}
40b8271b 6388
e86ce104 6389/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6390 this hook to a) provide some gcc support functions, and b) transfer
6391 dynamic linking information gathered so far on function code symbol
6392 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6393
b34976b6 6394static bfd_boolean
4ce794b7
AM
6395ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6396 struct bfd_link_info *info)
e86ce104
AM
6397{
6398 struct ppc_link_hash_table *htab;
82bd7b59 6399 unsigned int i;
deb0e272
AM
6400 const struct sfpr_def_parms funcs[] =
6401 {
6402 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6403 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6404 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6405 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6406 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6407 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6408 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6409 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6410 { "._savef", 14, 31, savefpr, savefpr1_tail },
6411 { "._restf", 14, 31, restfpr, restfpr1_tail },
6412 { "_savevr_", 20, 31, savevr, savevr_tail },
6413 { "_restvr_", 20, 31, restvr, restvr_tail }
6414 };
e86ce104
AM
6415
6416 htab = ppc_hash_table (info);
4dfe6ac6
NC
6417 if (htab == NULL)
6418 return FALSE;
6419
82bd7b59
AM
6420 if (htab->sfpr == NULL)
6421 /* We don't have any relocs. */
b34976b6 6422 return TRUE;
82bd7b59 6423
deb0e272
AM
6424 /* Provide any missing _save* and _rest* functions. */
6425 htab->sfpr->size = 0;
6426 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6427 if (!sfpr_define (info, &funcs[i]))
6428 return FALSE;
82bd7b59 6429
4ce794b7 6430 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6431
eea6121a 6432 if (htab->sfpr->size == 0)
8423293d 6433 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6434
b34976b6 6435 return TRUE;
e86ce104
AM
6436}
6437
6438/* Adjust a symbol defined by a dynamic object and referenced by a
6439 regular object. The current definition is in some section of the
6440 dynamic object, but we're not including those sections. We have to
6441 change the definition to something the rest of the link can
6442 understand. */
6443
b34976b6 6444static bfd_boolean
4ce794b7
AM
6445ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6446 struct elf_link_hash_entry *h)
e86ce104
AM
6447{
6448 struct ppc_link_hash_table *htab;
e86ce104 6449 asection *s;
e86ce104
AM
6450
6451 htab = ppc_hash_table (info);
4dfe6ac6
NC
6452 if (htab == NULL)
6453 return FALSE;
e86ce104
AM
6454
6455 /* Deal with function syms. */
6456 if (h->type == STT_FUNC
e054468f 6457 || h->type == STT_GNU_IFUNC
f5385ebf 6458 || h->needs_plt)
e86ce104
AM
6459 {
6460 /* Clear procedure linkage table information for any symbol that
6461 won't need a .plt entry. */
411e1bfb
AM
6462 struct plt_entry *ent;
6463 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6464 if (ent->plt.refcount > 0)
6465 break;
8387904d 6466 if (ent == NULL
e054468f
AM
6467 || (h->type != STT_GNU_IFUNC
6468 && (SYMBOL_CALLS_LOCAL (info, h)
6469 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6470 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6471 {
411e1bfb 6472 h->plt.plist = NULL;
f5385ebf 6473 h->needs_plt = 0;
40b8271b 6474 }
5bd4f169 6475 }
bbd7ec4a 6476 else
411e1bfb 6477 h->plt.plist = NULL;
5bd4f169
AM
6478
6479 /* If this is a weak symbol, and there is a real definition, the
6480 processor independent code will have arranged for us to see the
6481 real definition first, and we can just use the same value. */
f6e332e6 6482 if (h->u.weakdef != NULL)
5bd4f169 6483 {
f6e332e6
AM
6484 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6485 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6486 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6487 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6488 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6489 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6490 return TRUE;
5bd4f169
AM
6491 }
6492
5bd4f169
AM
6493 /* If we are creating a shared library, we must presume that the
6494 only references to the symbol are via the global offset table.
6495 For such cases we need not do anything here; the relocations will
6496 be handled correctly by relocate_section. */
6497 if (info->shared)
b34976b6 6498 return TRUE;
5bd4f169 6499
65f38f15
AM
6500 /* If there are no references to this symbol that do not use the
6501 GOT, we don't need to generate a copy reloc. */
f5385ebf 6502 if (!h->non_got_ref)
b34976b6 6503 return TRUE;
65f38f15 6504
b186458a
JJ
6505 /* Don't generate a copy reloc for symbols defined in the executable. */
6506 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6507 return TRUE;
6508
f4656909 6509 if (ELIMINATE_COPY_RELOCS)
65f38f15 6510 {
f4656909 6511 struct ppc_link_hash_entry * eh;
6061a67d 6512 struct elf_dyn_relocs *p;
65f38f15 6513
f4656909
AM
6514 eh = (struct ppc_link_hash_entry *) h;
6515 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6516 {
6517 s = p->sec->output_section;
6518 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6519 break;
6520 }
6521
6522 /* If we didn't find any dynamic relocs in read-only sections, then
6523 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6524 if (p == NULL)
6525 {
f5385ebf 6526 h->non_got_ref = 0;
f4656909
AM
6527 return TRUE;
6528 }
65f38f15
AM
6529 }
6530
5d35169e 6531 if (h->plt.plist != NULL)
97b639ba
AM
6532 {
6533 /* We should never get here, but unfortunately there are versions
6534 of gcc out there that improperly (for this ABI) put initialized
6535 function pointers, vtable refs and suchlike in read-only
6536 sections. Allow them to proceed, but warn that this might
6537 break at runtime. */
25f53a85 6538 info->callbacks->einfo
8de848d8 6539 (_("%P: copy reloc against `%s' requires lazy plt linking; "
25f53a85 6540 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6541 h->root.root.string);
6542 }
5d35169e
AM
6543
6544 /* This is a reference to a symbol defined by a dynamic object which
6545 is not a function. */
6546
909272ee
AM
6547 if (h->size == 0)
6548 {
8de848d8 6549 info->callbacks->einfo (_("%P: dynamic variable `%s' is zero size\n"),
25f53a85 6550 h->root.root.string);
909272ee
AM
6551 return TRUE;
6552 }
6553
5bd4f169
AM
6554 /* We must allocate the symbol in our .dynbss section, which will
6555 become part of the .bss section of the executable. There will be
6556 an entry for this symbol in the .dynsym section. The dynamic
6557 object will contain position independent code, so all references
6558 from the dynamic object to this symbol will go through the global
6559 offset table. The dynamic linker will use the .dynsym entry to
6560 determine the address it must put in the global offset table, so
6561 both the dynamic object and the regular object will refer to the
6562 same memory location for the variable. */
5bd4f169 6563
04c9666a
AM
6564 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6565 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6566 runtime process image. We need to remember the offset into the
6567 .rela.bss section we are going to use. */
6568 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6569 {
eea6121a 6570 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6571 h->needs_copy = 1;
5bd4f169
AM
6572 }
6573
4ce794b7 6574 s = htab->dynbss;
5bd4f169 6575
027297b7 6576 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6577}
6578
e86ce104
AM
6579/* If given a function descriptor symbol, hide both the function code
6580 sym and the descriptor. */
6581static void
4ce794b7
AM
6582ppc64_elf_hide_symbol (struct bfd_link_info *info,
6583 struct elf_link_hash_entry *h,
6584 bfd_boolean force_local)
e86ce104 6585{
34814b9f 6586 struct ppc_link_hash_entry *eh;
e86ce104
AM
6587 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6588
34814b9f
AM
6589 eh = (struct ppc_link_hash_entry *) h;
6590 if (eh->is_func_descriptor)
e86ce104 6591 {
34814b9f 6592 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6593
721956f4 6594 if (fh == NULL)
d1329ca3
AM
6595 {
6596 const char *p, *q;
6597 struct ppc_link_hash_table *htab;
6598 char save;
6599
6600 /* We aren't supposed to use alloca in BFD because on
6601 systems which do not have alloca the version in libiberty
6602 calls xmalloc, which might cause the program to crash
6603 when it runs out of memory. This function doesn't have a
6604 return status, so there's no way to gracefully return an
6605 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6606 accessed; It's either a string in an ELF string table,
6607 or allocated in an objalloc structure. */
d1329ca3 6608
34814b9f 6609 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6610 save = *p;
6611 *(char *) p = '.';
6612 htab = ppc_hash_table (info);
4dfe6ac6
NC
6613 if (htab == NULL)
6614 return;
6615
34814b9f
AM
6616 fh = (struct ppc_link_hash_entry *)
6617 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6618 *(char *) p = save;
6619
6620 /* Unfortunately, if it so happens that the string we were
6621 looking for was allocated immediately before this string,
6622 then we overwrote the string terminator. That's the only
6623 reason the lookup should fail. */
6624 if (fh == NULL)
6625 {
34814b9f
AM
6626 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6627 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6628 --q, --p;
34814b9f
AM
6629 if (q < eh->elf.root.root.string && *p == '.')
6630 fh = (struct ppc_link_hash_entry *)
6631 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6632 }
6633 if (fh != NULL)
6634 {
34814b9f
AM
6635 eh->oh = fh;
6636 fh->oh = eh;
d1329ca3
AM
6637 }
6638 }
e86ce104 6639 if (fh != NULL)
34814b9f 6640 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6641 }
6642}
6643
411e1bfb 6644static bfd_boolean
8843416a
AM
6645get_sym_h (struct elf_link_hash_entry **hp,
6646 Elf_Internal_Sym **symp,
6647 asection **symsecp,
f961d9dd 6648 unsigned char **tls_maskp,
8843416a
AM
6649 Elf_Internal_Sym **locsymsp,
6650 unsigned long r_symndx,
6651 bfd *ibfd)
411e1bfb 6652{
0ffa91dd 6653 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6654
6655 if (r_symndx >= symtab_hdr->sh_info)
6656 {
6657 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6658 struct elf_link_hash_entry *h;
6659
6660 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6661 h = elf_follow_link (h);
411e1bfb
AM
6662
6663 if (hp != NULL)
6664 *hp = h;
6665
6666 if (symp != NULL)
6667 *symp = NULL;
6668
6669 if (symsecp != NULL)
6670 {
6671 asection *symsec = NULL;
6672 if (h->root.type == bfd_link_hash_defined
6673 || h->root.type == bfd_link_hash_defweak)
6674 symsec = h->root.u.def.section;
6675 *symsecp = symsec;
6676 }
6677
e7b938ca 6678 if (tls_maskp != NULL)
411e1bfb
AM
6679 {
6680 struct ppc_link_hash_entry *eh;
6681
6682 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6683 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6684 }
6685 }
6686 else
6687 {
6688 Elf_Internal_Sym *sym;
6689 Elf_Internal_Sym *locsyms = *locsymsp;
6690
6691 if (locsyms == NULL)
6692 {
6693 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6694 if (locsyms == NULL)
6695 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6696 symtab_hdr->sh_info,
6697 0, NULL, NULL, NULL);
6698 if (locsyms == NULL)
6699 return FALSE;
6700 *locsymsp = locsyms;
6701 }
6702 sym = locsyms + r_symndx;
6703
6704 if (hp != NULL)
6705 *hp = NULL;
6706
6707 if (symp != NULL)
6708 *symp = sym;
6709
6710 if (symsecp != NULL)
cb33740c 6711 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6712
e7b938ca 6713 if (tls_maskp != NULL)
411e1bfb
AM
6714 {
6715 struct got_entry **lgot_ents;
f961d9dd 6716 unsigned char *tls_mask;
411e1bfb 6717
e7b938ca 6718 tls_mask = NULL;
411e1bfb
AM
6719 lgot_ents = elf_local_got_ents (ibfd);
6720 if (lgot_ents != NULL)
6721 {
e054468f
AM
6722 struct plt_entry **local_plt = (struct plt_entry **)
6723 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6724 unsigned char *lgot_masks = (unsigned char *)
e054468f 6725 (local_plt + symtab_hdr->sh_info);
e7b938ca 6726 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6727 }
e7b938ca 6728 *tls_maskp = tls_mask;
411e1bfb
AM
6729 }
6730 }
6731 return TRUE;
6732}
6733
e7b938ca 6734/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6735 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6736 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6737
6738static int
f961d9dd 6739get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6740 unsigned long *toc_symndx,
6741 bfd_vma *toc_addend,
0d4792f7 6742 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6743 const Elf_Internal_Rela *rel,
6744 bfd *ibfd)
411e1bfb
AM
6745{
6746 unsigned long r_symndx;
0d4792f7 6747 int next_r;
411e1bfb
AM
6748 struct elf_link_hash_entry *h;
6749 Elf_Internal_Sym *sym;
6750 asection *sec;
6751 bfd_vma off;
6752
6753 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6754 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6755 return 0;
411e1bfb 6756
e7b938ca 6757 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6758 || sec == NULL
6bee8834 6759 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6760 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6761 return 1;
411e1bfb
AM
6762
6763 /* Look inside a TOC section too. */
6764 if (h != NULL)
6765 {
6766 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6767 off = h->root.u.def.value;
6768 }
6769 else
6770 off = sym->st_value;
6771 off += rel->r_addend;
6772 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6773 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6774 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6775 if (toc_symndx != NULL)
6776 *toc_symndx = r_symndx;
3a71aa26
AM
6777 if (toc_addend != NULL)
6778 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6779 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6780 return 0;
854b41e7 6781 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6782 && (next_r == -1 || next_r == -2))
6783 return 1 - next_r;
951fd09b 6784 return 1;
411e1bfb
AM
6785}
6786
3b421ab3
AM
6787/* Find (or create) an entry in the tocsave hash table. */
6788
6789static struct tocsave_entry *
6790tocsave_find (struct ppc_link_hash_table *htab,
6791 enum insert_option insert,
6792 Elf_Internal_Sym **local_syms,
6793 const Elf_Internal_Rela *irela,
6794 bfd *ibfd)
6795{
6796 unsigned long r_indx;
6797 struct elf_link_hash_entry *h;
6798 Elf_Internal_Sym *sym;
6799 struct tocsave_entry ent, *p;
6800 hashval_t hash;
6801 struct tocsave_entry **slot;
6802
6803 r_indx = ELF64_R_SYM (irela->r_info);
6804 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6805 return NULL;
6806 if (ent.sec == NULL || ent.sec->output_section == NULL)
6807 {
6808 (*_bfd_error_handler)
6809 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
6810 return NULL;
6811 }
6812
6813 if (h != NULL)
6814 ent.offset = h->root.u.def.value;
6815 else
6816 ent.offset = sym->st_value;
6817 ent.offset += irela->r_addend;
6818
6819 hash = tocsave_htab_hash (&ent);
6820 slot = ((struct tocsave_entry **)
6821 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6822 if (slot == NULL)
6823 return NULL;
6824
6825 if (*slot == NULL)
6826 {
6827 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6828 if (p == NULL)
6829 return NULL;
6830 *p = ent;
6831 *slot = p;
6832 }
6833 return *slot;
6834}
6835
754021d0 6836/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6837 code for the old ABI, these will already have been done. */
754021d0
AM
6838
6839static bfd_boolean
6840adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6841{
6842 struct ppc_link_hash_entry *eh;
6843 asection *sym_sec;
74f0fb50 6844 struct _opd_sec_data *opd;
754021d0
AM
6845
6846 if (h->root.type == bfd_link_hash_indirect)
6847 return TRUE;
6848
754021d0
AM
6849 if (h->root.type != bfd_link_hash_defined
6850 && h->root.type != bfd_link_hash_defweak)
6851 return TRUE;
6852
6853 eh = (struct ppc_link_hash_entry *) h;
6854 if (eh->adjust_done)
6855 return TRUE;
6856
6857 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6858 opd = get_opd_info (sym_sec);
6859 if (opd != NULL && opd->adjust != NULL)
754021d0 6860 {
74f0fb50 6861 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6862 if (adjust == -1)
6863 {
6864 /* This entry has been deleted. */
b3fac117 6865 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6866 if (dsec == NULL)
6867 {
6868 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6869 if (elf_discarded_section (dsec))
6870 {
b3fac117 6871 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6872 break;
6873 }
6874 }
4025353c 6875 eh->elf.root.u.def.value = 0;
81688140 6876 eh->elf.root.u.def.section = dsec;
4025353c
AM
6877 }
6878 else
6879 eh->elf.root.u.def.value += adjust;
754021d0
AM
6880 eh->adjust_done = 1;
6881 }
6882 return TRUE;
6883}
6884
8c1d1bb8
AM
6885/* Handles decrementing dynamic reloc counts for the reloc specified by
6886 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6887 have already been determined. */
6888
6889static bfd_boolean
6890dec_dynrel_count (bfd_vma r_info,
6891 asection *sec,
6892 struct bfd_link_info *info,
6893 Elf_Internal_Sym **local_syms,
6894 struct elf_link_hash_entry *h,
6895 asection *sym_sec)
6896{
6897 enum elf_ppc64_reloc_type r_type;
6061a67d
AM
6898 struct elf_dyn_relocs *p;
6899 struct elf_dyn_relocs **pp;
8c1d1bb8
AM
6900
6901 /* Can this reloc be dynamic? This switch, and later tests here
6902 should be kept in sync with the code in check_relocs. */
6903 r_type = ELF64_R_TYPE (r_info);
6904 switch (r_type)
6905 {
6906 default:
6907 return TRUE;
6908
6909 case R_PPC64_TPREL16:
6910 case R_PPC64_TPREL16_LO:
6911 case R_PPC64_TPREL16_HI:
6912 case R_PPC64_TPREL16_HA:
6913 case R_PPC64_TPREL16_DS:
6914 case R_PPC64_TPREL16_LO_DS:
6915 case R_PPC64_TPREL16_HIGHER:
6916 case R_PPC64_TPREL16_HIGHERA:
6917 case R_PPC64_TPREL16_HIGHEST:
6918 case R_PPC64_TPREL16_HIGHESTA:
6919 if (!info->shared)
6920 return TRUE;
6921
6922 case R_PPC64_TPREL64:
6923 case R_PPC64_DTPMOD64:
6924 case R_PPC64_DTPREL64:
6925 case R_PPC64_ADDR64:
6926 case R_PPC64_REL30:
6927 case R_PPC64_REL32:
6928 case R_PPC64_REL64:
6929 case R_PPC64_ADDR14:
6930 case R_PPC64_ADDR14_BRNTAKEN:
6931 case R_PPC64_ADDR14_BRTAKEN:
6932 case R_PPC64_ADDR16:
6933 case R_PPC64_ADDR16_DS:
6934 case R_PPC64_ADDR16_HA:
6935 case R_PPC64_ADDR16_HI:
6936 case R_PPC64_ADDR16_HIGHER:
6937 case R_PPC64_ADDR16_HIGHERA:
6938 case R_PPC64_ADDR16_HIGHEST:
6939 case R_PPC64_ADDR16_HIGHESTA:
6940 case R_PPC64_ADDR16_LO:
6941 case R_PPC64_ADDR16_LO_DS:
6942 case R_PPC64_ADDR24:
6943 case R_PPC64_ADDR32:
6944 case R_PPC64_UADDR16:
6945 case R_PPC64_UADDR32:
6946 case R_PPC64_UADDR64:
6947 case R_PPC64_TOC:
6948 break;
6949 }
6950
6951 if (local_syms != NULL)
6952 {
6953 unsigned long r_symndx;
6954 Elf_Internal_Sym *sym;
6955 bfd *ibfd = sec->owner;
6956
6957 r_symndx = ELF64_R_SYM (r_info);
6958 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6959 return FALSE;
6960 }
6961
6962 if ((info->shared
1d483afe 6963 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8
AM
6964 || (h != NULL
6965 && (!info->symbolic
6966 || h->root.type == bfd_link_hash_defweak
6967 || !h->def_regular))))
6968 || (ELIMINATE_COPY_RELOCS
6969 && !info->shared
6970 && h != NULL
6971 && (h->root.type == bfd_link_hash_defweak
6972 || !h->def_regular)))
6973 ;
6974 else
6975 return TRUE;
6976
6977 if (h != NULL)
6978 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6979 else
6edfbbad 6980 {
60124e18
AM
6981 if (sym_sec != NULL)
6982 {
6983 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6061a67d 6984 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6985 }
6986 else
6987 {
6988 void *vpp = &elf_section_data (sec)->local_dynrel;
6061a67d 6989 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6990 }
6991
6992 /* elf_gc_sweep may have already removed all dyn relocs associated
6993 with local syms for a given section. Don't report a dynreloc
6994 miscount. */
6995 if (*pp == NULL)
6996 return TRUE;
6edfbbad 6997 }
8c1d1bb8
AM
6998
6999 while ((p = *pp) != NULL)
7000 {
7001 if (p->sec == sec)
7002 {
1d483afe 7003 if (!must_be_dyn_reloc (info, r_type))
8c1d1bb8
AM
7004 p->pc_count -= 1;
7005 p->count -= 1;
7006 if (p->count == 0)
7007 *pp = p->next;
7008 return TRUE;
7009 }
7010 pp = &p->next;
7011 }
7012
8de848d8 7013 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7014 sec->owner, sec);
8c1d1bb8
AM
7015 bfd_set_error (bfd_error_bad_value);
7016 return FALSE;
7017}
7018
754021d0
AM
7019/* Remove unused Official Procedure Descriptor entries. Currently we
7020 only remove those associated with functions in discarded link-once
7021 sections, or weakly defined functions that have been overridden. It
7022 would be possible to remove many more entries for statically linked
7023 applications. */
7024
b34976b6 7025bfd_boolean
33c0ec9d 7026ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
7027{
7028 bfd *ibfd;
754021d0 7029 bfd_boolean some_edited = FALSE;
3f764659 7030 asection *need_pad = NULL;
1e2f5b6e 7031
411e1bfb 7032 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
7033 {
7034 asection *sec;
7035 Elf_Internal_Rela *relstart, *rel, *relend;
7036 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7037 Elf_Internal_Sym *local_syms;
1e2f5b6e 7038 bfd_vma offset;
74f0fb50 7039 struct _opd_sec_data *opd;
3f764659
JJ
7040 bfd_boolean need_edit, add_aux_fields;
7041 bfd_size_type cnt_16b = 0;
1e2f5b6e 7042
854b41e7
AM
7043 if (!is_ppc64_elf (ibfd))
7044 continue;
7045
1e2f5b6e 7046 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7047 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7048 continue;
7049
4b85d634
AM
7050 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
7051 continue;
7052
1e2f5b6e
AM
7053 if (sec->output_section == bfd_abs_section_ptr)
7054 continue;
7055
7056 /* Look through the section relocs. */
7057 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7058 continue;
7059
6cdc0ccc 7060 local_syms = NULL;
0ffa91dd 7061 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7062
7063 /* Read the relocations. */
4ce794b7 7064 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7065 info->keep_memory);
1e2f5b6e 7066 if (relstart == NULL)
b34976b6 7067 return FALSE;
1e2f5b6e
AM
7068
7069 /* First run through the relocs to check they are sane, and to
7070 determine whether we need to edit this opd section. */
b34976b6 7071 need_edit = FALSE;
3f764659 7072 need_pad = sec;
1e2f5b6e
AM
7073 offset = 0;
7074 relend = relstart + sec->reloc_count;
50bc7936 7075 for (rel = relstart; rel < relend; )
1e2f5b6e 7076 {
04c9666a 7077 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7078 unsigned long r_symndx;
7079 asection *sym_sec;
7080 struct elf_link_hash_entry *h;
7081 Elf_Internal_Sym *sym;
7082
3f764659 7083 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7084 only interested in the reloc pointing to a function entry
7085 point. */
50bc7936
AM
7086 if (rel->r_offset != offset
7087 || rel + 1 >= relend
7088 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
7089 {
7090 /* If someone messes with .opd alignment then after a
7091 "ld -r" we might have padding in the middle of .opd.
7092 Also, there's nothing to prevent someone putting
7093 something silly in .opd with the assembler. No .opd
b34976b6 7094 optimization for them! */
3f764659 7095 broken_opd:
1e2f5b6e 7096 (*_bfd_error_handler)
d003868e 7097 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 7098 need_edit = FALSE;
1e2f5b6e
AM
7099 break;
7100 }
7101
50bc7936
AM
7102 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7103 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7104 {
7105 (*_bfd_error_handler)
d003868e
AM
7106 (_("%B: unexpected reloc type %u in .opd section"),
7107 ibfd, r_type);
50bc7936
AM
7108 need_edit = FALSE;
7109 break;
7110 }
7111
1e2f5b6e 7112 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7113 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7114 r_symndx, ibfd))
50bc7936 7115 goto error_ret;
1e2f5b6e
AM
7116
7117 if (sym_sec == NULL || sym_sec->owner == NULL)
7118 {
411e1bfb
AM
7119 const char *sym_name;
7120 if (h != NULL)
7121 sym_name = h->root.root.string;
7122 else
26c61ae5
L
7123 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7124 sym_sec);
411e1bfb 7125
1e2f5b6e 7126 (*_bfd_error_handler)
d003868e
AM
7127 (_("%B: undefined sym `%s' in .opd section"),
7128 ibfd, sym_name);
b34976b6 7129 need_edit = FALSE;
1e2f5b6e
AM
7130 break;
7131 }
7132
51020317
AM
7133 /* opd entries are always for functions defined in the
7134 current input bfd. If the symbol isn't defined in the
7135 input bfd, then we won't be using the function in this
7136 bfd; It must be defined in a linkonce section in another
7137 bfd, or is weak. It's also possible that we are
7138 discarding the function due to a linker script /DISCARD/,
7139 which we test for via the output_section. */
7140 if (sym_sec->owner != ibfd
7141 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7142 need_edit = TRUE;
1e2f5b6e 7143
50bc7936 7144 rel += 2;
3f764659
JJ
7145 if (rel == relend
7146 || (rel + 1 == relend && rel->r_offset == offset + 16))
7147 {
7148 if (sec->size == offset + 24)
7149 {
7150 need_pad = NULL;
7151 break;
7152 }
7153 if (rel == relend && sec->size == offset + 16)
7154 {
7155 cnt_16b++;
7156 break;
7157 }
7158 goto broken_opd;
7159 }
7160
7161 if (rel->r_offset == offset + 24)
7162 offset += 24;
7163 else if (rel->r_offset != offset + 16)
7164 goto broken_opd;
7165 else if (rel + 1 < relend
7166 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7167 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7168 {
7169 offset += 16;
7170 cnt_16b++;
7171 }
7172 else if (rel + 2 < relend
7173 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7174 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7175 {
7176 offset += 24;
7177 rel += 1;
7178 }
7179 else
7180 goto broken_opd;
1e2f5b6e
AM
7181 }
7182
3f764659
JJ
7183 add_aux_fields = non_overlapping && cnt_16b > 0;
7184
7185 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7186 {
7187 Elf_Internal_Rela *write_rel;
d4730f92 7188 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7189 bfd_byte *rptr, *wptr;
983bddc8 7190 bfd_byte *new_contents;
b34976b6 7191 bfd_boolean skip;
3f764659 7192 long opd_ent_size;
74f0fb50
AM
7193 bfd_size_type amt;
7194
983bddc8 7195 new_contents = NULL;
74f0fb50
AM
7196 amt = sec->size * sizeof (long) / 8;
7197 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7198 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7199 if (opd->adjust == NULL)
7200 return FALSE;
7201 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7202
7203 /* This seems a waste of time as input .opd sections are all
7204 zeros as generated by gcc, but I suppose there's no reason
7205 this will always be so. We might start putting something in
7206 the third word of .opd entries. */
7207 if ((sec->flags & SEC_IN_MEMORY) == 0)
7208 {
eea6121a
AM
7209 bfd_byte *loc;
7210 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7211 {
eea6121a
AM
7212 if (loc != NULL)
7213 free (loc);
50bc7936 7214 error_ret:
6cdc0ccc
AM
7215 if (local_syms != NULL
7216 && symtab_hdr->contents != (unsigned char *) local_syms)
7217 free (local_syms);
6cdc0ccc
AM
7218 if (elf_section_data (sec)->relocs != relstart)
7219 free (relstart);
b34976b6 7220 return FALSE;
6cdc0ccc 7221 }
1e2f5b6e
AM
7222 sec->contents = loc;
7223 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7224 }
7225
7226 elf_section_data (sec)->relocs = relstart;
7227
3f764659 7228 new_contents = sec->contents;
3f764659
JJ
7229 if (add_aux_fields)
7230 {
7231 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7232 if (new_contents == NULL)
7233 return FALSE;
7234 need_pad = FALSE;
3f764659 7235 }
b4f4e59f
AM
7236 wptr = new_contents;
7237 rptr = sec->contents;
3f764659 7238
1e2f5b6e 7239 write_rel = relstart;
b34976b6 7240 skip = FALSE;
1e2f5b6e 7241 offset = 0;
3f764659 7242 opd_ent_size = 0;
1e2f5b6e
AM
7243 for (rel = relstart; rel < relend; rel++)
7244 {
50bc7936
AM
7245 unsigned long r_symndx;
7246 asection *sym_sec;
7247 struct elf_link_hash_entry *h;
7248 Elf_Internal_Sym *sym;
7249
7250 r_symndx = ELF64_R_SYM (rel->r_info);
7251 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7252 r_symndx, ibfd))
50bc7936
AM
7253 goto error_ret;
7254
1e2f5b6e
AM
7255 if (rel->r_offset == offset)
7256 {
50bc7936 7257 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7258
7259 /* See if the .opd entry is full 24 byte or
7260 16 byte (with fd_aux entry overlapped with next
7261 fd_func). */
7262 opd_ent_size = 24;
7263 if ((rel + 2 == relend && sec->size == offset + 16)
7264 || (rel + 3 < relend
7265 && rel[2].r_offset == offset + 16
7266 && rel[3].r_offset == offset + 24
7267 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7268 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7269 opd_ent_size = 16;
7270
4025353c
AM
7271 if (h != NULL
7272 && h->root.root.string[0] == '.')
c4f68ce3 7273 {
4dfe6ac6
NC
7274 struct ppc_link_hash_table *htab;
7275
7276 htab = ppc_hash_table (info);
7277 if (htab != NULL)
7278 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7279 htab);
c4f68ce3
AM
7280 if (fdh != NULL
7281 && fdh->elf.root.type != bfd_link_hash_defined
7282 && fdh->elf.root.type != bfd_link_hash_defweak)
7283 fdh = NULL;
7284 }
1e2f5b6e 7285
51020317
AM
7286 skip = (sym_sec->owner != ibfd
7287 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7288 if (skip)
7289 {
4025353c 7290 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7291 {
7292 /* Arrange for the function descriptor sym
7293 to be dropped. */
d6fe2dc1
AM
7294 fdh->elf.root.u.def.value = 0;
7295 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7296 }
74f0fb50 7297 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7298 }
7299 else
1e2f5b6e
AM
7300 {
7301 /* We'll be keeping this opd entry. */
7302
4025353c 7303 if (fdh != NULL)
1e2f5b6e 7304 {
754021d0
AM
7305 /* Redefine the function descriptor symbol to
7306 this location in the opd section. It is
7307 necessary to update the value here rather
7308 than using an array of adjustments as we do
7309 for local symbols, because various places
7310 in the generic ELF code use the value
7311 stored in u.def.value. */
3f764659 7312 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7313 fdh->adjust_done = 1;
1e2f5b6e 7314 }
754021d0
AM
7315
7316 /* Local syms are a bit tricky. We could
7317 tweak them as they can be cached, but
7318 we'd need to look through the local syms
7319 for the function descriptor sym which we
7320 don't have at the moment. So keep an
7321 array of adjustments. */
74f0fb50 7322 opd->adjust[rel->r_offset / 8]
3f764659 7323 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7324
7325 if (wptr != rptr)
3f764659
JJ
7326 memcpy (wptr, rptr, opd_ent_size);
7327 wptr += opd_ent_size;
7328 if (add_aux_fields && opd_ent_size == 16)
7329 {
7330 memset (wptr, '\0', 8);
7331 wptr += 8;
7332 }
1e2f5b6e 7333 }
3f764659
JJ
7334 rptr += opd_ent_size;
7335 offset += opd_ent_size;
1e2f5b6e
AM
7336 }
7337
50bc7936
AM
7338 if (skip)
7339 {
60124e18
AM
7340 if (!NO_OPD_RELOCS
7341 && !info->relocatable
18d944df
AM
7342 && !dec_dynrel_count (rel->r_info, sec, info,
7343 NULL, h, sym_sec))
8c1d1bb8 7344 goto error_ret;
50bc7936
AM
7345 }
7346 else
1e2f5b6e 7347 {
50bc7936
AM
7348 /* We need to adjust any reloc offsets to point to the
7349 new opd entries. While we're at it, we may as well
7350 remove redundant relocs. */
74f0fb50 7351 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7352 if (write_rel != rel)
7353 memcpy (write_rel, rel, sizeof (*rel));
7354 ++write_rel;
7355 }
7356 }
7357
3f764659 7358 sec->size = wptr - new_contents;
1e2f5b6e 7359 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7360 if (add_aux_fields)
7361 {
7362 free (sec->contents);
7363 sec->contents = new_contents;
7364 }
7365
05bf9422 7366 /* Fudge the header size too, as this is used later in
cdcf6e38 7367 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7368 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7369 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7370 some_edited = TRUE;
1e2f5b6e 7371 }
6cdc0ccc 7372 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7373 free (relstart);
6cdc0ccc 7374
411e1bfb
AM
7375 if (local_syms != NULL
7376 && symtab_hdr->contents != (unsigned char *) local_syms)
7377 {
7378 if (!info->keep_memory)
7379 free (local_syms);
7380 else
7381 symtab_hdr->contents = (unsigned char *) local_syms;
7382 }
7383 }
7384
754021d0
AM
7385 if (some_edited)
7386 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7387
3f764659
JJ
7388 /* If we are doing a final link and the last .opd entry is just 16 byte
7389 long, add a 8 byte padding after it. */
7390 if (need_pad != NULL && !info->relocatable)
7391 {
7392 bfd_byte *p;
7393
7394 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7395 {
7396 BFD_ASSERT (need_pad->size > 0);
7397
7398 p = bfd_malloc (need_pad->size + 8);
7399 if (p == NULL)
7400 return FALSE;
699733f6 7401
3f764659
JJ
7402 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7403 p, 0, need_pad->size))
7404 return FALSE;
7405
7406 need_pad->contents = p;
7407 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7408 }
7409 else
7410 {
7411 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7412 if (p == NULL)
7413 return FALSE;
7414
7415 need_pad->contents = p;
7416 }
7417
7418 memset (need_pad->contents + need_pad->size, 0, 8);
7419 need_pad->size += 8;
7420 }
7421
411e1bfb
AM
7422 return TRUE;
7423}
7424
e1918d23 7425/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7426
e1918d23 7427asection *
33c0ec9d
AM
7428ppc64_elf_tls_setup (struct bfd_link_info *info,
7429 int no_tls_get_addr_opt,
7430 int *no_multi_toc)
411e1bfb 7431{
411e1bfb
AM
7432 struct ppc_link_hash_table *htab;
7433
411e1bfb 7434 htab = ppc_hash_table (info);
4dfe6ac6
NC
7435 if (htab == NULL)
7436 return NULL;
7437
33c0ec9d
AM
7438 if (*no_multi_toc)
7439 htab->do_multi_toc = 0;
7440 else if (!htab->do_multi_toc)
7441 *no_multi_toc = 1;
7442
3a71aa26
AM
7443 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7444 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7445 FALSE, FALSE, TRUE));
a7f2871e
AM
7446 /* Move dynamic linking info to the function descriptor sym. */
7447 if (htab->tls_get_addr != NULL)
7448 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7449 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7450 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7451 FALSE, FALSE, TRUE));
a7f2871e
AM
7452 if (!no_tls_get_addr_opt)
7453 {
7454 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7455
7456 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7457 FALSE, FALSE, TRUE);
7458 if (opt != NULL)
7459 func_desc_adjust (opt, info);
7460 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7461 FALSE, FALSE, TRUE);
7462 if (opt_fd != NULL
7463 && (opt_fd->root.type == bfd_link_hash_defined
7464 || opt_fd->root.type == bfd_link_hash_defweak))
7465 {
7466 /* If glibc supports an optimized __tls_get_addr call stub,
7467 signalled by the presence of __tls_get_addr_opt, and we'll
7468 be calling __tls_get_addr via a plt call stub, then
7469 make __tls_get_addr point to __tls_get_addr_opt. */
7470 tga_fd = &htab->tls_get_addr_fd->elf;
7471 if (htab->elf.dynamic_sections_created
7472 && tga_fd != NULL
7473 && (tga_fd->type == STT_FUNC
7474 || tga_fd->needs_plt)
7475 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7476 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7477 && tga_fd->root.type == bfd_link_hash_undefweak)))
7478 {
7479 struct plt_entry *ent;
7480
7481 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7482 if (ent->plt.refcount > 0)
7483 break;
7484 if (ent != NULL)
7485 {
7486 tga_fd->root.type = bfd_link_hash_indirect;
7487 tga_fd->root.u.i.link = &opt_fd->root;
7488 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7489 if (opt_fd->dynindx != -1)
7490 {
7491 /* Use __tls_get_addr_opt in dynamic relocations. */
7492 opt_fd->dynindx = -1;
7493 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7494 opt_fd->dynstr_index);
7495 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7496 return NULL;
a7f2871e
AM
7497 }
7498 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7499 tga = &htab->tls_get_addr->elf;
7500 if (opt != NULL && tga != NULL)
7501 {
7502 tga->root.type = bfd_link_hash_indirect;
7503 tga->root.u.i.link = &opt->root;
7504 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7505 _bfd_elf_link_hash_hide_symbol (info, opt,
7506 tga->forced_local);
7507 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7508 }
7509 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7510 htab->tls_get_addr_fd->is_func_descriptor = 1;
7511 if (htab->tls_get_addr != NULL)
7512 {
7513 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7514 htab->tls_get_addr->is_func = 1;
7515 }
7516 }
7517 }
7518 }
7519 else
7520 no_tls_get_addr_opt = TRUE;
7521 }
7522 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7523 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7524}
8387904d 7525
3a71aa26
AM
7526/* Return TRUE iff REL is a branch reloc with a global symbol matching
7527 HASH1 or HASH2. */
8387904d 7528
3a71aa26
AM
7529static bfd_boolean
7530branch_reloc_hash_match (const bfd *ibfd,
7531 const Elf_Internal_Rela *rel,
7532 const struct ppc_link_hash_entry *hash1,
7533 const struct ppc_link_hash_entry *hash2)
7534{
7535 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7536 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7537 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7538
e054468f 7539 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7540 {
3a71aa26
AM
7541 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7542 struct elf_link_hash_entry *h;
8387904d 7543
3a71aa26 7544 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7545 h = elf_follow_link (h);
3a71aa26
AM
7546 if (h == &hash1->elf || h == &hash2->elf)
7547 return TRUE;
a48ebf4d 7548 }
3a71aa26 7549 return FALSE;
951fd09b 7550}
411e1bfb 7551
951fd09b
AM
7552/* Run through all the TLS relocs looking for optimization
7553 opportunities. The linker has been hacked (see ppc64elf.em) to do
7554 a preliminary section layout so that we know the TLS segment
7555 offsets. We can't optimize earlier because some optimizations need
7556 to know the tp offset, and we need to optimize before allocating
7557 dynamic relocations. */
7558
7559bfd_boolean
33c0ec9d 7560ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7561{
7562 bfd *ibfd;
7563 asection *sec;
7564 struct ppc_link_hash_table *htab;
663a1470 7565 unsigned char *toc_ref;
102890f0 7566 int pass;
951fd09b 7567
1d483afe 7568 if (info->relocatable || !info->executable)
411e1bfb
AM
7569 return TRUE;
7570
951fd09b 7571 htab = ppc_hash_table (info);
4dfe6ac6
NC
7572 if (htab == NULL)
7573 return FALSE;
7574
663a1470
AM
7575 /* Make two passes over the relocs. On the first pass, mark toc
7576 entries involved with tls relocs, and check that tls relocs
7577 involved in setting up a tls_get_addr call are indeed followed by
7578 such a call. If they are not, we can't do any tls optimization.
7579 On the second pass twiddle tls_mask flags to notify
7580 relocate_section that optimization can be done, and adjust got
7581 and plt refcounts. */
7582 toc_ref = NULL;
7583 for (pass = 0; pass < 2; ++pass)
7584 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7585 {
7586 Elf_Internal_Sym *locsyms = NULL;
7587 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7588
102890f0
AM
7589 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7590 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7591 {
7592 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7593 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7594
102890f0
AM
7595 /* Read the relocations. */
7596 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7597 info->keep_memory);
7598 if (relstart == NULL)
7599 return FALSE;
411e1bfb 7600
102890f0
AM
7601 relend = relstart + sec->reloc_count;
7602 for (rel = relstart; rel < relend; rel++)
7603 {
7604 enum elf_ppc64_reloc_type r_type;
7605 unsigned long r_symndx;
7606 struct elf_link_hash_entry *h;
7607 Elf_Internal_Sym *sym;
7608 asection *sym_sec;
f961d9dd
AM
7609 unsigned char *tls_mask;
7610 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7611 bfd_vma value;
7612 bfd_boolean ok_tprel, is_local;
7613 long toc_ref_index = 0;
7614 int expecting_tls_get_addr = 0;
663a1470 7615 bfd_boolean ret = FALSE;
411e1bfb 7616
102890f0
AM
7617 r_symndx = ELF64_R_SYM (rel->r_info);
7618 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7619 r_symndx, ibfd))
7620 {
7621 err_free_rel:
7622 if (elf_section_data (sec)->relocs != relstart)
7623 free (relstart);
7624 if (toc_ref != NULL)
7625 free (toc_ref);
7626 if (locsyms != NULL
0ffa91dd 7627 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7628 != (unsigned char *) locsyms))
7629 free (locsyms);
663a1470 7630 return ret;
102890f0 7631 }
411e1bfb 7632
102890f0
AM
7633 if (h != NULL)
7634 {
766bc656
AM
7635 if (h->root.type == bfd_link_hash_defined
7636 || h->root.type == bfd_link_hash_defweak)
7637 value = h->root.u.def.value;
7638 else if (h->root.type == bfd_link_hash_undefweak)
7639 value = 0;
7640 else
663a1470
AM
7641 {
7642 found_tls_get_addr_arg = 0;
7643 continue;
7644 }
102890f0
AM
7645 }
7646 else
7647 /* Symbols referenced by TLS relocs must be of type
7648 STT_TLS. So no need for .opd local sym adjust. */
7649 value = sym->st_value;
7650
7651 ok_tprel = FALSE;
7652 is_local = FALSE;
7653 if (h == NULL
7654 || !h->def_dynamic)
7655 {
7656 is_local = TRUE;
766bc656
AM
7657 if (h != NULL
7658 && h->root.type == bfd_link_hash_undefweak)
7659 ok_tprel = TRUE;
7660 else
7661 {
7662 value += sym_sec->output_offset;
7663 value += sym_sec->output_section->vma;
7664 value -= htab->elf.tls_sec->vma;
7665 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7666 < (bfd_vma) 1 << 32);
7667 }
102890f0 7668 }
951fd09b 7669
102890f0 7670 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7671 /* If this section has old-style __tls_get_addr calls
7672 without marker relocs, then check that each
7673 __tls_get_addr call reloc is preceded by a reloc
7674 that conceivably belongs to the __tls_get_addr arg
7675 setup insn. If we don't find matching arg setup
7676 relocs, don't do any tls optimization. */
7677 if (pass == 0
7678 && sec->has_tls_get_addr_call
7679 && h != NULL
7680 && (h == &htab->tls_get_addr->elf
7681 || h == &htab->tls_get_addr_fd->elf)
7682 && !found_tls_get_addr_arg
7683 && is_branch_reloc (r_type))
7684 {
25f53a85 7685 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7686 "TLS optimization disabled\n"),
7687 ibfd, sec, rel->r_offset);
7688 ret = TRUE;
7689 goto err_free_rel;
7690 }
7691
7692 found_tls_get_addr_arg = 0;
102890f0
AM
7693 switch (r_type)
7694 {
7695 case R_PPC64_GOT_TLSLD16:
7696 case R_PPC64_GOT_TLSLD16_LO:
7697 expecting_tls_get_addr = 1;
663a1470 7698 found_tls_get_addr_arg = 1;
102890f0
AM
7699 /* Fall thru */
7700
7701 case R_PPC64_GOT_TLSLD16_HI:
7702 case R_PPC64_GOT_TLSLD16_HA:
7703 /* These relocs should never be against a symbol
7704 defined in a shared lib. Leave them alone if
7705 that turns out to be the case. */
7706 if (!is_local)
7707 continue;
411e1bfb 7708
102890f0 7709 /* LD -> LE */
411e1bfb 7710 tls_set = 0;
102890f0
AM
7711 tls_clear = TLS_LD;
7712 tls_type = TLS_TLS | TLS_LD;
7713 break;
411e1bfb 7714
102890f0
AM
7715 case R_PPC64_GOT_TLSGD16:
7716 case R_PPC64_GOT_TLSGD16_LO:
7717 expecting_tls_get_addr = 1;
663a1470 7718 found_tls_get_addr_arg = 1;
102890f0
AM
7719 /* Fall thru */
7720
7721 case R_PPC64_GOT_TLSGD16_HI:
7722 case R_PPC64_GOT_TLSGD16_HA:
7723 if (ok_tprel)
7724 /* GD -> LE */
411e1bfb 7725 tls_set = 0;
102890f0
AM
7726 else
7727 /* GD -> IE */
7728 tls_set = TLS_TLS | TLS_TPRELGD;
7729 tls_clear = TLS_GD;
7730 tls_type = TLS_TLS | TLS_GD;
7731 break;
7732
7733 case R_PPC64_GOT_TPREL16_DS:
7734 case R_PPC64_GOT_TPREL16_LO_DS:
7735 case R_PPC64_GOT_TPREL16_HI:
7736 case R_PPC64_GOT_TPREL16_HA:
7737 if (ok_tprel)
7738 {
7739 /* IE -> LE */
7740 tls_set = 0;
7741 tls_clear = TLS_TPREL;
7742 tls_type = TLS_TLS | TLS_TPREL;
7743 break;
7744 }
411e1bfb
AM
7745 continue;
7746
727fc41e
AM
7747 case R_PPC64_TLSGD:
7748 case R_PPC64_TLSLD:
663a1470
AM
7749 found_tls_get_addr_arg = 1;
7750 /* Fall thru */
7751
7752 case R_PPC64_TLS:
7753 case R_PPC64_TOC16:
7754 case R_PPC64_TOC16_LO:
102890f0
AM
7755 if (sym_sec == NULL || sym_sec != toc)
7756 continue;
7757
7758 /* Mark this toc entry as referenced by a TLS
7759 code sequence. We can do that now in the
7760 case of R_PPC64_TLS, and after checking for
7761 tls_get_addr for the TOC16 relocs. */
7762 if (toc_ref == NULL)
663a1470
AM
7763 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7764 if (toc_ref == NULL)
7765 goto err_free_rel;
7766
102890f0
AM
7767 if (h != NULL)
7768 value = h->root.u.def.value;
7769 else
7770 value = sym->st_value;
7771 value += rel->r_addend;
7772 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 7773 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7774 if (r_type == R_PPC64_TLS
7775 || r_type == R_PPC64_TLSGD
7776 || r_type == R_PPC64_TLSLD)
102890f0
AM
7777 {
7778 toc_ref[toc_ref_index] = 1;
7779 continue;
7780 }
7781
7782 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7783 continue;
7784
7785 tls_set = 0;
7786 tls_clear = 0;
7787 expecting_tls_get_addr = 2;
7788 break;
7789
7790 case R_PPC64_TPREL64:
7791 if (pass == 0
7792 || sec != toc
7793 || toc_ref == NULL
663a1470 7794 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7795 continue;
7796 if (ok_tprel)
7797 {
7798 /* IE -> LE */
7799 tls_set = TLS_EXPLICIT;
7800 tls_clear = TLS_TPREL;
7801 break;
7802 }
7803 continue;
7804
7805 case R_PPC64_DTPMOD64:
7806 if (pass == 0
7807 || sec != toc
7808 || toc_ref == NULL
663a1470 7809 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7810 continue;
7811 if (rel + 1 < relend
7812 && (rel[1].r_info
7813 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7814 && rel[1].r_offset == rel->r_offset + 8)
7815 {
7816 if (ok_tprel)
7817 /* GD -> LE */
7818 tls_set = TLS_EXPLICIT | TLS_GD;
7819 else
7820 /* GD -> IE */
7821 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7822 tls_clear = TLS_GD;
7823 }
7824 else
7825 {
7826 if (!is_local)
7827 continue;
7828
7829 /* LD -> LE */
7830 tls_set = TLS_EXPLICIT;
7831 tls_clear = TLS_LD;
7832 }
7833 break;
7834
7835 default:
7836 continue;
7837 }
7838
7839 if (pass == 0)
7840 {
727fc41e
AM
7841 if (!expecting_tls_get_addr
7842 || !sec->has_tls_get_addr_call)
102890f0
AM
7843 continue;
7844
3a71aa26
AM
7845 if (rel + 1 < relend
7846 && branch_reloc_hash_match (ibfd, rel + 1,
7847 htab->tls_get_addr,
7848 htab->tls_get_addr_fd))
102890f0 7849 {
3a71aa26 7850 if (expecting_tls_get_addr == 2)
102890f0 7851 {
3a71aa26 7852 /* Check for toc tls entries. */
f961d9dd 7853 unsigned char *toc_tls;
3a71aa26
AM
7854 int retval;
7855
7856 retval = get_tls_mask (&toc_tls, NULL, NULL,
7857 &locsyms,
7858 rel, ibfd);
7859 if (retval == 0)
7860 goto err_free_rel;
663a1470
AM
7861 if (toc_tls != NULL)
7862 {
7863 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7864 found_tls_get_addr_arg = 1;
7865 if (retval > 1)
7866 toc_ref[toc_ref_index] = 1;
7867 }
102890f0 7868 }
3a71aa26 7869 continue;
102890f0
AM
7870 }
7871
7872 if (expecting_tls_get_addr != 1)
7873 continue;
7874
7875 /* Uh oh, we didn't find the expected call. We
7876 could just mark this symbol to exclude it
7877 from tls optimization but it's safer to skip
663a1470 7878 the entire optimization. */
25f53a85 7879 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7880 "TLS optimization disabled\n"),
7881 ibfd, sec, rel->r_offset);
7882 ret = TRUE;
7883 goto err_free_rel;
102890f0
AM
7884 }
7885
85f7a9cb 7886 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
7887 {
7888 struct plt_entry *ent;
7889 for (ent = htab->tls_get_addr->elf.plt.plist;
7890 ent != NULL;
7891 ent = ent->next)
7892 if (ent->addend == 0)
411e1bfb 7893 {
102890f0 7894 if (ent->plt.refcount > 0)
30038c59 7895 {
102890f0
AM
7896 ent->plt.refcount -= 1;
7897 expecting_tls_get_addr = 0;
30038c59 7898 }
102890f0 7899 break;
411e1bfb 7900 }
102890f0 7901 }
411e1bfb 7902
85f7a9cb 7903 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
7904 {
7905 struct plt_entry *ent;
7906 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7907 ent != NULL;
7908 ent = ent->next)
7909 if (ent->addend == 0)
411e1bfb 7910 {
102890f0
AM
7911 if (ent->plt.refcount > 0)
7912 ent->plt.refcount -= 1;
7913 break;
411e1bfb 7914 }
102890f0 7915 }
411e1bfb 7916
102890f0 7917 if (tls_clear == 0)
30038c59
AM
7918 continue;
7919
102890f0
AM
7920 if ((tls_set & TLS_EXPLICIT) == 0)
7921 {
7922 struct got_entry *ent;
411e1bfb 7923
102890f0
AM
7924 /* Adjust got entry for this reloc. */
7925 if (h != NULL)
7926 ent = h->got.glist;
7927 else
7928 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7929
102890f0
AM
7930 for (; ent != NULL; ent = ent->next)
7931 if (ent->addend == rel->r_addend
7932 && ent->owner == ibfd
7933 && ent->tls_type == tls_type)
7934 break;
7935 if (ent == NULL)
7936 abort ();
411e1bfb 7937
102890f0
AM
7938 if (tls_set == 0)
7939 {
7940 /* We managed to get rid of a got entry. */
7941 if (ent->got.refcount > 0)
7942 ent->got.refcount -= 1;
7943 }
7944 }
7945 else
7946 {
7947 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7948 we'll lose one or two dyn relocs. */
7949 if (!dec_dynrel_count (rel->r_info, sec, info,
7950 NULL, h, sym_sec))
7951 return FALSE;
411e1bfb 7952
102890f0
AM
7953 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7954 {
7955 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7956 NULL, h, sym_sec))
7957 return FALSE;
7958 }
7959 }
411e1bfb 7960
102890f0
AM
7961 *tls_mask |= tls_set;
7962 *tls_mask &= ~tls_clear;
7963 }
8c1d1bb8 7964
102890f0
AM
7965 if (elf_section_data (sec)->relocs != relstart)
7966 free (relstart);
7967 }
411e1bfb 7968
663a1470
AM
7969 if (locsyms != NULL
7970 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7971 {
7972 if (!info->keep_memory)
7973 free (locsyms);
7974 else
7975 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7976 }
7977 }
411e1bfb 7978
663a1470
AM
7979 if (toc_ref != NULL)
7980 free (toc_ref);
b34976b6 7981 return TRUE;
1e2f5b6e 7982}
b34976b6 7983
c5614fa4
AM
7984/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7985 the values of any global symbols in a toc section that has been
7986 edited. Globals in toc sections should be a rarity, so this function
7987 sets a flag if any are found in toc sections other than the one just
7988 edited, so that futher hash table traversals can be avoided. */
7989
7990struct adjust_toc_info
7991{
7992 asection *toc;
7993 unsigned long *skip;
7994 bfd_boolean global_toc_syms;
7995};
7996
ba761f19
AM
7997enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7998
c5614fa4
AM
7999static bfd_boolean
8000adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8001{
8002 struct ppc_link_hash_entry *eh;
8003 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8004 unsigned long i;
c5614fa4 8005
c5614fa4
AM
8006 if (h->root.type != bfd_link_hash_defined
8007 && h->root.type != bfd_link_hash_defweak)
8008 return TRUE;
8009
8010 eh = (struct ppc_link_hash_entry *) h;
8011 if (eh->adjust_done)
8012 return TRUE;
8013
8014 if (eh->elf.root.u.def.section == toc_inf->toc)
8015 {
854b41e7
AM
8016 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8017 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8018 else
854b41e7
AM
8019 i = eh->elf.root.u.def.value >> 3;
8020
ba761f19 8021 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8022 {
8023 (*_bfd_error_handler)
854b41e7
AM
8024 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8025 do
8026 ++i;
ba761f19 8027 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8028 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8029 }
854b41e7
AM
8030
8031 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8032 eh->adjust_done = 1;
8033 }
8034 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8035 toc_inf->global_toc_syms = TRUE;
8036
8037 return TRUE;
8038}
8039
560c8763
AM
8040/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8041
8042static bfd_boolean
8043ok_lo_toc_insn (unsigned int insn)
8044{
8045 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8046 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8047 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8048 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8049 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8050 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8051 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8052 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8053 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8054 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8055 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8056 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8057 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8058 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8059 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8060 && (insn & 3) != 1)
8061 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8062 && ((insn & 3) == 0 || (insn & 3) == 3))
8063 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8064}
8065
c5614fa4
AM
8066/* Examine all relocs referencing .toc sections in order to remove
8067 unused .toc entries. */
8068
8069bfd_boolean
33c0ec9d 8070ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8071{
8072 bfd *ibfd;
8073 struct adjust_toc_info toc_inf;
67f0cbdb 8074 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8075
67f0cbdb 8076 htab->do_toc_opt = 1;
c5614fa4
AM
8077 toc_inf.global_toc_syms = TRUE;
8078 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8079 {
8080 asection *toc, *sec;
8081 Elf_Internal_Shdr *symtab_hdr;
8082 Elf_Internal_Sym *local_syms;
425b145b 8083 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8084 unsigned long *skip, *drop;
8085 unsigned char *used;
8086 unsigned char *keep, last, some_unused;
8087
854b41e7
AM
8088 if (!is_ppc64_elf (ibfd))
8089 continue;
8090
c5614fa4
AM
8091 toc = bfd_get_section_by_name (ibfd, ".toc");
8092 if (toc == NULL
92b7a70f 8093 || toc->size == 0
c5614fa4
AM
8094 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
8095 || elf_discarded_section (toc))
8096 continue;
8097
425b145b 8098 toc_relocs = NULL;
c5614fa4 8099 local_syms = NULL;
0ffa91dd 8100 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8101
8102 /* Look at sections dropped from the final link. */
8103 skip = NULL;
8104 relstart = NULL;
8105 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8106 {
8107 if (sec->reloc_count == 0
8108 || !elf_discarded_section (sec)
8109 || get_opd_info (sec)
8110 || (sec->flags & SEC_ALLOC) == 0
8111 || (sec->flags & SEC_DEBUGGING) != 0)
8112 continue;
8113
8114 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8115 if (relstart == NULL)
8116 goto error_ret;
8117
8118 /* Run through the relocs to see which toc entries might be
8119 unused. */
8120 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8121 {
8122 enum elf_ppc64_reloc_type r_type;
8123 unsigned long r_symndx;
8124 asection *sym_sec;
8125 struct elf_link_hash_entry *h;
8126 Elf_Internal_Sym *sym;
8127 bfd_vma val;
8128
8129 r_type = ELF64_R_TYPE (rel->r_info);
8130 switch (r_type)
8131 {
8132 default:
8133 continue;
8134
8135 case R_PPC64_TOC16:
8136 case R_PPC64_TOC16_LO:
8137 case R_PPC64_TOC16_HI:
8138 case R_PPC64_TOC16_HA:
8139 case R_PPC64_TOC16_DS:
8140 case R_PPC64_TOC16_LO_DS:
8141 break;
8142 }
8143
8144 r_symndx = ELF64_R_SYM (rel->r_info);
8145 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8146 r_symndx, ibfd))
8147 goto error_ret;
8148
8149 if (sym_sec != toc)
8150 continue;
8151
8152 if (h != NULL)
8153 val = h->root.u.def.value;
8154 else
8155 val = sym->st_value;
8156 val += rel->r_addend;
8157
8158 if (val >= toc->size)
8159 continue;
8160
8161 /* Anything in the toc ought to be aligned to 8 bytes.
8162 If not, don't mark as unused. */
8163 if (val & 7)
8164 continue;
8165
8166 if (skip == NULL)
8167 {
854b41e7 8168 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8169 if (skip == NULL)
8170 goto error_ret;
8171 }
8172
ba761f19 8173 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8174 }
8175
8176 if (elf_section_data (sec)->relocs != relstart)
8177 free (relstart);
8178 }
8179
ba761f19
AM
8180 /* For largetoc loads of address constants, we can convert
8181 . addis rx,2,addr@got@ha
8182 . ld ry,addr@got@l(rx)
8183 to
8184 . addis rx,2,addr@toc@ha
8185 . addi ry,rx,addr@toc@l
8186 when addr is within 2G of the toc pointer. This then means
8187 that the word storing "addr" in the toc is no longer needed. */
8188
8189 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8190 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8191 && toc->reloc_count != 0)
8192 {
8193 /* Read toc relocs. */
425b145b
AM
8194 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8195 info->keep_memory);
8196 if (toc_relocs == NULL)
ba761f19
AM
8197 goto error_ret;
8198
425b145b 8199 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8200 {
8201 enum elf_ppc64_reloc_type r_type;
8202 unsigned long r_symndx;
8203 asection *sym_sec;
8204 struct elf_link_hash_entry *h;
8205 Elf_Internal_Sym *sym;
8206 bfd_vma val, addr;
8207
8208 r_type = ELF64_R_TYPE (rel->r_info);
8209 if (r_type != R_PPC64_ADDR64)
8210 continue;
8211
8212 r_symndx = ELF64_R_SYM (rel->r_info);
8213 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8214 r_symndx, ibfd))
8215 goto error_ret;
8216
425b145b
AM
8217 if (sym_sec == NULL
8218 || elf_discarded_section (sym_sec))
8219 continue;
8220
726d3ab0 8221 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8222 continue;
8223
8224 if (h != NULL)
bddc25c9
AM
8225 {
8226 if (h->type == STT_GNU_IFUNC)
8227 continue;
8228 val = h->root.u.def.value;
8229 }
ba761f19 8230 else
bddc25c9
AM
8231 {
8232 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8233 continue;
8234 val = sym->st_value;
8235 }
ba761f19
AM
8236 val += rel->r_addend;
8237 val += sym_sec->output_section->vma + sym_sec->output_offset;
8238
8239 /* We don't yet know the exact toc pointer value, but we
8240 know it will be somewhere in the toc section. Don't
8241 optimize if the difference from any possible toc
8242 pointer is outside [ff..f80008000, 7fff7fff]. */
8243 addr = toc->output_section->vma + TOC_BASE_OFF;
8244 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8245 continue;
8246
8247 addr = toc->output_section->vma + toc->output_section->rawsize;
8248 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8249 continue;
8250
8251 if (skip == NULL)
8252 {
8253 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8254 if (skip == NULL)
8255 goto error_ret;
8256 }
8257
8258 skip[rel->r_offset >> 3]
425b145b 8259 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8260 }
ba761f19
AM
8261 }
8262
c5614fa4
AM
8263 if (skip == NULL)
8264 continue;
8265
8266 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8267 if (used == NULL)
8268 {
8269 error_ret:
8270 if (local_syms != NULL
8271 && symtab_hdr->contents != (unsigned char *) local_syms)
8272 free (local_syms);
8273 if (sec != NULL
8274 && relstart != NULL
8275 && elf_section_data (sec)->relocs != relstart)
8276 free (relstart);
425b145b
AM
8277 if (toc_relocs != NULL
8278 && elf_section_data (toc)->relocs != toc_relocs)
8279 free (toc_relocs);
c5614fa4
AM
8280 if (skip != NULL)
8281 free (skip);
8282 return FALSE;
8283 }
8284
30038c59
AM
8285 /* Now check all kept sections that might reference the toc.
8286 Check the toc itself last. */
8287 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8288 : ibfd->sections);
c5614fa4 8289 sec != NULL;
c5614fa4 8290 sec = (sec == toc ? NULL
c5614fa4 8291 : sec->next == NULL ? toc
30038c59 8292 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8293 : sec->next))
8294 {
8295 int repeat;
8296
8297 if (sec->reloc_count == 0
8298 || elf_discarded_section (sec)
8299 || get_opd_info (sec)
8300 || (sec->flags & SEC_ALLOC) == 0
8301 || (sec->flags & SEC_DEBUGGING) != 0)
8302 continue;
8303
854b41e7
AM
8304 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8305 info->keep_memory);
c5614fa4
AM
8306 if (relstart == NULL)
8307 goto error_ret;
8308
8309 /* Mark toc entries referenced as used. */
8310 repeat = 0;
8311 do
8312 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8313 {
8314 enum elf_ppc64_reloc_type r_type;
8315 unsigned long r_symndx;
8316 asection *sym_sec;
8317 struct elf_link_hash_entry *h;
8318 Elf_Internal_Sym *sym;
8319 bfd_vma val;
560c8763 8320 enum {no_check, check_lo, check_ha} insn_check;
c5614fa4
AM
8321
8322 r_type = ELF64_R_TYPE (rel->r_info);
98528052
AM
8323 switch (r_type)
8324 {
8325 default:
560c8763 8326 insn_check = no_check;
98528052
AM
8327 break;
8328
8329 case R_PPC64_GOT_TLSLD16_HA:
8330 case R_PPC64_GOT_TLSGD16_HA:
8331 case R_PPC64_GOT_TPREL16_HA:
8332 case R_PPC64_GOT_DTPREL16_HA:
8333 case R_PPC64_GOT16_HA:
8334 case R_PPC64_TOC16_HA:
560c8763
AM
8335 insn_check = check_ha;
8336 break;
98528052 8337
560c8763
AM
8338 case R_PPC64_GOT_TLSLD16_LO:
8339 case R_PPC64_GOT_TLSGD16_LO:
8340 case R_PPC64_GOT_TPREL16_LO_DS:
8341 case R_PPC64_GOT_DTPREL16_LO_DS:
8342 case R_PPC64_GOT16_LO:
8343 case R_PPC64_GOT16_LO_DS:
8344 case R_PPC64_TOC16_LO:
8345 case R_PPC64_TOC16_LO_DS:
8346 insn_check = check_lo;
98528052
AM
8347 break;
8348 }
8349
560c8763
AM
8350 if (insn_check != no_check)
8351 {
8352 bfd_vma off = rel->r_offset & ~3;
8353 unsigned char buf[4];
8354 unsigned int insn;
8355
8356 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8357 {
8358 free (used);
8359 goto error_ret;
8360 }
8361 insn = bfd_get_32 (ibfd, buf);
8362 if (insn_check == check_lo
8363 ? !ok_lo_toc_insn (insn)
8364 : ((insn & ((0x3f << 26) | 0x1f << 16))
8365 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8366 {
8367 char str[12];
8368
8369 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8370 sprintf (str, "%#08x", insn);
8371 info->callbacks->einfo
8372 (_("%P: %H: toc optimization is not supported for"
8373 " %s instruction.\n"),
8374 ibfd, sec, rel->r_offset & ~3, str);
8375 }
8376 }
8377
c5614fa4
AM
8378 switch (r_type)
8379 {
8380 case R_PPC64_TOC16:
8381 case R_PPC64_TOC16_LO:
8382 case R_PPC64_TOC16_HI:
8383 case R_PPC64_TOC16_HA:
8384 case R_PPC64_TOC16_DS:
8385 case R_PPC64_TOC16_LO_DS:
8386 /* In case we're taking addresses of toc entries. */
8387 case R_PPC64_ADDR64:
8388 break;
8389
8390 default:
8391 continue;
8392 }
8393
8394 r_symndx = ELF64_R_SYM (rel->r_info);
8395 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8396 r_symndx, ibfd))
8397 {
8398 free (used);
8399 goto error_ret;
8400 }
8401
8402 if (sym_sec != toc)
8403 continue;
8404
8405 if (h != NULL)
8406 val = h->root.u.def.value;
8407 else
8408 val = sym->st_value;
8409 val += rel->r_addend;
8410
8411 if (val >= toc->size)
8412 continue;
8413
ba761f19
AM
8414 if ((skip[val >> 3] & can_optimize) != 0)
8415 {
8416 bfd_vma off;
8417 unsigned char opc;
8418
8419 switch (r_type)
8420 {
8421 case R_PPC64_TOC16_HA:
8422 break;
8423
8424 case R_PPC64_TOC16_LO_DS:
8425 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8426 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
98528052
AM
8427 {
8428 free (used);
8429 goto error_ret;
8430 }
ba761f19
AM
8431 if ((opc & (0x3f << 2)) == (58u << 2))
8432 break;
8433 /* Fall thru */
8434
8435 default:
8436 /* Wrong sort of reloc, or not a ld. We may
8437 as well clear ref_from_discarded too. */
8438 skip[val >> 3] = 0;
8439 }
8440 }
8441
c5614fa4
AM
8442 /* For the toc section, we only mark as used if
8443 this entry itself isn't unused. */
8444 if (sec == toc
8445 && !used[val >> 3]
8446 && (used[rel->r_offset >> 3]
ba761f19 8447 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
c5614fa4
AM
8448 /* Do all the relocs again, to catch reference
8449 chains. */
8450 repeat = 1;
8451
8452 used[val >> 3] = 1;
8453 }
8454 while (repeat);
854b41e7
AM
8455
8456 if (elf_section_data (sec)->relocs != relstart)
8457 free (relstart);
c5614fa4
AM
8458 }
8459
8460 /* Merge the used and skip arrays. Assume that TOC
8461 doublewords not appearing as either used or unused belong
8462 to to an entry more than one doubleword in size. */
8463 for (drop = skip, keep = used, last = 0, some_unused = 0;
8464 drop < skip + (toc->size + 7) / 8;
8465 ++drop, ++keep)
8466 {
8467 if (*keep)
8468 {
ba761f19
AM
8469 *drop &= ~ref_from_discarded;
8470 if ((*drop & can_optimize) != 0)
8471 some_unused = 1;
c5614fa4
AM
8472 last = 0;
8473 }
b140b010 8474 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8475 {
8476 some_unused = 1;
ba761f19 8477 last = ref_from_discarded;
c5614fa4
AM
8478 }
8479 else
8480 *drop = last;
8481 }
8482
8483 free (used);
8484
8485 if (some_unused)
8486 {
8487 bfd_byte *contents, *src;
8488 unsigned long off;
d62b3684 8489 Elf_Internal_Sym *sym;
ba761f19 8490 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8491
8492 /* Shuffle the toc contents, and at the same time convert the
8493 skip array from booleans into offsets. */
8494 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8495 goto error_ret;
8496
8497 elf_section_data (toc)->this_hdr.contents = contents;
8498
8499 for (src = contents, off = 0, drop = skip;
8500 src < contents + toc->size;
8501 src += 8, ++drop)
8502 {
ba761f19
AM
8503 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8504 off += 8;
c5614fa4
AM
8505 else if (off != 0)
8506 {
8507 *drop = off;
8508 memcpy (src - off, src, 8);
8509 }
8510 }
854b41e7 8511 *drop = off;
c5614fa4
AM
8512 toc->rawsize = toc->size;
8513 toc->size = src - contents - off;
8514
ba761f19
AM
8515 /* Adjust addends for relocs against the toc section sym,
8516 and optimize any accesses we can. */
c5614fa4
AM
8517 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8518 {
8519 if (sec->reloc_count == 0
8520 || elf_discarded_section (sec))
8521 continue;
8522
8523 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8524 info->keep_memory);
c5614fa4
AM
8525 if (relstart == NULL)
8526 goto error_ret;
8527
8528 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8529 {
8530 enum elf_ppc64_reloc_type r_type;
8531 unsigned long r_symndx;
8532 asection *sym_sec;
8533 struct elf_link_hash_entry *h;
854b41e7 8534 bfd_vma val;
c5614fa4
AM
8535
8536 r_type = ELF64_R_TYPE (rel->r_info);
8537 switch (r_type)
8538 {
8539 default:
8540 continue;
8541
8542 case R_PPC64_TOC16:
8543 case R_PPC64_TOC16_LO:
8544 case R_PPC64_TOC16_HI:
8545 case R_PPC64_TOC16_HA:
8546 case R_PPC64_TOC16_DS:
8547 case R_PPC64_TOC16_LO_DS:
8548 case R_PPC64_ADDR64:
8549 break;
8550 }
8551
8552 r_symndx = ELF64_R_SYM (rel->r_info);
8553 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8554 r_symndx, ibfd))
8555 goto error_ret;
8556
ba761f19 8557 if (sym_sec != toc)
c5614fa4
AM
8558 continue;
8559
ba761f19
AM
8560 if (h != NULL)
8561 val = h->root.u.def.value;
8562 else
8563 {
8564 val = sym->st_value;
8565 if (val != 0)
8566 local_toc_syms = TRUE;
8567 }
8568
8569 val += rel->r_addend;
854b41e7
AM
8570
8571 if (val > toc->rawsize)
8572 val = toc->rawsize;
ba761f19
AM
8573 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8574 continue;
8575 else if ((skip[val >> 3] & can_optimize) != 0)
8576 {
8577 Elf_Internal_Rela *tocrel
425b145b 8578 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8579 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8580
8581 switch (r_type)
8582 {
8583 case R_PPC64_TOC16_HA:
8584 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8585 break;
8586
8587 case R_PPC64_TOC16_LO_DS:
8588 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8589 break;
8590
8591 default:
28942f62
AM
8592 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8593 ppc_howto_init ();
b140b010
AM
8594 info->callbacks->einfo
8595 (_("%P: %H: %s relocation references "
8596 "optimized away TOC entry\n"),
8597 ibfd, sec, rel->r_offset,
8598 ppc64_elf_howto_table[r_type]->name);
8599 bfd_set_error (bfd_error_bad_value);
8600 goto error_ret;
ba761f19
AM
8601 }
8602 rel->r_addend = tocrel->r_addend;
8603 elf_section_data (sec)->relocs = relstart;
8604 continue;
8605 }
8606
8607 if (h != NULL || sym->st_value != 0)
8608 continue;
854b41e7
AM
8609
8610 rel->r_addend -= skip[val >> 3];
8611 elf_section_data (sec)->relocs = relstart;
c5614fa4 8612 }
854b41e7
AM
8613
8614 if (elf_section_data (sec)->relocs != relstart)
8615 free (relstart);
c5614fa4
AM
8616 }
8617
8618 /* We shouldn't have local or global symbols defined in the TOC,
8619 but handle them anyway. */
df22d223
AM
8620 if (local_syms != NULL)
8621 for (sym = local_syms;
8622 sym < local_syms + symtab_hdr->sh_info;
8623 ++sym)
8624 if (sym->st_value != 0
8625 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8626 {
8627 unsigned long i;
854b41e7 8628
df22d223
AM
8629 if (sym->st_value > toc->rawsize)
8630 i = toc->rawsize >> 3;
8631 else
8632 i = sym->st_value >> 3;
854b41e7 8633
df22d223
AM
8634 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8635 {
8636 if (local_toc_syms)
8637 (*_bfd_error_handler)
8638 (_("%s defined on removed toc entry"),
8639 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8640 do
8641 ++i;
8642 while ((skip[i] & (ref_from_discarded | can_optimize)));
8643 sym->st_value = (bfd_vma) i << 3;
8644 }
d62b3684 8645
df22d223
AM
8646 sym->st_value -= skip[i];
8647 symtab_hdr->contents = (unsigned char *) local_syms;
8648 }
c5614fa4 8649
854b41e7 8650 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8651 if (toc_inf.global_toc_syms)
8652 {
8653 toc_inf.toc = toc;
8654 toc_inf.skip = skip;
8655 toc_inf.global_toc_syms = FALSE;
8656 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8657 &toc_inf);
8658 }
854b41e7
AM
8659
8660 if (toc->reloc_count != 0)
8661 {
d4730f92 8662 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8663 Elf_Internal_Rela *wrel;
8664 bfd_size_type sz;
8665
854b41e7 8666 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8667 if (toc_relocs == NULL)
8668 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8669 info->keep_memory);
8670 if (toc_relocs == NULL)
8671 goto error_ret;
8672
425b145b
AM
8673 wrel = toc_relocs;
8674 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8675 if ((skip[rel->r_offset >> 3]
8676 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8677 {
8678 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8679 wrel->r_info = rel->r_info;
8680 wrel->r_addend = rel->r_addend;
8681 ++wrel;
8682 }
8683 else if (!dec_dynrel_count (rel->r_info, toc, info,
8684 &local_syms, NULL, NULL))
8685 goto error_ret;
8686
425b145b
AM
8687 elf_section_data (toc)->relocs = toc_relocs;
8688 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8689 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8690 sz = rel_hdr->sh_entsize;
8691 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8692 }
c5614fa4 8693 }
28be611c
AM
8694 else if (toc_relocs != NULL
8695 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8696 free (toc_relocs);
c5614fa4
AM
8697
8698 if (local_syms != NULL
8699 && symtab_hdr->contents != (unsigned char *) local_syms)
8700 {
8701 if (!info->keep_memory)
8702 free (local_syms);
8703 else
8704 symtab_hdr->contents = (unsigned char *) local_syms;
8705 }
8706 free (skip);
8707 }
8708
8709 return TRUE;
8710}
8711
1bbe0902
AM
8712/* Return true iff input section I references the TOC using
8713 instructions limited to +/-32k offsets. */
8714
8715bfd_boolean
8716ppc64_elf_has_small_toc_reloc (asection *i)
8717{
8718 return (is_ppc64_elf (i->owner)
8719 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8720}
8721
927be08e
AM
8722/* Allocate space for one GOT entry. */
8723
8724static void
8725allocate_got (struct elf_link_hash_entry *h,
8726 struct bfd_link_info *info,
8727 struct got_entry *gent)
8728{
8729 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8730 bfd_boolean dyn;
8731 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8732 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8733 ? 16 : 8);
8734 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8735 ? 2 : 1) * sizeof (Elf64_External_Rela);
8736 asection *got = ppc64_elf_tdata (gent->owner)->got;
8737
8738 gent->got.offset = got->size;
8739 got->size += entsize;
8740
8741 dyn = htab->elf.dynamic_sections_created;
8742 if ((info->shared
8743 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8744 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8745 || h->root.type != bfd_link_hash_undefweak))
8746 {
8747 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8748 relgot->size += rentsize;
8749 }
8750 else if (h->type == STT_GNU_IFUNC)
8751 {
8752 asection *relgot = htab->reliplt;
8753 relgot->size += rentsize;
8754 htab->got_reli_size += rentsize;
8755 }
8756}
8757
7865406b
AM
8758/* This function merges got entries in the same toc group. */
8759
8760static void
8761merge_got_entries (struct got_entry **pent)
8762{
8763 struct got_entry *ent, *ent2;
8764
8765 for (ent = *pent; ent != NULL; ent = ent->next)
8766 if (!ent->is_indirect)
8767 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8768 if (!ent2->is_indirect
8769 && ent2->addend == ent->addend
8770 && ent2->tls_type == ent->tls_type
8771 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8772 {
8773 ent2->is_indirect = TRUE;
8774 ent2->got.ent = ent;
8775 }
8776}
8777
65f38f15
AM
8778/* Allocate space in .plt, .got and associated reloc sections for
8779 dynamic relocs. */
5bd4f169 8780
b34976b6 8781static bfd_boolean
4ce794b7 8782allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8783{
65f38f15
AM
8784 struct bfd_link_info *info;
8785 struct ppc_link_hash_table *htab;
5bd4f169 8786 asection *s;
65f38f15 8787 struct ppc_link_hash_entry *eh;
6061a67d 8788 struct elf_dyn_relocs *p;
0b8bcf0d 8789 struct got_entry **pgent, *gent;
5bd4f169 8790
e92d460e 8791 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8792 return TRUE;
5bd4f169 8793
65f38f15
AM
8794 info = (struct bfd_link_info *) inf;
8795 htab = ppc_hash_table (info);
4dfe6ac6
NC
8796 if (htab == NULL)
8797 return FALSE;
5bd4f169 8798
e054468f
AM
8799 if ((htab->elf.dynamic_sections_created
8800 && h->dynindx != -1
8801 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8802 || h->type == STT_GNU_IFUNC)
5bd4f169 8803 {
411e1bfb
AM
8804 struct plt_entry *pent;
8805 bfd_boolean doneone = FALSE;
8806 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8807 if (pent->plt.refcount > 0)
8808 {
25f23106
AM
8809 if (!htab->elf.dynamic_sections_created
8810 || h->dynindx == -1)
e054468f
AM
8811 {
8812 s = htab->iplt;
8813 pent->plt.offset = s->size;
8814 s->size += PLT_ENTRY_SIZE;
8815 s = htab->reliplt;
8816 }
8817 else
8818 {
8819 /* If this is the first .plt entry, make room for the special
8820 first entry. */
8821 s = htab->plt;
8822 if (s->size == 0)
8823 s->size += PLT_INITIAL_ENTRY_SIZE;
8824
8825 pent->plt.offset = s->size;
8826
8827 /* Make room for this entry. */
8828 s->size += PLT_ENTRY_SIZE;
8829
8830 /* Make room for the .glink code. */
8831 s = htab->glink;
8832 if (s->size == 0)
8833 s->size += GLINK_CALL_STUB_SIZE;
8834 /* We need bigger stubs past index 32767. */
8835 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8836 s->size += 4;
8837 s->size += 2*4;
8838
8839 /* We also need to make an entry in the .rela.plt section. */
8840 s = htab->relplt;
8841 }
eea6121a 8842 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
8843 doneone = TRUE;
8844 }
8845 else
8846 pent->plt.offset = (bfd_vma) -1;
8847 if (!doneone)
65f38f15 8848 {
411e1bfb 8849 h->plt.plist = NULL;
f5385ebf 8850 h->needs_plt = 0;
65f38f15
AM
8851 }
8852 }
8853 else
8854 {
411e1bfb 8855 h->plt.plist = NULL;
f5385ebf 8856 h->needs_plt = 0;
65f38f15
AM
8857 }
8858
951fd09b
AM
8859 eh = (struct ppc_link_hash_entry *) h;
8860 /* Run through the TLS GD got entries first if we're changing them
8861 to TPREL. */
e7b938ca 8862 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
8863 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8864 if (gent->got.refcount > 0
8865 && (gent->tls_type & TLS_GD) != 0)
8866 {
8867 /* This was a GD entry that has been converted to TPREL. If
8868 there happens to be a TPREL entry we can use that one. */
8869 struct got_entry *ent;
8870 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8871 if (ent->got.refcount > 0
8872 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8873 && ent->addend == gent->addend
8874 && ent->owner == gent->owner)
951fd09b
AM
8875 {
8876 gent->got.refcount = 0;
8877 break;
8878 }
8879
8880 /* If not, then we'll be using our own TPREL entry. */
8881 if (gent->got.refcount != 0)
8882 gent->tls_type = TLS_TLS | TLS_TPREL;
8883 }
8884
7865406b
AM
8885 /* Remove any list entry that won't generate a word in the GOT before
8886 we call merge_got_entries. Otherwise we risk merging to empty
8887 entries. */
0b8bcf0d
AM
8888 pgent = &h->got.glist;
8889 while ((gent = *pgent) != NULL)
411e1bfb 8890 if (gent->got.refcount > 0)
7865406b
AM
8891 {
8892 if ((gent->tls_type & TLS_LD) != 0
8893 && !h->def_dynamic)
8894 {
8895 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8896 *pgent = gent->next;
8897 }
8898 else
8899 pgent = &gent->next;
8900 }
8901 else
8902 *pgent = gent->next;
8903
8904 if (!htab->do_multi_toc)
8905 merge_got_entries (&h->got.glist);
8906
8907 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8908 if (!gent->is_indirect)
411e1bfb
AM
8909 {
8910 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
8911 Undefined weak syms won't yet be marked as dynamic,
8912 nor will all TLS symbols. */
411e1bfb 8913 if (h->dynindx == -1
b099ab9f 8914 && !h->forced_local
25f23106 8915 && h->type != STT_GNU_IFUNC
b099ab9f 8916 && htab->elf.dynamic_sections_created)
411e1bfb 8917 {
c152c796 8918 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
8919 return FALSE;
8920 }
65f38f15 8921
0c8d6e5c 8922 if (!is_ppc64_elf (gent->owner))
927be08e 8923 abort ();
0ffa91dd 8924
927be08e 8925 allocate_got (h, info, gent);
411e1bfb 8926 }
65f38f15 8927
b099ab9f 8928 if (eh->dyn_relocs == NULL
25f23106
AM
8929 || (!htab->elf.dynamic_sections_created
8930 && h->type != STT_GNU_IFUNC))
b34976b6 8931 return TRUE;
65f38f15
AM
8932
8933 /* In the shared -Bsymbolic case, discard space allocated for
8934 dynamic pc-relative relocs against symbols which turn out to be
8935 defined in regular objects. For the normal shared case, discard
8936 space for relocs that have become local due to symbol visibility
8937 changes. */
8938
8939 if (info->shared)
8940 {
9c7a29a3 8941 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 8942 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
8943 generated via assembly. We want calls to protected symbols to
8944 resolve directly to the function rather than going via the plt.
8945 If people want function pointer comparisons to work as expected
8946 then they should avoid writing weird assembly. */
09695f56 8947 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 8948 {
6061a67d 8949 struct elf_dyn_relocs **pp;
65f38f15
AM
8950
8951 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 8952 {
65f38f15
AM
8953 p->count -= p->pc_count;
8954 p->pc_count = 0;
8955 if (p->count == 0)
8956 *pp = p->next;
8957 else
8958 pp = &p->next;
5bd4f169 8959 }
65f38f15 8960 }
4e795f50
AM
8961
8962 /* Also discard relocs on undefined weak syms with non-default
8963 visibility. */
cab87ef9
AM
8964 if (eh->dyn_relocs != NULL
8965 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
8966 {
8967 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8968 eh->dyn_relocs = NULL;
8969
8970 /* Make sure this symbol is output as a dynamic symbol.
8971 Undefined weak syms won't yet be marked as dynamic. */
8972 else if (h->dynindx == -1
8973 && !h->forced_local)
8974 {
8975 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8976 return FALSE;
8977 }
8978 }
65f38f15 8979 }
25f23106
AM
8980 else if (h->type == STT_GNU_IFUNC)
8981 {
8982 if (!h->non_got_ref)
8983 eh->dyn_relocs = NULL;
8984 }
f4656909 8985 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
8986 {
8987 /* For the non-shared case, discard space for relocs against
8988 symbols which turn out to need copy relocs or are not
8989 dynamic. */
8990
f5385ebf 8991 if (!h->non_got_ref
f5385ebf 8992 && !h->def_regular)
65f38f15
AM
8993 {
8994 /* Make sure this symbol is output as a dynamic symbol.
8995 Undefined weak syms won't yet be marked as dynamic. */
8996 if (h->dynindx == -1
f5385ebf 8997 && !h->forced_local)
65f38f15 8998 {
c152c796 8999 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 9000 return FALSE;
65f38f15
AM
9001 }
9002
9003 /* If that succeeded, we know we'll be keeping all the
9004 relocs. */
9005 if (h->dynindx != -1)
9006 goto keep;
9007 }
9008
9009 eh->dyn_relocs = NULL;
9010
ec338859 9011 keep: ;
65f38f15
AM
9012 }
9013
9014 /* Finally, allocate space. */
9015 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9016 {
9017 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
9018 if (!htab->elf.dynamic_sections_created)
9019 sreloc = htab->reliplt;
eea6121a 9020 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9021 }
9022
b34976b6 9023 return TRUE;
65f38f15
AM
9024}
9025
9026/* Find any dynamic relocs that apply to read-only sections. */
9027
b34976b6 9028static bfd_boolean
4ce794b7 9029readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
9030{
9031 struct ppc_link_hash_entry *eh;
6061a67d 9032 struct elf_dyn_relocs *p;
65f38f15
AM
9033
9034 eh = (struct ppc_link_hash_entry *) h;
9035 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9036 {
9037 asection *s = p->sec->output_section;
9038
9039 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9040 {
4ce794b7 9041 struct bfd_link_info *info = inf;
65f38f15
AM
9042
9043 info->flags |= DF_TEXTREL;
9044
9045 /* Not an error, just cut short the traversal. */
b34976b6 9046 return FALSE;
65f38f15
AM
9047 }
9048 }
b34976b6 9049 return TRUE;
65f38f15
AM
9050}
9051
9052/* Set the sizes of the dynamic sections. */
9053
b34976b6 9054static bfd_boolean
4ce794b7
AM
9055ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
9056 struct bfd_link_info *info)
65f38f15
AM
9057{
9058 struct ppc_link_hash_table *htab;
9059 bfd *dynobj;
9060 asection *s;
b34976b6 9061 bfd_boolean relocs;
65f38f15 9062 bfd *ibfd;
7865406b 9063 struct got_entry *first_tlsld;
65f38f15
AM
9064
9065 htab = ppc_hash_table (info);
4dfe6ac6
NC
9066 if (htab == NULL)
9067 return FALSE;
9068
65f38f15
AM
9069 dynobj = htab->elf.dynobj;
9070 if (dynobj == NULL)
9071 abort ();
9072
9073 if (htab->elf.dynamic_sections_created)
9074 {
9075 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 9076 if (info->executable)
65f38f15
AM
9077 {
9078 s = bfd_get_section_by_name (dynobj, ".interp");
9079 if (s == NULL)
9080 abort ();
eea6121a 9081 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9082 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9083 }
9084 }
9085
9086 /* Set up .got offsets for local syms, and space for local dynamic
9087 relocs. */
9088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9089 {
411e1bfb
AM
9090 struct got_entry **lgot_ents;
9091 struct got_entry **end_lgot_ents;
e054468f
AM
9092 struct plt_entry **local_plt;
9093 struct plt_entry **end_local_plt;
f961d9dd 9094 unsigned char *lgot_masks;
65f38f15
AM
9095 bfd_size_type locsymcount;
9096 Elf_Internal_Shdr *symtab_hdr;
9097 asection *srel;
9098
0c8d6e5c 9099 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9100 continue;
9101
9102 for (s = ibfd->sections; s != NULL; s = s->next)
9103 {
6061a67d 9104 struct elf_dyn_relocs *p;
65f38f15 9105
6edfbbad 9106 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9107 {
ec338859
AM
9108 if (!bfd_is_abs_section (p->sec)
9109 && bfd_is_abs_section (p->sec->output_section))
9110 {
9111 /* Input section has been discarded, either because
9112 it is a copy of a linkonce section or due to
9113 linker script /DISCARD/, so we'll be discarding
9114 the relocs too. */
9115 }
248866a8 9116 else if (p->count != 0)
ec338859
AM
9117 {
9118 srel = elf_section_data (p->sec)->sreloc;
25f23106
AM
9119 if (!htab->elf.dynamic_sections_created)
9120 srel = htab->reliplt;
eea6121a 9121 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9122 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9123 info->flags |= DF_TEXTREL;
ec338859 9124 }
65f38f15
AM
9125 }
9126 }
9127
411e1bfb
AM
9128 lgot_ents = elf_local_got_ents (ibfd);
9129 if (!lgot_ents)
65f38f15
AM
9130 continue;
9131
0ffa91dd 9132 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9133 locsymcount = symtab_hdr->sh_info;
411e1bfb 9134 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9135 local_plt = (struct plt_entry **) end_lgot_ents;
9136 end_local_plt = local_plt + locsymcount;
f961d9dd 9137 lgot_masks = (unsigned char *) end_local_plt;
e717da7e
AM
9138 s = ppc64_elf_tdata (ibfd)->got;
9139 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 9140 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9141 {
0b8bcf0d 9142 struct got_entry **pent, *ent;
411e1bfb 9143
0b8bcf0d
AM
9144 pent = lgot_ents;
9145 while ((ent = *pent) != NULL)
411e1bfb
AM
9146 if (ent->got.refcount > 0)
9147 {
e7b938ca 9148 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9149 {
927be08e 9150 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9151 *pent = ent->next;
411e1bfb
AM
9152 }
9153 else
9154 {
25f23106 9155 unsigned int num = 1;
eea6121a 9156 ent->got.offset = s->size;
e7b938ca 9157 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
25f23106
AM
9158 num = 2;
9159 s->size += num * 8;
9160 if (info->shared)
9161 srel->size += num * sizeof (Elf64_External_Rela);
9162 else if ((*lgot_masks & PLT_IFUNC) != 0)
927be08e
AM
9163 {
9164 htab->reliplt->size
9165 += num * sizeof (Elf64_External_Rela);
9166 htab->got_reli_size
9167 += num * sizeof (Elf64_External_Rela);
9168 }
0b8bcf0d 9169 pent = &ent->next;
411e1bfb
AM
9170 }
9171 }
9172 else
0b8bcf0d 9173 *pent = ent->next;
65f38f15 9174 }
e054468f
AM
9175
9176 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9177 for (; local_plt < end_local_plt; ++local_plt)
9178 {
9179 struct plt_entry *ent;
9180
9181 for (ent = *local_plt; ent != NULL; ent = ent->next)
9182 if (ent->plt.refcount > 0)
9183 {
91d6fa6a 9184 s = htab->iplt;
e054468f
AM
9185 ent->plt.offset = s->size;
9186 s->size += PLT_ENTRY_SIZE;
9187
25f23106 9188 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9189 }
9190 else
9191 ent->plt.offset = (bfd_vma) -1;
9192 }
65f38f15
AM
9193 }
9194
9195 /* Allocate global sym .plt and .got entries, and space for global
9196 sym dynamic relocs. */
4ce794b7 9197 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 9198
7865406b 9199 first_tlsld = NULL;
102890f0
AM
9200 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9201 {
7865406b
AM
9202 struct got_entry *ent;
9203
0c8d6e5c 9204 if (!is_ppc64_elf (ibfd))
102890f0
AM
9205 continue;
9206
7865406b
AM
9207 ent = ppc64_tlsld_got (ibfd);
9208 if (ent->got.refcount > 0)
102890f0 9209 {
7865406b 9210 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9211 {
7865406b
AM
9212 ent->is_indirect = TRUE;
9213 ent->got.ent = first_tlsld;
9214 }
9215 else
9216 {
9217 if (first_tlsld == NULL)
9218 first_tlsld = ent;
9219 s = ppc64_elf_tdata (ibfd)->got;
9220 ent->got.offset = s->size;
9221 ent->owner = ibfd;
9222 s->size += 16;
9223 if (info->shared)
9224 {
9225 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9226 srel->size += sizeof (Elf64_External_Rela);
9227 }
102890f0
AM
9228 }
9229 }
9230 else
7865406b 9231 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9232 }
9233
65f38f15
AM
9234 /* We now have determined the sizes of the various dynamic sections.
9235 Allocate memory for them. */
b34976b6 9236 relocs = FALSE;
65f38f15
AM
9237 for (s = dynobj->sections; s != NULL; s = s->next)
9238 {
9239 if ((s->flags & SEC_LINKER_CREATED) == 0)
9240 continue;
9241
4ce794b7 9242 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9243 /* These haven't been allocated yet; don't strip. */
9244 continue;
e717da7e
AM
9245 else if (s == htab->got
9246 || s == htab->plt
e054468f 9247 || s == htab->iplt
c456f082
AM
9248 || s == htab->glink
9249 || s == htab->dynbss)
65f38f15
AM
9250 {
9251 /* Strip this section if we don't need it; see the
9252 comment below. */
5bd4f169 9253 }
58d180e8
AM
9254 else if (s == htab->glink_eh_frame)
9255 {
9256 if (!bfd_is_abs_section (s->output_section))
9257 /* Not sized yet. */
9258 continue;
9259 }
70cc837d 9260 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9261 {
c456f082 9262 if (s->size != 0)
5bd4f169 9263 {
4ce794b7 9264 if (s != htab->relplt)
b34976b6 9265 relocs = TRUE;
5bd4f169
AM
9266
9267 /* We use the reloc_count field as a counter if we need
9268 to copy relocs into the output file. */
9269 s->reloc_count = 0;
9270 }
9271 }
65f38f15 9272 else
5bd4f169
AM
9273 {
9274 /* It's not one of our sections, so don't allocate space. */
9275 continue;
9276 }
9277
eea6121a 9278 if (s->size == 0)
5bd4f169 9279 {
c456f082
AM
9280 /* If we don't need this section, strip it from the
9281 output file. This is mostly to handle .rela.bss and
9282 .rela.plt. We must create both sections in
9283 create_dynamic_sections, because they must be created
9284 before the linker maps input sections to output
9285 sections. The linker does that before
9286 adjust_dynamic_symbol is called, and it is that
9287 function which decides whether anything needs to go
9288 into these sections. */
8423293d 9289 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9290 continue;
9291 }
9292
c456f082 9293 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9294 continue;
9295
65f38f15
AM
9296 /* Allocate memory for the section contents. We use bfd_zalloc
9297 here in case unused entries are not reclaimed before the
9298 section's contents are written out. This should not happen,
411e1bfb
AM
9299 but this way if it does we get a R_PPC64_NONE reloc in .rela
9300 sections instead of garbage.
9301 We also rely on the section contents being zero when writing
9302 the GOT. */
eea6121a 9303 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9304 if (s->contents == NULL)
b34976b6 9305 return FALSE;
5bd4f169
AM
9306 }
9307
e717da7e
AM
9308 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9309 {
0c8d6e5c 9310 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9311 continue;
9312
e717da7e
AM
9313 s = ppc64_elf_tdata (ibfd)->got;
9314 if (s != NULL && s != htab->got)
9315 {
eea6121a 9316 if (s->size == 0)
8423293d 9317 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9318 else
9319 {
eea6121a 9320 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9321 if (s->contents == NULL)
9322 return FALSE;
9323 }
9324 }
9325 s = ppc64_elf_tdata (ibfd)->relgot;
9326 if (s != NULL)
9327 {
eea6121a 9328 if (s->size == 0)
8423293d 9329 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9330 else
9331 {
eea6121a 9332 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9333 if (s->contents == NULL)
9334 return FALSE;
9335 relocs = TRUE;
9336 s->reloc_count = 0;
9337 }
9338 }
9339 }
9340
e86ce104 9341 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9342 {
9343 /* Add some entries to the .dynamic section. We fill in the
9344 values later, in ppc64_elf_finish_dynamic_sections, but we
9345 must add the entries now so that we get the correct size for
9346 the .dynamic section. The DT_DEBUG entry is filled in by the
9347 dynamic linker and used by the debugger. */
dc810e39 9348#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9349 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9350
36af4a4e 9351 if (info->executable)
5bd4f169 9352 {
dc810e39 9353 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9354 return FALSE;
5bd4f169
AM
9355 }
9356
eea6121a 9357 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9358 {
dc810e39
AM
9359 if (!add_dynamic_entry (DT_PLTGOT, 0)
9360 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9361 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9362 || !add_dynamic_entry (DT_JMPREL, 0)
9363 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9364 return FALSE;
5bd4f169
AM
9365 }
9366
19397422
AM
9367 if (NO_OPD_RELOCS)
9368 {
9369 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9370 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9371 return FALSE;
19397422
AM
9372 }
9373
a7f2871e
AM
9374 if (!htab->no_tls_get_addr_opt
9375 && htab->tls_get_addr_fd != NULL
9376 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9377 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9378 return FALSE;
9379
5bd4f169
AM
9380 if (relocs)
9381 {
dc810e39
AM
9382 if (!add_dynamic_entry (DT_RELA, 0)
9383 || !add_dynamic_entry (DT_RELASZ, 0)
9384 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9385 return FALSE;
5bd4f169 9386
65f38f15
AM
9387 /* If any dynamic relocs apply to a read-only section,
9388 then we need a DT_TEXTREL entry. */
248866a8 9389 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9390 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9391
65f38f15 9392 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9393 {
65f38f15 9394 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9395 return FALSE;
5bd4f169 9396 }
5bd4f169 9397 }
5bd4f169 9398 }
65f38f15 9399#undef add_dynamic_entry
5bd4f169 9400
b34976b6 9401 return TRUE;
5bd4f169
AM
9402}
9403
721956f4 9404/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9405
4ce794b7
AM
9406static inline enum ppc_stub_type
9407ppc_type_of_stub (asection *input_sec,
9408 const Elf_Internal_Rela *rel,
9409 struct ppc_link_hash_entry **hash,
e054468f 9410 struct plt_entry **plt_ent,
4ce794b7 9411 bfd_vma destination)
5bd4f169 9412{
721956f4
AM
9413 struct ppc_link_hash_entry *h = *hash;
9414 bfd_vma location;
9415 bfd_vma branch_offset;
9416 bfd_vma max_branch_offset;
4ce794b7 9417 enum elf_ppc64_reloc_type r_type;
5bd4f169 9418
721956f4
AM
9419 if (h != NULL)
9420 {
e054468f 9421 struct plt_entry *ent;
7fe2b9a6 9422 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9423 if (h->oh != NULL
9424 && h->oh->is_func_descriptor)
7b8f6675
AM
9425 {
9426 fdh = ppc_follow_link (h->oh);
9427 *hash = fdh;
9428 }
8387904d 9429
e054468f
AM
9430 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9431 if (ent->addend == rel->r_addend
9432 && ent->plt.offset != (bfd_vma) -1)
9433 {
e054468f
AM
9434 *plt_ent = ent;
9435 return ppc_stub_plt_call;
9436 }
5bd4f169 9437
7fe2b9a6
AM
9438 /* Here, we know we don't have a plt entry. If we don't have a
9439 either a defined function descriptor or a defined entry symbol
9440 in a regular object file, then it is pointless trying to make
9441 any other type of stub. */
854b41e7
AM
9442 if (!is_static_defined (&fdh->elf)
9443 && !is_static_defined (&h->elf))
721956f4 9444 return ppc_stub_none;
5d1634d7 9445 }
e054468f
AM
9446 else if (elf_local_got_ents (input_sec->owner) != NULL)
9447 {
9448 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9449 struct plt_entry **local_plt = (struct plt_entry **)
9450 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9451 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9452
9453 if (local_plt[r_symndx] != NULL)
9454 {
9455 struct plt_entry *ent;
9456
9457 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9458 if (ent->addend == rel->r_addend
9459 && ent->plt.offset != (bfd_vma) -1)
9460 {
9461 *plt_ent = ent;
9462 return ppc_stub_plt_call;
9463 }
9464 }
9465 }
5d1634d7 9466
721956f4
AM
9467 /* Determine where the call point is. */
9468 location = (input_sec->output_offset
9469 + input_sec->output_section->vma
9470 + rel->r_offset);
5d1634d7 9471
721956f4
AM
9472 branch_offset = destination - location;
9473 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9474
721956f4
AM
9475 /* Determine if a long branch stub is needed. */
9476 max_branch_offset = 1 << 25;
4ce794b7 9477 if (r_type != R_PPC64_REL24)
721956f4 9478 max_branch_offset = 1 << 15;
5d1634d7 9479
721956f4
AM
9480 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9481 /* We need a stub. Figure out whether a long_branch or plt_branch
9482 is needed later. */
9483 return ppc_stub_long_branch;
5d1634d7 9484
721956f4 9485 return ppc_stub_none;
5d1634d7
AM
9486}
9487
794e51c0
AM
9488/* With power7 weakly ordered memory model, it is possible for ld.so
9489 to update a plt entry in one thread and have another thread see a
9490 stale zero toc entry. To avoid this we need some sort of acquire
9491 barrier in the call stub. One solution is to make the load of the
9492 toc word seem to appear to depend on the load of the function entry
9493 word. Another solution is to test for r2 being zero, and branch to
9494 the appropriate glink entry if so.
9495
9496 . fake dep barrier compare
9497 . ld 11,xxx(2) ld 11,xxx(2)
9498 . mtctr 11 mtctr 11
9499 . xor 11,11,11 ld 2,xxx+8(2)
9500 . add 2,2,11 cmpldi 2,0
9501 . ld 2,xxx+8(2) bnectr+
9502 . bctr b <glink_entry>
9503
9504 The solution involving the compare turns out to be faster, so
9505 that's what we use unless the branch won't reach. */
9506
9507#define ALWAYS_USE_FAKE_DEP 0
9508#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 9509
5d1634d7
AM
9510#define PPC_LO(v) ((v) & 0xffff)
9511#define PPC_HI(v) (((v) >> 16) & 0xffff)
9512#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9513
794e51c0
AM
9514static inline unsigned int
9515plt_stub_size (struct ppc_link_hash_table *htab,
9516 struct ppc_stub_hash_entry *stub_entry,
9517 bfd_vma off)
9518{
9519 unsigned size = PLT_CALL_STUB_SIZE;
9520
9521 if (!(ALWAYS_EMIT_R2SAVE
9522 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
9523 size -= 4;
9524 if (!htab->plt_static_chain)
9525 size -= 4;
9526 if (htab->plt_thread_safe)
9527 size += 8;
9528 if (PPC_HA (off) == 0)
9529 size -= 4;
9530 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9531 size += 4;
9532 if (stub_entry->h != NULL
9533 && (stub_entry->h == htab->tls_get_addr_fd
9534 || stub_entry->h == htab->tls_get_addr)
9535 && !htab->no_tls_get_addr_opt)
9536 size += 13 * 4;
9537 return size;
9538}
9539
9540/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9541 then return the padding needed to do so. */
9542static inline unsigned int
9543plt_stub_pad (struct ppc_link_hash_table *htab,
9544 struct ppc_stub_hash_entry *stub_entry,
9545 bfd_vma plt_off)
9546{
9547 int stub_align = 1 << htab->plt_stub_align;
9548 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9549 bfd_vma stub_off = stub_entry->stub_sec->size;
9550
9551 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9552 > (stub_size & -stub_align))
9553 return stub_align - (stub_off & (stub_align - 1));
9554 return 0;
9555}
9556
9557/* Build a .plt call stub. */
9558
9559static inline bfd_byte *
9560build_plt_stub (struct ppc_link_hash_table *htab,
9561 struct ppc_stub_hash_entry *stub_entry,
9562 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9563{
9564 bfd *obfd = htab->stub_bfd;
9565 bfd_boolean plt_static_chain = htab->plt_static_chain;
9566 bfd_boolean plt_thread_safe = htab->plt_thread_safe;
9567 bfd_boolean use_fake_dep = plt_thread_safe;
9568 bfd_vma cmp_branch_off = 0;
9569
9570 if (!ALWAYS_USE_FAKE_DEP
9571 && plt_thread_safe
9572 && !(stub_entry->h != NULL
9573 && (stub_entry->h == htab->tls_get_addr_fd
9574 || stub_entry->h == htab->tls_get_addr)
9575 && !htab->no_tls_get_addr_opt))
9576 {
9577 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9578 bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
9579 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
9580 bfd_vma to, from;
9581
9582 if (pltindex > 32767)
9583 glinkoff += (pltindex - 32767) * 4;
9584 to = (glinkoff
9585 + htab->glink->output_offset
9586 + htab->glink->output_section->vma);
9587 from = (p - stub_entry->stub_sec->contents
9588 + 4 * (ALWAYS_EMIT_R2SAVE
9589 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9590 + 4 * (PPC_HA (offset) != 0)
9591 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9592 != PPC_HA (offset))
9593 + 4 * (plt_static_chain != 0)
9594 + 20
9595 + stub_entry->stub_sec->output_offset
9596 + stub_entry->stub_sec->output_section->vma);
9597 cmp_branch_off = to - from;
9598 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9599 }
9600
ac2df442
AM
9601 if (PPC_HA (offset) != 0)
9602 {
176a0d42
AM
9603 if (r != NULL)
9604 {
794e51c0
AM
9605 if (ALWAYS_EMIT_R2SAVE
9606 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9607 r[0].r_offset += 4;
176a0d42 9608 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 9609 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
9610 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9611 r[1].r_addend = r[0].r_addend;
c7131b65 9612 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
9613 {
9614 r[2].r_offset = r[1].r_offset + 4;
9615 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9616 r[2].r_addend = r[0].r_addend;
9617 }
9618 else
9619 {
794e51c0 9620 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
176a0d42
AM
9621 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9622 r[2].r_addend = r[0].r_addend + 8;
c7131b65
AM
9623 if (plt_static_chain)
9624 {
9625 r[3].r_offset = r[2].r_offset + 4;
9626 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9627 r[3].r_addend = r[0].r_addend + 16;
9628 }
176a0d42
AM
9629 }
9630 }
794e51c0
AM
9631 if (ALWAYS_EMIT_R2SAVE
9632 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9633 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
3b421ab3 9634 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
ac2df442 9635 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9df0ef5f 9636 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9637 {
9638 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9639 offset = 0;
9640 }
9641 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
794e51c0
AM
9642 if (use_fake_dep)
9643 {
9644 bfd_put_32 (obfd, XOR_R11_R11_R11, p), p += 4;
9645 bfd_put_32 (obfd, ADD_R12_R12_R11, p), p += 4;
9646 }
ac2df442 9647 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9df0ef5f
AM
9648 if (plt_static_chain)
9649 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
9650 }
9651 else
9652 {
176a0d42
AM
9653 if (r != NULL)
9654 {
794e51c0
AM
9655 if (ALWAYS_EMIT_R2SAVE
9656 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9657 r[0].r_offset += 4;
176a0d42 9658 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65 9659 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
9660 {
9661 r[1].r_offset = r[0].r_offset + 4;
9662 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9663 r[1].r_addend = r[0].r_addend;
9664 }
9665 else
9666 {
794e51c0 9667 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
176a0d42 9668 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65
AM
9669 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9670 if (plt_static_chain)
9671 {
9672 r[2].r_offset = r[1].r_offset + 4;
9673 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9674 r[2].r_addend = r[0].r_addend + 8;
9675 }
176a0d42
AM
9676 }
9677 }
794e51c0
AM
9678 if (ALWAYS_EMIT_R2SAVE
9679 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9680 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
ac2df442 9681 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9df0ef5f 9682 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9683 {
9684 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9685 offset = 0;
9686 }
9687 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
794e51c0
AM
9688 if (use_fake_dep)
9689 {
9690 bfd_put_32 (obfd, XOR_R11_R11_R11, p), p += 4;
9691 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
9692 }
9df0ef5f
AM
9693 if (plt_static_chain)
9694 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
ac2df442 9695 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
ac2df442 9696 }
794e51c0
AM
9697 if (plt_thread_safe && !use_fake_dep)
9698 {
9699 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
9700 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
9701 bfd_put_32 (obfd, B_DOT + cmp_branch_off, p), p += 4;
9702 }
9703 else
9704 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
9705 return p;
9706}
9707
a7f2871e
AM
9708/* Build a special .plt call stub for __tls_get_addr. */
9709
9710#define LD_R11_0R3 0xe9630000
9711#define LD_R12_0R3 0xe9830000
9712#define MR_R0_R3 0x7c601b78
9713#define CMPDI_R11_0 0x2c2b0000
9714#define ADD_R3_R12_R13 0x7c6c6a14
9715#define BEQLR 0x4d820020
9716#define MR_R3_R0 0x7c030378
9717#define MFLR_R11 0x7d6802a6
9718#define STD_R11_0R1 0xf9610000
9719#define BCTRL 0x4e800421
9720#define LD_R11_0R1 0xe9610000
9721#define LD_R2_0R1 0xe8410000
9722#define MTLR_R11 0x7d6803a6
9723
9724static inline bfd_byte *
794e51c0
AM
9725build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9726 struct ppc_stub_hash_entry *stub_entry,
9727 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 9728{
794e51c0
AM
9729 bfd *obfd = htab->stub_bfd;
9730
a7f2871e
AM
9731 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9732 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9733 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9734 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9735 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9736 bfd_put_32 (obfd, BEQLR, p), p += 4;
9737 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9738 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9739 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9740
9741 if (r != NULL)
9742 r[0].r_offset += 9 * 4;
794e51c0 9743 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
9744 bfd_put_32 (obfd, BCTRL, p - 4);
9745
9746 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9747 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9748 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9749 bfd_put_32 (obfd, BLR, p), p += 4;
9750
9751 return p;
9752}
9753
176a0d42
AM
9754static Elf_Internal_Rela *
9755get_relocs (asection *sec, int count)
9756{
9757 Elf_Internal_Rela *relocs;
9758 struct bfd_elf_section_data *elfsec_data;
9759
9760 elfsec_data = elf_section_data (sec);
9761 relocs = elfsec_data->relocs;
9762 if (relocs == NULL)
9763 {
9764 bfd_size_type relsize;
9765 relsize = sec->reloc_count * sizeof (*relocs);
9766 relocs = bfd_alloc (sec->owner, relsize);
9767 if (relocs == NULL)
9768 return NULL;
9769 elfsec_data->relocs = relocs;
d4730f92
BS
9770 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9771 sizeof (Elf_Internal_Shdr));
9772 if (elfsec_data->rela.hdr == NULL)
9773 return NULL;
9774 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9775 * sizeof (Elf64_External_Rela));
9776 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9777 sec->reloc_count = 0;
9778 }
9779 relocs += sec->reloc_count;
9780 sec->reloc_count += count;
9781 return relocs;
9782}
9783
aa374f67 9784static bfd_vma
25f53a85 9785get_r2off (struct bfd_link_info *info,
aa374f67
AM
9786 struct ppc_stub_hash_entry *stub_entry)
9787{
25f53a85 9788 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
9789 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9790
9791 if (r2off == 0)
9792 {
9793 /* Support linking -R objects. Get the toc pointer from the
9794 opd entry. */
9795 char buf[8];
9796 asection *opd = stub_entry->h->elf.root.u.def.section;
9797 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9798
9799 if (strcmp (opd->name, ".opd") != 0
9800 || opd->reloc_count != 0)
9801 {
8de848d8 9802 info->callbacks->einfo (_("%P: cannot find opd entry toc for %s\n"),
25f53a85 9803 stub_entry->h->elf.root.root.string);
aa374f67
AM
9804 bfd_set_error (bfd_error_bad_value);
9805 return 0;
9806 }
9807 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9808 return 0;
9809 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 9810 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
9811 }
9812 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9813 return r2off;
9814}
9815
b34976b6 9816static bfd_boolean
4ce794b7 9817ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 9818{
721956f4
AM
9819 struct ppc_stub_hash_entry *stub_entry;
9820 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
9821 struct bfd_link_info *info;
9822 struct ppc_link_hash_table *htab;
721956f4
AM
9823 bfd_byte *loc;
9824 bfd_byte *p;
ee75fd95 9825 bfd_vma dest, off;
721956f4 9826 int size;
176a0d42 9827 Elf_Internal_Rela *r;
e054468f 9828 asection *plt;
5d1634d7 9829
721956f4
AM
9830 /* Massage our args to the form they really have. */
9831 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 9832 info = in_arg;
5d1634d7 9833
5d1634d7 9834 htab = ppc_hash_table (info);
4dfe6ac6
NC
9835 if (htab == NULL)
9836 return FALSE;
5d1634d7 9837
721956f4 9838 /* Make a note of the offset within the stubs for this entry. */
eea6121a 9839 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 9840 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 9841
4ce794b7 9842 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 9843 switch (stub_entry->stub_type)
5d1634d7 9844 {
721956f4 9845 case ppc_stub_long_branch:
ad8e1ba5 9846 case ppc_stub_long_branch_r2off:
721956f4 9847 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
9848 off = dest = (stub_entry->target_value
9849 + stub_entry->target_section->output_offset
9850 + stub_entry->target_section->output_section->vma);
5d1634d7 9851
721956f4
AM
9852 /* And this is where we are coming from. */
9853 off -= (stub_entry->stub_offset
97b639ba
AM
9854 + stub_entry->stub_sec->output_offset
9855 + stub_entry->stub_sec->output_section->vma);
e86ce104 9856
ac2df442
AM
9857 size = 4;
9858 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 9859 {
25f53a85 9860 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 9861
aa374f67
AM
9862 if (r2off == 0)
9863 {
9864 htab->stub_error = TRUE;
9865 return FALSE;
9866 }
97b639ba 9867 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9868 loc += 4;
ac2df442
AM
9869 size = 12;
9870 if (PPC_HA (r2off) != 0)
9871 {
9872 size = 16;
9873 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9874 loc += 4;
9875 }
97b639ba 9876 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 9877 loc += 4;
ac2df442 9878 off -= size - 4;
ad8e1ba5 9879 }
97b639ba 9880 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 9881
5c3dead3
AM
9882 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9883 {
8de848d8 9884 info->callbacks->einfo (_("%P: long branch stub `%s' offset overflow\n"),
25f53a85 9885 stub_entry->root.string);
5c3dead3
AM
9886 htab->stub_error = TRUE;
9887 return FALSE;
9888 }
ee75fd95
AM
9889
9890 if (info->emitrelocations)
9891 {
176a0d42
AM
9892 r = get_relocs (stub_entry->stub_sec, 1);
9893 if (r == NULL)
9894 return FALSE;
ee75fd95
AM
9895 r->r_offset = loc - stub_entry->stub_sec->contents;
9896 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9897 r->r_addend = dest;
9898 if (stub_entry->h != NULL)
9899 {
9900 struct elf_link_hash_entry **hashes;
9901 unsigned long symndx;
9902 struct ppc_link_hash_entry *h;
9903
9904 hashes = elf_sym_hashes (htab->stub_bfd);
9905 if (hashes == NULL)
9906 {
9907 bfd_size_type hsize;
9908
9909 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9910 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9911 if (hashes == NULL)
9912 return FALSE;
9913 elf_sym_hashes (htab->stub_bfd) = hashes;
9914 htab->stub_globals = 1;
9915 }
9916 symndx = htab->stub_globals++;
9917 h = stub_entry->h;
9918 hashes[symndx] = &h->elf;
9919 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9920 if (h->oh != NULL && h->oh->is_func)
b31867b6 9921 h = ppc_follow_link (h->oh);
ee75fd95
AM
9922 if (h->elf.root.u.def.section != stub_entry->target_section)
9923 /* H is an opd symbol. The addend must be zero. */
9924 r->r_addend = 0;
9925 else
9926 {
9927 off = (h->elf.root.u.def.value
9928 + h->elf.root.u.def.section->output_offset
9929 + h->elf.root.u.def.section->output_section->vma);
9930 r->r_addend -= off;
9931 }
9932 }
9933 }
721956f4 9934 break;
e86ce104 9935
721956f4 9936 case ppc_stub_plt_branch:
ad8e1ba5 9937 case ppc_stub_plt_branch_r2off:
721956f4
AM
9938 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9939 stub_entry->root.string + 9,
b34976b6 9940 FALSE, FALSE);
721956f4
AM
9941 if (br_entry == NULL)
9942 {
8de848d8 9943 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 9944 stub_entry->root.string);
b34976b6
AM
9945 htab->stub_error = TRUE;
9946 return FALSE;
721956f4
AM
9947 }
9948
176a0d42
AM
9949 dest = (stub_entry->target_value
9950 + stub_entry->target_section->output_offset
9951 + stub_entry->target_section->output_section->vma);
721956f4 9952
176a0d42 9953 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 9954 htab->brlt->contents + br_entry->offset);
721956f4 9955
f94498ff 9956 if (br_entry->iter == htab->stub_iteration)
721956f4 9957 {
f94498ff 9958 br_entry->iter = 0;
84f5d08e 9959
f94498ff 9960 if (htab->relbrlt != NULL)
84f5d08e 9961 {
f94498ff
AM
9962 /* Create a reloc for the branch lookup table entry. */
9963 Elf_Internal_Rela rela;
9964 bfd_byte *rl;
9965
9966 rela.r_offset = (br_entry->offset
9967 + htab->brlt->output_offset
9968 + htab->brlt->output_section->vma);
9969 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9970 rela.r_addend = dest;
f94498ff
AM
9971
9972 rl = htab->relbrlt->contents;
9973 rl += (htab->relbrlt->reloc_count++
9974 * sizeof (Elf64_External_Rela));
9975 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9976 }
9977 else if (info->emitrelocations)
9978 {
176a0d42
AM
9979 r = get_relocs (htab->brlt, 1);
9980 if (r == NULL)
9981 return FALSE;
9982 /* brlt, being SEC_LINKER_CREATED does not go through the
9983 normal reloc processing. Symbols and offsets are not
9984 translated from input file to output file form, so
9985 set up the offset per the output file. */
f94498ff
AM
9986 r->r_offset = (br_entry->offset
9987 + htab->brlt->output_offset
9988 + htab->brlt->output_section->vma);
9989 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9990 r->r_addend = dest;
84f5d08e 9991 }
84f5d08e 9992 }
721956f4 9993
176a0d42
AM
9994 dest = (br_entry->offset
9995 + htab->brlt->output_offset
9996 + htab->brlt->output_section->vma);
9997
9998 off = (dest
4ce794b7 9999 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 10000 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10001
ad8e1ba5 10002 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10003 {
25f53a85 10004 info->callbacks->einfo
8de848d8 10005 (_("%P: linkage table error against `%s'\n"),
721956f4 10006 stub_entry->root.string);
5d1634d7 10007 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10008 htab->stub_error = TRUE;
10009 return FALSE;
5d1634d7 10010 }
41bd81ab 10011
176a0d42
AM
10012 if (info->emitrelocations)
10013 {
10014 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10015 if (r == NULL)
10016 return FALSE;
10017 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10018 if (bfd_big_endian (info->output_bfd))
10019 r[0].r_offset += 2;
176a0d42
AM
10020 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10021 r[0].r_offset += 4;
10022 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10023 r[0].r_addend = dest;
10024 if (PPC_HA (off) != 0)
10025 {
10026 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10027 r[1].r_offset = r[0].r_offset + 4;
10028 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10029 r[1].r_addend = r[0].r_addend;
10030 }
10031 }
10032
ad8e1ba5
AM
10033 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10034 {
176a0d42 10035 if (PPC_HA (off) != 0)
ac2df442
AM
10036 {
10037 size = 16;
176a0d42 10038 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10039 loc += 4;
176a0d42 10040 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
10041 }
10042 else
10043 {
10044 size = 12;
176a0d42 10045 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442 10046 }
ad8e1ba5
AM
10047 }
10048 else
10049 {
25f53a85 10050 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
10051
10052 if (r2off == 0)
10053 {
10054 htab->stub_error = TRUE;
10055 return FALSE;
10056 }
ad8e1ba5 10057
97b639ba 10058 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 10059 loc += 4;
ac2df442 10060 size = 20;
176a0d42 10061 if (PPC_HA (off) != 0)
ac2df442
AM
10062 {
10063 size += 4;
176a0d42 10064 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 10065 loc += 4;
176a0d42 10066 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
10067 loc += 4;
10068 }
10069 else
10070 {
176a0d42 10071 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442
AM
10072 loc += 4;
10073 }
10074
10075 if (PPC_HA (r2off) != 0)
10076 {
10077 size += 4;
10078 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10079 loc += 4;
10080 }
97b639ba 10081 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
10082 }
10083 loc += 4;
97b639ba 10084 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 10085 loc += 4;
97b639ba 10086 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 10087 break;
5d1634d7 10088
721956f4 10089 case ppc_stub_plt_call:
794e51c0 10090 case ppc_stub_plt_call_r2save:
e054468f 10091 if (stub_entry->h != NULL
b31867b6
AM
10092 && stub_entry->h->is_func_descriptor
10093 && stub_entry->h->oh != NULL)
c862ae31 10094 {
b31867b6
AM
10095 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10096
10097 /* If the old-ABI "dot-symbol" is undefined make it weak so
10098 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10099 FIXME: We used to define the symbol on one of the call
10100 stubs instead, which is why we test symbol section id
10101 against htab->top_id in various places. Likely all
10102 these checks could now disappear. */
10103 if (fh->elf.root.type == bfd_link_hash_undefined)
10104 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
10105 /* Stop undo_symbol_twiddle changing it back to undefined. */
10106 fh->was_undefined = 0;
c862ae31
AM
10107 }
10108
721956f4 10109 /* Now build the stub. */
e054468f 10110 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 10111 if (dest >= (bfd_vma) -2)
721956f4
AM
10112 abort ();
10113
e054468f 10114 plt = htab->plt;
25f23106
AM
10115 if (!htab->elf.dynamic_sections_created
10116 || stub_entry->h == NULL
10117 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10118 plt = htab->iplt;
10119
10120 dest += plt->output_offset + plt->output_section->vma;
10121
10122 if (stub_entry->h == NULL
10123 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10124 {
10125 Elf_Internal_Rela rela;
10126 bfd_byte *rl;
10127
10128 rela.r_offset = dest;
25f23106 10129 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
10130 rela.r_addend = (stub_entry->target_value
10131 + stub_entry->target_section->output_offset
10132 + stub_entry->target_section->output_section->vma);
10133
25f23106
AM
10134 rl = (htab->reliplt->contents
10135 + (htab->reliplt->reloc_count++
10136 * sizeof (Elf64_External_Rela)));
10137 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
10138 stub_entry->plt_ent->plt.offset |= 1;
10139 }
176a0d42
AM
10140
10141 off = (dest
e054468f 10142 - elf_gp (plt->output_section->owner)
176a0d42 10143 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10144
ad8e1ba5 10145 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10146 {
25f53a85 10147 info->callbacks->einfo
8de848d8 10148 (_("%P: linkage table error against `%s'\n"),
e054468f
AM
10149 stub_entry->h != NULL
10150 ? stub_entry->h->elf.root.root.string
10151 : "<local sym>");
721956f4 10152 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10153 htab->stub_error = TRUE;
10154 return FALSE;
721956f4
AM
10155 }
10156
794e51c0
AM
10157 if (htab->plt_stub_align != 0)
10158 {
10159 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10160
10161 stub_entry->stub_sec->size += pad;
10162 stub_entry->stub_offset = stub_entry->stub_sec->size;
10163 loc += pad;
10164 }
10165
176a0d42
AM
10166 r = NULL;
10167 if (info->emitrelocations)
10168 {
10169 r = get_relocs (stub_entry->stub_sec,
c7131b65
AM
10170 (2
10171 + (PPC_HA (off) != 0)
10172 + (htab->plt_static_chain
10173 && PPC_HA (off + 16) == PPC_HA (off))));
176a0d42
AM
10174 if (r == NULL)
10175 return FALSE;
10176 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10177 if (bfd_big_endian (info->output_bfd))
10178 r[0].r_offset += 2;
176a0d42
AM
10179 r[0].r_addend = dest;
10180 }
a7f2871e
AM
10181 if (stub_entry->h != NULL
10182 && (stub_entry->h == htab->tls_get_addr_fd
10183 || stub_entry->h == htab->tls_get_addr)
10184 && !htab->no_tls_get_addr_opt)
794e51c0 10185 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10186 else
794e51c0 10187 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10188 size = p - loc;
10189 break;
10190
10191 default:
10192 BFD_FAIL ();
b34976b6 10193 return FALSE;
721956f4
AM
10194 }
10195
eea6121a 10196 stub_entry->stub_sec->size += size;
97b639ba 10197
ee75fd95 10198 if (htab->emit_stub_syms)
97b639ba
AM
10199 {
10200 struct elf_link_hash_entry *h;
ee75fd95
AM
10201 size_t len1, len2;
10202 char *name;
10203 const char *const stub_str[] = { "long_branch",
10204 "long_branch_r2off",
10205 "plt_branch",
10206 "plt_branch_r2off",
794e51c0 10207 "plt_call",
ee75fd95
AM
10208 "plt_call" };
10209
10210 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10211 len2 = strlen (stub_entry->root.string);
10212 name = bfd_malloc (len1 + len2 + 2);
10213 if (name == NULL)
10214 return FALSE;
10215 memcpy (name, stub_entry->root.string, 9);
10216 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10217 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10218 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10219 if (h == NULL)
10220 return FALSE;
10221 if (h->root.type == bfd_link_hash_new)
10222 {
10223 h->root.type = bfd_link_hash_defined;
10224 h->root.u.def.section = stub_entry->stub_sec;
10225 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10226 h->ref_regular = 1;
10227 h->def_regular = 1;
10228 h->ref_regular_nonweak = 1;
10229 h->forced_local = 1;
10230 h->non_elf = 0;
97b639ba
AM
10231 }
10232 }
10233
b34976b6 10234 return TRUE;
721956f4
AM
10235}
10236
10237/* As above, but don't actually build the stub. Just bump offset so
10238 we know stub section sizes, and select plt_branch stubs where
10239 long_branch stubs won't do. */
10240
b34976b6 10241static bfd_boolean
4ce794b7 10242ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10243{
10244 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10245 struct bfd_link_info *info;
721956f4
AM
10246 struct ppc_link_hash_table *htab;
10247 bfd_vma off;
10248 int size;
10249
10250 /* Massage our args to the form they really have. */
10251 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10252 info = in_arg;
10253
10254 htab = ppc_hash_table (info);
4dfe6ac6
NC
10255 if (htab == NULL)
10256 return FALSE;
721956f4 10257
794e51c0
AM
10258 if (stub_entry->stub_type == ppc_stub_plt_call
10259 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 10260 {
e054468f
AM
10261 asection *plt;
10262 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 10263 if (off >= (bfd_vma) -2)
411e1bfb 10264 abort ();
e054468f 10265 plt = htab->plt;
25f23106
AM
10266 if (!htab->elf.dynamic_sections_created
10267 || stub_entry->h == NULL
10268 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10269 plt = htab->iplt;
10270 off += (plt->output_offset
10271 + plt->output_section->vma
10272 - elf_gp (plt->output_section->owner)
ad8e1ba5 10273 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10274
794e51c0
AM
10275 size = plt_stub_size (htab, stub_entry, off);
10276 if (htab->plt_stub_align)
10277 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
10278 if (info->emitrelocations)
10279 {
10280 stub_entry->stub_sec->reloc_count
c7131b65
AM
10281 += (2
10282 + (PPC_HA (off) != 0)
10283 + (htab->plt_static_chain
10284 && PPC_HA (off + 16) == PPC_HA (off)));
176a0d42
AM
10285 stub_entry->stub_sec->flags |= SEC_RELOC;
10286 }
721956f4
AM
10287 }
10288 else
10289 {
ad8e1ba5
AM
10290 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10291 variants. */
ac2df442
AM
10292 bfd_vma r2off = 0;
10293
721956f4
AM
10294 off = (stub_entry->target_value
10295 + stub_entry->target_section->output_offset
10296 + stub_entry->target_section->output_section->vma);
eea6121a 10297 off -= (stub_entry->stub_sec->size
721956f4
AM
10298 + stub_entry->stub_sec->output_offset
10299 + stub_entry->stub_sec->output_section->vma);
10300
ad8e1ba5
AM
10301 /* Reset the stub type from the plt variant in case we now
10302 can reach with a shorter stub. */
10303 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10304 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10305
10306 size = 4;
10307 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10308 {
25f53a85 10309 r2off = get_r2off (info, stub_entry);
aa374f67
AM
10310 if (r2off == 0)
10311 {
10312 htab->stub_error = TRUE;
10313 return FALSE;
10314 }
ac2df442
AM
10315 size = 12;
10316 if (PPC_HA (r2off) != 0)
10317 size = 16;
10318 off -= size - 4;
ad8e1ba5
AM
10319 }
10320
10321 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
10322 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10323 {
10324 struct ppc_branch_hash_entry *br_entry;
10325
10326 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10327 stub_entry->root.string + 9,
b34976b6 10328 TRUE, FALSE);
721956f4
AM
10329 if (br_entry == NULL)
10330 {
8de848d8 10331 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 10332 stub_entry->root.string);
b34976b6
AM
10333 htab->stub_error = TRUE;
10334 return FALSE;
721956f4
AM
10335 }
10336
10337 if (br_entry->iter != htab->stub_iteration)
10338 {
10339 br_entry->iter = htab->stub_iteration;
eea6121a
AM
10340 br_entry->offset = htab->brlt->size;
10341 htab->brlt->size += 8;
63bc6f6c 10342
ee75fd95 10343 if (htab->relbrlt != NULL)
eea6121a 10344 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
10345 else if (info->emitrelocations)
10346 {
10347 htab->brlt->reloc_count += 1;
10348 htab->brlt->flags |= SEC_RELOC;
10349 }
721956f4 10350 }
ad8e1ba5
AM
10351
10352 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
10353 off = (br_entry->offset
10354 + htab->brlt->output_offset
10355 + htab->brlt->output_section->vma
10356 - elf_gp (htab->brlt->output_section->owner)
10357 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10358
176a0d42
AM
10359 if (info->emitrelocations)
10360 {
10361 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10362 stub_entry->stub_sec->flags |= SEC_RELOC;
10363 }
10364
ac2df442
AM
10365 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10366 {
10367 size = 12;
176a0d42 10368 if (PPC_HA (off) != 0)
ac2df442
AM
10369 size = 16;
10370 }
10371 else
10372 {
10373 size = 20;
176a0d42 10374 if (PPC_HA (off) != 0)
ac2df442
AM
10375 size += 4;
10376
10377 if (PPC_HA (r2off) != 0)
10378 size += 4;
10379 }
721956f4 10380 }
84f5d08e
AM
10381 else if (info->emitrelocations)
10382 {
10383 stub_entry->stub_sec->reloc_count += 1;
10384 stub_entry->stub_sec->flags |= SEC_RELOC;
10385 }
721956f4
AM
10386 }
10387
eea6121a 10388 stub_entry->stub_sec->size += size;
b34976b6 10389 return TRUE;
721956f4
AM
10390}
10391
10392/* Set up various things so that we can make a list of input sections
10393 for each output section included in the link. Returns -1 on error,
cedb70c5 10394 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10395
10396int
927be08e
AM
10397ppc64_elf_setup_section_lists
10398 (struct bfd_link_info *info,
10399 asection *(*add_stub_section) (const char *, asection *),
10400 void (*layout_sections_again) (void))
721956f4
AM
10401{
10402 bfd *input_bfd;
734b6cf9 10403 int top_id, top_index, id;
721956f4 10404 asection *section;
734b6cf9 10405 asection **input_list;
721956f4
AM
10406 bfd_size_type amt;
10407 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10408
4dfe6ac6
NC
10409 if (htab == NULL)
10410 return -1;
927be08e
AM
10411 /* Stash our params away. */
10412 htab->add_stub_section = add_stub_section;
10413 htab->layout_sections_again = layout_sections_again;
4c52953f 10414
4ce794b7 10415 if (htab->brlt == NULL)
721956f4
AM
10416 return 0;
10417
1e2f5b6e 10418 /* Find the top input section id. */
3d6f9012 10419 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10420 input_bfd != NULL;
10421 input_bfd = input_bfd->link_next)
10422 {
721956f4
AM
10423 for (section = input_bfd->sections;
10424 section != NULL;
10425 section = section->next)
10426 {
10427 if (top_id < section->id)
10428 top_id = section->id;
10429 }
10430 }
721956f4 10431
8f3bab57 10432 htab->top_id = top_id;
721956f4 10433 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10434 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10435 if (htab->stub_group == NULL)
10436 return -1;
10437
3d6f9012
AM
10438 /* Set toc_off for com, und, abs and ind sections. */
10439 for (id = 0; id < 3; id++)
10440 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10441
734b6cf9
AM
10442 /* We can't use output_bfd->section_count here to find the top output
10443 section index as some sections may have been removed, and
8423293d 10444 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10445 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10446 section != NULL;
10447 section = section->next)
10448 {
10449 if (top_index < section->index)
10450 top_index = section->index;
10451 }
10452
10453 htab->top_index = top_index;
10454 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10455 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10456 htab->input_list = input_list;
10457 if (input_list == NULL)
10458 return -1;
10459
721956f4
AM
10460 return 1;
10461}
10462
927be08e
AM
10463/* Set up for first pass at multitoc partitioning. */
10464
10465void
10466ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10467{
10468 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10469
10470 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10471 htab->toc_curr = elf_gp (info->output_bfd);
10472 htab->toc_bfd = NULL;
10473 htab->toc_first_sec = NULL;
10474}
10475
e717da7e
AM
10476/* The linker repeatedly calls this function for each TOC input section
10477 and linker generated GOT section. Group input bfds such that the toc
927be08e 10478 within a group is less than 64k in size. */
ad8e1ba5 10479
927be08e 10480bfd_boolean
4ce794b7 10481ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10482{
10483 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10484 bfd_vma addr, off, limit;
ad8e1ba5 10485
4dfe6ac6
NC
10486 if (htab == NULL)
10487 return FALSE;
10488
927be08e 10489 if (!htab->second_toc_pass)
4c52953f 10490 {
927be08e 10491 /* Keep track of the first .toc or .got section for this input bfd. */
bf102f86
AM
10492 if (htab->toc_bfd != isec->owner)
10493 {
10494 htab->toc_bfd = isec->owner;
10495 htab->toc_first_sec = isec;
10496 }
927be08e 10497
bf102f86
AM
10498 addr = isec->output_offset + isec->output_section->vma;
10499 off = addr - htab->toc_curr;
d77c8a4b
AM
10500 limit = 0x80008000;
10501 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10502 limit = 0x10000;
10503 if (off + isec->size > limit)
bf102f86
AM
10504 {
10505 addr = (htab->toc_first_sec->output_offset
10506 + htab->toc_first_sec->output_section->vma);
10507 htab->toc_curr = addr;
10508 }
99877b66 10509
927be08e
AM
10510 /* toc_curr is the base address of this toc group. Set elf_gp
10511 for the input section to be the offset relative to the
10512 output toc base plus 0x8000. Making the input elf_gp an
10513 offset allows us to move the toc as a whole without
10514 recalculating input elf_gp. */
10515 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10516 off += TOC_BASE_OFF;
10517
10518 /* Die if someone uses a linker script that doesn't keep input
10519 file .toc and .got together. */
10520 if (elf_gp (isec->owner) != 0
10521 && elf_gp (isec->owner) != off)
10522 return FALSE;
10523
10524 elf_gp (isec->owner) = off;
10525 return TRUE;
4c52953f 10526 }
927be08e
AM
10527
10528 /* During the second pass toc_first_sec points to the start of
10529 a toc group, and toc_curr is used to track the old elf_gp.
10530 We use toc_bfd to ensure we only look at each bfd once. */
10531 if (htab->toc_bfd == isec->owner)
10532 return TRUE;
10533 htab->toc_bfd = isec->owner;
10534
10535 if (htab->toc_first_sec == NULL
10536 || htab->toc_curr != elf_gp (isec->owner))
10537 {
10538 htab->toc_curr = elf_gp (isec->owner);
10539 htab->toc_first_sec = isec;
10540 }
10541 addr = (htab->toc_first_sec->output_offset
10542 + htab->toc_first_sec->output_section->vma);
10543 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10544 elf_gp (isec->owner) = off;
10545
10546 return TRUE;
ad8e1ba5
AM
10547}
10548
927be08e
AM
10549/* Called via elf_link_hash_traverse to merge GOT entries for global
10550 symbol H. */
10551
10552static bfd_boolean
10553merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10554{
10555 if (h->root.type == bfd_link_hash_indirect)
10556 return TRUE;
10557
927be08e
AM
10558 merge_got_entries (&h->got.glist);
10559
10560 return TRUE;
10561}
10562
10563/* Called via elf_link_hash_traverse to allocate GOT entries for global
10564 symbol H. */
10565
10566static bfd_boolean
10567reallocate_got (struct elf_link_hash_entry *h, void *inf)
10568{
10569 struct got_entry *gent;
10570
10571 if (h->root.type == bfd_link_hash_indirect)
10572 return TRUE;
10573
927be08e
AM
10574 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10575 if (!gent->is_indirect)
10576 allocate_got (h, (struct bfd_link_info *) inf, gent);
10577 return TRUE;
10578}
10579
10580/* Called on the first multitoc pass after the last call to
10581 ppc64_elf_next_toc_section. This function removes duplicate GOT
10582 entries. */
10583
10584bfd_boolean
10585ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10586{
10587 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10588 struct bfd *ibfd, *ibfd2;
10589 bfd_boolean done_something;
10590
10591 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10592
7865406b
AM
10593 if (!htab->do_multi_toc)
10594 return FALSE;
10595
d0fae19d 10596 /* Merge global sym got entries within a toc group. */
927be08e
AM
10597 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10598
10599 /* And tlsld_got. */
10600 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10601 {
10602 struct got_entry *ent, *ent2;
10603
10604 if (!is_ppc64_elf (ibfd))
10605 continue;
10606
10607 ent = ppc64_tlsld_got (ibfd);
10608 if (!ent->is_indirect
10609 && ent->got.offset != (bfd_vma) -1)
10610 {
10611 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10612 {
10613 if (!is_ppc64_elf (ibfd2))
10614 continue;
10615
10616 ent2 = ppc64_tlsld_got (ibfd2);
10617 if (!ent2->is_indirect
10618 && ent2->got.offset != (bfd_vma) -1
10619 && elf_gp (ibfd2) == elf_gp (ibfd))
10620 {
10621 ent2->is_indirect = TRUE;
10622 ent2->got.ent = ent;
10623 }
10624 }
10625 }
10626 }
10627
10628 /* Zap sizes of got sections. */
10629 htab->reliplt->rawsize = htab->reliplt->size;
10630 htab->reliplt->size -= htab->got_reli_size;
10631 htab->got_reli_size = 0;
10632
10633 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10634 {
10635 asection *got, *relgot;
10636
10637 if (!is_ppc64_elf (ibfd))
10638 continue;
10639
10640 got = ppc64_elf_tdata (ibfd)->got;
10641 if (got != NULL)
10642 {
10643 got->rawsize = got->size;
10644 got->size = 0;
10645 relgot = ppc64_elf_tdata (ibfd)->relgot;
10646 relgot->rawsize = relgot->size;
10647 relgot->size = 0;
10648 }
10649 }
10650
10651 /* Now reallocate the got, local syms first. We don't need to
10652 allocate section contents again since we never increase size. */
10653 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10654 {
10655 struct got_entry **lgot_ents;
10656 struct got_entry **end_lgot_ents;
10657 struct plt_entry **local_plt;
10658 struct plt_entry **end_local_plt;
f961d9dd 10659 unsigned char *lgot_masks;
927be08e
AM
10660 bfd_size_type locsymcount;
10661 Elf_Internal_Shdr *symtab_hdr;
10662 asection *s, *srel;
10663
10664 if (!is_ppc64_elf (ibfd))
10665 continue;
10666
10667 lgot_ents = elf_local_got_ents (ibfd);
10668 if (!lgot_ents)
10669 continue;
10670
10671 symtab_hdr = &elf_symtab_hdr (ibfd);
10672 locsymcount = symtab_hdr->sh_info;
10673 end_lgot_ents = lgot_ents + locsymcount;
10674 local_plt = (struct plt_entry **) end_lgot_ents;
10675 end_local_plt = local_plt + locsymcount;
f961d9dd 10676 lgot_masks = (unsigned char *) end_local_plt;
927be08e
AM
10677 s = ppc64_elf_tdata (ibfd)->got;
10678 srel = ppc64_elf_tdata (ibfd)->relgot;
10679 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10680 {
10681 struct got_entry *ent;
10682
10683 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d
AM
10684 {
10685 unsigned int num = 1;
10686 ent->got.offset = s->size;
10687 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10688 num = 2;
10689 s->size += num * 8;
10690 if (info->shared)
10691 srel->size += num * sizeof (Elf64_External_Rela);
10692 else if ((*lgot_masks & PLT_IFUNC) != 0)
10693 {
10694 htab->reliplt->size
10695 += num * sizeof (Elf64_External_Rela);
10696 htab->got_reli_size
10697 += num * sizeof (Elf64_External_Rela);
10698 }
10699 }
927be08e
AM
10700 }
10701 }
10702
10703 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10704
10705 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10706 {
10707 struct got_entry *ent;
10708
10709 if (!is_ppc64_elf (ibfd))
10710 continue;
10711
10712 ent = ppc64_tlsld_got (ibfd);
10713 if (!ent->is_indirect
10714 && ent->got.offset != (bfd_vma) -1)
10715 {
10716 asection *s = ppc64_elf_tdata (ibfd)->got;
10717 ent->got.offset = s->size;
10718 s->size += 16;
10719 if (info->shared)
10720 {
10721 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10722 srel->size += sizeof (Elf64_External_Rela);
10723 }
10724 }
10725 }
10726
10727 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10728 if (!done_something)
10729 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10730 {
10731 asection *got;
10732
10733 if (!is_ppc64_elf (ibfd))
10734 continue;
10735
10736 got = ppc64_elf_tdata (ibfd)->got;
10737 if (got != NULL)
10738 {
10739 done_something = got->rawsize != got->size;
10740 if (done_something)
10741 break;
10742 }
10743 }
10744
10745 if (done_something)
10746 (*htab->layout_sections_again) ();
10747
10748 /* Set up for second pass over toc sections to recalculate elf_gp
10749 on input sections. */
10750 htab->toc_bfd = NULL;
10751 htab->toc_first_sec = NULL;
10752 htab->second_toc_pass = TRUE;
10753 return done_something;
10754}
10755
10756/* Called after second pass of multitoc partitioning. */
10757
10758void
10759ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10760{
10761 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10762
10763 /* After the second pass, toc_curr tracks the TOC offset used
10764 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 10765 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
10766}
10767
9b5ecbd0
AM
10768/* No toc references were found in ISEC. If the code in ISEC makes no
10769 calls, then there's no need to use toc adjusting stubs when branching
10770 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
10771 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10772 needed, and 2 if a cyclical call-graph was found but no other reason
10773 for a stub was detected. If called from the top level, a return of
10774 2 means the same as a return of 0. */
9b5ecbd0
AM
10775
10776static int
4ce794b7 10777toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 10778{
9b5ecbd0 10779 int ret;
70cc837d
AM
10780
10781 /* Mark this section as checked. */
10782 isec->call_check_done = 1;
9b5ecbd0 10783
772119ce
AM
10784 /* We know none of our code bearing sections will need toc stubs. */
10785 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10786 return 0;
10787
eea6121a 10788 if (isec->size == 0)
082c50f8
AM
10789 return 0;
10790
4c52953f
AM
10791 if (isec->output_section == NULL)
10792 return 0;
10793
4c52953f 10794 ret = 0;
70cc837d 10795 if (isec->reloc_count != 0)
9b5ecbd0 10796 {
70cc837d
AM
10797 Elf_Internal_Rela *relstart, *rel;
10798 Elf_Internal_Sym *local_syms;
10799 struct ppc_link_hash_table *htab;
2917689a 10800
70cc837d
AM
10801 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10802 info->keep_memory);
10803 if (relstart == NULL)
10804 return -1;
90aecf7a 10805
70cc837d
AM
10806 /* Look for branches to outside of this section. */
10807 local_syms = NULL;
10808 htab = ppc_hash_table (info);
10809 if (htab == NULL)
10810 return -1;
4c52953f 10811
70cc837d 10812 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 10813 {
70cc837d
AM
10814 enum elf_ppc64_reloc_type r_type;
10815 unsigned long r_symndx;
10816 struct elf_link_hash_entry *h;
10817 struct ppc_link_hash_entry *eh;
10818 Elf_Internal_Sym *sym;
10819 asection *sym_sec;
10820 struct _opd_sec_data *opd;
10821 bfd_vma sym_value;
10822 bfd_vma dest;
10823
10824 r_type = ELF64_R_TYPE (rel->r_info);
10825 if (r_type != R_PPC64_REL24
10826 && r_type != R_PPC64_REL14
10827 && r_type != R_PPC64_REL14_BRTAKEN
10828 && r_type != R_PPC64_REL14_BRNTAKEN)
10829 continue;
4c52953f 10830
70cc837d
AM
10831 r_symndx = ELF64_R_SYM (rel->r_info);
10832 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10833 isec->owner))
4c52953f 10834 {
70cc837d
AM
10835 ret = -1;
10836 break;
10837 }
4c52953f 10838
70cc837d
AM
10839 /* Calls to dynamic lib functions go through a plt call stub
10840 that uses r2. */
10841 eh = (struct ppc_link_hash_entry *) h;
10842 if (eh != NULL
10843 && (eh->elf.plt.plist != NULL
10844 || (eh->oh != NULL
10845 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10846 {
10847 ret = 1;
10848 break;
4c52953f
AM
10849 }
10850
70cc837d
AM
10851 if (sym_sec == NULL)
10852 /* Ignore other undefined symbols. */
4c52953f 10853 continue;
4c52953f 10854
70cc837d
AM
10855 /* Assume branches to other sections not included in the
10856 link need stubs too, to cover -R and absolute syms. */
10857 if (sym_sec->output_section == NULL)
10858 {
10859 ret = 1;
10860 break;
10861 }
4c52953f 10862
70cc837d
AM
10863 if (h == NULL)
10864 sym_value = sym->st_value;
10865 else
10866 {
10867 if (h->root.type != bfd_link_hash_defined
10868 && h->root.type != bfd_link_hash_defweak)
10869 abort ();
10870 sym_value = h->root.u.def.value;
10871 }
10872 sym_value += rel->r_addend;
4c52953f 10873
70cc837d
AM
10874 /* If this branch reloc uses an opd sym, find the code section. */
10875 opd = get_opd_info (sym_sec);
10876 if (opd != NULL)
10877 {
10878 if (h == NULL && opd->adjust != NULL)
10879 {
10880 long adjust;
4c52953f 10881
70cc837d
AM
10882 adjust = opd->adjust[sym->st_value / 8];
10883 if (adjust == -1)
10884 /* Assume deleted functions won't ever be called. */
10885 continue;
10886 sym_value += adjust;
10887 }
4c52953f 10888
70cc837d
AM
10889 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10890 if (dest == (bfd_vma) -1)
10891 continue;
10892 }
10893 else
10894 dest = (sym_value
10895 + sym_sec->output_offset
10896 + sym_sec->output_section->vma);
4c52953f 10897
70cc837d
AM
10898 /* Ignore branch to self. */
10899 if (sym_sec == isec)
10900 continue;
4c52953f 10901
70cc837d
AM
10902 /* If the called function uses the toc, we need a stub. */
10903 if (sym_sec->has_toc_reloc
10904 || sym_sec->makes_toc_func_call)
4c52953f 10905 {
70cc837d 10906 ret = 1;
4c52953f
AM
10907 break;
10908 }
70cc837d
AM
10909
10910 /* Assume any branch that needs a long branch stub might in fact
10911 need a plt_branch stub. A plt_branch stub uses r2. */
10912 else if (dest - (isec->output_offset
10913 + isec->output_section->vma
10914 + rel->r_offset) + (1 << 25) >= (2 << 25))
4c52953f 10915 {
70cc837d
AM
10916 ret = 1;
10917 break;
10918 }
10919
10920 /* If calling back to a section in the process of being
10921 tested, we can't say for sure that no toc adjusting stubs
10922 are needed, so don't return zero. */
10923 else if (sym_sec->call_check_in_progress)
10924 ret = 2;
10925
10926 /* Branches to another section that itself doesn't have any TOC
10927 references are OK. Recursively call ourselves to check. */
10928 else if (!sym_sec->call_check_done)
10929 {
10930 int recur;
10931
10932 /* Mark current section as indeterminate, so that other
10933 sections that call back to current won't be marked as
10934 known. */
10935 isec->call_check_in_progress = 1;
10936 recur = toc_adjusting_stub_needed (info, sym_sec);
10937 isec->call_check_in_progress = 0;
10938
4c52953f
AM
10939 if (recur != 0)
10940 {
70cc837d
AM
10941 ret = recur;
10942 if (recur != 2)
10943 break;
4c52953f
AM
10944 }
10945 }
4c52953f 10946 }
70cc837d
AM
10947
10948 if (local_syms != NULL
10949 && (elf_symtab_hdr (isec->owner).contents
10950 != (unsigned char *) local_syms))
10951 free (local_syms);
10952 if (elf_section_data (isec)->relocs != relstart)
10953 free (relstart);
9b5ecbd0
AM
10954 }
10955
70cc837d
AM
10956 if ((ret & 1) == 0
10957 && isec->map_head.s != NULL
10958 && (strcmp (isec->output_section->name, ".init") == 0
10959 || strcmp (isec->output_section->name, ".fini") == 0))
10960 {
10961 if (isec->map_head.s->has_toc_reloc
10962 || isec->map_head.s->makes_toc_func_call)
10963 ret = 1;
10964 else if (!isec->map_head.s->call_check_done)
10965 {
10966 int recur;
10967 isec->call_check_in_progress = 1;
10968 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10969 isec->call_check_in_progress = 0;
10970 if (recur != 0)
10971 ret = recur;
10972 }
10973 }
10974
10975 if (ret == 1)
10976 isec->makes_toc_func_call = 1;
4c52953f 10977
9b5ecbd0
AM
10978 return ret;
10979}
10980
721956f4
AM
10981/* The linker repeatedly calls this function for each input section,
10982 in the order that input sections are linked into output sections.
10983 Build lists of input sections to determine groupings between which
10984 we may insert linker stubs. */
10985
9b5ecbd0 10986bfd_boolean
4ce794b7 10987ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
10988{
10989 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10990
4dfe6ac6
NC
10991 if (htab == NULL)
10992 return FALSE;
10993
734b6cf9
AM
10994 if ((isec->output_section->flags & SEC_CODE) != 0
10995 && isec->output_section->index <= htab->top_index)
721956f4 10996 {
734b6cf9 10997 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 10998 /* Steal the link_sec pointer for our list. */
721956f4 10999#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
11000 /* This happens to make the list in reverse order,
11001 which is what we want. */
734b6cf9
AM
11002 PREV_SEC (isec) = *list;
11003 *list = isec;
721956f4 11004 }
ad8e1ba5 11005
4c52953f 11006 if (htab->multi_toc_needed)
9b5ecbd0 11007 {
4c52953f
AM
11008 /* If a code section has a function that uses the TOC then we need
11009 to use the right TOC (obviously). Also, make sure that .opd gets
11010 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
11011 can't find their function symbol (shouldn't ever happen now).
11012 Also specially treat .fixup for the linux kernel. .fixup
11013 contains branches, but only back to the function that hit an
11014 exception. */
11015 if (isec->has_toc_reloc
11016 || (isec->flags & SEC_CODE) == 0
11017 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
11018 {
11019 if (elf_gp (isec->owner) != 0)
11020 htab->toc_curr = elf_gp (isec->owner);
11021 }
6683a28d
AM
11022 else
11023 {
11024 if (!isec->call_check_done
11025 && toc_adjusting_stub_needed (info, isec) < 0)
11026 return FALSE;
11027 /* If we make a local call from this section, ie. a branch
11028 without a following nop, then we have no place to put a
11029 toc restoring insn. We must use the same toc group as
11030 the callee.
11031 Testing makes_toc_func_call actually tests for *any*
11032 calls to functions that need a good toc pointer. A more
11033 precise test would be better, as this one will set
11034 incorrect values for pasted .init/.fini fragments.
11035 (Fixed later in check_pasted_section.) */
11036 if (isec->makes_toc_func_call
11037 && elf_gp (isec->owner) != 0)
11038 htab->toc_curr = elf_gp (isec->owner);
11039 }
9b5ecbd0 11040 }
ad8e1ba5
AM
11041
11042 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 11043 Use the last TOC base. */
ad8e1ba5 11044 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11045 return TRUE;
721956f4
AM
11046}
11047
70cc837d
AM
11048/* Check that all .init and .fini sections use the same toc, if they
11049 have toc relocs. */
11050
11051static bfd_boolean
11052check_pasted_section (struct bfd_link_info *info, const char *name)
11053{
11054 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11055
11056 if (o != NULL)
11057 {
11058 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11059 bfd_vma toc_off = 0;
11060 asection *i;
11061
11062 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11063 if (i->has_toc_reloc)
11064 {
11065 if (toc_off == 0)
11066 toc_off = htab->stub_group[i->id].toc_off;
11067 else if (toc_off != htab->stub_group[i->id].toc_off)
11068 return FALSE;
11069 }
6683a28d
AM
11070
11071 if (toc_off == 0)
11072 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11073 if (i->makes_toc_func_call)
11074 {
11075 toc_off = htab->stub_group[i->id].toc_off;
11076 break;
11077 }
11078
70cc837d
AM
11079 /* Make sure the whole pasted function uses the same toc offset. */
11080 if (toc_off != 0)
11081 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11082 htab->stub_group[i->id].toc_off = toc_off;
11083 }
11084 return TRUE;
11085}
11086
11087bfd_boolean
11088ppc64_elf_check_init_fini (struct bfd_link_info *info)
11089{
11090 return (check_pasted_section (info, ".init")
11091 & check_pasted_section (info, ".fini"));
11092}
11093
721956f4
AM
11094/* See whether we can group stub sections together. Grouping stub
11095 sections may result in fewer stubs. More importantly, we need to
11096 put all .init* and .fini* stubs at the beginning of the .init or
11097 .fini output sections respectively, because glibc splits the
11098 _init and _fini functions into multiple parts. Putting a stub in
11099 the middle of a function is not a good idea. */
11100
11101static void
4ce794b7
AM
11102group_sections (struct ppc_link_hash_table *htab,
11103 bfd_size_type stub_group_size,
11104 bfd_boolean stubs_always_before_branch)
721956f4 11105{
7c8fe5c4
AM
11106 asection **list;
11107 bfd_size_type stub14_group_size;
11108 bfd_boolean suppress_size_errors;
11109
11110 suppress_size_errors = FALSE;
11111 stub14_group_size = stub_group_size;
11112 if (stub_group_size == 1)
11113 {
11114 /* Default values. */
11115 if (stubs_always_before_branch)
11116 {
11117 stub_group_size = 0x1e00000;
11118 stub14_group_size = 0x7800;
11119 }
11120 else
11121 {
11122 stub_group_size = 0x1c00000;
11123 stub14_group_size = 0x7000;
11124 }
11125 suppress_size_errors = TRUE;
11126 }
11127
11128 list = htab->input_list + htab->top_index;
734b6cf9 11129 do
721956f4 11130 {
734b6cf9
AM
11131 asection *tail = *list;
11132 while (tail != NULL)
721956f4 11133 {
734b6cf9
AM
11134 asection *curr;
11135 asection *prev;
11136 bfd_size_type total;
11137 bfd_boolean big_sec;
11138 bfd_vma curr_toc;
11139
11140 curr = tail;
eea6121a 11141 total = tail->size;
6bee8834
AM
11142 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11143 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
11144 ? stub14_group_size : stub_group_size);
11145 if (big_sec && !suppress_size_errors)
5c3dead3
AM
11146 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11147 tail->owner, tail);
734b6cf9
AM
11148 curr_toc = htab->stub_group[tail->id].toc_off;
11149
11150 while ((prev = PREV_SEC (curr)) != NULL
11151 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11152 < (ppc64_elf_section_data (prev) != NULL
11153 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11154 ? stub14_group_size : stub_group_size))
ad8e1ba5 11155 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
11156 curr = prev;
11157
11158 /* OK, the size from the start of CURR to the end is less
11159 than stub_group_size and thus can be handled by one stub
11160 section. (or the tail section is itself larger than
11161 stub_group_size, in which case we may be toast.) We
11162 should really be keeping track of the total size of stubs
11163 added here, as stubs contribute to the final output
11164 section size. That's a little tricky, and this way will
11165 only break if stubs added make the total size more than
11166 2^25, ie. for the default stub_group_size, if stubs total
11167 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11168 do
721956f4
AM
11169 {
11170 prev = PREV_SEC (tail);
734b6cf9 11171 /* Set up this stub group. */
721956f4
AM
11172 htab->stub_group[tail->id].link_sec = curr;
11173 }
734b6cf9
AM
11174 while (tail != curr && (tail = prev) != NULL);
11175
11176 /* But wait, there's more! Input sections up to stub_group_size
11177 bytes before the stub section can be handled by it too.
11178 Don't do this if we have a really large section after the
11179 stubs, as adding more stubs increases the chance that
11180 branches may not reach into the stub section. */
11181 if (!stubs_always_before_branch && !big_sec)
11182 {
11183 total = 0;
11184 while (prev != NULL
11185 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11186 < (ppc64_elf_section_data (prev) != NULL
11187 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11188 ? stub14_group_size : stub_group_size))
734b6cf9
AM
11189 && htab->stub_group[prev->id].toc_off == curr_toc)
11190 {
11191 tail = prev;
11192 prev = PREV_SEC (tail);
11193 htab->stub_group[tail->id].link_sec = curr;
11194 }
11195 }
11196 tail = prev;
721956f4
AM
11197 }
11198 }
734b6cf9
AM
11199 while (list-- != htab->input_list);
11200 free (htab->input_list);
721956f4
AM
11201#undef PREV_SEC
11202}
11203
58d180e8
AM
11204static const unsigned char glink_eh_frame_cie[] =
11205{
11206 0, 0, 0, 16, /* length. */
11207 0, 0, 0, 0, /* id. */
11208 1, /* CIE version. */
11209 'z', 'R', 0, /* Augmentation string. */
11210 4, /* Code alignment. */
11211 0x78, /* Data alignment. */
11212 65, /* RA reg. */
11213 1, /* Augmentation size. */
11214 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11215 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11216};
11217
d969d15f
AM
11218/* Stripping output sections is normally done before dynamic section
11219 symbols have been allocated. This function is called later, and
11220 handles cases like htab->brlt which is mapped to its own output
11221 section. */
11222
11223static void
11224maybe_strip_output (struct bfd_link_info *info, asection *isec)
11225{
11226 if (isec->size == 0
11227 && isec->output_section->size == 0
11228 && !bfd_section_removed_from_list (info->output_bfd,
11229 isec->output_section)
11230 && elf_section_data (isec->output_section)->dynindx == 0)
11231 {
11232 isec->output_section->flags |= SEC_EXCLUDE;
11233 bfd_section_list_remove (info->output_bfd, isec->output_section);
11234 info->output_bfd->section_count--;
11235 }
11236}
11237
721956f4
AM
11238/* Determine and set the size of the stub section for a final link.
11239
11240 The basic idea here is to examine all the relocations looking for
11241 PC-relative calls to a target that is unreachable with a "bl"
11242 instruction. */
11243
b34976b6 11244bfd_boolean
9df0ef5f 11245ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
794e51c0
AM
11246 bfd_boolean plt_static_chain, int plt_thread_safe,
11247 int plt_stub_align)
721956f4
AM
11248{
11249 bfd_size_type stub_group_size;
b34976b6 11250 bfd_boolean stubs_always_before_branch;
721956f4
AM
11251 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11252
4dfe6ac6
NC
11253 if (htab == NULL)
11254 return FALSE;
11255
9df0ef5f 11256 htab->plt_static_chain = plt_static_chain;
794e51c0
AM
11257 htab->plt_stub_align = plt_stub_align;
11258 if (plt_thread_safe == -1)
11259 {
11260 const char *const thread_starter[] =
11261 {
11262 "pthread_create",
11263 /* libstdc++ */
11264 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11265 /* librt */
11266 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11267 "mq_notify", "create_timer",
11268 /* libanl */
11269 "getaddrinfo_a",
11270 /* libgomp */
11271 "GOMP_parallel_start",
11272 "GOMP_parallel_loop_static_start",
11273 "GOMP_parallel_loop_dynamic_start",
11274 "GOMP_parallel_loop_guided_start",
11275 "GOMP_parallel_loop_runtime_start",
11276 "GOMP_parallel_sections_start",
11277 };
11278 unsigned i;
11279
11280 for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11281 {
11282 struct elf_link_hash_entry *h;
11283 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11284 FALSE, FALSE, TRUE);
11285 plt_thread_safe = h != NULL && h->ref_regular;
11286 if (plt_thread_safe)
11287 break;
11288 }
11289 }
11290 htab->plt_thread_safe = plt_thread_safe;
721956f4
AM
11291 stubs_always_before_branch = group_size < 0;
11292 if (group_size < 0)
11293 stub_group_size = -group_size;
11294 else
11295 stub_group_size = group_size;
721956f4
AM
11296
11297 group_sections (htab, stub_group_size, stubs_always_before_branch);
11298
721956f4
AM
11299 while (1)
11300 {
11301 bfd *input_bfd;
11302 unsigned int bfd_indx;
11303 asection *stub_sec;
721956f4
AM
11304
11305 htab->stub_iteration += 1;
721956f4
AM
11306
11307 for (input_bfd = info->input_bfds, bfd_indx = 0;
11308 input_bfd != NULL;
11309 input_bfd = input_bfd->link_next, bfd_indx++)
11310 {
11311 Elf_Internal_Shdr *symtab_hdr;
11312 asection *section;
6cdc0ccc 11313 Elf_Internal_Sym *local_syms = NULL;
721956f4 11314
0c8d6e5c 11315 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11316 continue;
11317
721956f4 11318 /* We'll need the symbol table in a second. */
0ffa91dd 11319 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11320 if (symtab_hdr->sh_info == 0)
11321 continue;
11322
721956f4
AM
11323 /* Walk over each section attached to the input bfd. */
11324 for (section = input_bfd->sections;
11325 section != NULL;
11326 section = section->next)
11327 {
721956f4 11328 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11329
11330 /* If there aren't any relocs, then there's nothing more
11331 to do. */
11332 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11333 || (section->flags & SEC_ALLOC) == 0
11334 || (section->flags & SEC_LOAD) == 0
11335 || (section->flags & SEC_CODE) == 0
721956f4
AM
11336 || section->reloc_count == 0)
11337 continue;
11338
11339 /* If this section is a link-once section that will be
11340 discarded, then don't create any stubs. */
11341 if (section->output_section == NULL
927be08e 11342 || section->output_section->owner != info->output_bfd)
721956f4
AM
11343 continue;
11344
1e2f5b6e
AM
11345 /* Get the relocs. */
11346 internal_relocs
4ce794b7 11347 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 11348 info->keep_memory);
721956f4 11349 if (internal_relocs == NULL)
1e2f5b6e 11350 goto error_ret_free_local;
721956f4
AM
11351
11352 /* Now examine each relocation. */
11353 irela = internal_relocs;
11354 irelaend = irela + section->reloc_count;
11355 for (; irela < irelaend; irela++)
11356 {
4ce794b7
AM
11357 enum elf_ppc64_reloc_type r_type;
11358 unsigned int r_indx;
721956f4
AM
11359 enum ppc_stub_type stub_type;
11360 struct ppc_stub_hash_entry *stub_entry;
8387904d 11361 asection *sym_sec, *code_sec;
e054468f 11362 bfd_vma sym_value, code_value;
721956f4 11363 bfd_vma destination;
8843416a 11364 bfd_boolean ok_dest;
721956f4 11365 struct ppc_link_hash_entry *hash;
8387904d 11366 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
11367 struct elf_link_hash_entry *h;
11368 Elf_Internal_Sym *sym;
721956f4
AM
11369 char *stub_name;
11370 const asection *id_sec;
74f0fb50 11371 struct _opd_sec_data *opd;
e054468f 11372 struct plt_entry *plt_ent;
721956f4
AM
11373
11374 r_type = ELF64_R_TYPE (irela->r_info);
11375 r_indx = ELF64_R_SYM (irela->r_info);
11376
4ce794b7 11377 if (r_type >= R_PPC64_max)
721956f4
AM
11378 {
11379 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 11380 goto error_ret_free_internal;
721956f4
AM
11381 }
11382
11383 /* Only look for stubs on branch instructions. */
4ce794b7
AM
11384 if (r_type != R_PPC64_REL24
11385 && r_type != R_PPC64_REL14
11386 && r_type != R_PPC64_REL14_BRTAKEN
11387 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
11388 continue;
11389
11390 /* Now determine the call target, its name, value,
11391 section. */
411e1bfb
AM
11392 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11393 r_indx, input_bfd))
11394 goto error_ret_free_internal;
11395 hash = (struct ppc_link_hash_entry *) h;
11396
8843416a 11397 ok_dest = FALSE;
8387904d 11398 fdh = NULL;
7fe2b9a6 11399 sym_value = 0;
411e1bfb 11400 if (hash == NULL)
721956f4 11401 {
411e1bfb 11402 sym_value = sym->st_value;
8843416a 11403 ok_dest = TRUE;
721956f4 11404 }
7fe2b9a6
AM
11405 else if (hash->elf.root.type == bfd_link_hash_defined
11406 || hash->elf.root.type == bfd_link_hash_defweak)
11407 {
11408 sym_value = hash->elf.root.u.def.value;
11409 if (sym_sec->output_section != NULL)
11410 ok_dest = TRUE;
11411 }
11412 else if (hash->elf.root.type == bfd_link_hash_undefweak
11413 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11414 {
99877b66 11415 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11416 use the func descriptor sym instead if it is
11417 defined. */
ceb1f1ef 11418 if (hash->elf.root.root.string[0] == '.'
b31867b6 11419 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 11420 {
8387904d
AM
11421 if (fdh->elf.root.type == bfd_link_hash_defined
11422 || fdh->elf.root.type == bfd_link_hash_defweak)
11423 {
11424 sym_sec = fdh->elf.root.u.def.section;
11425 sym_value = fdh->elf.root.u.def.value;
11426 if (sym_sec->output_section != NULL)
11427 ok_dest = TRUE;
11428 }
99877b66
AM
11429 else
11430 fdh = NULL;
8387904d 11431 }
7fe2b9a6
AM
11432 }
11433 else
11434 {
11435 bfd_set_error (bfd_error_bad_value);
11436 goto error_ret_free_internal;
721956f4
AM
11437 }
11438
8843416a
AM
11439 destination = 0;
11440 if (ok_dest)
11441 {
11442 sym_value += irela->r_addend;
11443 destination = (sym_value
11444 + sym_sec->output_offset
11445 + sym_sec->output_section->vma);
11446 }
11447
8387904d 11448 code_sec = sym_sec;
e054468f 11449 code_value = sym_value;
74f0fb50
AM
11450 opd = get_opd_info (sym_sec);
11451 if (opd != NULL)
8387904d
AM
11452 {
11453 bfd_vma dest;
11454
74f0fb50 11455 if (hash == NULL && opd->adjust != NULL)
8387904d 11456 {
74f0fb50 11457 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11458 if (adjust == -1)
11459 continue;
e054468f 11460 code_value += adjust;
8387904d
AM
11461 sym_value += adjust;
11462 }
11463 dest = opd_entry_value (sym_sec, sym_value,
e054468f 11464 &code_sec, &code_value);
8387904d
AM
11465 if (dest != (bfd_vma) -1)
11466 {
11467 destination = dest;
11468 if (fdh != NULL)
11469 {
11470 /* Fixup old ABI sym to point at code
11471 entry. */
99877b66 11472 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11473 hash->elf.root.u.def.section = code_sec;
e054468f 11474 hash->elf.root.u.def.value = code_value;
8387904d
AM
11475 }
11476 }
11477 }
11478
721956f4 11479 /* Determine what (if any) linker stub is needed. */
e054468f 11480 plt_ent = NULL;
721956f4 11481 stub_type = ppc_type_of_stub (section, irela, &hash,
e054468f 11482 &plt_ent, destination);
ad8e1ba5
AM
11483
11484 if (stub_type != ppc_stub_plt_call)
11485 {
11486 /* Check whether we need a TOC adjusting stub.
11487 Since the linker pastes together pieces from
11488 different object files when creating the
11489 _init and _fini functions, it may be that a
11490 call to what looks like a local sym is in
11491 fact a call needing a TOC adjustment. */
8387904d
AM
11492 if (code_sec != NULL
11493 && code_sec->output_section != NULL
11494 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11495 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11496 && (code_sec->has_toc_reloc
11497 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
11498 stub_type = ppc_stub_long_branch_r2off;
11499 }
11500
721956f4
AM
11501 if (stub_type == ppc_stub_none)
11502 continue;
11503
411e1bfb
AM
11504 /* __tls_get_addr calls might be eliminated. */
11505 if (stub_type != ppc_stub_plt_call
11506 && hash != NULL
8387904d
AM
11507 && (hash == htab->tls_get_addr
11508 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11509 && section->has_tls_reloc
11510 && irela != internal_relocs)
11511 {
11512 /* Get tls info. */
f961d9dd 11513 unsigned char *tls_mask;
411e1bfb 11514
3a71aa26 11515 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11516 irela - 1, input_bfd))
11517 goto error_ret_free_internal;
e7b938ca 11518 if (*tls_mask != 0)
411e1bfb
AM
11519 continue;
11520 }
11521
3b421ab3
AM
11522 if (stub_type == ppc_stub_plt_call
11523 && irela + 1 < irelaend
11524 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
11525 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
11526 {
11527 if (!tocsave_find (htab, INSERT,
11528 &local_syms, irela + 1, input_bfd))
11529 goto error_ret_free_internal;
11530 }
11531 else if (stub_type == ppc_stub_plt_call)
11532 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 11533
721956f4
AM
11534 /* Support for grouping stub sections. */
11535 id_sec = htab->stub_group[section->id].link_sec;
11536
11537 /* Get the name of this stub. */
11538 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11539 if (!stub_name)
11540 goto error_ret_free_internal;
11541
11542 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11543 stub_name, FALSE, FALSE);
721956f4
AM
11544 if (stub_entry != NULL)
11545 {
11546 /* The proper stub has already been created. */
11547 free (stub_name);
794e51c0
AM
11548 if (stub_type == ppc_stub_plt_call_r2save)
11549 stub_entry->stub_type = stub_type;
721956f4
AM
11550 continue;
11551 }
11552
25f53a85 11553 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
11554 if (stub_entry == NULL)
11555 {
11556 free (stub_name);
6cdc0ccc
AM
11557 error_ret_free_internal:
11558 if (elf_section_data (section)->relocs == NULL)
11559 free (internal_relocs);
11560 error_ret_free_local:
11561 if (local_syms != NULL
11562 && (symtab_hdr->contents
11563 != (unsigned char *) local_syms))
11564 free (local_syms);
b34976b6 11565 return FALSE;
721956f4
AM
11566 }
11567
ad8e1ba5 11568 stub_entry->stub_type = stub_type;
794e51c0
AM
11569 if (stub_type != ppc_stub_plt_call
11570 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
11571 {
11572 stub_entry->target_value = code_value;
11573 stub_entry->target_section = code_sec;
11574 }
11575 else
11576 {
11577 stub_entry->target_value = sym_value;
11578 stub_entry->target_section = sym_sec;
11579 }
721956f4 11580 stub_entry->h = hash;
e054468f 11581 stub_entry->plt_ent = plt_ent;
411e1bfb 11582 stub_entry->addend = irela->r_addend;
ee75fd95
AM
11583
11584 if (stub_entry->h != NULL)
11585 htab->stub_globals += 1;
721956f4
AM
11586 }
11587
11588 /* We're done with the internal relocs, free them. */
6cdc0ccc 11589 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 11590 free (internal_relocs);
721956f4 11591 }
6cdc0ccc
AM
11592
11593 if (local_syms != NULL
11594 && symtab_hdr->contents != (unsigned char *) local_syms)
11595 {
11596 if (!info->keep_memory)
11597 free (local_syms);
11598 else
11599 symtab_hdr->contents = (unsigned char *) local_syms;
11600 }
721956f4
AM
11601 }
11602
5c3dead3 11603 /* We may have added some stubs. Find out the new size of the
721956f4
AM
11604 stub sections. */
11605 for (stub_sec = htab->stub_bfd->sections;
11606 stub_sec != NULL;
11607 stub_sec = stub_sec->next)
e717da7e 11608 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 11609 {
5c3dead3 11610 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
11611 stub_sec->size = 0;
11612 stub_sec->reloc_count = 0;
84f5d08e 11613 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 11614 }
eea6121a
AM
11615
11616 htab->brlt->size = 0;
84f5d08e
AM
11617 htab->brlt->reloc_count = 0;
11618 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 11619 if (htab->relbrlt != NULL)
eea6121a 11620 htab->relbrlt->size = 0;
721956f4 11621
63bc6f6c 11622 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 11623
176a0d42
AM
11624 if (info->emitrelocations
11625 && htab->glink != NULL && htab->glink->size != 0)
11626 {
11627 htab->glink->reloc_count = 1;
11628 htab->glink->flags |= SEC_RELOC;
11629 }
11630
58d180e8
AM
11631 if (htab->glink_eh_frame != NULL
11632 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11633 && (htab->glink_eh_frame->flags & SEC_EXCLUDE) == 0)
11634 {
11635 bfd_size_type size = 0;
11636
11637 for (stub_sec = htab->stub_bfd->sections;
11638 stub_sec != NULL;
11639 stub_sec = stub_sec->next)
11640 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11641 size += 20;
11642 if (htab->glink != NULL && htab->glink->size != 0)
11643 size += 24;
11644 if (size != 0)
11645 size += sizeof (glink_eh_frame_cie);
11646 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11647 htab->glink_eh_frame->size = size;
11648 }
11649
794e51c0
AM
11650 if (htab->plt_stub_align != 0)
11651 for (stub_sec = htab->stub_bfd->sections;
11652 stub_sec != NULL;
11653 stub_sec = stub_sec->next)
11654 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11655 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
11656 & (-1 << htab->plt_stub_align));
11657
5c3dead3
AM
11658 for (stub_sec = htab->stub_bfd->sections;
11659 stub_sec != NULL;
11660 stub_sec = stub_sec->next)
11661 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11662 && stub_sec->rawsize != stub_sec->size)
11663 break;
11664
11665 /* Exit from this loop when no stubs have been added, and no stubs
11666 have changed size. */
58d180e8
AM
11667 if (stub_sec == NULL
11668 && (htab->glink_eh_frame == NULL
11669 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
11670 break;
11671
721956f4
AM
11672 /* Ask the linker to do its stuff. */
11673 (*htab->layout_sections_again) ();
11674 }
11675
d969d15f
AM
11676 maybe_strip_output (info, htab->brlt);
11677 if (htab->glink_eh_frame != NULL)
11678 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 11679
b34976b6 11680 return TRUE;
721956f4
AM
11681}
11682
11683/* Called after we have determined section placement. If sections
805fc799 11684 move, we'll be called again. Provide a value for TOCstart. */
721956f4 11685
805fc799 11686bfd_vma
4ce794b7 11687ppc64_elf_toc (bfd *obfd)
721956f4 11688{
805fc799
AM
11689 asection *s;
11690 bfd_vma TOCstart;
721956f4 11691
805fc799
AM
11692 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11693 order. The TOC starts where the first of these sections starts. */
11694 s = bfd_get_section_by_name (obfd, ".got");
e054468f 11695 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11696 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 11697 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11698 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 11699 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11700 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 11701 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
11702 {
11703 /* This may happen for
11704 o references to TOC base (SYM@toc / TOC[tc0]) without a
11705 .toc directive
11706 o bad linker script
11707 o --gc-sections and empty TOC sections
11708
11709 FIXME: Warn user? */
11710
11711 /* Look for a likely section. We probably won't even be
11712 using TOCstart. */
11713 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11714 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11715 | SEC_EXCLUDE))
805fc799
AM
11716 == (SEC_ALLOC | SEC_SMALL_DATA))
11717 break;
721956f4 11718 if (s == NULL)
805fc799 11719 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11720 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
11721 == (SEC_ALLOC | SEC_SMALL_DATA))
11722 break;
721956f4 11723 if (s == NULL)
805fc799 11724 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11725 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11726 == SEC_ALLOC)
805fc799 11727 break;
721956f4 11728 if (s == NULL)
805fc799 11729 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11730 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
11731 break;
11732 }
721956f4 11733
805fc799
AM
11734 TOCstart = 0;
11735 if (s != NULL)
11736 TOCstart = s->output_section->vma + s->output_offset;
721956f4 11737
805fc799 11738 return TOCstart;
721956f4
AM
11739}
11740
11741/* Build all the stubs associated with the current output file.
11742 The stubs are kept in a hash table attached to the main linker
11743 hash table. This function is called via gldelf64ppc_finish. */
11744
b34976b6 11745bfd_boolean
4ce794b7
AM
11746ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11747 struct bfd_link_info *info,
11748 char **stats)
5d1634d7
AM
11749{
11750 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 11751 asection *stub_sec;
5d1634d7 11752 bfd_byte *p;
e717da7e 11753 int stub_sec_count = 0;
5d1634d7 11754
4dfe6ac6
NC
11755 if (htab == NULL)
11756 return FALSE;
11757
ad8e1ba5 11758 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
11759
11760 /* Allocate memory to hold the linker stubs. */
721956f4
AM
11761 for (stub_sec = htab->stub_bfd->sections;
11762 stub_sec != NULL;
11763 stub_sec = stub_sec->next)
eea6121a
AM
11764 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11765 && stub_sec->size != 0)
e717da7e 11766 {
eea6121a
AM
11767 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11768 if (stub_sec->contents == NULL)
11769 return FALSE;
11770 /* We want to check that built size is the same as calculated
11771 size. rawsize is a convenient location to use. */
11772 stub_sec->rawsize = stub_sec->size;
11773 stub_sec->size = 0;
e717da7e 11774 }
5d1634d7 11775
23eb7e01 11776 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 11777 {
9f951329 11778 unsigned int indx;
ad8e1ba5 11779 bfd_vma plt0;
9f951329 11780
721956f4 11781 /* Build the .glink plt call stub. */
97b639ba
AM
11782 if (htab->emit_stub_syms)
11783 {
11784 struct elf_link_hash_entry *h;
468392fb
AM
11785 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11786 TRUE, FALSE, FALSE);
97b639ba
AM
11787 if (h == NULL)
11788 return FALSE;
11789 if (h->root.type == bfd_link_hash_new)
11790 {
11791 h->root.type = bfd_link_hash_defined;
11792 h->root.u.def.section = htab->glink;
ee4bf8d2 11793 h->root.u.def.value = 8;
f5385ebf
AM
11794 h->ref_regular = 1;
11795 h->def_regular = 1;
11796 h->ref_regular_nonweak = 1;
11797 h->forced_local = 1;
11798 h->non_elf = 0;
97b639ba
AM
11799 }
11800 }
176a0d42
AM
11801 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11802 if (info->emitrelocations)
11803 {
11804 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11805 if (r == NULL)
11806 return FALSE;
11807 r->r_offset = (htab->glink->output_offset
11808 + htab->glink->output_section->vma);
11809 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11810 r->r_addend = plt0;
11811 }
4ce794b7 11812 p = htab->glink->contents;
176a0d42 11813 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
11814 bfd_put_64 (htab->glink->owner, plt0, p);
11815 p += 8;
11816 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 11817 p += 4;
ee4bf8d2 11818 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 11819 p += 4;
ee4bf8d2 11820 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 11821 p += 4;
ee4bf8d2 11822 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 11823 p += 4;
ee4bf8d2 11824 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 11825 p += 4;
ee4bf8d2 11826 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 11827 p += 4;
ee4bf8d2 11828 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 11829 p += 4;
4ce794b7 11830 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 11831 p += 4;
4ce794b7 11832 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 11833 p += 4;
4ce794b7 11834 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 11835 p += 4;
4ce794b7 11836 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 11837 p += 4;
ee4bf8d2
AM
11838 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11839 {
11840 bfd_put_32 (htab->glink->owner, NOP, p);
11841 p += 4;
11842 }
ad8e1ba5 11843
9f951329
AM
11844 /* Build the .glink lazy link call stubs. */
11845 indx = 0;
eea6121a 11846 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
11847 {
11848 if (indx < 0x8000)
11849 {
4ce794b7 11850 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
11851 p += 4;
11852 }
11853 else
11854 {
4ce794b7 11855 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 11856 p += 4;
4ce794b7 11857 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
11858 p += 4;
11859 }
4ce794b7 11860 bfd_put_32 (htab->glink->owner,
ee4bf8d2 11861 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 11862 indx++;
9f951329
AM
11863 p += 4;
11864 }
eea6121a 11865 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 11866 }
5d1634d7 11867
eea6121a 11868 if (htab->brlt->size != 0)
721956f4 11869 {
4ce794b7 11870 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 11871 htab->brlt->size);
4ce794b7 11872 if (htab->brlt->contents == NULL)
b34976b6 11873 return FALSE;
721956f4 11874 }
ee75fd95 11875 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
11876 {
11877 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 11878 htab->relbrlt->size);
63bc6f6c
AM
11879 if (htab->relbrlt->contents == NULL)
11880 return FALSE;
11881 }
5d1634d7 11882
58d180e8
AM
11883 if (htab->glink_eh_frame != NULL
11884 && htab->glink_eh_frame->size != 0)
11885 {
11886 bfd_vma val;
11887
11888 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
11889 if (p == NULL)
11890 return FALSE;
11891 htab->glink_eh_frame->contents = p;
11892
11893 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11894
11895 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
11896 /* CIE length (rewrite in case little-endian). */
11897 bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
11898 p += sizeof (glink_eh_frame_cie);
11899
11900 for (stub_sec = htab->stub_bfd->sections;
11901 stub_sec != NULL;
11902 stub_sec = stub_sec->next)
11903 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11904 {
11905 /* FDE length. */
11906 bfd_put_32 (htab->elf.dynobj, 16, p);
11907 p += 4;
11908 /* CIE pointer. */
11909 val = p - htab->glink_eh_frame->contents;
11910 bfd_put_32 (htab->elf.dynobj, val, p);
11911 p += 4;
11912 /* Offset to stub section. */
11913 val = (stub_sec->output_section->vma
11914 + stub_sec->output_offset);
11915 val -= (htab->glink_eh_frame->output_section->vma
11916 + htab->glink_eh_frame->output_offset);
11917 val -= p - htab->glink_eh_frame->contents;
11918 if (val + 0x80000000 > 0xffffffff)
11919 {
11920 info->callbacks->einfo
8de848d8 11921 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
11922 stub_sec->name);
11923 return FALSE;
11924 }
11925 bfd_put_32 (htab->elf.dynobj, val, p);
11926 p += 4;
11927 /* stub section size. */
11928 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
11929 p += 4;
11930 /* Augmentation. */
11931 p += 1;
11932 /* Pad. */
11933 p += 3;
11934 }
11935 if (htab->glink != NULL && htab->glink->size != 0)
11936 {
11937 /* FDE length. */
11938 bfd_put_32 (htab->elf.dynobj, 20, p);
11939 p += 4;
11940 /* CIE pointer. */
11941 val = p - htab->glink_eh_frame->contents;
11942 bfd_put_32 (htab->elf.dynobj, val, p);
11943 p += 4;
11944 /* Offset to .glink. */
11945 val = (htab->glink->output_section->vma
11946 + htab->glink->output_offset
11947 + 8);
11948 val -= (htab->glink_eh_frame->output_section->vma
11949 + htab->glink_eh_frame->output_offset);
11950 val -= p - htab->glink_eh_frame->contents;
11951 if (val + 0x80000000 > 0xffffffff)
11952 {
11953 info->callbacks->einfo
8de848d8 11954 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
11955 htab->glink->name);
11956 return FALSE;
11957 }
11958 bfd_put_32 (htab->elf.dynobj, val, p);
11959 p += 4;
11960 /* .glink size. */
11961 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
11962 p += 4;
11963 /* Augmentation. */
11964 p += 1;
11965
11966 *p++ = DW_CFA_advance_loc + 1;
11967 *p++ = DW_CFA_register;
11968 *p++ = 65;
11969 *p++ = 12;
11970 *p++ = DW_CFA_advance_loc + 4;
11971 *p++ = DW_CFA_restore_extended;
11972 *p++ = 65;
11973 }
11974 htab->glink_eh_frame->size = p - htab->glink_eh_frame->contents;
11975 }
11976
721956f4
AM
11977 /* Build the stubs as directed by the stub hash table. */
11978 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 11979
aa8a7074
AM
11980 if (htab->relbrlt != NULL)
11981 htab->relbrlt->reloc_count = 0;
11982
794e51c0
AM
11983 if (htab->plt_stub_align != 0)
11984 for (stub_sec = htab->stub_bfd->sections;
11985 stub_sec != NULL;
11986 stub_sec = stub_sec->next)
11987 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11988 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
11989 & (-1 << htab->plt_stub_align));
11990
721956f4
AM
11991 for (stub_sec = htab->stub_bfd->sections;
11992 stub_sec != NULL;
11993 stub_sec = stub_sec->next)
e717da7e
AM
11994 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11995 {
11996 stub_sec_count += 1;
eea6121a 11997 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
11998 break;
11999 }
5d1634d7 12000
721956f4 12001 if (stub_sec != NULL
58d180e8
AM
12002 || htab->glink->rawsize != htab->glink->size
12003 || (htab->glink_eh_frame != NULL
12004 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 12005 {
b34976b6 12006 htab->stub_error = TRUE;
8de848d8 12007 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 12008 }
721956f4 12009
d2a300cf
AM
12010 if (htab->stub_error)
12011 return FALSE;
12012
12013 if (stats != NULL)
12014 {
12015 *stats = bfd_malloc (500);
12016 if (*stats == NULL)
12017 return FALSE;
12018
ee75fd95 12019 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
12020 " branch %lu\n"
12021 " toc adjust %lu\n"
12022 " long branch %lu\n"
12023 " long toc adj %lu\n"
794e51c0
AM
12024 " plt call %lu\n"
12025 " plt call toc %lu"),
e717da7e 12026 stub_sec_count,
ee75fd95 12027 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
12028 htab->stub_count[ppc_stub_long_branch - 1],
12029 htab->stub_count[ppc_stub_long_branch_r2off - 1],
12030 htab->stub_count[ppc_stub_plt_branch - 1],
12031 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0
AM
12032 htab->stub_count[ppc_stub_plt_call - 1],
12033 htab->stub_count[ppc_stub_plt_call_r2save - 1]);
d2a300cf
AM
12034 }
12035 return TRUE;
5bd4f169
AM
12036}
12037
99877b66
AM
12038/* This function undoes the changes made by add_symbol_adjust. */
12039
12040static bfd_boolean
12041undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12042{
12043 struct ppc_link_hash_entry *eh;
12044
12045 if (h->root.type == bfd_link_hash_indirect)
12046 return TRUE;
12047
99877b66
AM
12048 eh = (struct ppc_link_hash_entry *) h;
12049 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12050 return TRUE;
12051
12052 eh->elf.root.type = bfd_link_hash_undefined;
12053 return TRUE;
12054}
12055
12056void
12057ppc64_elf_restore_symbols (struct bfd_link_info *info)
12058{
12059 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
12060
12061 if (htab != NULL)
12062 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
12063}
12064
60124e18
AM
12065/* What to do when ld finds relocations against symbols defined in
12066 discarded sections. */
12067
12068static unsigned int
12069ppc64_elf_action_discarded (asection *sec)
12070{
12071 if (strcmp (".opd", sec->name) == 0)
12072 return 0;
12073
12074 if (strcmp (".toc", sec->name) == 0)
12075 return 0;
12076
bce50a28
JJ
12077 if (strcmp (".toc1", sec->name) == 0)
12078 return 0;
12079
60124e18
AM
12080 return _bfd_elf_default_action_discarded (sec);
12081}
12082
5bd4f169
AM
12083/* The RELOCATE_SECTION function is called by the ELF backend linker
12084 to handle the relocations for a section.
12085
12086 The relocs are always passed as Rela structures; if the section
12087 actually uses Rel structures, the r_addend field will always be
12088 zero.
12089
12090 This function is responsible for adjust the section contents as
12091 necessary, and (if using Rela relocs and generating a
1049f94e 12092 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
12093 necessary.
12094
12095 This function does not have to worry about setting the reloc
12096 address or the reloc symbol index.
12097
12098 LOCAL_SYMS is a pointer to the swapped in local symbols.
12099
12100 LOCAL_SECTIONS is an array giving the section in the input file
12101 corresponding to the st_shndx field of each local symbol.
12102
12103 The global hash table entry for the global symbols can be found
12104 via elf_sym_hashes (input_bfd).
12105
1049f94e 12106 When generating relocatable output, this function must handle
5bd4f169
AM
12107 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
12108 going to be the section symbol corresponding to the output
12109 section, which means that the addend must be adjusted
12110 accordingly. */
12111
b34976b6 12112static bfd_boolean
4ce794b7
AM
12113ppc64_elf_relocate_section (bfd *output_bfd,
12114 struct bfd_link_info *info,
12115 bfd *input_bfd,
12116 asection *input_section,
12117 bfd_byte *contents,
12118 Elf_Internal_Rela *relocs,
12119 Elf_Internal_Sym *local_syms,
12120 asection **local_sections)
5bd4f169 12121{
65f38f15 12122 struct ppc_link_hash_table *htab;
5bd4f169
AM
12123 Elf_Internal_Shdr *symtab_hdr;
12124 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
12125 Elf_Internal_Rela *rel;
12126 Elf_Internal_Rela *relend;
411e1bfb
AM
12127 Elf_Internal_Rela outrel;
12128 bfd_byte *loc;
411e1bfb 12129 struct got_entry **local_got_ents;
5bd4f169 12130 bfd_vma TOCstart;
b34976b6
AM
12131 bfd_boolean ret = TRUE;
12132 bfd_boolean is_opd;
794e51c0
AM
12133 /* Assume 'at' branch hints. */
12134 bfd_boolean is_isa_v2 = TRUE;
4fe5ca5b 12135 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 12136
65f38f15 12137 /* Initialize howto table if needed. */
5bd4f169 12138 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
12139 ppc_howto_init ();
12140
65f38f15 12141 htab = ppc_hash_table (info);
4dfe6ac6
NC
12142 if (htab == NULL)
12143 return FALSE;
ee75fd95
AM
12144
12145 /* Don't relocate stub sections. */
12146 if (input_section->owner == htab->stub_bfd)
12147 return TRUE;
12148
0c8d6e5c 12149 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 12150
411e1bfb 12151 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 12152 TOCstart = elf_gp (output_bfd);
0ffa91dd 12153 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 12154 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 12155 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 12156
5bd4f169
AM
12157 rel = relocs;
12158 relend = relocs + input_section->reloc_count;
12159 for (; rel < relend; rel++)
12160 {
04c9666a 12161 enum elf_ppc64_reloc_type r_type;
4cc603a5 12162 bfd_vma addend, orig_addend;
5bd4f169
AM
12163 bfd_reloc_status_type r;
12164 Elf_Internal_Sym *sym;
12165 asection *sec;
039b3fef
AM
12166 struct elf_link_hash_entry *h_elf;
12167 struct ppc_link_hash_entry *h;
12168 struct ppc_link_hash_entry *fdh;
5bd4f169 12169 const char *sym_name;
0d4792f7 12170 unsigned long r_symndx, toc_symndx;
3a71aa26 12171 bfd_vma toc_addend;
f961d9dd
AM
12172 unsigned char tls_mask, tls_gd, tls_type;
12173 unsigned char sym_type;
5bd4f169 12174 bfd_vma relocation;
b34976b6
AM
12175 bfd_boolean unresolved_reloc;
12176 bfd_boolean warned;
67f0cbdb 12177 unsigned int insn;
e11840f9 12178 unsigned int mask;
721956f4
AM
12179 struct ppc_stub_hash_entry *stub_entry;
12180 bfd_vma max_br_offset;
12181 bfd_vma from;
5bd4f169 12182
4ce794b7 12183 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 12184 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
12185
12186 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12187 symbol of the previous ADDR64 reloc. The symbol gives us the
12188 proper TOC base to use. */
12189 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12190 && rel != relocs
12191 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12192 && is_opd)
12193 r_symndx = ELF64_R_SYM (rel[-1].r_info);
12194
4ce794b7
AM
12195 sym = NULL;
12196 sec = NULL;
039b3fef 12197 h_elf = NULL;
4ce794b7 12198 sym_name = NULL;
b34976b6
AM
12199 unresolved_reloc = FALSE;
12200 warned = FALSE;
4cc603a5 12201 orig_addend = rel->r_addend;
65f38f15 12202
0b13192e 12203 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
12204 {
12205 /* It's a local symbol. */
74f0fb50 12206 struct _opd_sec_data *opd;
4025353c 12207
5bd4f169
AM
12208 sym = local_syms + r_symndx;
12209 sec = local_sections[r_symndx];
26c61ae5 12210 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 12211 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 12212 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
12213 opd = get_opd_info (sec);
12214 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 12215 {
74f0fb50 12216 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
12217 if (adjust == -1)
12218 relocation = 0;
12219 else
4cc603a5
AM
12220 {
12221 /* If this is a relocation against the opd section sym
12222 and we have edited .opd, adjust the reloc addend so
12223 that ld -r and ld --emit-relocs output is correct.
12224 If it is a reloc against some other .opd symbol,
12225 then the symbol value will be adjusted later. */
12226 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12227 rel->r_addend += adjust;
12228 else
12229 relocation += adjust;
12230 }
1e2f5b6e 12231 }
5bd4f169
AM
12232 }
12233 else
12234 {
b2a8e766
AM
12235 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12236 r_symndx, symtab_hdr, sym_hashes,
039b3fef 12237 h_elf, sec, relocation,
b2a8e766 12238 unresolved_reloc, warned);
039b3fef
AM
12239 sym_name = h_elf->root.root.string;
12240 sym_type = h_elf->type;
5bd4f169 12241 }
039b3fef 12242 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 12243
ab96bf03 12244 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
12245 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12246 rel, relend,
12247 ppc64_elf_howto_table[r_type],
12248 contents);
ab96bf03
AM
12249
12250 if (info->relocatable)
12251 continue;
12252
951fd09b
AM
12253 /* TLS optimizations. Replace instruction sequences and relocs
12254 based on information we collected in tls_optimize. We edit
12255 RELOCS so that --emit-relocs will output something sensible
12256 for the final instruction stream. */
12257 tls_mask = 0;
12258 tls_gd = 0;
0d4792f7 12259 toc_symndx = 0;
727fc41e
AM
12260 if (h != NULL)
12261 tls_mask = h->tls_mask;
12262 else if (local_got_ents != NULL)
411e1bfb 12263 {
e054468f
AM
12264 struct plt_entry **local_plt = (struct plt_entry **)
12265 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 12266 unsigned char *lgot_masks = (unsigned char *)
e054468f 12267 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
12268 tls_mask = lgot_masks[r_symndx];
12269 }
12270 if (tls_mask == 0
12271 && (r_type == R_PPC64_TLS
12272 || r_type == R_PPC64_TLSGD
12273 || r_type == R_PPC64_TLSLD))
12274 {
12275 /* Check for toc tls entries. */
f961d9dd 12276 unsigned char *toc_tls;
0d4792f7 12277
727fc41e
AM
12278 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12279 &local_syms, rel, input_bfd))
12280 return FALSE;
0d4792f7 12281
727fc41e
AM
12282 if (toc_tls)
12283 tls_mask = *toc_tls;
0d4792f7
AM
12284 }
12285
12286 /* Check that tls relocs are used with tls syms, and non-tls
12287 relocs are used with non-tls syms. */
cf35638d 12288 if (r_symndx != STN_UNDEF
0d4792f7
AM
12289 && r_type != R_PPC64_NONE
12290 && (h == NULL
039b3fef
AM
12291 || h->elf.root.type == bfd_link_hash_defined
12292 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
12293 && (IS_PPC64_TLS_RELOC (r_type)
12294 != (sym_type == STT_TLS
12295 || (sym_type == STT_SECTION
12296 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 12297 {
727fc41e
AM
12298 if (tls_mask != 0
12299 && (r_type == R_PPC64_TLS
12300 || r_type == R_PPC64_TLSGD
12301 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
12302 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12303 ;
12304 else
25f53a85 12305 info->callbacks->einfo
1d483afe 12306 (!IS_PPC64_TLS_RELOC (r_type)
8de848d8
AM
12307 ? _("%P: %H: %s used with TLS symbol %s\n")
12308 : _("%P: %H: %s used with non-TLS symbol %s\n"),
25f53a85 12309 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
12310 ppc64_elf_howto_table[r_type]->name,
12311 sym_name);
411e1bfb
AM
12312 }
12313
12314 /* Ensure reloc mapping code below stays sane. */
12315 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12316 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12317 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12318 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12319 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12320 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12321 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12322 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12323 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12324 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12325 abort ();
0d4792f7 12326
411e1bfb
AM
12327 switch (r_type)
12328 {
12329 default:
411e1bfb
AM
12330 break;
12331
ba761f19
AM
12332 case R_PPC64_LO_DS_OPT:
12333 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12334 if ((insn & (0x3f << 26)) != 58u << 26)
12335 abort ();
12336 insn += (14u << 26) - (58u << 26);
12337 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12338 r_type = R_PPC64_TOC16_LO;
12339 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12340 break;
12341
411e1bfb
AM
12342 case R_PPC64_TOC16:
12343 case R_PPC64_TOC16_LO:
12344 case R_PPC64_TOC16_DS:
12345 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
12346 {
12347 /* Check for toc tls entries. */
f961d9dd 12348 unsigned char *toc_tls;
951fd09b 12349 int retval;
411e1bfb 12350
3a71aa26
AM
12351 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12352 &local_syms, rel, input_bfd);
951fd09b 12353 if (retval == 0)
411e1bfb
AM
12354 return FALSE;
12355
12356 if (toc_tls)
12357 {
951fd09b 12358 tls_mask = *toc_tls;
411e1bfb
AM
12359 if (r_type == R_PPC64_TOC16_DS
12360 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
12361 {
12362 if (tls_mask != 0
12363 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12364 goto toctprel;
12365 }
411e1bfb 12366 else
951fd09b
AM
12367 {
12368 /* If we found a GD reloc pair, then we might be
12369 doing a GD->IE transition. */
12370 if (retval == 2)
12371 {
12372 tls_gd = TLS_TPRELGD;
12373 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12374 goto tls_ldgd_opt;
951fd09b
AM
12375 }
12376 else if (retval == 3)
12377 {
12378 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 12379 goto tls_ldgd_opt;
951fd09b
AM
12380 }
12381 }
411e1bfb
AM
12382 }
12383 }
12384 break;
12385
9d6ded02
AM
12386 case R_PPC64_GOT_TPREL16_HI:
12387 case R_PPC64_GOT_TPREL16_HA:
12388 if (tls_mask != 0
12389 && (tls_mask & TLS_TPREL) == 0)
12390 {
12391 rel->r_offset -= d_offset;
12392 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12393 r_type = R_PPC64_NONE;
12394 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12395 }
12396 break;
12397
411e1bfb
AM
12398 case R_PPC64_GOT_TPREL16_DS:
12399 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
12400 if (tls_mask != 0
12401 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12402 {
81407a69 12403 toctprel:
4fe5ca5b 12404 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
12405 insn &= 31 << 21;
12406 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 12407 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 12408 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
12409 if (toc_symndx != 0)
12410 {
12411 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12412 rel->r_addend = toc_addend;
0d4792f7
AM
12413 /* We changed the symbol. Start over in order to
12414 get h, sym, sec etc. right. */
12415 rel--;
12416 continue;
12417 }
12418 else
12419 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12420 }
12421 break;
12422
12423 case R_PPC64_TLS:
951fd09b
AM
12424 if (tls_mask != 0
12425 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12426 {
411e1bfb 12427 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
12428 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12429 if (insn == 0)
411e1bfb 12430 abort ();
411e1bfb 12431 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 12432 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
12433 PPC64_TPREL16_LO which is at low-order half-word. */
12434 rel->r_offset += d_offset;
0d4792f7
AM
12435 r_type = R_PPC64_TPREL16_LO;
12436 if (toc_symndx != 0)
12437 {
12438 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12439 rel->r_addend = toc_addend;
0d4792f7
AM
12440 /* We changed the symbol. Start over in order to
12441 get h, sym, sec etc. right. */
12442 rel--;
12443 continue;
12444 }
12445 else
12446 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12447 }
12448 break;
12449
411e1bfb
AM
12450 case R_PPC64_GOT_TLSGD16_HI:
12451 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
12452 tls_gd = TLS_TPRELGD;
12453 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12454 goto tls_gdld_hi;
12455 break;
12456
411e1bfb
AM
12457 case R_PPC64_GOT_TLSLD16_HI:
12458 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12459 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 12460 {
951fd09b
AM
12461 tls_gdld_hi:
12462 if ((tls_mask & tls_gd) != 0)
12463 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12464 + R_PPC64_GOT_TPREL16_DS);
12465 else
411e1bfb 12466 {
4fe5ca5b 12467 rel->r_offset -= d_offset;
727ac201 12468 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 12469 r_type = R_PPC64_NONE;
411e1bfb 12470 }
951fd09b 12471 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12472 }
12473 break;
12474
951fd09b
AM
12475 case R_PPC64_GOT_TLSGD16:
12476 case R_PPC64_GOT_TLSGD16_LO:
12477 tls_gd = TLS_TPRELGD;
12478 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12479 goto tls_ldgd_opt;
951fd09b 12480 break;
411e1bfb 12481
951fd09b
AM
12482 case R_PPC64_GOT_TLSLD16:
12483 case R_PPC64_GOT_TLSLD16_LO:
12484 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12485 {
3a71aa26 12486 unsigned int insn1, insn2, insn3;
102890f0
AM
12487 bfd_vma offset;
12488
12489 tls_ldgd_opt:
727fc41e
AM
12490 offset = (bfd_vma) -1;
12491 /* If not using the newer R_PPC64_TLSGD/LD to mark
12492 __tls_get_addr calls, we must trust that the call
12493 stays with its arg setup insns, ie. that the next
12494 reloc is the __tls_get_addr call associated with
12495 the current reloc. Edit both insns. */
12496 if (input_section->has_tls_get_addr_call
12497 && rel + 1 < relend
12498 && branch_reloc_hash_match (input_bfd, rel + 1,
12499 htab->tls_get_addr,
12500 htab->tls_get_addr_fd))
12501 offset = rel[1].r_offset;
102890f0 12502 if ((tls_mask & tls_gd) != 0)
411e1bfb 12503 {
102890f0 12504 /* IE */
3a71aa26
AM
12505 insn1 = bfd_get_32 (output_bfd,
12506 contents + rel->r_offset - d_offset);
102890f0
AM
12507 insn1 &= (1 << 26) - (1 << 2);
12508 insn1 |= 58 << 26; /* ld */
12509 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 12510 if (offset != (bfd_vma) -1)
f58d5a2d 12511 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
12512 if ((tls_mask & TLS_EXPLICIT) == 0)
12513 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12514 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 12515 else
102890f0
AM
12516 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12517 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12518 }
12519 else
12520 {
12521 /* LE */
12522 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12523 insn2 = 0x38630000; /* addi 3,3,0 */
12524 if (tls_gd == 0)
951fd09b 12525 {
102890f0 12526 /* Was an LD reloc. */
1d483afe
AM
12527 if (toc_symndx)
12528 sec = local_sections[toc_symndx];
12529 for (r_symndx = 0;
12530 r_symndx < symtab_hdr->sh_info;
12531 r_symndx++)
12532 if (local_sections[r_symndx] == sec)
12533 break;
12534 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12535 r_symndx = STN_UNDEF;
102890f0 12536 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12537 if (r_symndx != STN_UNDEF)
1d483afe
AM
12538 rel->r_addend -= (local_syms[r_symndx].st_value
12539 + sec->output_offset
12540 + sec->output_section->vma);
951fd09b 12541 }
102890f0 12542 else if (toc_symndx != 0)
3a71aa26
AM
12543 {
12544 r_symndx = toc_symndx;
12545 rel->r_addend = toc_addend;
12546 }
102890f0
AM
12547 r_type = R_PPC64_TPREL16_HA;
12548 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
12549 if (offset != (bfd_vma) -1)
12550 {
12551 rel[1].r_info = ELF64_R_INFO (r_symndx,
12552 R_PPC64_TPREL16_LO);
12553 rel[1].r_offset = offset + d_offset;
12554 rel[1].r_addend = rel->r_addend;
12555 }
102890f0 12556 }
3a71aa26
AM
12557 bfd_put_32 (output_bfd, insn1,
12558 contents + rel->r_offset - d_offset);
727fc41e
AM
12559 if (offset != (bfd_vma) -1)
12560 {
12561 insn3 = bfd_get_32 (output_bfd,
12562 contents + offset + 4);
12563 if (insn3 == NOP
12564 || insn3 == CROR_151515 || insn3 == CROR_313131)
12565 {
12566 rel[1].r_offset += 4;
12567 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12568 insn2 = NOP;
12569 }
12570 bfd_put_32 (output_bfd, insn2, contents + offset);
12571 }
12572 if ((tls_mask & tls_gd) == 0
12573 && (tls_gd == 0 || toc_symndx != 0))
12574 {
12575 /* We changed the symbol. Start over in order
12576 to get h, sym, sec etc. right. */
12577 rel--;
12578 continue;
12579 }
12580 }
12581 break;
12582
12583 case R_PPC64_TLSGD:
12584 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12585 {
12586 unsigned int insn2, insn3;
12587 bfd_vma offset = rel->r_offset;
12588
12589 if ((tls_mask & TLS_TPRELGD) != 0)
12590 {
12591 /* IE */
12592 r_type = R_PPC64_NONE;
12593 insn2 = 0x7c636a14; /* add 3,3,13 */
12594 }
12595 else
12596 {
12597 /* LE */
12598 if (toc_symndx != 0)
12599 {
12600 r_symndx = toc_symndx;
12601 rel->r_addend = toc_addend;
12602 }
12603 r_type = R_PPC64_TPREL16_LO;
12604 rel->r_offset = offset + d_offset;
12605 insn2 = 0x38630000; /* addi 3,3,0 */
12606 }
12607 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12608 /* Zap the reloc on the _tls_get_addr call too. */
12609 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12610 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
12611 insn3 = bfd_get_32 (output_bfd,
12612 contents + offset + 4);
102890f0
AM
12613 if (insn3 == NOP
12614 || insn3 == CROR_151515 || insn3 == CROR_313131)
12615 {
727fc41e 12616 rel->r_offset += 4;
3a71aa26
AM
12617 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12618 insn2 = NOP;
102890f0 12619 }
102890f0 12620 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 12621 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 12622 {
102890f0
AM
12623 rel--;
12624 continue;
411e1bfb 12625 }
411e1bfb 12626 }
411e1bfb
AM
12627 break;
12628
727fc41e
AM
12629 case R_PPC64_TLSLD:
12630 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12631 {
12632 unsigned int insn2, insn3;
12633 bfd_vma offset = rel->r_offset;
12634
12635 if (toc_symndx)
12636 sec = local_sections[toc_symndx];
12637 for (r_symndx = 0;
12638 r_symndx < symtab_hdr->sh_info;
12639 r_symndx++)
12640 if (local_sections[r_symndx] == sec)
12641 break;
12642 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12643 r_symndx = STN_UNDEF;
727fc41e 12644 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12645 if (r_symndx != STN_UNDEF)
727fc41e
AM
12646 rel->r_addend -= (local_syms[r_symndx].st_value
12647 + sec->output_offset
12648 + sec->output_section->vma);
12649
12650 r_type = R_PPC64_TPREL16_LO;
12651 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12652 rel->r_offset = offset + d_offset;
12653 /* Zap the reloc on the _tls_get_addr call too. */
12654 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12655 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
12656 insn2 = 0x38630000; /* addi 3,3,0 */
12657 insn3 = bfd_get_32 (output_bfd,
12658 contents + offset + 4);
12659 if (insn3 == NOP
12660 || insn3 == CROR_151515 || insn3 == CROR_313131)
12661 {
12662 rel->r_offset += 4;
12663 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12664 insn2 = NOP;
12665 }
12666 bfd_put_32 (output_bfd, insn2, contents + offset);
12667 rel--;
12668 continue;
12669 }
12670 break;
12671
411e1bfb 12672 case R_PPC64_DTPMOD64:
951fd09b
AM
12673 if (rel + 1 < relend
12674 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12675 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 12676 {
951fd09b
AM
12677 if ((tls_mask & TLS_GD) == 0)
12678 {
12679 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12680 if ((tls_mask & TLS_TPRELGD) != 0)
12681 r_type = R_PPC64_TPREL64;
12682 else
12683 {
4ce794b7 12684 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
12685 r_type = R_PPC64_NONE;
12686 }
12687 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12688 }
12689 }
12690 else
12691 {
12692 if ((tls_mask & TLS_LD) == 0)
411e1bfb 12693 {
4ce794b7 12694 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 12695 r_type = R_PPC64_NONE;
951fd09b 12696 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 12697 }
411e1bfb
AM
12698 }
12699 break;
12700
12701 case R_PPC64_TPREL64:
951fd09b 12702 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
12703 {
12704 r_type = R_PPC64_NONE;
12705 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12706 }
12707 break;
12708 }
12709
12710 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 12711 insn = 0;
b25116a9
AM
12712 max_br_offset = 1 << 25;
12713 addend = rel->r_addend;
65f38f15 12714 switch (r_type)
5bd4f169
AM
12715 {
12716 default:
65f38f15 12717 break;
5bd4f169 12718
3b421ab3
AM
12719 case R_PPC64_TOCSAVE:
12720 if (relocation + addend == (rel->r_offset
12721 + input_section->output_offset
12722 + input_section->output_section->vma)
12723 && tocsave_find (htab, NO_INSERT,
12724 &local_syms, rel, input_bfd))
12725 {
12726 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
12727 if (insn == NOP
12728 || insn == CROR_151515 || insn == CROR_313131)
12729 bfd_put_32 (input_bfd, STD_R2_40R1,
12730 contents + rel->r_offset);
12731 }
12732 break;
12733
65f38f15
AM
12734 /* Branch taken prediction relocations. */
12735 case R_PPC64_ADDR14_BRTAKEN:
12736 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
12737 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12738 /* Fall thru. */
65f38f15 12739
86c76c7b 12740 /* Branch not taken prediction relocations. */
65f38f15
AM
12741 case R_PPC64_ADDR14_BRNTAKEN:
12742 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
12743 insn |= bfd_get_32 (output_bfd,
12744 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 12745 /* Fall thru. */
86c76c7b 12746
b25116a9
AM
12747 case R_PPC64_REL14:
12748 max_br_offset = 1 << 15;
12749 /* Fall thru. */
5bd4f169 12750
65f38f15 12751 case R_PPC64_REL24:
ad8e1ba5
AM
12752 /* Calls to functions with a different TOC, such as calls to
12753 shared objects, need to alter the TOC pointer. This is
12754 done using a linkage stub. A REL24 branching to these
12755 linkage stubs needs to be followed by a nop, as the nop
12756 will be replaced with an instruction to restore the TOC
12757 base pointer. */
8387904d 12758 fdh = h;
b31867b6
AM
12759 if (h != NULL
12760 && h->oh != NULL
12761 && h->oh->is_func_descriptor)
12762 fdh = ppc_follow_link (h->oh);
6abec6bc
AM
12763 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12764 if (stub_entry != NULL
ad8e1ba5 12765 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 12766 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
12767 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12768 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 12769 {
b25116a9 12770 bfd_boolean can_plt_call = FALSE;
721956f4 12771
eea6121a 12772 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 12773 {
b25116a9
AM
12774 unsigned long nop;
12775 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12776 if (nop == NOP
12777 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 12778 {
a7f2871e
AM
12779 if (h != NULL
12780 && (h == htab->tls_get_addr_fd
12781 || h == htab->tls_get_addr)
12782 && !htab->no_tls_get_addr_opt)
12783 {
12784 /* Special stub used, leave nop alone. */
12785 }
12786 else
12787 bfd_put_32 (input_bfd, LD_R2_40R1,
12788 contents + rel->r_offset + 4);
b25116a9 12789 can_plt_call = TRUE;
41bd81ab 12790 }
5bd4f169 12791 }
721956f4
AM
12792
12793 if (!can_plt_call)
12794 {
794e51c0
AM
12795 if (stub_entry->stub_type == ppc_stub_plt_call
12796 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
ad8e1ba5
AM
12797 {
12798 /* If this is a plain branch rather than a branch
4fa1c237
AM
12799 and link, don't require a nop. However, don't
12800 allow tail calls in a shared library as they
12801 will result in r2 being corrupted. */
b25116a9
AM
12802 unsigned long br;
12803 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 12804 if (info->executable && (br & 1) == 0)
b25116a9 12805 can_plt_call = TRUE;
15b552e9
AM
12806 else
12807 stub_entry = NULL;
ad8e1ba5 12808 }
6ab189d5 12809 else if (h != NULL
039b3fef 12810 && strcmp (h->elf.root.root.string,
6ab189d5
AM
12811 ".__libc_start_main") == 0)
12812 {
12813 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 12814 can_plt_call = TRUE;
6ab189d5 12815 }
ad8e1ba5
AM
12816 else
12817 {
12818 if (strcmp (input_section->output_section->name,
12819 ".init") == 0
12820 || strcmp (input_section->output_section->name,
12821 ".fini") == 0)
25f53a85 12822 info->callbacks->einfo
8de848d8 12823 (_("%P: %H: automatic multiple TOCs "
ad8e1ba5 12824 "not supported using your crt files; "
25f53a85
AM
12825 "recompile with -mminimal-toc or upgrade gcc\n"),
12826 input_bfd, input_section, rel->r_offset);
ad8e1ba5 12827 else
25f53a85 12828 info->callbacks->einfo
8de848d8 12829 (_("%P: %H: sibling call optimization to `%s' "
ad8e1ba5
AM
12830 "does not allow automatic multiple TOCs; "
12831 "recompile with -mminimal-toc or "
12832 "-fno-optimize-sibling-calls, "
25f53a85
AM
12833 "or make `%s' extern\n"),
12834 input_bfd, input_section, rel->r_offset,
ad8e1ba5
AM
12835 sym_name,
12836 sym_name);
12837 bfd_set_error (bfd_error_bad_value);
12838 ret = FALSE;
12839 }
721956f4
AM
12840 }
12841
b25116a9 12842 if (can_plt_call
794e51c0
AM
12843 && (stub_entry->stub_type == ppc_stub_plt_call
12844 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
12845 unresolved_reloc = FALSE;
12846 }
12847
6abec6bc
AM
12848 if ((stub_entry == NULL
12849 || stub_entry->stub_type == ppc_stub_long_branch
12850 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
12851 && get_opd_info (sec) != NULL)
12852 {
12853 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
12854 bfd_vma off = (relocation + addend
12855 - sec->output_section->vma
12856 - sec->output_offset);
8387904d
AM
12857 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12858 if (dest != (bfd_vma) -1)
12859 {
12860 relocation = dest;
12861 addend = 0;
12862 }
12863 }
12864
b25116a9
AM
12865 /* If the branch is out of reach we ought to have a long
12866 branch stub. */
12867 from = (rel->r_offset
12868 + input_section->output_offset
12869 + input_section->output_section->vma);
12870
6abec6bc
AM
12871 if (stub_entry != NULL
12872 && (stub_entry->stub_type == ppc_stub_long_branch
12873 || stub_entry->stub_type == ppc_stub_plt_branch)
12874 && (r_type == R_PPC64_ADDR14_BRTAKEN
12875 || r_type == R_PPC64_ADDR14_BRNTAKEN
12876 || (relocation + addend - from + max_br_offset
12877 < 2 * max_br_offset)))
12878 /* Don't use the stub if this branch is in range. */
12879 stub_entry = NULL;
b25116a9
AM
12880
12881 if (stub_entry != NULL)
12882 {
12883 /* Munge up the value and addend so that we call the stub
12884 rather than the procedure directly. */
12885 relocation = (stub_entry->stub_offset
12886 + stub_entry->stub_sec->output_offset
12887 + stub_entry->stub_sec->output_section->vma);
12888 addend = 0;
3b421ab3 12889
794e51c0
AM
12890 if ((stub_entry->stub_type == ppc_stub_plt_call
12891 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
12892 && (ALWAYS_EMIT_R2SAVE
12893 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
12894 && rel + 1 < relend
12895 && rel[1].r_offset == rel->r_offset + 4
12896 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
12897 relocation += 4;
b25116a9
AM
12898 }
12899
12900 if (insn != 0)
12901 {
794e51c0 12902 if (is_isa_v2)
721956f4 12903 {
b25116a9
AM
12904 /* Set 'a' bit. This is 0b00010 in BO field for branch
12905 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12906 for branch on CTR insns (BO == 1a00t or 1a01t). */
12907 if ((insn & (0x14 << 21)) == (0x04 << 21))
12908 insn |= 0x02 << 21;
12909 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12910 insn |= 0x08 << 21;
12911 else
12912 break;
12913 }
12914 else
12915 {
12916 /* Invert 'y' bit if not the default. */
4cc603a5 12917 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 12918 insn ^= 0x01 << 21;
721956f4 12919 }
b25116a9
AM
12920
12921 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 12922 }
e86ce104 12923
06da1e8e
AM
12924 /* NOP out calls to undefined weak functions.
12925 We can thus call a weak function without first
12926 checking whether the function is defined. */
b25116a9 12927 else if (h != NULL
039b3fef 12928 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 12929 && h->elf.dynindx == -1
b25116a9
AM
12930 && r_type == R_PPC64_REL24
12931 && relocation == 0
4cc603a5 12932 && addend == 0)
e86ce104 12933 {
06da1e8e
AM
12934 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12935 continue;
e86ce104 12936 }
65f38f15
AM
12937 break;
12938 }
5bd4f169 12939
65f38f15 12940 /* Set `addend'. */
411e1bfb 12941 tls_type = 0;
65f38f15
AM
12942 switch (r_type)
12943 {
12944 default:
25f53a85 12945 info->callbacks->einfo
8de848d8 12946 (_("%P: %B: unknown relocation type %d for symbol %s\n"),
d003868e 12947 input_bfd, (int) r_type, sym_name);
5bd4f169 12948
65f38f15 12949 bfd_set_error (bfd_error_bad_value);
b34976b6 12950 ret = FALSE;
65f38f15 12951 continue;
5bd4f169 12952
65f38f15 12953 case R_PPC64_NONE:
411e1bfb 12954 case R_PPC64_TLS:
727fc41e
AM
12955 case R_PPC64_TLSGD:
12956 case R_PPC64_TLSLD:
3b421ab3 12957 case R_PPC64_TOCSAVE:
04c9666a
AM
12958 case R_PPC64_GNU_VTINHERIT:
12959 case R_PPC64_GNU_VTENTRY:
65f38f15 12960 continue;
5bd4f169
AM
12961
12962 /* GOT16 relocations. Like an ADDR16 using the symbol's
12963 address in the GOT as relocation value instead of the
411e1bfb 12964 symbol's value itself. Also, create a GOT entry for the
5bd4f169 12965 symbol and put the symbol value there. */
411e1bfb
AM
12966 case R_PPC64_GOT_TLSGD16:
12967 case R_PPC64_GOT_TLSGD16_LO:
12968 case R_PPC64_GOT_TLSGD16_HI:
12969 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 12970 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
12971 goto dogot;
12972
12973 case R_PPC64_GOT_TLSLD16:
12974 case R_PPC64_GOT_TLSLD16_LO:
12975 case R_PPC64_GOT_TLSLD16_HI:
12976 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12977 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
12978 goto dogot;
12979
12980 case R_PPC64_GOT_TPREL16_DS:
12981 case R_PPC64_GOT_TPREL16_LO_DS:
12982 case R_PPC64_GOT_TPREL16_HI:
12983 case R_PPC64_GOT_TPREL16_HA:
12984 tls_type = TLS_TLS | TLS_TPREL;
12985 goto dogot;
12986
12987 case R_PPC64_GOT_DTPREL16_DS:
12988 case R_PPC64_GOT_DTPREL16_LO_DS:
12989 case R_PPC64_GOT_DTPREL16_HI:
12990 case R_PPC64_GOT_DTPREL16_HA:
12991 tls_type = TLS_TLS | TLS_DTPREL;
12992 goto dogot;
12993
65f38f15
AM
12994 case R_PPC64_GOT16:
12995 case R_PPC64_GOT16_LO:
12996 case R_PPC64_GOT16_HI:
12997 case R_PPC64_GOT16_HA:
12998 case R_PPC64_GOT16_DS:
12999 case R_PPC64_GOT16_LO_DS:
411e1bfb 13000 dogot:
5bd4f169
AM
13001 {
13002 /* Relocation is to the entry for this symbol in the global
13003 offset table. */
e717da7e 13004 asection *got;
d881513a 13005 bfd_vma *offp;
5bd4f169 13006 bfd_vma off;
d881513a 13007 unsigned long indx = 0;
927be08e 13008 struct got_entry *ent;
65f38f15 13009
d881513a
AM
13010 if (tls_type == (TLS_TLS | TLS_LD)
13011 && (h == NULL
f5385ebf 13012 || !h->elf.def_dynamic))
927be08e 13013 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 13014 else
5bd4f169 13015 {
5bd4f169 13016
d881513a
AM
13017 if (h != NULL)
13018 {
13019 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
13020 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13021 &h->elf)
d881513a 13022 || (info->shared
726d3ab0 13023 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
13024 /* This is actually a static link, or it is a
13025 -Bsymbolic link and the symbol is defined
13026 locally, or the symbol was forced to be local
13027 because of a version file. */
13028 ;
13029 else
13030 {
039b3fef 13031 indx = h->elf.dynindx;
d881513a
AM
13032 unresolved_reloc = FALSE;
13033 }
039b3fef 13034 ent = h->elf.got.glist;
d881513a 13035 }
411e1bfb 13036 else
5bd4f169 13037 {
d881513a
AM
13038 if (local_got_ents == NULL)
13039 abort ();
13040 ent = local_got_ents[r_symndx];
5bd4f169 13041 }
d881513a
AM
13042
13043 for (; ent != NULL; ent = ent->next)
4cc603a5 13044 if (ent->addend == orig_addend
e717da7e 13045 && ent->owner == input_bfd
d881513a
AM
13046 && ent->tls_type == tls_type)
13047 break;
5bd4f169 13048 }
411e1bfb 13049
927be08e
AM
13050 if (ent == NULL)
13051 abort ();
13052 if (ent->is_indirect)
13053 ent = ent->got.ent;
13054 offp = &ent->got.offset;
13055 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
13056 if (got == NULL)
13057 abort ();
13058
411e1bfb
AM
13059 /* The offset must always be a multiple of 8. We use the
13060 least significant bit to record whether we have already
13061 processed this entry. */
d881513a 13062 off = *offp;
411e1bfb
AM
13063 if ((off & 1) != 0)
13064 off &= ~1;
5bd4f169
AM
13065 else
13066 {
411e1bfb
AM
13067 /* Generate relocs for the dynamic linker, except in
13068 the case of TLSLD where we'll use one entry per
13069 module. */
25f23106
AM
13070 asection *relgot;
13071 bfd_boolean ifunc;
e717da7e 13072
d881513a 13073 *offp = off | 1;
25f23106
AM
13074 relgot = NULL;
13075 ifunc = (h != NULL
13076 ? h->elf.type == STT_GNU_IFUNC
13077 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
4e795f50 13078 if ((info->shared || indx != 0)
927be08e
AM
13079 && (h == NULL
13080 || (tls_type == (TLS_TLS | TLS_LD)
13081 && !h->elf.def_dynamic)
039b3fef
AM
13082 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13083 || h->elf.root.type != bfd_link_hash_undefweak))
927be08e 13084 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106
AM
13085 else if (ifunc)
13086 relgot = htab->reliplt;
13087 if (relgot != NULL)
5bd4f169 13088 {
e717da7e
AM
13089 outrel.r_offset = (got->output_section->vma
13090 + got->output_offset
411e1bfb 13091 + off);
4cc603a5 13092 outrel.r_addend = addend;
d881513a 13093 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 13094 {
411e1bfb 13095 outrel.r_addend = 0;
e515b051 13096 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
13097 if (tls_type == (TLS_TLS | TLS_GD))
13098 {
e717da7e
AM
13099 loc = relgot->contents;
13100 loc += (relgot->reloc_count++
d881513a
AM
13101 * sizeof (Elf64_External_Rela));
13102 bfd_elf64_swap_reloca_out (output_bfd,
13103 &outrel, loc);
e515b051 13104 outrel.r_offset += 8;
4cc603a5 13105 outrel.r_addend = addend;
d881513a
AM
13106 outrel.r_info
13107 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 13108 }
411e1bfb 13109 }
951fd09b 13110 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 13111 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 13112 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13113 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
13114 else if (indx != 0)
13115 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13116 else
81407a69 13117 {
25f23106
AM
13118 if (ifunc)
13119 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13120 else
13121 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
13122
13123 /* Write the .got section contents for the sake
13124 of prelink. */
e717da7e 13125 loc = got->contents + off;
23fbd6fa
JJ
13126 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13127 loc);
81407a69 13128 }
81407a69
AM
13129
13130 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
13131 {
13132 outrel.r_addend += relocation;
13133 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 13134 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 13135 }
e717da7e
AM
13136 loc = relgot->contents;
13137 loc += (relgot->reloc_count++
411e1bfb
AM
13138 * sizeof (Elf64_External_Rela));
13139 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13140 }
13141
ad8e1ba5 13142 /* Init the .got section contents here if we're not
81407a69 13143 emitting a reloc. */
d881513a 13144 else
411e1bfb 13145 {
4cc603a5 13146 relocation += addend;
7b609f53
AM
13147 if (tls_type == (TLS_TLS | TLS_LD))
13148 relocation = 1;
13149 else if (tls_type != 0)
411e1bfb 13150 {
e1918d23 13151 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 13152 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13153 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 13154
7b609f53
AM
13155 if (tls_type == (TLS_TLS | TLS_GD))
13156 {
13157 bfd_put_64 (output_bfd, relocation,
e717da7e 13158 got->contents + off + 8);
7b609f53
AM
13159 relocation = 1;
13160 }
411e1bfb 13161 }
7b609f53 13162
411e1bfb 13163 bfd_put_64 (output_bfd, relocation,
e717da7e 13164 got->contents + off);
5bd4f169
AM
13165 }
13166 }
13167
65f38f15
AM
13168 if (off >= (bfd_vma) -2)
13169 abort ();
13170
bf102f86
AM
13171 relocation = got->output_section->vma + got->output_offset + off;
13172 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 13173 }
65f38f15
AM
13174 break;
13175
13176 case R_PPC64_PLT16_HA:
13177 case R_PPC64_PLT16_HI:
13178 case R_PPC64_PLT16_LO:
13179 case R_PPC64_PLT32:
13180 case R_PPC64_PLT64:
13181 /* Relocation is to the entry for this symbol in the
13182 procedure linkage table. */
13183
13184 /* Resolve a PLT reloc against a local symbol directly,
13185 without using the procedure linkage table. */
13186 if (h == NULL)
13187 break;
13188
411e1bfb
AM
13189 /* It's possible that we didn't make a PLT entry for this
13190 symbol. This happens when statically linking PIC code,
13191 or when using -Bsymbolic. Go find a match if there is a
13192 PLT entry. */
4ce794b7 13193 if (htab->plt != NULL)
65f38f15 13194 {
411e1bfb 13195 struct plt_entry *ent;
039b3fef 13196 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 13197 if (ent->addend == orig_addend
411e1bfb
AM
13198 && ent->plt.offset != (bfd_vma) -1)
13199 {
4ce794b7
AM
13200 relocation = (htab->plt->output_section->vma
13201 + htab->plt->output_offset
411e1bfb
AM
13202 + ent->plt.offset);
13203 unresolved_reloc = FALSE;
13204 }
65f38f15 13205 }
65f38f15 13206 break;
5bd4f169 13207
0b13192e
AM
13208 case R_PPC64_TOC:
13209 /* Relocation value is TOC base. */
13210 relocation = TOCstart;
cf35638d 13211 if (r_symndx == STN_UNDEF)
0b13192e 13212 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
13213 else if (unresolved_reloc)
13214 ;
13215 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
13216 relocation += htab->stub_group[sec->id].toc_off;
13217 else
13218 unresolved_reloc = TRUE;
ab96bf03 13219 goto dodyn;
0b13192e 13220
5bd4f169
AM
13221 /* TOC16 relocs. We want the offset relative to the TOC base,
13222 which is the address of the start of the TOC plus 0x8000.
13223 The TOC consists of sections .got, .toc, .tocbss, and .plt,
13224 in this order. */
65f38f15
AM
13225 case R_PPC64_TOC16:
13226 case R_PPC64_TOC16_LO:
13227 case R_PPC64_TOC16_HI:
13228 case R_PPC64_TOC16_DS:
13229 case R_PPC64_TOC16_LO_DS:
13230 case R_PPC64_TOC16_HA:
ad8e1ba5 13231 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
13232 break;
13233
13234 /* Relocate against the beginning of the section. */
65f38f15
AM
13235 case R_PPC64_SECTOFF:
13236 case R_PPC64_SECTOFF_LO:
13237 case R_PPC64_SECTOFF_HI:
13238 case R_PPC64_SECTOFF_DS:
13239 case R_PPC64_SECTOFF_LO_DS:
13240 case R_PPC64_SECTOFF_HA:
4ce794b7 13241 if (sec != NULL)
65f38f15 13242 addend -= sec->output_section->vma;
5bd4f169
AM
13243 break;
13244
25f23106
AM
13245 case R_PPC64_REL16:
13246 case R_PPC64_REL16_LO:
13247 case R_PPC64_REL16_HI:
13248 case R_PPC64_REL16_HA:
13249 break;
13250
721956f4
AM
13251 case R_PPC64_REL14:
13252 case R_PPC64_REL14_BRNTAKEN:
13253 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
13254 case R_PPC64_REL24:
13255 break;
13256
411e1bfb
AM
13257 case R_PPC64_TPREL16:
13258 case R_PPC64_TPREL16_LO:
13259 case R_PPC64_TPREL16_HI:
13260 case R_PPC64_TPREL16_HA:
13261 case R_PPC64_TPREL16_DS:
13262 case R_PPC64_TPREL16_LO_DS:
13263 case R_PPC64_TPREL16_HIGHER:
13264 case R_PPC64_TPREL16_HIGHERA:
13265 case R_PPC64_TPREL16_HIGHEST:
13266 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
13267 if (h != NULL
13268 && h->elf.root.type == bfd_link_hash_undefweak
13269 && h->elf.dynindx == -1)
13270 {
13271 /* Make this relocation against an undefined weak symbol
13272 resolve to zero. This is really just a tweak, since
13273 code using weak externs ought to check that they are
13274 defined before using them. */
13275 bfd_byte *p = contents + rel->r_offset - d_offset;
13276
13277 insn = bfd_get_32 (output_bfd, p);
13278 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13279 if (insn != 0)
13280 bfd_put_32 (output_bfd, insn, p);
13281 break;
13282 }
e1918d23 13283 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13284 if (info->shared)
13285 /* The TPREL16 relocs shouldn't really be used in shared
13286 libs as they will result in DT_TEXTREL being set, but
13287 support them anyway. */
13288 goto dodyn;
13289 break;
13290
13291 case R_PPC64_DTPREL16:
13292 case R_PPC64_DTPREL16_LO:
13293 case R_PPC64_DTPREL16_HI:
13294 case R_PPC64_DTPREL16_HA:
13295 case R_PPC64_DTPREL16_DS:
13296 case R_PPC64_DTPREL16_LO_DS:
13297 case R_PPC64_DTPREL16_HIGHER:
13298 case R_PPC64_DTPREL16_HIGHERA:
13299 case R_PPC64_DTPREL16_HIGHEST:
13300 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 13301 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13302 break;
13303
e515b051
AM
13304 case R_PPC64_DTPMOD64:
13305 relocation = 1;
13306 addend = 0;
13307 goto dodyn;
13308
411e1bfb 13309 case R_PPC64_TPREL64:
e1918d23 13310 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13311 goto dodyn;
13312
13313 case R_PPC64_DTPREL64:
e1918d23 13314 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13315 /* Fall thru */
13316
65f38f15
AM
13317 /* Relocations that may need to be propagated if this is a
13318 dynamic object. */
04c9666a 13319 case R_PPC64_REL30:
65f38f15
AM
13320 case R_PPC64_REL32:
13321 case R_PPC64_REL64:
13322 case R_PPC64_ADDR14:
13323 case R_PPC64_ADDR14_BRNTAKEN:
13324 case R_PPC64_ADDR14_BRTAKEN:
13325 case R_PPC64_ADDR16:
13326 case R_PPC64_ADDR16_DS:
13327 case R_PPC64_ADDR16_HA:
13328 case R_PPC64_ADDR16_HI:
13329 case R_PPC64_ADDR16_HIGHER:
13330 case R_PPC64_ADDR16_HIGHERA:
13331 case R_PPC64_ADDR16_HIGHEST:
13332 case R_PPC64_ADDR16_HIGHESTA:
13333 case R_PPC64_ADDR16_LO:
13334 case R_PPC64_ADDR16_LO_DS:
13335 case R_PPC64_ADDR24:
65f38f15
AM
13336 case R_PPC64_ADDR32:
13337 case R_PPC64_ADDR64:
13338 case R_PPC64_UADDR16:
13339 case R_PPC64_UADDR32:
13340 case R_PPC64_UADDR64:
411e1bfb 13341 dodyn:
5d1634d7 13342 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
13343 break;
13344
41bd81ab
AM
13345 if (NO_OPD_RELOCS && is_opd)
13346 break;
13347
65f38f15 13348 if ((info->shared
4e795f50 13349 && (h == NULL
039b3fef
AM
13350 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13351 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 13352 && (must_be_dyn_reloc (info, r_type)
039b3fef 13353 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
13354 || (ELIMINATE_COPY_RELOCS
13355 && !info->shared
65f38f15 13356 && h != NULL
039b3fef 13357 && h->elf.dynindx != -1
f5385ebf 13358 && !h->elf.non_got_ref
25f23106
AM
13359 && !h->elf.def_regular)
13360 || (!info->shared
13361 && (h != NULL
13362 ? h->elf.type == STT_GNU_IFUNC
13363 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 13364 {
b34976b6 13365 bfd_boolean skip, relocate;
65f38f15 13366 asection *sreloc;
1cf1f670 13367 bfd_vma out_off;
65f38f15
AM
13368
13369 /* When generating a dynamic object, these relocations
13370 are copied into the output file to be resolved at run
13371 time. */
13372
b34976b6
AM
13373 skip = FALSE;
13374 relocate = FALSE;
65f38f15 13375
1cf1f670
AM
13376 out_off = _bfd_elf_section_offset (output_bfd, info,
13377 input_section, rel->r_offset);
13378 if (out_off == (bfd_vma) -1)
b34976b6 13379 skip = TRUE;
1cf1f670 13380 else if (out_off == (bfd_vma) -2)
b34976b6 13381 skip = TRUE, relocate = TRUE;
1cf1f670
AM
13382 out_off += (input_section->output_section->vma
13383 + input_section->output_offset);
13384 outrel.r_offset = out_off;
411e1bfb 13385 outrel.r_addend = rel->r_addend;
65f38f15 13386
1cf1f670
AM
13387 /* Optimize unaligned reloc use. */
13388 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13389 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13390 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13391 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13392 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13393 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13394 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13395 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13396 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13397
65f38f15 13398 if (skip)
0bb2d96a 13399 memset (&outrel, 0, sizeof outrel);
726d3ab0 13400 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
13401 && !is_opd
13402 && r_type != R_PPC64_TOC)
039b3fef 13403 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
13404 else
13405 {
41bd81ab
AM
13406 /* This symbol is local, or marked to become local,
13407 or this is an opd section reloc which must point
13408 at a local function. */
65f38f15 13409 outrel.r_addend += relocation;
e86ce104 13410 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 13411 {
3fad3c7c 13412 if (is_opd && h != NULL)
afbe61cf
AM
13413 {
13414 /* Lie about opd entries. This case occurs
13415 when building shared libraries and we
13416 reference a function in another shared
3fad3c7c
AM
13417 lib. The same thing happens for a weak
13418 definition in an application that's
13419 overridden by a strong definition in a
13420 shared lib. (I believe this is a generic
13421 bug in binutils handling of weak syms.)
13422 In these cases we won't use the opd
1e2f5b6e 13423 entry in this lib. */
b34976b6 13424 unresolved_reloc = FALSE;
afbe61cf 13425 }
25f23106
AM
13426 if (!is_opd
13427 && r_type == R_PPC64_ADDR64
13428 && (h != NULL
13429 ? h->elf.type == STT_GNU_IFUNC
13430 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13431 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13432 else
13433 {
13434 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 13435
25f23106
AM
13436 /* We need to relocate .opd contents for ld.so.
13437 Prelink also wants simple and consistent rules
13438 for relocs. This make all RELATIVE relocs have
13439 *r_offset equal to r_addend. */
13440 relocate = TRUE;
13441 }
65f38f15
AM
13442 }
13443 else
13444 {
13445 long indx = 0;
13446
25f23106
AM
13447 if (h != NULL
13448 ? h->elf.type == STT_GNU_IFUNC
13449 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13450 {
25f53a85 13451 info->callbacks->einfo
8de848d8 13452 (_("%P: %H: relocation %s for indirect "
25f53a85
AM
13453 "function %s unsupported\n"),
13454 input_bfd, input_section, rel->r_offset,
25f23106
AM
13455 ppc64_elf_howto_table[r_type]->name,
13456 sym_name);
13457 ret = FALSE;
13458 }
cf35638d 13459 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
13460 ;
13461 else if (sec == NULL || sec->owner == NULL)
13462 {
13463 bfd_set_error (bfd_error_bad_value);
b34976b6 13464 return FALSE;
65f38f15
AM
13465 }
13466 else
13467 {
13468 asection *osec;
13469
13470 osec = sec->output_section;
13471 indx = elf_section_data (osec)->dynindx;
13472
74541ad4
AM
13473 if (indx == 0)
13474 {
13475 if ((osec->flags & SEC_READONLY) == 0
13476 && htab->elf.data_index_section != NULL)
13477 osec = htab->elf.data_index_section;
13478 else
13479 osec = htab->elf.text_index_section;
13480 indx = elf_section_data (osec)->dynindx;
13481 }
13482 BFD_ASSERT (indx != 0);
13483
65f38f15
AM
13484 /* We are turning this relocation into one
13485 against a section symbol, so subtract out
13486 the output section's address but not the
13487 offset of the input section in the output
13488 section. */
13489 outrel.r_addend -= osec->vma;
13490 }
13491
13492 outrel.r_info = ELF64_R_INFO (indx, r_type);
13493 }
13494 }
13495
13496 sreloc = elf_section_data (input_section)->sreloc;
25f23106
AM
13497 if (!htab->elf.dynamic_sections_created)
13498 sreloc = htab->reliplt;
65f38f15
AM
13499 if (sreloc == NULL)
13500 abort ();
13501
dfbb6ac9
AM
13502 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13503 >= sreloc->size)
13504 abort ();
947216bf
AM
13505 loc = sreloc->contents;
13506 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
13507 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13508
13509 /* If this reloc is against an external symbol, it will
13510 be computed at runtime, so there's no need to do
81407a69
AM
13511 anything now. However, for the sake of prelink ensure
13512 that the section contents are a known value. */
65f38f15 13513 if (! relocate)
81407a69
AM
13514 {
13515 unresolved_reloc = FALSE;
13516 /* The value chosen here is quite arbitrary as ld.so
13517 ignores section contents except for the special
13518 case of .opd where the contents might be accessed
13519 before relocation. Choose zero, as that won't
13520 cause reloc overflow. */
13521 relocation = 0;
13522 addend = 0;
13523 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13524 to improve backward compatibility with older
13525 versions of ld. */
13526 if (r_type == R_PPC64_ADDR64)
13527 addend = outrel.r_addend;
13528 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 13529 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
13530 addend = (input_section->output_section->vma
13531 + input_section->output_offset
13532 + rel->r_offset);
81407a69 13533 }
65f38f15 13534 }
5bd4f169
AM
13535 break;
13536
65f38f15
AM
13537 case R_PPC64_COPY:
13538 case R_PPC64_GLOB_DAT:
13539 case R_PPC64_JMP_SLOT:
25f23106 13540 case R_PPC64_JMP_IREL:
65f38f15
AM
13541 case R_PPC64_RELATIVE:
13542 /* We shouldn't ever see these dynamic relocs in relocatable
13543 files. */
ae9a127f 13544 /* Fall through. */
65f38f15
AM
13545
13546 case R_PPC64_PLTGOT16:
13547 case R_PPC64_PLTGOT16_DS:
13548 case R_PPC64_PLTGOT16_HA:
13549 case R_PPC64_PLTGOT16_HI:
13550 case R_PPC64_PLTGOT16_LO:
13551 case R_PPC64_PLTGOT16_LO_DS:
13552 case R_PPC64_PLTREL32:
13553 case R_PPC64_PLTREL64:
13554 /* These ones haven't been implemented yet. */
13555
25f53a85 13556 info->callbacks->einfo
8de848d8 13557 (_("%P: %B: relocation %s is not supported for symbol %s\n"),
d003868e 13558 input_bfd,
4ce794b7 13559 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
13560
13561 bfd_set_error (bfd_error_invalid_operation);
b34976b6 13562 ret = FALSE;
5bd4f169 13563 continue;
65f38f15 13564 }
5bd4f169 13565
67f0cbdb
AM
13566 /* Multi-instruction sequences that access the TOC can be
13567 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13568 to nop; addi rb,r2,x; */
13569 switch (r_type)
13570 {
13571 default:
13572 break;
13573
13574 case R_PPC64_GOT_TLSLD16_HI:
13575 case R_PPC64_GOT_TLSGD16_HI:
13576 case R_PPC64_GOT_TPREL16_HI:
13577 case R_PPC64_GOT_DTPREL16_HI:
13578 case R_PPC64_GOT16_HI:
13579 case R_PPC64_TOC16_HI:
13580 /* These relocs would only be useful if building up an
13581 offset to later add to r2, perhaps in an indexed
13582 addressing mode instruction. Don't try to optimize.
13583 Unfortunately, the possibility of someone building up an
13584 offset like this or even with the HA relocs, means that
13585 we need to check the high insn when optimizing the low
13586 insn. */
13587 break;
13588
13589 case R_PPC64_GOT_TLSLD16_HA:
13590 case R_PPC64_GOT_TLSGD16_HA:
13591 case R_PPC64_GOT_TPREL16_HA:
13592 case R_PPC64_GOT_DTPREL16_HA:
13593 case R_PPC64_GOT16_HA:
13594 case R_PPC64_TOC16_HA:
98528052 13595 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 13596 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
13597 {
13598 bfd_byte *p = contents + (rel->r_offset & ~3);
13599 bfd_put_32 (input_bfd, NOP, p);
13600 }
67f0cbdb
AM
13601 break;
13602
13603 case R_PPC64_GOT_TLSLD16_LO:
13604 case R_PPC64_GOT_TLSGD16_LO:
13605 case R_PPC64_GOT_TPREL16_LO_DS:
13606 case R_PPC64_GOT_DTPREL16_LO_DS:
13607 case R_PPC64_GOT16_LO:
13608 case R_PPC64_GOT16_LO_DS:
13609 case R_PPC64_TOC16_LO:
13610 case R_PPC64_TOC16_LO_DS:
98528052 13611 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 13612 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
13613 {
13614 bfd_byte *p = contents + (rel->r_offset & ~3);
13615 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
13616 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
13617 {
13618 /* Transform addic to addi when we change reg. */
13619 insn &= ~((0x3f << 26) | (0x1f << 16));
13620 insn |= (14u << 26) | (2 << 16);
13621 }
13622 else
67f0cbdb 13623 {
98528052
AM
13624 insn &= ~(0x1f << 16);
13625 insn |= 2 << 16;
67f0cbdb 13626 }
560c8763 13627 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
13628 }
13629 break;
13630 }
13631
65f38f15
AM
13632 /* Do any further special processing. */
13633 switch (r_type)
13634 {
13635 default:
13636 break;
13637
13638 case R_PPC64_ADDR16_HA:
25f23106 13639 case R_PPC64_REL16_HA:
65f38f15
AM
13640 case R_PPC64_ADDR16_HIGHERA:
13641 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
13642 case R_PPC64_TOC16_HA:
13643 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
13644 case R_PPC64_TPREL16_HA:
13645 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
13646 case R_PPC64_TPREL16_HIGHER:
13647 case R_PPC64_TPREL16_HIGHERA:
13648 case R_PPC64_TPREL16_HIGHEST:
13649 case R_PPC64_TPREL16_HIGHESTA:
13650 case R_PPC64_DTPREL16_HIGHER:
13651 case R_PPC64_DTPREL16_HIGHERA:
13652 case R_PPC64_DTPREL16_HIGHEST:
13653 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
13654 /* It's just possible that this symbol is a weak symbol
13655 that's not actually defined anywhere. In that case,
13656 'sec' would be NULL, and we should leave the symbol
13657 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
13658 if (sec == NULL)
13659 break;
13660 /* Fall thru */
13661
13662 case R_PPC64_GOT16_HA:
13663 case R_PPC64_PLTGOT16_HA:
13664 case R_PPC64_PLT16_HA:
13665 case R_PPC64_GOT_TLSGD16_HA:
13666 case R_PPC64_GOT_TLSLD16_HA:
13667 case R_PPC64_GOT_TPREL16_HA:
13668 case R_PPC64_GOT_DTPREL16_HA:
13669 /* Add 0x10000 if sign bit in 0:15 is set.
13670 Bits 0:15 are not used. */
13671 addend += 0x8000;
65f38f15
AM
13672 break;
13673
13674 case R_PPC64_ADDR16_DS:
13675 case R_PPC64_ADDR16_LO_DS:
13676 case R_PPC64_GOT16_DS:
13677 case R_PPC64_GOT16_LO_DS:
13678 case R_PPC64_PLT16_LO_DS:
13679 case R_PPC64_SECTOFF_DS:
13680 case R_PPC64_SECTOFF_LO_DS:
13681 case R_PPC64_TOC16_DS:
13682 case R_PPC64_TOC16_LO_DS:
13683 case R_PPC64_PLTGOT16_DS:
13684 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
13685 case R_PPC64_GOT_TPREL16_DS:
13686 case R_PPC64_GOT_TPREL16_LO_DS:
13687 case R_PPC64_GOT_DTPREL16_DS:
13688 case R_PPC64_GOT_DTPREL16_LO_DS:
13689 case R_PPC64_TPREL16_DS:
13690 case R_PPC64_TPREL16_LO_DS:
13691 case R_PPC64_DTPREL16_DS:
13692 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
13693 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13694 mask = 3;
13695 /* If this reloc is against an lq insn, then the value must be
13696 a multiple of 16. This is somewhat of a hack, but the
13697 "correct" way to do this by defining _DQ forms of all the
13698 _DS relocs bloats all reloc switches in this file. It
13699 doesn't seem to make much sense to use any of these relocs
13700 in data, so testing the insn should be safe. */
494dac0c 13701 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
13702 mask = 15;
13703 if (((relocation + addend) & mask) != 0)
65f38f15 13704 {
25f53a85 13705 info->callbacks->einfo
8de848d8 13706 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 13707 input_bfd, input_section, rel->r_offset,
4ce794b7 13708 ppc64_elf_howto_table[r_type]->name,
adadcc0c 13709 mask + 1);
65f38f15 13710 bfd_set_error (bfd_error_bad_value);
b34976b6 13711 ret = FALSE;
65f38f15
AM
13712 continue;
13713 }
13714 break;
5bd4f169
AM
13715 }
13716
239e1f3a
AM
13717 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13718 because such sections are not SEC_ALLOC and thus ld.so will
13719 not process them. */
65f38f15 13720 if (unresolved_reloc
239e1f3a 13721 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
13722 && h->elf.def_dynamic)
13723 && _bfd_elf_section_offset (output_bfd, info, input_section,
13724 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 13725 {
25f53a85 13726 info->callbacks->einfo
8de848d8 13727 (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
25f53a85 13728 input_bfd, input_section, rel->r_offset,
7b609f53 13729 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 13730 h->elf.root.root.string);
b34976b6 13731 ret = FALSE;
9c07fe7c 13732 }
5bd4f169 13733
65f38f15 13734 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
13735 input_bfd,
13736 input_section,
13737 contents,
411e1bfb 13738 rel->r_offset,
5bd4f169
AM
13739 relocation,
13740 addend);
13741
ef60b7ff 13742 if (r != bfd_reloc_ok)
5bd4f169 13743 {
cd27b276
AM
13744 if (sym_name == NULL)
13745 sym_name = "(null)";
13746 if (r == bfd_reloc_overflow)
5bd4f169 13747 {
cd27b276
AM
13748 if (warned)
13749 continue;
13750 if (h != NULL
039b3fef 13751 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 13752 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
13753 {
13754 /* Assume this is a call protected by other code that
13755 detects the symbol is undefined. If this is the case,
13756 we can safely ignore the overflow. If not, the
13757 program is hosed anyway, and a little warning isn't
13758 going to help. */
13759
13760 continue;
13761 }
13762
ef60b7ff 13763 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
13764 (info, (h ? &h->elf.root : NULL), sym_name,
13765 ppc64_elf_howto_table[r_type]->name,
4cc603a5 13766 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 13767 return FALSE;
ef60b7ff
AM
13768 }
13769 else
13770 {
25f53a85 13771 info->callbacks->einfo
8de848d8 13772 (_("%P: %H: %s reloc against `%s': error %d\n"),
25f53a85 13773 input_bfd, input_section, rel->r_offset,
4ce794b7 13774 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
13775 sym_name,
13776 (int) r);
b34976b6 13777 ret = FALSE;
ef60b7ff 13778 }
5bd4f169 13779 }
5bd4f169
AM
13780 }
13781
645ea6a9
AM
13782 /* If we're emitting relocations, then shortly after this function
13783 returns, reloc offsets and addends for this section will be
13784 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
13785 file rather than the input. Save a copy of the relocs for
13786 opd_entry_value. */
4cc603a5 13787 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
13788 {
13789 bfd_size_type amt;
13790 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13791 rel = bfd_alloc (input_bfd, amt);
13792 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13793 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13794 if (rel == NULL)
13795 return FALSE;
13796 memcpy (rel, relocs, amt);
13797 }
5bd4f169
AM
13798 return ret;
13799}
13800
754021d0
AM
13801/* Adjust the value of any local symbols in opd sections. */
13802
6e0b88f1 13803static int
754021d0
AM
13804ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13805 const char *name ATTRIBUTE_UNUSED,
13806 Elf_Internal_Sym *elfsym,
13807 asection *input_sec,
13808 struct elf_link_hash_entry *h)
13809{
74f0fb50
AM
13810 struct _opd_sec_data *opd;
13811 long adjust;
754021d0
AM
13812 bfd_vma value;
13813
4025353c 13814 if (h != NULL)
6e0b88f1 13815 return 1;
4025353c 13816
74f0fb50
AM
13817 opd = get_opd_info (input_sec);
13818 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 13819 return 1;
754021d0
AM
13820
13821 value = elfsym->st_value - input_sec->output_offset;
13822 if (!info->relocatable)
13823 value -= input_sec->output_section->vma;
13824
74f0fb50 13825 adjust = opd->adjust[value / 8];
4025353c 13826 if (adjust == -1)
6e0b88f1
AM
13827 return 2;
13828
13829 elfsym->st_value += adjust;
13830 return 1;
754021d0
AM
13831}
13832
5bd4f169
AM
13833/* Finish up dynamic symbol handling. We set the contents of various
13834 dynamic sections here. */
13835
b34976b6 13836static bfd_boolean
4ce794b7
AM
13837ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13838 struct bfd_link_info *info,
13839 struct elf_link_hash_entry *h,
13840 Elf_Internal_Sym *sym)
5bd4f169 13841{
65f38f15 13842 struct ppc_link_hash_table *htab;
8387904d
AM
13843 struct plt_entry *ent;
13844 Elf_Internal_Rela rela;
13845 bfd_byte *loc;
5bd4f169 13846
65f38f15 13847 htab = ppc_hash_table (info);
4dfe6ac6
NC
13848 if (htab == NULL)
13849 return FALSE;
5bd4f169 13850
8387904d
AM
13851 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13852 if (ent->plt.offset != (bfd_vma) -1)
13853 {
13854 /* This symbol has an entry in the procedure linkage
13855 table. Set it up. */
e054468f
AM
13856 if (!htab->elf.dynamic_sections_created
13857 || h->dynindx == -1)
13858 {
13859 BFD_ASSERT (h->type == STT_GNU_IFUNC
13860 && h->def_regular
13861 && (h->root.type == bfd_link_hash_defined
13862 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
13863 rela.r_offset = (htab->iplt->output_section->vma
13864 + htab->iplt->output_offset
13865 + ent->plt.offset);
13866 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
13867 rela.r_addend = (h->root.u.def.value
13868 + h->root.u.def.section->output_offset
13869 + h->root.u.def.section->output_section->vma
13870 + ent->addend);
25f23106
AM
13871 loc = (htab->reliplt->contents
13872 + (htab->reliplt->reloc_count++
13873 * sizeof (Elf64_External_Rela)));
e054468f
AM
13874 }
13875 else
13876 {
25f23106
AM
13877 rela.r_offset = (htab->plt->output_section->vma
13878 + htab->plt->output_offset
13879 + ent->plt.offset);
e054468f
AM
13880 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13881 rela.r_addend = ent->addend;
25f23106
AM
13882 loc = (htab->relplt->contents
13883 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13884 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 13885 }
8387904d
AM
13886 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13887 }
5bd4f169 13888
f5385ebf 13889 if (h->needs_copy)
5bd4f169 13890 {
65f38f15 13891 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 13892
65f38f15
AM
13893 if (h->dynindx == -1
13894 || (h->root.type != bfd_link_hash_defined
13895 && h->root.type != bfd_link_hash_defweak)
4ce794b7 13896 || htab->relbss == NULL)
65f38f15 13897 abort ();
5bd4f169
AM
13898
13899 rela.r_offset = (h->root.u.def.value
13900 + h->root.u.def.section->output_section->vma
13901 + h->root.u.def.section->output_offset);
13902 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13903 rela.r_addend = 0;
4ce794b7
AM
13904 loc = htab->relbss->contents;
13905 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 13906 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
13907 }
13908
5bd4f169
AM
13909 /* Mark some specially defined symbols as absolute. */
13910 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13911 sym->st_shndx = SHN_ABS;
13912
b34976b6 13913 return TRUE;
5bd4f169
AM
13914}
13915
65f38f15
AM
13916/* Used to decide how to sort relocs in an optimal manner for the
13917 dynamic linker, before writing them out. */
13918
13919static enum elf_reloc_type_class
4ce794b7 13920ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 13921{
04c9666a 13922 enum elf_ppc64_reloc_type r_type;
a33d1f77 13923
4ce794b7 13924 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 13925 switch (r_type)
65f38f15
AM
13926 {
13927 case R_PPC64_RELATIVE:
13928 return reloc_class_relative;
13929 case R_PPC64_JMP_SLOT:
13930 return reloc_class_plt;
13931 case R_PPC64_COPY:
13932 return reloc_class_copy;
13933 default:
13934 return reloc_class_normal;
13935 }
13936}
13937
5bd4f169
AM
13938/* Finish up the dynamic sections. */
13939
b34976b6 13940static bfd_boolean
4ce794b7
AM
13941ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13942 struct bfd_link_info *info)
5bd4f169 13943{
65f38f15
AM
13944 struct ppc_link_hash_table *htab;
13945 bfd *dynobj;
5bd4f169 13946 asection *sdyn;
5bd4f169 13947
65f38f15 13948 htab = ppc_hash_table (info);
4dfe6ac6
NC
13949 if (htab == NULL)
13950 return FALSE;
13951
65f38f15 13952 dynobj = htab->elf.dynobj;
5bd4f169
AM
13953 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13954
65f38f15 13955 if (htab->elf.dynamic_sections_created)
5bd4f169 13956 {
5bd4f169
AM
13957 Elf64_External_Dyn *dyncon, *dynconend;
13958
4ce794b7 13959 if (sdyn == NULL || htab->got == NULL)
65f38f15 13960 abort ();
5bd4f169
AM
13961
13962 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 13963 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
13964 for (; dyncon < dynconend; dyncon++)
13965 {
13966 Elf_Internal_Dyn dyn;
19397422 13967 asection *s;
5bd4f169
AM
13968
13969 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13970
13971 switch (dyn.d_tag)
13972 {
65f38f15
AM
13973 default:
13974 continue;
5bd4f169 13975
5d1634d7 13976 case DT_PPC64_GLINK:
4ce794b7 13977 s = htab->glink;
6348e046 13978 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
13979 /* We stupidly defined DT_PPC64_GLINK to be the start
13980 of glink rather than the first entry point, which is
13981 what ld.so needs, and now have a bigger stub to
13982 support automatic multiple TOCs. */
13983 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
13984 break;
13985
19397422
AM
13986 case DT_PPC64_OPD:
13987 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13988 if (s == NULL)
13989 continue;
13990 dyn.d_un.d_ptr = s->vma;
19397422
AM
13991 break;
13992
13993 case DT_PPC64_OPDSZ:
13994 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13995 if (s == NULL)
13996 continue;
eea6121a 13997 dyn.d_un.d_val = s->size;
19397422
AM
13998 break;
13999
65f38f15 14000 case DT_PLTGOT:
4ce794b7 14001 s = htab->plt;
6348e046 14002 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
14003 break;
14004
14005 case DT_JMPREL:
4ce794b7 14006 s = htab->relplt;
6348e046 14007 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 14008 break;
5bd4f169 14009
65f38f15 14010 case DT_PLTRELSZ:
eea6121a 14011 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
14012 break;
14013
14014 case DT_RELASZ:
14015 /* Don't count procedure linkage table relocs in the
14016 overall reloc count. */
4ce794b7 14017 s = htab->relplt;
6348e046
AM
14018 if (s == NULL)
14019 continue;
eea6121a 14020 dyn.d_un.d_val -= s->size;
6348e046
AM
14021 break;
14022
14023 case DT_RELA:
14024 /* We may not be using the standard ELF linker script.
14025 If .rela.plt is the first .rela section, we adjust
14026 DT_RELA to not include it. */
4ce794b7 14027 s = htab->relplt;
6348e046
AM
14028 if (s == NULL)
14029 continue;
14030 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14031 continue;
eea6121a 14032 dyn.d_un.d_ptr += s->size;
65f38f15 14033 break;
5bd4f169 14034 }
5bd4f169 14035
65f38f15 14036 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 14037 }
5bd4f169
AM
14038 }
14039
eea6121a 14040 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
14041 {
14042 /* Fill in the first entry in the global offset table.
14043 We use it to hold the link-time TOCbase. */
14044 bfd_put_64 (output_bfd,
60ee0d4a 14045 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 14046 htab->got->contents);
5d1634d7
AM
14047
14048 /* Set .got entry size. */
4ce794b7 14049 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
14050 }
14051
eea6121a 14052 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
14053 {
14054 /* Set .plt entry size. */
4ce794b7 14055 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
14056 = PLT_ENTRY_SIZE;
14057 }
14058
84f5d08e
AM
14059 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14060 brlt ourselves if emitrelocations. */
14061 if (htab->brlt != NULL
14062 && htab->brlt->reloc_count != 0
14063 && !_bfd_elf_link_output_relocs (output_bfd,
14064 htab->brlt,
d4730f92 14065 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
14066 elf_section_data (htab->brlt)->relocs,
14067 NULL))
14068 return FALSE;
14069
176a0d42
AM
14070 if (htab->glink != NULL
14071 && htab->glink->reloc_count != 0
14072 && !_bfd_elf_link_output_relocs (output_bfd,
14073 htab->glink,
d4730f92 14074 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
14075 elf_section_data (htab->glink)->relocs,
14076 NULL))
14077 return FALSE;
14078
58d180e8
AM
14079
14080 if (htab->glink_eh_frame != NULL
14081 && htab->glink_eh_frame->sec_info_type == ELF_INFO_TYPE_EH_FRAME
14082 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14083 htab->glink_eh_frame,
14084 htab->glink_eh_frame->contents))
14085 return FALSE;
14086
e717da7e 14087 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
14088 since we didn't add them to DYNOBJ. We know dynobj is the first
14089 bfd. */
e717da7e
AM
14090 while ((dynobj = dynobj->link_next) != NULL)
14091 {
14092 asection *s;
7b53ace3 14093
0c8d6e5c 14094 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
14095 continue;
14096
e717da7e
AM
14097 s = ppc64_elf_tdata (dynobj)->got;
14098 if (s != NULL
eea6121a 14099 && s->size != 0
e717da7e
AM
14100 && s->output_section != bfd_abs_section_ptr
14101 && !bfd_set_section_contents (output_bfd, s->output_section,
14102 s->contents, s->output_offset,
eea6121a 14103 s->size))
e717da7e
AM
14104 return FALSE;
14105 s = ppc64_elf_tdata (dynobj)->relgot;
14106 if (s != NULL
eea6121a 14107 && s->size != 0
e717da7e
AM
14108 && s->output_section != bfd_abs_section_ptr
14109 && !bfd_set_section_contents (output_bfd, s->output_section,
14110 s->contents, s->output_offset,
eea6121a 14111 s->size))
e717da7e
AM
14112 return FALSE;
14113 }
f6c52c13 14114
b34976b6 14115 return TRUE;
5bd4f169
AM
14116}
14117
5bd4f169 14118#include "elf64-target.h"
7b8e7dad
AM
14119
14120/* FreeBSD support */
14121
14122#undef TARGET_LITTLE_SYM
14123#undef TARGET_LITTLE_NAME
14124
14125#undef TARGET_BIG_SYM
14126#define TARGET_BIG_SYM bfd_elf64_powerpc_freebsd_vec
14127#undef TARGET_BIG_NAME
14128#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14129
14130#undef ELF_OSABI
14131#define ELF_OSABI ELFOSABI_FREEBSD
14132
14133#undef elf64_bed
14134#define elf64_bed elf64_powerpc_fbsd_bed
14135
14136#include "elf64-target.h"
14137
This page took 3.472679 seconds and 4 git commands to generate.