ELFv2 stub, plt and glink changes
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
3a71aa26 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
794e51c0 3 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5bd4f169
AM
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 6 Largely rewritten by Alan Modra.
5bd4f169 7
ae9a127f 8 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 9
ae9a127f
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
ae9a127f 13 (at your option) any later version.
5bd4f169 14
ae9a127f
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
5bd4f169 19
4ce794b7
AM
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 23
cd123cb7 24
4ce794b7
AM
25/* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 28
3db64b00 29#include "sysdep.h"
183e98be 30#include <stdarg.h>
5bd4f169 31#include "bfd.h"
5bd4f169
AM
32#include "bfdlink.h"
33#include "libbfd.h"
34#include "elf-bfd.h"
04c9666a 35#include "elf/ppc64.h"
5d1634d7 36#include "elf64-ppc.h"
58d180e8 37#include "dwarf2.h"
5bd4f169 38
805fc799 39static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
41static bfd_reloc_status_type ppc64_elf_branch_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 43static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 45static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 47static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 49static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 51static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 53static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 55static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 57static bfd_vma opd_entry_value
aef36ac1 58 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 59
ad8e1ba5
AM
60#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
61#define TARGET_LITTLE_NAME "elf64-powerpcle"
62#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
63#define TARGET_BIG_NAME "elf64-powerpc"
64#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 65#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
66#define ELF_MACHINE_CODE EM_PPC64
67#define ELF_MAXPAGESIZE 0x10000
24718e3b 68#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
69#define elf_info_to_howto ppc64_elf_info_to_howto
70
71#define elf_backend_want_got_sym 0
72#define elf_backend_want_plt_sym 0
73#define elf_backend_plt_alignment 3
74#define elf_backend_plt_not_loaded 1
ad8e1ba5 75#define elf_backend_got_header_size 8
ad8e1ba5
AM
76#define elf_backend_can_gc_sections 1
77#define elf_backend_can_refcount 1
78#define elf_backend_rela_normal 1
6bfdb61b 79#define elf_backend_default_execstack 0
ad8e1ba5 80
e717da7e 81#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 82#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 83#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
84#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
85#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
86#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
87#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
88#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 89#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 90#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
91
92#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
93#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
94#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 95#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
96#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
97#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 98#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 99#define elf_backend_check_directives ppc64_elf_process_dot_syms
e5034e59 100#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 101#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 102#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 103#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 104#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
105#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
106#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
107#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
108#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 109#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
110#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
111#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 112#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 113#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
114#define elf_backend_relocate_section ppc64_elf_relocate_section
115#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
116#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
117#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 118#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 119#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 120#define elf_backend_post_process_headers _bfd_elf_set_osabi
6911b7dc 121#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
ad8e1ba5 122
5bd4f169
AM
123/* The name of the dynamic interpreter. This is put in the .interp
124 section. */
125#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
126
127/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 128#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
129
130/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 131#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169
AM
132
133/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
134#define TOC_BASE_OFF 0x8000
135
136/* Offset of tp and dtp pointers from start of TLS block. */
137#define TP_OFFSET 0x7000
138#define DTP_OFFSET 0x8000
5bd4f169 139
ad8e1ba5
AM
140/* .plt call stub instructions. The normal stub is like this, but
141 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 142 insert an addi to adjust r11. */
5d1634d7 143#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
71a39c98
AM
144#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
145#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
146#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
147#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
148#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
149#define BCTR 0x4e800420 /* bctr */
150
71a39c98 151#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,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
71a39c98
AM
155#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
156#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
157#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
794e51c0
AM
158#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
159#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
160#define BNECTR 0x4ca20420 /* bnectr+ */
161#define BNECTR_P4 0x4ce20420 /* bnectr+ */
162
71a39c98 163#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442
AM
164#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
165#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
166
ad8e1ba5
AM
167#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
168
ee4bf8d2 169/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 170#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
171 /* 0: */
172 /* .quad plt0-1f */
173 /* __glink: */
174#define MFLR_R12 0x7d8802a6 /* mflr %12 */
175#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
176 /* 1: */
177#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 178 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 179#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
180#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
181 /* ld %12,0(%11) */
182 /* ld %2,8(%11) */
183 /* mtctr %12 */
184 /* ld %11,16(%11) */
ee4bf8d2 185 /* bctr */
b9e5796b
AM
186#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
187#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
188#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
189#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
190#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
191
192/* Pad with this. */
193#define NOP 0x60000000
194
721956f4
AM
195/* Some other nops. */
196#define CROR_151515 0x4def7b82
197#define CROR_313131 0x4ffffb82
198
cedb70c5 199/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
200#define LI_R0_0 0x38000000 /* li %r0,0 */
201#define B_DOT 0x48000000 /* b . */
202
203/* After that, we need two instructions to load the index, followed by
204 a branch. */
205#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 206#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 207
deb0e272
AM
208/* Instructions used by the save and restore reg functions. */
209#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
210#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
211#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
212#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
213#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
214#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
215#define LI_R12_0 0x39800000 /* li %r12,0 */
216#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
217#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
218#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
219#define BLR 0x4e800020 /* blr */
220
41bd81ab
AM
221/* Since .opd is an array of descriptors and each entry will end up
222 with identical R_PPC64_RELATIVE relocs, there is really no need to
223 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 224 relocate .opd without reloc entries. */
41bd81ab
AM
225#ifndef NO_OPD_RELOCS
226#define NO_OPD_RELOCS 0
227#endif
5bd4f169 228\f
f5e87a1d 229#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 230
5bd4f169 231/* Relocation HOWTO's. */
04c9666a 232static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
233
234static reloc_howto_type ppc64_elf_howto_raw[] = {
235 /* This reloc does nothing. */
236 HOWTO (R_PPC64_NONE, /* type */
237 0, /* rightshift */
411e1bfb
AM
238 2, /* size (0 = byte, 1 = short, 2 = long) */
239 32, /* bitsize */
b34976b6 240 FALSE, /* pc_relative */
5bd4f169 241 0, /* bitpos */
f5e87a1d 242 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
243 bfd_elf_generic_reloc, /* special_function */
244 "R_PPC64_NONE", /* name */
b34976b6 245 FALSE, /* partial_inplace */
d006db6c 246 0, /* src_mask */
5bd4f169 247 0, /* dst_mask */
b34976b6 248 FALSE), /* pcrel_offset */
5bd4f169
AM
249
250 /* A standard 32 bit relocation. */
251 HOWTO (R_PPC64_ADDR32, /* type */
252 0, /* rightshift */
253 2, /* size (0 = byte, 1 = short, 2 = long) */
254 32, /* bitsize */
b34976b6 255 FALSE, /* pc_relative */
5bd4f169
AM
256 0, /* bitpos */
257 complain_overflow_bitfield, /* complain_on_overflow */
258 bfd_elf_generic_reloc, /* special_function */
259 "R_PPC64_ADDR32", /* name */
b34976b6 260 FALSE, /* partial_inplace */
5bd4f169
AM
261 0, /* src_mask */
262 0xffffffff, /* dst_mask */
b34976b6 263 FALSE), /* pcrel_offset */
5bd4f169
AM
264
265 /* An absolute 26 bit branch; the lower two bits must be zero.
266 FIXME: we don't check that, we just clear them. */
267 HOWTO (R_PPC64_ADDR24, /* type */
268 0, /* rightshift */
269 2, /* size (0 = byte, 1 = short, 2 = long) */
270 26, /* bitsize */
b34976b6 271 FALSE, /* pc_relative */
5bd4f169
AM
272 0, /* bitpos */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR24", /* name */
b34976b6 276 FALSE, /* partial_inplace */
d006db6c 277 0, /* src_mask */
f5e87a1d 278 0x03fffffc, /* dst_mask */
b34976b6 279 FALSE), /* pcrel_offset */
5bd4f169
AM
280
281 /* A standard 16 bit relocation. */
282 HOWTO (R_PPC64_ADDR16, /* type */
283 0, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
b34976b6 286 FALSE, /* pc_relative */
5bd4f169
AM
287 0, /* bitpos */
288 complain_overflow_bitfield, /* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC64_ADDR16", /* name */
b34976b6 291 FALSE, /* partial_inplace */
5bd4f169
AM
292 0, /* src_mask */
293 0xffff, /* dst_mask */
b34976b6 294 FALSE), /* pcrel_offset */
5bd4f169
AM
295
296 /* A 16 bit relocation without overflow. */
297 HOWTO (R_PPC64_ADDR16_LO, /* type */
298 0, /* rightshift */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
300 16, /* bitsize */
b34976b6 301 FALSE, /* pc_relative */
5bd4f169
AM
302 0, /* bitpos */
303 complain_overflow_dont,/* complain_on_overflow */
304 bfd_elf_generic_reloc, /* special_function */
305 "R_PPC64_ADDR16_LO", /* name */
b34976b6 306 FALSE, /* partial_inplace */
5bd4f169
AM
307 0, /* src_mask */
308 0xffff, /* dst_mask */
b34976b6 309 FALSE), /* pcrel_offset */
5bd4f169
AM
310
311 /* Bits 16-31 of an address. */
312 HOWTO (R_PPC64_ADDR16_HI, /* type */
313 16, /* rightshift */
314 1, /* size (0 = byte, 1 = short, 2 = long) */
315 16, /* bitsize */
b34976b6 316 FALSE, /* pc_relative */
5bd4f169 317 0, /* bitpos */
f9c6b907 318 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
319 bfd_elf_generic_reloc, /* special_function */
320 "R_PPC64_ADDR16_HI", /* name */
b34976b6 321 FALSE, /* partial_inplace */
5bd4f169
AM
322 0, /* src_mask */
323 0xffff, /* dst_mask */
b34976b6 324 FALSE), /* pcrel_offset */
5bd4f169
AM
325
326 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
327 bits, treated as a signed number, is negative. */
328 HOWTO (R_PPC64_ADDR16_HA, /* type */
329 16, /* rightshift */
330 1, /* size (0 = byte, 1 = short, 2 = long) */
331 16, /* bitsize */
b34976b6 332 FALSE, /* pc_relative */
5bd4f169 333 0, /* bitpos */
f9c6b907 334 complain_overflow_signed, /* complain_on_overflow */
805fc799 335 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 336 "R_PPC64_ADDR16_HA", /* name */
b34976b6 337 FALSE, /* partial_inplace */
5bd4f169
AM
338 0, /* src_mask */
339 0xffff, /* dst_mask */
b34976b6 340 FALSE), /* pcrel_offset */
5bd4f169
AM
341
342 /* An absolute 16 bit branch; the lower two bits must be zero.
343 FIXME: we don't check that, we just clear them. */
344 HOWTO (R_PPC64_ADDR14, /* type */
345 0, /* rightshift */
346 2, /* size (0 = byte, 1 = short, 2 = long) */
347 16, /* bitsize */
b34976b6 348 FALSE, /* pc_relative */
5bd4f169
AM
349 0, /* bitpos */
350 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 351 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 352 "R_PPC64_ADDR14", /* name */
b34976b6 353 FALSE, /* partial_inplace */
d006db6c 354 0, /* src_mask */
f5e87a1d 355 0x0000fffc, /* dst_mask */
b34976b6 356 FALSE), /* pcrel_offset */
5bd4f169
AM
357
358 /* An absolute 16 bit branch, for which bit 10 should be set to
359 indicate that the branch is expected to be taken. The lower two
360 bits must be zero. */
361 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
362 0, /* rightshift */
363 2, /* size (0 = byte, 1 = short, 2 = long) */
364 16, /* bitsize */
b34976b6 365 FALSE, /* pc_relative */
5bd4f169
AM
366 0, /* bitpos */
367 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 368 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 369 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 370 FALSE, /* partial_inplace */
d006db6c 371 0, /* src_mask */
f5e87a1d 372 0x0000fffc, /* dst_mask */
b34976b6 373 FALSE), /* pcrel_offset */
5bd4f169
AM
374
375 /* An absolute 16 bit branch, for which bit 10 should be set to
376 indicate that the branch is not expected to be taken. The lower
377 two bits must be zero. */
378 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 16, /* bitsize */
b34976b6 382 FALSE, /* pc_relative */
5bd4f169
AM
383 0, /* bitpos */
384 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 385 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 386 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 387 FALSE, /* partial_inplace */
d006db6c 388 0, /* src_mask */
f5e87a1d 389 0x0000fffc, /* dst_mask */
b34976b6 390 FALSE), /* pcrel_offset */
5bd4f169
AM
391
392 /* A relative 26 bit branch; the lower two bits must be zero. */
393 HOWTO (R_PPC64_REL24, /* type */
394 0, /* rightshift */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
396 26, /* bitsize */
b34976b6 397 TRUE, /* pc_relative */
5bd4f169
AM
398 0, /* bitpos */
399 complain_overflow_signed, /* complain_on_overflow */
2441e016 400 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 401 "R_PPC64_REL24", /* name */
b34976b6 402 FALSE, /* partial_inplace */
d006db6c 403 0, /* src_mask */
f5e87a1d 404 0x03fffffc, /* dst_mask */
b34976b6 405 TRUE), /* pcrel_offset */
5bd4f169
AM
406
407 /* A relative 16 bit branch; the lower two bits must be zero. */
408 HOWTO (R_PPC64_REL14, /* type */
409 0, /* rightshift */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
411 16, /* bitsize */
b34976b6 412 TRUE, /* pc_relative */
5bd4f169
AM
413 0, /* bitpos */
414 complain_overflow_signed, /* complain_on_overflow */
2441e016 415 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 416 "R_PPC64_REL14", /* name */
b34976b6 417 FALSE, /* partial_inplace */
d006db6c 418 0, /* src_mask */
f5e87a1d 419 0x0000fffc, /* dst_mask */
b34976b6 420 TRUE), /* pcrel_offset */
5bd4f169
AM
421
422 /* A relative 16 bit branch. Bit 10 should be set to indicate that
423 the branch is expected to be taken. The lower two bits must be
424 zero. */
425 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
426 0, /* rightshift */
427 2, /* size (0 = byte, 1 = short, 2 = long) */
428 16, /* bitsize */
b34976b6 429 TRUE, /* pc_relative */
5bd4f169
AM
430 0, /* bitpos */
431 complain_overflow_signed, /* complain_on_overflow */
805fc799 432 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 433 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 434 FALSE, /* partial_inplace */
d006db6c 435 0, /* src_mask */
f5e87a1d 436 0x0000fffc, /* dst_mask */
b34976b6 437 TRUE), /* pcrel_offset */
5bd4f169
AM
438
439 /* A relative 16 bit branch. Bit 10 should be set to indicate that
440 the branch is not expected to be taken. The lower two bits must
441 be zero. */
442 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
443 0, /* rightshift */
444 2, /* size (0 = byte, 1 = short, 2 = long) */
445 16, /* bitsize */
b34976b6 446 TRUE, /* pc_relative */
5bd4f169
AM
447 0, /* bitpos */
448 complain_overflow_signed, /* complain_on_overflow */
805fc799 449 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 450 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 451 FALSE, /* partial_inplace */
d006db6c 452 0, /* src_mask */
f5e87a1d 453 0x0000fffc, /* dst_mask */
b34976b6 454 TRUE), /* pcrel_offset */
5bd4f169
AM
455
456 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
457 symbol. */
458 HOWTO (R_PPC64_GOT16, /* type */
459 0, /* rightshift */
460 1, /* size (0 = byte, 1 = short, 2 = long) */
461 16, /* bitsize */
b34976b6 462 FALSE, /* pc_relative */
5bd4f169
AM
463 0, /* bitpos */
464 complain_overflow_signed, /* complain_on_overflow */
805fc799 465 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 466 "R_PPC64_GOT16", /* name */
b34976b6 467 FALSE, /* partial_inplace */
5bd4f169
AM
468 0, /* src_mask */
469 0xffff, /* dst_mask */
b34976b6 470 FALSE), /* pcrel_offset */
5bd4f169
AM
471
472 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
473 the symbol. */
474 HOWTO (R_PPC64_GOT16_LO, /* type */
475 0, /* rightshift */
476 1, /* size (0 = byte, 1 = short, 2 = long) */
477 16, /* bitsize */
b34976b6 478 FALSE, /* pc_relative */
5bd4f169
AM
479 0, /* bitpos */
480 complain_overflow_dont, /* complain_on_overflow */
805fc799 481 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 482 "R_PPC64_GOT16_LO", /* name */
b34976b6 483 FALSE, /* partial_inplace */
5bd4f169
AM
484 0, /* src_mask */
485 0xffff, /* dst_mask */
b34976b6 486 FALSE), /* pcrel_offset */
5bd4f169
AM
487
488 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
489 the symbol. */
490 HOWTO (R_PPC64_GOT16_HI, /* type */
491 16, /* rightshift */
492 1, /* size (0 = byte, 1 = short, 2 = long) */
493 16, /* bitsize */
b34976b6 494 FALSE, /* pc_relative */
5bd4f169 495 0, /* bitpos */
f9c6b907 496 complain_overflow_signed,/* complain_on_overflow */
805fc799 497 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 498 "R_PPC64_GOT16_HI", /* name */
b34976b6 499 FALSE, /* partial_inplace */
5bd4f169
AM
500 0, /* src_mask */
501 0xffff, /* dst_mask */
b34976b6 502 FALSE), /* pcrel_offset */
5bd4f169
AM
503
504 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
505 the symbol. */
506 HOWTO (R_PPC64_GOT16_HA, /* type */
507 16, /* rightshift */
508 1, /* size (0 = byte, 1 = short, 2 = long) */
509 16, /* bitsize */
b34976b6 510 FALSE, /* pc_relative */
5bd4f169 511 0, /* bitpos */
f9c6b907 512 complain_overflow_signed,/* complain_on_overflow */
805fc799 513 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 514 "R_PPC64_GOT16_HA", /* name */
b34976b6 515 FALSE, /* partial_inplace */
5bd4f169
AM
516 0, /* src_mask */
517 0xffff, /* dst_mask */
b34976b6 518 FALSE), /* pcrel_offset */
5bd4f169
AM
519
520 /* This is used only by the dynamic linker. The symbol should exist
521 both in the object being run and in some shared library. The
522 dynamic linker copies the data addressed by the symbol from the
523 shared library into the object, because the object being
524 run has to have the data at some particular address. */
525 HOWTO (R_PPC64_COPY, /* type */
526 0, /* rightshift */
f5e87a1d
AM
527 0, /* this one is variable size */
528 0, /* bitsize */
b34976b6 529 FALSE, /* pc_relative */
5bd4f169 530 0, /* bitpos */
f5e87a1d
AM
531 complain_overflow_dont, /* complain_on_overflow */
532 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 533 "R_PPC64_COPY", /* name */
b34976b6 534 FALSE, /* partial_inplace */
5bd4f169
AM
535 0, /* src_mask */
536 0, /* dst_mask */
b34976b6 537 FALSE), /* pcrel_offset */
5bd4f169
AM
538
539 /* Like R_PPC64_ADDR64, but used when setting global offset table
540 entries. */
541 HOWTO (R_PPC64_GLOB_DAT, /* type */
542 0, /* rightshift */
543 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
544 64, /* bitsize */
b34976b6 545 FALSE, /* pc_relative */
5bd4f169
AM
546 0, /* bitpos */
547 complain_overflow_dont, /* complain_on_overflow */
805fc799 548 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 549 "R_PPC64_GLOB_DAT", /* name */
b34976b6 550 FALSE, /* partial_inplace */
5bd4f169 551 0, /* src_mask */
f5e87a1d 552 ONES (64), /* dst_mask */
b34976b6 553 FALSE), /* pcrel_offset */
5bd4f169
AM
554
555 /* Created by the link editor. Marks a procedure linkage table
556 entry for a symbol. */
557 HOWTO (R_PPC64_JMP_SLOT, /* type */
558 0, /* rightshift */
559 0, /* size (0 = byte, 1 = short, 2 = long) */
560 0, /* bitsize */
b34976b6 561 FALSE, /* pc_relative */
5bd4f169
AM
562 0, /* bitpos */
563 complain_overflow_dont, /* complain_on_overflow */
805fc799 564 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 565 "R_PPC64_JMP_SLOT", /* name */
b34976b6 566 FALSE, /* partial_inplace */
5bd4f169
AM
567 0, /* src_mask */
568 0, /* dst_mask */
b34976b6 569 FALSE), /* pcrel_offset */
5bd4f169
AM
570
571 /* Used only by the dynamic linker. When the object is run, this
572 doubleword64 is set to the load address of the object, plus the
573 addend. */
574 HOWTO (R_PPC64_RELATIVE, /* type */
575 0, /* rightshift */
576 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
577 64, /* bitsize */
b34976b6 578 FALSE, /* pc_relative */
5bd4f169
AM
579 0, /* bitpos */
580 complain_overflow_dont, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_RELATIVE", /* name */
b34976b6 583 FALSE, /* partial_inplace */
5bd4f169 584 0, /* src_mask */
f5e87a1d 585 ONES (64), /* dst_mask */
b34976b6 586 FALSE), /* pcrel_offset */
5bd4f169
AM
587
588 /* Like R_PPC64_ADDR32, but may be unaligned. */
589 HOWTO (R_PPC64_UADDR32, /* type */
590 0, /* rightshift */
591 2, /* size (0 = byte, 1 = short, 2 = long) */
592 32, /* bitsize */
b34976b6 593 FALSE, /* pc_relative */
5bd4f169
AM
594 0, /* bitpos */
595 complain_overflow_bitfield, /* complain_on_overflow */
596 bfd_elf_generic_reloc, /* special_function */
597 "R_PPC64_UADDR32", /* name */
b34976b6 598 FALSE, /* partial_inplace */
5bd4f169
AM
599 0, /* src_mask */
600 0xffffffff, /* dst_mask */
b34976b6 601 FALSE), /* pcrel_offset */
5bd4f169
AM
602
603 /* Like R_PPC64_ADDR16, but may be unaligned. */
604 HOWTO (R_PPC64_UADDR16, /* type */
605 0, /* rightshift */
606 1, /* size (0 = byte, 1 = short, 2 = long) */
607 16, /* bitsize */
b34976b6 608 FALSE, /* pc_relative */
5bd4f169
AM
609 0, /* bitpos */
610 complain_overflow_bitfield, /* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_PPC64_UADDR16", /* name */
b34976b6 613 FALSE, /* partial_inplace */
5bd4f169
AM
614 0, /* src_mask */
615 0xffff, /* dst_mask */
b34976b6 616 FALSE), /* pcrel_offset */
5bd4f169
AM
617
618 /* 32-bit PC relative. */
619 HOWTO (R_PPC64_REL32, /* type */
620 0, /* rightshift */
621 2, /* size (0 = byte, 1 = short, 2 = long) */
622 32, /* bitsize */
b34976b6 623 TRUE, /* pc_relative */
5bd4f169 624 0, /* bitpos */
cedb70c5 625 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
626 complain_overflow_signed, /* complain_on_overflow */
627 bfd_elf_generic_reloc, /* special_function */
628 "R_PPC64_REL32", /* name */
b34976b6 629 FALSE, /* partial_inplace */
5bd4f169
AM
630 0, /* src_mask */
631 0xffffffff, /* dst_mask */
b34976b6 632 TRUE), /* pcrel_offset */
5bd4f169 633
10ed1bba 634 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
635 HOWTO (R_PPC64_PLT32, /* type */
636 0, /* rightshift */
637 2, /* size (0 = byte, 1 = short, 2 = long) */
638 32, /* bitsize */
b34976b6 639 FALSE, /* pc_relative */
5bd4f169
AM
640 0, /* bitpos */
641 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 642 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 643 "R_PPC64_PLT32", /* name */
b34976b6 644 FALSE, /* partial_inplace */
5bd4f169 645 0, /* src_mask */
f5e87a1d 646 0xffffffff, /* dst_mask */
b34976b6 647 FALSE), /* pcrel_offset */
5bd4f169
AM
648
649 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
650 FIXME: R_PPC64_PLTREL32 not supported. */
651 HOWTO (R_PPC64_PLTREL32, /* type */
652 0, /* rightshift */
653 2, /* size (0 = byte, 1 = short, 2 = long) */
654 32, /* bitsize */
b34976b6 655 TRUE, /* pc_relative */
5bd4f169
AM
656 0, /* bitpos */
657 complain_overflow_signed, /* complain_on_overflow */
658 bfd_elf_generic_reloc, /* special_function */
659 "R_PPC64_PLTREL32", /* name */
b34976b6 660 FALSE, /* partial_inplace */
5bd4f169 661 0, /* src_mask */
f5e87a1d 662 0xffffffff, /* dst_mask */
b34976b6 663 TRUE), /* pcrel_offset */
5bd4f169
AM
664
665 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
666 the symbol. */
667 HOWTO (R_PPC64_PLT16_LO, /* type */
668 0, /* rightshift */
669 1, /* size (0 = byte, 1 = short, 2 = long) */
670 16, /* bitsize */
b34976b6 671 FALSE, /* pc_relative */
5bd4f169
AM
672 0, /* bitpos */
673 complain_overflow_dont, /* complain_on_overflow */
805fc799 674 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 675 "R_PPC64_PLT16_LO", /* name */
b34976b6 676 FALSE, /* partial_inplace */
5bd4f169
AM
677 0, /* src_mask */
678 0xffff, /* dst_mask */
b34976b6 679 FALSE), /* pcrel_offset */
5bd4f169
AM
680
681 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
682 the symbol. */
683 HOWTO (R_PPC64_PLT16_HI, /* type */
684 16, /* rightshift */
685 1, /* size (0 = byte, 1 = short, 2 = long) */
686 16, /* bitsize */
b34976b6 687 FALSE, /* pc_relative */
5bd4f169 688 0, /* bitpos */
f9c6b907 689 complain_overflow_signed, /* complain_on_overflow */
805fc799 690 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 691 "R_PPC64_PLT16_HI", /* name */
b34976b6 692 FALSE, /* partial_inplace */
5bd4f169
AM
693 0, /* src_mask */
694 0xffff, /* dst_mask */
b34976b6 695 FALSE), /* pcrel_offset */
5bd4f169
AM
696
697 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
698 the symbol. */
699 HOWTO (R_PPC64_PLT16_HA, /* type */
700 16, /* rightshift */
701 1, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
b34976b6 703 FALSE, /* pc_relative */
5bd4f169 704 0, /* bitpos */
f9c6b907 705 complain_overflow_signed, /* complain_on_overflow */
805fc799 706 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 707 "R_PPC64_PLT16_HA", /* name */
b34976b6 708 FALSE, /* partial_inplace */
5bd4f169
AM
709 0, /* src_mask */
710 0xffff, /* dst_mask */
b34976b6 711 FALSE), /* pcrel_offset */
5bd4f169 712
c061c2d8 713 /* 16-bit section relative relocation. */
5bd4f169
AM
714 HOWTO (R_PPC64_SECTOFF, /* type */
715 0, /* rightshift */
c061c2d8
AM
716 1, /* size (0 = byte, 1 = short, 2 = long) */
717 16, /* bitsize */
b34976b6 718 FALSE, /* pc_relative */
5bd4f169
AM
719 0, /* bitpos */
720 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 721 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 722 "R_PPC64_SECTOFF", /* name */
b34976b6 723 FALSE, /* partial_inplace */
5bd4f169 724 0, /* src_mask */
c061c2d8 725 0xffff, /* dst_mask */
b34976b6 726 FALSE), /* pcrel_offset */
5bd4f169 727
c061c2d8 728 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
729 HOWTO (R_PPC64_SECTOFF_LO, /* type */
730 0, /* rightshift */
731 1, /* size (0 = byte, 1 = short, 2 = long) */
732 16, /* bitsize */
b34976b6 733 FALSE, /* pc_relative */
5bd4f169
AM
734 0, /* bitpos */
735 complain_overflow_dont, /* complain_on_overflow */
805fc799 736 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 737 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 738 FALSE, /* partial_inplace */
5bd4f169
AM
739 0, /* src_mask */
740 0xffff, /* dst_mask */
b34976b6 741 FALSE), /* pcrel_offset */
5bd4f169
AM
742
743 /* 16-bit upper half section relative relocation. */
744 HOWTO (R_PPC64_SECTOFF_HI, /* type */
745 16, /* rightshift */
746 1, /* size (0 = byte, 1 = short, 2 = long) */
747 16, /* bitsize */
b34976b6 748 FALSE, /* pc_relative */
5bd4f169 749 0, /* bitpos */
f9c6b907 750 complain_overflow_signed, /* complain_on_overflow */
805fc799 751 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 752 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 753 FALSE, /* partial_inplace */
5bd4f169
AM
754 0, /* src_mask */
755 0xffff, /* dst_mask */
b34976b6 756 FALSE), /* pcrel_offset */
5bd4f169
AM
757
758 /* 16-bit upper half adjusted section relative relocation. */
759 HOWTO (R_PPC64_SECTOFF_HA, /* type */
760 16, /* rightshift */
761 1, /* size (0 = byte, 1 = short, 2 = long) */
762 16, /* bitsize */
b34976b6 763 FALSE, /* pc_relative */
5bd4f169 764 0, /* bitpos */
f9c6b907 765 complain_overflow_signed, /* complain_on_overflow */
805fc799 766 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 767 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 768 FALSE, /* partial_inplace */
5bd4f169
AM
769 0, /* src_mask */
770 0xffff, /* dst_mask */
b34976b6 771 FALSE), /* pcrel_offset */
5bd4f169 772
04c9666a
AM
773 /* Like R_PPC64_REL24 without touching the two least significant bits. */
774 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
775 2, /* rightshift */
776 2, /* size (0 = byte, 1 = short, 2 = long) */
777 30, /* bitsize */
b34976b6 778 TRUE, /* pc_relative */
5bd4f169
AM
779 0, /* bitpos */
780 complain_overflow_dont, /* complain_on_overflow */
781 bfd_elf_generic_reloc, /* special_function */
04c9666a 782 "R_PPC64_REL30", /* name */
b34976b6 783 FALSE, /* partial_inplace */
d006db6c 784 0, /* src_mask */
5bd4f169 785 0xfffffffc, /* dst_mask */
b34976b6 786 TRUE), /* pcrel_offset */
5bd4f169
AM
787
788 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
789
790 /* A standard 64-bit relocation. */
791 HOWTO (R_PPC64_ADDR64, /* type */
792 0, /* rightshift */
793 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
794 64, /* bitsize */
b34976b6 795 FALSE, /* pc_relative */
5bd4f169
AM
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC64_ADDR64", /* name */
b34976b6 800 FALSE, /* partial_inplace */
5bd4f169 801 0, /* src_mask */
f5e87a1d 802 ONES (64), /* dst_mask */
b34976b6 803 FALSE), /* pcrel_offset */
5bd4f169
AM
804
805 /* The bits 32-47 of an address. */
806 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
807 32, /* rightshift */
808 1, /* size (0 = byte, 1 = short, 2 = long) */
809 16, /* bitsize */
b34976b6 810 FALSE, /* pc_relative */
5bd4f169
AM
811 0, /* bitpos */
812 complain_overflow_dont, /* complain_on_overflow */
813 bfd_elf_generic_reloc, /* special_function */
814 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 815 FALSE, /* partial_inplace */
5bd4f169
AM
816 0, /* src_mask */
817 0xffff, /* dst_mask */
b34976b6 818 FALSE), /* pcrel_offset */
5bd4f169
AM
819
820 /* The bits 32-47 of an address, plus 1 if the contents of the low
821 16 bits, treated as a signed number, is negative. */
822 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
823 32, /* rightshift */
824 1, /* size (0 = byte, 1 = short, 2 = long) */
825 16, /* bitsize */
b34976b6 826 FALSE, /* pc_relative */
5bd4f169
AM
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
805fc799 829 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 830 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 831 FALSE, /* partial_inplace */
5bd4f169
AM
832 0, /* src_mask */
833 0xffff, /* dst_mask */
b34976b6 834 FALSE), /* pcrel_offset */
5bd4f169
AM
835
836 /* The bits 48-63 of an address. */
837 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
838 48, /* rightshift */
839 1, /* size (0 = byte, 1 = short, 2 = long) */
840 16, /* bitsize */
b34976b6 841 FALSE, /* pc_relative */
5bd4f169
AM
842 0, /* bitpos */
843 complain_overflow_dont, /* complain_on_overflow */
844 bfd_elf_generic_reloc, /* special_function */
845 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 846 FALSE, /* partial_inplace */
5bd4f169
AM
847 0, /* src_mask */
848 0xffff, /* dst_mask */
b34976b6 849 FALSE), /* pcrel_offset */
5bd4f169
AM
850
851 /* The bits 48-63 of an address, plus 1 if the contents of the low
852 16 bits, treated as a signed number, is negative. */
853 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
854 48, /* rightshift */
855 1, /* size (0 = byte, 1 = short, 2 = long) */
856 16, /* bitsize */
b34976b6 857 FALSE, /* pc_relative */
5bd4f169
AM
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
805fc799 860 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 861 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 862 FALSE, /* partial_inplace */
5bd4f169
AM
863 0, /* src_mask */
864 0xffff, /* dst_mask */
b34976b6 865 FALSE), /* pcrel_offset */
5bd4f169
AM
866
867 /* Like ADDR64, but may be unaligned. */
868 HOWTO (R_PPC64_UADDR64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
b34976b6 872 FALSE, /* pc_relative */
5bd4f169
AM
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 "R_PPC64_UADDR64", /* name */
b34976b6 877 FALSE, /* partial_inplace */
5bd4f169 878 0, /* src_mask */
f5e87a1d 879 ONES (64), /* dst_mask */
b34976b6 880 FALSE), /* pcrel_offset */
5bd4f169
AM
881
882 /* 64-bit relative relocation. */
883 HOWTO (R_PPC64_REL64, /* type */
884 0, /* rightshift */
885 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886 64, /* bitsize */
b34976b6 887 TRUE, /* pc_relative */
5bd4f169
AM
888 0, /* bitpos */
889 complain_overflow_dont, /* complain_on_overflow */
890 bfd_elf_generic_reloc, /* special_function */
891 "R_PPC64_REL64", /* name */
b34976b6 892 FALSE, /* partial_inplace */
5bd4f169 893 0, /* src_mask */
f5e87a1d 894 ONES (64), /* dst_mask */
b34976b6 895 TRUE), /* pcrel_offset */
5bd4f169 896
cedb70c5 897 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
898 HOWTO (R_PPC64_PLT64, /* type */
899 0, /* rightshift */
900 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
901 64, /* bitsize */
b34976b6 902 FALSE, /* pc_relative */
5bd4f169
AM
903 0, /* bitpos */
904 complain_overflow_dont, /* complain_on_overflow */
805fc799 905 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 906 "R_PPC64_PLT64", /* name */
b34976b6 907 FALSE, /* partial_inplace */
5bd4f169 908 0, /* src_mask */
f5e87a1d 909 ONES (64), /* dst_mask */
b34976b6 910 FALSE), /* pcrel_offset */
5bd4f169
AM
911
912 /* 64-bit PC relative relocation to the symbol's procedure linkage
913 table. */
914 /* FIXME: R_PPC64_PLTREL64 not supported. */
915 HOWTO (R_PPC64_PLTREL64, /* type */
916 0, /* rightshift */
917 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
918 64, /* bitsize */
b34976b6 919 TRUE, /* pc_relative */
5bd4f169
AM
920 0, /* bitpos */
921 complain_overflow_dont, /* complain_on_overflow */
805fc799 922 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 923 "R_PPC64_PLTREL64", /* name */
b34976b6 924 FALSE, /* partial_inplace */
5bd4f169 925 0, /* src_mask */
f5e87a1d 926 ONES (64), /* dst_mask */
b34976b6 927 TRUE), /* pcrel_offset */
5bd4f169
AM
928
929 /* 16 bit TOC-relative relocation. */
930
931 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
932 HOWTO (R_PPC64_TOC16, /* type */
933 0, /* rightshift */
934 1, /* size (0 = byte, 1 = short, 2 = long) */
935 16, /* bitsize */
b34976b6 936 FALSE, /* pc_relative */
5bd4f169
AM
937 0, /* bitpos */
938 complain_overflow_signed, /* complain_on_overflow */
805fc799 939 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 940 "R_PPC64_TOC16", /* name */
b34976b6 941 FALSE, /* partial_inplace */
5bd4f169
AM
942 0, /* src_mask */
943 0xffff, /* dst_mask */
b34976b6 944 FALSE), /* pcrel_offset */
5bd4f169
AM
945
946 /* 16 bit TOC-relative relocation without overflow. */
947
948 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
949 HOWTO (R_PPC64_TOC16_LO, /* type */
950 0, /* rightshift */
951 1, /* size (0 = byte, 1 = short, 2 = long) */
952 16, /* bitsize */
b34976b6 953 FALSE, /* pc_relative */
5bd4f169
AM
954 0, /* bitpos */
955 complain_overflow_dont, /* complain_on_overflow */
805fc799 956 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 957 "R_PPC64_TOC16_LO", /* name */
b34976b6 958 FALSE, /* partial_inplace */
5bd4f169
AM
959 0, /* src_mask */
960 0xffff, /* dst_mask */
b34976b6 961 FALSE), /* pcrel_offset */
5bd4f169
AM
962
963 /* 16 bit TOC-relative relocation, high 16 bits. */
964
965 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
966 HOWTO (R_PPC64_TOC16_HI, /* type */
967 16, /* rightshift */
968 1, /* size (0 = byte, 1 = short, 2 = long) */
969 16, /* bitsize */
b34976b6 970 FALSE, /* pc_relative */
5bd4f169 971 0, /* bitpos */
f9c6b907 972 complain_overflow_signed, /* complain_on_overflow */
805fc799 973 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 974 "R_PPC64_TOC16_HI", /* name */
b34976b6 975 FALSE, /* partial_inplace */
5bd4f169
AM
976 0, /* src_mask */
977 0xffff, /* dst_mask */
b34976b6 978 FALSE), /* pcrel_offset */
5bd4f169
AM
979
980 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
981 contents of the low 16 bits, treated as a signed number, is
982 negative. */
983
984 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
985 HOWTO (R_PPC64_TOC16_HA, /* type */
986 16, /* rightshift */
987 1, /* size (0 = byte, 1 = short, 2 = long) */
988 16, /* bitsize */
b34976b6 989 FALSE, /* pc_relative */
5bd4f169 990 0, /* bitpos */
f9c6b907 991 complain_overflow_signed, /* complain_on_overflow */
805fc799 992 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 993 "R_PPC64_TOC16_HA", /* name */
b34976b6 994 FALSE, /* partial_inplace */
5bd4f169
AM
995 0, /* src_mask */
996 0xffff, /* dst_mask */
b34976b6 997 FALSE), /* pcrel_offset */
5bd4f169
AM
998
999 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1000
1001 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1002 HOWTO (R_PPC64_TOC, /* type */
1003 0, /* rightshift */
1004 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1005 64, /* bitsize */
b34976b6 1006 FALSE, /* pc_relative */
5bd4f169
AM
1007 0, /* bitpos */
1008 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 1009 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1010 "R_PPC64_TOC", /* name */
b34976b6 1011 FALSE, /* partial_inplace */
5bd4f169 1012 0, /* src_mask */
f5e87a1d 1013 ONES (64), /* dst_mask */
b34976b6 1014 FALSE), /* pcrel_offset */
5bd4f169
AM
1015
1016 /* Like R_PPC64_GOT16, but also informs the link editor that the
1017 value to relocate may (!) refer to a PLT entry which the link
1018 editor (a) may replace with the symbol value. If the link editor
1019 is unable to fully resolve the symbol, it may (b) create a PLT
1020 entry and store the address to the new PLT entry in the GOT.
1021 This permits lazy resolution of function symbols at run time.
1022 The link editor may also skip all of this and just (c) emit a
1023 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1024 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1025 HOWTO (R_PPC64_PLTGOT16, /* type */
1026 0, /* rightshift */
1027 1, /* size (0 = byte, 1 = short, 2 = long) */
1028 16, /* bitsize */
b34976b6 1029 FALSE, /* pc_relative */
5bd4f169
AM
1030 0, /* bitpos */
1031 complain_overflow_signed, /* complain_on_overflow */
805fc799 1032 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1033 "R_PPC64_PLTGOT16", /* name */
1034 FALSE, /* partial_inplace */
1035 0, /* src_mask */
1036 0xffff, /* dst_mask */
1037 FALSE), /* pcrel_offset */
1038
1039 /* Like R_PPC64_PLTGOT16, but without overflow. */
1040 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1041 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1042 0, /* rightshift */
1043 1, /* size (0 = byte, 1 = short, 2 = long) */
1044 16, /* bitsize */
1045 FALSE, /* pc_relative */
1046 0, /* bitpos */
1047 complain_overflow_dont, /* complain_on_overflow */
1048 ppc64_elf_unhandled_reloc, /* special_function */
1049 "R_PPC64_PLTGOT16_LO", /* name */
1050 FALSE, /* partial_inplace */
1051 0, /* src_mask */
1052 0xffff, /* dst_mask */
1053 FALSE), /* pcrel_offset */
1054
1055 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1056 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1057 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1058 16, /* rightshift */
1059 1, /* size (0 = byte, 1 = short, 2 = long) */
1060 16, /* bitsize */
1061 FALSE, /* pc_relative */
1062 0, /* bitpos */
f9c6b907 1063 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1064 ppc64_elf_unhandled_reloc, /* special_function */
1065 "R_PPC64_PLTGOT16_HI", /* name */
1066 FALSE, /* partial_inplace */
1067 0, /* src_mask */
1068 0xffff, /* dst_mask */
1069 FALSE), /* pcrel_offset */
1070
1071 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1072 1 if the contents of the low 16 bits, treated as a signed number,
1073 is negative. */
1074 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1075 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1076 16, /* rightshift */
1077 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 FALSE, /* pc_relative */
1080 0, /* bitpos */
f9c6b907 1081 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1082 ppc64_elf_unhandled_reloc, /* special_function */
1083 "R_PPC64_PLTGOT16_HA", /* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xffff, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1088
1089 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_ADDR16_DS, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_bitfield, /* complain_on_overflow */
1097 bfd_elf_generic_reloc, /* special_function */
1098 "R_PPC64_ADDR16_DS", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_dont,/* complain_on_overflow */
1112 bfd_elf_generic_reloc, /* special_function */
1113 "R_PPC64_ADDR16_LO_DS",/* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1118
1119 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_GOT16_DS, /* type */
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_signed, /* complain_on_overflow */
1127 ppc64_elf_unhandled_reloc, /* special_function */
1128 "R_PPC64_GOT16_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1133
1134 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc64_elf_unhandled_reloc, /* special_function */
1143 "R_PPC64_GOT16_LO_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1148
1149 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_dont, /* complain_on_overflow */
1157 ppc64_elf_unhandled_reloc, /* special_function */
1158 "R_PPC64_PLT16_LO_DS", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1163
1164 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1166 0, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_bitfield, /* complain_on_overflow */
1172 ppc64_elf_sectoff_reloc, /* special_function */
1173 "R_PPC64_SECTOFF_DS", /* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1178
1179 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_dont, /* complain_on_overflow */
1187 ppc64_elf_sectoff_reloc, /* special_function */
1188 "R_PPC64_SECTOFF_LO_DS",/* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1193
1194 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1195 HOWTO (R_PPC64_TOC16_DS, /* type */
1196 0, /* rightshift */
1197 1, /* size (0 = byte, 1 = short, 2 = long) */
1198 16, /* bitsize */
1199 FALSE, /* pc_relative */
1200 0, /* bitpos */
1201 complain_overflow_signed, /* complain_on_overflow */
1202 ppc64_elf_toc_reloc, /* special_function */
1203 "R_PPC64_TOC16_DS", /* name */
1204 FALSE, /* partial_inplace */
1205 0, /* src_mask */
1206 0xfffc, /* dst_mask */
1207 FALSE), /* pcrel_offset */
1208
1209 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1210 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1211 0, /* rightshift */
1212 1, /* size (0 = byte, 1 = short, 2 = long) */
1213 16, /* bitsize */
1214 FALSE, /* pc_relative */
1215 0, /* bitpos */
1216 complain_overflow_dont, /* complain_on_overflow */
1217 ppc64_elf_toc_reloc, /* special_function */
1218 "R_PPC64_TOC16_LO_DS", /* name */
1219 FALSE, /* partial_inplace */
1220 0, /* src_mask */
1221 0xfffc, /* dst_mask */
1222 FALSE), /* pcrel_offset */
1223
1224 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1225 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1226 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1227 0, /* rightshift */
1228 1, /* size (0 = byte, 1 = short, 2 = long) */
1229 16, /* bitsize */
1230 FALSE, /* pc_relative */
1231 0, /* bitpos */
1232 complain_overflow_signed, /* complain_on_overflow */
1233 ppc64_elf_unhandled_reloc, /* special_function */
1234 "R_PPC64_PLTGOT16_DS", /* name */
1235 FALSE, /* partial_inplace */
1236 0, /* src_mask */
1237 0xfffc, /* dst_mask */
1238 FALSE), /* pcrel_offset */
1239
1240 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1241 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1242 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1243 0, /* rightshift */
1244 1, /* size (0 = byte, 1 = short, 2 = long) */
1245 16, /* bitsize */
1246 FALSE, /* pc_relative */
1247 0, /* bitpos */
1248 complain_overflow_dont, /* complain_on_overflow */
1249 ppc64_elf_unhandled_reloc, /* special_function */
1250 "R_PPC64_PLTGOT16_LO_DS",/* name */
1251 FALSE, /* partial_inplace */
1252 0, /* src_mask */
1253 0xfffc, /* dst_mask */
1254 FALSE), /* pcrel_offset */
1255
727fc41e 1256 /* Marker relocs for TLS. */
411e1bfb
AM
1257 HOWTO (R_PPC64_TLS,
1258 0, /* rightshift */
1259 2, /* size (0 = byte, 1 = short, 2 = long) */
1260 32, /* bitsize */
1261 FALSE, /* pc_relative */
1262 0, /* bitpos */
1263 complain_overflow_dont, /* complain_on_overflow */
1264 bfd_elf_generic_reloc, /* special_function */
1265 "R_PPC64_TLS", /* name */
1266 FALSE, /* partial_inplace */
1267 0, /* src_mask */
1268 0, /* dst_mask */
1269 FALSE), /* pcrel_offset */
1270
727fc41e
AM
1271 HOWTO (R_PPC64_TLSGD,
1272 0, /* rightshift */
1273 2, /* size (0 = byte, 1 = short, 2 = long) */
1274 32, /* bitsize */
1275 FALSE, /* pc_relative */
1276 0, /* bitpos */
1277 complain_overflow_dont, /* complain_on_overflow */
1278 bfd_elf_generic_reloc, /* special_function */
1279 "R_PPC64_TLSGD", /* name */
1280 FALSE, /* partial_inplace */
1281 0, /* src_mask */
1282 0, /* dst_mask */
1283 FALSE), /* pcrel_offset */
1284
1285 HOWTO (R_PPC64_TLSLD,
1286 0, /* rightshift */
1287 2, /* size (0 = byte, 1 = short, 2 = long) */
1288 32, /* bitsize */
1289 FALSE, /* pc_relative */
1290 0, /* bitpos */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 bfd_elf_generic_reloc, /* special_function */
1293 "R_PPC64_TLSLD", /* name */
1294 FALSE, /* partial_inplace */
1295 0, /* src_mask */
1296 0, /* dst_mask */
1297 FALSE), /* pcrel_offset */
1298
3b421ab3
AM
1299 HOWTO (R_PPC64_TOCSAVE,
1300 0, /* rightshift */
1301 2, /* size (0 = byte, 1 = short, 2 = long) */
1302 32, /* bitsize */
1303 FALSE, /* pc_relative */
1304 0, /* bitpos */
1305 complain_overflow_dont, /* complain_on_overflow */
1306 bfd_elf_generic_reloc, /* special_function */
1307 "R_PPC64_TOCSAVE", /* name */
1308 FALSE, /* partial_inplace */
1309 0, /* src_mask */
1310 0, /* dst_mask */
1311 FALSE), /* pcrel_offset */
1312
411e1bfb
AM
1313 /* Computes the load module index of the load module that contains the
1314 definition of its TLS sym. */
1315 HOWTO (R_PPC64_DTPMOD64,
1316 0, /* rightshift */
1317 4, /* size (0 = byte, 1 = short, 2 = long) */
1318 64, /* bitsize */
1319 FALSE, /* pc_relative */
1320 0, /* bitpos */
1321 complain_overflow_dont, /* complain_on_overflow */
1322 ppc64_elf_unhandled_reloc, /* special_function */
1323 "R_PPC64_DTPMOD64", /* name */
1324 FALSE, /* partial_inplace */
1325 0, /* src_mask */
1326 ONES (64), /* dst_mask */
1327 FALSE), /* pcrel_offset */
1328
1329 /* Computes a dtv-relative displacement, the difference between the value
1330 of sym+add and the base address of the thread-local storage block that
1331 contains the definition of sym, minus 0x8000. */
1332 HOWTO (R_PPC64_DTPREL64,
1333 0, /* rightshift */
1334 4, /* size (0 = byte, 1 = short, 2 = long) */
1335 64, /* bitsize */
1336 FALSE, /* pc_relative */
1337 0, /* bitpos */
1338 complain_overflow_dont, /* complain_on_overflow */
1339 ppc64_elf_unhandled_reloc, /* special_function */
1340 "R_PPC64_DTPREL64", /* name */
1341 FALSE, /* partial_inplace */
1342 0, /* src_mask */
1343 ONES (64), /* dst_mask */
1344 FALSE), /* pcrel_offset */
1345
1346 /* A 16 bit dtprel reloc. */
1347 HOWTO (R_PPC64_DTPREL16,
1348 0, /* rightshift */
1349 1, /* size (0 = byte, 1 = short, 2 = long) */
1350 16, /* bitsize */
1351 FALSE, /* pc_relative */
1352 0, /* bitpos */
1353 complain_overflow_signed, /* complain_on_overflow */
1354 ppc64_elf_unhandled_reloc, /* special_function */
1355 "R_PPC64_DTPREL16", /* name */
1356 FALSE, /* partial_inplace */
1357 0, /* src_mask */
1358 0xffff, /* dst_mask */
1359 FALSE), /* pcrel_offset */
1360
1361 /* Like DTPREL16, but no overflow. */
1362 HOWTO (R_PPC64_DTPREL16_LO,
1363 0, /* rightshift */
1364 1, /* size (0 = byte, 1 = short, 2 = long) */
1365 16, /* bitsize */
1366 FALSE, /* pc_relative */
1367 0, /* bitpos */
1368 complain_overflow_dont, /* complain_on_overflow */
1369 ppc64_elf_unhandled_reloc, /* special_function */
1370 "R_PPC64_DTPREL16_LO", /* name */
1371 FALSE, /* partial_inplace */
1372 0, /* src_mask */
1373 0xffff, /* dst_mask */
1374 FALSE), /* pcrel_offset */
1375
1376 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1377 HOWTO (R_PPC64_DTPREL16_HI,
1378 16, /* rightshift */
1379 1, /* size (0 = byte, 1 = short, 2 = long) */
1380 16, /* bitsize */
1381 FALSE, /* pc_relative */
1382 0, /* bitpos */
f9c6b907 1383 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1384 ppc64_elf_unhandled_reloc, /* special_function */
1385 "R_PPC64_DTPREL16_HI", /* name */
1386 FALSE, /* partial_inplace */
1387 0, /* src_mask */
1388 0xffff, /* dst_mask */
1389 FALSE), /* pcrel_offset */
1390
1391 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1392 HOWTO (R_PPC64_DTPREL16_HA,
1393 16, /* rightshift */
1394 1, /* size (0 = byte, 1 = short, 2 = long) */
1395 16, /* bitsize */
1396 FALSE, /* pc_relative */
1397 0, /* bitpos */
f9c6b907 1398 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1399 ppc64_elf_unhandled_reloc, /* special_function */
1400 "R_PPC64_DTPREL16_HA", /* name */
1401 FALSE, /* partial_inplace */
1402 0, /* src_mask */
1403 0xffff, /* dst_mask */
1404 FALSE), /* pcrel_offset */
1405
1406 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1407 HOWTO (R_PPC64_DTPREL16_HIGHER,
1408 32, /* rightshift */
1409 1, /* size (0 = byte, 1 = short, 2 = long) */
1410 16, /* bitsize */
1411 FALSE, /* pc_relative */
1412 0, /* bitpos */
1413 complain_overflow_dont, /* complain_on_overflow */
1414 ppc64_elf_unhandled_reloc, /* special_function */
1415 "R_PPC64_DTPREL16_HIGHER", /* name */
1416 FALSE, /* partial_inplace */
1417 0, /* src_mask */
1418 0xffff, /* dst_mask */
1419 FALSE), /* pcrel_offset */
1420
1421 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1422 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1423 32, /* rightshift */
1424 1, /* size (0 = byte, 1 = short, 2 = long) */
1425 16, /* bitsize */
1426 FALSE, /* pc_relative */
1427 0, /* bitpos */
1428 complain_overflow_dont, /* complain_on_overflow */
1429 ppc64_elf_unhandled_reloc, /* special_function */
1430 "R_PPC64_DTPREL16_HIGHERA", /* name */
1431 FALSE, /* partial_inplace */
1432 0, /* src_mask */
1433 0xffff, /* dst_mask */
1434 FALSE), /* pcrel_offset */
1435
1436 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1437 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1438 48, /* rightshift */
1439 1, /* size (0 = byte, 1 = short, 2 = long) */
1440 16, /* bitsize */
1441 FALSE, /* pc_relative */
1442 0, /* bitpos */
1443 complain_overflow_dont, /* complain_on_overflow */
1444 ppc64_elf_unhandled_reloc, /* special_function */
1445 "R_PPC64_DTPREL16_HIGHEST", /* name */
1446 FALSE, /* partial_inplace */
1447 0, /* src_mask */
1448 0xffff, /* dst_mask */
1449 FALSE), /* pcrel_offset */
1450
1451 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1452 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1453 48, /* rightshift */
1454 1, /* size (0 = byte, 1 = short, 2 = long) */
1455 16, /* bitsize */
1456 FALSE, /* pc_relative */
1457 0, /* bitpos */
1458 complain_overflow_dont, /* complain_on_overflow */
1459 ppc64_elf_unhandled_reloc, /* special_function */
1460 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1461 FALSE, /* partial_inplace */
1462 0, /* src_mask */
1463 0xffff, /* dst_mask */
1464 FALSE), /* pcrel_offset */
1465
1466 /* Like DTPREL16, but for insns with a DS field. */
1467 HOWTO (R_PPC64_DTPREL16_DS,
1468 0, /* rightshift */
1469 1, /* size (0 = byte, 1 = short, 2 = long) */
1470 16, /* bitsize */
1471 FALSE, /* pc_relative */
1472 0, /* bitpos */
1473 complain_overflow_signed, /* complain_on_overflow */
1474 ppc64_elf_unhandled_reloc, /* special_function */
1475 "R_PPC64_DTPREL16_DS", /* name */
1476 FALSE, /* partial_inplace */
1477 0, /* src_mask */
1478 0xfffc, /* dst_mask */
1479 FALSE), /* pcrel_offset */
1480
1481 /* Like DTPREL16_DS, but no overflow. */
1482 HOWTO (R_PPC64_DTPREL16_LO_DS,
1483 0, /* rightshift */
1484 1, /* size (0 = byte, 1 = short, 2 = long) */
1485 16, /* bitsize */
1486 FALSE, /* pc_relative */
1487 0, /* bitpos */
1488 complain_overflow_dont, /* complain_on_overflow */
1489 ppc64_elf_unhandled_reloc, /* special_function */
1490 "R_PPC64_DTPREL16_LO_DS", /* name */
1491 FALSE, /* partial_inplace */
1492 0, /* src_mask */
1493 0xfffc, /* dst_mask */
1494 FALSE), /* pcrel_offset */
1495
1496 /* Computes a tp-relative displacement, the difference between the value of
1497 sym+add and the value of the thread pointer (r13). */
1498 HOWTO (R_PPC64_TPREL64,
1499 0, /* rightshift */
1500 4, /* size (0 = byte, 1 = short, 2 = long) */
1501 64, /* bitsize */
1502 FALSE, /* pc_relative */
1503 0, /* bitpos */
1504 complain_overflow_dont, /* complain_on_overflow */
1505 ppc64_elf_unhandled_reloc, /* special_function */
1506 "R_PPC64_TPREL64", /* name */
1507 FALSE, /* partial_inplace */
1508 0, /* src_mask */
1509 ONES (64), /* dst_mask */
1510 FALSE), /* pcrel_offset */
1511
1512 /* A 16 bit tprel reloc. */
1513 HOWTO (R_PPC64_TPREL16,
1514 0, /* rightshift */
1515 1, /* size (0 = byte, 1 = short, 2 = long) */
1516 16, /* bitsize */
1517 FALSE, /* pc_relative */
1518 0, /* bitpos */
1519 complain_overflow_signed, /* complain_on_overflow */
1520 ppc64_elf_unhandled_reloc, /* special_function */
1521 "R_PPC64_TPREL16", /* name */
1522 FALSE, /* partial_inplace */
1523 0, /* src_mask */
1524 0xffff, /* dst_mask */
1525 FALSE), /* pcrel_offset */
1526
1527 /* Like TPREL16, but no overflow. */
1528 HOWTO (R_PPC64_TPREL16_LO,
1529 0, /* rightshift */
1530 1, /* size (0 = byte, 1 = short, 2 = long) */
1531 16, /* bitsize */
1532 FALSE, /* pc_relative */
1533 0, /* bitpos */
1534 complain_overflow_dont, /* complain_on_overflow */
1535 ppc64_elf_unhandled_reloc, /* special_function */
1536 "R_PPC64_TPREL16_LO", /* name */
1537 FALSE, /* partial_inplace */
1538 0, /* src_mask */
1539 0xffff, /* dst_mask */
1540 FALSE), /* pcrel_offset */
1541
1542 /* Like TPREL16_LO, but next higher group of 16 bits. */
1543 HOWTO (R_PPC64_TPREL16_HI,
1544 16, /* rightshift */
1545 1, /* size (0 = byte, 1 = short, 2 = long) */
1546 16, /* bitsize */
1547 FALSE, /* pc_relative */
1548 0, /* bitpos */
f9c6b907 1549 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1550 ppc64_elf_unhandled_reloc, /* special_function */
1551 "R_PPC64_TPREL16_HI", /* name */
1552 FALSE, /* partial_inplace */
1553 0, /* src_mask */
1554 0xffff, /* dst_mask */
1555 FALSE), /* pcrel_offset */
1556
1557 /* Like TPREL16_HI, but adjust for low 16 bits. */
1558 HOWTO (R_PPC64_TPREL16_HA,
1559 16, /* rightshift */
1560 1, /* size (0 = byte, 1 = short, 2 = long) */
1561 16, /* bitsize */
1562 FALSE, /* pc_relative */
1563 0, /* bitpos */
f9c6b907 1564 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1565 ppc64_elf_unhandled_reloc, /* special_function */
1566 "R_PPC64_TPREL16_HA", /* name */
1567 FALSE, /* partial_inplace */
1568 0, /* src_mask */
1569 0xffff, /* dst_mask */
1570 FALSE), /* pcrel_offset */
1571
1572 /* Like TPREL16_HI, but next higher group of 16 bits. */
1573 HOWTO (R_PPC64_TPREL16_HIGHER,
1574 32, /* rightshift */
1575 1, /* size (0 = byte, 1 = short, 2 = long) */
1576 16, /* bitsize */
1577 FALSE, /* pc_relative */
1578 0, /* bitpos */
1579 complain_overflow_dont, /* complain_on_overflow */
1580 ppc64_elf_unhandled_reloc, /* special_function */
1581 "R_PPC64_TPREL16_HIGHER", /* name */
1582 FALSE, /* partial_inplace */
1583 0, /* src_mask */
1584 0xffff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586
1587 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1588 HOWTO (R_PPC64_TPREL16_HIGHERA,
1589 32, /* rightshift */
1590 1, /* size (0 = byte, 1 = short, 2 = long) */
1591 16, /* bitsize */
1592 FALSE, /* pc_relative */
1593 0, /* bitpos */
1594 complain_overflow_dont, /* complain_on_overflow */
1595 ppc64_elf_unhandled_reloc, /* special_function */
1596 "R_PPC64_TPREL16_HIGHERA", /* name */
1597 FALSE, /* partial_inplace */
1598 0, /* src_mask */
1599 0xffff, /* dst_mask */
1600 FALSE), /* pcrel_offset */
1601
1602 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1603 HOWTO (R_PPC64_TPREL16_HIGHEST,
1604 48, /* rightshift */
1605 1, /* size (0 = byte, 1 = short, 2 = long) */
1606 16, /* bitsize */
1607 FALSE, /* pc_relative */
1608 0, /* bitpos */
1609 complain_overflow_dont, /* complain_on_overflow */
1610 ppc64_elf_unhandled_reloc, /* special_function */
1611 "R_PPC64_TPREL16_HIGHEST", /* name */
1612 FALSE, /* partial_inplace */
1613 0, /* src_mask */
1614 0xffff, /* dst_mask */
1615 FALSE), /* pcrel_offset */
1616
1617 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1618 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1619 48, /* rightshift */
1620 1, /* size (0 = byte, 1 = short, 2 = long) */
1621 16, /* bitsize */
1622 FALSE, /* pc_relative */
1623 0, /* bitpos */
1624 complain_overflow_dont, /* complain_on_overflow */
1625 ppc64_elf_unhandled_reloc, /* special_function */
1626 "R_PPC64_TPREL16_HIGHESTA", /* name */
1627 FALSE, /* partial_inplace */
1628 0, /* src_mask */
1629 0xffff, /* dst_mask */
1630 FALSE), /* pcrel_offset */
1631
1632 /* Like TPREL16, but for insns with a DS field. */
1633 HOWTO (R_PPC64_TPREL16_DS,
1634 0, /* rightshift */
1635 1, /* size (0 = byte, 1 = short, 2 = long) */
1636 16, /* bitsize */
1637 FALSE, /* pc_relative */
1638 0, /* bitpos */
1639 complain_overflow_signed, /* complain_on_overflow */
1640 ppc64_elf_unhandled_reloc, /* special_function */
1641 "R_PPC64_TPREL16_DS", /* name */
1642 FALSE, /* partial_inplace */
1643 0, /* src_mask */
1644 0xfffc, /* dst_mask */
1645 FALSE), /* pcrel_offset */
1646
1647 /* Like TPREL16_DS, but no overflow. */
1648 HOWTO (R_PPC64_TPREL16_LO_DS,
1649 0, /* rightshift */
1650 1, /* size (0 = byte, 1 = short, 2 = long) */
1651 16, /* bitsize */
1652 FALSE, /* pc_relative */
1653 0, /* bitpos */
1654 complain_overflow_dont, /* complain_on_overflow */
1655 ppc64_elf_unhandled_reloc, /* special_function */
1656 "R_PPC64_TPREL16_LO_DS", /* name */
1657 FALSE, /* partial_inplace */
1658 0, /* src_mask */
1659 0xfffc, /* dst_mask */
1660 FALSE), /* pcrel_offset */
1661
1662 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1663 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1664 to the first entry relative to the TOC base (r2). */
1665 HOWTO (R_PPC64_GOT_TLSGD16,
1666 0, /* rightshift */
1667 1, /* size (0 = byte, 1 = short, 2 = long) */
1668 16, /* bitsize */
1669 FALSE, /* pc_relative */
1670 0, /* bitpos */
1671 complain_overflow_signed, /* complain_on_overflow */
1672 ppc64_elf_unhandled_reloc, /* special_function */
1673 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1674 FALSE, /* partial_inplace */
5bd4f169
AM
1675 0, /* src_mask */
1676 0xffff, /* dst_mask */
b34976b6 1677 FALSE), /* pcrel_offset */
5bd4f169 1678
411e1bfb
AM
1679 /* Like GOT_TLSGD16, but no overflow. */
1680 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1681 0, /* rightshift */
1682 1, /* size (0 = byte, 1 = short, 2 = long) */
1683 16, /* bitsize */
b34976b6 1684 FALSE, /* pc_relative */
5bd4f169
AM
1685 0, /* bitpos */
1686 complain_overflow_dont, /* complain_on_overflow */
805fc799 1687 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1688 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1689 FALSE, /* partial_inplace */
5bd4f169
AM
1690 0, /* src_mask */
1691 0xffff, /* dst_mask */
b34976b6 1692 FALSE), /* pcrel_offset */
5bd4f169 1693
411e1bfb
AM
1694 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1695 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1696 16, /* rightshift */
1697 1, /* size (0 = byte, 1 = short, 2 = long) */
1698 16, /* bitsize */
b34976b6 1699 FALSE, /* pc_relative */
5bd4f169 1700 0, /* bitpos */
f9c6b907 1701 complain_overflow_signed, /* complain_on_overflow */
805fc799 1702 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1703 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1704 FALSE, /* partial_inplace */
5bd4f169
AM
1705 0, /* src_mask */
1706 0xffff, /* dst_mask */
b34976b6 1707 FALSE), /* pcrel_offset */
5bd4f169 1708
411e1bfb
AM
1709 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1710 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1711 16, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 16, /* bitsize */
b34976b6 1714 FALSE, /* pc_relative */
5bd4f169 1715 0, /* bitpos */
f9c6b907 1716 complain_overflow_signed, /* complain_on_overflow */
805fc799 1717 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1718 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1719 FALSE, /* partial_inplace */
5bd4f169
AM
1720 0, /* src_mask */
1721 0xffff, /* dst_mask */
b34976b6 1722 FALSE), /* pcrel_offset */
5bd4f169 1723
411e1bfb
AM
1724 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1725 with values (sym+add)@dtpmod and zero, and computes the offset to the
1726 first entry relative to the TOC base (r2). */
1727 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1728 0, /* rightshift */
1729 1, /* size (0 = byte, 1 = short, 2 = long) */
1730 16, /* bitsize */
b34976b6 1731 FALSE, /* pc_relative */
5bd4f169 1732 0, /* bitpos */
411e1bfb
AM
1733 complain_overflow_signed, /* complain_on_overflow */
1734 ppc64_elf_unhandled_reloc, /* special_function */
1735 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1736 FALSE, /* partial_inplace */
d006db6c 1737 0, /* src_mask */
411e1bfb 1738 0xffff, /* dst_mask */
b34976b6 1739 FALSE), /* pcrel_offset */
5bd4f169 1740
411e1bfb
AM
1741 /* Like GOT_TLSLD16, but no overflow. */
1742 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1743 0, /* rightshift */
1744 1, /* size (0 = byte, 1 = short, 2 = long) */
1745 16, /* bitsize */
b34976b6 1746 FALSE, /* pc_relative */
5bd4f169 1747 0, /* bitpos */
411e1bfb
AM
1748 complain_overflow_dont, /* complain_on_overflow */
1749 ppc64_elf_unhandled_reloc, /* special_function */
1750 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1751 FALSE, /* partial_inplace */
d006db6c 1752 0, /* src_mask */
411e1bfb 1753 0xffff, /* dst_mask */
b34976b6 1754 FALSE), /* pcrel_offset */
5bd4f169 1755
411e1bfb
AM
1756 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1757 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1758 16, /* rightshift */
5bd4f169
AM
1759 1, /* size (0 = byte, 1 = short, 2 = long) */
1760 16, /* bitsize */
b34976b6 1761 FALSE, /* pc_relative */
5bd4f169 1762 0, /* bitpos */
f9c6b907 1763 complain_overflow_signed, /* complain_on_overflow */
805fc799 1764 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1765 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1766 FALSE, /* partial_inplace */
d006db6c 1767 0, /* src_mask */
411e1bfb 1768 0xffff, /* dst_mask */
b34976b6 1769 FALSE), /* pcrel_offset */
5bd4f169 1770
411e1bfb
AM
1771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1772 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1773 16, /* rightshift */
5bd4f169
AM
1774 1, /* size (0 = byte, 1 = short, 2 = long) */
1775 16, /* bitsize */
b34976b6 1776 FALSE, /* pc_relative */
5bd4f169 1777 0, /* bitpos */
f9c6b907 1778 complain_overflow_signed, /* complain_on_overflow */
805fc799 1779 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1780 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1781 FALSE, /* partial_inplace */
d006db6c 1782 0, /* src_mask */
411e1bfb 1783 0xffff, /* dst_mask */
b34976b6 1784 FALSE), /* pcrel_offset */
5bd4f169 1785
411e1bfb
AM
1786 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1787 the offset to the entry relative to the TOC base (r2). */
1788 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1789 0, /* rightshift */
1790 1, /* size (0 = byte, 1 = short, 2 = long) */
1791 16, /* bitsize */
b34976b6 1792 FALSE, /* pc_relative */
5bd4f169 1793 0, /* bitpos */
411e1bfb 1794 complain_overflow_signed, /* complain_on_overflow */
805fc799 1795 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1796 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1797 FALSE, /* partial_inplace */
d006db6c 1798 0, /* src_mask */
5bd4f169 1799 0xfffc, /* dst_mask */
b34976b6 1800 FALSE), /* pcrel_offset */
5bd4f169 1801
411e1bfb
AM
1802 /* Like GOT_DTPREL16_DS, but no overflow. */
1803 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1804 0, /* rightshift */
c061c2d8
AM
1805 1, /* size (0 = byte, 1 = short, 2 = long) */
1806 16, /* bitsize */
b34976b6 1807 FALSE, /* pc_relative */
5bd4f169 1808 0, /* bitpos */
411e1bfb
AM
1809 complain_overflow_dont, /* complain_on_overflow */
1810 ppc64_elf_unhandled_reloc, /* special_function */
1811 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1812 FALSE, /* partial_inplace */
d006db6c 1813 0, /* src_mask */
c061c2d8 1814 0xfffc, /* dst_mask */
b34976b6 1815 FALSE), /* pcrel_offset */
5bd4f169 1816
411e1bfb
AM
1817 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1818 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1819 16, /* rightshift */
5bd4f169
AM
1820 1, /* size (0 = byte, 1 = short, 2 = long) */
1821 16, /* bitsize */
b34976b6 1822 FALSE, /* pc_relative */
5bd4f169 1823 0, /* bitpos */
f9c6b907 1824 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1825 ppc64_elf_unhandled_reloc, /* special_function */
1826 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1827 FALSE, /* partial_inplace */
d006db6c 1828 0, /* src_mask */
411e1bfb 1829 0xffff, /* dst_mask */
b34976b6 1830 FALSE), /* pcrel_offset */
5bd4f169 1831
411e1bfb
AM
1832 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1833 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1834 16, /* rightshift */
1835 1, /* size (0 = byte, 1 = short, 2 = long) */
1836 16, /* bitsize */
1837 FALSE, /* pc_relative */
1838 0, /* bitpos */
f9c6b907 1839 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1840 ppc64_elf_unhandled_reloc, /* special_function */
1841 "R_PPC64_GOT_DTPREL16_HA", /* name */
1842 FALSE, /* partial_inplace */
1843 0, /* src_mask */
1844 0xffff, /* dst_mask */
1845 FALSE), /* pcrel_offset */
1846
1847 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1848 offset to the entry relative to the TOC base (r2). */
1849 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1850 0, /* rightshift */
1851 1, /* size (0 = byte, 1 = short, 2 = long) */
1852 16, /* bitsize */
b34976b6 1853 FALSE, /* pc_relative */
5bd4f169
AM
1854 0, /* bitpos */
1855 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1856 ppc64_elf_unhandled_reloc, /* special_function */
1857 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1858 FALSE, /* partial_inplace */
d006db6c 1859 0, /* src_mask */
ad8e1ba5 1860 0xfffc, /* dst_mask */
b34976b6 1861 FALSE), /* pcrel_offset */
5bd4f169 1862
411e1bfb
AM
1863 /* Like GOT_TPREL16_DS, but no overflow. */
1864 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1865 0, /* rightshift */
1866 1, /* size (0 = byte, 1 = short, 2 = long) */
1867 16, /* bitsize */
b34976b6 1868 FALSE, /* pc_relative */
5bd4f169
AM
1869 0, /* bitpos */
1870 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1871 ppc64_elf_unhandled_reloc, /* special_function */
1872 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1873 FALSE, /* partial_inplace */
d006db6c 1874 0, /* src_mask */
ad8e1ba5 1875 0xfffc, /* dst_mask */
b34976b6 1876 FALSE), /* pcrel_offset */
5bd4f169 1877
411e1bfb
AM
1878 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1879 HOWTO (R_PPC64_GOT_TPREL16_HI,
1880 16, /* rightshift */
5bd4f169
AM
1881 1, /* size (0 = byte, 1 = short, 2 = long) */
1882 16, /* bitsize */
b34976b6 1883 FALSE, /* pc_relative */
5bd4f169 1884 0, /* bitpos */
f9c6b907 1885 complain_overflow_signed, /* complain_on_overflow */
805fc799 1886 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1887 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1888 FALSE, /* partial_inplace */
d006db6c 1889 0, /* src_mask */
411e1bfb 1890 0xffff, /* dst_mask */
b34976b6 1891 FALSE), /* pcrel_offset */
5bd4f169 1892
411e1bfb
AM
1893 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1894 HOWTO (R_PPC64_GOT_TPREL16_HA,
1895 16, /* rightshift */
5bd4f169
AM
1896 1, /* size (0 = byte, 1 = short, 2 = long) */
1897 16, /* bitsize */
b34976b6 1898 FALSE, /* pc_relative */
5bd4f169 1899 0, /* bitpos */
f9c6b907 1900 complain_overflow_signed, /* complain_on_overflow */
805fc799 1901 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1902 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1903 FALSE, /* partial_inplace */
d006db6c 1904 0, /* src_mask */
411e1bfb 1905 0xffff, /* dst_mask */
b34976b6 1906 FALSE), /* pcrel_offset */
5bd4f169 1907
25f23106
AM
1908 HOWTO (R_PPC64_JMP_IREL, /* type */
1909 0, /* rightshift */
1910 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1911 0, /* bitsize */
1912 FALSE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_dont, /* complain_on_overflow */
1915 ppc64_elf_unhandled_reloc, /* special_function */
1916 "R_PPC64_JMP_IREL", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 0, /* dst_mask */
1920 FALSE), /* pcrel_offset */
1921
e054468f
AM
1922 HOWTO (R_PPC64_IRELATIVE, /* type */
1923 0, /* rightshift */
1924 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1925 64, /* bitsize */
1926 FALSE, /* pc_relative */
1927 0, /* bitpos */
1928 complain_overflow_dont, /* complain_on_overflow */
1929 bfd_elf_generic_reloc, /* special_function */
1930 "R_PPC64_IRELATIVE", /* name */
1931 FALSE, /* partial_inplace */
1932 0, /* src_mask */
1933 ONES (64), /* dst_mask */
1934 FALSE), /* pcrel_offset */
1935
25f23106
AM
1936 /* A 16 bit relative relocation. */
1937 HOWTO (R_PPC64_REL16, /* type */
1938 0, /* rightshift */
1939 1, /* size (0 = byte, 1 = short, 2 = long) */
1940 16, /* bitsize */
1941 TRUE, /* pc_relative */
1942 0, /* bitpos */
1943 complain_overflow_bitfield, /* complain_on_overflow */
1944 bfd_elf_generic_reloc, /* special_function */
1945 "R_PPC64_REL16", /* name */
1946 FALSE, /* partial_inplace */
1947 0, /* src_mask */
1948 0xffff, /* dst_mask */
1949 TRUE), /* pcrel_offset */
1950
1951 /* A 16 bit relative relocation without overflow. */
1952 HOWTO (R_PPC64_REL16_LO, /* type */
1953 0, /* rightshift */
1954 1, /* size (0 = byte, 1 = short, 2 = long) */
1955 16, /* bitsize */
1956 TRUE, /* pc_relative */
1957 0, /* bitpos */
1958 complain_overflow_dont,/* complain_on_overflow */
1959 bfd_elf_generic_reloc, /* special_function */
1960 "R_PPC64_REL16_LO", /* name */
1961 FALSE, /* partial_inplace */
1962 0, /* src_mask */
1963 0xffff, /* dst_mask */
1964 TRUE), /* pcrel_offset */
1965
1966 /* The high order 16 bits of a relative address. */
1967 HOWTO (R_PPC64_REL16_HI, /* type */
1968 16, /* rightshift */
1969 1, /* size (0 = byte, 1 = short, 2 = long) */
1970 16, /* bitsize */
1971 TRUE, /* pc_relative */
1972 0, /* bitpos */
f9c6b907 1973 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1974 bfd_elf_generic_reloc, /* special_function */
1975 "R_PPC64_REL16_HI", /* name */
1976 FALSE, /* partial_inplace */
1977 0, /* src_mask */
1978 0xffff, /* dst_mask */
1979 TRUE), /* pcrel_offset */
1980
1981 /* The high order 16 bits of a relative address, plus 1 if the contents of
1982 the low 16 bits, treated as a signed number, is negative. */
1983 HOWTO (R_PPC64_REL16_HA, /* type */
1984 16, /* rightshift */
1985 1, /* size (0 = byte, 1 = short, 2 = long) */
1986 16, /* bitsize */
1987 TRUE, /* pc_relative */
1988 0, /* bitpos */
f9c6b907 1989 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1990 ppc64_elf_ha_reloc, /* special_function */
1991 "R_PPC64_REL16_HA", /* name */
1992 FALSE, /* partial_inplace */
1993 0, /* src_mask */
1994 0xffff, /* dst_mask */
1995 TRUE), /* pcrel_offset */
1996
f9c6b907
AM
1997 /* Like R_PPC64_ADDR16_HI, but no overflow. */
1998 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
1999 16, /* rightshift */
2000 1, /* size (0 = byte, 1 = short, 2 = long) */
2001 16, /* bitsize */
2002 FALSE, /* pc_relative */
2003 0, /* bitpos */
2004 complain_overflow_dont, /* complain_on_overflow */
2005 bfd_elf_generic_reloc, /* special_function */
2006 "R_PPC64_ADDR16_HIGH", /* name */
2007 FALSE, /* partial_inplace */
2008 0, /* src_mask */
2009 0xffff, /* dst_mask */
2010 FALSE), /* pcrel_offset */
2011
2012 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2013 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2014 16, /* rightshift */
2015 1, /* size (0 = byte, 1 = short, 2 = long) */
2016 16, /* bitsize */
2017 FALSE, /* pc_relative */
2018 0, /* bitpos */
2019 complain_overflow_dont, /* complain_on_overflow */
2020 ppc64_elf_ha_reloc, /* special_function */
2021 "R_PPC64_ADDR16_HIGHA", /* name */
2022 FALSE, /* partial_inplace */
2023 0, /* src_mask */
2024 0xffff, /* dst_mask */
2025 FALSE), /* pcrel_offset */
2026
2027 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2028 HOWTO (R_PPC64_DTPREL16_HIGH,
2029 16, /* rightshift */
2030 1, /* size (0 = byte, 1 = short, 2 = long) */
2031 16, /* bitsize */
2032 FALSE, /* pc_relative */
2033 0, /* bitpos */
2034 complain_overflow_dont, /* complain_on_overflow */
2035 ppc64_elf_unhandled_reloc, /* special_function */
2036 "R_PPC64_DTPREL16_HIGH", /* name */
2037 FALSE, /* partial_inplace */
2038 0, /* src_mask */
2039 0xffff, /* dst_mask */
2040 FALSE), /* pcrel_offset */
2041
2042 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2043 HOWTO (R_PPC64_DTPREL16_HIGHA,
2044 16, /* rightshift */
2045 1, /* size (0 = byte, 1 = short, 2 = long) */
2046 16, /* bitsize */
2047 FALSE, /* pc_relative */
2048 0, /* bitpos */
2049 complain_overflow_dont, /* complain_on_overflow */
2050 ppc64_elf_unhandled_reloc, /* special_function */
2051 "R_PPC64_DTPREL16_HIGHA", /* name */
2052 FALSE, /* partial_inplace */
2053 0, /* src_mask */
2054 0xffff, /* dst_mask */
2055 FALSE), /* pcrel_offset */
2056
2057 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2058 HOWTO (R_PPC64_TPREL16_HIGH,
2059 16, /* rightshift */
2060 1, /* size (0 = byte, 1 = short, 2 = long) */
2061 16, /* bitsize */
2062 FALSE, /* pc_relative */
2063 0, /* bitpos */
2064 complain_overflow_dont, /* complain_on_overflow */
2065 ppc64_elf_unhandled_reloc, /* special_function */
2066 "R_PPC64_TPREL16_HIGH", /* name */
2067 FALSE, /* partial_inplace */
2068 0, /* src_mask */
2069 0xffff, /* dst_mask */
2070 FALSE), /* pcrel_offset */
2071
2072 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2073 HOWTO (R_PPC64_TPREL16_HIGHA,
2074 16, /* rightshift */
2075 1, /* size (0 = byte, 1 = short, 2 = long) */
2076 16, /* bitsize */
2077 FALSE, /* pc_relative */
2078 0, /* bitpos */
2079 complain_overflow_dont, /* complain_on_overflow */
2080 ppc64_elf_unhandled_reloc, /* special_function */
2081 "R_PPC64_TPREL16_HIGHA", /* name */
2082 FALSE, /* partial_inplace */
2083 0, /* src_mask */
2084 0xffff, /* dst_mask */
2085 FALSE), /* pcrel_offset */
2086
5bd4f169
AM
2087 /* GNU extension to record C++ vtable hierarchy. */
2088 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2089 0, /* rightshift */
2090 0, /* size (0 = byte, 1 = short, 2 = long) */
2091 0, /* bitsize */
b34976b6 2092 FALSE, /* pc_relative */
5bd4f169
AM
2093 0, /* bitpos */
2094 complain_overflow_dont, /* complain_on_overflow */
2095 NULL, /* special_function */
2096 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2097 FALSE, /* partial_inplace */
5bd4f169
AM
2098 0, /* src_mask */
2099 0, /* dst_mask */
b34976b6 2100 FALSE), /* pcrel_offset */
5bd4f169
AM
2101
2102 /* GNU extension to record C++ vtable member usage. */
2103 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2104 0, /* rightshift */
2105 0, /* size (0 = byte, 1 = short, 2 = long) */
2106 0, /* bitsize */
b34976b6 2107 FALSE, /* pc_relative */
5bd4f169
AM
2108 0, /* bitpos */
2109 complain_overflow_dont, /* complain_on_overflow */
2110 NULL, /* special_function */
2111 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2112 FALSE, /* partial_inplace */
5bd4f169
AM
2113 0, /* src_mask */
2114 0, /* dst_mask */
b34976b6 2115 FALSE), /* pcrel_offset */
5bd4f169
AM
2116};
2117
2118\f
2119/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2120 be done. */
2121
2122static void
4ce794b7 2123ppc_howto_init (void)
5bd4f169
AM
2124{
2125 unsigned int i, type;
2126
2127 for (i = 0;
2128 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2129 i++)
2130 {
2131 type = ppc64_elf_howto_raw[i].type;
2132 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2133 / sizeof (ppc64_elf_howto_table[0])));
2134 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2135 }
2136}
2137
2138static reloc_howto_type *
4ce794b7
AM
2139ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2140 bfd_reloc_code_real_type code)
5bd4f169 2141{
411e1bfb 2142 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2143
2144 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2145 /* Initialize howto table if needed. */
2146 ppc_howto_init ();
2147
4ce794b7 2148 switch (code)
5bd4f169
AM
2149 {
2150 default:
4ce794b7 2151 return NULL;
5bd4f169 2152
411e1bfb
AM
2153 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2154 break;
2155 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2156 break;
2157 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2158 break;
2159 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2160 break;
2161 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2162 break;
2163 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2164 break;
f9c6b907
AM
2165 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2166 break;
411e1bfb 2167 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2168 break;
f9c6b907
AM
2169 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2170 break;
411e1bfb 2171 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2172 break;
411e1bfb 2173 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2174 break;
411e1bfb 2175 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2176 break;
411e1bfb 2177 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2178 break;
411e1bfb 2179 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2180 break;
411e1bfb 2181 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2182 break;
411e1bfb 2183 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2184 break;
411e1bfb 2185 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2186 break;
411e1bfb 2187 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2188 break;
411e1bfb 2189 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2190 break;
411e1bfb 2191 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2192 break;
411e1bfb 2193 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2194 break;
411e1bfb 2195 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2196 break;
411e1bfb 2197 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2198 break;
411e1bfb 2199 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2200 break;
411e1bfb 2201 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2202 break;
411e1bfb 2203 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2204 break;
411e1bfb 2205 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2206 break;
411e1bfb 2207 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2208 break;
411e1bfb 2209 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2210 break;
411e1bfb 2211 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2212 break;
411e1bfb 2213 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2214 break;
411e1bfb 2215 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2216 break;
411e1bfb 2217 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2218 break;
411e1bfb 2219 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2220 break;
411e1bfb 2221 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2222 break;
411e1bfb 2223 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2224 break;
411e1bfb 2225 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2226 break;
411e1bfb 2227 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2228 break;
411e1bfb 2229 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2230 break;
411e1bfb 2231 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2232 break;
411e1bfb 2233 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2234 break;
411e1bfb 2235 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2236 break;
411e1bfb 2237 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2238 break;
411e1bfb 2239 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2240 break;
411e1bfb 2241 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2242 break;
411e1bfb 2243 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2244 break;
411e1bfb 2245 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2246 break;
411e1bfb 2247 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2248 break;
411e1bfb 2249 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2250 break;
411e1bfb 2251 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2252 break;
411e1bfb 2253 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2254 break;
411e1bfb 2255 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2256 break;
411e1bfb 2257 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2258 break;
411e1bfb 2259 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2260 break;
411e1bfb 2261 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2262 break;
411e1bfb 2263 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2264 break;
411e1bfb 2265 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2266 break;
411e1bfb 2267 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2268 break;
411e1bfb 2269 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2270 break;
411e1bfb 2271 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2272 break;
411e1bfb 2273 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2274 break;
411e1bfb 2275 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2276 break;
727fc41e
AM
2277 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2278 break;
2279 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2280 break;
411e1bfb 2281 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2282 break;
411e1bfb 2283 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2284 break;
411e1bfb 2285 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2286 break;
411e1bfb 2287 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2288 break;
f9c6b907
AM
2289 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2290 break;
411e1bfb 2291 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2292 break;
f9c6b907
AM
2293 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2294 break;
411e1bfb 2295 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2296 break;
411e1bfb
AM
2297 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2298 break;
2299 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2300 break;
2301 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2302 break;
f9c6b907
AM
2303 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2304 break;
411e1bfb
AM
2305 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2306 break;
f9c6b907
AM
2307 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2308 break;
411e1bfb
AM
2309 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2310 break;
2311 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2312 break;
2313 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2314 break;
2315 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2316 break;
2317 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2318 break;
2319 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2320 break;
2321 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2322 break;
2323 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2324 break;
2325 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2326 break;
2327 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2328 break;
2329 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2330 break;
2331 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2332 break;
2333 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2334 break;
2335 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2336 break;
2337 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2338 break;
2339 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2340 break;
2341 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2342 break;
2343 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2344 break;
2345 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2346 break;
2347 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2348 break;
2349 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2350 break;
2351 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2352 break;
2353 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2354 break;
2355 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2356 break;
2357 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2358 break;
2359 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2360 break;
2361 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2362 break;
2363 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2364 break;
2365 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2366 break;
25f23106
AM
2367 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2368 break;
2369 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2370 break;
2371 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2372 break;
2373 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2374 break;
411e1bfb
AM
2375 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2376 break;
2377 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2378 break;
2379 }
2380
4ce794b7 2381 return ppc64_elf_howto_table[r];
5bd4f169
AM
2382};
2383
157090f7
AM
2384static reloc_howto_type *
2385ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2386 const char *r_name)
2387{
2388 unsigned int i;
2389
2390 for (i = 0;
2391 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2392 i++)
2393 if (ppc64_elf_howto_raw[i].name != NULL
2394 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2395 return &ppc64_elf_howto_raw[i];
2396
2397 return NULL;
2398}
2399
5bd4f169
AM
2400/* Set the howto pointer for a PowerPC ELF reloc. */
2401
2402static void
4ce794b7
AM
2403ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2404 Elf_Internal_Rela *dst)
5bd4f169 2405{
65f38f15
AM
2406 unsigned int type;
2407
ef60b7ff 2408 /* Initialize howto table if needed. */
5bd4f169 2409 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2410 ppc_howto_init ();
2411
65f38f15 2412 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2413 if (type >= (sizeof (ppc64_elf_howto_table)
2414 / sizeof (ppc64_elf_howto_table[0])))
2415 {
2416 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2417 abfd, (int) type);
2418 type = R_PPC64_NONE;
d0fb9a8d 2419 }
65f38f15 2420 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2421}
2422
04c9666a 2423/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2424
2425static bfd_reloc_status_type
4ce794b7
AM
2426ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2427 void *data, asection *input_section,
2428 bfd *output_bfd, char **error_message)
5bd4f169 2429{
805fc799
AM
2430 /* If this is a relocatable link (output_bfd test tells us), just
2431 call the generic function. Any adjustment will be done at final
2432 link time. */
2433 if (output_bfd != NULL)
cedb70c5 2434 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2435 input_section, output_bfd, error_message);
2436
2437 /* Adjust the addend for sign extension of the low 16 bits.
2438 We won't actually be using the low 16 bits, so trashing them
2439 doesn't matter. */
2440 reloc_entry->addend += 0x8000;
2441 return bfd_reloc_continue;
2442}
5bd4f169 2443
2441e016
AM
2444static bfd_reloc_status_type
2445ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2446 void *data, asection *input_section,
2447 bfd *output_bfd, char **error_message)
2448{
2449 if (output_bfd != NULL)
2450 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2451 input_section, output_bfd, error_message);
2452
699733f6
AM
2453 if (strcmp (symbol->section->name, ".opd") == 0
2454 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2455 {
2456 bfd_vma dest = opd_entry_value (symbol->section,
2457 symbol->value + reloc_entry->addend,
aef36ac1 2458 NULL, NULL, FALSE);
2441e016
AM
2459 if (dest != (bfd_vma) -1)
2460 reloc_entry->addend = dest - (symbol->value
2461 + symbol->section->output_section->vma
2462 + symbol->section->output_offset);
2463 }
2464 return bfd_reloc_continue;
2465}
2466
805fc799 2467static bfd_reloc_status_type
4ce794b7
AM
2468ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2469 void *data, asection *input_section,
2470 bfd *output_bfd, char **error_message)
805fc799
AM
2471{
2472 long insn;
04c9666a 2473 enum elf_ppc64_reloc_type r_type;
805fc799 2474 bfd_size_type octets;
794e51c0
AM
2475 /* Assume 'at' branch hints. */
2476 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2477
2478 /* If this is a relocatable link (output_bfd test tells us), just
2479 call the generic function. Any adjustment will be done at final
2480 link time. */
5bd4f169 2481 if (output_bfd != NULL)
cedb70c5 2482 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2483 input_section, output_bfd, error_message);
2484
2485 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2486 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2487 insn &= ~(0x01 << 21);
4ce794b7 2488 r_type = reloc_entry->howto->type;
805fc799
AM
2489 if (r_type == R_PPC64_ADDR14_BRTAKEN
2490 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2491 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2492
794e51c0 2493 if (is_isa_v2)
5bd4f169 2494 {
805fc799
AM
2495 /* Set 'a' bit. This is 0b00010 in BO field for branch
2496 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2497 for branch on CTR insns (BO == 1a00t or 1a01t). */
2498 if ((insn & (0x14 << 21)) == (0x04 << 21))
2499 insn |= 0x02 << 21;
2500 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2501 insn |= 0x08 << 21;
2502 else
2441e016 2503 goto out;
5bd4f169 2504 }
805fc799
AM
2505 else
2506 {
2507 bfd_vma target = 0;
2508 bfd_vma from;
5bd4f169 2509
805fc799
AM
2510 if (!bfd_is_com_section (symbol->section))
2511 target = symbol->value;
2512 target += symbol->section->output_section->vma;
2513 target += symbol->section->output_offset;
2514 target += reloc_entry->addend;
5bd4f169 2515
805fc799
AM
2516 from = (reloc_entry->address
2517 + input_section->output_offset
2518 + input_section->output_section->vma);
5bd4f169 2519
805fc799
AM
2520 /* Invert 'y' bit if not the default. */
2521 if ((bfd_signed_vma) (target - from) < 0)
2522 insn ^= 0x01 << 21;
2523 }
4ce794b7 2524 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2525 out:
2526 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2527 input_section, output_bfd, error_message);
805fc799 2528}
5bd4f169 2529
805fc799 2530static bfd_reloc_status_type
4ce794b7
AM
2531ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2532 void *data, asection *input_section,
2533 bfd *output_bfd, char **error_message)
805fc799
AM
2534{
2535 /* If this is a relocatable link (output_bfd test tells us), just
2536 call the generic function. Any adjustment will be done at final
2537 link time. */
2538 if (output_bfd != NULL)
cedb70c5 2539 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2540 input_section, output_bfd, error_message);
5bd4f169 2541
805fc799
AM
2542 /* Subtract the symbol section base address. */
2543 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2544 return bfd_reloc_continue;
2545}
2546
805fc799 2547static bfd_reloc_status_type
4ce794b7
AM
2548ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2549 void *data, asection *input_section,
2550 bfd *output_bfd, char **error_message)
805fc799
AM
2551{
2552 /* If this is a relocatable link (output_bfd test tells us), just
2553 call the generic function. Any adjustment will be done at final
2554 link time. */
2555 if (output_bfd != NULL)
cedb70c5 2556 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2557 input_section, output_bfd, error_message);
2558
2559 /* Subtract the symbol section base address. */
2560 reloc_entry->addend -= symbol->section->output_section->vma;
2561
2562 /* Adjust the addend for sign extension of the low 16 bits. */
2563 reloc_entry->addend += 0x8000;
2564 return bfd_reloc_continue;
2565}
2566
2567static bfd_reloc_status_type
4ce794b7
AM
2568ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2569 void *data, asection *input_section,
2570 bfd *output_bfd, char **error_message)
805fc799
AM
2571{
2572 bfd_vma TOCstart;
2573
2574 /* If this is a relocatable link (output_bfd test tells us), just
2575 call the generic function. Any adjustment will be done at final
2576 link time. */
2577 if (output_bfd != NULL)
cedb70c5 2578 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2579 input_section, output_bfd, error_message);
2580
2581 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2582 if (TOCstart == 0)
1c865ab2 2583 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2584
2585 /* Subtract the TOC base address. */
2586 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2587 return bfd_reloc_continue;
2588}
2589
2590static bfd_reloc_status_type
4ce794b7
AM
2591ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2592 void *data, asection *input_section,
2593 bfd *output_bfd, char **error_message)
805fc799
AM
2594{
2595 bfd_vma TOCstart;
2596
2597 /* If this is a relocatable link (output_bfd test tells us), just
2598 call the generic function. Any adjustment will be done at final
2599 link time. */
2600 if (output_bfd != NULL)
cedb70c5 2601 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2602 input_section, output_bfd, error_message);
2603
2604 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2605 if (TOCstart == 0)
1c865ab2 2606 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2607
2608 /* Subtract the TOC base address. */
2609 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2610
2611 /* Adjust the addend for sign extension of the low 16 bits. */
2612 reloc_entry->addend += 0x8000;
2613 return bfd_reloc_continue;
2614}
2615
2616static bfd_reloc_status_type
4ce794b7
AM
2617ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2618 void *data, asection *input_section,
2619 bfd *output_bfd, char **error_message)
805fc799
AM
2620{
2621 bfd_vma TOCstart;
2622 bfd_size_type octets;
2623
2624 /* If this is a relocatable link (output_bfd test tells us), just
2625 call the generic function. Any adjustment will be done at final
2626 link time. */
2627 if (output_bfd != NULL)
cedb70c5 2628 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2629 input_section, output_bfd, error_message);
2630
2631 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2632 if (TOCstart == 0)
1c865ab2 2633 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2634
2635 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2636 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2637 return bfd_reloc_ok;
2638}
2639
2640static bfd_reloc_status_type
4ce794b7
AM
2641ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2642 void *data, asection *input_section,
2643 bfd *output_bfd, char **error_message)
805fc799
AM
2644{
2645 /* If this is a relocatable link (output_bfd test tells us), just
2646 call the generic function. Any adjustment will be done at final
2647 link time. */
2648 if (output_bfd != NULL)
cedb70c5 2649 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2650 input_section, output_bfd, error_message);
2651
2652 if (error_message != NULL)
2653 {
2654 static char buf[60];
2655 sprintf (buf, "generic linker can't handle %s",
2656 reloc_entry->howto->name);
2657 *error_message = buf;
2658 }
2659 return bfd_reloc_dangerous;
2660}
2661
927be08e
AM
2662/* Track GOT entries needed for a given symbol. We might need more
2663 than one got entry per symbol. */
2664struct got_entry
2665{
2666 struct got_entry *next;
2667
2668 /* The symbol addend that we'll be placing in the GOT. */
2669 bfd_vma addend;
2670
2671 /* Unlike other ELF targets, we use separate GOT entries for the same
2672 symbol referenced from different input files. This is to support
2673 automatic multiple TOC/GOT sections, where the TOC base can vary
2674 from one input file to another. After partitioning into TOC groups
2675 we merge entries within the group.
2676
2677 Point to the BFD owning this GOT entry. */
2678 bfd *owner;
2679
2680 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2681 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2682 unsigned char tls_type;
927be08e
AM
2683
2684 /* Non-zero if got.ent points to real entry. */
f961d9dd 2685 unsigned char is_indirect;
927be08e
AM
2686
2687 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2688 union
2689 {
2690 bfd_signed_vma refcount;
2691 bfd_vma offset;
2692 struct got_entry *ent;
2693 } got;
2694};
2695
2696/* The same for PLT. */
2697struct plt_entry
2698{
2699 struct plt_entry *next;
2700
2701 bfd_vma addend;
2702
2703 union
2704 {
2705 bfd_signed_vma refcount;
2706 bfd_vma offset;
2707 } plt;
2708};
2709
e717da7e
AM
2710struct ppc64_elf_obj_tdata
2711{
2712 struct elf_obj_tdata elf;
2713
2714 /* Shortcuts to dynamic linker sections. */
2715 asection *got;
2716 asection *relgot;
2717
b3fac117
AM
2718 /* Used during garbage collection. We attach global symbols defined
2719 on removed .opd entries to this section so that the sym is removed. */
2720 asection *deleted_section;
81688140 2721
927be08e 2722 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2723 sections means we potentially need one of these for each input bfd. */
927be08e 2724 struct got_entry tlsld_got;
8860955f 2725
729eabd5
AM
2726 union {
2727 /* A copy of relocs before they are modified for --emit-relocs. */
2728 Elf_Internal_Rela *relocs;
2729
2730 /* Section contents. */
2731 bfd_byte *contents;
2732 } opd;
d77c8a4b
AM
2733
2734 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2735 the reloc to be in the range -32768 to 32767. */
98528052
AM
2736 unsigned int has_small_toc_reloc : 1;
2737
560c8763
AM
2738 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2739 instruction not one we handle. */
2740 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2741};
2742
2743#define ppc64_elf_tdata(bfd) \
2744 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2745
2746#define ppc64_tlsld_got(bfd) \
2747 (&ppc64_elf_tdata (bfd)->tlsld_got)
2748
0c8d6e5c
AM
2749#define is_ppc64_elf(bfd) \
2750 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2751 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2752
e717da7e
AM
2753/* Override the generic function because we store some extras. */
2754
2755static bfd_boolean
2756ppc64_elf_mkobject (bfd *abfd)
2757{
0ffa91dd 2758 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2759 PPC64_ELF_DATA);
e717da7e
AM
2760}
2761
feee612b
AM
2762/* Fix bad default arch selected for a 64 bit input bfd when the
2763 default is 32 bit. */
2764
b34976b6 2765static bfd_boolean
4ce794b7 2766ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2767{
2768 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2769 {
2770 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2771
2772 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2773 {
2774 /* Relies on arch after 32 bit default being 64 bit default. */
2775 abfd->arch_info = abfd->arch_info->next;
2776 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2777 }
2778 }
b34976b6 2779 return TRUE;
feee612b
AM
2780}
2781
d37c89e5
AM
2782/* Support for core dump NOTE sections. */
2783
2784static bfd_boolean
2785ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2786{
eea6121a 2787 size_t offset, size;
d37c89e5
AM
2788
2789 if (note->descsz != 504)
2790 return FALSE;
2791
2792 /* pr_cursig */
228e534f 2793 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2794
2795 /* pr_pid */
228e534f 2796 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2797
2798 /* pr_reg */
2799 offset = 112;
eea6121a 2800 size = 384;
d37c89e5
AM
2801
2802 /* Make a ".reg/999" section. */
2803 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2804 size, note->descpos + offset);
d37c89e5
AM
2805}
2806
2807static bfd_boolean
2808ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2809{
2810 if (note->descsz != 136)
2811 return FALSE;
2812
228e534f 2813 elf_tdata (abfd)->core->pid
bc989cdc 2814 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2815 elf_tdata (abfd)->core->program
d37c89e5 2816 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2817 elf_tdata (abfd)->core->command
d37c89e5
AM
2818 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2819
2820 return TRUE;
2821}
2822
183e98be
AM
2823static char *
2824ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2825 ...)
2826{
2827 switch (note_type)
2828 {
2829 default:
2830 return NULL;
2831
2832 case NT_PRPSINFO:
2833 {
2834 char data[136];
2835 va_list ap;
2836
2837 va_start (ap, note_type);
75cd47ed 2838 memset (data, 0, sizeof (data));
183e98be
AM
2839 strncpy (data + 40, va_arg (ap, const char *), 16);
2840 strncpy (data + 56, va_arg (ap, const char *), 80);
2841 va_end (ap);
2842 return elfcore_write_note (abfd, buf, bufsiz,
2843 "CORE", note_type, data, sizeof (data));
2844 }
2845
2846 case NT_PRSTATUS:
2847 {
2848 char data[504];
2849 va_list ap;
2850 long pid;
2851 int cursig;
2852 const void *greg;
2853
2854 va_start (ap, note_type);
2855 memset (data, 0, 112);
2856 pid = va_arg (ap, long);
2857 bfd_put_32 (abfd, pid, data + 32);
2858 cursig = va_arg (ap, int);
2859 bfd_put_16 (abfd, cursig, data + 12);
2860 greg = va_arg (ap, const void *);
2861 memcpy (data + 112, greg, 384);
2862 memset (data + 496, 0, 8);
2863 va_end (ap);
2864 return elfcore_write_note (abfd, buf, bufsiz,
2865 "CORE", note_type, data, sizeof (data));
2866 }
2867 }
2868}
2869
5d35169e
AM
2870/* Add extra PPC sections. */
2871
b35d266b 2872static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2873{
0112cd26
NC
2874 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2875 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2876 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2877 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2878 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2879 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2880 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2881};
2882
7c8fe5c4
AM
2883enum _ppc64_sec_type {
2884 sec_normal = 0,
2885 sec_opd = 1,
2886 sec_toc = 2
2887};
2888
f0abc2a1
AM
2889struct _ppc64_elf_section_data
2890{
2891 struct bfd_elf_section_data elf;
411e1bfb 2892
f0abc2a1
AM
2893 union
2894 {
74f0fb50
AM
2895 /* An array with one entry for each opd function descriptor. */
2896 struct _opd_sec_data
2897 {
2898 /* Points to the function code section for local opd entries. */
2899 asection **func_sec;
2900
2901 /* After editing .opd, adjust references to opd local syms. */
2902 long *adjust;
2903 } opd;
7c8fe5c4 2904
3a71aa26
AM
2905 /* An array for toc sections, indexed by offset/8. */
2906 struct _toc_sec_data
2907 {
2908 /* Specifies the relocation symbol index used at a given toc offset. */
2909 unsigned *symndx;
2910
2911 /* And the relocation addend. */
2912 bfd_vma *add;
2913 } toc;
7c8fe5c4
AM
2914 } u;
2915
2916 enum _ppc64_sec_type sec_type:2;
411e1bfb 2917
7c8fe5c4
AM
2918 /* Flag set when small branches are detected. Used to
2919 select suitable defaults for the stub group size. */
2920 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2921};
2922
2923#define ppc64_elf_section_data(sec) \
411e1bfb 2924 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2925
2926static bfd_boolean
4ce794b7 2927ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2928{
f592407e
AM
2929 if (!sec->used_by_bfd)
2930 {
2931 struct _ppc64_elf_section_data *sdata;
2932 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2933
f592407e
AM
2934 sdata = bfd_zalloc (abfd, amt);
2935 if (sdata == NULL)
2936 return FALSE;
2937 sec->used_by_bfd = sdata;
2938 }
f0abc2a1
AM
2939
2940 return _bfd_elf_new_section_hook (abfd, sec);
2941}
4025353c 2942
74f0fb50 2943static struct _opd_sec_data *
4025353c
AM
2944get_opd_info (asection * sec)
2945{
2946 if (sec != NULL
2947 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2948 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2949 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2950 return NULL;
2951}
ee67d69a
AM
2952
2953static inline int
2954abiversion (bfd *abfd)
2955{
2956 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2957}
2958
2959static inline void
2960set_abiversion (bfd *abfd, int ver)
2961{
2962 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2963 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2964}
90e3cdf2
JJ
2965\f
2966/* Parameters for the qsort hook. */
90e3cdf2
JJ
2967static bfd_boolean synthetic_relocatable;
2968
699733f6 2969/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2970
2971static int
2972compare_symbols (const void *ap, const void *bp)
2973{
2974 const asymbol *a = * (const asymbol **) ap;
2975 const asymbol *b = * (const asymbol **) bp;
2976
699733f6
AM
2977 /* Section symbols first. */
2978 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2979 return -1;
699733f6 2980 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2981 return 1;
2982
699733f6 2983 /* then .opd symbols. */
ffcfec52
AM
2984 if (strcmp (a->section->name, ".opd") == 0
2985 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2986 return -1;
ffcfec52
AM
2987 if (strcmp (a->section->name, ".opd") != 0
2988 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2989 return 1;
2990
699733f6 2991 /* then other code symbols. */
90e3cdf2
JJ
2992 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2993 == (SEC_CODE | SEC_ALLOC)
2994 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2995 != (SEC_CODE | SEC_ALLOC))
2996 return -1;
2997
2998 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2999 != (SEC_CODE | SEC_ALLOC)
3000 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3001 == (SEC_CODE | SEC_ALLOC))
3002 return 1;
3003
3004 if (synthetic_relocatable)
3005 {
3006 if (a->section->id < b->section->id)
3007 return -1;
3008
3009 if (a->section->id > b->section->id)
3010 return 1;
3011 }
3012
3013 if (a->value + a->section->vma < b->value + b->section->vma)
3014 return -1;
3015
3016 if (a->value + a->section->vma > b->value + b->section->vma)
3017 return 1;
3018
4d35a0aa
AM
3019 /* For syms with the same value, prefer strong dynamic global function
3020 syms over other syms. */
3021 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3022 return -1;
3023
3024 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3025 return 1;
3026
3027 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3028 return -1;
3029
3030 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3031 return 1;
3032
3033 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3034 return -1;
3035
3036 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3037 return 1;
3038
3039 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3040 return -1;
3041
3042 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3043 return 1;
3044
90e3cdf2
JJ
3045 return 0;
3046}
3047
699733f6 3048/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3049
699733f6
AM
3050static asymbol *
3051sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 3052{
699733f6 3053 long mid;
90e3cdf2 3054
699733f6
AM
3055 if (id == -1)
3056 {
3057 while (lo < hi)
3058 {
3059 mid = (lo + hi) >> 1;
3060 if (syms[mid]->value + syms[mid]->section->vma < value)
3061 lo = mid + 1;
3062 else if (syms[mid]->value + syms[mid]->section->vma > value)
3063 hi = mid;
3064 else
3065 return syms[mid];
3066 }
3067 }
3068 else
3069 {
3070 while (lo < hi)
3071 {
3072 mid = (lo + hi) >> 1;
3073 if (syms[mid]->section->id < id)
3074 lo = mid + 1;
3075 else if (syms[mid]->section->id > id)
3076 hi = mid;
3077 else if (syms[mid]->value < value)
3078 lo = mid + 1;
3079 else if (syms[mid]->value > value)
3080 hi = mid;
3081 else
3082 return syms[mid];
3083 }
3084 }
3085 return NULL;
90e3cdf2
JJ
3086}
3087
468392fb
AM
3088static bfd_boolean
3089section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3090{
3091 bfd_vma vma = *(bfd_vma *) ptr;
3092 return ((section->flags & SEC_ALLOC) != 0
3093 && section->vma <= vma
3094 && vma < section->vma + section->size);
3095}
3096
699733f6 3097/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 3098 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
3099
3100static long
a7535cf3
AM
3101ppc64_elf_get_synthetic_symtab (bfd *abfd,
3102 long static_count, asymbol **static_syms,
3103 long dyn_count, asymbol **dyn_syms,
c9727e01 3104 asymbol **ret)
90e3cdf2
JJ
3105{
3106 asymbol *s;
699733f6
AM
3107 long i;
3108 long count;
90e3cdf2 3109 char *names;
a7535cf3 3110 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3111 asection *opd = NULL;
90e3cdf2 3112 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3113 asymbol **syms;
ee67d69a 3114 int abi = abiversion (abfd);
90e3cdf2
JJ
3115
3116 *ret = NULL;
3117
ee67d69a
AM
3118 if (abi < 2)
3119 {
3120 opd = bfd_get_section_by_name (abfd, ".opd");
3121 if (opd == NULL && abi == 1)
3122 return 0;
3123 }
90e3cdf2 3124
a7535cf3 3125 symcount = static_count;
c9727e01 3126 if (!relocatable)
a7535cf3 3127 symcount += dyn_count;
90e3cdf2 3128 if (symcount == 0)
c9727e01 3129 return 0;
90e3cdf2 3130
a7535cf3
AM
3131 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3132 if (syms == NULL)
7356fed5 3133 return -1;
a7535cf3
AM
3134
3135 if (!relocatable && static_count != 0 && dyn_count != 0)
3136 {
3137 /* Use both symbol tables. */
3138 memcpy (syms, static_syms, static_count * sizeof (*syms));
3139 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3140 }
3141 else if (!relocatable && static_count == 0)
3142 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3143 else
3144 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3145
90e3cdf2 3146 synthetic_relocatable = relocatable;
595da8c5 3147 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3148
c9727e01
AM
3149 if (!relocatable && symcount > 1)
3150 {
3151 long j;
3152 /* Trim duplicate syms, since we may have merged the normal and
3153 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3154 different values, so trim any with the same value. */
c9727e01
AM
3155 for (i = 1, j = 1; i < symcount; ++i)
3156 if (syms[i - 1]->value + syms[i - 1]->section->vma
3157 != syms[i]->value + syms[i]->section->vma)
3158 syms[j++] = syms[i];
3159 symcount = j;
3160 }
3161
699733f6 3162 i = 0;
ffcfec52 3163 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3164 ++i;
3165 codesecsym = i;
90e3cdf2 3166
699733f6
AM
3167 for (; i < symcount; ++i)
3168 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3169 != (SEC_CODE | SEC_ALLOC))
3170 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3171 break;
3172 codesecsymend = i;
90e3cdf2 3173
699733f6
AM
3174 for (; i < symcount; ++i)
3175 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3176 break;
3177 secsymend = i;
90e3cdf2 3178
699733f6 3179 for (; i < symcount; ++i)
ffcfec52 3180 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3181 break;
3182 opdsymend = i;
90e3cdf2 3183
699733f6
AM
3184 for (; i < symcount; ++i)
3185 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3186 != (SEC_CODE | SEC_ALLOC))
3187 break;
3188 symcount = i;
3189
c9727e01 3190 count = 0;
90e3cdf2 3191
699733f6 3192 if (relocatable)
90e3cdf2 3193 {
699733f6
AM
3194 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3195 arelent *r;
3196 size_t size;
3197 long relcount;
90e3cdf2 3198
468392fb
AM
3199 if (opdsymend == secsymend)
3200 goto done;
3201
699733f6 3202 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3203 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3204 if (relcount == 0)
c9727e01 3205 goto done;
90e3cdf2 3206
7356fed5
AM
3207 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3208 {
3209 count = -1;
3210 goto done;
3211 }
3212
699733f6 3213 size = 0;
595da8c5 3214 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3215 {
3216 asymbol *sym;
90e3cdf2 3217
595da8c5 3218 while (r < opd->relocation + relcount
699733f6
AM
3219 && r->address < syms[i]->value + opd->vma)
3220 ++r;
90e3cdf2 3221
595da8c5 3222 if (r == opd->relocation + relcount)
699733f6 3223 break;
90e3cdf2 3224
699733f6
AM
3225 if (r->address != syms[i]->value + opd->vma)
3226 continue;
90e3cdf2 3227
699733f6
AM
3228 if (r->howto->type != R_PPC64_ADDR64)
3229 continue;
90e3cdf2 3230
699733f6
AM
3231 sym = *r->sym_ptr_ptr;
3232 if (!sym_exists_at (syms, opdsymend, symcount,
3233 sym->section->id, sym->value + r->addend))
3234 {
3235 ++count;
3236 size += sizeof (asymbol);
3237 size += strlen (syms[i]->name) + 2;
3238 }
3239 }
90e3cdf2 3240
699733f6
AM
3241 s = *ret = bfd_malloc (size);
3242 if (s == NULL)
3243 {
7356fed5 3244 count = -1;
c9727e01 3245 goto done;
699733f6 3246 }
90e3cdf2 3247
699733f6 3248 names = (char *) (s + count);
90e3cdf2 3249
595da8c5 3250 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3251 {
699733f6 3252 asymbol *sym;
90e3cdf2 3253
595da8c5 3254 while (r < opd->relocation + relcount
699733f6
AM
3255 && r->address < syms[i]->value + opd->vma)
3256 ++r;
90e3cdf2 3257
595da8c5 3258 if (r == opd->relocation + relcount)
699733f6
AM
3259 break;
3260
3261 if (r->address != syms[i]->value + opd->vma)
3262 continue;
3263
3264 if (r->howto->type != R_PPC64_ADDR64)
3265 continue;
90e3cdf2 3266
699733f6
AM
3267 sym = *r->sym_ptr_ptr;
3268 if (!sym_exists_at (syms, opdsymend, symcount,
3269 sym->section->id, sym->value + r->addend))
3270 {
3271 size_t len;
3272
3273 *s = *syms[i];
6ba2a415 3274 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3275 s->section = sym->section;
3276 s->value = sym->value + r->addend;
3277 s->name = names;
3278 *names++ = '.';
3279 len = strlen (syms[i]->name);
3280 memcpy (names, syms[i]->name, len + 1);
3281 names += len + 1;
6f610d07
UW
3282 /* Have udata.p point back to the original symbol this
3283 synthetic symbol was derived from. */
3284 s->udata.p = syms[i];
699733f6
AM
3285 s++;
3286 }
3287 }
3288 }
3289 else
90e3cdf2 3290 {
468392fb 3291 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3292 bfd_byte *contents = NULL;
699733f6 3293 size_t size;
468392fb
AM
3294 long plt_count = 0;
3295 bfd_vma glink_vma = 0, resolv_vma = 0;
3296 asection *dynamic, *glink = NULL, *relplt = NULL;
3297 arelent *p;
90e3cdf2 3298
ee67d69a 3299 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3300 {
ee67d69a 3301 free_contents_and_exit:
699733f6 3302 if (contents)
ee67d69a 3303 free (contents);
7356fed5 3304 count = -1;
c9727e01 3305 goto done;
699733f6 3306 }
90e3cdf2 3307
699733f6
AM
3308 size = 0;
3309 for (i = secsymend; i < opdsymend; ++i)
3310 {
3311 bfd_vma ent;
90e3cdf2 3312
5ef11c02
AM
3313 /* Ignore bogus symbols. */
3314 if (syms[i]->value > opd->size - 8)
3315 continue;
3316
699733f6
AM
3317 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3318 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3319 {
3320 ++count;
3321 size += sizeof (asymbol);
3322 size += strlen (syms[i]->name) + 2;
3323 }
3324 }
90e3cdf2 3325
468392fb 3326 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3327 if (dyn_count != 0
3328 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3329 {
3330 bfd_byte *dynbuf, *extdyn, *extdynend;
3331 size_t extdynsize;
3332 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3333
3334 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3335 goto free_contents_and_exit;
3336
3337 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3338 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3339
3340 extdyn = dynbuf;
3341 extdynend = extdyn + dynamic->size;
3342 for (; extdyn < extdynend; extdyn += extdynsize)
3343 {
3344 Elf_Internal_Dyn dyn;
3345 (*swap_dyn_in) (abfd, extdyn, &dyn);
3346
3347 if (dyn.d_tag == DT_NULL)
3348 break;
3349
3350 if (dyn.d_tag == DT_PPC64_GLINK)
3351 {
b9e5796b
AM
3352 /* The first glink stub starts at offset 32; see
3353 comment in ppc64_elf_finish_dynamic_sections. */
3354 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3355 /* The .glink section usually does not survive the final
3356 link; search for the section (usually .text) where the
3357 glink stubs now reside. */
3358 glink = bfd_sections_find_if (abfd, section_covers_vma,
3359 &glink_vma);
3360 break;
3361 }
3362 }
3363
3364 free (dynbuf);
3365 }
3366
3367 if (glink != NULL)
3368 {
3369 /* Determine __glink trampoline by reading the relative branch
3370 from the first glink stub. */
3371 bfd_byte buf[4];
b9e5796b
AM
3372 unsigned int off = 0;
3373
3374 while (bfd_get_section_contents (abfd, glink, buf,
3375 glink_vma + off - glink->vma, 4))
468392fb
AM
3376 {
3377 unsigned int insn = bfd_get_32 (abfd, buf);
3378 insn ^= B_DOT;
3379 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3380 {
3381 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3382 break;
3383 }
3384 off += 4;
3385 if (off > 4)
3386 break;
468392fb
AM
3387 }
3388
3389 if (resolv_vma)
3390 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3391
066ee829
AM
3392 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3393 if (relplt != NULL)
3394 {
3395 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3396 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3397 goto free_contents_and_exit;
68ffbac6 3398
066ee829
AM
3399 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3400 size += plt_count * sizeof (asymbol);
468392fb 3401
066ee829
AM
3402 p = relplt->relocation;
3403 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3404 {
3405 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3406 if (p->addend != 0)
3407 size += sizeof ("+0x") - 1 + 16;
3408 }
066ee829 3409 }
468392fb
AM
3410 }
3411
699733f6
AM
3412 s = *ret = bfd_malloc (size);
3413 if (s == NULL)
7356fed5 3414 goto free_contents_and_exit;
90e3cdf2 3415
468392fb 3416 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3417
699733f6 3418 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3419 {
699733f6 3420 bfd_vma ent;
90e3cdf2 3421
5ef11c02
AM
3422 if (syms[i]->value > opd->size - 8)
3423 continue;
3424
699733f6
AM
3425 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3426 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3427 {
c9727e01 3428 long lo, hi;
699733f6 3429 size_t len;
c9727e01 3430 asection *sec = abfd->sections;
90e3cdf2 3431
699733f6
AM
3432 *s = *syms[i];
3433 lo = codesecsym;
3434 hi = codesecsymend;
3435 while (lo < hi)
3436 {
c9727e01 3437 long mid = (lo + hi) >> 1;
699733f6
AM
3438 if (syms[mid]->section->vma < ent)
3439 lo = mid + 1;
3440 else if (syms[mid]->section->vma > ent)
3441 hi = mid;
3442 else
c9727e01
AM
3443 {
3444 sec = syms[mid]->section;
3445 break;
3446 }
699733f6
AM
3447 }
3448
c9727e01 3449 if (lo >= hi && lo > codesecsym)
699733f6 3450 sec = syms[lo - 1]->section;
699733f6
AM
3451
3452 for (; sec != NULL; sec = sec->next)
3453 {
3454 if (sec->vma > ent)
3455 break;
63524580
JK
3456 /* SEC_LOAD may not be set if SEC is from a separate debug
3457 info file. */
3458 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3459 break;
3460 if ((sec->flags & SEC_CODE) != 0)
3461 s->section = sec;
3462 }
6ba2a415 3463 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3464 s->value = ent - s->section->vma;
3465 s->name = names;
3466 *names++ = '.';
3467 len = strlen (syms[i]->name);
3468 memcpy (names, syms[i]->name, len + 1);
3469 names += len + 1;
6f610d07
UW
3470 /* Have udata.p point back to the original symbol this
3471 synthetic symbol was derived from. */
3472 s->udata.p = syms[i];
699733f6 3473 s++;
90e3cdf2 3474 }
90e3cdf2 3475 }
699733f6 3476 free (contents);
468392fb
AM
3477
3478 if (glink != NULL && relplt != NULL)
3479 {
3480 if (resolv_vma)
3481 {
3482 /* Add a symbol for the main glink trampoline. */
86a4952b 3483 memset (s, 0, sizeof *s);
468392fb 3484 s->the_bfd = abfd;
6ba2a415 3485 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3486 s->section = glink;
3487 s->value = resolv_vma - glink->vma;
3488 s->name = names;
3489 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3490 names += sizeof ("__glink_PLTresolve");
3491 s++;
3492 count++;
3493 }
3494
3495 /* FIXME: It would be very much nicer to put sym@plt on the
3496 stub rather than on the glink branch table entry. The
3497 objdump disassembler would then use a sensible symbol
3498 name on plt calls. The difficulty in doing so is
3499 a) finding the stubs, and,
3500 b) matching stubs against plt entries, and,
3501 c) there can be multiple stubs for a given plt entry.
3502
3503 Solving (a) could be done by code scanning, but older
3504 ppc64 binaries used different stubs to current code.
3505 (b) is the tricky one since you need to known the toc
3506 pointer for at least one function that uses a pic stub to
3507 be able to calculate the plt address referenced.
3508 (c) means gdb would need to set multiple breakpoints (or
3509 find the glink branch itself) when setting breakpoints
3510 for pending shared library loads. */
3511 p = relplt->relocation;
3512 for (i = 0; i < plt_count; i++, p++)
3513 {
3514 size_t len;
3515
3516 *s = **p->sym_ptr_ptr;
3517 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3518 we are defining a symbol, ensure one of them is set. */
3519 if ((s->flags & BSF_LOCAL) == 0)
3520 s->flags |= BSF_GLOBAL;
6ba2a415 3521 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3522 s->section = glink;
3523 s->value = glink_vma - glink->vma;
3524 s->name = names;
3525 s->udata.p = NULL;
3526 len = strlen ((*p->sym_ptr_ptr)->name);
3527 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3528 names += len;
e054468f
AM
3529 if (p->addend != 0)
3530 {
3531 memcpy (names, "+0x", sizeof ("+0x") - 1);
3532 names += sizeof ("+0x") - 1;
3533 bfd_sprintf_vma (abfd, names, p->addend);
3534 names += strlen (names);
3535 }
468392fb
AM
3536 memcpy (names, "@plt", sizeof ("@plt"));
3537 names += sizeof ("@plt");
3538 s++;
b9e5796b
AM
3539 if (abi < 2)
3540 {
3541 glink_vma += 8;
3542 if (i >= 0x8000)
3543 glink_vma += 4;
3544 }
3545 else
468392fb
AM
3546 glink_vma += 4;
3547 }
3548 count += plt_count;
3549 }
90e3cdf2
JJ
3550 }
3551
c9727e01 3552 done:
a7535cf3 3553 free (syms);
90e3cdf2
JJ
3554 return count;
3555}
5bd4f169 3556\f
65f38f15
AM
3557/* The following functions are specific to the ELF linker, while
3558 functions above are used generally. Those named ppc64_elf_* are
3559 called by the main ELF linker code. They appear in this file more
3560 or less in the order in which they are called. eg.
3561 ppc64_elf_check_relocs is called early in the link process,
3562 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3563 called.
3564
3565 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3566 functions have both a function code symbol and a function descriptor
3567 symbol. A call to foo in a relocatable object file looks like:
3568
3569 . .text
3570 . x:
3571 . bl .foo
3572 . nop
3573
3574 The function definition in another object file might be:
3575
3576 . .section .opd
3577 . foo: .quad .foo
3578 . .quad .TOC.@tocbase
3579 . .quad 0
3580 .
3581 . .text
3582 . .foo: blr
3583
3584 When the linker resolves the call during a static link, the branch
3585 unsurprisingly just goes to .foo and the .opd information is unused.
3586 If the function definition is in a shared library, things are a little
3587 different: The call goes via a plt call stub, the opd information gets
3588 copied to the plt, and the linker patches the nop.
3589
3590 . x:
3591 . bl .foo_stub
3592 . ld 2,40(1)
3593 .
3594 .
3595 . .foo_stub:
71a39c98
AM
3596 . std 2,40(1) # in practice, the call stub
3597 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3598 . addi 11,11,Lfoo@toc@l # this is the general idea
3599 . ld 12,0(11)
3600 . ld 2,8(11)
3601 . mtctr 12
3602 . ld 11,16(11)
e86ce104
AM
3603 . bctr
3604 .
3605 . .section .plt
3606 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3607
3608 The "reloc ()" notation is supposed to indicate that the linker emits
3609 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3610 copying.
3611
3612 What are the difficulties here? Well, firstly, the relocations
3613 examined by the linker in check_relocs are against the function code
3614 sym .foo, while the dynamic relocation in the plt is emitted against
3615 the function descriptor symbol, foo. Somewhere along the line, we need
3616 to carefully copy dynamic link information from one symbol to the other.
3617 Secondly, the generic part of the elf linker will make .foo a dynamic
3618 symbol as is normal for most other backends. We need foo dynamic
3619 instead, at least for an application final link. However, when
3620 creating a shared library containing foo, we need to have both symbols
3621 dynamic so that references to .foo are satisfied during the early
3622 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3623 definition from some other object, eg. a static library.
3624
3625 Update: As of August 2004, we support a new convention. Function
3626 calls may use the function descriptor symbol, ie. "bl foo". This
3627 behaves exactly as "bl .foo". */
65f38f15 3628
1d483afe 3629/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3630 selects those that must be copied when linking a shared library,
3631 even when the symbol is local. */
65f38f15 3632
1d483afe
AM
3633static int
3634must_be_dyn_reloc (struct bfd_link_info *info,
3635 enum elf_ppc64_reloc_type r_type)
3636{
3637 switch (r_type)
3638 {
3639 default:
3640 return 1;
3641
3642 case R_PPC64_REL32:
3643 case R_PPC64_REL64:
3644 case R_PPC64_REL30:
3645 return 0;
3646
3647 case R_PPC64_TPREL16:
3648 case R_PPC64_TPREL16_LO:
3649 case R_PPC64_TPREL16_HI:
3650 case R_PPC64_TPREL16_HA:
3651 case R_PPC64_TPREL16_DS:
3652 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3653 case R_PPC64_TPREL16_HIGH:
3654 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3655 case R_PPC64_TPREL16_HIGHER:
3656 case R_PPC64_TPREL16_HIGHERA:
3657 case R_PPC64_TPREL16_HIGHEST:
3658 case R_PPC64_TPREL16_HIGHESTA:
3659 case R_PPC64_TPREL64:
3660 return !info->executable;
3661 }
3662}
65f38f15 3663
f4656909
AM
3664/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3665 copying dynamic variables from a shared lib into an app's dynbss
3666 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3667 shared lib. With code that gcc generates, it's vital that this be
3668 enabled; In the PowerPC64 ABI, the address of a function is actually
3669 the address of a function descriptor, which resides in the .opd
3670 section. gcc uses the descriptor directly rather than going via the
3671 GOT as some other ABI's do, which means that initialized function
3672 pointers must reference the descriptor. Thus, a function pointer
3673 initialized to the address of a function in a shared library will
3674 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3675 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3676 presents a problem as a plt entry for that function is also
3677 initialized from the function descriptor symbol and the copy reloc
3678 may not be initialized first. */
a23b6845 3679#define ELIMINATE_COPY_RELOCS 1
f4656909 3680
721956f4 3681/* Section name for stubs is the associated section name plus this
29942be8
NC
3682 string. */
3683#define STUB_SUFFIX ".stub"
721956f4
AM
3684
3685/* Linker stubs.
3686 ppc_stub_long_branch:
3687 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3688 destination, but a 24 bit branch in a stub section will reach.
3689 . b dest
3690
3691 ppc_stub_plt_branch:
3692 Similar to the above, but a 24 bit branch in the stub section won't
3693 reach its destination.
71a39c98
AM
3694 . addis %r11,%r2,xxx@toc@ha
3695 . ld %r12,xxx@toc@l(%r11)
3696 . mtctr %r12
721956f4
AM
3697 . bctr
3698
3699 ppc_stub_plt_call:
2c66dc6c
AM
3700 Used to call a function in a shared library. If it so happens that
3701 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3702 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3703 . std %r2,40(%r1)
71a39c98
AM
3704 . addis %r11,%r2,xxx@toc@ha
3705 . ld %r12,xxx+0@toc@l(%r11)
3706 . mtctr %r12
3707 . ld %r2,xxx+8@toc@l(%r11)
3708 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3709 . bctr
ad8e1ba5
AM
3710
3711 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3712 code to adjust the value and save r2 to support multiple toc sections.
3713 A ppc_stub_long_branch with an r2 offset looks like:
3714 . std %r2,40(%r1)
3715 . addis %r2,%r2,off@ha
3716 . addi %r2,%r2,off@l
3717 . b dest
3718
3719 A ppc_stub_plt_branch with an r2 offset looks like:
3720 . std %r2,40(%r1)
71a39c98
AM
3721 . addis %r11,%r2,xxx@toc@ha
3722 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3723 . addis %r2,%r2,off@ha
3724 . addi %r2,%r2,off@l
71a39c98 3725 . mtctr %r12
ad8e1ba5 3726 . bctr
ac2df442
AM
3727
3728 In cases where the "addis" instruction would add zero, the "addis" is
3729 omitted and following instructions modified slightly in some cases.
721956f4
AM
3730*/
3731
3732enum ppc_stub_type {
3733 ppc_stub_none,
3734 ppc_stub_long_branch,
ad8e1ba5 3735 ppc_stub_long_branch_r2off,
721956f4 3736 ppc_stub_plt_branch,
ad8e1ba5 3737 ppc_stub_plt_branch_r2off,
794e51c0
AM
3738 ppc_stub_plt_call,
3739 ppc_stub_plt_call_r2save
721956f4
AM
3740};
3741
3742struct ppc_stub_hash_entry {
3743
3744 /* Base hash table entry structure. */
3745 struct bfd_hash_entry root;
3746
ad8e1ba5
AM
3747 enum ppc_stub_type stub_type;
3748
721956f4
AM
3749 /* The stub section. */
3750 asection *stub_sec;
3751
3752 /* Offset within stub_sec of the beginning of this stub. */
3753 bfd_vma stub_offset;
3754
3755 /* Given the symbol's value and its section we can determine its final
3756 value when building the stubs (so the stub knows where to jump. */
3757 bfd_vma target_value;
3758 asection *target_section;
3759
721956f4
AM
3760 /* The symbol table entry, if any, that this was derived from. */
3761 struct ppc_link_hash_entry *h;
e054468f 3762 struct plt_entry *plt_ent;
721956f4
AM
3763
3764 /* Where this stub is being called from, or, in the case of combined
3765 stub sections, the first input section in the group. */
3766 asection *id_sec;
6911b7dc
AM
3767
3768 /* Symbol st_other. */
3769 unsigned char other;
721956f4
AM
3770};
3771
3772struct ppc_branch_hash_entry {
3773
3774 /* Base hash table entry structure. */
3775 struct bfd_hash_entry root;
3776
c456f082 3777 /* Offset within branch lookup table. */
721956f4
AM
3778 unsigned int offset;
3779
3780 /* Generation marker. */
3781 unsigned int iter;
3782};
65f38f15 3783
19e08130
AM
3784/* Used to track dynamic relocations for local symbols. */
3785struct ppc_dyn_relocs
3786{
3787 struct ppc_dyn_relocs *next;
3788
3789 /* The input section of the reloc. */
3790 asection *sec;
3791
3792 /* Total number of relocs copied for the input section. */
3793 unsigned int count : 31;
3794
3795 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3796 unsigned int ifunc : 1;
3797};
3798
65f38f15
AM
3799struct ppc_link_hash_entry
3800{
3801 struct elf_link_hash_entry elf;
3802
b3fac117
AM
3803 union {
3804 /* A pointer to the most recently used stub hash entry against this
3805 symbol. */
3806 struct ppc_stub_hash_entry *stub_cache;
3807
3808 /* A pointer to the next symbol starting with a '.' */
3809 struct ppc_link_hash_entry *next_dot_sym;
3810 } u;
721956f4 3811
65f38f15 3812 /* Track dynamic relocs copied for this symbol. */
6061a67d 3813 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3814
721956f4 3815 /* Link between function code and descriptor symbols. */
34814b9f 3816 struct ppc_link_hash_entry *oh;
721956f4 3817
e86ce104
AM
3818 /* Flag function code and descriptor symbols. */
3819 unsigned int is_func:1;
3820 unsigned int is_func_descriptor:1;
908b32fc 3821 unsigned int fake:1;
411e1bfb 3822
c5614fa4
AM
3823 /* Whether global opd/toc sym has been adjusted or not.
3824 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3825 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3826 unsigned int adjust_done:1;
3827
99877b66
AM
3828 /* Set if we twiddled this symbol to weak at some stage. */
3829 unsigned int was_undefined:1;
3830
411e1bfb 3831 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3832 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3833 corresponding relocs are encountered during check_relocs.
3834 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3835 indicate the corresponding GOT entry type is not needed.
3836 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3837 a TPREL one. We use a separate flag rather than setting TPREL
3838 just for convenience in distinguishing the two cases. */
3839#define TLS_GD 1 /* GD reloc. */
3840#define TLS_LD 2 /* LD reloc. */
3841#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3842#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3843#define TLS_TLS 16 /* Any TLS reloc. */
3844#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3845#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3846#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3847 unsigned char tls_mask;
65f38f15
AM
3848};
3849
3850/* ppc64 ELF linker hash table. */
3851
3852struct ppc_link_hash_table
3853{
3854 struct elf_link_hash_table elf;
3855
721956f4
AM
3856 /* The stub hash table. */
3857 struct bfd_hash_table stub_hash_table;
3858
3859 /* Another hash table for plt_branch stubs. */
3860 struct bfd_hash_table branch_hash_table;
3861
3b421ab3
AM
3862 /* Hash table for function prologue tocsave. */
3863 htab_t tocsave_htab;
3864
721956f4
AM
3865 /* Linker stub bfd. */
3866 bfd *stub_bfd;
3867
3868 /* Linker call-backs. */
4ce794b7
AM
3869 asection * (*add_stub_section) (const char *, asection *);
3870 void (*layout_sections_again) (void);
721956f4
AM
3871
3872 /* Array to keep track of which stub sections have been created, and
3873 information on stub grouping. */
3874 struct map_stub {
3875 /* This is the section to which stubs in the group will be attached. */
3876 asection *link_sec;
3877 /* The stub section. */
3878 asection *stub_sec;
ad8e1ba5
AM
3879 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3880 bfd_vma toc_off;
721956f4
AM
3881 } *stub_group;
3882
ad8e1ba5
AM
3883 /* Temp used when calculating TOC pointers. */
3884 bfd_vma toc_curr;
bf102f86
AM
3885 bfd *toc_bfd;
3886 asection *toc_first_sec;
ad8e1ba5 3887
8f3bab57
AM
3888 /* Highest input section id. */
3889 int top_id;
3890
734b6cf9
AM
3891 /* Highest output section index. */
3892 int top_index;
3893
b3fac117
AM
3894 /* Used when adding symbols. */
3895 struct ppc_link_hash_entry *dot_syms;
3896
734b6cf9
AM
3897 /* List of input sections for each output section. */
3898 asection **input_list;
721956f4 3899
65f38f15 3900 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3901 asection *got;
4ce794b7
AM
3902 asection *plt;
3903 asection *relplt;
e054468f
AM
3904 asection *iplt;
3905 asection *reliplt;
4ce794b7
AM
3906 asection *dynbss;
3907 asection *relbss;
3908 asection *glink;
82bd7b59 3909 asection *sfpr;
4ce794b7
AM
3910 asection *brlt;
3911 asection *relbrlt;
58d180e8 3912 asection *glink_eh_frame;
ec338859 3913
8387904d
AM
3914 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3915 struct ppc_link_hash_entry *tls_get_addr;
3916 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3917
927be08e
AM
3918 /* The size of reliplt used by got entry relocs. */
3919 bfd_size_type got_reli_size;
3920
9b5ecbd0 3921 /* Statistics. */
794e51c0 3922 unsigned long stub_count[ppc_stub_plt_call_r2save];
9b5ecbd0 3923
ee75fd95
AM
3924 /* Number of stubs against global syms. */
3925 unsigned long stub_globals;
3926
794e51c0
AM
3927 /* Alignment of PLT call stubs. */
3928 unsigned int plt_stub_align:4;
3929
ee67d69a
AM
3930 /* Set if we're linking code with function descriptors. */
3931 unsigned int opd_abi:1;
3932
9df0ef5f
AM
3933 /* Set if PLT call stubs should load r11. */
3934 unsigned int plt_static_chain:1;
3935
794e51c0
AM
3936 /* Set if PLT call stubs need a read-read barrier. */
3937 unsigned int plt_thread_safe:1;
3938
ad8e1ba5 3939 /* Set if we should emit symbols for stubs. */
99877b66 3940 unsigned int emit_stub_syms:1;
ad8e1ba5 3941
a7f2871e
AM
3942 /* Set if __tls_get_addr optimization should not be done. */
3943 unsigned int no_tls_get_addr_opt:1;
3944
4c52953f 3945 /* Support for multiple toc sections. */
33c0ec9d 3946 unsigned int do_multi_toc:1;
4c52953f 3947 unsigned int multi_toc_needed:1;
927be08e 3948 unsigned int second_toc_pass:1;
67f0cbdb 3949 unsigned int do_toc_opt:1;
4c52953f 3950
5d1634d7 3951 /* Set on error. */
99877b66 3952 unsigned int stub_error:1;
721956f4 3953
7d9616d7 3954 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3955 unsigned int twiddled_syms:1;
721956f4
AM
3956
3957 /* Incremented every time we size stubs. */
3958 unsigned int stub_iteration;
5d1634d7 3959
87d72d41
AM
3960 /* Small local sym cache. */
3961 struct sym_cache sym_cache;
65f38f15
AM
3962};
3963
4c52953f
AM
3964/* Rename some of the generic section flags to better document how they
3965 are used here. */
b0dddeec
AM
3966
3967/* Nonzero if this section has TLS related relocations. */
3968#define has_tls_reloc sec_flg0
3969
3970/* Nonzero if this section has a call to __tls_get_addr. */
3971#define has_tls_get_addr_call sec_flg1
3972
3973/* Nonzero if this section has any toc or got relocs. */
3974#define has_toc_reloc sec_flg2
3975
3976/* Nonzero if this section has a call to another section that uses
3977 the toc or got. */
d77c8a4b 3978#define makes_toc_func_call sec_flg3
b0dddeec
AM
3979
3980/* Recursion protection when determining above flag. */
d77c8a4b 3981#define call_check_in_progress sec_flg4
70cc837d 3982#define call_check_done sec_flg5
4c52953f 3983
65f38f15
AM
3984/* Get the ppc64 ELF linker hash table from a link_info structure. */
3985
3986#define ppc_hash_table(p) \
4dfe6ac6
NC
3987 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3988 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3989
721956f4
AM
3990#define ppc_stub_hash_lookup(table, string, create, copy) \
3991 ((struct ppc_stub_hash_entry *) \
3992 bfd_hash_lookup ((table), (string), (create), (copy)))
3993
3994#define ppc_branch_hash_lookup(table, string, create, copy) \
3995 ((struct ppc_branch_hash_entry *) \
3996 bfd_hash_lookup ((table), (string), (create), (copy)))
3997
3998/* Create an entry in the stub hash table. */
3999
4000static struct bfd_hash_entry *
4ce794b7
AM
4001stub_hash_newfunc (struct bfd_hash_entry *entry,
4002 struct bfd_hash_table *table,
4003 const char *string)
721956f4
AM
4004{
4005 /* Allocate the structure if it has not already been allocated by a
4006 subclass. */
4007 if (entry == NULL)
4008 {
4009 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4010 if (entry == NULL)
4011 return entry;
4012 }
4013
4014 /* Call the allocation method of the superclass. */
4015 entry = bfd_hash_newfunc (entry, table, string);
4016 if (entry != NULL)
4017 {
4018 struct ppc_stub_hash_entry *eh;
4019
4020 /* Initialize the local fields. */
4021 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4022 eh->stub_type = ppc_stub_none;
721956f4
AM
4023 eh->stub_sec = NULL;
4024 eh->stub_offset = 0;
4025 eh->target_value = 0;
4026 eh->target_section = NULL;
721956f4 4027 eh->h = NULL;
6911b7dc 4028 eh->plt_ent = NULL;
721956f4 4029 eh->id_sec = NULL;
6911b7dc 4030 eh->other = 0;
721956f4
AM
4031 }
4032
4033 return entry;
4034}
4035
4036/* Create an entry in the branch hash table. */
4037
4038static struct bfd_hash_entry *
4ce794b7
AM
4039branch_hash_newfunc (struct bfd_hash_entry *entry,
4040 struct bfd_hash_table *table,
4041 const char *string)
721956f4
AM
4042{
4043 /* Allocate the structure if it has not already been allocated by a
4044 subclass. */
4045 if (entry == NULL)
4046 {
4047 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4048 if (entry == NULL)
4049 return entry;
4050 }
4051
4052 /* Call the allocation method of the superclass. */
4053 entry = bfd_hash_newfunc (entry, table, string);
4054 if (entry != NULL)
4055 {
4056 struct ppc_branch_hash_entry *eh;
4057
4058 /* Initialize the local fields. */
4059 eh = (struct ppc_branch_hash_entry *) entry;
4060 eh->offset = 0;
4061 eh->iter = 0;
4062 }
4063
4064 return entry;
4065}
4066
65f38f15
AM
4067/* Create an entry in a ppc64 ELF linker hash table. */
4068
4069static struct bfd_hash_entry *
4ce794b7
AM
4070link_hash_newfunc (struct bfd_hash_entry *entry,
4071 struct bfd_hash_table *table,
4072 const char *string)
65f38f15
AM
4073{
4074 /* Allocate the structure if it has not already been allocated by a
4075 subclass. */
4076 if (entry == NULL)
4077 {
4078 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4079 if (entry == NULL)
4080 return entry;
4081 }
4082
4083 /* Call the allocation method of the superclass. */
4084 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4085 if (entry != NULL)
4086 {
4087 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4088
b3fac117 4089 memset (&eh->u.stub_cache, 0,
908b32fc 4090 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4091 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4092
4093 /* When making function calls, old ABI code references function entry
4094 points (dot symbols), while new ABI code references the function
4095 descriptor symbol. We need to make any combination of reference and
4096 definition work together, without breaking archive linking.
4097
4098 For a defined function "foo" and an undefined call to "bar":
4099 An old object defines "foo" and ".foo", references ".bar" (possibly
4100 "bar" too).
4101 A new object defines "foo" and references "bar".
4102
4103 A new object thus has no problem with its undefined symbols being
4104 satisfied by definitions in an old object. On the other hand, the
4105 old object won't have ".bar" satisfied by a new object.
4106
4107 Keep a list of newly added dot-symbols. */
4108
4109 if (string[0] == '.')
4110 {
4111 struct ppc_link_hash_table *htab;
4112
4113 htab = (struct ppc_link_hash_table *) table;
4114 eh->u.next_dot_sym = htab->dot_syms;
4115 htab->dot_syms = eh;
4116 }
65f38f15
AM
4117 }
4118
4119 return entry;
4120}
4121
3b421ab3
AM
4122struct tocsave_entry {
4123 asection *sec;
4124 bfd_vma offset;
4125};
4126
4127static hashval_t
4128tocsave_htab_hash (const void *p)
4129{
4130 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4131 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4132}
4133
4134static int
4135tocsave_htab_eq (const void *p1, const void *p2)
4136{
4137 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4138 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4139 return e1->sec == e2->sec && e1->offset == e2->offset;
4140}
4141
65f38f15
AM
4142/* Create a ppc64 ELF linker hash table. */
4143
4144static struct bfd_link_hash_table *
4ce794b7 4145ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4146{
4147 struct ppc_link_hash_table *htab;
4148 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4149
4ce794b7 4150 htab = bfd_zmalloc (amt);
65f38f15
AM
4151 if (htab == NULL)
4152 return NULL;
4153
66eb6687 4154 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4155 sizeof (struct ppc_link_hash_entry),
4156 PPC64_ELF_DATA))
65f38f15 4157 {
e2d34d7d 4158 free (htab);
65f38f15
AM
4159 return NULL;
4160 }
4161
721956f4 4162 /* Init the stub hash table too. */
66eb6687
AM
4163 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4164 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
4165 return NULL;
4166
4167 /* And the branch hash table. */
66eb6687
AM
4168 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4169 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
4170 return NULL;
4171
3b421ab3
AM
4172 htab->tocsave_htab = htab_try_create (1024,
4173 tocsave_htab_hash,
4174 tocsave_htab_eq,
4175 NULL);
4176 if (htab->tocsave_htab == NULL)
4177 return NULL;
4178
3254fd24
AM
4179 /* Initializing two fields of the union is just cosmetic. We really
4180 only care about glist, but when compiled on a 32-bit host the
4181 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4182 debugger inspection of these fields look nicer. */
a6aa5195
AM
4183 htab->elf.init_got_refcount.refcount = 0;
4184 htab->elf.init_got_refcount.glist = NULL;
4185 htab->elf.init_plt_refcount.refcount = 0;
4186 htab->elf.init_plt_refcount.glist = NULL;
4187 htab->elf.init_got_offset.offset = 0;
4188 htab->elf.init_got_offset.glist = NULL;
4189 htab->elf.init_plt_offset.offset = 0;
4190 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4191
65f38f15
AM
4192 return &htab->elf.root;
4193}
4194
721956f4
AM
4195/* Free the derived linker hash table. */
4196
4197static void
4ce794b7 4198ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4 4199{
3b421ab3 4200 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
721956f4 4201
3b421ab3
AM
4202 bfd_hash_table_free (&htab->stub_hash_table);
4203 bfd_hash_table_free (&htab->branch_hash_table);
4204 if (htab->tocsave_htab)
4205 htab_delete (htab->tocsave_htab);
9f7c3e5e 4206 _bfd_elf_link_hash_table_free (hash);
721956f4
AM
4207}
4208
bfeb4a28
AM
4209/* Create sections for linker generated code. */
4210
4211static bfd_boolean
4212create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4213{
4214 struct ppc_link_hash_table *htab;
4215 flagword flags;
4216
4217 htab = ppc_hash_table (info);
4218
4219 /* Create .sfpr for code to save and restore fp regs. */
4220 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4221 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4222 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4223 flags);
4224 if (htab->sfpr == NULL
4225 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4226 return FALSE;
4227
4228 /* Create .glink for lazy dynamic linking support. */
4229 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4230 flags);
4231 if (htab->glink == NULL
4232 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4233 return FALSE;
4234
4235 if (!info->no_ld_generated_unwind_info)
4236 {
4237 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4238 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4239 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4240 ".eh_frame",
4241 flags);
4242 if (htab->glink_eh_frame == NULL
4243 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4244 return FALSE;
4245 }
4246
4247 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4248 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4249 if (htab->iplt == NULL
4250 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4251 return FALSE;
4252
4253 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4254 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4255 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4256 ".rela.iplt",
4257 flags);
4258 if (htab->reliplt == NULL
4259 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4260 return FALSE;
4261
4262 /* Create branch lookup table for plt_branch stubs. */
4263 flags = (SEC_ALLOC | SEC_LOAD
4264 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4265 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4266 flags);
4267 if (htab->brlt == NULL
4268 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4269 return FALSE;
4270
4271 if (!info->shared)
4272 return TRUE;
4273
4274 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4275 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4276 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4277 ".rela.branch_lt",
4278 flags);
4279 if (htab->relbrlt == NULL
4280 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4281 return FALSE;
4282
4283 return TRUE;
4284}
4285
e717da7e
AM
4286/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4287
bfeb4a28 4288bfd_boolean
e717da7e
AM
4289ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4290{
4291 struct ppc_link_hash_table *htab;
4292
4293 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4294
4295/* Always hook our dynamic sections into the first bfd, which is the
4296 linker created stub bfd. This ensures that the GOT header is at
4297 the start of the output TOC section. */
4298 htab = ppc_hash_table (info);
4dfe6ac6 4299 if (htab == NULL)
bfeb4a28 4300 return FALSE;
e717da7e
AM
4301 htab->stub_bfd = abfd;
4302 htab->elf.dynobj = abfd;
bfeb4a28
AM
4303
4304 if (info->relocatable)
4305 return TRUE;
4306
4307 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4308}
4309
721956f4
AM
4310/* Build a name for an entry in the stub hash table. */
4311
4312static char *
4ce794b7
AM
4313ppc_stub_name (const asection *input_section,
4314 const asection *sym_sec,
4315 const struct ppc_link_hash_entry *h,
4316 const Elf_Internal_Rela *rel)
721956f4
AM
4317{
4318 char *stub_name;
bcaa2f82 4319 ssize_t len;
721956f4
AM
4320
4321 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4322 offsets from a sym as a branch target? In fact, we could
4323 probably assume the addend is always zero. */
4324 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4325
4326 if (h)
4327 {
4328 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4329 stub_name = bfd_malloc (len);
46de2a7c
AM
4330 if (stub_name == NULL)
4331 return stub_name;
4332
bcaa2f82
AM
4333 len = sprintf (stub_name, "%08x.%s+%x",
4334 input_section->id & 0xffffffff,
4335 h->elf.root.root.string,
4336 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4337 }
4338 else
4339 {
ad8e1ba5 4340 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4341 stub_name = bfd_malloc (len);
46de2a7c
AM
4342 if (stub_name == NULL)
4343 return stub_name;
4344
bcaa2f82
AM
4345 len = sprintf (stub_name, "%08x.%x:%x+%x",
4346 input_section->id & 0xffffffff,
4347 sym_sec->id & 0xffffffff,
4348 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4349 (int) rel->r_addend & 0xffffffff);
721956f4 4350 }
bcaa2f82 4351 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4352 stub_name[len - 2] = 0;
721956f4
AM
4353 return stub_name;
4354}
4355
4356/* Look up an entry in the stub hash. Stub entries are cached because
4357 creating the stub name takes a bit of time. */
4358
4359static struct ppc_stub_hash_entry *
4ce794b7
AM
4360ppc_get_stub_entry (const asection *input_section,
4361 const asection *sym_sec,
039b3fef 4362 struct ppc_link_hash_entry *h,
4ce794b7
AM
4363 const Elf_Internal_Rela *rel,
4364 struct ppc_link_hash_table *htab)
721956f4
AM
4365{
4366 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4367 const asection *id_sec;
4368
4369 /* If this input section is part of a group of sections sharing one
4370 stub section, then use the id of the first section in the group.
4371 Stub names need to include a section id, as there may well be
4372 more than one stub used to reach say, printf, and we need to
4373 distinguish between them. */
4374 id_sec = htab->stub_group[input_section->id].link_sec;
4375
b3fac117
AM
4376 if (h != NULL && h->u.stub_cache != NULL
4377 && h->u.stub_cache->h == h
4378 && h->u.stub_cache->id_sec == id_sec)
721956f4 4379 {
b3fac117 4380 stub_entry = h->u.stub_cache;
721956f4
AM
4381 }
4382 else
4383 {
4384 char *stub_name;
4385
4386 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4387 if (stub_name == NULL)
4388 return NULL;
4389
4390 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4391 stub_name, FALSE, FALSE);
721956f4 4392 if (h != NULL)
b3fac117 4393 h->u.stub_cache = stub_entry;
721956f4
AM
4394
4395 free (stub_name);
4396 }
4397
4398 return stub_entry;
4399}
4400
4401/* Add a new stub entry to the stub hash. Not all fields of the new
4402 stub entry are initialised. */
4403
4404static struct ppc_stub_hash_entry *
4ce794b7
AM
4405ppc_add_stub (const char *stub_name,
4406 asection *section,
25f53a85 4407 struct bfd_link_info *info)
721956f4 4408{
25f53a85 4409 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4410 asection *link_sec;
4411 asection *stub_sec;
4412 struct ppc_stub_hash_entry *stub_entry;
4413
4414 link_sec = htab->stub_group[section->id].link_sec;
4415 stub_sec = htab->stub_group[section->id].stub_sec;
4416 if (stub_sec == NULL)
4417 {
4418 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4419 if (stub_sec == NULL)
4420 {
d4c88bbb 4421 size_t namelen;
721956f4
AM
4422 bfd_size_type len;
4423 char *s_name;
4424
d4c88bbb
AM
4425 namelen = strlen (link_sec->name);
4426 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4427 s_name = bfd_alloc (htab->stub_bfd, len);
4428 if (s_name == NULL)
4429 return NULL;
4430
d4c88bbb
AM
4431 memcpy (s_name, link_sec->name, namelen);
4432 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4433 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4434 if (stub_sec == NULL)
4435 return NULL;
4436 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4437 }
4438 htab->stub_group[section->id].stub_sec = stub_sec;
4439 }
4440
4441 /* Enter this entry into the linker stub hash table. */
4442 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4443 TRUE, FALSE);
721956f4
AM
4444 if (stub_entry == NULL)
4445 {
8de848d8 4446 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4447 section->owner, stub_name);
721956f4
AM
4448 return NULL;
4449 }
4450
4451 stub_entry->stub_sec = stub_sec;
4452 stub_entry->stub_offset = 0;
4453 stub_entry->id_sec = link_sec;
4454 return stub_entry;
4455}
4456
e717da7e
AM
4457/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4458 not already done. */
65f38f15 4459
b34976b6 4460static bfd_boolean
e717da7e 4461create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4462{
e717da7e
AM
4463 asection *got, *relgot;
4464 flagword flags;
4465 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4466
0c8d6e5c 4467 if (!is_ppc64_elf (abfd))
0ffa91dd 4468 return FALSE;
4dfe6ac6
NC
4469 if (htab == NULL)
4470 return FALSE;
0ffa91dd 4471
4ce794b7 4472 if (!htab->got)
e717da7e
AM
4473 {
4474 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4475 return FALSE;
4476
3d4d4302 4477 htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
e717da7e
AM
4478 if (!htab->got)
4479 abort ();
4480 }
4481
4482 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4483 | SEC_LINKER_CREATED);
4484
c456f082 4485 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4486 if (!got
e717da7e
AM
4487 || !bfd_set_section_alignment (abfd, got, 3))
4488 return FALSE;
65f38f15 4489
c456f082
AM
4490 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4491 flags | SEC_READONLY);
e717da7e 4492 if (!relgot
e717da7e 4493 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4494 return FALSE;
e717da7e
AM
4495
4496 ppc64_elf_tdata (abfd)->got = got;
4497 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4498 return TRUE;
65f38f15 4499}
5bd4f169 4500
82bd7b59 4501/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4502
b34976b6 4503static bfd_boolean
4ce794b7 4504ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4505{
65f38f15 4506 struct ppc_link_hash_table *htab;
5bd4f169 4507
65f38f15 4508 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4509 return FALSE;
65f38f15 4510
e717da7e 4511 htab = ppc_hash_table (info);
4dfe6ac6
NC
4512 if (htab == NULL)
4513 return FALSE;
4514
e717da7e 4515 if (!htab->got)
3d4d4302
AM
4516 htab->got = bfd_get_linker_section (dynobj, ".got");
4517 htab->plt = bfd_get_linker_section (dynobj, ".plt");
4518 htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4519 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
65f38f15 4520 if (!info->shared)
3d4d4302 4521 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4522
e717da7e 4523 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4524 || (!info->shared && !htab->relbss))
65f38f15
AM
4525 abort ();
4526
b34976b6 4527 return TRUE;
5bd4f169
AM
4528}
4529
b31867b6
AM
4530/* Follow indirect and warning symbol links. */
4531
4532static inline struct bfd_link_hash_entry *
4533follow_link (struct bfd_link_hash_entry *h)
4534{
4535 while (h->type == bfd_link_hash_indirect
4536 || h->type == bfd_link_hash_warning)
4537 h = h->u.i.link;
4538 return h;
4539}
4540
4541static inline struct elf_link_hash_entry *
4542elf_follow_link (struct elf_link_hash_entry *h)
4543{
4544 return (struct elf_link_hash_entry *) follow_link (&h->root);
4545}
4546
4547static inline struct ppc_link_hash_entry *
4548ppc_follow_link (struct ppc_link_hash_entry *h)
4549{
4550 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4551}
4552
40d16e0b
AM
4553/* Merge PLT info on FROM with that on TO. */
4554
4555static void
4556move_plt_plist (struct ppc_link_hash_entry *from,
4557 struct ppc_link_hash_entry *to)
4558{
4559 if (from->elf.plt.plist != NULL)
4560 {
4561 if (to->elf.plt.plist != NULL)
4562 {
4563 struct plt_entry **entp;
4564 struct plt_entry *ent;
4565
4566 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4567 {
4568 struct plt_entry *dent;
4569
4570 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4571 if (dent->addend == ent->addend)
4572 {
4573 dent->plt.refcount += ent->plt.refcount;
4574 *entp = ent->next;
4575 break;
4576 }
4577 if (dent == NULL)
4578 entp = &ent->next;
4579 }
4580 *entp = to->elf.plt.plist;
4581 }
4582
4583 to->elf.plt.plist = from->elf.plt.plist;
4584 from->elf.plt.plist = NULL;
4585 }
4586}
4587
65f38f15
AM
4588/* Copy the extra info we tack onto an elf_link_hash_entry. */
4589
4590static void
fcfa13d2
AM
4591ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4592 struct elf_link_hash_entry *dir,
4593 struct elf_link_hash_entry *ind)
65f38f15
AM
4594{
4595 struct ppc_link_hash_entry *edir, *eind;
4596
4597 edir = (struct ppc_link_hash_entry *) dir;
4598 eind = (struct ppc_link_hash_entry *) ind;
4599
c79d6685
AM
4600 edir->is_func |= eind->is_func;
4601 edir->is_func_descriptor |= eind->is_func_descriptor;
4602 edir->tls_mask |= eind->tls_mask;
4603 if (eind->oh != NULL)
4604 edir->oh = ppc_follow_link (eind->oh);
4605
4606 /* If called to transfer flags for a weakdef during processing
4607 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4608 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4609 if (!(ELIMINATE_COPY_RELOCS
4610 && eind->elf.root.type != bfd_link_hash_indirect
4611 && edir->elf.dynamic_adjusted))
4612 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4613
4614 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4615 edir->elf.ref_regular |= eind->elf.ref_regular;
4616 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4617 edir->elf.needs_plt |= eind->elf.needs_plt;
4618
411e1bfb 4619 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4620 if (eind->dyn_relocs != NULL)
65f38f15 4621 {
bbd7ec4a
AM
4622 if (edir->dyn_relocs != NULL)
4623 {
6061a67d
AM
4624 struct elf_dyn_relocs **pp;
4625 struct elf_dyn_relocs *p;
bbd7ec4a 4626
fcfa13d2 4627 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4628 list. Merge any entries against the same section. */
4629 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4630 {
6061a67d 4631 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4632
4633 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4634 if (q->sec == p->sec)
4635 {
4636 q->pc_count += p->pc_count;
4637 q->count += p->count;
4638 *pp = p->next;
4639 break;
4640 }
4641 if (q == NULL)
4642 pp = &p->next;
4643 }
4644 *pp = edir->dyn_relocs;
4645 }
4646
65f38f15
AM
4647 edir->dyn_relocs = eind->dyn_relocs;
4648 eind->dyn_relocs = NULL;
4649 }
65f38f15 4650
68ba6d40
AM
4651 /* If we were called to copy over info for a weak sym, that's all.
4652 You might think dyn_relocs need not be copied over; After all,
4653 both syms will be dynamic or both non-dynamic so we're just
68ffbac6 4654 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
68ba6d40
AM
4655 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4656 dyn_relocs in read-only sections, and it does so on what is the
4657 DIR sym here. */
4658 if (eind->elf.root.type != bfd_link_hash_indirect)
4659 return;
4660
81848ca0
AM
4661 /* Copy over got entries that we may have already seen to the
4662 symbol which just became indirect. */
411e1bfb
AM
4663 if (eind->elf.got.glist != NULL)
4664 {
4665 if (edir->elf.got.glist != NULL)
4666 {
4667 struct got_entry **entp;
4668 struct got_entry *ent;
4669
4670 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4671 {
4672 struct got_entry *dent;
4673
4674 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4675 if (dent->addend == ent->addend
e717da7e 4676 && dent->owner == ent->owner
411e1bfb
AM
4677 && dent->tls_type == ent->tls_type)
4678 {
4679 dent->got.refcount += ent->got.refcount;
4680 *entp = ent->next;
4681 break;
4682 }
4683 if (dent == NULL)
4684 entp = &ent->next;
4685 }
4686 *entp = edir->elf.got.glist;
4687 }
4688
4689 edir->elf.got.glist = eind->elf.got.glist;
4690 eind->elf.got.glist = NULL;
4691 }
4692
4693 /* And plt entries. */
40d16e0b 4694 move_plt_plist (eind, edir);
411e1bfb 4695
fcfa13d2 4696 if (eind->elf.dynindx != -1)
411e1bfb 4697 {
fcfa13d2
AM
4698 if (edir->elf.dynindx != -1)
4699 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4700 edir->elf.dynstr_index);
411e1bfb
AM
4701 edir->elf.dynindx = eind->elf.dynindx;
4702 edir->elf.dynstr_index = eind->elf.dynstr_index;
4703 eind->elf.dynindx = -1;
4704 eind->elf.dynstr_index = 0;
4705 }
411e1bfb
AM
4706}
4707
8387904d
AM
4708/* Find the function descriptor hash entry from the given function code
4709 hash entry FH. Link the entries via their OH fields. */
4710
4711static struct ppc_link_hash_entry *
b31867b6 4712lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4713{
4714 struct ppc_link_hash_entry *fdh = fh->oh;
4715
4716 if (fdh == NULL)
4717 {
4718 const char *fd_name = fh->elf.root.root.string + 1;
4719
4720 fdh = (struct ppc_link_hash_entry *)
4721 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4722 if (fdh == NULL)
4723 return fdh;
4724
4725 fdh->is_func_descriptor = 1;
4726 fdh->oh = fh;
4727 fh->is_func = 1;
4728 fh->oh = fdh;
8387904d
AM
4729 }
4730
b31867b6 4731 return ppc_follow_link (fdh);
8387904d
AM
4732}
4733
bb700d78
AM
4734/* Make a fake function descriptor sym for the code sym FH. */
4735
4736static struct ppc_link_hash_entry *
4737make_fdh (struct bfd_link_info *info,
908b32fc 4738 struct ppc_link_hash_entry *fh)
bb700d78
AM
4739{
4740 bfd *abfd;
4741 asymbol *newsym;
4742 struct bfd_link_hash_entry *bh;
4743 struct ppc_link_hash_entry *fdh;
4744
4745 abfd = fh->elf.root.u.undef.abfd;
4746 newsym = bfd_make_empty_symbol (abfd);
4747 newsym->name = fh->elf.root.root.string + 1;
4748 newsym->section = bfd_und_section_ptr;
4749 newsym->value = 0;
908b32fc 4750 newsym->flags = BSF_WEAK;
bb700d78
AM
4751
4752 bh = NULL;
4753 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4754 newsym->flags, newsym->section,
4755 newsym->value, NULL, FALSE, FALSE,
4756 &bh))
4757 return NULL;
4758
4759 fdh = (struct ppc_link_hash_entry *) bh;
4760 fdh->elf.non_elf = 0;
908b32fc
AM
4761 fdh->fake = 1;
4762 fdh->is_func_descriptor = 1;
4763 fdh->oh = fh;
4764 fh->is_func = 1;
4765 fh->oh = fdh;
bb700d78
AM
4766 return fdh;
4767}
4768
8387904d
AM
4769/* Fix function descriptor symbols defined in .opd sections to be
4770 function type. */
555cd476
AM
4771
4772static bfd_boolean
c16153ae 4773ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4774 struct bfd_link_info *info,
555cd476 4775 Elf_Internal_Sym *isym,
6911b7dc 4776 const char **name,
555cd476
AM
4777 flagword *flags ATTRIBUTE_UNUSED,
4778 asection **sec,
4779 bfd_vma *value ATTRIBUTE_UNUSED)
4780{
f64b2e8d
NC
4781 if ((ibfd->flags & DYNAMIC) == 0
4782 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4783 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4784
e054468f 4785 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4786 {
4787 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4788 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4789 }
e054468f
AM
4790 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4791 ;
4792 else if (*sec != NULL
70cc837d 4793 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4794 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4795
6911b7dc
AM
4796 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4797 {
4798 if (abiversion (ibfd) == 0)
4799 set_abiversion (ibfd, 2);
4800 else if (abiversion (ibfd) == 1)
4801 {
4802 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4803 " for ABI version 1\n"), name);
4804 bfd_set_error (bfd_error_bad_value);
4805 return FALSE;
4806 }
4807 }
4808
555cd476
AM
4809 return TRUE;
4810}
4811
6911b7dc
AM
4812/* Merge non-visibility st_other attributes: local entry point. */
4813
4814static void
4815ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4816 const Elf_Internal_Sym *isym,
4817 bfd_boolean definition,
4818 bfd_boolean dynamic)
4819{
4820 if (definition && !dynamic)
4821 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4822 | ELF_ST_VISIBILITY (h->other));
4823}
4824
8387904d 4825/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4826 inclusion of a new ABI object archive that defines "bar".
4827 NAME is a symbol defined in an archive. Return a symbol in the hash
4828 table that might be satisfied by the archive symbols. */
8387904d
AM
4829
4830static struct elf_link_hash_entry *
4831ppc64_elf_archive_symbol_lookup (bfd *abfd,
4832 struct bfd_link_info *info,
4833 const char *name)
4834{
4835 struct elf_link_hash_entry *h;
4836 char *dot_name;
4837 size_t len;
4838
4839 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4840 if (h != NULL
4841 /* Don't return this sym if it is a fake function descriptor
4842 created by add_symbol_adjust. */
4843 && !(h->root.type == bfd_link_hash_undefweak
4844 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4845 return h;
4846
4847 if (name[0] == '.')
4848 return h;
4849
4850 len = strlen (name);
4851 dot_name = bfd_alloc (abfd, len + 2);
4852 if (dot_name == NULL)
4853 return (struct elf_link_hash_entry *) 0 - 1;
4854 dot_name[0] = '.';
4855 memcpy (dot_name + 1, name, len + 1);
4856 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4857 bfd_release (abfd, dot_name);
4858 return h;
4859}
4860
4861/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4862 new ABI object defines "bar". Well, at least, undefined dot symbols
4863 are made weak. This stops later archive searches from including an
4864 object if we already have a function descriptor definition. It also
35b0ce59
AM
4865 prevents the linker complaining about undefined symbols.
4866 We also check and correct mismatched symbol visibility here. The
4867 most restrictive visibility of the function descriptor and the
4868 function entry symbol is used. */
8387904d
AM
4869
4870static bfd_boolean
b3fac117 4871add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4872{
8387904d
AM
4873 struct ppc_link_hash_table *htab;
4874 struct ppc_link_hash_entry *fdh;
4875
b3fac117 4876 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4877 return TRUE;
4878
b3fac117
AM
4879 if (eh->elf.root.type == bfd_link_hash_warning)
4880 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4881
b3fac117
AM
4882 if (eh->elf.root.root.string[0] != '.')
4883 abort ();
8387904d 4884
b3fac117 4885 htab = ppc_hash_table (info);
4dfe6ac6
NC
4886 if (htab == NULL)
4887 return FALSE;
4888
b31867b6
AM
4889 fdh = lookup_fdh (eh, htab);
4890 if (fdh == NULL)
4891 {
4892 if (!info->relocatable
4893 && (eh->elf.root.type == bfd_link_hash_undefined
4894 || eh->elf.root.type == bfd_link_hash_undefweak)
4895 && eh->elf.ref_regular)
4896 {
4897 /* Make an undefweak function descriptor sym, which is enough to
4898 pull in an --as-needed shared lib, but won't cause link
4899 errors. Archives are handled elsewhere. */
4900 fdh = make_fdh (info, eh);
4901 if (fdh == NULL)
4902 return FALSE;
4903 fdh->elf.ref_regular = 1;
4904 }
bb700d78 4905 }
b31867b6 4906 else
8387904d 4907 {
35b0ce59
AM
4908 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4909 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4910 if (entry_vis < descr_vis)
4911 fdh->elf.other += entry_vis - descr_vis;
4912 else if (entry_vis > descr_vis)
4913 eh->elf.other += descr_vis - entry_vis;
4914
e87d886e
AM
4915 if ((fdh->elf.root.type == bfd_link_hash_defined
4916 || fdh->elf.root.type == bfd_link_hash_defweak)
4917 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4918 {
4919 eh->elf.root.type = bfd_link_hash_undefweak;
4920 eh->was_undefined = 1;
4921 htab->twiddled_syms = 1;
4922 }
8387904d 4923 }
99877b66 4924
8387904d
AM
4925 return TRUE;
4926}
4927
b3fac117
AM
4928/* Process list of dot-symbols we made in link_hash_newfunc. */
4929
8387904d 4930static bfd_boolean
7d9616d7 4931ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4932{
99877b66 4933 struct ppc_link_hash_table *htab;
b3fac117 4934 struct ppc_link_hash_entry **p, *eh;
433817dd 4935
0c8d6e5c 4936 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4937 return TRUE;
4dfe6ac6
NC
4938 htab = ppc_hash_table (info);
4939 if (htab == NULL)
4940 return FALSE;
35b0ce59 4941
0c8d6e5c 4942 if (is_ppc64_elf (ibfd))
b3fac117
AM
4943 {
4944 p = &htab->dot_syms;
4945 while ((eh = *p) != NULL)
4946 {
4947 *p = NULL;
1c865ab2
AM
4948 if (&eh->elf == htab->elf.hgot)
4949 ;
4950 else if (htab->elf.hgot == NULL
4951 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4952 htab->elf.hgot = &eh->elf;
4953 else if (!add_symbol_adjust (eh, info))
b3fac117
AM
4954 return FALSE;
4955 p = &eh->u.next_dot_sym;
4956 }
4957 }
4958
4959 /* Clear the list for non-ppc64 input files. */
4960 p = &htab->dot_syms;
4961 while ((eh = *p) != NULL)
4962 {
4963 *p = NULL;
4964 p = &eh->u.next_dot_sym;
4965 }
99877b66
AM
4966
4967 /* We need to fix the undefs list for any syms we have twiddled to
4968 undef_weak. */
4969 if (htab->twiddled_syms)
4970 {
77cfaee6 4971 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4972 htab->twiddled_syms = 0;
4973 }
b3fac117 4974 return TRUE;
8387904d
AM
4975}
4976
97fed1c9
JJ
4977/* Undo hash table changes when an --as-needed input file is determined
4978 not to be needed. */
4979
4980static bfd_boolean
e5034e59
AM
4981ppc64_elf_notice_as_needed (bfd *ibfd,
4982 struct bfd_link_info *info,
4983 enum notice_asneeded_action act)
97fed1c9 4984{
e5034e59
AM
4985 if (act == notice_not_needed)
4986 {
4987 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4988
e5034e59
AM
4989 if (htab == NULL)
4990 return FALSE;
4dfe6ac6 4991
e5034e59
AM
4992 htab->dot_syms = NULL;
4993 }
4994 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4995}
4996
aa374f67
AM
4997/* If --just-symbols against a final linked binary, then assume we need
4998 toc adjusting stubs when calling functions defined there. */
4999
5000static void
5001ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5002{
5003 if ((sec->flags & SEC_CODE) != 0
5004 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5005 && is_ppc64_elf (sec->owner))
5006 {
5007 asection *got = bfd_get_section_by_name (sec->owner, ".got");
5008 if (got != NULL
5009 && got->size >= elf_backend_got_header_size
5010 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5011 sec->has_toc_reloc = 1;
5012 }
5013 _bfd_elf_link_just_syms (sec, info);
5014}
5015
e054468f 5016static struct plt_entry **
4ce794b7
AM
5017update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5018 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5019{
5020 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5021 struct plt_entry **local_plt;
f961d9dd 5022 unsigned char *local_got_tls_masks;
411e1bfb
AM
5023
5024 if (local_got_ents == NULL)
5025 {
5026 bfd_size_type size = symtab_hdr->sh_info;
5027
e054468f
AM
5028 size *= (sizeof (*local_got_ents)
5029 + sizeof (*local_plt)
5030 + sizeof (*local_got_tls_masks));
4ce794b7 5031 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5032 if (local_got_ents == NULL)
e054468f 5033 return NULL;
411e1bfb
AM
5034 elf_local_got_ents (abfd) = local_got_ents;
5035 }
5036
e054468f 5037 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5038 {
5039 struct got_entry *ent;
5040
5041 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5042 if (ent->addend == r_addend
5043 && ent->owner == abfd
5044 && ent->tls_type == tls_type)
411e1bfb
AM
5045 break;
5046 if (ent == NULL)
5047 {
5048 bfd_size_type amt = sizeof (*ent);
4ce794b7 5049 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5050 if (ent == NULL)
5051 return FALSE;
5052 ent->next = local_got_ents[r_symndx];
5053 ent->addend = r_addend;
e717da7e 5054 ent->owner = abfd;
411e1bfb 5055 ent->tls_type = tls_type;
927be08e 5056 ent->is_indirect = FALSE;
411e1bfb
AM
5057 ent->got.refcount = 0;
5058 local_got_ents[r_symndx] = ent;
5059 }
5060 ent->got.refcount += 1;
5061 }
5062
e054468f 5063 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5064 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5065 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5066
5067 return local_plt + r_symndx;
65f38f15
AM
5068}
5069
411e1bfb 5070static bfd_boolean
e054468f 5071update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5072{
411e1bfb 5073 struct plt_entry *ent;
1e2f5b6e 5074
e054468f 5075 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5076 if (ent->addend == addend)
5077 break;
5078 if (ent == NULL)
1e2f5b6e 5079 {
411e1bfb 5080 bfd_size_type amt = sizeof (*ent);
4ce794b7 5081 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5082 if (ent == NULL)
5083 return FALSE;
e054468f 5084 ent->next = *plist;
411e1bfb
AM
5085 ent->addend = addend;
5086 ent->plt.refcount = 0;
e054468f 5087 *plist = ent;
1e2f5b6e 5088 }
411e1bfb 5089 ent->plt.refcount += 1;
b34976b6 5090 return TRUE;
1e2f5b6e
AM
5091}
5092
e054468f
AM
5093static bfd_boolean
5094is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5095{
5096 return (r_type == R_PPC64_REL24
5097 || r_type == R_PPC64_REL14
5098 || r_type == R_PPC64_REL14_BRTAKEN
5099 || r_type == R_PPC64_REL14_BRNTAKEN
5100 || r_type == R_PPC64_ADDR24
5101 || r_type == R_PPC64_ADDR14
5102 || r_type == R_PPC64_ADDR14_BRTAKEN
5103 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5104}
5105
5bd4f169 5106/* Look through the relocs for a section during the first phase, and
65f38f15 5107 calculate needed space in the global offset table, procedure
5d1634d7 5108 linkage table, and dynamic reloc sections. */
5bd4f169 5109
b34976b6 5110static bfd_boolean
4ce794b7
AM
5111ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5112 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5113{
65f38f15 5114 struct ppc_link_hash_table *htab;
5bd4f169 5115 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5116 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5117 const Elf_Internal_Rela *rel;
5118 const Elf_Internal_Rela *rel_end;
5bd4f169 5119 asection *sreloc;
1e2f5b6e 5120 asection **opd_sym_map;
3a71aa26 5121 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5122
1049f94e 5123 if (info->relocatable)
b34976b6 5124 return TRUE;
5bd4f169 5125
680a3378
AM
5126 /* Don't do anything special with non-loaded, non-alloced sections.
5127 In particular, any relocs in such sections should not affect GOT
5128 and PLT reference counting (ie. we don't allow them to create GOT
5129 or PLT entries), there's no possibility or desire to optimize TLS
5130 relocs, and there's not much point in propagating relocs to shared
5131 libs that the dynamic linker won't relocate. */
5132 if ((sec->flags & SEC_ALLOC) == 0)
5133 return TRUE;
5134
0c8d6e5c 5135 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5136
65f38f15 5137 htab = ppc_hash_table (info);
4dfe6ac6
NC
5138 if (htab == NULL)
5139 return FALSE;
5140
3a71aa26
AM
5141 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5142 FALSE, FALSE, TRUE);
5143 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5144 FALSE, FALSE, TRUE);
0ffa91dd 5145 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5146 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5147 sreloc = NULL;
1e2f5b6e 5148 opd_sym_map = NULL;
70cc837d 5149 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
5150 {
5151 /* Garbage collection needs some extra help with .opd sections.
5152 We don't want to necessarily keep everything referenced by
5153 relocs in .opd, as that would keep all functions. Instead,
5154 if we reference an .opd symbol (a function descriptor), we
5155 want to keep the function code symbol's section. This is
5156 easy for global symbols, but for local syms we need to keep
74f0fb50 5157 information about the associated function section. */
1e2f5b6e
AM
5158 bfd_size_type amt;
5159
ee67d69a
AM
5160 if (abiversion (abfd) == 0)
5161 set_abiversion (abfd, 1);
5162 else if (abiversion (abfd) == 2)
5163 {
5164 info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5165 abiversion (abfd));
5166 bfd_set_error (bfd_error_bad_value);
5167 return FALSE;
5168 }
74f0fb50 5169 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 5170 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 5171 if (opd_sym_map == NULL)
b34976b6 5172 return FALSE;
74f0fb50 5173 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
5174 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5175 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 5176 }
5bd4f169
AM
5177
5178 rel_end = relocs + sec->reloc_count;
5179 for (rel = relocs; rel < rel_end; rel++)
5180 {
5181 unsigned long r_symndx;
5182 struct elf_link_hash_entry *h;
04c9666a 5183 enum elf_ppc64_reloc_type r_type;
727fc41e 5184 int tls_type;
7c8fe5c4 5185 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 5186 struct plt_entry **ifunc;
5bd4f169
AM
5187
5188 r_symndx = ELF64_R_SYM (rel->r_info);
5189 if (r_symndx < symtab_hdr->sh_info)
5190 h = NULL;
5191 else
973a3492
L
5192 {
5193 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5194 h = elf_follow_link (h);
81fbe831
AM
5195
5196 /* PR15323, ref flags aren't set for references in the same
5197 object. */
5198 h->root.non_ir_ref = 1;
1c865ab2
AM
5199
5200 if (h == htab->elf.hgot)
5201 sec->has_toc_reloc = 1;
973a3492 5202 }
5bd4f169 5203
727fc41e 5204 tls_type = 0;
e054468f 5205 ifunc = NULL;
25f23106
AM
5206 if (h != NULL)
5207 {
5208 if (h->type == STT_GNU_IFUNC)
5209 {
5210 h->needs_plt = 1;
5211 ifunc = &h->plt.plist;
5212 }
5213 }
5214 else
5215 {
5216 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5217 abfd, r_symndx);
5218 if (isym == NULL)
5219 return FALSE;
5220
5221 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5222 {
5223 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5224 rel->r_addend, PLT_IFUNC);
5225 if (ifunc == NULL)
5226 return FALSE;
5227 }
5228 }
4ce794b7 5229 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
5230 if (is_branch_reloc (r_type))
5231 {
5232 if (h != NULL && (h == tga || h == dottga))
5233 {
5234 if (rel != relocs
5235 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5236 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5237 /* We have a new-style __tls_get_addr call with a marker
5238 reloc. */
5239 ;
5240 else
5241 /* Mark this section as having an old-style call. */
5242 sec->has_tls_get_addr_call = 1;
5243 }
727fc41e 5244
e054468f 5245 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5246 if (ifunc != NULL
5247 && !update_plt_info (abfd, ifunc, rel->r_addend))
5248 return FALSE;
5249 }
727fc41e 5250
a33d1f77 5251 switch (r_type)
5bd4f169 5252 {
727fc41e
AM
5253 case R_PPC64_TLSGD:
5254 case R_PPC64_TLSLD:
5255 /* These special tls relocs tie a call to __tls_get_addr with
5256 its parameter symbol. */
5257 break;
5258
411e1bfb
AM
5259 case R_PPC64_GOT_TLSLD16:
5260 case R_PPC64_GOT_TLSLD16_LO:
5261 case R_PPC64_GOT_TLSLD16_HI:
5262 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5263 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5264 goto dogottls;
5265
5266 case R_PPC64_GOT_TLSGD16:
5267 case R_PPC64_GOT_TLSGD16_LO:
5268 case R_PPC64_GOT_TLSGD16_HI:
5269 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5270 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5271 goto dogottls;
5272
5273 case R_PPC64_GOT_TPREL16_DS:
5274 case R_PPC64_GOT_TPREL16_LO_DS:
5275 case R_PPC64_GOT_TPREL16_HI:
5276 case R_PPC64_GOT_TPREL16_HA:
1d483afe 5277 if (!info->executable)
411e1bfb
AM
5278 info->flags |= DF_STATIC_TLS;
5279 tls_type = TLS_TLS | TLS_TPREL;
5280 goto dogottls;
5281
5282 case R_PPC64_GOT_DTPREL16_DS:
5283 case R_PPC64_GOT_DTPREL16_LO_DS:
5284 case R_PPC64_GOT_DTPREL16_HI:
5285 case R_PPC64_GOT_DTPREL16_HA:
5286 tls_type = TLS_TLS | TLS_DTPREL;
5287 dogottls:
5288 sec->has_tls_reloc = 1;
5289 /* Fall thru */
5290
5bd4f169 5291 case R_PPC64_GOT16:
5bd4f169 5292 case R_PPC64_GOT16_DS:
65f38f15
AM
5293 case R_PPC64_GOT16_HA:
5294 case R_PPC64_GOT16_HI:
5295 case R_PPC64_GOT16_LO:
5bd4f169 5296 case R_PPC64_GOT16_LO_DS:
65f38f15 5297 /* This symbol requires a global offset table entry. */
4c52953f 5298 sec->has_toc_reloc = 1;
33c0ec9d
AM
5299 if (r_type == R_PPC64_GOT_TLSLD16
5300 || r_type == R_PPC64_GOT_TLSGD16
5301 || r_type == R_PPC64_GOT_TPREL16_DS
5302 || r_type == R_PPC64_GOT_DTPREL16_DS
5303 || r_type == R_PPC64_GOT16
5304 || r_type == R_PPC64_GOT16_DS)
5305 {
5306 htab->do_multi_toc = 1;
d77c8a4b 5307 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5308 }
5309
e717da7e
AM
5310 if (ppc64_elf_tdata (abfd)->got == NULL
5311 && !create_got_section (abfd, info))
b34976b6 5312 return FALSE;
5bd4f169
AM
5313
5314 if (h != NULL)
5315 {
411e1bfb
AM
5316 struct ppc_link_hash_entry *eh;
5317 struct got_entry *ent;
65f38f15 5318
411e1bfb
AM
5319 eh = (struct ppc_link_hash_entry *) h;
5320 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5321 if (ent->addend == rel->r_addend
e717da7e 5322 && ent->owner == abfd
411e1bfb
AM
5323 && ent->tls_type == tls_type)
5324 break;
5325 if (ent == NULL)
5bd4f169 5326 {
411e1bfb 5327 bfd_size_type amt = sizeof (*ent);
4ce794b7 5328 ent = bfd_alloc (abfd, amt);
411e1bfb 5329 if (ent == NULL)
b34976b6 5330 return FALSE;
411e1bfb
AM
5331 ent->next = eh->elf.got.glist;
5332 ent->addend = rel->r_addend;
e717da7e 5333 ent->owner = abfd;
411e1bfb 5334 ent->tls_type = tls_type;
927be08e 5335 ent->is_indirect = FALSE;
411e1bfb
AM
5336 ent->got.refcount = 0;
5337 eh->elf.got.glist = ent;
5bd4f169 5338 }
411e1bfb 5339 ent->got.refcount += 1;
e7b938ca 5340 eh->tls_mask |= tls_type;
5bd4f169 5341 }
411e1bfb
AM
5342 else
5343 /* This is a global offset table entry for a local symbol. */
5344 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5345 rel->r_addend, tls_type))
5346 return FALSE;
5bd4f169
AM
5347 break;
5348
5bd4f169 5349 case R_PPC64_PLT16_HA:
65f38f15
AM
5350 case R_PPC64_PLT16_HI:
5351 case R_PPC64_PLT16_LO:
5352 case R_PPC64_PLT32:
5353 case R_PPC64_PLT64:
5bd4f169 5354 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5355 actually build the entry in adjust_dynamic_symbol,
5356 because this might be a case of linking PIC code without
5357 linking in any dynamic objects, in which case we don't
5358 need to generate a procedure linkage table after all. */
5bd4f169
AM
5359 if (h == NULL)
5360 {
5361 /* It does not make sense to have a procedure linkage
3fad3c7c 5362 table entry for a local symbol. */
5bd4f169 5363 bfd_set_error (bfd_error_bad_value);
b34976b6 5364 return FALSE;
5bd4f169 5365 }
411e1bfb 5366 else
e054468f
AM
5367 {
5368 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5369 return FALSE;
5370 h->needs_plt = 1;
5371 if (h->root.root.string[0] == '.'
5372 && h->root.root.string[1] != '\0')
5373 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5374 }
5bd4f169
AM
5375 break;
5376
5377 /* The following relocations don't need to propagate the
5378 relocation if linking a shared object since they are
5379 section relative. */
5380 case R_PPC64_SECTOFF:
5381 case R_PPC64_SECTOFF_LO:
5382 case R_PPC64_SECTOFF_HI:
5383 case R_PPC64_SECTOFF_HA:
5384 case R_PPC64_SECTOFF_DS:
5385 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5386 case R_PPC64_DTPREL16:
5387 case R_PPC64_DTPREL16_LO:
5388 case R_PPC64_DTPREL16_HI:
5389 case R_PPC64_DTPREL16_HA:
5390 case R_PPC64_DTPREL16_DS:
5391 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5392 case R_PPC64_DTPREL16_HIGH:
5393 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5394 case R_PPC64_DTPREL16_HIGHER:
5395 case R_PPC64_DTPREL16_HIGHERA:
5396 case R_PPC64_DTPREL16_HIGHEST:
5397 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5398 break;
5399
ad8e1ba5 5400 /* Nor do these. */
25f23106
AM
5401 case R_PPC64_REL16:
5402 case R_PPC64_REL16_LO:
5403 case R_PPC64_REL16_HI:
5404 case R_PPC64_REL16_HA:
5405 break;
5406
ad8e1ba5 5407 case R_PPC64_TOC16:
33c0ec9d
AM
5408 case R_PPC64_TOC16_DS:
5409 htab->do_multi_toc = 1;
d77c8a4b 5410 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5411 case R_PPC64_TOC16_LO:
5412 case R_PPC64_TOC16_HI:
5413 case R_PPC64_TOC16_HA:
ad8e1ba5 5414 case R_PPC64_TOC16_LO_DS:
4c52953f 5415 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5416 break;
5417
5bd4f169
AM
5418 /* This relocation describes the C++ object vtable hierarchy.
5419 Reconstruct it for later use during GC. */
5420 case R_PPC64_GNU_VTINHERIT:
c152c796 5421 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5422 return FALSE;
5bd4f169
AM
5423 break;
5424
5425 /* This relocation describes which C++ vtable entries are actually
5426 used. Record for later use during GC. */
5427 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5428 BFD_ASSERT (h != NULL);
5429 if (h != NULL
5430 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5431 return FALSE;
5bd4f169
AM
5432 break;
5433
721956f4
AM
5434 case R_PPC64_REL14:
5435 case R_PPC64_REL14_BRTAKEN:
5436 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5437 {
5438 asection *dest = NULL;
5439
5440 /* Heuristic: If jumping outside our section, chances are
5441 we are going to need a stub. */
5442 if (h != NULL)
5443 {
5444 /* If the sym is weak it may be overridden later, so
5445 don't assume we know where a weak sym lives. */
5446 if (h->root.type == bfd_link_hash_defined)
5447 dest = h->root.u.def.section;
5448 }
5449 else
87d72d41
AM
5450 {
5451 Elf_Internal_Sym *isym;
5452
5453 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5454 abfd, r_symndx);
5455 if (isym == NULL)
5456 return FALSE;
5457
5458 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5459 }
5460
220c76dd 5461 if (dest != sec)
7c8fe5c4 5462 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5463 }
721956f4
AM
5464 /* Fall through. */
5465
5d1634d7 5466 case R_PPC64_REL24:
e054468f 5467 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5468 {
5469 /* We may need a .plt entry if the function this reloc
5470 refers to is in a shared lib. */
e054468f 5471 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5472 return FALSE;
e054468f
AM
5473 h->needs_plt = 1;
5474 if (h->root.root.string[0] == '.'
5475 && h->root.root.string[1] != '\0')
5476 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5477 if (h == tga || h == dottga)
411e1bfb 5478 sec->has_tls_reloc = 1;
411e1bfb
AM
5479 }
5480 break;
5481
5482 case R_PPC64_TPREL64:
5483 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5484 if (!info->executable)
411e1bfb
AM
5485 info->flags |= DF_STATIC_TLS;
5486 goto dotlstoc;
5487
5488 case R_PPC64_DTPMOD64:
5489 if (rel + 1 < rel_end
5490 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5491 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5492 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5493 else
951fd09b 5494 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5495 goto dotlstoc;
5496
5497 case R_PPC64_DTPREL64:
5498 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5499 if (rel != relocs
5500 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5501 && rel[-1].r_offset == rel->r_offset - 8)
5502 /* This is the second reloc of a dtpmod, dtprel pair.
5503 Don't mark with TLS_DTPREL. */
5504 goto dodyn;
5505
5506 dotlstoc:
5507 sec->has_tls_reloc = 1;
5508 if (h != NULL)
5509 {
5510 struct ppc_link_hash_entry *eh;
5511 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5512 eh->tls_mask |= tls_type;
411e1bfb
AM
5513 }
5514 else
5515 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5516 rel->r_addend, tls_type))
5517 return FALSE;
5518
7c8fe5c4
AM
5519 ppc64_sec = ppc64_elf_section_data (sec);
5520 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5521 {
3a71aa26
AM
5522 bfd_size_type amt;
5523
e7b938ca 5524 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5525 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5526 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5527 if (ppc64_sec->u.toc.symndx == NULL)
5528 return FALSE;
5529 amt = sec->size * sizeof (bfd_vma) / 8;
5530 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5531 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5532 return FALSE;
7c8fe5c4
AM
5533 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5534 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5535 }
5536 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5537 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5538 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5539
5540 /* Mark the second slot of a GD or LD entry.
5541 -1 to indicate GD and -2 to indicate LD. */
5542 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5543 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5544 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5545 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5546 goto dodyn;
5547
5548 case R_PPC64_TPREL16:
5549 case R_PPC64_TPREL16_LO:
5550 case R_PPC64_TPREL16_HI:
5551 case R_PPC64_TPREL16_HA:
5552 case R_PPC64_TPREL16_DS:
5553 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5554 case R_PPC64_TPREL16_HIGH:
5555 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5556 case R_PPC64_TPREL16_HIGHER:
5557 case R_PPC64_TPREL16_HIGHERA:
5558 case R_PPC64_TPREL16_HIGHEST:
5559 case R_PPC64_TPREL16_HIGHESTA:
5560 if (info->shared)
5561 {
1d483afe
AM
5562 if (!info->executable)
5563 info->flags |= DF_STATIC_TLS;
411e1bfb 5564 goto dodyn;
5d1634d7
AM
5565 }
5566 break;
5567
e86ce104 5568 case R_PPC64_ADDR64:
1e2f5b6e 5569 if (opd_sym_map != NULL
1e2f5b6e 5570 && rel + 1 < rel_end
4ce794b7 5571 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5572 {
8387904d
AM
5573 if (h != NULL)
5574 {
5575 if (h->root.root.string[0] == '.'
5576 && h->root.root.string[1] != 0
b31867b6 5577 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5578 ;
5579 else
5580 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5581 }
5582 else
5583 {
5584 asection *s;
87d72d41 5585 Elf_Internal_Sym *isym;
1e2f5b6e 5586
87d72d41
AM
5587 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5588 abfd, r_symndx);
5589 if (isym == NULL)
8387904d 5590 return FALSE;
87d72d41
AM
5591
5592 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5593 if (s != NULL && s != sec)
3f764659 5594 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5595 }
1e2f5b6e 5596 }
e86ce104
AM
5597 /* Fall through. */
5598
04c9666a 5599 case R_PPC64_REL30:
5bd4f169 5600 case R_PPC64_REL32:
04c9666a 5601 case R_PPC64_REL64:
65f38f15
AM
5602 case R_PPC64_ADDR14:
5603 case R_PPC64_ADDR14_BRNTAKEN:
5604 case R_PPC64_ADDR14_BRTAKEN:
5605 case R_PPC64_ADDR16:
5606 case R_PPC64_ADDR16_DS:
5607 case R_PPC64_ADDR16_HA:
5608 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5609 case R_PPC64_ADDR16_HIGH:
5610 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5611 case R_PPC64_ADDR16_HIGHER:
5612 case R_PPC64_ADDR16_HIGHERA:
5613 case R_PPC64_ADDR16_HIGHEST:
5614 case R_PPC64_ADDR16_HIGHESTA:
5615 case R_PPC64_ADDR16_LO:
5616 case R_PPC64_ADDR16_LO_DS:
5617 case R_PPC64_ADDR24:
65f38f15 5618 case R_PPC64_ADDR32:
65f38f15
AM
5619 case R_PPC64_UADDR16:
5620 case R_PPC64_UADDR32:
5621 case R_PPC64_UADDR64:
5bd4f169 5622 case R_PPC64_TOC:
81848ca0
AM
5623 if (h != NULL && !info->shared)
5624 /* We may need a copy reloc. */
f5385ebf 5625 h->non_got_ref = 1;
81848ca0 5626
41bd81ab 5627 /* Don't propagate .opd relocs. */
1e2f5b6e 5628 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5629 break;
e86ce104 5630
65f38f15
AM
5631 /* If we are creating a shared library, and this is a reloc
5632 against a global symbol, or a non PC relative reloc
5633 against a local symbol, then we need to copy the reloc
5634 into the shared library. However, if we are linking with
5635 -Bsymbolic, we do not need to copy a reloc against a
5636 global symbol which is defined in an object we are
5637 including in the link (i.e., DEF_REGULAR is set). At
5638 this point we have not seen all the input files, so it is
5639 possible that DEF_REGULAR is not set now but will be set
5640 later (it is never cleared). In case of a weak definition,
5641 DEF_REGULAR may be cleared later by a strong definition in
5642 a shared library. We account for that possibility below by
f4656909 5643 storing information in the dyn_relocs field of the hash
65f38f15
AM
5644 table entry. A similar situation occurs when creating
5645 shared libraries and symbol visibility changes render the
5646 symbol local.
5647
5648 If on the other hand, we are creating an executable, we
5649 may need to keep relocations for symbols satisfied by a
5650 dynamic library if we manage to avoid copy relocs for the
5651 symbol. */
411e1bfb 5652 dodyn:
65f38f15 5653 if ((info->shared
1d483afe 5654 && (must_be_dyn_reloc (info, r_type)
65f38f15 5655 || (h != NULL
198f1157 5656 && (!SYMBOLIC_BIND (info, h)
65f38f15 5657 || h->root.type == bfd_link_hash_defweak
f5385ebf 5658 || !h->def_regular))))
f4656909
AM
5659 || (ELIMINATE_COPY_RELOCS
5660 && !info->shared
65f38f15
AM
5661 && h != NULL
5662 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5663 || !h->def_regular))
5664 || (!info->shared
5665 && ifunc != NULL))
5bd4f169 5666 {
65f38f15
AM
5667 /* We must copy these reloc types into the output file.
5668 Create a reloc section in dynobj and make room for
5669 this reloc. */
5bd4f169
AM
5670 if (sreloc == NULL)
5671 {
83bac4b0
NC
5672 sreloc = _bfd_elf_make_dynamic_reloc_section
5673 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5674
5bd4f169 5675 if (sreloc == NULL)
83bac4b0 5676 return FALSE;
5bd4f169
AM
5677 }
5678
65f38f15
AM
5679 /* If this is a global symbol, we count the number of
5680 relocations we need for this symbol. */
5681 if (h != NULL)
5682 {
19e08130
AM
5683 struct elf_dyn_relocs *p;
5684 struct elf_dyn_relocs **head;
5685
ec338859 5686 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5687 p = *head;
5688 if (p == NULL || p->sec != sec)
5689 {
5690 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5691 if (p == NULL)
5692 return FALSE;
5693 p->next = *head;
5694 *head = p;
5695 p->sec = sec;
5696 p->count = 0;
5697 p->pc_count = 0;
5698 }
5699 p->count += 1;
5700 if (!must_be_dyn_reloc (info, r_type))
5701 p->pc_count += 1;
65f38f15
AM
5702 }
5703 else
5704 {
ec338859
AM
5705 /* Track dynamic relocs needed for local syms too.
5706 We really need local syms available to do this
5707 easily. Oh well. */
19e08130
AM
5708 struct ppc_dyn_relocs *p;
5709 struct ppc_dyn_relocs **head;
5710 bfd_boolean is_ifunc;
ec338859 5711 asection *s;
6edfbbad 5712 void *vpp;
87d72d41 5713 Elf_Internal_Sym *isym;
6edfbbad 5714
87d72d41
AM
5715 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5716 abfd, r_symndx);
5717 if (isym == NULL)
b34976b6 5718 return FALSE;
ec338859 5719
87d72d41
AM
5720 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5721 if (s == NULL)
5722 s = sec;
5723
6edfbbad 5724 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5725 head = (struct ppc_dyn_relocs **) vpp;
5726 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5727 p = *head;
5728 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5729 p = p->next;
5730 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5731 {
5732 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5733 if (p == NULL)
5734 return FALSE;
5735 p->next = *head;
5736 *head = p;
5737 p->sec = sec;
5738 p->ifunc = is_ifunc;
5739 p->count = 0;
5740 }
5741 p->count += 1;
ec338859 5742 }
65f38f15 5743 }
5bd4f169 5744 break;
65f38f15
AM
5745
5746 default:
96e0dda4 5747 break;
5bd4f169
AM
5748 }
5749 }
5750
b34976b6 5751 return TRUE;
5bd4f169
AM
5752}
5753
ee67d69a
AM
5754/* Merge backend specific data from an object file to the output
5755 object file when linking. */
5756
5757static bfd_boolean
5758ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5759{
5760 unsigned long iflags, oflags;
5761
5762 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5763 return TRUE;
5764
5765 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5766 return TRUE;
5767
5768 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5769 return FALSE;
5770
5771 iflags = elf_elfheader (ibfd)->e_flags;
5772 oflags = elf_elfheader (obfd)->e_flags;
5773
5774 if (!elf_flags_init (obfd) || oflags == 0)
5775 {
5776 elf_flags_init (obfd) = TRUE;
5777 elf_elfheader (obfd)->e_flags = iflags;
5778 }
5779 else if (iflags == oflags || iflags == 0)
5780 ;
5781 else if (iflags & ~EF_PPC64_ABI)
5782 {
5783 (*_bfd_error_handler)
5784 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5785 bfd_set_error (bfd_error_bad_value);
5786 return FALSE;
5787 }
5788 else
5789 {
5790 (*_bfd_error_handler)
5791 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5792 ibfd, iflags, oflags);
5793 bfd_set_error (bfd_error_bad_value);
5794 return FALSE;
5795 }
5796
5797 /* Merge Tag_compatibility attributes and any common GNU ones. */
5798 _bfd_elf_merge_object_attributes (ibfd, obfd);
5799
5800 return TRUE;
5801}
5802
5803static bfd_boolean
5804ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5805{
5806 /* Print normal ELF private data. */
5807 _bfd_elf_print_private_bfd_data (abfd, ptr);
5808
5809 if (elf_elfheader (abfd)->e_flags != 0)
5810 {
5811 FILE *file = ptr;
5812
5813 /* xgettext:c-format */
5814 fprintf (file, _("private flags = 0x%lx:"),
5815 elf_elfheader (abfd)->e_flags);
5816
5817 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5818 fprintf (file, _(" [abiv%ld]"),
5819 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5820 fputc ('\n', file);
5821 }
5822
5823 return TRUE;
5824}
5825
8387904d
AM
5826/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5827 of the code entry point, and its section. */
5828
5829static bfd_vma
5830opd_entry_value (asection *opd_sec,
5831 bfd_vma offset,
5832 asection **code_sec,
aef36ac1
AM
5833 bfd_vma *code_off,
5834 bfd_boolean in_code_sec)
8387904d
AM
5835{
5836 bfd *opd_bfd = opd_sec->owner;
8860955f 5837 Elf_Internal_Rela *relocs;
8387904d 5838 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5839 bfd_vma val;
8387904d 5840
9f296da3
AM
5841 /* No relocs implies we are linking a --just-symbols object, or looking
5842 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5843 if (opd_sec->reloc_count == 0)
5844 {
729eabd5 5845 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5846
729eabd5
AM
5847 if (contents == NULL)
5848 {
5849 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5850 return (bfd_vma) -1;
5851 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5852 }
ee1e4ede 5853
729eabd5 5854 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5855 if (code_sec != NULL)
5856 {
5857 asection *sec, *likely = NULL;
ee1e4ede 5858
aef36ac1 5859 if (in_code_sec)
4b85d634 5860 {
aef36ac1
AM
5861 sec = *code_sec;
5862 if (sec->vma <= val
5863 && val < sec->vma + sec->size)
5864 likely = sec;
5865 else
5866 val = -1;
5867 }
5868 else
5869 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5870 if (sec->vma <= val
5871 && (sec->flags & SEC_LOAD) != 0
5872 && (sec->flags & SEC_ALLOC) != 0)
5873 likely = sec;
5874 if (likely != NULL)
5875 {
5876 *code_sec = likely;
5877 if (code_off != NULL)
5878 *code_off = val - likely->vma;
4b85d634
AM
5879 }
5880 }
aef36ac1 5881 return val;
4b85d634
AM
5882 }
5883
0c8d6e5c 5884 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5885
729eabd5 5886 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5887 if (relocs == NULL)
5888 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5889
8387904d 5890 /* Go find the opd reloc at the sym address. */
8860955f 5891 lo = relocs;
8387904d
AM
5892 BFD_ASSERT (lo != NULL);
5893 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5894 val = (bfd_vma) -1;
8387904d
AM
5895 while (lo < hi)
5896 {
5897 look = lo + (hi - lo) / 2;
5898 if (look->r_offset < offset)
5899 lo = look + 1;
5900 else if (look->r_offset > offset)
5901 hi = look;
5902 else
5903 {
0ffa91dd
NC
5904 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5905
8387904d
AM
5906 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5907 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5908 {
5909 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5910 asection *sec;
5911
62599110
AM
5912 if (symndx < symtab_hdr->sh_info
5913 || elf_sym_hashes (opd_bfd) == NULL)
8387904d
AM
5914 {
5915 Elf_Internal_Sym *sym;
5916
5917 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5918 if (sym == NULL)
5919 {
62599110
AM
5920 size_t symcnt = symtab_hdr->sh_info;
5921 if (elf_sym_hashes (opd_bfd) == NULL)
5922 symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5923 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
8387904d
AM
5924 0, NULL, NULL, NULL);
5925 if (sym == NULL)
645ea6a9 5926 break;
8387904d
AM
5927 symtab_hdr->contents = (bfd_byte *) sym;
5928 }
5929
5930 sym += symndx;
5931 val = sym->st_value;
cb33740c 5932 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5933 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5934 }
5935 else
5936 {
5937 struct elf_link_hash_entry **sym_hashes;
5938 struct elf_link_hash_entry *rh;
5939
5940 sym_hashes = elf_sym_hashes (opd_bfd);
5941 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5942 if (rh != NULL)
5943 {
5944 rh = elf_follow_link (rh);
5945 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5946 || rh->root.type == bfd_link_hash_defweak);
5947 val = rh->root.u.def.value;
5948 sec = rh->root.u.def.section;
5949 }
5950 else
5951 {
5952 /* Handle the odd case where we can be called
5953 during bfd_elf_link_add_symbols before the
5954 symbol hashes have been fully populated. */
5955 Elf_Internal_Sym *sym;
5956
5957 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5958 symndx, NULL, NULL, NULL);
5959 if (sym == NULL)
5960 break;
5961
5962 val = sym->st_value;
5963 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5964 free (sym);
5965 }
8387904d
AM
5966 }
5967 val += look->r_addend;
5968 if (code_off != NULL)
5969 *code_off = val;
5970 if (code_sec != NULL)
aef36ac1
AM
5971 {
5972 if (in_code_sec && *code_sec != sec)
5973 return -1;
5974 else
5975 *code_sec = sec;
5976 }
8387904d
AM
5977 if (sec != NULL && sec->output_section != NULL)
5978 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5979 }
5980 break;
5981 }
5982 }
645ea6a9 5983
645ea6a9 5984 return val;
8387904d
AM
5985}
5986
aef36ac1
AM
5987/* If the ELF symbol SYM might be a function in SEC, return the
5988 function size and set *CODE_OFF to the function's entry point,
5989 otherwise return zero. */
9f296da3 5990
aef36ac1
AM
5991static bfd_size_type
5992ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5993 bfd_vma *code_off)
9f296da3 5994{
aef36ac1
AM
5995 bfd_size_type size;
5996
5997 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5998 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5999 return 0;
6000
6001 size = 0;
6002 if (!(sym->flags & BSF_SYNTHETIC))
6003 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6004
6005 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6006 {
aef36ac1
AM
6007 if (opd_entry_value (sym->section, sym->value,
6008 &sec, code_off, TRUE) == (bfd_vma) -1)
6009 return 0;
6010 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6011 symbol. This size has nothing to do with the code size of the
6012 function, which is what we're supposed to return, but the
6013 code size isn't available without looking up the dot-sym.
6014 However, doing that would be a waste of time particularly
6015 since elf_find_function will look at the dot-sym anyway.
6016 Now, elf_find_function will keep the largest size of any
6017 function sym found at the code address of interest, so return
6018 1 here to avoid it incorrectly caching a larger function size
6019 for a small function. This does mean we return the wrong
6020 size for a new-ABI function of size 24, but all that does is
6021 disable caching for such functions. */
6022 if (size == 24)
6023 size = 1;
9f296da3 6024 }
aef36ac1
AM
6025 else
6026 {
6027 if (sym->section != sec)
6028 return 0;
6029 *code_off = sym->value;
6030 }
6031 if (size == 0)
6032 size = 1;
6033 return size;
9f296da3
AM
6034}
6035
854b41e7
AM
6036/* Return true if symbol is defined in a regular object file. */
6037
6038static bfd_boolean
6039is_static_defined (struct elf_link_hash_entry *h)
6040{
6041 return ((h->root.type == bfd_link_hash_defined
6042 || h->root.type == bfd_link_hash_defweak)
6043 && h->root.u.def.section != NULL
6044 && h->root.u.def.section->output_section != NULL);
6045}
6046
b31867b6
AM
6047/* If FDH is a function descriptor symbol, return the associated code
6048 entry symbol if it is defined. Return NULL otherwise. */
6049
6050static struct ppc_link_hash_entry *
6051defined_code_entry (struct ppc_link_hash_entry *fdh)
6052{
6053 if (fdh->is_func_descriptor)
6054 {
6055 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6056 if (fh->elf.root.type == bfd_link_hash_defined
6057 || fh->elf.root.type == bfd_link_hash_defweak)
6058 return fh;
6059 }
6060 return NULL;
6061}
6062
6063/* If FH is a function code entry symbol, return the associated
6064 function descriptor symbol if it is defined. Return NULL otherwise. */
6065
6066static struct ppc_link_hash_entry *
6067defined_func_desc (struct ppc_link_hash_entry *fh)
6068{
6069 if (fh->oh != NULL
6070 && fh->oh->is_func_descriptor)
6071 {
6072 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6073 if (fdh->elf.root.type == bfd_link_hash_defined
6074 || fdh->elf.root.type == bfd_link_hash_defweak)
6075 return fdh;
6076 }
6077 return NULL;
6078}
6079
74f0fb50
AM
6080/* Mark all our entry sym sections, both opd and code section. */
6081
6082static void
6083ppc64_elf_gc_keep (struct bfd_link_info *info)
6084{
6085 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6086 struct bfd_sym_chain *sym;
6087
4dfe6ac6
NC
6088 if (htab == NULL)
6089 return;
6090
74f0fb50
AM
6091 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6092 {
b31867b6 6093 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6094 asection *sec;
6095
6096 eh = (struct ppc_link_hash_entry *)
b31867b6 6097 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6098 if (eh == NULL)
6099 continue;
6100 if (eh->elf.root.type != bfd_link_hash_defined
6101 && eh->elf.root.type != bfd_link_hash_defweak)
6102 continue;
6103
b31867b6
AM
6104 fh = defined_code_entry (eh);
6105 if (fh != NULL)
74f0fb50 6106 {
b31867b6 6107 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6108 sec->flags |= SEC_KEEP;
6109 }
6110 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6111 && opd_entry_value (eh->elf.root.u.def.section,
6112 eh->elf.root.u.def.value,
aef36ac1 6113 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6114 sec->flags |= SEC_KEEP;
6115
6116 sec = eh->elf.root.u.def.section;
6117 sec->flags |= SEC_KEEP;
6118 }
6119}
6120
64d03ab5
AM
6121/* Mark sections containing dynamically referenced symbols. When
6122 building shared libraries, we must assume that any visible symbol is
6123 referenced. */
6124
6125static bfd_boolean
6126ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6127{
6128 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6129 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6130 struct ppc_link_hash_entry *fdh;
64d03ab5 6131
64d03ab5 6132 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6133 fdh = defined_func_desc (eh);
6134 if (fdh != NULL)
6135 eh = fdh;
64d03ab5
AM
6136
6137 if ((eh->elf.root.type == bfd_link_hash_defined
6138 || eh->elf.root.type == bfd_link_hash_defweak)
6139 && (eh->elf.ref_dynamic
6140 || (!info->executable
6141 && eh->elf.def_regular
6142 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8
AM
6143 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6144 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6145 || !bfd_hide_sym_by_version (info->version_info,
6146 eh->elf.root.root.string)))))
64d03ab5
AM
6147 {
6148 asection *code_sec;
b31867b6 6149 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6150
6151 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6152
6153 /* Function descriptor syms cause the associated
6154 function code sym section to be marked. */
b31867b6
AM
6155 fh = defined_code_entry (eh);
6156 if (fh != NULL)
6157 {
6158 code_sec = fh->elf.root.u.def.section;
6159 code_sec->flags |= SEC_KEEP;
6160 }
64d03ab5
AM
6161 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6162 && opd_entry_value (eh->elf.root.u.def.section,
6163 eh->elf.root.u.def.value,
aef36ac1 6164 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6165 code_sec->flags |= SEC_KEEP;
6166 }
6167
6168 return TRUE;
6169}
6170
5bd4f169
AM
6171/* Return the section that should be marked against GC for a given
6172 relocation. */
6173
6174static asection *
4ce794b7 6175ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6176 struct bfd_link_info *info,
4ce794b7
AM
6177 Elf_Internal_Rela *rel,
6178 struct elf_link_hash_entry *h,
6179 Elf_Internal_Sym *sym)
5bd4f169 6180{
ccfa59ea
AM
6181 asection *rsec;
6182
ccfa59ea
AM
6183 /* Syms return NULL if we're marking .opd, so we avoid marking all
6184 function sections, as all functions are referenced in .opd. */
6185 rsec = NULL;
6186 if (get_opd_info (sec) != NULL)
6187 return rsec;
1e2f5b6e 6188
5bd4f169
AM
6189 if (h != NULL)
6190 {
04c9666a 6191 enum elf_ppc64_reloc_type r_type;
b31867b6 6192 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6193
4ce794b7 6194 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6195 switch (r_type)
5bd4f169
AM
6196 {
6197 case R_PPC64_GNU_VTINHERIT:
6198 case R_PPC64_GNU_VTENTRY:
6199 break;
6200
6201 default:
6202 switch (h->root.type)
6203 {
6204 case bfd_link_hash_defined:
6205 case bfd_link_hash_defweak:
ccfa59ea 6206 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6207 fdh = defined_func_desc (eh);
6208 if (fdh != NULL)
6209 eh = fdh;
1e2f5b6e
AM
6210
6211 /* Function descriptor syms cause the associated
6212 function code sym section to be marked. */
b31867b6
AM
6213 fh = defined_code_entry (eh);
6214 if (fh != NULL)
ccfa59ea
AM
6215 {
6216 /* They also mark their opd section. */
74f0fb50 6217 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6218
b31867b6 6219 rsec = fh->elf.root.u.def.section;
ccfa59ea 6220 }
8387904d
AM
6221 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6222 && opd_entry_value (eh->elf.root.u.def.section,
6223 eh->elf.root.u.def.value,
aef36ac1 6224 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6225 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6226 else
1e2f5b6e
AM
6227 rsec = h->root.u.def.section;
6228 break;
5bd4f169
AM
6229
6230 case bfd_link_hash_common:
1e2f5b6e
AM
6231 rsec = h->root.u.c.p->section;
6232 break;
5bd4f169
AM
6233
6234 default:
fb34365b 6235 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6236 }
6237 }
6238 }
6239 else
6240 {
74f0fb50 6241 struct _opd_sec_data *opd;
1e2f5b6e
AM
6242
6243 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6244 opd = get_opd_info (rsec);
6245 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6246 {
74f0fb50 6247 rsec->gc_mark = 1;
ccfa59ea 6248
74f0fb50 6249 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 6250 }
5bd4f169
AM
6251 }
6252
1e2f5b6e 6253 return rsec;
5bd4f169
AM
6254}
6255
65f38f15
AM
6256/* Update the .got, .plt. and dynamic reloc reference counts for the
6257 section being removed. */
5bd4f169 6258
b34976b6 6259static bfd_boolean
4ce794b7
AM
6260ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6261 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6262{
411e1bfb 6263 struct ppc_link_hash_table *htab;
5bd4f169
AM
6264 Elf_Internal_Shdr *symtab_hdr;
6265 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6266 struct got_entry **local_got_ents;
5bd4f169 6267 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6268
7dda2462
TG
6269 if (info->relocatable)
6270 return TRUE;
6271
680a3378
AM
6272 if ((sec->flags & SEC_ALLOC) == 0)
6273 return TRUE;
6274
ec338859
AM
6275 elf_section_data (sec)->local_dynrel = NULL;
6276
411e1bfb 6277 htab = ppc_hash_table (info);
4dfe6ac6
NC
6278 if (htab == NULL)
6279 return FALSE;
6280
0ffa91dd 6281 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6282 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6283 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6284
6285 relend = relocs + sec->reloc_count;
6286 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6287 {
6288 unsigned long r_symndx;
04c9666a 6289 enum elf_ppc64_reloc_type r_type;
58ac9f71 6290 struct elf_link_hash_entry *h = NULL;
f961d9dd 6291 unsigned char tls_type = 0;
5bd4f169 6292
a33d1f77 6293 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6294 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6295 if (r_symndx >= symtab_hdr->sh_info)
6296 {
6297 struct ppc_link_hash_entry *eh;
6061a67d
AM
6298 struct elf_dyn_relocs **pp;
6299 struct elf_dyn_relocs *p;
58ac9f71
AM
6300
6301 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6302 h = elf_follow_link (h);
58ac9f71
AM
6303 eh = (struct ppc_link_hash_entry *) h;
6304
6305 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6306 if (p->sec == sec)
6307 {
6308 /* Everything must go for SEC. */
6309 *pp = p->next;
6310 break;
6311 }
6312 }
6313
e054468f
AM
6314 if (is_branch_reloc (r_type))
6315 {
6316 struct plt_entry **ifunc = NULL;
6317 if (h != NULL)
6318 {
6319 if (h->type == STT_GNU_IFUNC)
6320 ifunc = &h->plt.plist;
6321 }
6322 else if (local_got_ents != NULL)
6323 {
6324 struct plt_entry **local_plt = (struct plt_entry **)
6325 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 6326 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
6327 (local_plt + symtab_hdr->sh_info);
6328 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6329 ifunc = local_plt + r_symndx;
6330 }
6331 if (ifunc != NULL)
6332 {
6333 struct plt_entry *ent;
6334
6335 for (ent = *ifunc; ent != NULL; ent = ent->next)
6336 if (ent->addend == rel->r_addend)
6337 break;
6338 if (ent == NULL)
6339 abort ();
6340 if (ent->plt.refcount > 0)
6341 ent->plt.refcount -= 1;
6342 continue;
6343 }
6344 }
6345
a33d1f77
AM
6346 switch (r_type)
6347 {
411e1bfb
AM
6348 case R_PPC64_GOT_TLSLD16:
6349 case R_PPC64_GOT_TLSLD16_LO:
6350 case R_PPC64_GOT_TLSLD16_HI:
6351 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6352 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6353 goto dogot;
6354
6355 case R_PPC64_GOT_TLSGD16:
6356 case R_PPC64_GOT_TLSGD16_LO:
6357 case R_PPC64_GOT_TLSGD16_HI:
6358 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6359 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6360 goto dogot;
6361
6362 case R_PPC64_GOT_TPREL16_DS:
6363 case R_PPC64_GOT_TPREL16_LO_DS:
6364 case R_PPC64_GOT_TPREL16_HI:
6365 case R_PPC64_GOT_TPREL16_HA:
6366 tls_type = TLS_TLS | TLS_TPREL;
6367 goto dogot;
6368
6369 case R_PPC64_GOT_DTPREL16_DS:
6370 case R_PPC64_GOT_DTPREL16_LO_DS:
6371 case R_PPC64_GOT_DTPREL16_HI:
6372 case R_PPC64_GOT_DTPREL16_HA:
6373 tls_type = TLS_TLS | TLS_DTPREL;
6374 goto dogot;
6375
a33d1f77
AM
6376 case R_PPC64_GOT16:
6377 case R_PPC64_GOT16_DS:
6378 case R_PPC64_GOT16_HA:
6379 case R_PPC64_GOT16_HI:
6380 case R_PPC64_GOT16_LO:
6381 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6382 dogot:
6383 {
6384 struct got_entry *ent;
6385
58ac9f71
AM
6386 if (h != NULL)
6387 ent = h->got.glist;
411e1bfb
AM
6388 else
6389 ent = local_got_ents[r_symndx];
6390
6391 for (; ent != NULL; ent = ent->next)
6392 if (ent->addend == rel->r_addend
e717da7e 6393 && ent->owner == abfd
411e1bfb
AM
6394 && ent->tls_type == tls_type)
6395 break;
6396 if (ent == NULL)
6397 abort ();
6398 if (ent->got.refcount > 0)
6399 ent->got.refcount -= 1;
6400 }
a33d1f77 6401 break;
65f38f15 6402
a33d1f77
AM
6403 case R_PPC64_PLT16_HA:
6404 case R_PPC64_PLT16_HI:
6405 case R_PPC64_PLT16_LO:
6406 case R_PPC64_PLT32:
6407 case R_PPC64_PLT64:
721956f4
AM
6408 case R_PPC64_REL14:
6409 case R_PPC64_REL14_BRNTAKEN:
6410 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6411 case R_PPC64_REL24:
58ac9f71 6412 if (h != NULL)
5d1634d7 6413 {
411e1bfb
AM
6414 struct plt_entry *ent;
6415
411e1bfb
AM
6416 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6417 if (ent->addend == rel->r_addend)
6418 break;
7c6c1722 6419 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6420 ent->plt.refcount -= 1;
5d1634d7 6421 }
e86ce104 6422 break;
5d1634d7 6423
a33d1f77
AM
6424 default:
6425 break;
6426 }
6427 }
b34976b6 6428 return TRUE;
5bd4f169
AM
6429}
6430
deb0e272
AM
6431/* The maximum size of .sfpr. */
6432#define SFPR_MAX (218*4)
6433
6434struct sfpr_def_parms
6435{
699733f6
AM
6436 const char name[12];
6437 unsigned char lo, hi;
deb0e272
AM
6438 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6439 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6440};
6441
6442/* Auto-generate _save*, _rest* functions in .sfpr. */
6443
4dfe6ac6 6444static bfd_boolean
deb0e272
AM
6445sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6446{
6447 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6448 unsigned int i;
6449 size_t len = strlen (parm->name);
6450 bfd_boolean writing = FALSE;
699733f6 6451 char sym[16];
deb0e272 6452
4dfe6ac6
NC
6453 if (htab == NULL)
6454 return FALSE;
6455
deb0e272
AM
6456 memcpy (sym, parm->name, len);
6457 sym[len + 2] = 0;
6458
6459 for (i = parm->lo; i <= parm->hi; i++)
6460 {
6461 struct elf_link_hash_entry *h;
6462
6463 sym[len + 0] = i / 10 + '0';
6464 sym[len + 1] = i % 10 + '0';
6465 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6466 if (h != NULL
f5385ebf 6467 && !h->def_regular)
deb0e272
AM
6468 {
6469 h->root.type = bfd_link_hash_defined;
6470 h->root.u.def.section = htab->sfpr;
6471 h->root.u.def.value = htab->sfpr->size;
6472 h->type = STT_FUNC;
f5385ebf 6473 h->def_regular = 1;
deb0e272
AM
6474 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6475 writing = TRUE;
6476 if (htab->sfpr->contents == NULL)
6477 {
6478 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6479 if (htab->sfpr->contents == NULL)
6480 return FALSE;
6481 }
6482 }
6483 if (writing)
6484 {
6485 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6486 if (i != parm->hi)
6487 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6488 else
6489 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6490 htab->sfpr->size = p - htab->sfpr->contents;
6491 }
6492 }
6493
6494 return TRUE;
6495}
6496
6497static bfd_byte *
6498savegpr0 (bfd *abfd, bfd_byte *p, int r)
6499{
6500 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6501 return p + 4;
6502}
6503
6504static bfd_byte *
6505savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6506{
6507 p = savegpr0 (abfd, p, r);
6508 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6509 p = p + 4;
6510 bfd_put_32 (abfd, BLR, p);
6511 return p + 4;
6512}
6513
6514static bfd_byte *
6515restgpr0 (bfd *abfd, bfd_byte *p, int r)
6516{
6517 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6518 return p + 4;
6519}
6520
6521static bfd_byte *
6522restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6523{
6524 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6525 p = p + 4;
6526 p = restgpr0 (abfd, p, r);
6527 bfd_put_32 (abfd, MTLR_R0, p);
6528 p = p + 4;
6529 if (r == 29)
6530 {
6531 p = restgpr0 (abfd, p, 30);
6532 p = restgpr0 (abfd, p, 31);
6533 }
6534 bfd_put_32 (abfd, BLR, p);
6535 return p + 4;
6536}
6537
6538static bfd_byte *
6539savegpr1 (bfd *abfd, bfd_byte *p, int r)
6540{
6541 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6542 return p + 4;
6543}
6544
6545static bfd_byte *
6546savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6547{
6548 p = savegpr1 (abfd, p, r);
6549 bfd_put_32 (abfd, BLR, p);
6550 return p + 4;
6551}
6552
6553static bfd_byte *
6554restgpr1 (bfd *abfd, bfd_byte *p, int r)
6555{
6556 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6557 return p + 4;
6558}
6559
6560static bfd_byte *
6561restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6562{
6563 p = restgpr1 (abfd, p, r);
6564 bfd_put_32 (abfd, BLR, p);
6565 return p + 4;
6566}
6567
6568static bfd_byte *
6569savefpr (bfd *abfd, bfd_byte *p, int r)
6570{
6571 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6572 return p + 4;
6573}
6574
6575static bfd_byte *
6576savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6577{
6578 p = savefpr (abfd, p, r);
6579 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6580 p = p + 4;
6581 bfd_put_32 (abfd, BLR, p);
6582 return p + 4;
6583}
6584
6585static bfd_byte *
6586restfpr (bfd *abfd, bfd_byte *p, int r)
6587{
6588 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6589 return p + 4;
6590}
6591
6592static bfd_byte *
6593restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6594{
6595 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6596 p = p + 4;
6597 p = restfpr (abfd, p, r);
6598 bfd_put_32 (abfd, MTLR_R0, p);
6599 p = p + 4;
6600 if (r == 29)
6601 {
6602 p = restfpr (abfd, p, 30);
6603 p = restfpr (abfd, p, 31);
6604 }
6605 bfd_put_32 (abfd, BLR, p);
6606 return p + 4;
6607}
6608
6609static bfd_byte *
6610savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6611{
6612 p = savefpr (abfd, p, r);
6613 bfd_put_32 (abfd, BLR, p);
6614 return p + 4;
6615}
6616
6617static bfd_byte *
6618restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6619{
6620 p = restfpr (abfd, p, r);
6621 bfd_put_32 (abfd, BLR, p);
6622 return p + 4;
6623}
6624
6625static bfd_byte *
6626savevr (bfd *abfd, bfd_byte *p, int r)
6627{
6628 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6629 p = p + 4;
6630 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6631 return p + 4;
6632}
6633
6634static bfd_byte *
6635savevr_tail (bfd *abfd, bfd_byte *p, int r)
6636{
6637 p = savevr (abfd, p, r);
6638 bfd_put_32 (abfd, BLR, p);
6639 return p + 4;
6640}
6641
6642static bfd_byte *
6643restvr (bfd *abfd, bfd_byte *p, int r)
6644{
6645 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6646 p = p + 4;
6647 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6648 return p + 4;
6649}
6650
6651static bfd_byte *
6652restvr_tail (bfd *abfd, bfd_byte *p, int r)
6653{
6654 p = restvr (abfd, p, r);
6655 bfd_put_32 (abfd, BLR, p);
6656 return p + 4;
6657}
6658
e86ce104
AM
6659/* Called via elf_link_hash_traverse to transfer dynamic linking
6660 information on function code symbol entries to their corresponding
6661 function descriptor symbol entries. */
deb0e272 6662
b34976b6 6663static bfd_boolean
4ce794b7 6664func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6665{
e86ce104 6666 struct bfd_link_info *info;
65f38f15 6667 struct ppc_link_hash_table *htab;
411e1bfb 6668 struct plt_entry *ent;
50bc7936
AM
6669 struct ppc_link_hash_entry *fh;
6670 struct ppc_link_hash_entry *fdh;
6671 bfd_boolean force_local;
5bd4f169 6672
50bc7936
AM
6673 fh = (struct ppc_link_hash_entry *) h;
6674 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6675 return TRUE;
e86ce104 6676
4ce794b7 6677 info = inf;
65f38f15 6678 htab = ppc_hash_table (info);
4dfe6ac6
NC
6679 if (htab == NULL)
6680 return FALSE;
5bd4f169 6681
c09bdfe5
AM
6682 /* Resolve undefined references to dot-symbols as the value
6683 in the function descriptor, if we have one in a regular object.
6684 This is to satisfy cases like ".quad .foo". Calls to functions
6685 in dynamic objects are handled elsewhere. */
6686 if (fh->elf.root.type == bfd_link_hash_undefweak
6687 && fh->was_undefined
b31867b6
AM
6688 && (fdh = defined_func_desc (fh)) != NULL
6689 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6690 && opd_entry_value (fdh->elf.root.u.def.section,
6691 fdh->elf.root.u.def.value,
c09bdfe5 6692 &fh->elf.root.u.def.section,
aef36ac1 6693 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6694 {
b31867b6 6695 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6696 fh->elf.forced_local = 1;
b31867b6
AM
6697 fh->elf.def_regular = fdh->elf.def_regular;
6698 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6699 }
6700
e86ce104
AM
6701 /* If this is a function code symbol, transfer dynamic linking
6702 information to the function descriptor symbol. */
50bc7936 6703 if (!fh->is_func)
b34976b6 6704 return TRUE;
e86ce104 6705
50bc7936 6706 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6707 if (ent->plt.refcount > 0)
6708 break;
50bc7936
AM
6709 if (ent == NULL
6710 || fh->elf.root.root.string[0] != '.'
6711 || fh->elf.root.root.string[1] == '\0')
6712 return TRUE;
5bd4f169 6713
50bc7936
AM
6714 /* Find the corresponding function descriptor symbol. Create it
6715 as undefined if necessary. */
5bd4f169 6716
b31867b6 6717 fdh = lookup_fdh (fh, htab);
50bc7936 6718 if (fdh == NULL
df131623 6719 && !info->executable
50bc7936
AM
6720 && (fh->elf.root.type == bfd_link_hash_undefined
6721 || fh->elf.root.type == bfd_link_hash_undefweak))
6722 {
908b32fc 6723 fdh = make_fdh (info, fh);
bb700d78
AM
6724 if (fdh == NULL)
6725 return FALSE;
50bc7936 6726 }
648cca2c 6727
908b32fc 6728 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6729 code symbol is strong undefined, make the fake sym the same.
6730 If the function code symbol is defined, then force the fake
6731 descriptor local; We can't support overriding of symbols in a
6732 shared library on a fake descriptor. */
908b32fc
AM
6733
6734 if (fdh != NULL
6735 && fdh->fake
433817dd 6736 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6737 {
433817dd
AM
6738 if (fh->elf.root.type == bfd_link_hash_undefined)
6739 {
6740 fdh->elf.root.type = bfd_link_hash_undefined;
6741 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6742 }
6743 else if (fh->elf.root.type == bfd_link_hash_defined
6744 || fh->elf.root.type == bfd_link_hash_defweak)
6745 {
6746 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6747 }
908b32fc
AM
6748 }
6749
50bc7936 6750 if (fdh != NULL
f5385ebf 6751 && !fdh->elf.forced_local
df131623 6752 && (!info->executable
f5385ebf
AM
6753 || fdh->elf.def_dynamic
6754 || fdh->elf.ref_dynamic
50bc7936
AM
6755 || (fdh->elf.root.type == bfd_link_hash_undefweak
6756 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6757 {
6758 if (fdh->elf.dynindx == -1)
c152c796 6759 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6760 return FALSE;
f5385ebf
AM
6761 fdh->elf.ref_regular |= fh->elf.ref_regular;
6762 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6763 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6764 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6765 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6766 {
40d16e0b 6767 move_plt_plist (fh, fdh);
f5385ebf 6768 fdh->elf.needs_plt = 1;
e86ce104 6769 }
50bc7936 6770 fdh->is_func_descriptor = 1;
34814b9f
AM
6771 fdh->oh = fh;
6772 fh->oh = fdh;
e86ce104
AM
6773 }
6774
50bc7936
AM
6775 /* Now that the info is on the function descriptor, clear the
6776 function code sym info. Any function code syms for which we
6777 don't have a definition in a regular file, we force local.
6778 This prevents a shared library from exporting syms that have
6779 been imported from another library. Function code syms that
6780 are really in the library we must leave global to prevent the
6781 linker dragging in a definition from a static library. */
93f3fa99
AM
6782 force_local = (!fh->elf.def_regular
6783 || fdh == NULL
6784 || !fdh->elf.def_regular
6785 || fdh->elf.forced_local);
50bc7936
AM
6786 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6787
b34976b6 6788 return TRUE;
e86ce104 6789}
40b8271b 6790
e86ce104 6791/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6792 this hook to a) provide some gcc support functions, and b) transfer
6793 dynamic linking information gathered so far on function code symbol
6794 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6795
b34976b6 6796static bfd_boolean
4ce794b7
AM
6797ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6798 struct bfd_link_info *info)
e86ce104
AM
6799{
6800 struct ppc_link_hash_table *htab;
82bd7b59 6801 unsigned int i;
27fc25a1 6802 static const struct sfpr_def_parms funcs[] =
deb0e272
AM
6803 {
6804 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6805 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6806 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6807 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6808 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6809 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6810 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6811 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6812 { "._savef", 14, 31, savefpr, savefpr1_tail },
6813 { "._restf", 14, 31, restfpr, restfpr1_tail },
6814 { "_savevr_", 20, 31, savevr, savevr_tail },
6815 { "_restvr_", 20, 31, restvr, restvr_tail }
6816 };
e86ce104
AM
6817
6818 htab = ppc_hash_table (info);
4dfe6ac6
NC
6819 if (htab == NULL)
6820 return FALSE;
6821
5295321c
AM
6822 if (!info->relocatable
6823 && htab->elf.hgot != NULL)
6824 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
c66bb0ee 6825
82bd7b59
AM
6826 if (htab->sfpr == NULL)
6827 /* We don't have any relocs. */
b34976b6 6828 return TRUE;
82bd7b59 6829
deb0e272
AM
6830 /* Provide any missing _save* and _rest* functions. */
6831 htab->sfpr->size = 0;
27fc25a1
AM
6832 if (!info->relocatable)
6833 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6834 if (!sfpr_define (info, &funcs[i]))
6835 return FALSE;
82bd7b59 6836
4ce794b7 6837 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6838
eea6121a 6839 if (htab->sfpr->size == 0)
8423293d 6840 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6841
b34976b6 6842 return TRUE;
e86ce104
AM
6843}
6844
6845/* Adjust a symbol defined by a dynamic object and referenced by a
6846 regular object. The current definition is in some section of the
6847 dynamic object, but we're not including those sections. We have to
6848 change the definition to something the rest of the link can
6849 understand. */
6850
b34976b6 6851static bfd_boolean
4ce794b7
AM
6852ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6853 struct elf_link_hash_entry *h)
e86ce104
AM
6854{
6855 struct ppc_link_hash_table *htab;
e86ce104 6856 asection *s;
e86ce104
AM
6857
6858 htab = ppc_hash_table (info);
4dfe6ac6
NC
6859 if (htab == NULL)
6860 return FALSE;
e86ce104
AM
6861
6862 /* Deal with function syms. */
6863 if (h->type == STT_FUNC
e054468f 6864 || h->type == STT_GNU_IFUNC
f5385ebf 6865 || h->needs_plt)
e86ce104
AM
6866 {
6867 /* Clear procedure linkage table information for any symbol that
6868 won't need a .plt entry. */
411e1bfb
AM
6869 struct plt_entry *ent;
6870 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6871 if (ent->plt.refcount > 0)
6872 break;
8387904d 6873 if (ent == NULL
e054468f
AM
6874 || (h->type != STT_GNU_IFUNC
6875 && (SYMBOL_CALLS_LOCAL (info, h)
6876 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6877 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6878 {
411e1bfb 6879 h->plt.plist = NULL;
f5385ebf 6880 h->needs_plt = 0;
40b8271b 6881 }
5bd4f169 6882 }
bbd7ec4a 6883 else
411e1bfb 6884 h->plt.plist = NULL;
5bd4f169
AM
6885
6886 /* If this is a weak symbol, and there is a real definition, the
6887 processor independent code will have arranged for us to see the
6888 real definition first, and we can just use the same value. */
f6e332e6 6889 if (h->u.weakdef != NULL)
5bd4f169 6890 {
f6e332e6
AM
6891 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6892 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6893 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6894 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6895 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6896 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6897 return TRUE;
5bd4f169
AM
6898 }
6899
5bd4f169
AM
6900 /* If we are creating a shared library, we must presume that the
6901 only references to the symbol are via the global offset table.
6902 For such cases we need not do anything here; the relocations will
6903 be handled correctly by relocate_section. */
6904 if (info->shared)
b34976b6 6905 return TRUE;
5bd4f169 6906
65f38f15
AM
6907 /* If there are no references to this symbol that do not use the
6908 GOT, we don't need to generate a copy reloc. */
f5385ebf 6909 if (!h->non_got_ref)
b34976b6 6910 return TRUE;
65f38f15 6911
b186458a
JJ
6912 /* Don't generate a copy reloc for symbols defined in the executable. */
6913 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6914 return TRUE;
6915
f4656909 6916 if (ELIMINATE_COPY_RELOCS)
65f38f15 6917 {
f4656909 6918 struct ppc_link_hash_entry * eh;
6061a67d 6919 struct elf_dyn_relocs *p;
65f38f15 6920
f4656909
AM
6921 eh = (struct ppc_link_hash_entry *) h;
6922 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6923 {
6924 s = p->sec->output_section;
6925 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6926 break;
6927 }
6928
6929 /* If we didn't find any dynamic relocs in read-only sections, then
6930 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6931 if (p == NULL)
6932 {
f5385ebf 6933 h->non_got_ref = 0;
f4656909
AM
6934 return TRUE;
6935 }
65f38f15
AM
6936 }
6937
5d35169e 6938 if (h->plt.plist != NULL)
97b639ba
AM
6939 {
6940 /* We should never get here, but unfortunately there are versions
6941 of gcc out there that improperly (for this ABI) put initialized
6942 function pointers, vtable refs and suchlike in read-only
6943 sections. Allow them to proceed, but warn that this might
6944 break at runtime. */
25f53a85 6945 info->callbacks->einfo
bc30df16 6946 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 6947 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6948 h->root.root.string);
6949 }
5d35169e
AM
6950
6951 /* This is a reference to a symbol defined by a dynamic object which
6952 is not a function. */
6953
5bd4f169
AM
6954 /* We must allocate the symbol in our .dynbss section, which will
6955 become part of the .bss section of the executable. There will be
6956 an entry for this symbol in the .dynsym section. The dynamic
6957 object will contain position independent code, so all references
6958 from the dynamic object to this symbol will go through the global
6959 offset table. The dynamic linker will use the .dynsym entry to
6960 determine the address it must put in the global offset table, so
6961 both the dynamic object and the regular object will refer to the
6962 same memory location for the variable. */
5bd4f169 6963
04c9666a
AM
6964 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6965 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6966 runtime process image. We need to remember the offset into the
6967 .rela.bss section we are going to use. */
1d7e9d18 6968 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6969 {
eea6121a 6970 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6971 h->needs_copy = 1;
5bd4f169
AM
6972 }
6973
4ce794b7 6974 s = htab->dynbss;
5bd4f169 6975
027297b7 6976 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6977}
6978
e86ce104
AM
6979/* If given a function descriptor symbol, hide both the function code
6980 sym and the descriptor. */
6981static void
4ce794b7
AM
6982ppc64_elf_hide_symbol (struct bfd_link_info *info,
6983 struct elf_link_hash_entry *h,
6984 bfd_boolean force_local)
e86ce104 6985{
34814b9f 6986 struct ppc_link_hash_entry *eh;
e86ce104
AM
6987 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6988
34814b9f
AM
6989 eh = (struct ppc_link_hash_entry *) h;
6990 if (eh->is_func_descriptor)
e86ce104 6991 {
34814b9f 6992 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6993
721956f4 6994 if (fh == NULL)
d1329ca3
AM
6995 {
6996 const char *p, *q;
6997 struct ppc_link_hash_table *htab;
6998 char save;
6999
7000 /* We aren't supposed to use alloca in BFD because on
7001 systems which do not have alloca the version in libiberty
7002 calls xmalloc, which might cause the program to crash
7003 when it runs out of memory. This function doesn't have a
7004 return status, so there's no way to gracefully return an
7005 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7006 accessed; It's either a string in an ELF string table,
7007 or allocated in an objalloc structure. */
d1329ca3 7008
34814b9f 7009 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7010 save = *p;
7011 *(char *) p = '.';
7012 htab = ppc_hash_table (info);
4dfe6ac6
NC
7013 if (htab == NULL)
7014 return;
7015
34814b9f
AM
7016 fh = (struct ppc_link_hash_entry *)
7017 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7018 *(char *) p = save;
7019
7020 /* Unfortunately, if it so happens that the string we were
7021 looking for was allocated immediately before this string,
7022 then we overwrote the string terminator. That's the only
7023 reason the lookup should fail. */
7024 if (fh == NULL)
7025 {
34814b9f
AM
7026 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7027 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7028 --q, --p;
34814b9f
AM
7029 if (q < eh->elf.root.root.string && *p == '.')
7030 fh = (struct ppc_link_hash_entry *)
7031 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7032 }
7033 if (fh != NULL)
7034 {
34814b9f
AM
7035 eh->oh = fh;
7036 fh->oh = eh;
d1329ca3
AM
7037 }
7038 }
e86ce104 7039 if (fh != NULL)
34814b9f 7040 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7041 }
7042}
7043
411e1bfb 7044static bfd_boolean
8843416a
AM
7045get_sym_h (struct elf_link_hash_entry **hp,
7046 Elf_Internal_Sym **symp,
7047 asection **symsecp,
f961d9dd 7048 unsigned char **tls_maskp,
8843416a
AM
7049 Elf_Internal_Sym **locsymsp,
7050 unsigned long r_symndx,
7051 bfd *ibfd)
411e1bfb 7052{
0ffa91dd 7053 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7054
7055 if (r_symndx >= symtab_hdr->sh_info)
7056 {
7057 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7058 struct elf_link_hash_entry *h;
7059
7060 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7061 h = elf_follow_link (h);
411e1bfb
AM
7062
7063 if (hp != NULL)
7064 *hp = h;
7065
7066 if (symp != NULL)
7067 *symp = NULL;
7068
7069 if (symsecp != NULL)
7070 {
7071 asection *symsec = NULL;
7072 if (h->root.type == bfd_link_hash_defined
7073 || h->root.type == bfd_link_hash_defweak)
7074 symsec = h->root.u.def.section;
7075 *symsecp = symsec;
7076 }
7077
e7b938ca 7078 if (tls_maskp != NULL)
411e1bfb
AM
7079 {
7080 struct ppc_link_hash_entry *eh;
7081
7082 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7083 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7084 }
7085 }
7086 else
7087 {
7088 Elf_Internal_Sym *sym;
7089 Elf_Internal_Sym *locsyms = *locsymsp;
7090
7091 if (locsyms == NULL)
7092 {
7093 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7094 if (locsyms == NULL)
7095 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7096 symtab_hdr->sh_info,
7097 0, NULL, NULL, NULL);
7098 if (locsyms == NULL)
7099 return FALSE;
7100 *locsymsp = locsyms;
7101 }
7102 sym = locsyms + r_symndx;
7103
7104 if (hp != NULL)
7105 *hp = NULL;
7106
7107 if (symp != NULL)
7108 *symp = sym;
7109
7110 if (symsecp != NULL)
cb33740c 7111 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7112
e7b938ca 7113 if (tls_maskp != NULL)
411e1bfb
AM
7114 {
7115 struct got_entry **lgot_ents;
f961d9dd 7116 unsigned char *tls_mask;
411e1bfb 7117
e7b938ca 7118 tls_mask = NULL;
411e1bfb
AM
7119 lgot_ents = elf_local_got_ents (ibfd);
7120 if (lgot_ents != NULL)
7121 {
e054468f
AM
7122 struct plt_entry **local_plt = (struct plt_entry **)
7123 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7124 unsigned char *lgot_masks = (unsigned char *)
e054468f 7125 (local_plt + symtab_hdr->sh_info);
e7b938ca 7126 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7127 }
e7b938ca 7128 *tls_maskp = tls_mask;
411e1bfb
AM
7129 }
7130 }
7131 return TRUE;
7132}
7133
e7b938ca 7134/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7135 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7136 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7137
7138static int
f961d9dd 7139get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7140 unsigned long *toc_symndx,
7141 bfd_vma *toc_addend,
0d4792f7 7142 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7143 const Elf_Internal_Rela *rel,
7144 bfd *ibfd)
411e1bfb
AM
7145{
7146 unsigned long r_symndx;
0d4792f7 7147 int next_r;
411e1bfb
AM
7148 struct elf_link_hash_entry *h;
7149 Elf_Internal_Sym *sym;
7150 asection *sec;
7151 bfd_vma off;
7152
7153 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7154 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7155 return 0;
411e1bfb 7156
e7b938ca 7157 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7158 || sec == NULL
6bee8834 7159 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7160 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7161 return 1;
411e1bfb
AM
7162
7163 /* Look inside a TOC section too. */
7164 if (h != NULL)
7165 {
7166 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7167 off = h->root.u.def.value;
7168 }
7169 else
7170 off = sym->st_value;
7171 off += rel->r_addend;
7172 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7173 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7174 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7175 if (toc_symndx != NULL)
7176 *toc_symndx = r_symndx;
3a71aa26
AM
7177 if (toc_addend != NULL)
7178 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7179 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7180 return 0;
854b41e7 7181 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7182 && (next_r == -1 || next_r == -2))
7183 return 1 - next_r;
951fd09b 7184 return 1;
411e1bfb
AM
7185}
7186
3b421ab3
AM
7187/* Find (or create) an entry in the tocsave hash table. */
7188
7189static struct tocsave_entry *
7190tocsave_find (struct ppc_link_hash_table *htab,
7191 enum insert_option insert,
7192 Elf_Internal_Sym **local_syms,
7193 const Elf_Internal_Rela *irela,
7194 bfd *ibfd)
7195{
7196 unsigned long r_indx;
7197 struct elf_link_hash_entry *h;
7198 Elf_Internal_Sym *sym;
7199 struct tocsave_entry ent, *p;
7200 hashval_t hash;
7201 struct tocsave_entry **slot;
7202
7203 r_indx = ELF64_R_SYM (irela->r_info);
7204 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7205 return NULL;
7206 if (ent.sec == NULL || ent.sec->output_section == NULL)
7207 {
7208 (*_bfd_error_handler)
7209 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7210 return NULL;
7211 }
7212
7213 if (h != NULL)
7214 ent.offset = h->root.u.def.value;
7215 else
7216 ent.offset = sym->st_value;
7217 ent.offset += irela->r_addend;
7218
7219 hash = tocsave_htab_hash (&ent);
7220 slot = ((struct tocsave_entry **)
7221 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7222 if (slot == NULL)
7223 return NULL;
7224
7225 if (*slot == NULL)
7226 {
7227 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7228 if (p == NULL)
7229 return NULL;
7230 *p = ent;
7231 *slot = p;
7232 }
7233 return *slot;
7234}
7235
754021d0 7236/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7237 code for the old ABI, these will already have been done. */
754021d0
AM
7238
7239static bfd_boolean
7240adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7241{
7242 struct ppc_link_hash_entry *eh;
7243 asection *sym_sec;
74f0fb50 7244 struct _opd_sec_data *opd;
754021d0
AM
7245
7246 if (h->root.type == bfd_link_hash_indirect)
7247 return TRUE;
7248
754021d0
AM
7249 if (h->root.type != bfd_link_hash_defined
7250 && h->root.type != bfd_link_hash_defweak)
7251 return TRUE;
7252
7253 eh = (struct ppc_link_hash_entry *) h;
7254 if (eh->adjust_done)
7255 return TRUE;
7256
7257 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7258 opd = get_opd_info (sym_sec);
7259 if (opd != NULL && opd->adjust != NULL)
754021d0 7260 {
74f0fb50 7261 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
7262 if (adjust == -1)
7263 {
7264 /* This entry has been deleted. */
b3fac117 7265 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7266 if (dsec == NULL)
7267 {
7268 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7269 if (discarded_section (dsec))
81688140 7270 {
b3fac117 7271 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7272 break;
7273 }
7274 }
4025353c 7275 eh->elf.root.u.def.value = 0;
81688140 7276 eh->elf.root.u.def.section = dsec;
4025353c
AM
7277 }
7278 else
7279 eh->elf.root.u.def.value += adjust;
754021d0
AM
7280 eh->adjust_done = 1;
7281 }
7282 return TRUE;
7283}
7284
8c1d1bb8 7285/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7286 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7287 have already been determined. */
7288
7289static bfd_boolean
7290dec_dynrel_count (bfd_vma r_info,
7291 asection *sec,
7292 struct bfd_link_info *info,
7293 Elf_Internal_Sym **local_syms,
7294 struct elf_link_hash_entry *h,
19e08130 7295 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7296{
7297 enum elf_ppc64_reloc_type r_type;
19e08130 7298 asection *sym_sec = NULL;
8c1d1bb8
AM
7299
7300 /* Can this reloc be dynamic? This switch, and later tests here
7301 should be kept in sync with the code in check_relocs. */
7302 r_type = ELF64_R_TYPE (r_info);
7303 switch (r_type)
7304 {
7305 default:
7306 return TRUE;
7307
7308 case R_PPC64_TPREL16:
7309 case R_PPC64_TPREL16_LO:
7310 case R_PPC64_TPREL16_HI:
7311 case R_PPC64_TPREL16_HA:
7312 case R_PPC64_TPREL16_DS:
7313 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7314 case R_PPC64_TPREL16_HIGH:
7315 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7316 case R_PPC64_TPREL16_HIGHER:
7317 case R_PPC64_TPREL16_HIGHERA:
7318 case R_PPC64_TPREL16_HIGHEST:
7319 case R_PPC64_TPREL16_HIGHESTA:
7320 if (!info->shared)
7321 return TRUE;
7322
7323 case R_PPC64_TPREL64:
7324 case R_PPC64_DTPMOD64:
7325 case R_PPC64_DTPREL64:
7326 case R_PPC64_ADDR64:
7327 case R_PPC64_REL30:
7328 case R_PPC64_REL32:
7329 case R_PPC64_REL64:
7330 case R_PPC64_ADDR14:
7331 case R_PPC64_ADDR14_BRNTAKEN:
7332 case R_PPC64_ADDR14_BRTAKEN:
7333 case R_PPC64_ADDR16:
7334 case R_PPC64_ADDR16_DS:
7335 case R_PPC64_ADDR16_HA:
7336 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7337 case R_PPC64_ADDR16_HIGH:
7338 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7339 case R_PPC64_ADDR16_HIGHER:
7340 case R_PPC64_ADDR16_HIGHERA:
7341 case R_PPC64_ADDR16_HIGHEST:
7342 case R_PPC64_ADDR16_HIGHESTA:
7343 case R_PPC64_ADDR16_LO:
7344 case R_PPC64_ADDR16_LO_DS:
7345 case R_PPC64_ADDR24:
7346 case R_PPC64_ADDR32:
7347 case R_PPC64_UADDR16:
7348 case R_PPC64_UADDR32:
7349 case R_PPC64_UADDR64:
7350 case R_PPC64_TOC:
7351 break;
7352 }
7353
7354 if (local_syms != NULL)
7355 {
7356 unsigned long r_symndx;
8c1d1bb8
AM
7357 bfd *ibfd = sec->owner;
7358
7359 r_symndx = ELF64_R_SYM (r_info);
7360 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7361 return FALSE;
7362 }
7363
7364 if ((info->shared
1d483afe 7365 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7366 || (h != NULL
198f1157 7367 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7368 || h->root.type == bfd_link_hash_defweak
7369 || !h->def_regular))))
7370 || (ELIMINATE_COPY_RELOCS
7371 && !info->shared
7372 && h != NULL
7373 && (h->root.type == bfd_link_hash_defweak
7374 || !h->def_regular)))
7375 ;
7376 else
7377 return TRUE;
7378
7379 if (h != NULL)
6edfbbad 7380 {
19e08130
AM
7381 struct elf_dyn_relocs *p;
7382 struct elf_dyn_relocs **pp;
7383 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7384
7385 /* elf_gc_sweep may have already removed all dyn relocs associated
7386 with local syms for a given section. Also, symbol flags are
7387 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7388 report a dynreloc miscount. */
7389 if (*pp == NULL && info->gc_sections)
7390 return TRUE;
7391
7392 while ((p = *pp) != NULL)
60124e18 7393 {
19e08130
AM
7394 if (p->sec == sec)
7395 {
7396 if (!must_be_dyn_reloc (info, r_type))
7397 p->pc_count -= 1;
7398 p->count -= 1;
7399 if (p->count == 0)
7400 *pp = p->next;
7401 return TRUE;
7402 }
7403 pp = &p->next;
60124e18 7404 }
6edfbbad 7405 }
19e08130
AM
7406 else
7407 {
7408 struct ppc_dyn_relocs *p;
7409 struct ppc_dyn_relocs **pp;
7410 void *vpp;
7411 bfd_boolean is_ifunc;
8c1d1bb8 7412
19e08130
AM
7413 if (local_syms == NULL)
7414 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7415 if (sym_sec == NULL)
7416 sym_sec = sec;
c57da1a7 7417
19e08130
AM
7418 vpp = &elf_section_data (sym_sec)->local_dynrel;
7419 pp = (struct ppc_dyn_relocs **) vpp;
7420
7421 if (*pp == NULL && info->gc_sections)
7422 return TRUE;
7423
7424 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7425 while ((p = *pp) != NULL)
8c1d1bb8 7426 {
19e08130
AM
7427 if (p->sec == sec && p->ifunc == is_ifunc)
7428 {
7429 p->count -= 1;
7430 if (p->count == 0)
7431 *pp = p->next;
7432 return TRUE;
7433 }
7434 pp = &p->next;
8c1d1bb8 7435 }
8c1d1bb8
AM
7436 }
7437
8de848d8 7438 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7439 sec->owner, sec);
8c1d1bb8
AM
7440 bfd_set_error (bfd_error_bad_value);
7441 return FALSE;
7442}
7443
754021d0
AM
7444/* Remove unused Official Procedure Descriptor entries. Currently we
7445 only remove those associated with functions in discarded link-once
7446 sections, or weakly defined functions that have been overridden. It
7447 would be possible to remove many more entries for statically linked
7448 applications. */
7449
b34976b6 7450bfd_boolean
33c0ec9d 7451ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
7452{
7453 bfd *ibfd;
754021d0 7454 bfd_boolean some_edited = FALSE;
3f764659 7455 asection *need_pad = NULL;
1e2f5b6e 7456
411e1bfb 7457 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
7458 {
7459 asection *sec;
7460 Elf_Internal_Rela *relstart, *rel, *relend;
7461 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7462 Elf_Internal_Sym *local_syms;
1e2f5b6e 7463 bfd_vma offset;
74f0fb50 7464 struct _opd_sec_data *opd;
3f764659
JJ
7465 bfd_boolean need_edit, add_aux_fields;
7466 bfd_size_type cnt_16b = 0;
1e2f5b6e 7467
854b41e7
AM
7468 if (!is_ppc64_elf (ibfd))
7469 continue;
7470
1e2f5b6e 7471 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7472 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7473 continue;
7474
dbaa2011 7475 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7476 continue;
7477
1e2f5b6e
AM
7478 if (sec->output_section == bfd_abs_section_ptr)
7479 continue;
7480
7481 /* Look through the section relocs. */
7482 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7483 continue;
7484
6cdc0ccc 7485 local_syms = NULL;
0ffa91dd 7486 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7487
7488 /* Read the relocations. */
4ce794b7 7489 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7490 info->keep_memory);
1e2f5b6e 7491 if (relstart == NULL)
b34976b6 7492 return FALSE;
1e2f5b6e
AM
7493
7494 /* First run through the relocs to check they are sane, and to
7495 determine whether we need to edit this opd section. */
b34976b6 7496 need_edit = FALSE;
3f764659 7497 need_pad = sec;
1e2f5b6e
AM
7498 offset = 0;
7499 relend = relstart + sec->reloc_count;
50bc7936 7500 for (rel = relstart; rel < relend; )
1e2f5b6e 7501 {
04c9666a 7502 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7503 unsigned long r_symndx;
7504 asection *sym_sec;
7505 struct elf_link_hash_entry *h;
7506 Elf_Internal_Sym *sym;
7507
3f764659 7508 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7509 only interested in the reloc pointing to a function entry
7510 point. */
50bc7936
AM
7511 if (rel->r_offset != offset
7512 || rel + 1 >= relend
7513 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
7514 {
7515 /* If someone messes with .opd alignment then after a
7516 "ld -r" we might have padding in the middle of .opd.
7517 Also, there's nothing to prevent someone putting
7518 something silly in .opd with the assembler. No .opd
b34976b6 7519 optimization for them! */
3f764659 7520 broken_opd:
1e2f5b6e 7521 (*_bfd_error_handler)
d003868e 7522 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 7523 need_edit = FALSE;
1e2f5b6e
AM
7524 break;
7525 }
7526
50bc7936
AM
7527 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7528 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7529 {
7530 (*_bfd_error_handler)
d003868e
AM
7531 (_("%B: unexpected reloc type %u in .opd section"),
7532 ibfd, r_type);
50bc7936
AM
7533 need_edit = FALSE;
7534 break;
7535 }
7536
1e2f5b6e 7537 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7538 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7539 r_symndx, ibfd))
50bc7936 7540 goto error_ret;
1e2f5b6e
AM
7541
7542 if (sym_sec == NULL || sym_sec->owner == NULL)
7543 {
411e1bfb
AM
7544 const char *sym_name;
7545 if (h != NULL)
7546 sym_name = h->root.root.string;
7547 else
26c61ae5
L
7548 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7549 sym_sec);
411e1bfb 7550
1e2f5b6e 7551 (*_bfd_error_handler)
d003868e
AM
7552 (_("%B: undefined sym `%s' in .opd section"),
7553 ibfd, sym_name);
b34976b6 7554 need_edit = FALSE;
1e2f5b6e
AM
7555 break;
7556 }
7557
51020317
AM
7558 /* opd entries are always for functions defined in the
7559 current input bfd. If the symbol isn't defined in the
7560 input bfd, then we won't be using the function in this
7561 bfd; It must be defined in a linkonce section in another
7562 bfd, or is weak. It's also possible that we are
7563 discarding the function due to a linker script /DISCARD/,
7564 which we test for via the output_section. */
7565 if (sym_sec->owner != ibfd
7566 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7567 need_edit = TRUE;
1e2f5b6e 7568
50bc7936 7569 rel += 2;
3f764659
JJ
7570 if (rel == relend
7571 || (rel + 1 == relend && rel->r_offset == offset + 16))
7572 {
7573 if (sec->size == offset + 24)
7574 {
7575 need_pad = NULL;
7576 break;
7577 }
7578 if (rel == relend && sec->size == offset + 16)
7579 {
7580 cnt_16b++;
7581 break;
7582 }
7583 goto broken_opd;
7584 }
7585
7586 if (rel->r_offset == offset + 24)
7587 offset += 24;
7588 else if (rel->r_offset != offset + 16)
7589 goto broken_opd;
7590 else if (rel + 1 < relend
7591 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7592 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7593 {
7594 offset += 16;
7595 cnt_16b++;
7596 }
7597 else if (rel + 2 < relend
7598 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7599 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7600 {
7601 offset += 24;
7602 rel += 1;
7603 }
7604 else
7605 goto broken_opd;
1e2f5b6e
AM
7606 }
7607
3f764659
JJ
7608 add_aux_fields = non_overlapping && cnt_16b > 0;
7609
7610 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7611 {
7612 Elf_Internal_Rela *write_rel;
d4730f92 7613 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7614 bfd_byte *rptr, *wptr;
983bddc8 7615 bfd_byte *new_contents;
b34976b6 7616 bfd_boolean skip;
3f764659 7617 long opd_ent_size;
74f0fb50
AM
7618 bfd_size_type amt;
7619
983bddc8 7620 new_contents = NULL;
74f0fb50
AM
7621 amt = sec->size * sizeof (long) / 8;
7622 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7623 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7624 if (opd->adjust == NULL)
7625 return FALSE;
7626 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7627
7628 /* This seems a waste of time as input .opd sections are all
7629 zeros as generated by gcc, but I suppose there's no reason
7630 this will always be so. We might start putting something in
7631 the third word of .opd entries. */
7632 if ((sec->flags & SEC_IN_MEMORY) == 0)
7633 {
eea6121a
AM
7634 bfd_byte *loc;
7635 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7636 {
eea6121a
AM
7637 if (loc != NULL)
7638 free (loc);
50bc7936 7639 error_ret:
6cdc0ccc
AM
7640 if (local_syms != NULL
7641 && symtab_hdr->contents != (unsigned char *) local_syms)
7642 free (local_syms);
6cdc0ccc
AM
7643 if (elf_section_data (sec)->relocs != relstart)
7644 free (relstart);
b34976b6 7645 return FALSE;
6cdc0ccc 7646 }
1e2f5b6e
AM
7647 sec->contents = loc;
7648 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7649 }
7650
7651 elf_section_data (sec)->relocs = relstart;
7652
3f764659 7653 new_contents = sec->contents;
3f764659
JJ
7654 if (add_aux_fields)
7655 {
7656 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7657 if (new_contents == NULL)
7658 return FALSE;
7659 need_pad = FALSE;
3f764659 7660 }
b4f4e59f
AM
7661 wptr = new_contents;
7662 rptr = sec->contents;
3f764659 7663
1e2f5b6e 7664 write_rel = relstart;
b34976b6 7665 skip = FALSE;
1e2f5b6e 7666 offset = 0;
3f764659 7667 opd_ent_size = 0;
1e2f5b6e
AM
7668 for (rel = relstart; rel < relend; rel++)
7669 {
50bc7936
AM
7670 unsigned long r_symndx;
7671 asection *sym_sec;
7672 struct elf_link_hash_entry *h;
7673 Elf_Internal_Sym *sym;
7674
7675 r_symndx = ELF64_R_SYM (rel->r_info);
7676 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7677 r_symndx, ibfd))
50bc7936
AM
7678 goto error_ret;
7679
1e2f5b6e
AM
7680 if (rel->r_offset == offset)
7681 {
50bc7936 7682 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7683
7684 /* See if the .opd entry is full 24 byte or
7685 16 byte (with fd_aux entry overlapped with next
7686 fd_func). */
7687 opd_ent_size = 24;
7688 if ((rel + 2 == relend && sec->size == offset + 16)
7689 || (rel + 3 < relend
7690 && rel[2].r_offset == offset + 16
7691 && rel[3].r_offset == offset + 24
7692 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7693 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7694 opd_ent_size = 16;
7695
4025353c
AM
7696 if (h != NULL
7697 && h->root.root.string[0] == '.')
c4f68ce3 7698 {
4dfe6ac6
NC
7699 struct ppc_link_hash_table *htab;
7700
7701 htab = ppc_hash_table (info);
7702 if (htab != NULL)
7703 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7704 htab);
c4f68ce3
AM
7705 if (fdh != NULL
7706 && fdh->elf.root.type != bfd_link_hash_defined
7707 && fdh->elf.root.type != bfd_link_hash_defweak)
7708 fdh = NULL;
7709 }
1e2f5b6e 7710
51020317
AM
7711 skip = (sym_sec->owner != ibfd
7712 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7713 if (skip)
7714 {
4025353c 7715 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7716 {
7717 /* Arrange for the function descriptor sym
7718 to be dropped. */
d6fe2dc1
AM
7719 fdh->elf.root.u.def.value = 0;
7720 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7721 }
74f0fb50 7722 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7723 }
7724 else
1e2f5b6e
AM
7725 {
7726 /* We'll be keeping this opd entry. */
7727
4025353c 7728 if (fdh != NULL)
1e2f5b6e 7729 {
754021d0
AM
7730 /* Redefine the function descriptor symbol to
7731 this location in the opd section. It is
7732 necessary to update the value here rather
7733 than using an array of adjustments as we do
7734 for local symbols, because various places
7735 in the generic ELF code use the value
7736 stored in u.def.value. */
3f764659 7737 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7738 fdh->adjust_done = 1;
1e2f5b6e 7739 }
754021d0
AM
7740
7741 /* Local syms are a bit tricky. We could
7742 tweak them as they can be cached, but
7743 we'd need to look through the local syms
7744 for the function descriptor sym which we
7745 don't have at the moment. So keep an
7746 array of adjustments. */
74f0fb50 7747 opd->adjust[rel->r_offset / 8]
3f764659 7748 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7749
7750 if (wptr != rptr)
3f764659
JJ
7751 memcpy (wptr, rptr, opd_ent_size);
7752 wptr += opd_ent_size;
7753 if (add_aux_fields && opd_ent_size == 16)
7754 {
7755 memset (wptr, '\0', 8);
7756 wptr += 8;
7757 }
1e2f5b6e 7758 }
3f764659
JJ
7759 rptr += opd_ent_size;
7760 offset += opd_ent_size;
1e2f5b6e
AM
7761 }
7762
50bc7936
AM
7763 if (skip)
7764 {
60124e18
AM
7765 if (!NO_OPD_RELOCS
7766 && !info->relocatable
18d944df 7767 && !dec_dynrel_count (rel->r_info, sec, info,
19e08130 7768 NULL, h, sym))
8c1d1bb8 7769 goto error_ret;
50bc7936
AM
7770 }
7771 else
1e2f5b6e 7772 {
50bc7936
AM
7773 /* We need to adjust any reloc offsets to point to the
7774 new opd entries. While we're at it, we may as well
7775 remove redundant relocs. */
74f0fb50 7776 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7777 if (write_rel != rel)
7778 memcpy (write_rel, rel, sizeof (*rel));
7779 ++write_rel;
7780 }
7781 }
7782
3f764659 7783 sec->size = wptr - new_contents;
1e2f5b6e 7784 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7785 if (add_aux_fields)
7786 {
7787 free (sec->contents);
7788 sec->contents = new_contents;
7789 }
7790
05bf9422 7791 /* Fudge the header size too, as this is used later in
cdcf6e38 7792 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7793 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7794 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7795 some_edited = TRUE;
1e2f5b6e 7796 }
6cdc0ccc 7797 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7798 free (relstart);
6cdc0ccc 7799
411e1bfb
AM
7800 if (local_syms != NULL
7801 && symtab_hdr->contents != (unsigned char *) local_syms)
7802 {
7803 if (!info->keep_memory)
7804 free (local_syms);
7805 else
7806 symtab_hdr->contents = (unsigned char *) local_syms;
7807 }
7808 }
7809
754021d0
AM
7810 if (some_edited)
7811 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7812
3f764659
JJ
7813 /* If we are doing a final link and the last .opd entry is just 16 byte
7814 long, add a 8 byte padding after it. */
7815 if (need_pad != NULL && !info->relocatable)
7816 {
7817 bfd_byte *p;
7818
7819 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7820 {
7821 BFD_ASSERT (need_pad->size > 0);
7822
7823 p = bfd_malloc (need_pad->size + 8);
7824 if (p == NULL)
7825 return FALSE;
699733f6 7826
3f764659
JJ
7827 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7828 p, 0, need_pad->size))
7829 return FALSE;
7830
7831 need_pad->contents = p;
7832 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7833 }
7834 else
7835 {
7836 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7837 if (p == NULL)
7838 return FALSE;
7839
7840 need_pad->contents = p;
7841 }
7842
7843 memset (need_pad->contents + need_pad->size, 0, 8);
7844 need_pad->size += 8;
7845 }
7846
411e1bfb
AM
7847 return TRUE;
7848}
7849
e1918d23 7850/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7851
e1918d23 7852asection *
33c0ec9d
AM
7853ppc64_elf_tls_setup (struct bfd_link_info *info,
7854 int no_tls_get_addr_opt,
7855 int *no_multi_toc)
411e1bfb 7856{
411e1bfb
AM
7857 struct ppc_link_hash_table *htab;
7858
411e1bfb 7859 htab = ppc_hash_table (info);
4dfe6ac6
NC
7860 if (htab == NULL)
7861 return NULL;
7862
ee67d69a
AM
7863 if (abiversion (info->output_bfd) == 1)
7864 htab->opd_abi = 1;
7865
33c0ec9d
AM
7866 if (*no_multi_toc)
7867 htab->do_multi_toc = 0;
7868 else if (!htab->do_multi_toc)
7869 *no_multi_toc = 1;
7870
3a71aa26
AM
7871 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7872 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7873 FALSE, FALSE, TRUE));
a7f2871e
AM
7874 /* Move dynamic linking info to the function descriptor sym. */
7875 if (htab->tls_get_addr != NULL)
7876 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7877 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7878 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7879 FALSE, FALSE, TRUE));
a7f2871e
AM
7880 if (!no_tls_get_addr_opt)
7881 {
7882 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7883
7884 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7885 FALSE, FALSE, TRUE);
7886 if (opt != NULL)
7887 func_desc_adjust (opt, info);
7888 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7889 FALSE, FALSE, TRUE);
7890 if (opt_fd != NULL
7891 && (opt_fd->root.type == bfd_link_hash_defined
7892 || opt_fd->root.type == bfd_link_hash_defweak))
7893 {
7894 /* If glibc supports an optimized __tls_get_addr call stub,
7895 signalled by the presence of __tls_get_addr_opt, and we'll
7896 be calling __tls_get_addr via a plt call stub, then
7897 make __tls_get_addr point to __tls_get_addr_opt. */
7898 tga_fd = &htab->tls_get_addr_fd->elf;
7899 if (htab->elf.dynamic_sections_created
7900 && tga_fd != NULL
7901 && (tga_fd->type == STT_FUNC
7902 || tga_fd->needs_plt)
7903 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7904 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7905 && tga_fd->root.type == bfd_link_hash_undefweak)))
7906 {
7907 struct plt_entry *ent;
7908
7909 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7910 if (ent->plt.refcount > 0)
7911 break;
7912 if (ent != NULL)
7913 {
7914 tga_fd->root.type = bfd_link_hash_indirect;
7915 tga_fd->root.u.i.link = &opt_fd->root;
7916 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7917 if (opt_fd->dynindx != -1)
7918 {
7919 /* Use __tls_get_addr_opt in dynamic relocations. */
7920 opt_fd->dynindx = -1;
7921 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7922 opt_fd->dynstr_index);
7923 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7924 return NULL;
a7f2871e
AM
7925 }
7926 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7927 tga = &htab->tls_get_addr->elf;
7928 if (opt != NULL && tga != NULL)
7929 {
7930 tga->root.type = bfd_link_hash_indirect;
7931 tga->root.u.i.link = &opt->root;
7932 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7933 _bfd_elf_link_hash_hide_symbol (info, opt,
7934 tga->forced_local);
7935 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7936 }
7937 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7938 htab->tls_get_addr_fd->is_func_descriptor = 1;
7939 if (htab->tls_get_addr != NULL)
7940 {
7941 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7942 htab->tls_get_addr->is_func = 1;
7943 }
7944 }
7945 }
7946 }
7947 else
7948 no_tls_get_addr_opt = TRUE;
7949 }
7950 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7951 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7952}
8387904d 7953
3a71aa26
AM
7954/* Return TRUE iff REL is a branch reloc with a global symbol matching
7955 HASH1 or HASH2. */
8387904d 7956
3a71aa26
AM
7957static bfd_boolean
7958branch_reloc_hash_match (const bfd *ibfd,
7959 const Elf_Internal_Rela *rel,
7960 const struct ppc_link_hash_entry *hash1,
7961 const struct ppc_link_hash_entry *hash2)
7962{
7963 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7964 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7965 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7966
e054468f 7967 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7968 {
3a71aa26
AM
7969 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7970 struct elf_link_hash_entry *h;
8387904d 7971
3a71aa26 7972 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7973 h = elf_follow_link (h);
3a71aa26
AM
7974 if (h == &hash1->elf || h == &hash2->elf)
7975 return TRUE;
a48ebf4d 7976 }
3a71aa26 7977 return FALSE;
951fd09b 7978}
411e1bfb 7979
951fd09b
AM
7980/* Run through all the TLS relocs looking for optimization
7981 opportunities. The linker has been hacked (see ppc64elf.em) to do
7982 a preliminary section layout so that we know the TLS segment
7983 offsets. We can't optimize earlier because some optimizations need
7984 to know the tp offset, and we need to optimize before allocating
7985 dynamic relocations. */
7986
7987bfd_boolean
33c0ec9d 7988ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7989{
7990 bfd *ibfd;
7991 asection *sec;
7992 struct ppc_link_hash_table *htab;
663a1470 7993 unsigned char *toc_ref;
102890f0 7994 int pass;
951fd09b 7995
1d483afe 7996 if (info->relocatable || !info->executable)
411e1bfb
AM
7997 return TRUE;
7998
951fd09b 7999 htab = ppc_hash_table (info);
4dfe6ac6
NC
8000 if (htab == NULL)
8001 return FALSE;
8002
663a1470
AM
8003 /* Make two passes over the relocs. On the first pass, mark toc
8004 entries involved with tls relocs, and check that tls relocs
8005 involved in setting up a tls_get_addr call are indeed followed by
8006 such a call. If they are not, we can't do any tls optimization.
8007 On the second pass twiddle tls_mask flags to notify
8008 relocate_section that optimization can be done, and adjust got
8009 and plt refcounts. */
8010 toc_ref = NULL;
8011 for (pass = 0; pass < 2; ++pass)
8012 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8013 {
8014 Elf_Internal_Sym *locsyms = NULL;
8015 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8016
102890f0
AM
8017 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8018 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8019 {
8020 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8021 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8022
102890f0
AM
8023 /* Read the relocations. */
8024 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8025 info->keep_memory);
8026 if (relstart == NULL)
8027 return FALSE;
411e1bfb 8028
102890f0
AM
8029 relend = relstart + sec->reloc_count;
8030 for (rel = relstart; rel < relend; rel++)
8031 {
8032 enum elf_ppc64_reloc_type r_type;
8033 unsigned long r_symndx;
8034 struct elf_link_hash_entry *h;
8035 Elf_Internal_Sym *sym;
8036 asection *sym_sec;
f961d9dd
AM
8037 unsigned char *tls_mask;
8038 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8039 bfd_vma value;
8040 bfd_boolean ok_tprel, is_local;
8041 long toc_ref_index = 0;
8042 int expecting_tls_get_addr = 0;
663a1470 8043 bfd_boolean ret = FALSE;
411e1bfb 8044
102890f0
AM
8045 r_symndx = ELF64_R_SYM (rel->r_info);
8046 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8047 r_symndx, ibfd))
8048 {
8049 err_free_rel:
8050 if (elf_section_data (sec)->relocs != relstart)
8051 free (relstart);
8052 if (toc_ref != NULL)
8053 free (toc_ref);
8054 if (locsyms != NULL
0ffa91dd 8055 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8056 != (unsigned char *) locsyms))
8057 free (locsyms);
663a1470 8058 return ret;
102890f0 8059 }
411e1bfb 8060
102890f0
AM
8061 if (h != NULL)
8062 {
766bc656
AM
8063 if (h->root.type == bfd_link_hash_defined
8064 || h->root.type == bfd_link_hash_defweak)
8065 value = h->root.u.def.value;
8066 else if (h->root.type == bfd_link_hash_undefweak)
8067 value = 0;
8068 else
663a1470
AM
8069 {
8070 found_tls_get_addr_arg = 0;
8071 continue;
8072 }
102890f0
AM
8073 }
8074 else
8075 /* Symbols referenced by TLS relocs must be of type
8076 STT_TLS. So no need for .opd local sym adjust. */
8077 value = sym->st_value;
8078
8079 ok_tprel = FALSE;
8080 is_local = FALSE;
8081 if (h == NULL
8082 || !h->def_dynamic)
8083 {
8084 is_local = TRUE;
766bc656
AM
8085 if (h != NULL
8086 && h->root.type == bfd_link_hash_undefweak)
8087 ok_tprel = TRUE;
8088 else
8089 {
8090 value += sym_sec->output_offset;
8091 value += sym_sec->output_section->vma;
8092 value -= htab->elf.tls_sec->vma;
8093 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8094 < (bfd_vma) 1 << 32);
8095 }
102890f0 8096 }
951fd09b 8097
102890f0 8098 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8099 /* If this section has old-style __tls_get_addr calls
8100 without marker relocs, then check that each
8101 __tls_get_addr call reloc is preceded by a reloc
8102 that conceivably belongs to the __tls_get_addr arg
8103 setup insn. If we don't find matching arg setup
8104 relocs, don't do any tls optimization. */
8105 if (pass == 0
8106 && sec->has_tls_get_addr_call
8107 && h != NULL
8108 && (h == &htab->tls_get_addr->elf
8109 || h == &htab->tls_get_addr_fd->elf)
8110 && !found_tls_get_addr_arg
8111 && is_branch_reloc (r_type))
8112 {
25f53a85 8113 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8114 "TLS optimization disabled\n"),
8115 ibfd, sec, rel->r_offset);
8116 ret = TRUE;
8117 goto err_free_rel;
8118 }
8119
8120 found_tls_get_addr_arg = 0;
102890f0
AM
8121 switch (r_type)
8122 {
8123 case R_PPC64_GOT_TLSLD16:
8124 case R_PPC64_GOT_TLSLD16_LO:
8125 expecting_tls_get_addr = 1;
663a1470 8126 found_tls_get_addr_arg = 1;
102890f0
AM
8127 /* Fall thru */
8128
8129 case R_PPC64_GOT_TLSLD16_HI:
8130 case R_PPC64_GOT_TLSLD16_HA:
8131 /* These relocs should never be against a symbol
8132 defined in a shared lib. Leave them alone if
8133 that turns out to be the case. */
8134 if (!is_local)
8135 continue;
411e1bfb 8136
102890f0 8137 /* LD -> LE */
411e1bfb 8138 tls_set = 0;
102890f0
AM
8139 tls_clear = TLS_LD;
8140 tls_type = TLS_TLS | TLS_LD;
8141 break;
411e1bfb 8142
102890f0
AM
8143 case R_PPC64_GOT_TLSGD16:
8144 case R_PPC64_GOT_TLSGD16_LO:
8145 expecting_tls_get_addr = 1;
663a1470 8146 found_tls_get_addr_arg = 1;
102890f0
AM
8147 /* Fall thru */
8148
8149 case R_PPC64_GOT_TLSGD16_HI:
8150 case R_PPC64_GOT_TLSGD16_HA:
8151 if (ok_tprel)
8152 /* GD -> LE */
411e1bfb 8153 tls_set = 0;
102890f0
AM
8154 else
8155 /* GD -> IE */
8156 tls_set = TLS_TLS | TLS_TPRELGD;
8157 tls_clear = TLS_GD;
8158 tls_type = TLS_TLS | TLS_GD;
8159 break;
8160
8161 case R_PPC64_GOT_TPREL16_DS:
8162 case R_PPC64_GOT_TPREL16_LO_DS:
8163 case R_PPC64_GOT_TPREL16_HI:
8164 case R_PPC64_GOT_TPREL16_HA:
8165 if (ok_tprel)
8166 {
8167 /* IE -> LE */
8168 tls_set = 0;
8169 tls_clear = TLS_TPREL;
8170 tls_type = TLS_TLS | TLS_TPREL;
8171 break;
8172 }
411e1bfb
AM
8173 continue;
8174
727fc41e
AM
8175 case R_PPC64_TLSGD:
8176 case R_PPC64_TLSLD:
663a1470
AM
8177 found_tls_get_addr_arg = 1;
8178 /* Fall thru */
8179
8180 case R_PPC64_TLS:
8181 case R_PPC64_TOC16:
8182 case R_PPC64_TOC16_LO:
102890f0
AM
8183 if (sym_sec == NULL || sym_sec != toc)
8184 continue;
8185
8186 /* Mark this toc entry as referenced by a TLS
8187 code sequence. We can do that now in the
8188 case of R_PPC64_TLS, and after checking for
8189 tls_get_addr for the TOC16 relocs. */
8190 if (toc_ref == NULL)
663a1470
AM
8191 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8192 if (toc_ref == NULL)
8193 goto err_free_rel;
8194
102890f0
AM
8195 if (h != NULL)
8196 value = h->root.u.def.value;
8197 else
8198 value = sym->st_value;
8199 value += rel->r_addend;
8200 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 8201 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8202 if (r_type == R_PPC64_TLS
8203 || r_type == R_PPC64_TLSGD
8204 || r_type == R_PPC64_TLSLD)
102890f0
AM
8205 {
8206 toc_ref[toc_ref_index] = 1;
8207 continue;
8208 }
8209
8210 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8211 continue;
8212
8213 tls_set = 0;
8214 tls_clear = 0;
8215 expecting_tls_get_addr = 2;
8216 break;
8217
8218 case R_PPC64_TPREL64:
8219 if (pass == 0
8220 || sec != toc
8221 || toc_ref == NULL
663a1470 8222 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8223 continue;
8224 if (ok_tprel)
8225 {
8226 /* IE -> LE */
8227 tls_set = TLS_EXPLICIT;
8228 tls_clear = TLS_TPREL;
8229 break;
8230 }
8231 continue;
8232
8233 case R_PPC64_DTPMOD64:
8234 if (pass == 0
8235 || sec != toc
8236 || toc_ref == NULL
663a1470 8237 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8238 continue;
8239 if (rel + 1 < relend
8240 && (rel[1].r_info
8241 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8242 && rel[1].r_offset == rel->r_offset + 8)
8243 {
8244 if (ok_tprel)
8245 /* GD -> LE */
8246 tls_set = TLS_EXPLICIT | TLS_GD;
8247 else
8248 /* GD -> IE */
8249 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8250 tls_clear = TLS_GD;
8251 }
8252 else
8253 {
8254 if (!is_local)
8255 continue;
8256
8257 /* LD -> LE */
8258 tls_set = TLS_EXPLICIT;
8259 tls_clear = TLS_LD;
8260 }
8261 break;
8262
8263 default:
8264 continue;
8265 }
8266
8267 if (pass == 0)
8268 {
727fc41e
AM
8269 if (!expecting_tls_get_addr
8270 || !sec->has_tls_get_addr_call)
102890f0
AM
8271 continue;
8272
3a71aa26
AM
8273 if (rel + 1 < relend
8274 && branch_reloc_hash_match (ibfd, rel + 1,
8275 htab->tls_get_addr,
8276 htab->tls_get_addr_fd))
102890f0 8277 {
3a71aa26 8278 if (expecting_tls_get_addr == 2)
102890f0 8279 {
3a71aa26 8280 /* Check for toc tls entries. */
f961d9dd 8281 unsigned char *toc_tls;
3a71aa26
AM
8282 int retval;
8283
8284 retval = get_tls_mask (&toc_tls, NULL, NULL,
8285 &locsyms,
8286 rel, ibfd);
8287 if (retval == 0)
8288 goto err_free_rel;
663a1470
AM
8289 if (toc_tls != NULL)
8290 {
8291 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8292 found_tls_get_addr_arg = 1;
8293 if (retval > 1)
8294 toc_ref[toc_ref_index] = 1;
8295 }
102890f0 8296 }
3a71aa26 8297 continue;
102890f0
AM
8298 }
8299
8300 if (expecting_tls_get_addr != 1)
8301 continue;
8302
8303 /* Uh oh, we didn't find the expected call. We
8304 could just mark this symbol to exclude it
8305 from tls optimization but it's safer to skip
663a1470 8306 the entire optimization. */
25f53a85 8307 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8308 "TLS optimization disabled\n"),
8309 ibfd, sec, rel->r_offset);
8310 ret = TRUE;
8311 goto err_free_rel;
102890f0
AM
8312 }
8313
85f7a9cb 8314 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8315 {
8316 struct plt_entry *ent;
8317 for (ent = htab->tls_get_addr->elf.plt.plist;
8318 ent != NULL;
8319 ent = ent->next)
8320 if (ent->addend == 0)
411e1bfb 8321 {
102890f0 8322 if (ent->plt.refcount > 0)
30038c59 8323 {
102890f0
AM
8324 ent->plt.refcount -= 1;
8325 expecting_tls_get_addr = 0;
30038c59 8326 }
102890f0 8327 break;
411e1bfb 8328 }
102890f0 8329 }
411e1bfb 8330
85f7a9cb 8331 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8332 {
8333 struct plt_entry *ent;
8334 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8335 ent != NULL;
8336 ent = ent->next)
8337 if (ent->addend == 0)
411e1bfb 8338 {
102890f0
AM
8339 if (ent->plt.refcount > 0)
8340 ent->plt.refcount -= 1;
8341 break;
411e1bfb 8342 }
102890f0 8343 }
411e1bfb 8344
102890f0 8345 if (tls_clear == 0)
30038c59
AM
8346 continue;
8347
102890f0
AM
8348 if ((tls_set & TLS_EXPLICIT) == 0)
8349 {
8350 struct got_entry *ent;
411e1bfb 8351
102890f0
AM
8352 /* Adjust got entry for this reloc. */
8353 if (h != NULL)
8354 ent = h->got.glist;
8355 else
8356 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8357
102890f0
AM
8358 for (; ent != NULL; ent = ent->next)
8359 if (ent->addend == rel->r_addend
8360 && ent->owner == ibfd
8361 && ent->tls_type == tls_type)
8362 break;
8363 if (ent == NULL)
8364 abort ();
411e1bfb 8365
102890f0
AM
8366 if (tls_set == 0)
8367 {
8368 /* We managed to get rid of a got entry. */
8369 if (ent->got.refcount > 0)
8370 ent->got.refcount -= 1;
8371 }
8372 }
8373 else
8374 {
8375 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8376 we'll lose one or two dyn relocs. */
8377 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8378 NULL, h, sym))
102890f0 8379 return FALSE;
411e1bfb 8380
102890f0
AM
8381 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8382 {
8383 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8384 NULL, h, sym))
102890f0
AM
8385 return FALSE;
8386 }
8387 }
411e1bfb 8388
102890f0
AM
8389 *tls_mask |= tls_set;
8390 *tls_mask &= ~tls_clear;
8391 }
8c1d1bb8 8392
102890f0
AM
8393 if (elf_section_data (sec)->relocs != relstart)
8394 free (relstart);
8395 }
411e1bfb 8396
663a1470
AM
8397 if (locsyms != NULL
8398 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8399 {
8400 if (!info->keep_memory)
8401 free (locsyms);
8402 else
8403 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8404 }
8405 }
411e1bfb 8406
663a1470
AM
8407 if (toc_ref != NULL)
8408 free (toc_ref);
b34976b6 8409 return TRUE;
1e2f5b6e 8410}
b34976b6 8411
c5614fa4
AM
8412/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8413 the values of any global symbols in a toc section that has been
8414 edited. Globals in toc sections should be a rarity, so this function
8415 sets a flag if any are found in toc sections other than the one just
8416 edited, so that futher hash table traversals can be avoided. */
8417
8418struct adjust_toc_info
8419{
8420 asection *toc;
8421 unsigned long *skip;
8422 bfd_boolean global_toc_syms;
8423};
8424
ba761f19
AM
8425enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8426
c5614fa4
AM
8427static bfd_boolean
8428adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8429{
8430 struct ppc_link_hash_entry *eh;
8431 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8432 unsigned long i;
c5614fa4 8433
c5614fa4
AM
8434 if (h->root.type != bfd_link_hash_defined
8435 && h->root.type != bfd_link_hash_defweak)
8436 return TRUE;
8437
8438 eh = (struct ppc_link_hash_entry *) h;
8439 if (eh->adjust_done)
8440 return TRUE;
8441
8442 if (eh->elf.root.u.def.section == toc_inf->toc)
8443 {
854b41e7
AM
8444 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8445 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8446 else
854b41e7
AM
8447 i = eh->elf.root.u.def.value >> 3;
8448
ba761f19 8449 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8450 {
8451 (*_bfd_error_handler)
854b41e7
AM
8452 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8453 do
8454 ++i;
ba761f19 8455 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8456 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8457 }
854b41e7
AM
8458
8459 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8460 eh->adjust_done = 1;
8461 }
8462 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8463 toc_inf->global_toc_syms = TRUE;
8464
8465 return TRUE;
8466}
8467
560c8763
AM
8468/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8469
8470static bfd_boolean
8471ok_lo_toc_insn (unsigned int insn)
8472{
8473 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8474 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8475 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8476 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8477 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8478 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8479 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8480 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8481 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8482 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8483 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8484 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8485 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8486 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8487 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8488 && (insn & 3) != 1)
8489 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8490 && ((insn & 3) == 0 || (insn & 3) == 3))
8491 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8492}
8493
c5614fa4
AM
8494/* Examine all relocs referencing .toc sections in order to remove
8495 unused .toc entries. */
8496
8497bfd_boolean
33c0ec9d 8498ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8499{
8500 bfd *ibfd;
8501 struct adjust_toc_info toc_inf;
67f0cbdb 8502 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8503
67f0cbdb 8504 htab->do_toc_opt = 1;
c5614fa4
AM
8505 toc_inf.global_toc_syms = TRUE;
8506 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8507 {
8508 asection *toc, *sec;
8509 Elf_Internal_Shdr *symtab_hdr;
8510 Elf_Internal_Sym *local_syms;
425b145b 8511 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8512 unsigned long *skip, *drop;
8513 unsigned char *used;
8514 unsigned char *keep, last, some_unused;
8515
854b41e7
AM
8516 if (!is_ppc64_elf (ibfd))
8517 continue;
8518
c5614fa4
AM
8519 toc = bfd_get_section_by_name (ibfd, ".toc");
8520 if (toc == NULL
92b7a70f 8521 || toc->size == 0
dbaa2011
AM
8522 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8523 || discarded_section (toc))
c5614fa4
AM
8524 continue;
8525
425b145b 8526 toc_relocs = NULL;
c5614fa4 8527 local_syms = NULL;
0ffa91dd 8528 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8529
8530 /* Look at sections dropped from the final link. */
8531 skip = NULL;
8532 relstart = NULL;
8533 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8534 {
8535 if (sec->reloc_count == 0
dbaa2011 8536 || !discarded_section (sec)
c5614fa4
AM
8537 || get_opd_info (sec)
8538 || (sec->flags & SEC_ALLOC) == 0
8539 || (sec->flags & SEC_DEBUGGING) != 0)
8540 continue;
8541
8542 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8543 if (relstart == NULL)
8544 goto error_ret;
8545
8546 /* Run through the relocs to see which toc entries might be
8547 unused. */
8548 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8549 {
8550 enum elf_ppc64_reloc_type r_type;
8551 unsigned long r_symndx;
8552 asection *sym_sec;
8553 struct elf_link_hash_entry *h;
8554 Elf_Internal_Sym *sym;
8555 bfd_vma val;
8556
8557 r_type = ELF64_R_TYPE (rel->r_info);
8558 switch (r_type)
8559 {
8560 default:
8561 continue;
8562
8563 case R_PPC64_TOC16:
8564 case R_PPC64_TOC16_LO:
8565 case R_PPC64_TOC16_HI:
8566 case R_PPC64_TOC16_HA:
8567 case R_PPC64_TOC16_DS:
8568 case R_PPC64_TOC16_LO_DS:
8569 break;
8570 }
8571
8572 r_symndx = ELF64_R_SYM (rel->r_info);
8573 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8574 r_symndx, ibfd))
8575 goto error_ret;
8576
8577 if (sym_sec != toc)
8578 continue;
8579
8580 if (h != NULL)
8581 val = h->root.u.def.value;
8582 else
8583 val = sym->st_value;
8584 val += rel->r_addend;
8585
8586 if (val >= toc->size)
8587 continue;
8588
8589 /* Anything in the toc ought to be aligned to 8 bytes.
8590 If not, don't mark as unused. */
8591 if (val & 7)
8592 continue;
8593
8594 if (skip == NULL)
8595 {
854b41e7 8596 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8597 if (skip == NULL)
8598 goto error_ret;
8599 }
8600
ba761f19 8601 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8602 }
8603
8604 if (elf_section_data (sec)->relocs != relstart)
8605 free (relstart);
8606 }
8607
ba761f19
AM
8608 /* For largetoc loads of address constants, we can convert
8609 . addis rx,2,addr@got@ha
8610 . ld ry,addr@got@l(rx)
8611 to
8612 . addis rx,2,addr@toc@ha
8613 . addi ry,rx,addr@toc@l
8614 when addr is within 2G of the toc pointer. This then means
8615 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8616
ba761f19
AM
8617 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8618 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8619 && toc->reloc_count != 0)
8620 {
8621 /* Read toc relocs. */
425b145b
AM
8622 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8623 info->keep_memory);
8624 if (toc_relocs == NULL)
ba761f19
AM
8625 goto error_ret;
8626
425b145b 8627 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8628 {
8629 enum elf_ppc64_reloc_type r_type;
8630 unsigned long r_symndx;
8631 asection *sym_sec;
8632 struct elf_link_hash_entry *h;
8633 Elf_Internal_Sym *sym;
8634 bfd_vma val, addr;
8635
8636 r_type = ELF64_R_TYPE (rel->r_info);
8637 if (r_type != R_PPC64_ADDR64)
8638 continue;
8639
8640 r_symndx = ELF64_R_SYM (rel->r_info);
8641 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8642 r_symndx, ibfd))
8643 goto error_ret;
8644
425b145b 8645 if (sym_sec == NULL
dbaa2011 8646 || discarded_section (sym_sec))
425b145b
AM
8647 continue;
8648
726d3ab0 8649 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8650 continue;
8651
8652 if (h != NULL)
bddc25c9
AM
8653 {
8654 if (h->type == STT_GNU_IFUNC)
8655 continue;
8656 val = h->root.u.def.value;
8657 }
ba761f19 8658 else
bddc25c9
AM
8659 {
8660 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8661 continue;
8662 val = sym->st_value;
8663 }
ba761f19
AM
8664 val += rel->r_addend;
8665 val += sym_sec->output_section->vma + sym_sec->output_offset;
8666
8667 /* We don't yet know the exact toc pointer value, but we
8668 know it will be somewhere in the toc section. Don't
8669 optimize if the difference from any possible toc
8670 pointer is outside [ff..f80008000, 7fff7fff]. */
8671 addr = toc->output_section->vma + TOC_BASE_OFF;
8672 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8673 continue;
8674
8675 addr = toc->output_section->vma + toc->output_section->rawsize;
8676 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8677 continue;
8678
8679 if (skip == NULL)
8680 {
8681 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8682 if (skip == NULL)
8683 goto error_ret;
8684 }
8685
8686 skip[rel->r_offset >> 3]
425b145b 8687 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8688 }
ba761f19
AM
8689 }
8690
c5614fa4
AM
8691 if (skip == NULL)
8692 continue;
8693
8694 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8695 if (used == NULL)
8696 {
8697 error_ret:
8698 if (local_syms != NULL
8699 && symtab_hdr->contents != (unsigned char *) local_syms)
8700 free (local_syms);
8701 if (sec != NULL
8702 && relstart != NULL
8703 && elf_section_data (sec)->relocs != relstart)
8704 free (relstart);
425b145b
AM
8705 if (toc_relocs != NULL
8706 && elf_section_data (toc)->relocs != toc_relocs)
8707 free (toc_relocs);
c5614fa4
AM
8708 if (skip != NULL)
8709 free (skip);
8710 return FALSE;
8711 }
8712
30038c59
AM
8713 /* Now check all kept sections that might reference the toc.
8714 Check the toc itself last. */
8715 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8716 : ibfd->sections);
c5614fa4 8717 sec != NULL;
c5614fa4 8718 sec = (sec == toc ? NULL
c5614fa4 8719 : sec->next == NULL ? toc
30038c59 8720 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8721 : sec->next))
8722 {
8723 int repeat;
8724
8725 if (sec->reloc_count == 0
dbaa2011 8726 || discarded_section (sec)
c5614fa4
AM
8727 || get_opd_info (sec)
8728 || (sec->flags & SEC_ALLOC) == 0
8729 || (sec->flags & SEC_DEBUGGING) != 0)
8730 continue;
8731
854b41e7
AM
8732 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8733 info->keep_memory);
c5614fa4
AM
8734 if (relstart == NULL)
8735 goto error_ret;
8736
8737 /* Mark toc entries referenced as used. */
c5614fa4 8738 do
d4f1ee75
AM
8739 {
8740 repeat = 0;
8741 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8742 {
8743 enum elf_ppc64_reloc_type r_type;
8744 unsigned long r_symndx;
8745 asection *sym_sec;
8746 struct elf_link_hash_entry *h;
8747 Elf_Internal_Sym *sym;
8748 bfd_vma val;
8749 enum {no_check, check_lo, check_ha} insn_check;
98528052 8750
d4f1ee75
AM
8751 r_type = ELF64_R_TYPE (rel->r_info);
8752 switch (r_type)
8753 {
8754 default:
8755 insn_check = no_check;
8756 break;
98528052 8757
d4f1ee75
AM
8758 case R_PPC64_GOT_TLSLD16_HA:
8759 case R_PPC64_GOT_TLSGD16_HA:
8760 case R_PPC64_GOT_TPREL16_HA:
8761 case R_PPC64_GOT_DTPREL16_HA:
8762 case R_PPC64_GOT16_HA:
8763 case R_PPC64_TOC16_HA:
8764 insn_check = check_ha;
8765 break;
98528052 8766
d4f1ee75
AM
8767 case R_PPC64_GOT_TLSLD16_LO:
8768 case R_PPC64_GOT_TLSGD16_LO:
8769 case R_PPC64_GOT_TPREL16_LO_DS:
8770 case R_PPC64_GOT_DTPREL16_LO_DS:
8771 case R_PPC64_GOT16_LO:
8772 case R_PPC64_GOT16_LO_DS:
8773 case R_PPC64_TOC16_LO:
8774 case R_PPC64_TOC16_LO_DS:
8775 insn_check = check_lo;
8776 break;
8777 }
560c8763 8778
d4f1ee75
AM
8779 if (insn_check != no_check)
8780 {
8781 bfd_vma off = rel->r_offset & ~3;
8782 unsigned char buf[4];
8783 unsigned int insn;
c5614fa4 8784
d4f1ee75
AM
8785 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8786 {
8787 free (used);
8788 goto error_ret;
8789 }
8790 insn = bfd_get_32 (ibfd, buf);
8791 if (insn_check == check_lo
8792 ? !ok_lo_toc_insn (insn)
8793 : ((insn & ((0x3f << 26) | 0x1f << 16))
8794 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8795 {
8796 char str[12];
8797
8798 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8799 sprintf (str, "%#08x", insn);
8800 info->callbacks->einfo
8801 (_("%P: %H: toc optimization is not supported for"
8802 " %s instruction.\n"),
8803 ibfd, sec, rel->r_offset & ~3, str);
8804 }
8805 }
c5614fa4 8806
d4f1ee75
AM
8807 switch (r_type)
8808 {
8809 case R_PPC64_TOC16:
8810 case R_PPC64_TOC16_LO:
8811 case R_PPC64_TOC16_HI:
8812 case R_PPC64_TOC16_HA:
8813 case R_PPC64_TOC16_DS:
8814 case R_PPC64_TOC16_LO_DS:
8815 /* In case we're taking addresses of toc entries. */
8816 case R_PPC64_ADDR64:
8817 break;
c5614fa4 8818
d4f1ee75
AM
8819 default:
8820 continue;
8821 }
c5614fa4 8822
d4f1ee75
AM
8823 r_symndx = ELF64_R_SYM (rel->r_info);
8824 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8825 r_symndx, ibfd))
8826 {
8827 free (used);
8828 goto error_ret;
8829 }
c5614fa4 8830
d4f1ee75
AM
8831 if (sym_sec != toc)
8832 continue;
c5614fa4 8833
d4f1ee75
AM
8834 if (h != NULL)
8835 val = h->root.u.def.value;
8836 else
8837 val = sym->st_value;
8838 val += rel->r_addend;
ba761f19 8839
d4f1ee75
AM
8840 if (val >= toc->size)
8841 continue;
ba761f19 8842
d4f1ee75
AM
8843 if ((skip[val >> 3] & can_optimize) != 0)
8844 {
8845 bfd_vma off;
8846 unsigned char opc;
8847
8848 switch (r_type)
8849 {
8850 case R_PPC64_TOC16_HA:
ba761f19 8851 break;
ba761f19 8852
d4f1ee75
AM
8853 case R_PPC64_TOC16_LO_DS:
8854 off = rel->r_offset;
8855 off += (bfd_big_endian (ibfd) ? -2 : 3);
8856 if (!bfd_get_section_contents (ibfd, sec, &opc,
8857 off, 1))
8858 {
8859 free (used);
8860 goto error_ret;
8861 }
8862 if ((opc & (0x3f << 2)) == (58u << 2))
8863 break;
8864 /* Fall thru */
ba761f19 8865
d4f1ee75
AM
8866 default:
8867 /* Wrong sort of reloc, or not a ld. We may
8868 as well clear ref_from_discarded too. */
8869 skip[val >> 3] = 0;
8870 }
8871 }
8872
8873 if (sec != toc)
8874 used[val >> 3] = 1;
8875 /* For the toc section, we only mark as used if this
8876 entry itself isn't unused. */
8877 else if ((used[rel->r_offset >> 3]
8878 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8879 && !used[val >> 3])
8880 {
8881 /* Do all the relocs again, to catch reference
8882 chains. */
8883 repeat = 1;
8884 used[val >> 3] = 1;
8885 }
8886 }
8887 }
c5614fa4 8888 while (repeat);
854b41e7
AM
8889
8890 if (elf_section_data (sec)->relocs != relstart)
8891 free (relstart);
c5614fa4
AM
8892 }
8893
8894 /* Merge the used and skip arrays. Assume that TOC
8895 doublewords not appearing as either used or unused belong
8896 to to an entry more than one doubleword in size. */
8897 for (drop = skip, keep = used, last = 0, some_unused = 0;
8898 drop < skip + (toc->size + 7) / 8;
8899 ++drop, ++keep)
8900 {
8901 if (*keep)
8902 {
ba761f19
AM
8903 *drop &= ~ref_from_discarded;
8904 if ((*drop & can_optimize) != 0)
8905 some_unused = 1;
c5614fa4
AM
8906 last = 0;
8907 }
b140b010 8908 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8909 {
8910 some_unused = 1;
ba761f19 8911 last = ref_from_discarded;
c5614fa4
AM
8912 }
8913 else
8914 *drop = last;
8915 }
8916
8917 free (used);
8918
8919 if (some_unused)
8920 {
8921 bfd_byte *contents, *src;
8922 unsigned long off;
d62b3684 8923 Elf_Internal_Sym *sym;
ba761f19 8924 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8925
8926 /* Shuffle the toc contents, and at the same time convert the
8927 skip array from booleans into offsets. */
8928 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8929 goto error_ret;
8930
8931 elf_section_data (toc)->this_hdr.contents = contents;
8932
8933 for (src = contents, off = 0, drop = skip;
8934 src < contents + toc->size;
8935 src += 8, ++drop)
8936 {
ba761f19
AM
8937 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8938 off += 8;
c5614fa4
AM
8939 else if (off != 0)
8940 {
8941 *drop = off;
8942 memcpy (src - off, src, 8);
8943 }
8944 }
854b41e7 8945 *drop = off;
c5614fa4
AM
8946 toc->rawsize = toc->size;
8947 toc->size = src - contents - off;
8948
ba761f19
AM
8949 /* Adjust addends for relocs against the toc section sym,
8950 and optimize any accesses we can. */
c5614fa4
AM
8951 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8952 {
8953 if (sec->reloc_count == 0
dbaa2011 8954 || discarded_section (sec))
c5614fa4
AM
8955 continue;
8956
8957 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8958 info->keep_memory);
c5614fa4
AM
8959 if (relstart == NULL)
8960 goto error_ret;
8961
8962 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8963 {
8964 enum elf_ppc64_reloc_type r_type;
8965 unsigned long r_symndx;
8966 asection *sym_sec;
8967 struct elf_link_hash_entry *h;
854b41e7 8968 bfd_vma val;
c5614fa4
AM
8969
8970 r_type = ELF64_R_TYPE (rel->r_info);
8971 switch (r_type)
8972 {
8973 default:
8974 continue;
8975
8976 case R_PPC64_TOC16:
8977 case R_PPC64_TOC16_LO:
8978 case R_PPC64_TOC16_HI:
8979 case R_PPC64_TOC16_HA:
8980 case R_PPC64_TOC16_DS:
8981 case R_PPC64_TOC16_LO_DS:
8982 case R_PPC64_ADDR64:
8983 break;
8984 }
8985
8986 r_symndx = ELF64_R_SYM (rel->r_info);
8987 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8988 r_symndx, ibfd))
8989 goto error_ret;
8990
ba761f19 8991 if (sym_sec != toc)
c5614fa4
AM
8992 continue;
8993
ba761f19
AM
8994 if (h != NULL)
8995 val = h->root.u.def.value;
8996 else
8997 {
8998 val = sym->st_value;
8999 if (val != 0)
9000 local_toc_syms = TRUE;
9001 }
9002
9003 val += rel->r_addend;
854b41e7
AM
9004
9005 if (val > toc->rawsize)
9006 val = toc->rawsize;
ba761f19
AM
9007 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9008 continue;
9009 else if ((skip[val >> 3] & can_optimize) != 0)
9010 {
9011 Elf_Internal_Rela *tocrel
425b145b 9012 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9013 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9014
9015 switch (r_type)
9016 {
9017 case R_PPC64_TOC16_HA:
9018 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9019 break;
9020
9021 case R_PPC64_TOC16_LO_DS:
9022 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9023 break;
9024
9025 default:
28942f62
AM
9026 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9027 ppc_howto_init ();
b140b010 9028 info->callbacks->einfo
bc30df16 9029 (_("%P: %H: %s references "
b140b010
AM
9030 "optimized away TOC entry\n"),
9031 ibfd, sec, rel->r_offset,
9032 ppc64_elf_howto_table[r_type]->name);
9033 bfd_set_error (bfd_error_bad_value);
9034 goto error_ret;
ba761f19
AM
9035 }
9036 rel->r_addend = tocrel->r_addend;
9037 elf_section_data (sec)->relocs = relstart;
9038 continue;
9039 }
9040
9041 if (h != NULL || sym->st_value != 0)
9042 continue;
854b41e7
AM
9043
9044 rel->r_addend -= skip[val >> 3];
9045 elf_section_data (sec)->relocs = relstart;
c5614fa4 9046 }
854b41e7
AM
9047
9048 if (elf_section_data (sec)->relocs != relstart)
9049 free (relstart);
c5614fa4
AM
9050 }
9051
9052 /* We shouldn't have local or global symbols defined in the TOC,
9053 but handle them anyway. */
df22d223
AM
9054 if (local_syms != NULL)
9055 for (sym = local_syms;
9056 sym < local_syms + symtab_hdr->sh_info;
9057 ++sym)
9058 if (sym->st_value != 0
9059 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9060 {
9061 unsigned long i;
854b41e7 9062
df22d223
AM
9063 if (sym->st_value > toc->rawsize)
9064 i = toc->rawsize >> 3;
9065 else
9066 i = sym->st_value >> 3;
854b41e7 9067
df22d223
AM
9068 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9069 {
9070 if (local_toc_syms)
9071 (*_bfd_error_handler)
9072 (_("%s defined on removed toc entry"),
9073 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9074 do
9075 ++i;
9076 while ((skip[i] & (ref_from_discarded | can_optimize)));
9077 sym->st_value = (bfd_vma) i << 3;
9078 }
d62b3684 9079
df22d223
AM
9080 sym->st_value -= skip[i];
9081 symtab_hdr->contents = (unsigned char *) local_syms;
9082 }
c5614fa4 9083
854b41e7 9084 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9085 if (toc_inf.global_toc_syms)
9086 {
9087 toc_inf.toc = toc;
9088 toc_inf.skip = skip;
9089 toc_inf.global_toc_syms = FALSE;
9090 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9091 &toc_inf);
9092 }
854b41e7
AM
9093
9094 if (toc->reloc_count != 0)
9095 {
d4730f92 9096 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9097 Elf_Internal_Rela *wrel;
9098 bfd_size_type sz;
9099
854b41e7 9100 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9101 if (toc_relocs == NULL)
9102 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9103 info->keep_memory);
9104 if (toc_relocs == NULL)
9105 goto error_ret;
9106
425b145b
AM
9107 wrel = toc_relocs;
9108 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9109 if ((skip[rel->r_offset >> 3]
9110 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9111 {
9112 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9113 wrel->r_info = rel->r_info;
9114 wrel->r_addend = rel->r_addend;
9115 ++wrel;
9116 }
9117 else if (!dec_dynrel_count (rel->r_info, toc, info,
9118 &local_syms, NULL, NULL))
9119 goto error_ret;
9120
425b145b
AM
9121 elf_section_data (toc)->relocs = toc_relocs;
9122 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9123 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9124 sz = rel_hdr->sh_entsize;
9125 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9126 }
c5614fa4 9127 }
28be611c
AM
9128 else if (toc_relocs != NULL
9129 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9130 free (toc_relocs);
c5614fa4
AM
9131
9132 if (local_syms != NULL
9133 && symtab_hdr->contents != (unsigned char *) local_syms)
9134 {
9135 if (!info->keep_memory)
9136 free (local_syms);
9137 else
9138 symtab_hdr->contents = (unsigned char *) local_syms;
9139 }
9140 free (skip);
9141 }
9142
9143 return TRUE;
9144}
9145
1bbe0902
AM
9146/* Return true iff input section I references the TOC using
9147 instructions limited to +/-32k offsets. */
9148
9149bfd_boolean
9150ppc64_elf_has_small_toc_reloc (asection *i)
9151{
9152 return (is_ppc64_elf (i->owner)
9153 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9154}
9155
927be08e
AM
9156/* Allocate space for one GOT entry. */
9157
9158static void
9159allocate_got (struct elf_link_hash_entry *h,
9160 struct bfd_link_info *info,
9161 struct got_entry *gent)
9162{
9163 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9164 bfd_boolean dyn;
9165 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9166 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9167 ? 16 : 8);
9168 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9169 ? 2 : 1) * sizeof (Elf64_External_Rela);
9170 asection *got = ppc64_elf_tdata (gent->owner)->got;
9171
9172 gent->got.offset = got->size;
9173 got->size += entsize;
9174
9175 dyn = htab->elf.dynamic_sections_created;
19e08130 9176 if (h->type == STT_GNU_IFUNC)
927be08e 9177 {
19e08130
AM
9178 htab->reliplt->size += rentsize;
9179 htab->got_reli_size += rentsize;
927be08e 9180 }
19e08130
AM
9181 else if ((info->shared
9182 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9183 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9184 || h->root.type != bfd_link_hash_undefweak))
927be08e 9185 {
19e08130 9186 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9187 relgot->size += rentsize;
927be08e
AM
9188 }
9189}
9190
7865406b
AM
9191/* This function merges got entries in the same toc group. */
9192
9193static void
9194merge_got_entries (struct got_entry **pent)
9195{
9196 struct got_entry *ent, *ent2;
9197
9198 for (ent = *pent; ent != NULL; ent = ent->next)
9199 if (!ent->is_indirect)
9200 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9201 if (!ent2->is_indirect
9202 && ent2->addend == ent->addend
9203 && ent2->tls_type == ent->tls_type
9204 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9205 {
9206 ent2->is_indirect = TRUE;
9207 ent2->got.ent = ent;
9208 }
9209}
9210
65f38f15
AM
9211/* Allocate space in .plt, .got and associated reloc sections for
9212 dynamic relocs. */
5bd4f169 9213
b34976b6 9214static bfd_boolean
4ce794b7 9215allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9216{
65f38f15
AM
9217 struct bfd_link_info *info;
9218 struct ppc_link_hash_table *htab;
5bd4f169 9219 asection *s;
65f38f15 9220 struct ppc_link_hash_entry *eh;
6061a67d 9221 struct elf_dyn_relocs *p;
0b8bcf0d 9222 struct got_entry **pgent, *gent;
5bd4f169 9223
e92d460e 9224 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9225 return TRUE;
5bd4f169 9226
65f38f15
AM
9227 info = (struct bfd_link_info *) inf;
9228 htab = ppc_hash_table (info);
4dfe6ac6
NC
9229 if (htab == NULL)
9230 return FALSE;
5bd4f169 9231
e054468f
AM
9232 if ((htab->elf.dynamic_sections_created
9233 && h->dynindx != -1
9234 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9235 || h->type == STT_GNU_IFUNC)
5bd4f169 9236 {
411e1bfb
AM
9237 struct plt_entry *pent;
9238 bfd_boolean doneone = FALSE;
9239 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9240 if (pent->plt.refcount > 0)
9241 {
25f23106
AM
9242 if (!htab->elf.dynamic_sections_created
9243 || h->dynindx == -1)
e054468f
AM
9244 {
9245 s = htab->iplt;
9246 pent->plt.offset = s->size;
b9e5796b 9247 s->size += PLT_ENTRY_SIZE (htab);
e054468f
AM
9248 s = htab->reliplt;
9249 }
9250 else
9251 {
9252 /* If this is the first .plt entry, make room for the special
9253 first entry. */
9254 s = htab->plt;
9255 if (s->size == 0)
b9e5796b 9256 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
e054468f
AM
9257
9258 pent->plt.offset = s->size;
9259
9260 /* Make room for this entry. */
b9e5796b 9261 s->size += PLT_ENTRY_SIZE (htab);
e054468f
AM
9262
9263 /* Make room for the .glink code. */
9264 s = htab->glink;
9265 if (s->size == 0)
9266 s->size += GLINK_CALL_STUB_SIZE;
b9e5796b
AM
9267 if (htab->opd_abi)
9268 {
9269 /* We need bigger stubs past index 32767. */
9270 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9271 s->size += 4;
9272 s->size += 2*4;
9273 }
9274 else
e054468f 9275 s->size += 4;
e054468f
AM
9276
9277 /* We also need to make an entry in the .rela.plt section. */
9278 s = htab->relplt;
9279 }
eea6121a 9280 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
9281 doneone = TRUE;
9282 }
9283 else
9284 pent->plt.offset = (bfd_vma) -1;
9285 if (!doneone)
65f38f15 9286 {
411e1bfb 9287 h->plt.plist = NULL;
f5385ebf 9288 h->needs_plt = 0;
65f38f15
AM
9289 }
9290 }
9291 else
9292 {
411e1bfb 9293 h->plt.plist = NULL;
f5385ebf 9294 h->needs_plt = 0;
65f38f15
AM
9295 }
9296
951fd09b
AM
9297 eh = (struct ppc_link_hash_entry *) h;
9298 /* Run through the TLS GD got entries first if we're changing them
9299 to TPREL. */
e7b938ca 9300 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9301 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9302 if (gent->got.refcount > 0
9303 && (gent->tls_type & TLS_GD) != 0)
9304 {
9305 /* This was a GD entry that has been converted to TPREL. If
9306 there happens to be a TPREL entry we can use that one. */
9307 struct got_entry *ent;
9308 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9309 if (ent->got.refcount > 0
9310 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9311 && ent->addend == gent->addend
9312 && ent->owner == gent->owner)
951fd09b
AM
9313 {
9314 gent->got.refcount = 0;
9315 break;
9316 }
9317
9318 /* If not, then we'll be using our own TPREL entry. */
9319 if (gent->got.refcount != 0)
9320 gent->tls_type = TLS_TLS | TLS_TPREL;
9321 }
9322
7865406b
AM
9323 /* Remove any list entry that won't generate a word in the GOT before
9324 we call merge_got_entries. Otherwise we risk merging to empty
9325 entries. */
0b8bcf0d
AM
9326 pgent = &h->got.glist;
9327 while ((gent = *pgent) != NULL)
411e1bfb 9328 if (gent->got.refcount > 0)
7865406b
AM
9329 {
9330 if ((gent->tls_type & TLS_LD) != 0
9331 && !h->def_dynamic)
9332 {
9333 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9334 *pgent = gent->next;
9335 }
9336 else
9337 pgent = &gent->next;
9338 }
9339 else
9340 *pgent = gent->next;
9341
9342 if (!htab->do_multi_toc)
9343 merge_got_entries (&h->got.glist);
9344
9345 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9346 if (!gent->is_indirect)
411e1bfb
AM
9347 {
9348 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9349 Undefined weak syms won't yet be marked as dynamic,
9350 nor will all TLS symbols. */
411e1bfb 9351 if (h->dynindx == -1
b099ab9f 9352 && !h->forced_local
25f23106 9353 && h->type != STT_GNU_IFUNC
b099ab9f 9354 && htab->elf.dynamic_sections_created)
411e1bfb 9355 {
c152c796 9356 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9357 return FALSE;
9358 }
65f38f15 9359
0c8d6e5c 9360 if (!is_ppc64_elf (gent->owner))
927be08e 9361 abort ();
0ffa91dd 9362
927be08e 9363 allocate_got (h, info, gent);
411e1bfb 9364 }
65f38f15 9365
b099ab9f 9366 if (eh->dyn_relocs == NULL
25f23106
AM
9367 || (!htab->elf.dynamic_sections_created
9368 && h->type != STT_GNU_IFUNC))
b34976b6 9369 return TRUE;
65f38f15
AM
9370
9371 /* In the shared -Bsymbolic case, discard space allocated for
9372 dynamic pc-relative relocs against symbols which turn out to be
9373 defined in regular objects. For the normal shared case, discard
9374 space for relocs that have become local due to symbol visibility
9375 changes. */
9376
9377 if (info->shared)
9378 {
9c7a29a3 9379 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 9380 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
9381 generated via assembly. We want calls to protected symbols to
9382 resolve directly to the function rather than going via the plt.
9383 If people want function pointer comparisons to work as expected
9384 then they should avoid writing weird assembly. */
09695f56 9385 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 9386 {
6061a67d 9387 struct elf_dyn_relocs **pp;
65f38f15
AM
9388
9389 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 9390 {
65f38f15
AM
9391 p->count -= p->pc_count;
9392 p->pc_count = 0;
9393 if (p->count == 0)
9394 *pp = p->next;
9395 else
9396 pp = &p->next;
5bd4f169 9397 }
65f38f15 9398 }
4e795f50
AM
9399
9400 /* Also discard relocs on undefined weak syms with non-default
9401 visibility. */
cab87ef9
AM
9402 if (eh->dyn_relocs != NULL
9403 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
9404 {
9405 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9406 eh->dyn_relocs = NULL;
9407
9408 /* Make sure this symbol is output as a dynamic symbol.
9409 Undefined weak syms won't yet be marked as dynamic. */
9410 else if (h->dynindx == -1
9411 && !h->forced_local)
9412 {
9413 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9414 return FALSE;
9415 }
9416 }
65f38f15 9417 }
25f23106
AM
9418 else if (h->type == STT_GNU_IFUNC)
9419 {
9420 if (!h->non_got_ref)
9421 eh->dyn_relocs = NULL;
9422 }
f4656909 9423 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
9424 {
9425 /* For the non-shared case, discard space for relocs against
9426 symbols which turn out to need copy relocs or are not
9427 dynamic. */
9428
f5385ebf 9429 if (!h->non_got_ref
f5385ebf 9430 && !h->def_regular)
65f38f15
AM
9431 {
9432 /* Make sure this symbol is output as a dynamic symbol.
9433 Undefined weak syms won't yet be marked as dynamic. */
9434 if (h->dynindx == -1
f5385ebf 9435 && !h->forced_local)
65f38f15 9436 {
c152c796 9437 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 9438 return FALSE;
65f38f15
AM
9439 }
9440
9441 /* If that succeeded, we know we'll be keeping all the
9442 relocs. */
9443 if (h->dynindx != -1)
9444 goto keep;
9445 }
9446
9447 eh->dyn_relocs = NULL;
9448
ec338859 9449 keep: ;
65f38f15
AM
9450 }
9451
9452 /* Finally, allocate space. */
9453 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9454 {
9455 asection *sreloc = elf_section_data (p->sec)->sreloc;
19e08130 9456 if (eh->elf.type == STT_GNU_IFUNC)
25f23106 9457 sreloc = htab->reliplt;
eea6121a 9458 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9459 }
9460
b34976b6 9461 return TRUE;
65f38f15
AM
9462}
9463
9464/* Find any dynamic relocs that apply to read-only sections. */
9465
b34976b6 9466static bfd_boolean
4ce794b7 9467readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
9468{
9469 struct ppc_link_hash_entry *eh;
6061a67d 9470 struct elf_dyn_relocs *p;
65f38f15
AM
9471
9472 eh = (struct ppc_link_hash_entry *) h;
9473 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9474 {
9475 asection *s = p->sec->output_section;
9476
9477 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9478 {
4ce794b7 9479 struct bfd_link_info *info = inf;
65f38f15
AM
9480
9481 info->flags |= DF_TEXTREL;
9482
9483 /* Not an error, just cut short the traversal. */
b34976b6 9484 return FALSE;
65f38f15
AM
9485 }
9486 }
b34976b6 9487 return TRUE;
65f38f15
AM
9488}
9489
9490/* Set the sizes of the dynamic sections. */
9491
b34976b6 9492static bfd_boolean
ee67d69a 9493ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9494 struct bfd_link_info *info)
65f38f15
AM
9495{
9496 struct ppc_link_hash_table *htab;
9497 bfd *dynobj;
9498 asection *s;
b34976b6 9499 bfd_boolean relocs;
65f38f15 9500 bfd *ibfd;
7865406b 9501 struct got_entry *first_tlsld;
65f38f15
AM
9502
9503 htab = ppc_hash_table (info);
4dfe6ac6
NC
9504 if (htab == NULL)
9505 return FALSE;
9506
65f38f15
AM
9507 dynobj = htab->elf.dynobj;
9508 if (dynobj == NULL)
9509 abort ();
9510
9511 if (htab->elf.dynamic_sections_created)
9512 {
9513 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 9514 if (info->executable)
65f38f15 9515 {
3d4d4302 9516 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9517 if (s == NULL)
9518 abort ();
eea6121a 9519 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9520 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9521 }
9522 }
9523
9524 /* Set up .got offsets for local syms, and space for local dynamic
9525 relocs. */
9526 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9527 {
411e1bfb
AM
9528 struct got_entry **lgot_ents;
9529 struct got_entry **end_lgot_ents;
e054468f
AM
9530 struct plt_entry **local_plt;
9531 struct plt_entry **end_local_plt;
f961d9dd 9532 unsigned char *lgot_masks;
65f38f15
AM
9533 bfd_size_type locsymcount;
9534 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9535
0c8d6e5c 9536 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9537 continue;
9538
9539 for (s = ibfd->sections; s != NULL; s = s->next)
9540 {
19e08130 9541 struct ppc_dyn_relocs *p;
65f38f15 9542
6edfbbad 9543 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9544 {
ec338859
AM
9545 if (!bfd_is_abs_section (p->sec)
9546 && bfd_is_abs_section (p->sec->output_section))
9547 {
9548 /* Input section has been discarded, either because
9549 it is a copy of a linkonce section or due to
9550 linker script /DISCARD/, so we'll be discarding
9551 the relocs too. */
9552 }
248866a8 9553 else if (p->count != 0)
ec338859 9554 {
19e08130
AM
9555 asection *srel = elf_section_data (p->sec)->sreloc;
9556 if (p->ifunc)
25f23106 9557 srel = htab->reliplt;
eea6121a 9558 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9559 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9560 info->flags |= DF_TEXTREL;
ec338859 9561 }
65f38f15
AM
9562 }
9563 }
9564
411e1bfb
AM
9565 lgot_ents = elf_local_got_ents (ibfd);
9566 if (!lgot_ents)
65f38f15
AM
9567 continue;
9568
0ffa91dd 9569 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9570 locsymcount = symtab_hdr->sh_info;
411e1bfb 9571 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9572 local_plt = (struct plt_entry **) end_lgot_ents;
9573 end_local_plt = local_plt + locsymcount;
f961d9dd 9574 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9575 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9576 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9577 {
0b8bcf0d 9578 struct got_entry **pent, *ent;
411e1bfb 9579
0b8bcf0d
AM
9580 pent = lgot_ents;
9581 while ((ent = *pent) != NULL)
411e1bfb
AM
9582 if (ent->got.refcount > 0)
9583 {
e7b938ca 9584 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9585 {
927be08e 9586 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9587 *pent = ent->next;
411e1bfb
AM
9588 }
9589 else
9590 {
19e08130
AM
9591 unsigned int ent_size = 8;
9592 unsigned int rel_size = sizeof (Elf64_External_Rela);
9593
eea6121a 9594 ent->got.offset = s->size;
e7b938ca 9595 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9596 {
19e08130
AM
9597 ent_size *= 2;
9598 rel_size *= 2;
9599 }
9600 s->size += ent_size;
9601 if ((*lgot_masks & PLT_IFUNC) != 0)
9602 {
9603 htab->reliplt->size += rel_size;
9604 htab->got_reli_size += rel_size;
9605 }
9606 else if (info->shared)
9607 {
9608 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9609 srel->size += rel_size;
927be08e 9610 }
0b8bcf0d 9611 pent = &ent->next;
411e1bfb
AM
9612 }
9613 }
9614 else
0b8bcf0d 9615 *pent = ent->next;
65f38f15 9616 }
e054468f
AM
9617
9618 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9619 for (; local_plt < end_local_plt; ++local_plt)
9620 {
9621 struct plt_entry *ent;
9622
9623 for (ent = *local_plt; ent != NULL; ent = ent->next)
9624 if (ent->plt.refcount > 0)
9625 {
91d6fa6a 9626 s = htab->iplt;
e054468f 9627 ent->plt.offset = s->size;
b9e5796b 9628 s->size += PLT_ENTRY_SIZE (htab);
e054468f 9629
25f23106 9630 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9631 }
9632 else
9633 ent->plt.offset = (bfd_vma) -1;
9634 }
65f38f15
AM
9635 }
9636
9637 /* Allocate global sym .plt and .got entries, and space for global
9638 sym dynamic relocs. */
4ce794b7 9639 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 9640
7865406b 9641 first_tlsld = NULL;
102890f0
AM
9642 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9643 {
7865406b
AM
9644 struct got_entry *ent;
9645
0c8d6e5c 9646 if (!is_ppc64_elf (ibfd))
102890f0
AM
9647 continue;
9648
7865406b
AM
9649 ent = ppc64_tlsld_got (ibfd);
9650 if (ent->got.refcount > 0)
102890f0 9651 {
7865406b 9652 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9653 {
7865406b
AM
9654 ent->is_indirect = TRUE;
9655 ent->got.ent = first_tlsld;
9656 }
9657 else
9658 {
9659 if (first_tlsld == NULL)
9660 first_tlsld = ent;
9661 s = ppc64_elf_tdata (ibfd)->got;
9662 ent->got.offset = s->size;
9663 ent->owner = ibfd;
9664 s->size += 16;
9665 if (info->shared)
9666 {
9667 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9668 srel->size += sizeof (Elf64_External_Rela);
9669 }
102890f0
AM
9670 }
9671 }
9672 else
7865406b 9673 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9674 }
9675
65f38f15
AM
9676 /* We now have determined the sizes of the various dynamic sections.
9677 Allocate memory for them. */
b34976b6 9678 relocs = FALSE;
65f38f15
AM
9679 for (s = dynobj->sections; s != NULL; s = s->next)
9680 {
9681 if ((s->flags & SEC_LINKER_CREATED) == 0)
9682 continue;
9683
4ce794b7 9684 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9685 /* These haven't been allocated yet; don't strip. */
9686 continue;
e717da7e
AM
9687 else if (s == htab->got
9688 || s == htab->plt
e054468f 9689 || s == htab->iplt
c456f082
AM
9690 || s == htab->glink
9691 || s == htab->dynbss)
65f38f15
AM
9692 {
9693 /* Strip this section if we don't need it; see the
9694 comment below. */
5bd4f169 9695 }
58d180e8
AM
9696 else if (s == htab->glink_eh_frame)
9697 {
9698 if (!bfd_is_abs_section (s->output_section))
9699 /* Not sized yet. */
9700 continue;
9701 }
70cc837d 9702 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9703 {
c456f082 9704 if (s->size != 0)
5bd4f169 9705 {
4ce794b7 9706 if (s != htab->relplt)
b34976b6 9707 relocs = TRUE;
5bd4f169
AM
9708
9709 /* We use the reloc_count field as a counter if we need
9710 to copy relocs into the output file. */
9711 s->reloc_count = 0;
9712 }
9713 }
65f38f15 9714 else
5bd4f169
AM
9715 {
9716 /* It's not one of our sections, so don't allocate space. */
9717 continue;
9718 }
9719
eea6121a 9720 if (s->size == 0)
5bd4f169 9721 {
c456f082
AM
9722 /* If we don't need this section, strip it from the
9723 output file. This is mostly to handle .rela.bss and
9724 .rela.plt. We must create both sections in
9725 create_dynamic_sections, because they must be created
9726 before the linker maps input sections to output
9727 sections. The linker does that before
9728 adjust_dynamic_symbol is called, and it is that
9729 function which decides whether anything needs to go
9730 into these sections. */
8423293d 9731 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9732 continue;
9733 }
9734
c456f082 9735 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9736 continue;
9737
65f38f15
AM
9738 /* Allocate memory for the section contents. We use bfd_zalloc
9739 here in case unused entries are not reclaimed before the
9740 section's contents are written out. This should not happen,
411e1bfb
AM
9741 but this way if it does we get a R_PPC64_NONE reloc in .rela
9742 sections instead of garbage.
9743 We also rely on the section contents being zero when writing
9744 the GOT. */
eea6121a 9745 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9746 if (s->contents == NULL)
b34976b6 9747 return FALSE;
5bd4f169
AM
9748 }
9749
e717da7e
AM
9750 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9751 {
0c8d6e5c 9752 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9753 continue;
9754
e717da7e
AM
9755 s = ppc64_elf_tdata (ibfd)->got;
9756 if (s != NULL && s != htab->got)
9757 {
eea6121a 9758 if (s->size == 0)
8423293d 9759 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9760 else
9761 {
eea6121a 9762 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9763 if (s->contents == NULL)
9764 return FALSE;
9765 }
9766 }
9767 s = ppc64_elf_tdata (ibfd)->relgot;
9768 if (s != NULL)
9769 {
eea6121a 9770 if (s->size == 0)
8423293d 9771 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9772 else
9773 {
eea6121a 9774 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9775 if (s->contents == NULL)
9776 return FALSE;
9777 relocs = TRUE;
9778 s->reloc_count = 0;
9779 }
9780 }
9781 }
9782
e86ce104 9783 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9784 {
9785 /* Add some entries to the .dynamic section. We fill in the
9786 values later, in ppc64_elf_finish_dynamic_sections, but we
9787 must add the entries now so that we get the correct size for
9788 the .dynamic section. The DT_DEBUG entry is filled in by the
9789 dynamic linker and used by the debugger. */
dc810e39 9790#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9791 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9792
36af4a4e 9793 if (info->executable)
5bd4f169 9794 {
dc810e39 9795 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9796 return FALSE;
5bd4f169
AM
9797 }
9798
eea6121a 9799 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9800 {
dc810e39
AM
9801 if (!add_dynamic_entry (DT_PLTGOT, 0)
9802 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9803 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9804 || !add_dynamic_entry (DT_JMPREL, 0)
9805 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9806 return FALSE;
5bd4f169
AM
9807 }
9808
ee67d69a 9809 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9810 {
9811 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9812 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9813 return FALSE;
19397422
AM
9814 }
9815
a7f2871e
AM
9816 if (!htab->no_tls_get_addr_opt
9817 && htab->tls_get_addr_fd != NULL
9818 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9819 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9820 return FALSE;
9821
5bd4f169
AM
9822 if (relocs)
9823 {
dc810e39
AM
9824 if (!add_dynamic_entry (DT_RELA, 0)
9825 || !add_dynamic_entry (DT_RELASZ, 0)
9826 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9827 return FALSE;
5bd4f169 9828
65f38f15
AM
9829 /* If any dynamic relocs apply to a read-only section,
9830 then we need a DT_TEXTREL entry. */
248866a8 9831 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9832 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9833
65f38f15 9834 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9835 {
65f38f15 9836 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9837 return FALSE;
5bd4f169 9838 }
5bd4f169 9839 }
5bd4f169 9840 }
65f38f15 9841#undef add_dynamic_entry
5bd4f169 9842
b34976b6 9843 return TRUE;
5bd4f169
AM
9844}
9845
721956f4 9846/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9847
4ce794b7
AM
9848static inline enum ppc_stub_type
9849ppc_type_of_stub (asection *input_sec,
9850 const Elf_Internal_Rela *rel,
9851 struct ppc_link_hash_entry **hash,
e054468f 9852 struct plt_entry **plt_ent,
6911b7dc
AM
9853 bfd_vma destination,
9854 unsigned long local_off)
5bd4f169 9855{
721956f4
AM
9856 struct ppc_link_hash_entry *h = *hash;
9857 bfd_vma location;
9858 bfd_vma branch_offset;
9859 bfd_vma max_branch_offset;
4ce794b7 9860 enum elf_ppc64_reloc_type r_type;
5bd4f169 9861
721956f4
AM
9862 if (h != NULL)
9863 {
e054468f 9864 struct plt_entry *ent;
7fe2b9a6 9865 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9866 if (h->oh != NULL
9867 && h->oh->is_func_descriptor)
7b8f6675
AM
9868 {
9869 fdh = ppc_follow_link (h->oh);
9870 *hash = fdh;
9871 }
8387904d 9872
e054468f
AM
9873 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9874 if (ent->addend == rel->r_addend
9875 && ent->plt.offset != (bfd_vma) -1)
9876 {
e054468f
AM
9877 *plt_ent = ent;
9878 return ppc_stub_plt_call;
9879 }
5bd4f169 9880
7fe2b9a6
AM
9881 /* Here, we know we don't have a plt entry. If we don't have a
9882 either a defined function descriptor or a defined entry symbol
9883 in a regular object file, then it is pointless trying to make
9884 any other type of stub. */
854b41e7
AM
9885 if (!is_static_defined (&fdh->elf)
9886 && !is_static_defined (&h->elf))
721956f4 9887 return ppc_stub_none;
5d1634d7 9888 }
e054468f
AM
9889 else if (elf_local_got_ents (input_sec->owner) != NULL)
9890 {
9891 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9892 struct plt_entry **local_plt = (struct plt_entry **)
9893 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9894 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9895
9896 if (local_plt[r_symndx] != NULL)
9897 {
9898 struct plt_entry *ent;
9899
9900 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9901 if (ent->addend == rel->r_addend
9902 && ent->plt.offset != (bfd_vma) -1)
9903 {
9904 *plt_ent = ent;
9905 return ppc_stub_plt_call;
9906 }
9907 }
9908 }
5d1634d7 9909
721956f4
AM
9910 /* Determine where the call point is. */
9911 location = (input_sec->output_offset
9912 + input_sec->output_section->vma
9913 + rel->r_offset);
5d1634d7 9914
721956f4
AM
9915 branch_offset = destination - location;
9916 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9917
721956f4
AM
9918 /* Determine if a long branch stub is needed. */
9919 max_branch_offset = 1 << 25;
4ce794b7 9920 if (r_type != R_PPC64_REL24)
721956f4 9921 max_branch_offset = 1 << 15;
5d1634d7 9922
6911b7dc 9923 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
9924 /* We need a stub. Figure out whether a long_branch or plt_branch
9925 is needed later. */
9926 return ppc_stub_long_branch;
5d1634d7 9927
721956f4 9928 return ppc_stub_none;
5d1634d7
AM
9929}
9930
794e51c0
AM
9931/* With power7 weakly ordered memory model, it is possible for ld.so
9932 to update a plt entry in one thread and have another thread see a
9933 stale zero toc entry. To avoid this we need some sort of acquire
9934 barrier in the call stub. One solution is to make the load of the
9935 toc word seem to appear to depend on the load of the function entry
9936 word. Another solution is to test for r2 being zero, and branch to
9937 the appropriate glink entry if so.
9938
9939 . fake dep barrier compare
71a39c98
AM
9940 . ld 12,xxx(2) ld 12,xxx(2)
9941 . mtctr 12 mtctr 12
9942 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
9943 . add 2,2,11 cmpldi 2,0
9944 . ld 2,xxx+8(2) bnectr+
9945 . bctr b <glink_entry>
9946
9947 The solution involving the compare turns out to be faster, so
9948 that's what we use unless the branch won't reach. */
9949
9950#define ALWAYS_USE_FAKE_DEP 0
9951#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 9952
5d1634d7
AM
9953#define PPC_LO(v) ((v) & 0xffff)
9954#define PPC_HI(v) (((v) >> 16) & 0xffff)
9955#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9956
794e51c0
AM
9957static inline unsigned int
9958plt_stub_size (struct ppc_link_hash_table *htab,
9959 struct ppc_stub_hash_entry *stub_entry,
9960 bfd_vma off)
9961{
b9e5796b
AM
9962 unsigned size = 12;
9963
9964 if (ALWAYS_EMIT_R2SAVE
9965 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9966 size += 4;
9967 if (PPC_HA (off) != 0)
794e51c0 9968 size += 4;
b9e5796b
AM
9969 if (htab->opd_abi)
9970 {
9971 size += 4;
9972 if (htab->plt_static_chain)
9973 size += 4;
9974 if (htab->plt_thread_safe)
9975 size += 8;
9976 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9977 size += 4;
9978 }
794e51c0
AM
9979 if (stub_entry->h != NULL
9980 && (stub_entry->h == htab->tls_get_addr_fd
9981 || stub_entry->h == htab->tls_get_addr)
9982 && !htab->no_tls_get_addr_opt)
9983 size += 13 * 4;
9984 return size;
9985}
9986
9987/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9988 then return the padding needed to do so. */
9989static inline unsigned int
9990plt_stub_pad (struct ppc_link_hash_table *htab,
9991 struct ppc_stub_hash_entry *stub_entry,
9992 bfd_vma plt_off)
9993{
9994 int stub_align = 1 << htab->plt_stub_align;
9995 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9996 bfd_vma stub_off = stub_entry->stub_sec->size;
9997
9998 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9999 > (stub_size & -stub_align))
10000 return stub_align - (stub_off & (stub_align - 1));
10001 return 0;
10002}
10003
10004/* Build a .plt call stub. */
10005
10006static inline bfd_byte *
10007build_plt_stub (struct ppc_link_hash_table *htab,
10008 struct ppc_stub_hash_entry *stub_entry,
10009 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10010{
10011 bfd *obfd = htab->stub_bfd;
b9e5796b 10012 bfd_boolean plt_load_toc = htab->opd_abi;
794e51c0
AM
10013 bfd_boolean plt_static_chain = htab->plt_static_chain;
10014 bfd_boolean plt_thread_safe = htab->plt_thread_safe;
10015 bfd_boolean use_fake_dep = plt_thread_safe;
10016 bfd_vma cmp_branch_off = 0;
10017
10018 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10019 && plt_load_toc
794e51c0
AM
10020 && plt_thread_safe
10021 && !(stub_entry->h != NULL
10022 && (stub_entry->h == htab->tls_get_addr_fd
10023 || stub_entry->h == htab->tls_get_addr)
10024 && !htab->no_tls_get_addr_opt))
10025 {
10026 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10027 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10028 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10029 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10030 bfd_vma to, from;
10031
68d62958
AM
10032 if (pltindex > 32768)
10033 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10034 to = (glinkoff
10035 + htab->glink->output_offset
10036 + htab->glink->output_section->vma);
10037 from = (p - stub_entry->stub_sec->contents
10038 + 4 * (ALWAYS_EMIT_R2SAVE
10039 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10040 + 4 * (PPC_HA (offset) != 0)
10041 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10042 != PPC_HA (offset))
10043 + 4 * (plt_static_chain != 0)
10044 + 20
10045 + stub_entry->stub_sec->output_offset
10046 + stub_entry->stub_sec->output_section->vma);
10047 cmp_branch_off = to - from;
10048 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10049 }
10050
ac2df442
AM
10051 if (PPC_HA (offset) != 0)
10052 {
176a0d42
AM
10053 if (r != NULL)
10054 {
794e51c0
AM
10055 if (ALWAYS_EMIT_R2SAVE
10056 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10057 r[0].r_offset += 4;
176a0d42 10058 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10059 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10060 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10061 r[1].r_addend = r[0].r_addend;
b9e5796b 10062 if (plt_load_toc)
176a0d42 10063 {
b9e5796b 10064 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10065 {
b9e5796b
AM
10066 r[2].r_offset = r[1].r_offset + 4;
10067 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10068 r[2].r_addend = r[0].r_addend;
10069 }
10070 else
10071 {
10072 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10073 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10074 r[2].r_addend = r[0].r_addend + 8;
10075 if (plt_static_chain)
10076 {
10077 r[3].r_offset = r[2].r_offset + 4;
10078 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10079 r[3].r_addend = r[0].r_addend + 16;
10080 }
c7131b65 10081 }
176a0d42
AM
10082 }
10083 }
794e51c0
AM
10084 if (ALWAYS_EMIT_R2SAVE
10085 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10086 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
71a39c98
AM
10087 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10088 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10089 if (plt_load_toc
10090 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10091 {
71a39c98 10092 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10093 offset = 0;
10094 }
71a39c98 10095 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10096 if (plt_load_toc)
794e51c0 10097 {
b9e5796b
AM
10098 if (use_fake_dep)
10099 {
10100 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10101 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10102 }
10103 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10104 if (plt_static_chain)
10105 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10106 }
ac2df442
AM
10107 }
10108 else
10109 {
176a0d42
AM
10110 if (r != NULL)
10111 {
794e51c0
AM
10112 if (ALWAYS_EMIT_R2SAVE
10113 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10114 r[0].r_offset += 4;
176a0d42 10115 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10116 if (plt_load_toc)
176a0d42 10117 {
b9e5796b 10118 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10119 {
b9e5796b
AM
10120 r[1].r_offset = r[0].r_offset + 4;
10121 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10122 r[1].r_addend = r[0].r_addend;
10123 }
10124 else
10125 {
10126 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10127 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10128 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10129 if (plt_static_chain)
10130 {
10131 r[2].r_offset = r[1].r_offset + 4;
10132 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10133 r[2].r_addend = r[0].r_addend + 8;
10134 }
c7131b65 10135 }
176a0d42
AM
10136 }
10137 }
794e51c0
AM
10138 if (ALWAYS_EMIT_R2SAVE
10139 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10140 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
71a39c98 10141 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10142 if (plt_load_toc
10143 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10144 {
10145 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10146 offset = 0;
10147 }
71a39c98 10148 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10149 if (plt_load_toc)
794e51c0 10150 {
b9e5796b
AM
10151 if (use_fake_dep)
10152 {
10153 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10154 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10155 }
10156 if (plt_static_chain)
10157 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10158 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10159 }
ac2df442 10160 }
b9e5796b 10161 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10162 {
10163 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10164 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10165 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10166 }
10167 else
10168 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10169 return p;
10170}
10171
a7f2871e
AM
10172/* Build a special .plt call stub for __tls_get_addr. */
10173
10174#define LD_R11_0R3 0xe9630000
10175#define LD_R12_0R3 0xe9830000
10176#define MR_R0_R3 0x7c601b78
10177#define CMPDI_R11_0 0x2c2b0000
10178#define ADD_R3_R12_R13 0x7c6c6a14
10179#define BEQLR 0x4d820020
10180#define MR_R3_R0 0x7c030378
10181#define MFLR_R11 0x7d6802a6
10182#define STD_R11_0R1 0xf9610000
10183#define BCTRL 0x4e800421
10184#define LD_R11_0R1 0xe9610000
10185#define LD_R2_0R1 0xe8410000
10186#define MTLR_R11 0x7d6803a6
10187
10188static inline bfd_byte *
794e51c0
AM
10189build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10190 struct ppc_stub_hash_entry *stub_entry,
10191 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10192{
794e51c0
AM
10193 bfd *obfd = htab->stub_bfd;
10194
a7f2871e
AM
10195 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10196 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10197 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10198 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10199 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10200 bfd_put_32 (obfd, BEQLR, p), p += 4;
10201 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10202 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10203 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
10204
10205 if (r != NULL)
10206 r[0].r_offset += 9 * 4;
794e51c0 10207 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10208 bfd_put_32 (obfd, BCTRL, p - 4);
10209
10210 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
10211 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
10212 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10213 bfd_put_32 (obfd, BLR, p), p += 4;
10214
10215 return p;
10216}
10217
176a0d42
AM
10218static Elf_Internal_Rela *
10219get_relocs (asection *sec, int count)
10220{
10221 Elf_Internal_Rela *relocs;
10222 struct bfd_elf_section_data *elfsec_data;
10223
10224 elfsec_data = elf_section_data (sec);
10225 relocs = elfsec_data->relocs;
10226 if (relocs == NULL)
10227 {
10228 bfd_size_type relsize;
10229 relsize = sec->reloc_count * sizeof (*relocs);
10230 relocs = bfd_alloc (sec->owner, relsize);
10231 if (relocs == NULL)
10232 return NULL;
10233 elfsec_data->relocs = relocs;
d4730f92
BS
10234 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10235 sizeof (Elf_Internal_Shdr));
10236 if (elfsec_data->rela.hdr == NULL)
10237 return NULL;
10238 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10239 * sizeof (Elf64_External_Rela));
10240 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10241 sec->reloc_count = 0;
10242 }
10243 relocs += sec->reloc_count;
10244 sec->reloc_count += count;
10245 return relocs;
10246}
10247
aa374f67 10248static bfd_vma
25f53a85 10249get_r2off (struct bfd_link_info *info,
aa374f67
AM
10250 struct ppc_stub_hash_entry *stub_entry)
10251{
25f53a85 10252 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
10253 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10254
10255 if (r2off == 0)
10256 {
10257 /* Support linking -R objects. Get the toc pointer from the
10258 opd entry. */
10259 char buf[8];
b9e5796b
AM
10260 if (!htab->opd_abi)
10261 return r2off;
aa374f67
AM
10262 asection *opd = stub_entry->h->elf.root.u.def.section;
10263 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10264
10265 if (strcmp (opd->name, ".opd") != 0
10266 || opd->reloc_count != 0)
10267 {
bc30df16 10268 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10269 stub_entry->h->elf.root.root.string);
aa374f67
AM
10270 bfd_set_error (bfd_error_bad_value);
10271 return 0;
10272 }
10273 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10274 return 0;
10275 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10276 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
10277 }
10278 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10279 return r2off;
10280}
10281
b34976b6 10282static bfd_boolean
4ce794b7 10283ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10284{
721956f4
AM
10285 struct ppc_stub_hash_entry *stub_entry;
10286 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10287 struct bfd_link_info *info;
10288 struct ppc_link_hash_table *htab;
721956f4
AM
10289 bfd_byte *loc;
10290 bfd_byte *p;
ee75fd95 10291 bfd_vma dest, off;
721956f4 10292 int size;
176a0d42 10293 Elf_Internal_Rela *r;
e054468f 10294 asection *plt;
5d1634d7 10295
721956f4
AM
10296 /* Massage our args to the form they really have. */
10297 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10298 info = in_arg;
5d1634d7 10299
5d1634d7 10300 htab = ppc_hash_table (info);
4dfe6ac6
NC
10301 if (htab == NULL)
10302 return FALSE;
5d1634d7 10303
721956f4 10304 /* Make a note of the offset within the stubs for this entry. */
eea6121a 10305 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 10306 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 10307
4ce794b7 10308 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10309 switch (stub_entry->stub_type)
5d1634d7 10310 {
721956f4 10311 case ppc_stub_long_branch:
ad8e1ba5 10312 case ppc_stub_long_branch_r2off:
721956f4 10313 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10314 dest = (stub_entry->target_value
10315 + stub_entry->target_section->output_offset
10316 + stub_entry->target_section->output_section->vma);
10317 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10318 off = dest;
5d1634d7 10319
721956f4
AM
10320 /* And this is where we are coming from. */
10321 off -= (stub_entry->stub_offset
97b639ba
AM
10322 + stub_entry->stub_sec->output_offset
10323 + stub_entry->stub_sec->output_section->vma);
e86ce104 10324
ac2df442
AM
10325 size = 4;
10326 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10327 {
25f53a85 10328 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10329
aa374f67
AM
10330 if (r2off == 0)
10331 {
10332 htab->stub_error = TRUE;
10333 return FALSE;
10334 }
97b639ba 10335 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 10336 loc += 4;
ac2df442
AM
10337 size = 12;
10338 if (PPC_HA (r2off) != 0)
10339 {
10340 size = 16;
10341 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10342 loc += 4;
10343 }
97b639ba 10344 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 10345 loc += 4;
ac2df442 10346 off -= size - 4;
ad8e1ba5 10347 }
97b639ba 10348 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10349
5c3dead3
AM
10350 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10351 {
bc30df16
AM
10352 info->callbacks->einfo
10353 (_("%P: long branch stub `%s' offset overflow\n"),
10354 stub_entry->root.string);
5c3dead3
AM
10355 htab->stub_error = TRUE;
10356 return FALSE;
10357 }
ee75fd95
AM
10358
10359 if (info->emitrelocations)
10360 {
176a0d42
AM
10361 r = get_relocs (stub_entry->stub_sec, 1);
10362 if (r == NULL)
10363 return FALSE;
ee75fd95
AM
10364 r->r_offset = loc - stub_entry->stub_sec->contents;
10365 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10366 r->r_addend = dest;
10367 if (stub_entry->h != NULL)
10368 {
10369 struct elf_link_hash_entry **hashes;
10370 unsigned long symndx;
10371 struct ppc_link_hash_entry *h;
10372
10373 hashes = elf_sym_hashes (htab->stub_bfd);
10374 if (hashes == NULL)
10375 {
10376 bfd_size_type hsize;
10377
10378 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10379 hashes = bfd_zalloc (htab->stub_bfd, hsize);
10380 if (hashes == NULL)
10381 return FALSE;
10382 elf_sym_hashes (htab->stub_bfd) = hashes;
10383 htab->stub_globals = 1;
10384 }
10385 symndx = htab->stub_globals++;
10386 h = stub_entry->h;
10387 hashes[symndx] = &h->elf;
10388 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10389 if (h->oh != NULL && h->oh->is_func)
b31867b6 10390 h = ppc_follow_link (h->oh);
ee75fd95
AM
10391 if (h->elf.root.u.def.section != stub_entry->target_section)
10392 /* H is an opd symbol. The addend must be zero. */
10393 r->r_addend = 0;
10394 else
10395 {
10396 off = (h->elf.root.u.def.value
10397 + h->elf.root.u.def.section->output_offset
10398 + h->elf.root.u.def.section->output_section->vma);
10399 r->r_addend -= off;
10400 }
10401 }
10402 }
721956f4 10403 break;
e86ce104 10404
721956f4 10405 case ppc_stub_plt_branch:
ad8e1ba5 10406 case ppc_stub_plt_branch_r2off:
721956f4
AM
10407 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10408 stub_entry->root.string + 9,
b34976b6 10409 FALSE, FALSE);
721956f4
AM
10410 if (br_entry == NULL)
10411 {
8de848d8 10412 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10413 stub_entry->root.string);
b34976b6
AM
10414 htab->stub_error = TRUE;
10415 return FALSE;
721956f4
AM
10416 }
10417
176a0d42
AM
10418 dest = (stub_entry->target_value
10419 + stub_entry->target_section->output_offset
10420 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10421 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10422 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10423
176a0d42 10424 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10425 htab->brlt->contents + br_entry->offset);
721956f4 10426
f94498ff 10427 if (br_entry->iter == htab->stub_iteration)
721956f4 10428 {
f94498ff 10429 br_entry->iter = 0;
84f5d08e 10430
f94498ff 10431 if (htab->relbrlt != NULL)
84f5d08e 10432 {
f94498ff
AM
10433 /* Create a reloc for the branch lookup table entry. */
10434 Elf_Internal_Rela rela;
10435 bfd_byte *rl;
10436
10437 rela.r_offset = (br_entry->offset
10438 + htab->brlt->output_offset
10439 + htab->brlt->output_section->vma);
10440 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10441 rela.r_addend = dest;
f94498ff
AM
10442
10443 rl = htab->relbrlt->contents;
10444 rl += (htab->relbrlt->reloc_count++
10445 * sizeof (Elf64_External_Rela));
10446 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10447 }
10448 else if (info->emitrelocations)
10449 {
176a0d42
AM
10450 r = get_relocs (htab->brlt, 1);
10451 if (r == NULL)
10452 return FALSE;
10453 /* brlt, being SEC_LINKER_CREATED does not go through the
10454 normal reloc processing. Symbols and offsets are not
10455 translated from input file to output file form, so
10456 set up the offset per the output file. */
f94498ff
AM
10457 r->r_offset = (br_entry->offset
10458 + htab->brlt->output_offset
10459 + htab->brlt->output_section->vma);
10460 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10461 r->r_addend = dest;
84f5d08e 10462 }
84f5d08e 10463 }
721956f4 10464
176a0d42
AM
10465 dest = (br_entry->offset
10466 + htab->brlt->output_offset
10467 + htab->brlt->output_section->vma);
10468
10469 off = (dest
4ce794b7 10470 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 10471 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10472
ad8e1ba5 10473 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10474 {
25f53a85 10475 info->callbacks->einfo
bc30df16 10476 (_("%P: linkage table error against `%T'\n"),
721956f4 10477 stub_entry->root.string);
5d1634d7 10478 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10479 htab->stub_error = TRUE;
10480 return FALSE;
5d1634d7 10481 }
41bd81ab 10482
176a0d42
AM
10483 if (info->emitrelocations)
10484 {
10485 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10486 if (r == NULL)
10487 return FALSE;
10488 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10489 if (bfd_big_endian (info->output_bfd))
10490 r[0].r_offset += 2;
b9e5796b
AM
10491 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off
10492 && htab->opd_abi)
176a0d42
AM
10493 r[0].r_offset += 4;
10494 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10495 r[0].r_addend = dest;
10496 if (PPC_HA (off) != 0)
10497 {
10498 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10499 r[1].r_offset = r[0].r_offset + 4;
10500 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10501 r[1].r_addend = r[0].r_addend;
10502 }
10503 }
10504
b9e5796b
AM
10505 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10506 || !htab->opd_abi)
ad8e1ba5 10507 {
176a0d42 10508 if (PPC_HA (off) != 0)
ac2df442
AM
10509 {
10510 size = 16;
71a39c98 10511 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
ac2df442 10512 loc += 4;
71a39c98 10513 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
ac2df442
AM
10514 }
10515 else
10516 {
10517 size = 12;
71a39c98 10518 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 10519 }
ad8e1ba5
AM
10520 }
10521 else
10522 {
25f53a85 10523 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
10524
10525 if (r2off == 0)
10526 {
10527 htab->stub_error = TRUE;
10528 return FALSE;
10529 }
ad8e1ba5 10530
97b639ba 10531 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 10532 loc += 4;
ac2df442 10533 size = 20;
176a0d42 10534 if (PPC_HA (off) != 0)
ac2df442
AM
10535 {
10536 size += 4;
71a39c98 10537 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
ac2df442 10538 loc += 4;
71a39c98 10539 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
ac2df442
AM
10540 loc += 4;
10541 }
10542 else
10543 {
71a39c98 10544 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
10545 loc += 4;
10546 }
10547
10548 if (PPC_HA (r2off) != 0)
10549 {
10550 size += 4;
10551 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10552 loc += 4;
10553 }
97b639ba 10554 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
10555 }
10556 loc += 4;
71a39c98 10557 bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 10558 loc += 4;
97b639ba 10559 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 10560 break;
5d1634d7 10561
721956f4 10562 case ppc_stub_plt_call:
794e51c0 10563 case ppc_stub_plt_call_r2save:
e054468f 10564 if (stub_entry->h != NULL
b31867b6
AM
10565 && stub_entry->h->is_func_descriptor
10566 && stub_entry->h->oh != NULL)
c862ae31 10567 {
b31867b6
AM
10568 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10569
10570 /* If the old-ABI "dot-symbol" is undefined make it weak so
10571 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10572 FIXME: We used to define the symbol on one of the call
10573 stubs instead, which is why we test symbol section id
10574 against htab->top_id in various places. Likely all
10575 these checks could now disappear. */
10576 if (fh->elf.root.type == bfd_link_hash_undefined)
10577 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
10578 /* Stop undo_symbol_twiddle changing it back to undefined. */
10579 fh->was_undefined = 0;
c862ae31
AM
10580 }
10581
721956f4 10582 /* Now build the stub. */
e054468f 10583 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 10584 if (dest >= (bfd_vma) -2)
721956f4
AM
10585 abort ();
10586
e054468f 10587 plt = htab->plt;
25f23106
AM
10588 if (!htab->elf.dynamic_sections_created
10589 || stub_entry->h == NULL
10590 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10591 plt = htab->iplt;
10592
10593 dest += plt->output_offset + plt->output_section->vma;
10594
10595 if (stub_entry->h == NULL
10596 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10597 {
10598 Elf_Internal_Rela rela;
10599 bfd_byte *rl;
10600
10601 rela.r_offset = dest;
ee67d69a
AM
10602 if (htab->opd_abi)
10603 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10604 else
10605 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
10606 rela.r_addend = (stub_entry->target_value
10607 + stub_entry->target_section->output_offset
10608 + stub_entry->target_section->output_section->vma);
10609
25f23106
AM
10610 rl = (htab->reliplt->contents
10611 + (htab->reliplt->reloc_count++
10612 * sizeof (Elf64_External_Rela)));
10613 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
10614 stub_entry->plt_ent->plt.offset |= 1;
10615 }
176a0d42
AM
10616
10617 off = (dest
e054468f 10618 - elf_gp (plt->output_section->owner)
176a0d42 10619 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10620
ad8e1ba5 10621 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10622 {
25f53a85 10623 info->callbacks->einfo
bc30df16 10624 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
10625 stub_entry->h != NULL
10626 ? stub_entry->h->elf.root.root.string
10627 : "<local sym>");
721956f4 10628 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10629 htab->stub_error = TRUE;
10630 return FALSE;
721956f4
AM
10631 }
10632
794e51c0
AM
10633 if (htab->plt_stub_align != 0)
10634 {
10635 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10636
10637 stub_entry->stub_sec->size += pad;
10638 stub_entry->stub_offset = stub_entry->stub_sec->size;
10639 loc += pad;
10640 }
10641
176a0d42
AM
10642 r = NULL;
10643 if (info->emitrelocations)
10644 {
10645 r = get_relocs (stub_entry->stub_sec,
c7131b65
AM
10646 (2
10647 + (PPC_HA (off) != 0)
10648 + (htab->plt_static_chain
10649 && PPC_HA (off + 16) == PPC_HA (off))));
176a0d42
AM
10650 if (r == NULL)
10651 return FALSE;
10652 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10653 if (bfd_big_endian (info->output_bfd))
10654 r[0].r_offset += 2;
176a0d42
AM
10655 r[0].r_addend = dest;
10656 }
a7f2871e
AM
10657 if (stub_entry->h != NULL
10658 && (stub_entry->h == htab->tls_get_addr_fd
10659 || stub_entry->h == htab->tls_get_addr)
10660 && !htab->no_tls_get_addr_opt)
794e51c0 10661 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10662 else
794e51c0 10663 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10664 size = p - loc;
10665 break;
10666
10667 default:
10668 BFD_FAIL ();
b34976b6 10669 return FALSE;
721956f4
AM
10670 }
10671
eea6121a 10672 stub_entry->stub_sec->size += size;
97b639ba 10673
ee75fd95 10674 if (htab->emit_stub_syms)
97b639ba
AM
10675 {
10676 struct elf_link_hash_entry *h;
ee75fd95
AM
10677 size_t len1, len2;
10678 char *name;
10679 const char *const stub_str[] = { "long_branch",
10680 "long_branch_r2off",
10681 "plt_branch",
10682 "plt_branch_r2off",
794e51c0 10683 "plt_call",
ee75fd95
AM
10684 "plt_call" };
10685
10686 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10687 len2 = strlen (stub_entry->root.string);
10688 name = bfd_malloc (len1 + len2 + 2);
10689 if (name == NULL)
10690 return FALSE;
10691 memcpy (name, stub_entry->root.string, 9);
10692 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10693 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10694 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10695 if (h == NULL)
10696 return FALSE;
10697 if (h->root.type == bfd_link_hash_new)
10698 {
10699 h->root.type = bfd_link_hash_defined;
10700 h->root.u.def.section = stub_entry->stub_sec;
10701 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10702 h->ref_regular = 1;
10703 h->def_regular = 1;
10704 h->ref_regular_nonweak = 1;
10705 h->forced_local = 1;
10706 h->non_elf = 0;
97b639ba
AM
10707 }
10708 }
10709
b34976b6 10710 return TRUE;
721956f4
AM
10711}
10712
10713/* As above, but don't actually build the stub. Just bump offset so
10714 we know stub section sizes, and select plt_branch stubs where
10715 long_branch stubs won't do. */
10716
b34976b6 10717static bfd_boolean
4ce794b7 10718ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10719{
10720 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10721 struct bfd_link_info *info;
721956f4
AM
10722 struct ppc_link_hash_table *htab;
10723 bfd_vma off;
10724 int size;
10725
10726 /* Massage our args to the form they really have. */
10727 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10728 info = in_arg;
10729
10730 htab = ppc_hash_table (info);
4dfe6ac6
NC
10731 if (htab == NULL)
10732 return FALSE;
721956f4 10733
794e51c0
AM
10734 if (stub_entry->stub_type == ppc_stub_plt_call
10735 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 10736 {
e054468f
AM
10737 asection *plt;
10738 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 10739 if (off >= (bfd_vma) -2)
411e1bfb 10740 abort ();
e054468f 10741 plt = htab->plt;
25f23106
AM
10742 if (!htab->elf.dynamic_sections_created
10743 || stub_entry->h == NULL
10744 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10745 plt = htab->iplt;
10746 off += (plt->output_offset
10747 + plt->output_section->vma
10748 - elf_gp (plt->output_section->owner)
ad8e1ba5 10749 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10750
794e51c0
AM
10751 size = plt_stub_size (htab, stub_entry, off);
10752 if (htab->plt_stub_align)
10753 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
10754 if (info->emitrelocations)
10755 {
10756 stub_entry->stub_sec->reloc_count
b9e5796b
AM
10757 += ((PPC_HA (off) != 0)
10758 + (htab->opd_abi
10759 ? 2 + (htab->plt_static_chain
10760 && PPC_HA (off + 16) == PPC_HA (off))
10761 : 1));
176a0d42
AM
10762 stub_entry->stub_sec->flags |= SEC_RELOC;
10763 }
721956f4
AM
10764 }
10765 else
10766 {
ad8e1ba5
AM
10767 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10768 variants. */
ac2df442 10769 bfd_vma r2off = 0;
6911b7dc 10770 bfd_vma local_off = 0;
ac2df442 10771
721956f4
AM
10772 off = (stub_entry->target_value
10773 + stub_entry->target_section->output_offset
10774 + stub_entry->target_section->output_section->vma);
eea6121a 10775 off -= (stub_entry->stub_sec->size
721956f4
AM
10776 + stub_entry->stub_sec->output_offset
10777 + stub_entry->stub_sec->output_section->vma);
10778
ad8e1ba5
AM
10779 /* Reset the stub type from the plt variant in case we now
10780 can reach with a shorter stub. */
10781 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10782 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10783
10784 size = 4;
10785 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10786 {
25f53a85 10787 r2off = get_r2off (info, stub_entry);
b9e5796b 10788 if (r2off == 0 && htab->opd_abi)
aa374f67
AM
10789 {
10790 htab->stub_error = TRUE;
10791 return FALSE;
10792 }
ac2df442
AM
10793 size = 12;
10794 if (PPC_HA (r2off) != 0)
10795 size = 16;
10796 off -= size - 4;
ad8e1ba5
AM
10797 }
10798
6911b7dc
AM
10799 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10800
b9e5796b
AM
10801 /* If the branch offset if too big, use a ppc_stub_plt_branch.
10802 Do the same for -R objects without function descriptors. */
10803 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
10804 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
10805 && r2off == 0))
721956f4
AM
10806 {
10807 struct ppc_branch_hash_entry *br_entry;
10808
10809 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10810 stub_entry->root.string + 9,
b34976b6 10811 TRUE, FALSE);
721956f4
AM
10812 if (br_entry == NULL)
10813 {
8de848d8 10814 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 10815 stub_entry->root.string);
b34976b6
AM
10816 htab->stub_error = TRUE;
10817 return FALSE;
721956f4
AM
10818 }
10819
10820 if (br_entry->iter != htab->stub_iteration)
10821 {
10822 br_entry->iter = htab->stub_iteration;
eea6121a
AM
10823 br_entry->offset = htab->brlt->size;
10824 htab->brlt->size += 8;
63bc6f6c 10825
ee75fd95 10826 if (htab->relbrlt != NULL)
eea6121a 10827 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
10828 else if (info->emitrelocations)
10829 {
10830 htab->brlt->reloc_count += 1;
10831 htab->brlt->flags |= SEC_RELOC;
10832 }
721956f4 10833 }
ad8e1ba5
AM
10834
10835 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
10836 off = (br_entry->offset
10837 + htab->brlt->output_offset
10838 + htab->brlt->output_section->vma
10839 - elf_gp (htab->brlt->output_section->owner)
10840 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10841
176a0d42
AM
10842 if (info->emitrelocations)
10843 {
10844 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10845 stub_entry->stub_sec->flags |= SEC_RELOC;
10846 }
10847
b9e5796b
AM
10848 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10849 || !htab->opd_abi)
ac2df442
AM
10850 {
10851 size = 12;
176a0d42 10852 if (PPC_HA (off) != 0)
ac2df442
AM
10853 size = 16;
10854 }
10855 else
10856 {
10857 size = 20;
176a0d42 10858 if (PPC_HA (off) != 0)
ac2df442
AM
10859 size += 4;
10860
10861 if (PPC_HA (r2off) != 0)
10862 size += 4;
10863 }
721956f4 10864 }
84f5d08e
AM
10865 else if (info->emitrelocations)
10866 {
10867 stub_entry->stub_sec->reloc_count += 1;
10868 stub_entry->stub_sec->flags |= SEC_RELOC;
10869 }
721956f4
AM
10870 }
10871
eea6121a 10872 stub_entry->stub_sec->size += size;
b34976b6 10873 return TRUE;
721956f4
AM
10874}
10875
10876/* Set up various things so that we can make a list of input sections
10877 for each output section included in the link. Returns -1 on error,
cedb70c5 10878 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10879
10880int
927be08e
AM
10881ppc64_elf_setup_section_lists
10882 (struct bfd_link_info *info,
10883 asection *(*add_stub_section) (const char *, asection *),
10884 void (*layout_sections_again) (void))
721956f4
AM
10885{
10886 bfd *input_bfd;
734b6cf9 10887 int top_id, top_index, id;
721956f4 10888 asection *section;
734b6cf9 10889 asection **input_list;
721956f4
AM
10890 bfd_size_type amt;
10891 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10892
4dfe6ac6
NC
10893 if (htab == NULL)
10894 return -1;
927be08e
AM
10895 /* Stash our params away. */
10896 htab->add_stub_section = add_stub_section;
10897 htab->layout_sections_again = layout_sections_again;
4c52953f 10898
1e2f5b6e 10899 /* Find the top input section id. */
3d6f9012 10900 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10901 input_bfd != NULL;
10902 input_bfd = input_bfd->link_next)
10903 {
721956f4
AM
10904 for (section = input_bfd->sections;
10905 section != NULL;
10906 section = section->next)
10907 {
10908 if (top_id < section->id)
10909 top_id = section->id;
10910 }
10911 }
721956f4 10912
8f3bab57 10913 htab->top_id = top_id;
721956f4 10914 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10915 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10916 if (htab->stub_group == NULL)
10917 return -1;
10918
3d6f9012
AM
10919 /* Set toc_off for com, und, abs and ind sections. */
10920 for (id = 0; id < 3; id++)
10921 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10922
734b6cf9
AM
10923 /* We can't use output_bfd->section_count here to find the top output
10924 section index as some sections may have been removed, and
8423293d 10925 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10926 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10927 section != NULL;
10928 section = section->next)
10929 {
10930 if (top_index < section->index)
10931 top_index = section->index;
10932 }
10933
10934 htab->top_index = top_index;
10935 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10936 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10937 htab->input_list = input_list;
10938 if (input_list == NULL)
10939 return -1;
10940
721956f4
AM
10941 return 1;
10942}
10943
927be08e
AM
10944/* Set up for first pass at multitoc partitioning. */
10945
10946void
10947ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10948{
10949 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10950
1c865ab2 10951 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
10952 htab->toc_bfd = NULL;
10953 htab->toc_first_sec = NULL;
10954}
10955
e717da7e
AM
10956/* The linker repeatedly calls this function for each TOC input section
10957 and linker generated GOT section. Group input bfds such that the toc
927be08e 10958 within a group is less than 64k in size. */
ad8e1ba5 10959
927be08e 10960bfd_boolean
4ce794b7 10961ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10962{
10963 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10964 bfd_vma addr, off, limit;
ad8e1ba5 10965
4dfe6ac6
NC
10966 if (htab == NULL)
10967 return FALSE;
10968
927be08e 10969 if (!htab->second_toc_pass)
4c52953f 10970 {
927be08e 10971 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
10972 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10973
10974 if (new_bfd)
bf102f86
AM
10975 {
10976 htab->toc_bfd = isec->owner;
10977 htab->toc_first_sec = isec;
10978 }
927be08e 10979
bf102f86
AM
10980 addr = isec->output_offset + isec->output_section->vma;
10981 off = addr - htab->toc_curr;
d77c8a4b
AM
10982 limit = 0x80008000;
10983 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10984 limit = 0x10000;
10985 if (off + isec->size > limit)
bf102f86
AM
10986 {
10987 addr = (htab->toc_first_sec->output_offset
10988 + htab->toc_first_sec->output_section->vma);
10989 htab->toc_curr = addr;
10990 }
99877b66 10991
927be08e
AM
10992 /* toc_curr is the base address of this toc group. Set elf_gp
10993 for the input section to be the offset relative to the
10994 output toc base plus 0x8000. Making the input elf_gp an
10995 offset allows us to move the toc as a whole without
10996 recalculating input elf_gp. */
10997 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10998 off += TOC_BASE_OFF;
10999
11000 /* Die if someone uses a linker script that doesn't keep input
11001 file .toc and .got together. */
a4fd3de5
AM
11002 if (new_bfd
11003 && elf_gp (isec->owner) != 0
927be08e
AM
11004 && elf_gp (isec->owner) != off)
11005 return FALSE;
11006
11007 elf_gp (isec->owner) = off;
11008 return TRUE;
4c52953f 11009 }
927be08e
AM
11010
11011 /* During the second pass toc_first_sec points to the start of
11012 a toc group, and toc_curr is used to track the old elf_gp.
11013 We use toc_bfd to ensure we only look at each bfd once. */
11014 if (htab->toc_bfd == isec->owner)
11015 return TRUE;
11016 htab->toc_bfd = isec->owner;
11017
11018 if (htab->toc_first_sec == NULL
11019 || htab->toc_curr != elf_gp (isec->owner))
11020 {
11021 htab->toc_curr = elf_gp (isec->owner);
11022 htab->toc_first_sec = isec;
11023 }
11024 addr = (htab->toc_first_sec->output_offset
11025 + htab->toc_first_sec->output_section->vma);
11026 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11027 elf_gp (isec->owner) = off;
11028
11029 return TRUE;
ad8e1ba5
AM
11030}
11031
927be08e
AM
11032/* Called via elf_link_hash_traverse to merge GOT entries for global
11033 symbol H. */
11034
11035static bfd_boolean
11036merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11037{
11038 if (h->root.type == bfd_link_hash_indirect)
11039 return TRUE;
11040
927be08e
AM
11041 merge_got_entries (&h->got.glist);
11042
11043 return TRUE;
11044}
11045
11046/* Called via elf_link_hash_traverse to allocate GOT entries for global
11047 symbol H. */
11048
11049static bfd_boolean
11050reallocate_got (struct elf_link_hash_entry *h, void *inf)
11051{
11052 struct got_entry *gent;
11053
11054 if (h->root.type == bfd_link_hash_indirect)
11055 return TRUE;
11056
927be08e
AM
11057 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11058 if (!gent->is_indirect)
11059 allocate_got (h, (struct bfd_link_info *) inf, gent);
11060 return TRUE;
11061}
11062
11063/* Called on the first multitoc pass after the last call to
11064 ppc64_elf_next_toc_section. This function removes duplicate GOT
11065 entries. */
11066
11067bfd_boolean
11068ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11069{
11070 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11071 struct bfd *ibfd, *ibfd2;
11072 bfd_boolean done_something;
11073
11074 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11075
7865406b
AM
11076 if (!htab->do_multi_toc)
11077 return FALSE;
11078
d0fae19d 11079 /* Merge global sym got entries within a toc group. */
927be08e
AM
11080 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11081
11082 /* And tlsld_got. */
11083 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11084 {
11085 struct got_entry *ent, *ent2;
11086
11087 if (!is_ppc64_elf (ibfd))
11088 continue;
11089
11090 ent = ppc64_tlsld_got (ibfd);
11091 if (!ent->is_indirect
11092 && ent->got.offset != (bfd_vma) -1)
11093 {
11094 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11095 {
11096 if (!is_ppc64_elf (ibfd2))
11097 continue;
11098
11099 ent2 = ppc64_tlsld_got (ibfd2);
11100 if (!ent2->is_indirect
11101 && ent2->got.offset != (bfd_vma) -1
11102 && elf_gp (ibfd2) == elf_gp (ibfd))
11103 {
11104 ent2->is_indirect = TRUE;
11105 ent2->got.ent = ent;
11106 }
11107 }
11108 }
11109 }
11110
11111 /* Zap sizes of got sections. */
11112 htab->reliplt->rawsize = htab->reliplt->size;
11113 htab->reliplt->size -= htab->got_reli_size;
11114 htab->got_reli_size = 0;
11115
11116 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11117 {
11118 asection *got, *relgot;
11119
11120 if (!is_ppc64_elf (ibfd))
11121 continue;
11122
11123 got = ppc64_elf_tdata (ibfd)->got;
11124 if (got != NULL)
11125 {
11126 got->rawsize = got->size;
11127 got->size = 0;
11128 relgot = ppc64_elf_tdata (ibfd)->relgot;
11129 relgot->rawsize = relgot->size;
11130 relgot->size = 0;
11131 }
11132 }
11133
11134 /* Now reallocate the got, local syms first. We don't need to
11135 allocate section contents again since we never increase size. */
11136 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11137 {
11138 struct got_entry **lgot_ents;
11139 struct got_entry **end_lgot_ents;
11140 struct plt_entry **local_plt;
11141 struct plt_entry **end_local_plt;
f961d9dd 11142 unsigned char *lgot_masks;
927be08e
AM
11143 bfd_size_type locsymcount;
11144 Elf_Internal_Shdr *symtab_hdr;
19e08130 11145 asection *s;
927be08e
AM
11146
11147 if (!is_ppc64_elf (ibfd))
11148 continue;
11149
11150 lgot_ents = elf_local_got_ents (ibfd);
11151 if (!lgot_ents)
11152 continue;
11153
11154 symtab_hdr = &elf_symtab_hdr (ibfd);
11155 locsymcount = symtab_hdr->sh_info;
11156 end_lgot_ents = lgot_ents + locsymcount;
11157 local_plt = (struct plt_entry **) end_lgot_ents;
11158 end_local_plt = local_plt + locsymcount;
f961d9dd 11159 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11160 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11161 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11162 {
11163 struct got_entry *ent;
11164
11165 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11166 {
19e08130
AM
11167 unsigned int ent_size = 8;
11168 unsigned int rel_size = sizeof (Elf64_External_Rela);
11169
d0fae19d
AM
11170 ent->got.offset = s->size;
11171 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11172 {
19e08130
AM
11173 ent_size *= 2;
11174 rel_size *= 2;
11175 }
11176 s->size += ent_size;
11177 if ((*lgot_masks & PLT_IFUNC) != 0)
11178 {
11179 htab->reliplt->size += rel_size;
11180 htab->got_reli_size += rel_size;
11181 }
11182 else if (info->shared)
11183 {
11184 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11185 srel->size += rel_size;
d0fae19d
AM
11186 }
11187 }
927be08e
AM
11188 }
11189 }
11190
11191 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11192
11193 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11194 {
11195 struct got_entry *ent;
11196
11197 if (!is_ppc64_elf (ibfd))
11198 continue;
11199
11200 ent = ppc64_tlsld_got (ibfd);
11201 if (!ent->is_indirect
11202 && ent->got.offset != (bfd_vma) -1)
11203 {
11204 asection *s = ppc64_elf_tdata (ibfd)->got;
11205 ent->got.offset = s->size;
11206 s->size += 16;
11207 if (info->shared)
11208 {
11209 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11210 srel->size += sizeof (Elf64_External_Rela);
11211 }
11212 }
11213 }
11214
11215 done_something = htab->reliplt->rawsize != htab->reliplt->size;
11216 if (!done_something)
11217 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11218 {
11219 asection *got;
11220
11221 if (!is_ppc64_elf (ibfd))
11222 continue;
11223
11224 got = ppc64_elf_tdata (ibfd)->got;
11225 if (got != NULL)
11226 {
11227 done_something = got->rawsize != got->size;
11228 if (done_something)
11229 break;
11230 }
11231 }
11232
11233 if (done_something)
11234 (*htab->layout_sections_again) ();
11235
11236 /* Set up for second pass over toc sections to recalculate elf_gp
11237 on input sections. */
11238 htab->toc_bfd = NULL;
11239 htab->toc_first_sec = NULL;
11240 htab->second_toc_pass = TRUE;
11241 return done_something;
11242}
11243
11244/* Called after second pass of multitoc partitioning. */
11245
11246void
11247ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11248{
11249 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11250
11251 /* After the second pass, toc_curr tracks the TOC offset used
11252 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11253 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11254}
11255
9b5ecbd0
AM
11256/* No toc references were found in ISEC. If the code in ISEC makes no
11257 calls, then there's no need to use toc adjusting stubs when branching
11258 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11259 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11260 needed, and 2 if a cyclical call-graph was found but no other reason
11261 for a stub was detected. If called from the top level, a return of
11262 2 means the same as a return of 0. */
9b5ecbd0
AM
11263
11264static int
4ce794b7 11265toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11266{
9b5ecbd0 11267 int ret;
70cc837d
AM
11268
11269 /* Mark this section as checked. */
11270 isec->call_check_done = 1;
9b5ecbd0 11271
772119ce
AM
11272 /* We know none of our code bearing sections will need toc stubs. */
11273 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11274 return 0;
11275
eea6121a 11276 if (isec->size == 0)
082c50f8
AM
11277 return 0;
11278
4c52953f
AM
11279 if (isec->output_section == NULL)
11280 return 0;
11281
4c52953f 11282 ret = 0;
70cc837d 11283 if (isec->reloc_count != 0)
9b5ecbd0 11284 {
70cc837d
AM
11285 Elf_Internal_Rela *relstart, *rel;
11286 Elf_Internal_Sym *local_syms;
11287 struct ppc_link_hash_table *htab;
2917689a 11288
70cc837d
AM
11289 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11290 info->keep_memory);
11291 if (relstart == NULL)
11292 return -1;
90aecf7a 11293
70cc837d
AM
11294 /* Look for branches to outside of this section. */
11295 local_syms = NULL;
11296 htab = ppc_hash_table (info);
11297 if (htab == NULL)
11298 return -1;
4c52953f 11299
70cc837d 11300 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11301 {
70cc837d
AM
11302 enum elf_ppc64_reloc_type r_type;
11303 unsigned long r_symndx;
11304 struct elf_link_hash_entry *h;
11305 struct ppc_link_hash_entry *eh;
11306 Elf_Internal_Sym *sym;
11307 asection *sym_sec;
11308 struct _opd_sec_data *opd;
11309 bfd_vma sym_value;
11310 bfd_vma dest;
11311
11312 r_type = ELF64_R_TYPE (rel->r_info);
11313 if (r_type != R_PPC64_REL24
11314 && r_type != R_PPC64_REL14
11315 && r_type != R_PPC64_REL14_BRTAKEN
11316 && r_type != R_PPC64_REL14_BRNTAKEN)
11317 continue;
4c52953f 11318
70cc837d
AM
11319 r_symndx = ELF64_R_SYM (rel->r_info);
11320 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11321 isec->owner))
4c52953f 11322 {
70cc837d
AM
11323 ret = -1;
11324 break;
11325 }
4c52953f 11326
70cc837d
AM
11327 /* Calls to dynamic lib functions go through a plt call stub
11328 that uses r2. */
11329 eh = (struct ppc_link_hash_entry *) h;
11330 if (eh != NULL
11331 && (eh->elf.plt.plist != NULL
11332 || (eh->oh != NULL
11333 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11334 {
11335 ret = 1;
11336 break;
4c52953f
AM
11337 }
11338
70cc837d
AM
11339 if (sym_sec == NULL)
11340 /* Ignore other undefined symbols. */
4c52953f 11341 continue;
4c52953f 11342
70cc837d
AM
11343 /* Assume branches to other sections not included in the
11344 link need stubs too, to cover -R and absolute syms. */
11345 if (sym_sec->output_section == NULL)
11346 {
11347 ret = 1;
11348 break;
11349 }
4c52953f 11350
70cc837d
AM
11351 if (h == NULL)
11352 sym_value = sym->st_value;
11353 else
11354 {
11355 if (h->root.type != bfd_link_hash_defined
11356 && h->root.type != bfd_link_hash_defweak)
11357 abort ();
11358 sym_value = h->root.u.def.value;
11359 }
11360 sym_value += rel->r_addend;
4c52953f 11361
70cc837d
AM
11362 /* If this branch reloc uses an opd sym, find the code section. */
11363 opd = get_opd_info (sym_sec);
11364 if (opd != NULL)
11365 {
11366 if (h == NULL && opd->adjust != NULL)
11367 {
11368 long adjust;
4c52953f 11369
70cc837d
AM
11370 adjust = opd->adjust[sym->st_value / 8];
11371 if (adjust == -1)
11372 /* Assume deleted functions won't ever be called. */
11373 continue;
11374 sym_value += adjust;
11375 }
4c52953f 11376
aef36ac1
AM
11377 dest = opd_entry_value (sym_sec, sym_value,
11378 &sym_sec, NULL, FALSE);
70cc837d
AM
11379 if (dest == (bfd_vma) -1)
11380 continue;
11381 }
11382 else
11383 dest = (sym_value
11384 + sym_sec->output_offset
11385 + sym_sec->output_section->vma);
4c52953f 11386
70cc837d
AM
11387 /* Ignore branch to self. */
11388 if (sym_sec == isec)
11389 continue;
4c52953f 11390
70cc837d
AM
11391 /* If the called function uses the toc, we need a stub. */
11392 if (sym_sec->has_toc_reloc
11393 || sym_sec->makes_toc_func_call)
4c52953f 11394 {
70cc837d 11395 ret = 1;
4c52953f
AM
11396 break;
11397 }
70cc837d
AM
11398
11399 /* Assume any branch that needs a long branch stub might in fact
11400 need a plt_branch stub. A plt_branch stub uses r2. */
11401 else if (dest - (isec->output_offset
11402 + isec->output_section->vma
6911b7dc
AM
11403 + rel->r_offset) + (1 << 25)
11404 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11405 ? h->other
11406 : sym->st_other))
4c52953f 11407 {
70cc837d
AM
11408 ret = 1;
11409 break;
11410 }
11411
11412 /* If calling back to a section in the process of being
11413 tested, we can't say for sure that no toc adjusting stubs
11414 are needed, so don't return zero. */
11415 else if (sym_sec->call_check_in_progress)
11416 ret = 2;
11417
11418 /* Branches to another section that itself doesn't have any TOC
11419 references are OK. Recursively call ourselves to check. */
11420 else if (!sym_sec->call_check_done)
11421 {
11422 int recur;
11423
11424 /* Mark current section as indeterminate, so that other
11425 sections that call back to current won't be marked as
11426 known. */
11427 isec->call_check_in_progress = 1;
11428 recur = toc_adjusting_stub_needed (info, sym_sec);
11429 isec->call_check_in_progress = 0;
11430
4c52953f
AM
11431 if (recur != 0)
11432 {
70cc837d
AM
11433 ret = recur;
11434 if (recur != 2)
11435 break;
4c52953f
AM
11436 }
11437 }
4c52953f 11438 }
70cc837d
AM
11439
11440 if (local_syms != NULL
11441 && (elf_symtab_hdr (isec->owner).contents
11442 != (unsigned char *) local_syms))
11443 free (local_syms);
11444 if (elf_section_data (isec)->relocs != relstart)
11445 free (relstart);
9b5ecbd0
AM
11446 }
11447
70cc837d
AM
11448 if ((ret & 1) == 0
11449 && isec->map_head.s != NULL
11450 && (strcmp (isec->output_section->name, ".init") == 0
11451 || strcmp (isec->output_section->name, ".fini") == 0))
11452 {
11453 if (isec->map_head.s->has_toc_reloc
11454 || isec->map_head.s->makes_toc_func_call)
11455 ret = 1;
11456 else if (!isec->map_head.s->call_check_done)
11457 {
11458 int recur;
11459 isec->call_check_in_progress = 1;
11460 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11461 isec->call_check_in_progress = 0;
11462 if (recur != 0)
11463 ret = recur;
11464 }
11465 }
11466
11467 if (ret == 1)
11468 isec->makes_toc_func_call = 1;
4c52953f 11469
9b5ecbd0
AM
11470 return ret;
11471}
11472
721956f4
AM
11473/* The linker repeatedly calls this function for each input section,
11474 in the order that input sections are linked into output sections.
11475 Build lists of input sections to determine groupings between which
11476 we may insert linker stubs. */
11477
9b5ecbd0 11478bfd_boolean
4ce794b7 11479ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11480{
11481 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11482
4dfe6ac6
NC
11483 if (htab == NULL)
11484 return FALSE;
11485
734b6cf9
AM
11486 if ((isec->output_section->flags & SEC_CODE) != 0
11487 && isec->output_section->index <= htab->top_index)
721956f4 11488 {
734b6cf9 11489 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 11490 /* Steal the link_sec pointer for our list. */
721956f4 11491#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
11492 /* This happens to make the list in reverse order,
11493 which is what we want. */
734b6cf9
AM
11494 PREV_SEC (isec) = *list;
11495 *list = isec;
721956f4 11496 }
ad8e1ba5 11497
4c52953f 11498 if (htab->multi_toc_needed)
9b5ecbd0 11499 {
4c52953f
AM
11500 /* If a code section has a function that uses the TOC then we need
11501 to use the right TOC (obviously). Also, make sure that .opd gets
11502 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
11503 can't find their function symbol (shouldn't ever happen now).
11504 Also specially treat .fixup for the linux kernel. .fixup
11505 contains branches, but only back to the function that hit an
11506 exception. */
11507 if (isec->has_toc_reloc
11508 || (isec->flags & SEC_CODE) == 0
11509 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
11510 {
11511 if (elf_gp (isec->owner) != 0)
11512 htab->toc_curr = elf_gp (isec->owner);
11513 }
6683a28d
AM
11514 else
11515 {
11516 if (!isec->call_check_done
11517 && toc_adjusting_stub_needed (info, isec) < 0)
11518 return FALSE;
11519 /* If we make a local call from this section, ie. a branch
11520 without a following nop, then we have no place to put a
11521 toc restoring insn. We must use the same toc group as
11522 the callee.
11523 Testing makes_toc_func_call actually tests for *any*
11524 calls to functions that need a good toc pointer. A more
11525 precise test would be better, as this one will set
11526 incorrect values for pasted .init/.fini fragments.
11527 (Fixed later in check_pasted_section.) */
11528 if (isec->makes_toc_func_call
11529 && elf_gp (isec->owner) != 0)
11530 htab->toc_curr = elf_gp (isec->owner);
11531 }
9b5ecbd0 11532 }
ad8e1ba5
AM
11533
11534 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 11535 Use the last TOC base. */
ad8e1ba5 11536 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11537 return TRUE;
721956f4
AM
11538}
11539
70cc837d
AM
11540/* Check that all .init and .fini sections use the same toc, if they
11541 have toc relocs. */
11542
11543static bfd_boolean
11544check_pasted_section (struct bfd_link_info *info, const char *name)
11545{
11546 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11547
11548 if (o != NULL)
11549 {
11550 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11551 bfd_vma toc_off = 0;
11552 asection *i;
11553
11554 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11555 if (i->has_toc_reloc)
11556 {
11557 if (toc_off == 0)
11558 toc_off = htab->stub_group[i->id].toc_off;
11559 else if (toc_off != htab->stub_group[i->id].toc_off)
11560 return FALSE;
11561 }
6683a28d
AM
11562
11563 if (toc_off == 0)
11564 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11565 if (i->makes_toc_func_call)
11566 {
11567 toc_off = htab->stub_group[i->id].toc_off;
11568 break;
11569 }
11570
70cc837d
AM
11571 /* Make sure the whole pasted function uses the same toc offset. */
11572 if (toc_off != 0)
11573 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11574 htab->stub_group[i->id].toc_off = toc_off;
11575 }
11576 return TRUE;
11577}
11578
11579bfd_boolean
11580ppc64_elf_check_init_fini (struct bfd_link_info *info)
11581{
11582 return (check_pasted_section (info, ".init")
11583 & check_pasted_section (info, ".fini"));
11584}
11585
721956f4
AM
11586/* See whether we can group stub sections together. Grouping stub
11587 sections may result in fewer stubs. More importantly, we need to
11588 put all .init* and .fini* stubs at the beginning of the .init or
11589 .fini output sections respectively, because glibc splits the
11590 _init and _fini functions into multiple parts. Putting a stub in
11591 the middle of a function is not a good idea. */
11592
11593static void
4ce794b7
AM
11594group_sections (struct ppc_link_hash_table *htab,
11595 bfd_size_type stub_group_size,
11596 bfd_boolean stubs_always_before_branch)
721956f4 11597{
7c8fe5c4
AM
11598 asection **list;
11599 bfd_size_type stub14_group_size;
11600 bfd_boolean suppress_size_errors;
11601
11602 suppress_size_errors = FALSE;
11603 stub14_group_size = stub_group_size;
11604 if (stub_group_size == 1)
11605 {
11606 /* Default values. */
11607 if (stubs_always_before_branch)
11608 {
11609 stub_group_size = 0x1e00000;
11610 stub14_group_size = 0x7800;
11611 }
11612 else
11613 {
11614 stub_group_size = 0x1c00000;
11615 stub14_group_size = 0x7000;
11616 }
11617 suppress_size_errors = TRUE;
11618 }
11619
11620 list = htab->input_list + htab->top_index;
734b6cf9 11621 do
721956f4 11622 {
734b6cf9
AM
11623 asection *tail = *list;
11624 while (tail != NULL)
721956f4 11625 {
734b6cf9
AM
11626 asection *curr;
11627 asection *prev;
11628 bfd_size_type total;
11629 bfd_boolean big_sec;
11630 bfd_vma curr_toc;
11631
11632 curr = tail;
eea6121a 11633 total = tail->size;
6bee8834
AM
11634 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11635 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
11636 ? stub14_group_size : stub_group_size);
11637 if (big_sec && !suppress_size_errors)
5c3dead3
AM
11638 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11639 tail->owner, tail);
734b6cf9
AM
11640 curr_toc = htab->stub_group[tail->id].toc_off;
11641
11642 while ((prev = PREV_SEC (curr)) != NULL
11643 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11644 < (ppc64_elf_section_data (prev) != NULL
11645 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11646 ? stub14_group_size : stub_group_size))
ad8e1ba5 11647 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
11648 curr = prev;
11649
11650 /* OK, the size from the start of CURR to the end is less
11651 than stub_group_size and thus can be handled by one stub
11652 section. (or the tail section is itself larger than
11653 stub_group_size, in which case we may be toast.) We
11654 should really be keeping track of the total size of stubs
11655 added here, as stubs contribute to the final output
11656 section size. That's a little tricky, and this way will
11657 only break if stubs added make the total size more than
11658 2^25, ie. for the default stub_group_size, if stubs total
11659 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11660 do
721956f4
AM
11661 {
11662 prev = PREV_SEC (tail);
734b6cf9 11663 /* Set up this stub group. */
721956f4
AM
11664 htab->stub_group[tail->id].link_sec = curr;
11665 }
734b6cf9
AM
11666 while (tail != curr && (tail = prev) != NULL);
11667
11668 /* But wait, there's more! Input sections up to stub_group_size
11669 bytes before the stub section can be handled by it too.
11670 Don't do this if we have a really large section after the
11671 stubs, as adding more stubs increases the chance that
11672 branches may not reach into the stub section. */
11673 if (!stubs_always_before_branch && !big_sec)
11674 {
11675 total = 0;
11676 while (prev != NULL
11677 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11678 < (ppc64_elf_section_data (prev) != NULL
11679 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11680 ? stub14_group_size : stub_group_size))
734b6cf9
AM
11681 && htab->stub_group[prev->id].toc_off == curr_toc)
11682 {
11683 tail = prev;
11684 prev = PREV_SEC (tail);
11685 htab->stub_group[tail->id].link_sec = curr;
11686 }
11687 }
11688 tail = prev;
721956f4
AM
11689 }
11690 }
734b6cf9
AM
11691 while (list-- != htab->input_list);
11692 free (htab->input_list);
721956f4
AM
11693#undef PREV_SEC
11694}
11695
58d180e8
AM
11696static const unsigned char glink_eh_frame_cie[] =
11697{
11698 0, 0, 0, 16, /* length. */
11699 0, 0, 0, 0, /* id. */
11700 1, /* CIE version. */
11701 'z', 'R', 0, /* Augmentation string. */
11702 4, /* Code alignment. */
11703 0x78, /* Data alignment. */
11704 65, /* RA reg. */
11705 1, /* Augmentation size. */
11706 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11707 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11708};
11709
d969d15f
AM
11710/* Stripping output sections is normally done before dynamic section
11711 symbols have been allocated. This function is called later, and
11712 handles cases like htab->brlt which is mapped to its own output
11713 section. */
11714
11715static void
11716maybe_strip_output (struct bfd_link_info *info, asection *isec)
11717{
11718 if (isec->size == 0
11719 && isec->output_section->size == 0
53d8967a 11720 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
11721 && !bfd_section_removed_from_list (info->output_bfd,
11722 isec->output_section)
11723 && elf_section_data (isec->output_section)->dynindx == 0)
11724 {
11725 isec->output_section->flags |= SEC_EXCLUDE;
11726 bfd_section_list_remove (info->output_bfd, isec->output_section);
11727 info->output_bfd->section_count--;
11728 }
11729}
11730
721956f4
AM
11731/* Determine and set the size of the stub section for a final link.
11732
11733 The basic idea here is to examine all the relocations looking for
11734 PC-relative calls to a target that is unreachable with a "bl"
11735 instruction. */
11736
b34976b6 11737bfd_boolean
9df0ef5f 11738ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
794e51c0
AM
11739 bfd_boolean plt_static_chain, int plt_thread_safe,
11740 int plt_stub_align)
721956f4
AM
11741{
11742 bfd_size_type stub_group_size;
b34976b6 11743 bfd_boolean stubs_always_before_branch;
721956f4
AM
11744 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11745
4dfe6ac6
NC
11746 if (htab == NULL)
11747 return FALSE;
11748
9df0ef5f 11749 htab->plt_static_chain = plt_static_chain;
794e51c0 11750 htab->plt_stub_align = plt_stub_align;
e2458743
AM
11751 if (plt_thread_safe == -1 && !info->executable)
11752 plt_thread_safe = 1;
b9e5796b
AM
11753 if (!htab->opd_abi)
11754 plt_thread_safe = 0;
11755 else if (plt_thread_safe == -1)
794e51c0 11756 {
e2458743 11757 static const char *const thread_starter[] =
794e51c0
AM
11758 {
11759 "pthread_create",
11760 /* libstdc++ */
11761 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11762 /* librt */
11763 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11764 "mq_notify", "create_timer",
11765 /* libanl */
11766 "getaddrinfo_a",
11767 /* libgomp */
11768 "GOMP_parallel_start",
11769 "GOMP_parallel_loop_static_start",
11770 "GOMP_parallel_loop_dynamic_start",
11771 "GOMP_parallel_loop_guided_start",
11772 "GOMP_parallel_loop_runtime_start",
68ffbac6 11773 "GOMP_parallel_sections_start",
794e51c0
AM
11774 };
11775 unsigned i;
11776
11777 for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11778 {
11779 struct elf_link_hash_entry *h;
11780 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11781 FALSE, FALSE, TRUE);
11782 plt_thread_safe = h != NULL && h->ref_regular;
11783 if (plt_thread_safe)
11784 break;
11785 }
11786 }
11787 htab->plt_thread_safe = plt_thread_safe;
721956f4
AM
11788 stubs_always_before_branch = group_size < 0;
11789 if (group_size < 0)
11790 stub_group_size = -group_size;
11791 else
11792 stub_group_size = group_size;
721956f4
AM
11793
11794 group_sections (htab, stub_group_size, stubs_always_before_branch);
11795
721956f4
AM
11796 while (1)
11797 {
11798 bfd *input_bfd;
11799 unsigned int bfd_indx;
11800 asection *stub_sec;
721956f4
AM
11801
11802 htab->stub_iteration += 1;
721956f4
AM
11803
11804 for (input_bfd = info->input_bfds, bfd_indx = 0;
11805 input_bfd != NULL;
11806 input_bfd = input_bfd->link_next, bfd_indx++)
11807 {
11808 Elf_Internal_Shdr *symtab_hdr;
11809 asection *section;
6cdc0ccc 11810 Elf_Internal_Sym *local_syms = NULL;
721956f4 11811
0c8d6e5c 11812 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11813 continue;
11814
721956f4 11815 /* We'll need the symbol table in a second. */
0ffa91dd 11816 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11817 if (symtab_hdr->sh_info == 0)
11818 continue;
11819
721956f4
AM
11820 /* Walk over each section attached to the input bfd. */
11821 for (section = input_bfd->sections;
11822 section != NULL;
11823 section = section->next)
11824 {
721956f4 11825 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11826
11827 /* If there aren't any relocs, then there's nothing more
11828 to do. */
11829 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11830 || (section->flags & SEC_ALLOC) == 0
11831 || (section->flags & SEC_LOAD) == 0
11832 || (section->flags & SEC_CODE) == 0
721956f4
AM
11833 || section->reloc_count == 0)
11834 continue;
11835
11836 /* If this section is a link-once section that will be
11837 discarded, then don't create any stubs. */
11838 if (section->output_section == NULL
927be08e 11839 || section->output_section->owner != info->output_bfd)
721956f4
AM
11840 continue;
11841
1e2f5b6e
AM
11842 /* Get the relocs. */
11843 internal_relocs
4ce794b7 11844 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 11845 info->keep_memory);
721956f4 11846 if (internal_relocs == NULL)
1e2f5b6e 11847 goto error_ret_free_local;
721956f4
AM
11848
11849 /* Now examine each relocation. */
11850 irela = internal_relocs;
11851 irelaend = irela + section->reloc_count;
11852 for (; irela < irelaend; irela++)
11853 {
4ce794b7
AM
11854 enum elf_ppc64_reloc_type r_type;
11855 unsigned int r_indx;
721956f4
AM
11856 enum ppc_stub_type stub_type;
11857 struct ppc_stub_hash_entry *stub_entry;
8387904d 11858 asection *sym_sec, *code_sec;
e054468f 11859 bfd_vma sym_value, code_value;
721956f4 11860 bfd_vma destination;
6911b7dc 11861 unsigned long local_off;
8843416a 11862 bfd_boolean ok_dest;
721956f4 11863 struct ppc_link_hash_entry *hash;
8387904d 11864 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
11865 struct elf_link_hash_entry *h;
11866 Elf_Internal_Sym *sym;
721956f4
AM
11867 char *stub_name;
11868 const asection *id_sec;
74f0fb50 11869 struct _opd_sec_data *opd;
e054468f 11870 struct plt_entry *plt_ent;
721956f4
AM
11871
11872 r_type = ELF64_R_TYPE (irela->r_info);
11873 r_indx = ELF64_R_SYM (irela->r_info);
11874
4ce794b7 11875 if (r_type >= R_PPC64_max)
721956f4
AM
11876 {
11877 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 11878 goto error_ret_free_internal;
721956f4
AM
11879 }
11880
11881 /* Only look for stubs on branch instructions. */
4ce794b7
AM
11882 if (r_type != R_PPC64_REL24
11883 && r_type != R_PPC64_REL14
11884 && r_type != R_PPC64_REL14_BRTAKEN
11885 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
11886 continue;
11887
11888 /* Now determine the call target, its name, value,
11889 section. */
411e1bfb
AM
11890 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11891 r_indx, input_bfd))
11892 goto error_ret_free_internal;
11893 hash = (struct ppc_link_hash_entry *) h;
11894
8843416a 11895 ok_dest = FALSE;
8387904d 11896 fdh = NULL;
7fe2b9a6 11897 sym_value = 0;
411e1bfb 11898 if (hash == NULL)
721956f4 11899 {
411e1bfb 11900 sym_value = sym->st_value;
8843416a 11901 ok_dest = TRUE;
721956f4 11902 }
7fe2b9a6
AM
11903 else if (hash->elf.root.type == bfd_link_hash_defined
11904 || hash->elf.root.type == bfd_link_hash_defweak)
11905 {
11906 sym_value = hash->elf.root.u.def.value;
11907 if (sym_sec->output_section != NULL)
11908 ok_dest = TRUE;
11909 }
11910 else if (hash->elf.root.type == bfd_link_hash_undefweak
11911 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11912 {
99877b66 11913 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11914 use the func descriptor sym instead if it is
11915 defined. */
ceb1f1ef 11916 if (hash->elf.root.root.string[0] == '.'
b31867b6 11917 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 11918 {
8387904d
AM
11919 if (fdh->elf.root.type == bfd_link_hash_defined
11920 || fdh->elf.root.type == bfd_link_hash_defweak)
11921 {
11922 sym_sec = fdh->elf.root.u.def.section;
11923 sym_value = fdh->elf.root.u.def.value;
11924 if (sym_sec->output_section != NULL)
11925 ok_dest = TRUE;
11926 }
99877b66
AM
11927 else
11928 fdh = NULL;
8387904d 11929 }
7fe2b9a6
AM
11930 }
11931 else
11932 {
11933 bfd_set_error (bfd_error_bad_value);
11934 goto error_ret_free_internal;
721956f4
AM
11935 }
11936
8843416a 11937 destination = 0;
6911b7dc 11938 local_off = 0;
8843416a
AM
11939 if (ok_dest)
11940 {
11941 sym_value += irela->r_addend;
11942 destination = (sym_value
11943 + sym_sec->output_offset
11944 + sym_sec->output_section->vma);
6911b7dc
AM
11945 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11946 ? hash->elf.other
11947 : sym->st_other);
8843416a
AM
11948 }
11949
8387904d 11950 code_sec = sym_sec;
e054468f 11951 code_value = sym_value;
74f0fb50
AM
11952 opd = get_opd_info (sym_sec);
11953 if (opd != NULL)
8387904d
AM
11954 {
11955 bfd_vma dest;
11956
74f0fb50 11957 if (hash == NULL && opd->adjust != NULL)
8387904d 11958 {
74f0fb50 11959 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11960 if (adjust == -1)
11961 continue;
e054468f 11962 code_value += adjust;
8387904d
AM
11963 sym_value += adjust;
11964 }
11965 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 11966 &code_sec, &code_value, FALSE);
8387904d
AM
11967 if (dest != (bfd_vma) -1)
11968 {
11969 destination = dest;
11970 if (fdh != NULL)
11971 {
11972 /* Fixup old ABI sym to point at code
11973 entry. */
99877b66 11974 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11975 hash->elf.root.u.def.section = code_sec;
e054468f 11976 hash->elf.root.u.def.value = code_value;
8387904d
AM
11977 }
11978 }
11979 }
11980
721956f4 11981 /* Determine what (if any) linker stub is needed. */
e054468f 11982 plt_ent = NULL;
721956f4 11983 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
11984 &plt_ent, destination,
11985 local_off);
ad8e1ba5
AM
11986
11987 if (stub_type != ppc_stub_plt_call)
11988 {
11989 /* Check whether we need a TOC adjusting stub.
11990 Since the linker pastes together pieces from
11991 different object files when creating the
11992 _init and _fini functions, it may be that a
11993 call to what looks like a local sym is in
11994 fact a call needing a TOC adjustment. */
8387904d
AM
11995 if (code_sec != NULL
11996 && code_sec->output_section != NULL
11997 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11998 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11999 && (code_sec->has_toc_reloc
12000 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12001 stub_type = ppc_stub_long_branch_r2off;
12002 }
12003
721956f4
AM
12004 if (stub_type == ppc_stub_none)
12005 continue;
12006
411e1bfb
AM
12007 /* __tls_get_addr calls might be eliminated. */
12008 if (stub_type != ppc_stub_plt_call
12009 && hash != NULL
8387904d
AM
12010 && (hash == htab->tls_get_addr
12011 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12012 && section->has_tls_reloc
12013 && irela != internal_relocs)
12014 {
12015 /* Get tls info. */
f961d9dd 12016 unsigned char *tls_mask;
411e1bfb 12017
3a71aa26 12018 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12019 irela - 1, input_bfd))
12020 goto error_ret_free_internal;
e7b938ca 12021 if (*tls_mask != 0)
411e1bfb
AM
12022 continue;
12023 }
12024
3b421ab3
AM
12025 if (stub_type == ppc_stub_plt_call
12026 && irela + 1 < irelaend
12027 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12028 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12029 {
12030 if (!tocsave_find (htab, INSERT,
12031 &local_syms, irela + 1, input_bfd))
12032 goto error_ret_free_internal;
12033 }
12034 else if (stub_type == ppc_stub_plt_call)
12035 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12036
721956f4
AM
12037 /* Support for grouping stub sections. */
12038 id_sec = htab->stub_group[section->id].link_sec;
12039
12040 /* Get the name of this stub. */
12041 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12042 if (!stub_name)
12043 goto error_ret_free_internal;
12044
12045 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12046 stub_name, FALSE, FALSE);
721956f4
AM
12047 if (stub_entry != NULL)
12048 {
12049 /* The proper stub has already been created. */
12050 free (stub_name);
794e51c0
AM
12051 if (stub_type == ppc_stub_plt_call_r2save)
12052 stub_entry->stub_type = stub_type;
721956f4
AM
12053 continue;
12054 }
12055
25f53a85 12056 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12057 if (stub_entry == NULL)
12058 {
12059 free (stub_name);
6cdc0ccc
AM
12060 error_ret_free_internal:
12061 if (elf_section_data (section)->relocs == NULL)
12062 free (internal_relocs);
12063 error_ret_free_local:
12064 if (local_syms != NULL
12065 && (symtab_hdr->contents
12066 != (unsigned char *) local_syms))
12067 free (local_syms);
b34976b6 12068 return FALSE;
721956f4
AM
12069 }
12070
ad8e1ba5 12071 stub_entry->stub_type = stub_type;
794e51c0
AM
12072 if (stub_type != ppc_stub_plt_call
12073 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12074 {
12075 stub_entry->target_value = code_value;
12076 stub_entry->target_section = code_sec;
12077 }
12078 else
12079 {
12080 stub_entry->target_value = sym_value;
12081 stub_entry->target_section = sym_sec;
12082 }
721956f4 12083 stub_entry->h = hash;
e054468f 12084 stub_entry->plt_ent = plt_ent;
6911b7dc 12085 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12086
12087 if (stub_entry->h != NULL)
12088 htab->stub_globals += 1;
721956f4
AM
12089 }
12090
12091 /* We're done with the internal relocs, free them. */
6cdc0ccc 12092 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12093 free (internal_relocs);
721956f4 12094 }
6cdc0ccc
AM
12095
12096 if (local_syms != NULL
12097 && symtab_hdr->contents != (unsigned char *) local_syms)
12098 {
12099 if (!info->keep_memory)
12100 free (local_syms);
12101 else
12102 symtab_hdr->contents = (unsigned char *) local_syms;
12103 }
721956f4
AM
12104 }
12105
5c3dead3 12106 /* We may have added some stubs. Find out the new size of the
721956f4
AM
12107 stub sections. */
12108 for (stub_sec = htab->stub_bfd->sections;
12109 stub_sec != NULL;
12110 stub_sec = stub_sec->next)
e717da7e 12111 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12112 {
5c3dead3 12113 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12114 stub_sec->size = 0;
12115 stub_sec->reloc_count = 0;
84f5d08e 12116 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12117 }
eea6121a
AM
12118
12119 htab->brlt->size = 0;
84f5d08e
AM
12120 htab->brlt->reloc_count = 0;
12121 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12122 if (htab->relbrlt != NULL)
eea6121a 12123 htab->relbrlt->size = 0;
721956f4 12124
63bc6f6c 12125 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12126
176a0d42
AM
12127 if (info->emitrelocations
12128 && htab->glink != NULL && htab->glink->size != 0)
12129 {
12130 htab->glink->reloc_count = 1;
12131 htab->glink->flags |= SEC_RELOC;
12132 }
12133
58d180e8
AM
12134 if (htab->glink_eh_frame != NULL
12135 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12136 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12137 {
4bbe044a 12138 size_t size = 0, align;
58d180e8
AM
12139
12140 for (stub_sec = htab->stub_bfd->sections;
12141 stub_sec != NULL;
12142 stub_sec = stub_sec->next)
12143 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12144 size += 20;
12145 if (htab->glink != NULL && htab->glink->size != 0)
12146 size += 24;
12147 if (size != 0)
12148 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12149 align = 1;
12150 align <<= htab->glink_eh_frame->output_section->alignment_power;
12151 align -= 1;
12152 size = (size + align) & ~align;
58d180e8
AM
12153 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12154 htab->glink_eh_frame->size = size;
12155 }
12156
794e51c0
AM
12157 if (htab->plt_stub_align != 0)
12158 for (stub_sec = htab->stub_bfd->sections;
12159 stub_sec != NULL;
12160 stub_sec = stub_sec->next)
12161 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12162 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12163 & (-1 << htab->plt_stub_align));
12164
5c3dead3
AM
12165 for (stub_sec = htab->stub_bfd->sections;
12166 stub_sec != NULL;
12167 stub_sec = stub_sec->next)
12168 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12169 && stub_sec->rawsize != stub_sec->size)
12170 break;
12171
12172 /* Exit from this loop when no stubs have been added, and no stubs
12173 have changed size. */
58d180e8
AM
12174 if (stub_sec == NULL
12175 && (htab->glink_eh_frame == NULL
12176 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12177 break;
12178
721956f4
AM
12179 /* Ask the linker to do its stuff. */
12180 (*htab->layout_sections_again) ();
12181 }
12182
d969d15f
AM
12183 maybe_strip_output (info, htab->brlt);
12184 if (htab->glink_eh_frame != NULL)
12185 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12186
b34976b6 12187 return TRUE;
721956f4
AM
12188}
12189
12190/* Called after we have determined section placement. If sections
805fc799 12191 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12192
805fc799 12193bfd_vma
1c865ab2 12194ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12195{
805fc799
AM
12196 asection *s;
12197 bfd_vma TOCstart;
721956f4 12198
805fc799
AM
12199 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12200 order. The TOC starts where the first of these sections starts. */
12201 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12202 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12203 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12204 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12205 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12206 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12207 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12208 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12209 {
12210 /* This may happen for
12211 o references to TOC base (SYM@toc / TOC[tc0]) without a
12212 .toc directive
12213 o bad linker script
12214 o --gc-sections and empty TOC sections
12215
12216 FIXME: Warn user? */
12217
12218 /* Look for a likely section. We probably won't even be
12219 using TOCstart. */
12220 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12221 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12222 | SEC_EXCLUDE))
805fc799
AM
12223 == (SEC_ALLOC | SEC_SMALL_DATA))
12224 break;
721956f4 12225 if (s == NULL)
805fc799 12226 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12227 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12228 == (SEC_ALLOC | SEC_SMALL_DATA))
12229 break;
721956f4 12230 if (s == NULL)
805fc799 12231 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12232 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12233 == SEC_ALLOC)
805fc799 12234 break;
721956f4 12235 if (s == NULL)
805fc799 12236 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12237 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12238 break;
12239 }
721956f4 12240
805fc799
AM
12241 TOCstart = 0;
12242 if (s != NULL)
12243 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12244
1c865ab2
AM
12245 _bfd_set_gp_value (obfd, TOCstart);
12246
12247 if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12248 {
12249 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12250
12251 if (htab != NULL
12252 && htab->elf.hgot != NULL)
12253 {
5295321c 12254 htab->elf.hgot->type = STT_OBJECT;
1c865ab2
AM
12255 htab->elf.hgot->root.type = bfd_link_hash_defined;
12256 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12257 htab->elf.hgot->root.u.def.section = s;
12258 }
12259 }
805fc799 12260 return TOCstart;
721956f4
AM
12261}
12262
12263/* Build all the stubs associated with the current output file.
12264 The stubs are kept in a hash table attached to the main linker
12265 hash table. This function is called via gldelf64ppc_finish. */
12266
b34976b6 12267bfd_boolean
4ce794b7
AM
12268ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12269 struct bfd_link_info *info,
12270 char **stats)
5d1634d7
AM
12271{
12272 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 12273 asection *stub_sec;
5d1634d7 12274 bfd_byte *p;
e717da7e 12275 int stub_sec_count = 0;
5d1634d7 12276
4dfe6ac6
NC
12277 if (htab == NULL)
12278 return FALSE;
12279
ad8e1ba5 12280 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
12281
12282 /* Allocate memory to hold the linker stubs. */
721956f4
AM
12283 for (stub_sec = htab->stub_bfd->sections;
12284 stub_sec != NULL;
12285 stub_sec = stub_sec->next)
eea6121a
AM
12286 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12287 && stub_sec->size != 0)
e717da7e 12288 {
eea6121a
AM
12289 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12290 if (stub_sec->contents == NULL)
12291 return FALSE;
12292 /* We want to check that built size is the same as calculated
12293 size. rawsize is a convenient location to use. */
12294 stub_sec->rawsize = stub_sec->size;
12295 stub_sec->size = 0;
e717da7e 12296 }
5d1634d7 12297
23eb7e01 12298 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12299 {
9f951329 12300 unsigned int indx;
ad8e1ba5 12301 bfd_vma plt0;
9f951329 12302
721956f4 12303 /* Build the .glink plt call stub. */
97b639ba
AM
12304 if (htab->emit_stub_syms)
12305 {
12306 struct elf_link_hash_entry *h;
468392fb
AM
12307 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12308 TRUE, FALSE, FALSE);
97b639ba
AM
12309 if (h == NULL)
12310 return FALSE;
12311 if (h->root.type == bfd_link_hash_new)
12312 {
12313 h->root.type = bfd_link_hash_defined;
12314 h->root.u.def.section = htab->glink;
ee4bf8d2 12315 h->root.u.def.value = 8;
f5385ebf
AM
12316 h->ref_regular = 1;
12317 h->def_regular = 1;
12318 h->ref_regular_nonweak = 1;
12319 h->forced_local = 1;
12320 h->non_elf = 0;
97b639ba
AM
12321 }
12322 }
176a0d42
AM
12323 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12324 if (info->emitrelocations)
12325 {
12326 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12327 if (r == NULL)
12328 return FALSE;
12329 r->r_offset = (htab->glink->output_offset
12330 + htab->glink->output_section->vma);
12331 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12332 r->r_addend = plt0;
12333 }
4ce794b7 12334 p = htab->glink->contents;
176a0d42 12335 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
12336 bfd_put_64 (htab->glink->owner, plt0, p);
12337 p += 8;
b9e5796b
AM
12338 if (htab->opd_abi)
12339 {
12340 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12341 p += 4;
12342 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12343 p += 4;
12344 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12345 p += 4;
12346 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12347 p += 4;
12348 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12349 p += 4;
12350 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12351 p += 4;
12352 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12353 p += 4;
12354 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12355 p += 4;
12356 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12357 p += 4;
12358 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12359 p += 4;
12360 }
12361 else
12362 {
12363 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12364 p += 4;
12365 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12366 p += 4;
12367 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12368 p += 4;
12369 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12370 p += 4;
12371 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12372 p += 4;
12373 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12374 p += 4;
12375 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12376 p += 4;
12377 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12378 p += 4;
12379 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12380 p += 4;
12381 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12382 p += 4;
12383 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12384 p += 4;
12385 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12386 p += 4;
12387 }
4ce794b7 12388 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 12389 p += 4;
ee4bf8d2
AM
12390 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12391 {
12392 bfd_put_32 (htab->glink->owner, NOP, p);
12393 p += 4;
12394 }
ad8e1ba5 12395
9f951329
AM
12396 /* Build the .glink lazy link call stubs. */
12397 indx = 0;
eea6121a 12398 while (p < htab->glink->contents + htab->glink->size)
9f951329 12399 {
b9e5796b 12400 if (htab->opd_abi)
9f951329 12401 {
b9e5796b
AM
12402 if (indx < 0x8000)
12403 {
12404 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12405 p += 4;
12406 }
12407 else
12408 {
12409 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12410 p += 4;
12411 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12412 p);
12413 p += 4;
12414 }
9f951329 12415 }
4ce794b7 12416 bfd_put_32 (htab->glink->owner,
ee4bf8d2 12417 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 12418 indx++;
9f951329
AM
12419 p += 4;
12420 }
eea6121a 12421 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 12422 }
5d1634d7 12423
eea6121a 12424 if (htab->brlt->size != 0)
721956f4 12425 {
4ce794b7 12426 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 12427 htab->brlt->size);
4ce794b7 12428 if (htab->brlt->contents == NULL)
b34976b6 12429 return FALSE;
721956f4 12430 }
ee75fd95 12431 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
12432 {
12433 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 12434 htab->relbrlt->size);
63bc6f6c
AM
12435 if (htab->relbrlt->contents == NULL)
12436 return FALSE;
12437 }
5d1634d7 12438
58d180e8
AM
12439 if (htab->glink_eh_frame != NULL
12440 && htab->glink_eh_frame->size != 0)
12441 {
12442 bfd_vma val;
4bbe044a
AM
12443 bfd_byte *last_fde;
12444 size_t last_fde_len, size, align, pad;
58d180e8
AM
12445
12446 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12447 if (p == NULL)
12448 return FALSE;
12449 htab->glink_eh_frame->contents = p;
4bbe044a 12450 last_fde = p;
58d180e8
AM
12451
12452 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12453
12454 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12455 /* CIE length (rewrite in case little-endian). */
4bbe044a
AM
12456 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12457 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
58d180e8
AM
12458 p += sizeof (glink_eh_frame_cie);
12459
12460 for (stub_sec = htab->stub_bfd->sections;
12461 stub_sec != NULL;
12462 stub_sec = stub_sec->next)
12463 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12464 {
4bbe044a
AM
12465 last_fde = p;
12466 last_fde_len = 16;
58d180e8
AM
12467 /* FDE length. */
12468 bfd_put_32 (htab->elf.dynobj, 16, p);
12469 p += 4;
12470 /* CIE pointer. */
12471 val = p - htab->glink_eh_frame->contents;
12472 bfd_put_32 (htab->elf.dynobj, val, p);
12473 p += 4;
12474 /* Offset to stub section. */
12475 val = (stub_sec->output_section->vma
12476 + stub_sec->output_offset);
12477 val -= (htab->glink_eh_frame->output_section->vma
12478 + htab->glink_eh_frame->output_offset);
12479 val -= p - htab->glink_eh_frame->contents;
12480 if (val + 0x80000000 > 0xffffffff)
12481 {
12482 info->callbacks->einfo
8de848d8 12483 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
12484 stub_sec->name);
12485 return FALSE;
12486 }
12487 bfd_put_32 (htab->elf.dynobj, val, p);
12488 p += 4;
12489 /* stub section size. */
12490 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12491 p += 4;
12492 /* Augmentation. */
12493 p += 1;
12494 /* Pad. */
12495 p += 3;
12496 }
12497 if (htab->glink != NULL && htab->glink->size != 0)
12498 {
4bbe044a
AM
12499 last_fde = p;
12500 last_fde_len = 20;
58d180e8
AM
12501 /* FDE length. */
12502 bfd_put_32 (htab->elf.dynobj, 20, p);
12503 p += 4;
12504 /* CIE pointer. */
12505 val = p - htab->glink_eh_frame->contents;
12506 bfd_put_32 (htab->elf.dynobj, val, p);
12507 p += 4;
12508 /* Offset to .glink. */
12509 val = (htab->glink->output_section->vma
12510 + htab->glink->output_offset
12511 + 8);
12512 val -= (htab->glink_eh_frame->output_section->vma
12513 + htab->glink_eh_frame->output_offset);
12514 val -= p - htab->glink_eh_frame->contents;
12515 if (val + 0x80000000 > 0xffffffff)
12516 {
12517 info->callbacks->einfo
8de848d8 12518 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
12519 htab->glink->name);
12520 return FALSE;
12521 }
12522 bfd_put_32 (htab->elf.dynobj, val, p);
12523 p += 4;
12524 /* .glink size. */
12525 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12526 p += 4;
12527 /* Augmentation. */
12528 p += 1;
12529
12530 *p++ = DW_CFA_advance_loc + 1;
12531 *p++ = DW_CFA_register;
12532 *p++ = 65;
12533 *p++ = 12;
12534 *p++ = DW_CFA_advance_loc + 4;
12535 *p++ = DW_CFA_restore_extended;
12536 *p++ = 65;
12537 }
4bbe044a
AM
12538 /* Subsume any padding into the last FDE if user .eh_frame
12539 sections are aligned more than glink_eh_frame. Otherwise any
12540 zero padding will be seen as a terminator. */
12541 size = p - htab->glink_eh_frame->contents;
12542 align = 1;
12543 align <<= htab->glink_eh_frame->output_section->alignment_power;
12544 align -= 1;
12545 pad = ((size + align) & ~align) - size;
12546 htab->glink_eh_frame->size = size + pad;
12547 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
58d180e8
AM
12548 }
12549
721956f4
AM
12550 /* Build the stubs as directed by the stub hash table. */
12551 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 12552
aa8a7074
AM
12553 if (htab->relbrlt != NULL)
12554 htab->relbrlt->reloc_count = 0;
12555
794e51c0
AM
12556 if (htab->plt_stub_align != 0)
12557 for (stub_sec = htab->stub_bfd->sections;
12558 stub_sec != NULL;
12559 stub_sec = stub_sec->next)
12560 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12561 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12562 & (-1 << htab->plt_stub_align));
12563
721956f4
AM
12564 for (stub_sec = htab->stub_bfd->sections;
12565 stub_sec != NULL;
12566 stub_sec = stub_sec->next)
e717da7e
AM
12567 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12568 {
12569 stub_sec_count += 1;
eea6121a 12570 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
12571 break;
12572 }
5d1634d7 12573
721956f4 12574 if (stub_sec != NULL
58d180e8
AM
12575 || htab->glink->rawsize != htab->glink->size
12576 || (htab->glink_eh_frame != NULL
12577 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 12578 {
b34976b6 12579 htab->stub_error = TRUE;
8de848d8 12580 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 12581 }
721956f4 12582
d2a300cf
AM
12583 if (htab->stub_error)
12584 return FALSE;
12585
12586 if (stats != NULL)
12587 {
12588 *stats = bfd_malloc (500);
12589 if (*stats == NULL)
12590 return FALSE;
12591
ee75fd95 12592 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
12593 " branch %lu\n"
12594 " toc adjust %lu\n"
12595 " long branch %lu\n"
12596 " long toc adj %lu\n"
794e51c0
AM
12597 " plt call %lu\n"
12598 " plt call toc %lu"),
e717da7e 12599 stub_sec_count,
ee75fd95 12600 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
12601 htab->stub_count[ppc_stub_long_branch - 1],
12602 htab->stub_count[ppc_stub_long_branch_r2off - 1],
12603 htab->stub_count[ppc_stub_plt_branch - 1],
12604 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0
AM
12605 htab->stub_count[ppc_stub_plt_call - 1],
12606 htab->stub_count[ppc_stub_plt_call_r2save - 1]);
d2a300cf
AM
12607 }
12608 return TRUE;
5bd4f169
AM
12609}
12610
99877b66
AM
12611/* This function undoes the changes made by add_symbol_adjust. */
12612
12613static bfd_boolean
12614undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12615{
12616 struct ppc_link_hash_entry *eh;
12617
12618 if (h->root.type == bfd_link_hash_indirect)
12619 return TRUE;
12620
99877b66
AM
12621 eh = (struct ppc_link_hash_entry *) h;
12622 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12623 return TRUE;
12624
12625 eh->elf.root.type = bfd_link_hash_undefined;
12626 return TRUE;
12627}
12628
12629void
12630ppc64_elf_restore_symbols (struct bfd_link_info *info)
12631{
12632 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
12633
12634 if (htab != NULL)
12635 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
12636}
12637
60124e18
AM
12638/* What to do when ld finds relocations against symbols defined in
12639 discarded sections. */
12640
12641static unsigned int
12642ppc64_elf_action_discarded (asection *sec)
12643{
12644 if (strcmp (".opd", sec->name) == 0)
12645 return 0;
12646
12647 if (strcmp (".toc", sec->name) == 0)
12648 return 0;
12649
bce50a28
JJ
12650 if (strcmp (".toc1", sec->name) == 0)
12651 return 0;
12652
60124e18
AM
12653 return _bfd_elf_default_action_discarded (sec);
12654}
12655
5bd4f169
AM
12656/* The RELOCATE_SECTION function is called by the ELF backend linker
12657 to handle the relocations for a section.
12658
12659 The relocs are always passed as Rela structures; if the section
12660 actually uses Rel structures, the r_addend field will always be
12661 zero.
12662
12663 This function is responsible for adjust the section contents as
12664 necessary, and (if using Rela relocs and generating a
1049f94e 12665 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
12666 necessary.
12667
12668 This function does not have to worry about setting the reloc
12669 address or the reloc symbol index.
12670
12671 LOCAL_SYMS is a pointer to the swapped in local symbols.
12672
12673 LOCAL_SECTIONS is an array giving the section in the input file
12674 corresponding to the st_shndx field of each local symbol.
12675
12676 The global hash table entry for the global symbols can be found
12677 via elf_sym_hashes (input_bfd).
12678
1049f94e 12679 When generating relocatable output, this function must handle
5bd4f169
AM
12680 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
12681 going to be the section symbol corresponding to the output
12682 section, which means that the addend must be adjusted
12683 accordingly. */
12684
b34976b6 12685static bfd_boolean
4ce794b7
AM
12686ppc64_elf_relocate_section (bfd *output_bfd,
12687 struct bfd_link_info *info,
12688 bfd *input_bfd,
12689 asection *input_section,
12690 bfd_byte *contents,
12691 Elf_Internal_Rela *relocs,
12692 Elf_Internal_Sym *local_syms,
12693 asection **local_sections)
5bd4f169 12694{
65f38f15 12695 struct ppc_link_hash_table *htab;
5bd4f169
AM
12696 Elf_Internal_Shdr *symtab_hdr;
12697 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
12698 Elf_Internal_Rela *rel;
12699 Elf_Internal_Rela *relend;
411e1bfb
AM
12700 Elf_Internal_Rela outrel;
12701 bfd_byte *loc;
411e1bfb 12702 struct got_entry **local_got_ents;
5bd4f169 12703 bfd_vma TOCstart;
b34976b6
AM
12704 bfd_boolean ret = TRUE;
12705 bfd_boolean is_opd;
794e51c0
AM
12706 /* Assume 'at' branch hints. */
12707 bfd_boolean is_isa_v2 = TRUE;
4fe5ca5b 12708 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 12709
65f38f15 12710 /* Initialize howto table if needed. */
5bd4f169 12711 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
12712 ppc_howto_init ();
12713
65f38f15 12714 htab = ppc_hash_table (info);
4dfe6ac6
NC
12715 if (htab == NULL)
12716 return FALSE;
ee75fd95
AM
12717
12718 /* Don't relocate stub sections. */
12719 if (input_section->owner == htab->stub_bfd)
12720 return TRUE;
12721
0c8d6e5c 12722 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 12723
411e1bfb 12724 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 12725 TOCstart = elf_gp (output_bfd);
0ffa91dd 12726 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 12727 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 12728 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 12729
5bd4f169
AM
12730 rel = relocs;
12731 relend = relocs + input_section->reloc_count;
12732 for (; rel < relend; rel++)
12733 {
04c9666a 12734 enum elf_ppc64_reloc_type r_type;
31c76678 12735 bfd_vma addend;
5bd4f169
AM
12736 bfd_reloc_status_type r;
12737 Elf_Internal_Sym *sym;
12738 asection *sec;
039b3fef
AM
12739 struct elf_link_hash_entry *h_elf;
12740 struct ppc_link_hash_entry *h;
12741 struct ppc_link_hash_entry *fdh;
5bd4f169 12742 const char *sym_name;
0d4792f7 12743 unsigned long r_symndx, toc_symndx;
3a71aa26 12744 bfd_vma toc_addend;
f961d9dd
AM
12745 unsigned char tls_mask, tls_gd, tls_type;
12746 unsigned char sym_type;
5bd4f169 12747 bfd_vma relocation;
b34976b6
AM
12748 bfd_boolean unresolved_reloc;
12749 bfd_boolean warned;
bc30df16 12750 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 12751 unsigned int insn;
e11840f9 12752 unsigned int mask;
721956f4
AM
12753 struct ppc_stub_hash_entry *stub_entry;
12754 bfd_vma max_br_offset;
12755 bfd_vma from;
31c76678 12756 const Elf_Internal_Rela orig_rel = *rel;
5bd4f169 12757
4ce794b7 12758 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 12759 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
12760
12761 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12762 symbol of the previous ADDR64 reloc. The symbol gives us the
12763 proper TOC base to use. */
12764 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12765 && rel != relocs
12766 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12767 && is_opd)
12768 r_symndx = ELF64_R_SYM (rel[-1].r_info);
12769
4ce794b7
AM
12770 sym = NULL;
12771 sec = NULL;
039b3fef 12772 h_elf = NULL;
4ce794b7 12773 sym_name = NULL;
b34976b6
AM
12774 unresolved_reloc = FALSE;
12775 warned = FALSE;
65f38f15 12776
0b13192e 12777 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
12778 {
12779 /* It's a local symbol. */
74f0fb50 12780 struct _opd_sec_data *opd;
4025353c 12781
5bd4f169
AM
12782 sym = local_syms + r_symndx;
12783 sec = local_sections[r_symndx];
26c61ae5 12784 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 12785 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 12786 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
12787 opd = get_opd_info (sec);
12788 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 12789 {
74f0fb50 12790 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
12791 if (adjust == -1)
12792 relocation = 0;
12793 else
4cc603a5
AM
12794 {
12795 /* If this is a relocation against the opd section sym
12796 and we have edited .opd, adjust the reloc addend so
12797 that ld -r and ld --emit-relocs output is correct.
12798 If it is a reloc against some other .opd symbol,
12799 then the symbol value will be adjusted later. */
12800 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12801 rel->r_addend += adjust;
12802 else
12803 relocation += adjust;
12804 }
1e2f5b6e 12805 }
5bd4f169
AM
12806 }
12807 else
12808 {
b2a8e766
AM
12809 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12810 r_symndx, symtab_hdr, sym_hashes,
039b3fef 12811 h_elf, sec, relocation,
b2a8e766 12812 unresolved_reloc, warned);
039b3fef
AM
12813 sym_name = h_elf->root.root.string;
12814 sym_type = h_elf->type;
b69fdb4e
AM
12815 if (sec != NULL
12816 && sec->owner == output_bfd
12817 && strcmp (sec->name, ".opd") == 0)
12818 {
12819 /* This is a symbol defined in a linker script. All
12820 such are defined in output sections, even those
12821 defined by simple assignment from a symbol defined in
12822 an input section. Transfer the symbol to an
12823 appropriate input .opd section, so that a branch to
12824 this symbol will be mapped to the location specified
12825 by the opd entry. */
12826 struct bfd_link_order *lo;
12827 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12828 if (lo->type == bfd_indirect_link_order)
12829 {
12830 asection *isec = lo->u.indirect.section;
12831 if (h_elf->root.u.def.value >= isec->output_offset
12832 && h_elf->root.u.def.value < (isec->output_offset
12833 + isec->size))
12834 {
12835 h_elf->root.u.def.value -= isec->output_offset;
12836 h_elf->root.u.def.section = isec;
12837 sec = isec;
12838 break;
12839 }
12840 }
12841 }
5bd4f169 12842 }
039b3fef 12843 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 12844
dbaa2011 12845 if (sec != NULL && discarded_section (sec))
e4067dbb 12846 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b
MR
12847 rel, 1, relend,
12848 ppc64_elf_howto_table[r_type], 0,
e4067dbb 12849 contents);
ab96bf03
AM
12850
12851 if (info->relocatable)
12852 continue;
12853
f40da81b
AM
12854 if (h != NULL && &h->elf == htab->elf.hgot)
12855 {
12856 relocation = (TOCstart
12857 + htab->stub_group[input_section->id].toc_off);
12858 sec = bfd_abs_section_ptr;
12859 unresolved_reloc = FALSE;
12860 }
12861
951fd09b
AM
12862 /* TLS optimizations. Replace instruction sequences and relocs
12863 based on information we collected in tls_optimize. We edit
12864 RELOCS so that --emit-relocs will output something sensible
12865 for the final instruction stream. */
12866 tls_mask = 0;
12867 tls_gd = 0;
0d4792f7 12868 toc_symndx = 0;
727fc41e
AM
12869 if (h != NULL)
12870 tls_mask = h->tls_mask;
12871 else if (local_got_ents != NULL)
411e1bfb 12872 {
e054468f
AM
12873 struct plt_entry **local_plt = (struct plt_entry **)
12874 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 12875 unsigned char *lgot_masks = (unsigned char *)
e054468f 12876 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
12877 tls_mask = lgot_masks[r_symndx];
12878 }
12879 if (tls_mask == 0
12880 && (r_type == R_PPC64_TLS
12881 || r_type == R_PPC64_TLSGD
12882 || r_type == R_PPC64_TLSLD))
12883 {
12884 /* Check for toc tls entries. */
f961d9dd 12885 unsigned char *toc_tls;
0d4792f7 12886
727fc41e
AM
12887 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12888 &local_syms, rel, input_bfd))
12889 return FALSE;
0d4792f7 12890
727fc41e
AM
12891 if (toc_tls)
12892 tls_mask = *toc_tls;
0d4792f7
AM
12893 }
12894
12895 /* Check that tls relocs are used with tls syms, and non-tls
12896 relocs are used with non-tls syms. */
cf35638d 12897 if (r_symndx != STN_UNDEF
0d4792f7
AM
12898 && r_type != R_PPC64_NONE
12899 && (h == NULL
039b3fef
AM
12900 || h->elf.root.type == bfd_link_hash_defined
12901 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
12902 && (IS_PPC64_TLS_RELOC (r_type)
12903 != (sym_type == STT_TLS
12904 || (sym_type == STT_SECTION
12905 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 12906 {
727fc41e
AM
12907 if (tls_mask != 0
12908 && (r_type == R_PPC64_TLS
12909 || r_type == R_PPC64_TLSGD
12910 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
12911 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12912 ;
12913 else
25f53a85 12914 info->callbacks->einfo
1d483afe 12915 (!IS_PPC64_TLS_RELOC (r_type)
bc30df16
AM
12916 ? _("%P: %H: %s used with TLS symbol `%T'\n")
12917 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 12918 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
12919 ppc64_elf_howto_table[r_type]->name,
12920 sym_name);
411e1bfb
AM
12921 }
12922
12923 /* Ensure reloc mapping code below stays sane. */
12924 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12925 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12926 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12927 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12928 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12929 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12930 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12931 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12932 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12933 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12934 abort ();
0d4792f7 12935
411e1bfb
AM
12936 switch (r_type)
12937 {
12938 default:
411e1bfb
AM
12939 break;
12940
ba761f19
AM
12941 case R_PPC64_LO_DS_OPT:
12942 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12943 if ((insn & (0x3f << 26)) != 58u << 26)
12944 abort ();
12945 insn += (14u << 26) - (58u << 26);
12946 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12947 r_type = R_PPC64_TOC16_LO;
12948 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12949 break;
12950
411e1bfb
AM
12951 case R_PPC64_TOC16:
12952 case R_PPC64_TOC16_LO:
12953 case R_PPC64_TOC16_DS:
12954 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
12955 {
12956 /* Check for toc tls entries. */
f961d9dd 12957 unsigned char *toc_tls;
951fd09b 12958 int retval;
411e1bfb 12959
3a71aa26
AM
12960 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12961 &local_syms, rel, input_bfd);
951fd09b 12962 if (retval == 0)
411e1bfb
AM
12963 return FALSE;
12964
12965 if (toc_tls)
12966 {
951fd09b 12967 tls_mask = *toc_tls;
411e1bfb
AM
12968 if (r_type == R_PPC64_TOC16_DS
12969 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
12970 {
12971 if (tls_mask != 0
12972 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12973 goto toctprel;
12974 }
411e1bfb 12975 else
951fd09b
AM
12976 {
12977 /* If we found a GD reloc pair, then we might be
12978 doing a GD->IE transition. */
12979 if (retval == 2)
12980 {
12981 tls_gd = TLS_TPRELGD;
12982 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12983 goto tls_ldgd_opt;
951fd09b
AM
12984 }
12985 else if (retval == 3)
12986 {
12987 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 12988 goto tls_ldgd_opt;
951fd09b
AM
12989 }
12990 }
411e1bfb
AM
12991 }
12992 }
12993 break;
12994
9d6ded02
AM
12995 case R_PPC64_GOT_TPREL16_HI:
12996 case R_PPC64_GOT_TPREL16_HA:
12997 if (tls_mask != 0
12998 && (tls_mask & TLS_TPREL) == 0)
12999 {
13000 rel->r_offset -= d_offset;
13001 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13002 r_type = R_PPC64_NONE;
13003 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13004 }
13005 break;
13006
411e1bfb
AM
13007 case R_PPC64_GOT_TPREL16_DS:
13008 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13009 if (tls_mask != 0
13010 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13011 {
81407a69 13012 toctprel:
4fe5ca5b 13013 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
13014 insn &= 31 << 21;
13015 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 13016 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 13017 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13018 if (toc_symndx != 0)
13019 {
13020 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13021 rel->r_addend = toc_addend;
0d4792f7
AM
13022 /* We changed the symbol. Start over in order to
13023 get h, sym, sec etc. right. */
13024 rel--;
13025 continue;
13026 }
13027 else
13028 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13029 }
13030 break;
13031
13032 case R_PPC64_TLS:
951fd09b
AM
13033 if (tls_mask != 0
13034 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13035 {
411e1bfb 13036 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
13037 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13038 if (insn == 0)
411e1bfb 13039 abort ();
411e1bfb 13040 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 13041 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13042 PPC64_TPREL16_LO which is at low-order half-word. */
13043 rel->r_offset += d_offset;
0d4792f7
AM
13044 r_type = R_PPC64_TPREL16_LO;
13045 if (toc_symndx != 0)
13046 {
13047 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13048 rel->r_addend = toc_addend;
0d4792f7
AM
13049 /* We changed the symbol. Start over in order to
13050 get h, sym, sec etc. right. */
13051 rel--;
13052 continue;
13053 }
13054 else
13055 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13056 }
13057 break;
13058
411e1bfb
AM
13059 case R_PPC64_GOT_TLSGD16_HI:
13060 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13061 tls_gd = TLS_TPRELGD;
13062 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13063 goto tls_gdld_hi;
13064 break;
13065
411e1bfb
AM
13066 case R_PPC64_GOT_TLSLD16_HI:
13067 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13068 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13069 {
951fd09b
AM
13070 tls_gdld_hi:
13071 if ((tls_mask & tls_gd) != 0)
13072 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13073 + R_PPC64_GOT_TPREL16_DS);
13074 else
411e1bfb 13075 {
4fe5ca5b 13076 rel->r_offset -= d_offset;
727ac201 13077 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 13078 r_type = R_PPC64_NONE;
411e1bfb 13079 }
951fd09b 13080 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13081 }
13082 break;
13083
951fd09b
AM
13084 case R_PPC64_GOT_TLSGD16:
13085 case R_PPC64_GOT_TLSGD16_LO:
13086 tls_gd = TLS_TPRELGD;
13087 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13088 goto tls_ldgd_opt;
951fd09b 13089 break;
411e1bfb 13090
951fd09b
AM
13091 case R_PPC64_GOT_TLSLD16:
13092 case R_PPC64_GOT_TLSLD16_LO:
13093 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13094 {
3a71aa26 13095 unsigned int insn1, insn2, insn3;
102890f0
AM
13096 bfd_vma offset;
13097
13098 tls_ldgd_opt:
727fc41e
AM
13099 offset = (bfd_vma) -1;
13100 /* If not using the newer R_PPC64_TLSGD/LD to mark
13101 __tls_get_addr calls, we must trust that the call
13102 stays with its arg setup insns, ie. that the next
13103 reloc is the __tls_get_addr call associated with
13104 the current reloc. Edit both insns. */
13105 if (input_section->has_tls_get_addr_call
13106 && rel + 1 < relend
13107 && branch_reloc_hash_match (input_bfd, rel + 1,
13108 htab->tls_get_addr,
13109 htab->tls_get_addr_fd))
13110 offset = rel[1].r_offset;
102890f0 13111 if ((tls_mask & tls_gd) != 0)
411e1bfb 13112 {
102890f0 13113 /* IE */
3a71aa26
AM
13114 insn1 = bfd_get_32 (output_bfd,
13115 contents + rel->r_offset - d_offset);
102890f0
AM
13116 insn1 &= (1 << 26) - (1 << 2);
13117 insn1 |= 58 << 26; /* ld */
13118 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13119 if (offset != (bfd_vma) -1)
f58d5a2d 13120 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13121 if ((tls_mask & TLS_EXPLICIT) == 0)
13122 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13123 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13124 else
102890f0
AM
13125 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13126 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13127 }
13128 else
13129 {
13130 /* LE */
13131 insn1 = 0x3c6d0000; /* addis 3,13,0 */
13132 insn2 = 0x38630000; /* addi 3,3,0 */
13133 if (tls_gd == 0)
951fd09b 13134 {
102890f0 13135 /* Was an LD reloc. */
1d483afe
AM
13136 if (toc_symndx)
13137 sec = local_sections[toc_symndx];
13138 for (r_symndx = 0;
13139 r_symndx < symtab_hdr->sh_info;
13140 r_symndx++)
13141 if (local_sections[r_symndx] == sec)
13142 break;
13143 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13144 r_symndx = STN_UNDEF;
102890f0 13145 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13146 if (r_symndx != STN_UNDEF)
1d483afe
AM
13147 rel->r_addend -= (local_syms[r_symndx].st_value
13148 + sec->output_offset
13149 + sec->output_section->vma);
951fd09b 13150 }
102890f0 13151 else if (toc_symndx != 0)
3a71aa26
AM
13152 {
13153 r_symndx = toc_symndx;
13154 rel->r_addend = toc_addend;
13155 }
102890f0
AM
13156 r_type = R_PPC64_TPREL16_HA;
13157 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13158 if (offset != (bfd_vma) -1)
13159 {
13160 rel[1].r_info = ELF64_R_INFO (r_symndx,
13161 R_PPC64_TPREL16_LO);
13162 rel[1].r_offset = offset + d_offset;
13163 rel[1].r_addend = rel->r_addend;
13164 }
102890f0 13165 }
3a71aa26
AM
13166 bfd_put_32 (output_bfd, insn1,
13167 contents + rel->r_offset - d_offset);
727fc41e
AM
13168 if (offset != (bfd_vma) -1)
13169 {
13170 insn3 = bfd_get_32 (output_bfd,
13171 contents + offset + 4);
13172 if (insn3 == NOP
13173 || insn3 == CROR_151515 || insn3 == CROR_313131)
13174 {
13175 rel[1].r_offset += 4;
13176 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13177 insn2 = NOP;
13178 }
13179 bfd_put_32 (output_bfd, insn2, contents + offset);
13180 }
13181 if ((tls_mask & tls_gd) == 0
13182 && (tls_gd == 0 || toc_symndx != 0))
13183 {
13184 /* We changed the symbol. Start over in order
13185 to get h, sym, sec etc. right. */
13186 rel--;
13187 continue;
13188 }
13189 }
13190 break;
13191
13192 case R_PPC64_TLSGD:
13193 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13194 {
13195 unsigned int insn2, insn3;
13196 bfd_vma offset = rel->r_offset;
13197
13198 if ((tls_mask & TLS_TPRELGD) != 0)
13199 {
13200 /* IE */
13201 r_type = R_PPC64_NONE;
13202 insn2 = 0x7c636a14; /* add 3,3,13 */
13203 }
13204 else
13205 {
13206 /* LE */
13207 if (toc_symndx != 0)
13208 {
13209 r_symndx = toc_symndx;
13210 rel->r_addend = toc_addend;
13211 }
13212 r_type = R_PPC64_TPREL16_LO;
13213 rel->r_offset = offset + d_offset;
13214 insn2 = 0x38630000; /* addi 3,3,0 */
13215 }
13216 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13217 /* Zap the reloc on the _tls_get_addr call too. */
13218 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13219 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
13220 insn3 = bfd_get_32 (output_bfd,
13221 contents + offset + 4);
102890f0
AM
13222 if (insn3 == NOP
13223 || insn3 == CROR_151515 || insn3 == CROR_313131)
13224 {
727fc41e 13225 rel->r_offset += 4;
3a71aa26
AM
13226 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13227 insn2 = NOP;
102890f0 13228 }
102890f0 13229 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 13230 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 13231 {
102890f0
AM
13232 rel--;
13233 continue;
411e1bfb 13234 }
411e1bfb 13235 }
411e1bfb
AM
13236 break;
13237
727fc41e
AM
13238 case R_PPC64_TLSLD:
13239 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13240 {
13241 unsigned int insn2, insn3;
13242 bfd_vma offset = rel->r_offset;
13243
13244 if (toc_symndx)
13245 sec = local_sections[toc_symndx];
13246 for (r_symndx = 0;
13247 r_symndx < symtab_hdr->sh_info;
13248 r_symndx++)
13249 if (local_sections[r_symndx] == sec)
13250 break;
13251 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13252 r_symndx = STN_UNDEF;
727fc41e 13253 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13254 if (r_symndx != STN_UNDEF)
727fc41e
AM
13255 rel->r_addend -= (local_syms[r_symndx].st_value
13256 + sec->output_offset
13257 + sec->output_section->vma);
13258
13259 r_type = R_PPC64_TPREL16_LO;
13260 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13261 rel->r_offset = offset + d_offset;
13262 /* Zap the reloc on the _tls_get_addr call too. */
13263 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13264 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
13265 insn2 = 0x38630000; /* addi 3,3,0 */
13266 insn3 = bfd_get_32 (output_bfd,
13267 contents + offset + 4);
13268 if (insn3 == NOP
13269 || insn3 == CROR_151515 || insn3 == CROR_313131)
13270 {
13271 rel->r_offset += 4;
13272 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13273 insn2 = NOP;
13274 }
13275 bfd_put_32 (output_bfd, insn2, contents + offset);
13276 rel--;
13277 continue;
13278 }
13279 break;
13280
411e1bfb 13281 case R_PPC64_DTPMOD64:
951fd09b
AM
13282 if (rel + 1 < relend
13283 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13284 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13285 {
951fd09b
AM
13286 if ((tls_mask & TLS_GD) == 0)
13287 {
13288 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13289 if ((tls_mask & TLS_TPRELGD) != 0)
13290 r_type = R_PPC64_TPREL64;
13291 else
13292 {
4ce794b7 13293 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13294 r_type = R_PPC64_NONE;
13295 }
13296 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13297 }
13298 }
13299 else
13300 {
13301 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13302 {
4ce794b7 13303 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13304 r_type = R_PPC64_NONE;
951fd09b 13305 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13306 }
411e1bfb
AM
13307 }
13308 break;
13309
13310 case R_PPC64_TPREL64:
951fd09b 13311 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13312 {
13313 r_type = R_PPC64_NONE;
13314 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13315 }
13316 break;
13317 }
13318
13319 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 13320 insn = 0;
b25116a9
AM
13321 max_br_offset = 1 << 25;
13322 addend = rel->r_addend;
bc30df16 13323 reloc_dest = DEST_NORMAL;
65f38f15 13324 switch (r_type)
5bd4f169
AM
13325 {
13326 default:
65f38f15 13327 break;
5bd4f169 13328
3b421ab3
AM
13329 case R_PPC64_TOCSAVE:
13330 if (relocation + addend == (rel->r_offset
13331 + input_section->output_offset
13332 + input_section->output_section->vma)
13333 && tocsave_find (htab, NO_INSERT,
13334 &local_syms, rel, input_bfd))
13335 {
13336 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13337 if (insn == NOP
13338 || insn == CROR_151515 || insn == CROR_313131)
13339 bfd_put_32 (input_bfd, STD_R2_40R1,
13340 contents + rel->r_offset);
13341 }
13342 break;
13343
65f38f15
AM
13344 /* Branch taken prediction relocations. */
13345 case R_PPC64_ADDR14_BRTAKEN:
13346 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
13347 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13348 /* Fall thru. */
65f38f15 13349
86c76c7b 13350 /* Branch not taken prediction relocations. */
65f38f15
AM
13351 case R_PPC64_ADDR14_BRNTAKEN:
13352 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
13353 insn |= bfd_get_32 (output_bfd,
13354 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 13355 /* Fall thru. */
86c76c7b 13356
b25116a9
AM
13357 case R_PPC64_REL14:
13358 max_br_offset = 1 << 15;
13359 /* Fall thru. */
5bd4f169 13360
65f38f15 13361 case R_PPC64_REL24:
ad8e1ba5
AM
13362 /* Calls to functions with a different TOC, such as calls to
13363 shared objects, need to alter the TOC pointer. This is
13364 done using a linkage stub. A REL24 branching to these
13365 linkage stubs needs to be followed by a nop, as the nop
13366 will be replaced with an instruction to restore the TOC
13367 base pointer. */
8387904d 13368 fdh = h;
b31867b6
AM
13369 if (h != NULL
13370 && h->oh != NULL
13371 && h->oh->is_func_descriptor)
13372 fdh = ppc_follow_link (h->oh);
31c76678
DK
13373 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13374 htab);
6abec6bc 13375 if (stub_entry != NULL
ad8e1ba5 13376 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 13377 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
13378 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13379 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 13380 {
b25116a9 13381 bfd_boolean can_plt_call = FALSE;
721956f4 13382
ba8ca3e7
AM
13383 /* All of these stubs will modify r2, so there must be a
13384 branch and link followed by a nop. The nop is
13385 replaced by an insn to restore r2. */
eea6121a 13386 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 13387 {
ba8ca3e7
AM
13388 unsigned long br;
13389
13390 br = bfd_get_32 (input_bfd,
13391 contents + rel->r_offset);
13392 if ((br & 1) != 0)
41bd81ab 13393 {
ba8ca3e7
AM
13394 unsigned long nop;
13395
13396 nop = bfd_get_32 (input_bfd,
13397 contents + rel->r_offset + 4);
13398 if (nop == NOP
13399 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 13400 {
ba8ca3e7
AM
13401 if (h != NULL
13402 && (h == htab->tls_get_addr_fd
13403 || h == htab->tls_get_addr)
13404 && !htab->no_tls_get_addr_opt)
13405 {
13406 /* Special stub used, leave nop alone. */
13407 }
13408 else
13409 bfd_put_32 (input_bfd, LD_R2_40R1,
13410 contents + rel->r_offset + 4);
13411 can_plt_call = TRUE;
a7f2871e 13412 }
41bd81ab 13413 }
5bd4f169 13414 }
721956f4 13415
ba8ca3e7 13416 if (!can_plt_call && h != NULL)
721956f4 13417 {
ba8ca3e7
AM
13418 const char *name = h->elf.root.root.string;
13419
13420 if (*name == '.')
13421 ++name;
13422
13423 if (strncmp (name, "__libc_start_main", 17) == 0
13424 && (name[17] == 0 || name[17] == '@'))
6ab189d5 13425 {
ba8ca3e7
AM
13426 /* Allow crt1 branch to go via a toc adjusting
13427 stub. Other calls that never return could do
13428 the same, if we could detect such. */
b25116a9 13429 can_plt_call = TRUE;
6ab189d5 13430 }
ba8ca3e7
AM
13431 }
13432
13433 if (!can_plt_call)
13434 {
13435 /* g++ as of 20130507 emits self-calls without a
13436 following nop. This is arguably wrong since we
13437 have conflicting information. On the one hand a
13438 global symbol and on the other a local call
13439 sequence, but don't error for this special case.
13440 It isn't possible to cheaply verify we have
13441 exactly such a call. Allow all calls to the same
13442 section. */
13443 asection *code_sec = sec;
13444
13445 if (get_opd_info (sec) != NULL)
ad8e1ba5 13446 {
ba8ca3e7
AM
13447 bfd_vma off = (relocation + addend
13448 - sec->output_section->vma
13449 - sec->output_offset);
bc30df16 13450
ba8ca3e7 13451 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 13452 }
ba8ca3e7
AM
13453 if (code_sec == input_section)
13454 can_plt_call = TRUE;
13455 }
13456
13457 if (!can_plt_call)
13458 {
13459 info->callbacks->einfo
13460 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13461 "recompile with -fPIC"),
13462 input_bfd, input_section, rel->r_offset, sym_name);
13463
13464 bfd_set_error (bfd_error_bad_value);
13465 ret = FALSE;
721956f4
AM
13466 }
13467
b25116a9 13468 if (can_plt_call
794e51c0
AM
13469 && (stub_entry->stub_type == ppc_stub_plt_call
13470 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
13471 unresolved_reloc = FALSE;
13472 }
13473
6abec6bc
AM
13474 if ((stub_entry == NULL
13475 || stub_entry->stub_type == ppc_stub_long_branch
13476 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
13477 && get_opd_info (sec) != NULL)
13478 {
13479 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
13480 bfd_vma off = (relocation + addend
13481 - sec->output_section->vma
13482 - sec->output_offset);
aef36ac1 13483 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
13484 if (dest != (bfd_vma) -1)
13485 {
13486 relocation = dest;
13487 addend = 0;
bc30df16 13488 reloc_dest = DEST_OPD;
8387904d
AM
13489 }
13490 }
13491
b25116a9
AM
13492 /* If the branch is out of reach we ought to have a long
13493 branch stub. */
13494 from = (rel->r_offset
13495 + input_section->output_offset
13496 + input_section->output_section->vma);
13497
6911b7dc
AM
13498 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13499 ? fdh->elf.other
13500 : sym->st_other);
13501
6abec6bc
AM
13502 if (stub_entry != NULL
13503 && (stub_entry->stub_type == ppc_stub_long_branch
13504 || stub_entry->stub_type == ppc_stub_plt_branch)
13505 && (r_type == R_PPC64_ADDR14_BRTAKEN
13506 || r_type == R_PPC64_ADDR14_BRNTAKEN
13507 || (relocation + addend - from + max_br_offset
13508 < 2 * max_br_offset)))
13509 /* Don't use the stub if this branch is in range. */
13510 stub_entry = NULL;
b25116a9
AM
13511
13512 if (stub_entry != NULL)
13513 {
13514 /* Munge up the value and addend so that we call the stub
13515 rather than the procedure directly. */
13516 relocation = (stub_entry->stub_offset
13517 + stub_entry->stub_sec->output_offset
13518 + stub_entry->stub_sec->output_section->vma);
13519 addend = 0;
bc30df16 13520 reloc_dest = DEST_STUB;
3b421ab3 13521
794e51c0
AM
13522 if ((stub_entry->stub_type == ppc_stub_plt_call
13523 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13524 && (ALWAYS_EMIT_R2SAVE
13525 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
13526 && rel + 1 < relend
13527 && rel[1].r_offset == rel->r_offset + 4
13528 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13529 relocation += 4;
b25116a9
AM
13530 }
13531
13532 if (insn != 0)
13533 {
794e51c0 13534 if (is_isa_v2)
721956f4 13535 {
b25116a9
AM
13536 /* Set 'a' bit. This is 0b00010 in BO field for branch
13537 on CR(BI) insns (BO == 001at or 011at), and 0b01000
13538 for branch on CTR insns (BO == 1a00t or 1a01t). */
13539 if ((insn & (0x14 << 21)) == (0x04 << 21))
13540 insn |= 0x02 << 21;
13541 else if ((insn & (0x14 << 21)) == (0x10 << 21))
13542 insn |= 0x08 << 21;
13543 else
13544 break;
13545 }
13546 else
13547 {
13548 /* Invert 'y' bit if not the default. */
4cc603a5 13549 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 13550 insn ^= 0x01 << 21;
721956f4 13551 }
b25116a9
AM
13552
13553 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 13554 }
e86ce104 13555
06da1e8e
AM
13556 /* NOP out calls to undefined weak functions.
13557 We can thus call a weak function without first
13558 checking whether the function is defined. */
b25116a9 13559 else if (h != NULL
039b3fef 13560 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 13561 && h->elf.dynindx == -1
b25116a9
AM
13562 && r_type == R_PPC64_REL24
13563 && relocation == 0
4cc603a5 13564 && addend == 0)
e86ce104 13565 {
06da1e8e
AM
13566 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13567 continue;
e86ce104 13568 }
65f38f15
AM
13569 break;
13570 }
5bd4f169 13571
65f38f15 13572 /* Set `addend'. */
411e1bfb 13573 tls_type = 0;
65f38f15
AM
13574 switch (r_type)
13575 {
13576 default:
25f53a85 13577 info->callbacks->einfo
bc30df16 13578 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 13579 input_bfd, (int) r_type, sym_name);
5bd4f169 13580
65f38f15 13581 bfd_set_error (bfd_error_bad_value);
b34976b6 13582 ret = FALSE;
65f38f15 13583 continue;
5bd4f169 13584
65f38f15 13585 case R_PPC64_NONE:
411e1bfb 13586 case R_PPC64_TLS:
727fc41e
AM
13587 case R_PPC64_TLSGD:
13588 case R_PPC64_TLSLD:
3b421ab3 13589 case R_PPC64_TOCSAVE:
04c9666a
AM
13590 case R_PPC64_GNU_VTINHERIT:
13591 case R_PPC64_GNU_VTENTRY:
65f38f15 13592 continue;
5bd4f169
AM
13593
13594 /* GOT16 relocations. Like an ADDR16 using the symbol's
13595 address in the GOT as relocation value instead of the
411e1bfb 13596 symbol's value itself. Also, create a GOT entry for the
5bd4f169 13597 symbol and put the symbol value there. */
411e1bfb
AM
13598 case R_PPC64_GOT_TLSGD16:
13599 case R_PPC64_GOT_TLSGD16_LO:
13600 case R_PPC64_GOT_TLSGD16_HI:
13601 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 13602 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
13603 goto dogot;
13604
13605 case R_PPC64_GOT_TLSLD16:
13606 case R_PPC64_GOT_TLSLD16_LO:
13607 case R_PPC64_GOT_TLSLD16_HI:
13608 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13609 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
13610 goto dogot;
13611
13612 case R_PPC64_GOT_TPREL16_DS:
13613 case R_PPC64_GOT_TPREL16_LO_DS:
13614 case R_PPC64_GOT_TPREL16_HI:
13615 case R_PPC64_GOT_TPREL16_HA:
13616 tls_type = TLS_TLS | TLS_TPREL;
13617 goto dogot;
13618
13619 case R_PPC64_GOT_DTPREL16_DS:
13620 case R_PPC64_GOT_DTPREL16_LO_DS:
13621 case R_PPC64_GOT_DTPREL16_HI:
13622 case R_PPC64_GOT_DTPREL16_HA:
13623 tls_type = TLS_TLS | TLS_DTPREL;
13624 goto dogot;
13625
65f38f15
AM
13626 case R_PPC64_GOT16:
13627 case R_PPC64_GOT16_LO:
13628 case R_PPC64_GOT16_HI:
13629 case R_PPC64_GOT16_HA:
13630 case R_PPC64_GOT16_DS:
13631 case R_PPC64_GOT16_LO_DS:
411e1bfb 13632 dogot:
5bd4f169
AM
13633 {
13634 /* Relocation is to the entry for this symbol in the global
13635 offset table. */
e717da7e 13636 asection *got;
d881513a 13637 bfd_vma *offp;
5bd4f169 13638 bfd_vma off;
d881513a 13639 unsigned long indx = 0;
927be08e 13640 struct got_entry *ent;
65f38f15 13641
d881513a
AM
13642 if (tls_type == (TLS_TLS | TLS_LD)
13643 && (h == NULL
f5385ebf 13644 || !h->elf.def_dynamic))
927be08e 13645 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 13646 else
5bd4f169 13647 {
5bd4f169 13648
d881513a
AM
13649 if (h != NULL)
13650 {
13651 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
13652 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13653 &h->elf)
d881513a 13654 || (info->shared
726d3ab0 13655 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
13656 /* This is actually a static link, or it is a
13657 -Bsymbolic link and the symbol is defined
13658 locally, or the symbol was forced to be local
13659 because of a version file. */
13660 ;
13661 else
13662 {
14acf4dc 13663 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 13664 indx = h->elf.dynindx;
d881513a
AM
13665 unresolved_reloc = FALSE;
13666 }
039b3fef 13667 ent = h->elf.got.glist;
d881513a 13668 }
411e1bfb 13669 else
5bd4f169 13670 {
d881513a
AM
13671 if (local_got_ents == NULL)
13672 abort ();
13673 ent = local_got_ents[r_symndx];
5bd4f169 13674 }
d881513a
AM
13675
13676 for (; ent != NULL; ent = ent->next)
31c76678 13677 if (ent->addend == orig_rel.r_addend
e717da7e 13678 && ent->owner == input_bfd
d881513a
AM
13679 && ent->tls_type == tls_type)
13680 break;
5bd4f169 13681 }
411e1bfb 13682
927be08e
AM
13683 if (ent == NULL)
13684 abort ();
13685 if (ent->is_indirect)
13686 ent = ent->got.ent;
13687 offp = &ent->got.offset;
13688 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
13689 if (got == NULL)
13690 abort ();
13691
411e1bfb
AM
13692 /* The offset must always be a multiple of 8. We use the
13693 least significant bit to record whether we have already
13694 processed this entry. */
d881513a 13695 off = *offp;
411e1bfb
AM
13696 if ((off & 1) != 0)
13697 off &= ~1;
5bd4f169
AM
13698 else
13699 {
411e1bfb
AM
13700 /* Generate relocs for the dynamic linker, except in
13701 the case of TLSLD where we'll use one entry per
13702 module. */
25f23106
AM
13703 asection *relgot;
13704 bfd_boolean ifunc;
e717da7e 13705
d881513a 13706 *offp = off | 1;
25f23106
AM
13707 relgot = NULL;
13708 ifunc = (h != NULL
13709 ? h->elf.type == STT_GNU_IFUNC
13710 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 13711 if (ifunc)
25f23106 13712 relgot = htab->reliplt;
19e08130
AM
13713 else if ((info->shared || indx != 0)
13714 && (h == NULL
13715 || (tls_type == (TLS_TLS | TLS_LD)
13716 && !h->elf.def_dynamic)
13717 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13718 || h->elf.root.type != bfd_link_hash_undefweak))
13719 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 13720 if (relgot != NULL)
5bd4f169 13721 {
e717da7e
AM
13722 outrel.r_offset = (got->output_section->vma
13723 + got->output_offset
411e1bfb 13724 + off);
4cc603a5 13725 outrel.r_addend = addend;
d881513a 13726 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 13727 {
411e1bfb 13728 outrel.r_addend = 0;
e515b051 13729 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
13730 if (tls_type == (TLS_TLS | TLS_GD))
13731 {
e717da7e
AM
13732 loc = relgot->contents;
13733 loc += (relgot->reloc_count++
d881513a
AM
13734 * sizeof (Elf64_External_Rela));
13735 bfd_elf64_swap_reloca_out (output_bfd,
13736 &outrel, loc);
e515b051 13737 outrel.r_offset += 8;
4cc603a5 13738 outrel.r_addend = addend;
d881513a
AM
13739 outrel.r_info
13740 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 13741 }
411e1bfb 13742 }
951fd09b 13743 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 13744 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 13745 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13746 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
13747 else if (indx != 0)
13748 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13749 else
81407a69 13750 {
25f23106
AM
13751 if (ifunc)
13752 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13753 else
13754 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
13755
13756 /* Write the .got section contents for the sake
13757 of prelink. */
e717da7e 13758 loc = got->contents + off;
23fbd6fa
JJ
13759 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13760 loc);
81407a69 13761 }
81407a69
AM
13762
13763 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
13764 {
13765 outrel.r_addend += relocation;
13766 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 13767 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 13768 }
e717da7e
AM
13769 loc = relgot->contents;
13770 loc += (relgot->reloc_count++
411e1bfb
AM
13771 * sizeof (Elf64_External_Rela));
13772 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13773 }
13774
ad8e1ba5 13775 /* Init the .got section contents here if we're not
81407a69 13776 emitting a reloc. */
d881513a 13777 else
411e1bfb 13778 {
4cc603a5 13779 relocation += addend;
7b609f53
AM
13780 if (tls_type == (TLS_TLS | TLS_LD))
13781 relocation = 1;
13782 else if (tls_type != 0)
411e1bfb 13783 {
e1918d23 13784 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 13785 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13786 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 13787
7b609f53
AM
13788 if (tls_type == (TLS_TLS | TLS_GD))
13789 {
13790 bfd_put_64 (output_bfd, relocation,
e717da7e 13791 got->contents + off + 8);
7b609f53
AM
13792 relocation = 1;
13793 }
411e1bfb 13794 }
7b609f53 13795
411e1bfb 13796 bfd_put_64 (output_bfd, relocation,
e717da7e 13797 got->contents + off);
5bd4f169
AM
13798 }
13799 }
13800
65f38f15
AM
13801 if (off >= (bfd_vma) -2)
13802 abort ();
13803
bf102f86
AM
13804 relocation = got->output_section->vma + got->output_offset + off;
13805 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 13806 }
65f38f15
AM
13807 break;
13808
13809 case R_PPC64_PLT16_HA:
13810 case R_PPC64_PLT16_HI:
13811 case R_PPC64_PLT16_LO:
13812 case R_PPC64_PLT32:
13813 case R_PPC64_PLT64:
13814 /* Relocation is to the entry for this symbol in the
13815 procedure linkage table. */
13816
13817 /* Resolve a PLT reloc against a local symbol directly,
13818 without using the procedure linkage table. */
13819 if (h == NULL)
13820 break;
13821
411e1bfb
AM
13822 /* It's possible that we didn't make a PLT entry for this
13823 symbol. This happens when statically linking PIC code,
13824 or when using -Bsymbolic. Go find a match if there is a
13825 PLT entry. */
4ce794b7 13826 if (htab->plt != NULL)
65f38f15 13827 {
411e1bfb 13828 struct plt_entry *ent;
039b3fef 13829 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
31c76678 13830 if (ent->addend == orig_rel.r_addend
411e1bfb
AM
13831 && ent->plt.offset != (bfd_vma) -1)
13832 {
4ce794b7
AM
13833 relocation = (htab->plt->output_section->vma
13834 + htab->plt->output_offset
411e1bfb
AM
13835 + ent->plt.offset);
13836 unresolved_reloc = FALSE;
13837 }
65f38f15 13838 }
65f38f15 13839 break;
5bd4f169 13840
0b13192e
AM
13841 case R_PPC64_TOC:
13842 /* Relocation value is TOC base. */
13843 relocation = TOCstart;
cf35638d 13844 if (r_symndx == STN_UNDEF)
0b13192e 13845 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
13846 else if (unresolved_reloc)
13847 ;
13848 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
13849 relocation += htab->stub_group[sec->id].toc_off;
13850 else
13851 unresolved_reloc = TRUE;
ab96bf03 13852 goto dodyn;
0b13192e 13853
5bd4f169
AM
13854 /* TOC16 relocs. We want the offset relative to the TOC base,
13855 which is the address of the start of the TOC plus 0x8000.
13856 The TOC consists of sections .got, .toc, .tocbss, and .plt,
13857 in this order. */
65f38f15
AM
13858 case R_PPC64_TOC16:
13859 case R_PPC64_TOC16_LO:
13860 case R_PPC64_TOC16_HI:
13861 case R_PPC64_TOC16_DS:
13862 case R_PPC64_TOC16_LO_DS:
13863 case R_PPC64_TOC16_HA:
ad8e1ba5 13864 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
13865 break;
13866
13867 /* Relocate against the beginning of the section. */
65f38f15
AM
13868 case R_PPC64_SECTOFF:
13869 case R_PPC64_SECTOFF_LO:
13870 case R_PPC64_SECTOFF_HI:
13871 case R_PPC64_SECTOFF_DS:
13872 case R_PPC64_SECTOFF_LO_DS:
13873 case R_PPC64_SECTOFF_HA:
4ce794b7 13874 if (sec != NULL)
65f38f15 13875 addend -= sec->output_section->vma;
5bd4f169
AM
13876 break;
13877
25f23106
AM
13878 case R_PPC64_REL16:
13879 case R_PPC64_REL16_LO:
13880 case R_PPC64_REL16_HI:
13881 case R_PPC64_REL16_HA:
13882 break;
13883
721956f4
AM
13884 case R_PPC64_REL14:
13885 case R_PPC64_REL14_BRNTAKEN:
13886 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
13887 case R_PPC64_REL24:
13888 break;
13889
411e1bfb
AM
13890 case R_PPC64_TPREL16:
13891 case R_PPC64_TPREL16_LO:
13892 case R_PPC64_TPREL16_HI:
13893 case R_PPC64_TPREL16_HA:
13894 case R_PPC64_TPREL16_DS:
13895 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
13896 case R_PPC64_TPREL16_HIGH:
13897 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
13898 case R_PPC64_TPREL16_HIGHER:
13899 case R_PPC64_TPREL16_HIGHERA:
13900 case R_PPC64_TPREL16_HIGHEST:
13901 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
13902 if (h != NULL
13903 && h->elf.root.type == bfd_link_hash_undefweak
13904 && h->elf.dynindx == -1)
13905 {
13906 /* Make this relocation against an undefined weak symbol
13907 resolve to zero. This is really just a tweak, since
13908 code using weak externs ought to check that they are
13909 defined before using them. */
13910 bfd_byte *p = contents + rel->r_offset - d_offset;
13911
13912 insn = bfd_get_32 (output_bfd, p);
13913 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13914 if (insn != 0)
13915 bfd_put_32 (output_bfd, insn, p);
13916 break;
13917 }
e1918d23 13918 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13919 if (info->shared)
13920 /* The TPREL16 relocs shouldn't really be used in shared
13921 libs as they will result in DT_TEXTREL being set, but
13922 support them anyway. */
13923 goto dodyn;
13924 break;
13925
13926 case R_PPC64_DTPREL16:
13927 case R_PPC64_DTPREL16_LO:
13928 case R_PPC64_DTPREL16_HI:
13929 case R_PPC64_DTPREL16_HA:
13930 case R_PPC64_DTPREL16_DS:
13931 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
13932 case R_PPC64_DTPREL16_HIGH:
13933 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
13934 case R_PPC64_DTPREL16_HIGHER:
13935 case R_PPC64_DTPREL16_HIGHERA:
13936 case R_PPC64_DTPREL16_HIGHEST:
13937 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 13938 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13939 break;
13940
e515b051
AM
13941 case R_PPC64_DTPMOD64:
13942 relocation = 1;
13943 addend = 0;
13944 goto dodyn;
13945
411e1bfb 13946 case R_PPC64_TPREL64:
e1918d23 13947 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13948 goto dodyn;
13949
13950 case R_PPC64_DTPREL64:
e1918d23 13951 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13952 /* Fall thru */
13953
65f38f15
AM
13954 /* Relocations that may need to be propagated if this is a
13955 dynamic object. */
04c9666a 13956 case R_PPC64_REL30:
65f38f15
AM
13957 case R_PPC64_REL32:
13958 case R_PPC64_REL64:
13959 case R_PPC64_ADDR14:
13960 case R_PPC64_ADDR14_BRNTAKEN:
13961 case R_PPC64_ADDR14_BRTAKEN:
13962 case R_PPC64_ADDR16:
13963 case R_PPC64_ADDR16_DS:
13964 case R_PPC64_ADDR16_HA:
13965 case R_PPC64_ADDR16_HI:
f9c6b907
AM
13966 case R_PPC64_ADDR16_HIGH:
13967 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
13968 case R_PPC64_ADDR16_HIGHER:
13969 case R_PPC64_ADDR16_HIGHERA:
13970 case R_PPC64_ADDR16_HIGHEST:
13971 case R_PPC64_ADDR16_HIGHESTA:
13972 case R_PPC64_ADDR16_LO:
13973 case R_PPC64_ADDR16_LO_DS:
13974 case R_PPC64_ADDR24:
65f38f15
AM
13975 case R_PPC64_ADDR32:
13976 case R_PPC64_ADDR64:
13977 case R_PPC64_UADDR16:
13978 case R_PPC64_UADDR32:
13979 case R_PPC64_UADDR64:
411e1bfb 13980 dodyn:
5d1634d7 13981 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
13982 break;
13983
41bd81ab
AM
13984 if (NO_OPD_RELOCS && is_opd)
13985 break;
13986
65f38f15 13987 if ((info->shared
4e795f50 13988 && (h == NULL
039b3fef
AM
13989 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13990 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 13991 && (must_be_dyn_reloc (info, r_type)
039b3fef 13992 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
13993 || (ELIMINATE_COPY_RELOCS
13994 && !info->shared
65f38f15 13995 && h != NULL
039b3fef 13996 && h->elf.dynindx != -1
f5385ebf 13997 && !h->elf.non_got_ref
25f23106
AM
13998 && !h->elf.def_regular)
13999 || (!info->shared
14000 && (h != NULL
14001 ? h->elf.type == STT_GNU_IFUNC
14002 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 14003 {
b34976b6 14004 bfd_boolean skip, relocate;
65f38f15 14005 asection *sreloc;
1cf1f670 14006 bfd_vma out_off;
65f38f15
AM
14007
14008 /* When generating a dynamic object, these relocations
14009 are copied into the output file to be resolved at run
14010 time. */
14011
b34976b6
AM
14012 skip = FALSE;
14013 relocate = FALSE;
65f38f15 14014
1cf1f670
AM
14015 out_off = _bfd_elf_section_offset (output_bfd, info,
14016 input_section, rel->r_offset);
14017 if (out_off == (bfd_vma) -1)
b34976b6 14018 skip = TRUE;
1cf1f670 14019 else if (out_off == (bfd_vma) -2)
b34976b6 14020 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14021 out_off += (input_section->output_section->vma
14022 + input_section->output_offset);
14023 outrel.r_offset = out_off;
411e1bfb 14024 outrel.r_addend = rel->r_addend;
65f38f15 14025
1cf1f670
AM
14026 /* Optimize unaligned reloc use. */
14027 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14028 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14029 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14030 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14031 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14032 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14033 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14034 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14035 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14036
65f38f15 14037 if (skip)
0bb2d96a 14038 memset (&outrel, 0, sizeof outrel);
726d3ab0 14039 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
14040 && !is_opd
14041 && r_type != R_PPC64_TOC)
14acf4dc
MR
14042 {
14043 BFD_ASSERT (h->elf.dynindx != -1);
14044 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14045 }
65f38f15
AM
14046 else
14047 {
41bd81ab
AM
14048 /* This symbol is local, or marked to become local,
14049 or this is an opd section reloc which must point
14050 at a local function. */
65f38f15 14051 outrel.r_addend += relocation;
e86ce104 14052 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14053 {
3fad3c7c 14054 if (is_opd && h != NULL)
afbe61cf
AM
14055 {
14056 /* Lie about opd entries. This case occurs
14057 when building shared libraries and we
14058 reference a function in another shared
3fad3c7c
AM
14059 lib. The same thing happens for a weak
14060 definition in an application that's
14061 overridden by a strong definition in a
14062 shared lib. (I believe this is a generic
14063 bug in binutils handling of weak syms.)
14064 In these cases we won't use the opd
1e2f5b6e 14065 entry in this lib. */
b34976b6 14066 unresolved_reloc = FALSE;
afbe61cf 14067 }
25f23106
AM
14068 if (!is_opd
14069 && r_type == R_PPC64_ADDR64
14070 && (h != NULL
14071 ? h->elf.type == STT_GNU_IFUNC
14072 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14073 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14074 else
14075 {
14076 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14077
25f23106
AM
14078 /* We need to relocate .opd contents for ld.so.
14079 Prelink also wants simple and consistent rules
14080 for relocs. This make all RELATIVE relocs have
14081 *r_offset equal to r_addend. */
14082 relocate = TRUE;
14083 }
65f38f15
AM
14084 }
14085 else
14086 {
14087 long indx = 0;
14088
25f23106
AM
14089 if (h != NULL
14090 ? h->elf.type == STT_GNU_IFUNC
14091 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14092 {
25f53a85 14093 info->callbacks->einfo
bc30df16
AM
14094 (_("%P: %H: %s for indirect "
14095 "function `%T' unsupported\n"),
25f53a85 14096 input_bfd, input_section, rel->r_offset,
25f23106
AM
14097 ppc64_elf_howto_table[r_type]->name,
14098 sym_name);
14099 ret = FALSE;
14100 }
cf35638d 14101 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14102 ;
14103 else if (sec == NULL || sec->owner == NULL)
14104 {
14105 bfd_set_error (bfd_error_bad_value);
b34976b6 14106 return FALSE;
65f38f15
AM
14107 }
14108 else
14109 {
14110 asection *osec;
14111
14112 osec = sec->output_section;
14113 indx = elf_section_data (osec)->dynindx;
14114
74541ad4
AM
14115 if (indx == 0)
14116 {
14117 if ((osec->flags & SEC_READONLY) == 0
14118 && htab->elf.data_index_section != NULL)
14119 osec = htab->elf.data_index_section;
14120 else
14121 osec = htab->elf.text_index_section;
14122 indx = elf_section_data (osec)->dynindx;
14123 }
14124 BFD_ASSERT (indx != 0);
14125
65f38f15
AM
14126 /* We are turning this relocation into one
14127 against a section symbol, so subtract out
14128 the output section's address but not the
14129 offset of the input section in the output
14130 section. */
14131 outrel.r_addend -= osec->vma;
14132 }
14133
14134 outrel.r_info = ELF64_R_INFO (indx, r_type);
14135 }
14136 }
14137
14138 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14139 if (h != NULL
14140 ? h->elf.type == STT_GNU_IFUNC
14141 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
25f23106 14142 sreloc = htab->reliplt;
65f38f15
AM
14143 if (sreloc == NULL)
14144 abort ();
14145
dfbb6ac9
AM
14146 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14147 >= sreloc->size)
14148 abort ();
947216bf
AM
14149 loc = sreloc->contents;
14150 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14151 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14152
14153 /* If this reloc is against an external symbol, it will
14154 be computed at runtime, so there's no need to do
81407a69
AM
14155 anything now. However, for the sake of prelink ensure
14156 that the section contents are a known value. */
65f38f15 14157 if (! relocate)
81407a69
AM
14158 {
14159 unresolved_reloc = FALSE;
14160 /* The value chosen here is quite arbitrary as ld.so
14161 ignores section contents except for the special
14162 case of .opd where the contents might be accessed
14163 before relocation. Choose zero, as that won't
14164 cause reloc overflow. */
14165 relocation = 0;
14166 addend = 0;
14167 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14168 to improve backward compatibility with older
14169 versions of ld. */
14170 if (r_type == R_PPC64_ADDR64)
14171 addend = outrel.r_addend;
14172 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14173 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14174 addend = (input_section->output_section->vma
14175 + input_section->output_offset
14176 + rel->r_offset);
81407a69 14177 }
65f38f15 14178 }
5bd4f169
AM
14179 break;
14180
65f38f15
AM
14181 case R_PPC64_COPY:
14182 case R_PPC64_GLOB_DAT:
14183 case R_PPC64_JMP_SLOT:
25f23106 14184 case R_PPC64_JMP_IREL:
65f38f15
AM
14185 case R_PPC64_RELATIVE:
14186 /* We shouldn't ever see these dynamic relocs in relocatable
14187 files. */
ae9a127f 14188 /* Fall through. */
65f38f15
AM
14189
14190 case R_PPC64_PLTGOT16:
14191 case R_PPC64_PLTGOT16_DS:
14192 case R_PPC64_PLTGOT16_HA:
14193 case R_PPC64_PLTGOT16_HI:
14194 case R_PPC64_PLTGOT16_LO:
14195 case R_PPC64_PLTGOT16_LO_DS:
14196 case R_PPC64_PLTREL32:
14197 case R_PPC64_PLTREL64:
14198 /* These ones haven't been implemented yet. */
14199
25f53a85 14200 info->callbacks->einfo
bc30df16 14201 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14202 input_bfd,
4ce794b7 14203 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14204
14205 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14206 ret = FALSE;
5bd4f169 14207 continue;
65f38f15 14208 }
5bd4f169 14209
67f0cbdb
AM
14210 /* Multi-instruction sequences that access the TOC can be
14211 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14212 to nop; addi rb,r2,x; */
14213 switch (r_type)
14214 {
14215 default:
14216 break;
14217
14218 case R_PPC64_GOT_TLSLD16_HI:
14219 case R_PPC64_GOT_TLSGD16_HI:
14220 case R_PPC64_GOT_TPREL16_HI:
14221 case R_PPC64_GOT_DTPREL16_HI:
14222 case R_PPC64_GOT16_HI:
14223 case R_PPC64_TOC16_HI:
14224 /* These relocs would only be useful if building up an
14225 offset to later add to r2, perhaps in an indexed
14226 addressing mode instruction. Don't try to optimize.
14227 Unfortunately, the possibility of someone building up an
14228 offset like this or even with the HA relocs, means that
14229 we need to check the high insn when optimizing the low
14230 insn. */
14231 break;
14232
14233 case R_PPC64_GOT_TLSLD16_HA:
14234 case R_PPC64_GOT_TLSGD16_HA:
14235 case R_PPC64_GOT_TPREL16_HA:
14236 case R_PPC64_GOT_DTPREL16_HA:
14237 case R_PPC64_GOT16_HA:
14238 case R_PPC64_TOC16_HA:
98528052 14239 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14240 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
14241 {
14242 bfd_byte *p = contents + (rel->r_offset & ~3);
14243 bfd_put_32 (input_bfd, NOP, p);
14244 }
67f0cbdb
AM
14245 break;
14246
14247 case R_PPC64_GOT_TLSLD16_LO:
14248 case R_PPC64_GOT_TLSGD16_LO:
14249 case R_PPC64_GOT_TPREL16_LO_DS:
14250 case R_PPC64_GOT_DTPREL16_LO_DS:
14251 case R_PPC64_GOT16_LO:
14252 case R_PPC64_GOT16_LO_DS:
14253 case R_PPC64_TOC16_LO:
14254 case R_PPC64_TOC16_LO_DS:
98528052 14255 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14256 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
14257 {
14258 bfd_byte *p = contents + (rel->r_offset & ~3);
14259 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
14260 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14261 {
14262 /* Transform addic to addi when we change reg. */
14263 insn &= ~((0x3f << 26) | (0x1f << 16));
14264 insn |= (14u << 26) | (2 << 16);
14265 }
14266 else
67f0cbdb 14267 {
98528052
AM
14268 insn &= ~(0x1f << 16);
14269 insn |= 2 << 16;
67f0cbdb 14270 }
560c8763 14271 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
14272 }
14273 break;
14274 }
14275
65f38f15
AM
14276 /* Do any further special processing. */
14277 switch (r_type)
14278 {
14279 default:
14280 break;
14281
25f23106 14282 case R_PPC64_REL16_HA:
f9c6b907
AM
14283 case R_PPC64_ADDR16_HA:
14284 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14285 case R_PPC64_ADDR16_HIGHERA:
14286 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
14287 case R_PPC64_TOC16_HA:
14288 case R_PPC64_SECTOFF_HA:
411e1bfb 14289 case R_PPC64_TPREL16_HA:
f9c6b907 14290 case R_PPC64_TPREL16_HIGHA:
411e1bfb 14291 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 14292 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
14293 case R_PPC64_DTPREL16_HA:
14294 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 14295 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 14296 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
14297 /* It's just possible that this symbol is a weak symbol
14298 that's not actually defined anywhere. In that case,
14299 'sec' would be NULL, and we should leave the symbol
14300 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
14301 if (sec == NULL)
14302 break;
14303 /* Fall thru */
14304
14305 case R_PPC64_GOT16_HA:
14306 case R_PPC64_PLTGOT16_HA:
14307 case R_PPC64_PLT16_HA:
14308 case R_PPC64_GOT_TLSGD16_HA:
14309 case R_PPC64_GOT_TLSLD16_HA:
14310 case R_PPC64_GOT_TPREL16_HA:
14311 case R_PPC64_GOT_DTPREL16_HA:
14312 /* Add 0x10000 if sign bit in 0:15 is set.
14313 Bits 0:15 are not used. */
14314 addend += 0x8000;
65f38f15
AM
14315 break;
14316
14317 case R_PPC64_ADDR16_DS:
14318 case R_PPC64_ADDR16_LO_DS:
14319 case R_PPC64_GOT16_DS:
14320 case R_PPC64_GOT16_LO_DS:
14321 case R_PPC64_PLT16_LO_DS:
14322 case R_PPC64_SECTOFF_DS:
14323 case R_PPC64_SECTOFF_LO_DS:
14324 case R_PPC64_TOC16_DS:
14325 case R_PPC64_TOC16_LO_DS:
14326 case R_PPC64_PLTGOT16_DS:
14327 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
14328 case R_PPC64_GOT_TPREL16_DS:
14329 case R_PPC64_GOT_TPREL16_LO_DS:
14330 case R_PPC64_GOT_DTPREL16_DS:
14331 case R_PPC64_GOT_DTPREL16_LO_DS:
14332 case R_PPC64_TPREL16_DS:
14333 case R_PPC64_TPREL16_LO_DS:
14334 case R_PPC64_DTPREL16_DS:
14335 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
14336 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14337 mask = 3;
14338 /* If this reloc is against an lq insn, then the value must be
14339 a multiple of 16. This is somewhat of a hack, but the
14340 "correct" way to do this by defining _DQ forms of all the
14341 _DS relocs bloats all reloc switches in this file. It
14342 doesn't seem to make much sense to use any of these relocs
14343 in data, so testing the insn should be safe. */
494dac0c 14344 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
14345 mask = 15;
14346 if (((relocation + addend) & mask) != 0)
65f38f15 14347 {
25f53a85 14348 info->callbacks->einfo
8de848d8 14349 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 14350 input_bfd, input_section, rel->r_offset,
4ce794b7 14351 ppc64_elf_howto_table[r_type]->name,
adadcc0c 14352 mask + 1);
65f38f15 14353 bfd_set_error (bfd_error_bad_value);
b34976b6 14354 ret = FALSE;
65f38f15
AM
14355 continue;
14356 }
14357 break;
5bd4f169
AM
14358 }
14359
239e1f3a
AM
14360 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14361 because such sections are not SEC_ALLOC and thus ld.so will
14362 not process them. */
65f38f15 14363 if (unresolved_reloc
239e1f3a 14364 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
14365 && h->elf.def_dynamic)
14366 && _bfd_elf_section_offset (output_bfd, info, input_section,
14367 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 14368 {
25f53a85 14369 info->callbacks->einfo
bc30df16 14370 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 14371 input_bfd, input_section, rel->r_offset,
7b609f53 14372 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 14373 h->elf.root.root.string);
b34976b6 14374 ret = FALSE;
9c07fe7c 14375 }
5bd4f169 14376
65f38f15 14377 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
14378 input_bfd,
14379 input_section,
14380 contents,
411e1bfb 14381 rel->r_offset,
5bd4f169
AM
14382 relocation,
14383 addend);
14384
ef60b7ff 14385 if (r != bfd_reloc_ok)
5bd4f169 14386 {
bc30df16
AM
14387 char *more_info = NULL;
14388 const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14389
14390 if (reloc_dest != DEST_NORMAL)
14391 {
14392 more_info = bfd_malloc (strlen (reloc_name) + 8);
14393 if (more_info != NULL)
14394 {
14395 strcpy (more_info, reloc_name);
14396 strcat (more_info, (reloc_dest == DEST_OPD
14397 ? " (OPD)" : " (stub)"));
14398 reloc_name = more_info;
14399 }
14400 }
14401
cd27b276 14402 if (r == bfd_reloc_overflow)
5bd4f169 14403 {
cd27b276
AM
14404 if (warned)
14405 continue;
14406 if (h != NULL
039b3fef 14407 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 14408 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
14409 {
14410 /* Assume this is a call protected by other code that
14411 detects the symbol is undefined. If this is the case,
14412 we can safely ignore the overflow. If not, the
14413 program is hosed anyway, and a little warning isn't
14414 going to help. */
14415
14416 continue;
14417 }
14418
ef60b7ff 14419 if (!((*info->callbacks->reloc_overflow)
bc30df16
AM
14420 (info, &h->elf.root, sym_name,
14421 reloc_name, orig_rel.r_addend,
14422 input_bfd, input_section, rel->r_offset)))
b34976b6 14423 return FALSE;
ef60b7ff
AM
14424 }
14425 else
14426 {
25f53a85 14427 info->callbacks->einfo
bc30df16 14428 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 14429 input_bfd, input_section, rel->r_offset,
bc30df16 14430 reloc_name, sym_name, (int) r);
b34976b6 14431 ret = FALSE;
ef60b7ff 14432 }
bc30df16
AM
14433 if (more_info != NULL)
14434 free (more_info);
5bd4f169 14435 }
5bd4f169
AM
14436 }
14437
645ea6a9
AM
14438 /* If we're emitting relocations, then shortly after this function
14439 returns, reloc offsets and addends for this section will be
14440 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
14441 file rather than the input. Save a copy of the relocs for
14442 opd_entry_value. */
4cc603a5 14443 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
14444 {
14445 bfd_size_type amt;
14446 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14447 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
14448 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14449 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
14450 if (rel == NULL)
14451 return FALSE;
14452 memcpy (rel, relocs, amt);
14453 }
5bd4f169
AM
14454 return ret;
14455}
14456
754021d0
AM
14457/* Adjust the value of any local symbols in opd sections. */
14458
6e0b88f1 14459static int
754021d0
AM
14460ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14461 const char *name ATTRIBUTE_UNUSED,
14462 Elf_Internal_Sym *elfsym,
14463 asection *input_sec,
14464 struct elf_link_hash_entry *h)
14465{
74f0fb50
AM
14466 struct _opd_sec_data *opd;
14467 long adjust;
754021d0
AM
14468 bfd_vma value;
14469
4025353c 14470 if (h != NULL)
6e0b88f1 14471 return 1;
4025353c 14472
74f0fb50
AM
14473 opd = get_opd_info (input_sec);
14474 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 14475 return 1;
754021d0
AM
14476
14477 value = elfsym->st_value - input_sec->output_offset;
14478 if (!info->relocatable)
14479 value -= input_sec->output_section->vma;
14480
74f0fb50 14481 adjust = opd->adjust[value / 8];
4025353c 14482 if (adjust == -1)
6e0b88f1
AM
14483 return 2;
14484
14485 elfsym->st_value += adjust;
14486 return 1;
754021d0
AM
14487}
14488
5bd4f169
AM
14489/* Finish up dynamic symbol handling. We set the contents of various
14490 dynamic sections here. */
14491
b34976b6 14492static bfd_boolean
4ce794b7
AM
14493ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14494 struct bfd_link_info *info,
14495 struct elf_link_hash_entry *h,
ab6dce23 14496 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 14497{
65f38f15 14498 struct ppc_link_hash_table *htab;
8387904d
AM
14499 struct plt_entry *ent;
14500 Elf_Internal_Rela rela;
14501 bfd_byte *loc;
5bd4f169 14502
65f38f15 14503 htab = ppc_hash_table (info);
4dfe6ac6
NC
14504 if (htab == NULL)
14505 return FALSE;
5bd4f169 14506
8387904d
AM
14507 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14508 if (ent->plt.offset != (bfd_vma) -1)
14509 {
14510 /* This symbol has an entry in the procedure linkage
14511 table. Set it up. */
e054468f
AM
14512 if (!htab->elf.dynamic_sections_created
14513 || h->dynindx == -1)
14514 {
14515 BFD_ASSERT (h->type == STT_GNU_IFUNC
14516 && h->def_regular
14517 && (h->root.type == bfd_link_hash_defined
14518 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
14519 rela.r_offset = (htab->iplt->output_section->vma
14520 + htab->iplt->output_offset
14521 + ent->plt.offset);
ee67d69a
AM
14522 if (htab->opd_abi)
14523 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14524 else
14525 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
14526 rela.r_addend = (h->root.u.def.value
14527 + h->root.u.def.section->output_offset
14528 + h->root.u.def.section->output_section->vma
14529 + ent->addend);
25f23106
AM
14530 loc = (htab->reliplt->contents
14531 + (htab->reliplt->reloc_count++
14532 * sizeof (Elf64_External_Rela)));
e054468f
AM
14533 }
14534 else
14535 {
25f23106
AM
14536 rela.r_offset = (htab->plt->output_section->vma
14537 + htab->plt->output_offset
14538 + ent->plt.offset);
e054468f
AM
14539 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14540 rela.r_addend = ent->addend;
25f23106 14541 loc = (htab->relplt->contents
b9e5796b
AM
14542 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14543 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 14544 }
8387904d
AM
14545 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14546 }
5bd4f169 14547
f5385ebf 14548 if (h->needs_copy)
5bd4f169 14549 {
65f38f15 14550 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 14551
65f38f15
AM
14552 if (h->dynindx == -1
14553 || (h->root.type != bfd_link_hash_defined
14554 && h->root.type != bfd_link_hash_defweak)
4ce794b7 14555 || htab->relbss == NULL)
65f38f15 14556 abort ();
5bd4f169
AM
14557
14558 rela.r_offset = (h->root.u.def.value
14559 + h->root.u.def.section->output_section->vma
14560 + h->root.u.def.section->output_offset);
14561 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14562 rela.r_addend = 0;
4ce794b7
AM
14563 loc = htab->relbss->contents;
14564 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 14565 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
14566 }
14567
b34976b6 14568 return TRUE;
5bd4f169
AM
14569}
14570
65f38f15
AM
14571/* Used to decide how to sort relocs in an optimal manner for the
14572 dynamic linker, before writing them out. */
14573
14574static enum elf_reloc_type_class
7e612e98
AM
14575ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14576 const asection *rel_sec,
14577 const Elf_Internal_Rela *rela)
65f38f15 14578{
04c9666a 14579 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
14580 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14581
14582 if (rel_sec == htab->reliplt)
14583 return reloc_class_ifunc;
a33d1f77 14584
4ce794b7 14585 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 14586 switch (r_type)
65f38f15
AM
14587 {
14588 case R_PPC64_RELATIVE:
14589 return reloc_class_relative;
14590 case R_PPC64_JMP_SLOT:
14591 return reloc_class_plt;
14592 case R_PPC64_COPY:
14593 return reloc_class_copy;
14594 default:
14595 return reloc_class_normal;
14596 }
14597}
14598
5bd4f169
AM
14599/* Finish up the dynamic sections. */
14600
b34976b6 14601static bfd_boolean
4ce794b7
AM
14602ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14603 struct bfd_link_info *info)
5bd4f169 14604{
65f38f15
AM
14605 struct ppc_link_hash_table *htab;
14606 bfd *dynobj;
5bd4f169 14607 asection *sdyn;
5bd4f169 14608
65f38f15 14609 htab = ppc_hash_table (info);
4dfe6ac6
NC
14610 if (htab == NULL)
14611 return FALSE;
14612
65f38f15 14613 dynobj = htab->elf.dynobj;
3d4d4302 14614 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 14615
65f38f15 14616 if (htab->elf.dynamic_sections_created)
5bd4f169 14617 {
5bd4f169
AM
14618 Elf64_External_Dyn *dyncon, *dynconend;
14619
4ce794b7 14620 if (sdyn == NULL || htab->got == NULL)
65f38f15 14621 abort ();
5bd4f169
AM
14622
14623 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 14624 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
14625 for (; dyncon < dynconend; dyncon++)
14626 {
14627 Elf_Internal_Dyn dyn;
19397422 14628 asection *s;
5bd4f169
AM
14629
14630 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14631
14632 switch (dyn.d_tag)
14633 {
65f38f15
AM
14634 default:
14635 continue;
5bd4f169 14636
5d1634d7 14637 case DT_PPC64_GLINK:
4ce794b7 14638 s = htab->glink;
6348e046 14639 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
14640 /* We stupidly defined DT_PPC64_GLINK to be the start
14641 of glink rather than the first entry point, which is
14642 what ld.so needs, and now have a bigger stub to
14643 support automatic multiple TOCs. */
b9e5796b 14644 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
14645 break;
14646
19397422
AM
14647 case DT_PPC64_OPD:
14648 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
14649 if (s == NULL)
14650 continue;
14651 dyn.d_un.d_ptr = s->vma;
19397422
AM
14652 break;
14653
14654 case DT_PPC64_OPDSZ:
14655 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
14656 if (s == NULL)
14657 continue;
eea6121a 14658 dyn.d_un.d_val = s->size;
19397422
AM
14659 break;
14660
65f38f15 14661 case DT_PLTGOT:
4ce794b7 14662 s = htab->plt;
6348e046 14663 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
14664 break;
14665
14666 case DT_JMPREL:
4ce794b7 14667 s = htab->relplt;
6348e046 14668 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 14669 break;
5bd4f169 14670
65f38f15 14671 case DT_PLTRELSZ:
eea6121a 14672 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
14673 break;
14674
14675 case DT_RELASZ:
14676 /* Don't count procedure linkage table relocs in the
14677 overall reloc count. */
4ce794b7 14678 s = htab->relplt;
6348e046
AM
14679 if (s == NULL)
14680 continue;
eea6121a 14681 dyn.d_un.d_val -= s->size;
6348e046
AM
14682 break;
14683
14684 case DT_RELA:
14685 /* We may not be using the standard ELF linker script.
14686 If .rela.plt is the first .rela section, we adjust
14687 DT_RELA to not include it. */
4ce794b7 14688 s = htab->relplt;
6348e046
AM
14689 if (s == NULL)
14690 continue;
14691 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14692 continue;
eea6121a 14693 dyn.d_un.d_ptr += s->size;
65f38f15 14694 break;
5bd4f169 14695 }
5bd4f169 14696
65f38f15 14697 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 14698 }
5bd4f169
AM
14699 }
14700
eea6121a 14701 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
14702 {
14703 /* Fill in the first entry in the global offset table.
14704 We use it to hold the link-time TOCbase. */
14705 bfd_put_64 (output_bfd,
60ee0d4a 14706 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 14707 htab->got->contents);
5d1634d7
AM
14708
14709 /* Set .got entry size. */
4ce794b7 14710 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
14711 }
14712
eea6121a 14713 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
14714 {
14715 /* Set .plt entry size. */
4ce794b7 14716 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
b9e5796b 14717 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
14718 }
14719
84f5d08e
AM
14720 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14721 brlt ourselves if emitrelocations. */
14722 if (htab->brlt != NULL
14723 && htab->brlt->reloc_count != 0
14724 && !_bfd_elf_link_output_relocs (output_bfd,
14725 htab->brlt,
d4730f92 14726 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
14727 elf_section_data (htab->brlt)->relocs,
14728 NULL))
14729 return FALSE;
14730
176a0d42
AM
14731 if (htab->glink != NULL
14732 && htab->glink->reloc_count != 0
14733 && !_bfd_elf_link_output_relocs (output_bfd,
14734 htab->glink,
d4730f92 14735 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
14736 elf_section_data (htab->glink)->relocs,
14737 NULL))
14738 return FALSE;
14739
58d180e8
AM
14740
14741 if (htab->glink_eh_frame != NULL
dbaa2011 14742 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
58d180e8
AM
14743 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14744 htab->glink_eh_frame,
14745 htab->glink_eh_frame->contents))
14746 return FALSE;
14747
e717da7e 14748 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
14749 since we didn't add them to DYNOBJ. We know dynobj is the first
14750 bfd. */
e717da7e
AM
14751 while ((dynobj = dynobj->link_next) != NULL)
14752 {
14753 asection *s;
7b53ace3 14754
0c8d6e5c 14755 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
14756 continue;
14757
e717da7e
AM
14758 s = ppc64_elf_tdata (dynobj)->got;
14759 if (s != NULL
eea6121a 14760 && s->size != 0
e717da7e
AM
14761 && s->output_section != bfd_abs_section_ptr
14762 && !bfd_set_section_contents (output_bfd, s->output_section,
14763 s->contents, s->output_offset,
eea6121a 14764 s->size))
e717da7e
AM
14765 return FALSE;
14766 s = ppc64_elf_tdata (dynobj)->relgot;
14767 if (s != NULL
eea6121a 14768 && s->size != 0
e717da7e
AM
14769 && s->output_section != bfd_abs_section_ptr
14770 && !bfd_set_section_contents (output_bfd, s->output_section,
14771 s->contents, s->output_offset,
eea6121a 14772 s->size))
e717da7e
AM
14773 return FALSE;
14774 }
f6c52c13 14775
b34976b6 14776 return TRUE;
5bd4f169
AM
14777}
14778
5bd4f169 14779#include "elf64-target.h"
7b8e7dad
AM
14780
14781/* FreeBSD support */
14782
14783#undef TARGET_LITTLE_SYM
14784#undef TARGET_LITTLE_NAME
14785
14786#undef TARGET_BIG_SYM
14787#define TARGET_BIG_SYM bfd_elf64_powerpc_freebsd_vec
14788#undef TARGET_BIG_NAME
14789#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14790
14791#undef ELF_OSABI
14792#define ELF_OSABI ELFOSABI_FREEBSD
14793
14794#undef elf64_bed
14795#define elf64_bed elf64_powerpc_fbsd_bed
14796
14797#include "elf64-target.h"
14798
This page took 2.288373 seconds and 4 git commands to generate.